function submitPage(page, action, id){
	//alert(page);
	//alert(action);
	//alert(id);
	if(page == "menu" || page == 'search'){
		document.webform.fuseaction.value = action;
		document.webform.page.value = page;
		if(action == 'schoollist'){
			document.webform.school_id.value = id;
		}
		document.webform.submit();	
	}
	if(page == "data"){
		document.webform.fuseaction.value = action;
		document.webform.page.value = page;
		document.webform.inst_id.value = id;
		document.webform.submit();	
	}
}

function getSearchType(obj, page, action, id){
	//alert(obj.options[obj.selectedIndex].value);
	document.webform.search_by.value = obj.options[obj.selectedIndex].value;
	submitPage(page, action, id);
}

function getUser(obj, page, action, id){
	//alert(obj.options[obj.selectedIndex].value);
	document.webform.school_id.value = obj.options[obj.selectedIndex].value;
	submitPage(page, action, id);
}


function validate(theform, page, action, id){
	if (theform.contact_name.value == ""){
		alert("A contact name is required.");      
		theform.contact_name.focus();
		theform.contact_name.select();
		return false;
	}
	
	if (theform.street.value == ""){
		alert("A street address or box number is required.");
		theform.street.focus();
		theform.street.select();
		return false;
	}
	
	if (theform.city.value == ""){
		alert ("Please enter a city name.");
		theform.city.focus();
		theform.city.select();
		return false;
	}
	
	if (theform.zip.value == ""){
		alert ("Please enter a zip code.");
		theform.zip.focus();
		theform.zip.select();
		return false;
	}
	else if (!zipCodeFormat(theform.zip.value)){
		theform.zip.focus();
		theform.zip.select();
		return false;
	}
	
	if (theform.phone.value == ""){
		alert ("Please enter a phone number in the format of NNN-NNN-NNNN.");
		theform.phone.focus();
		theform.phone.select();
		return false;
	}
	else if (!phoneFaxFormat(theform.phone.value, "phone")){
		theform.phone.focus();
		return false;
	}
	
	if (theform.fax.value == ""){
		alert ("Please enter a fax number in the format of NNN-NNN-NNNN.");
		theform.fax.focus();
		theform.fax.select();
		return false;
	}
	else if (!phoneFaxFormat(theform.fax.value, "fax")){
		theform.fax.focus();
		return false;
	}
	
	if (theform.email.value!=""){
	   if (theform.email.value.indexOf("@")==-1 ||      
	       theform.email.value.indexOf(".")==-1 ||
	       theform.email.value.indexOf(" ")!=-1 ||        
	       theform.email.value.length<6) {
      	alert("Please check again to make sure your email address is valid.\n A valid email address must contain the \@ symbol, have a domain suffix, and be at least 5 characters long.");
        	return false;
      }  
	}
	
	if (!theform.international[0].checked && !theform.international[1].checked){
		alert("Please select Yes/No for international Students");
		return false;
	}
		
	if (theform.app_accept_date.value == ""){
		alert ("Please enter the earliest date applications will be accepted.");
		theform.app_accept_date.focus();
		theform.app_accept_date.select();
		return false;
	}
	
	if (theform.assignment_date.value == ""){
		alert ("Please enter the earliest date students will be assigned.");
		theform.assignment_date.focus();
		theform.assignment_date.select();
		return false;
	}
	
	if (!theform.applicat[0].checked && !theform.applicat[1].checked){
		alert("Please Select Yes/No for application fee");
		return false;
	}
	else if (theform.applicat[1].checked){
		if(theform.us_stu_fee.options[theform.us_stu_fee.selectedIndex].value  != "$0" ){
		 	alert("Please enter $0 for US students when you select \"No\" for application fee");
			theform.us_stu_fee.focus();
			return false;
		 }
		 if(theform.international_stu_fee.options[theform.international_stu_fee.selectedIndex].value  != "$0" &&  theform.international_stu_fee.options[theform.international_stu_fee.selectedIndex].value  != "na"){
		 	alert("Please enter $0 or NA for International students when you select \"No\" for application fee");
			theform.international_stu_fee.focus();
			return false;
		 }
	}
	else if(theform.applicat[0].checked){
		/* let us student select $0 even if the application fee is yes
		if(theform.us_stu_fee.options[theform.us_stu_fee.selectedIndex].value  == "$0" ){
		 	alert("Please don't enter $0 for US students when you select \"Yes\" for application fee");
			theform.us_stu_fee.focus();
			return false;
		 }
		 */
		 
		 if(theform.international_stu_fee.options[theform.international_stu_fee.selectedIndex].value  == "$0"){
		 	alert("Please don't enter $0 for International students when you select \"Yes\" for application fee");
			theform.international_stu_fee.focus();
			return false;
		 }
	}
	
	if (!theform.app_fee[0].checked && !theform.app_fee[1].checked){
		alert("Please Select Yes/No for application fee per elective");
		return false;
	}
	
	if (!theform.tuition[0].checked && !theform.tuition[1].checked){
		alert("Please Select Yes/No for tuition fee");
		return false;
	}
	else if (theform.tuition[1].checked && theform.tuition_amt.value != ""){
		alert("Please do not enter a tuition amount when you select \"No\"");
		theform.tuition_amt.focus();
		return false;
	}
	else if(theform.tuition[0].checked && theform.tuition_amt.value == ""){
		alert("Please enter a tuition amount when you select \"Yes\"");
		theform.tuition_amt.focus();
		return false;
	}
	
	if (!theform.tuition_per_elective[0].checked && !theform.tuition_per_elective[1].checked){
		alert("Please Select Yes/No for tutition charged per elective");
		theform.tuition_per_elective.focus();
		theform.tuition_per_elective.select();
		return false;
	}
	
	if (!theform.additional_fee[0].checked && !theform.additional_fee[1].checked){
		alert("Please Select Yes/No for additional fee after elective period");
		return false;
	}
	
	if (!theform.fee_for_health_plan[0].checked && !theform.fee_for_health_plan[1].checked){
		alert("Please Select Yes/No for additional required fee of health plan");
		return false;
	}
	else if (theform.fee_for_health_plan[1].checked && theform.health_plan_amt.value != ""){
		alert("Please do not enter a health plan amount when you select \"No\"");
		theform.health_plan_amt.focus();
		return false;
	}
	else if(theform.fee_for_health_plan[0].checked && theform.health_plan_amt.value == ""){
		alert("Please enter a health plan amount when you select \"Yes\"");
		theform.health_plan_amt.focus();
		return false;
	}
	
	if (theform.malpractice_amt.value == ""){
		alert ("Please enter the amount of malpractice insurance required.");
		theform.malpractice_amt.focus();
		theform.malpractice_amt.select();
		return false;
	}
	
	if (!theform.malpractice_insurance_pol[0].checked && !theform.malpractice_insurance_pol[1].checked){
		alert("Please Select Yes/No for malpractice insurance policy");
		return false;
	}
	else if (theform.malpractice_insurance_pol[1].checked){
		if(theform.malpractice_coverage.options[theform.malpractice_coverage.selectedIndex].value  != "$0" ){
		 	alert("Please enter $0 when you select \"No\" for malpractice insurance policy");
			theform.malpractice_coverage.focus();
			return false;
		 }
	}
	/*
	else if(theform.malpractice_insurance_pol[0].checked){
		if(theform.malpractice_coverage.options[theform.malpractice_coverage.selectedIndex].value  == "$0" ){
		 	alert("Please don't enter $0 when you select \"Yes\" for malpractice insurance policy");
			theform.malpractice_coverage.focus();
			return false;
		 }
	}
	*/
	if (!theform.per_elective[0].checked && !theform.per_elective[1].checked){
		alert("Please Select Yes/No for fee charged per elective");
		return false;
	}
	
	if (!theform.background_check[0].checked && !theform.background_check[1].checked){
		alert("Please Select Yes/No for Criminal Background Check");
		return false;
	}
	else if (theform.background_check[1].checked && theform.background_check_amt.value != ""){
		alert("Please do not enter a fee amount when you select \"No\"");
		theform.background_check_amt.focus();
		return false;
	}
	else if(theform.background_check[0].checked && theform.background_check_amt.value == ""){
		alert("Please enter a fee amount when you select \"Yes\"");
		theform.background_check_amt.focus();
		return false;
	}
	
	removeHypen(theform);
	submitPage(page, action, id);
	return true;
}

function removeHypen(theform){
	theform.phone.value=stripCharsNotInBag(theform.phone.value, '0123456789');
	theform.fax.value=stripCharsNotInBag(theform.fax.value, '0123456789');
}

// Removes all characters which do NOT appear in string bag 
// from string s.
function stripCharsNotInBag (s, bag)
{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}            

function IsAlphabetic(sText){
   var ValidChars = "abcdefghijklmnopqrstuvwxyz-' ";
   var IsAlphabetic = true;
   var AChar;
    
   for (i = 0; i < sText.length && IsAlphabetic == true; i++) 
   { 
      AChar = sText.charAt(i); 
	  AChar = AChar.toLowerCase();	  
   
      if (ValidChars.indexOf(AChar) == -1) 
      {
         IsAlphabetic = false;
      }
   }
   
   return IsAlphabetic;   
}

function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber = true;
   var Char;
    
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
   
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   
   return IsNumber;   
}

function zipCodeFormat(theString) {
	var zipF1 = /[0-9]{5}[-][0-9]{4}/;
	var zipF2 = /[0-9]{5}/;
	var zipF3 = /[A-Z][0-9][A-Z]\s[0-9][A-Z][0-9]/;
	
	if (theString.length != 10 && theString.length != 5 && theString.length != 7){
		alert("Please verify your zip/post code.  The one entered is not valid.");
		return false;
	}
	else if (theString.length == 10 && !theString.match(zipF1)){
		alert("Please verify your zip code.  The one entered is not valid.");
		return false;
	}
	else if (theString.length == 5 && !theString.match(zipF2)){
		alert("Please verify your zip code.  The one entered is not valid.");
		return false;
	}
	else if (theString.length == 7 && !theString.match(zipF3)){
		alert("Please verify your post code.  The one entered is not valid.");
		return false;
	}
	return true;
}

function phoneFaxFormat(theString, type){
	var phoneFaxF = /[0-9]{3}[-][0-9]{3}[-][0-9]{4}/;
	
	if (theString.length != 12){
		alert("Please verify your " + type + " number in the format of NNN-NNN-NNNN. The one entered is not valid.");
		return false;
	}
	else if (theString.length == 12 && !theString.match(phoneFaxF)){
		alert("Please verify your " + type + " number in the format of NNN-NNN-NNNN. The one entered is not valid.");
		return false;
	}
	return true;
}
	
	
	
