/*


if (theForm.application_address.value == "")
if (theForm.application_town.value == "")
if (theForm.application_county.value == "")
if (theForm.application_postcode.value == "")
if (theForm.application_country.value == "")

if (theForm.application_referee_1.value == "")
if (theForm.application_referee_1_relationship.value == "")
if (theForm.application_referee_2.value == "")
if (theForm.application_referee_2_relationship.value == "")

*/




function handleError() {
alert("error");
	return true;
}


//window.onerror = handleError;


var allowSubmit=true;


var formSelected="";

function alphaNumericCheck(str){
var regex=/^([a-zA-Z0-9_-]+)$/;
if(regex.test(str)){
return true;
} else {
return false;
}
}


var updateStrVar="";

opendaysArray=["number_people","age_child","select_dob_day","select_dob_month","select_dob_year","select_boarding","name_of_child","tour_yes","tour_no","year_of_entry","name","house","street","town","county","postcode","telephone","email","marketing","answer"];
prospectusArray=["select_title","first_name","surname","House_number_name","street_name","city_town","postal_code","country","home_telephone","select_dob_day","select_dob_month","select_dob_year","level_of_entry","answer"];
performingArray=["name_of_show","type_of_show","select_day","select_month","select_tickets","select_title","initials","surname","address","town","country","postcode","telephone","email"];
reportrequest3Array=["pupils_name","school","no_of_years_attended","exam_predictions","science_at_gcse_single","science_at_gcse_dual","science_at_gcse_triple","maths_position","english_position","academic_strengths_weaknesses","strengths_in_art_music","sporting_strengths","non_sporting_interests","health","general_personality","other_comments","name","email"];
reportrequest6Array=["pupils_name","school","no_of_years_attended","exam_predictions","learning_difficulties","academic_strengths_weaknesses","strengths_in_art_music","main_sporting_strengths","non_sporting_interests","health","general_personality","other_comments","name","email"];
teachingapp_1Array=["post","select_title","address","forenames","surname","town","county","country","nationality","age","phone","phone_eve","salary","email","current_school_company","current_position_held","ref1","profrelationship_1","ref2","profrelationship_2"];
//teachingapp_2Array=[""];
teachingapp_3Array=["name_ofschool_age_range","full_details_and_dates"];
teachingapp_4Array=["correct","answer"];
prepreportArray=["name","email","answer"];
preprepopendaysArray=["number_people","age_child","select_dob_day","select_dob_month","select_dob_year","name_of_child","year_of_entry","name","house","street","town","county","postcode","telephone","email","select_marketing","answer"];
nonapp_1Array=["non_post","non_select_title","non_surname","non_forenames","non_home_tel_no","non_email","non_nationality"];
nonapp_4Array=["ref1","ref1address","position1","ref2","ref2address","position2","list99_yes","list99_no","criminal_yes2","criminal_no2","certify","crbcheck","answer"];


function ScrollToElement(theElement){
  var selectedPosX = 0;
  var selectedPosY = 0;           
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }                   		      
 window.scrollTo(selectedPosX,selectedPosY-20);
}


var tempArray=new Array();
var fieldValue="";
var tmpRadioYes;
var tmpRadioNo;
var tmpRadio2Yes;
var tmpRadio2No;
var tmpCorrect;
var tmpCertify;
var tmpCrbCheck;

var tmpScienceAtGcseSingle;
var tmpScienceAtGcseDual;
var tmpScienceAtGcseTriple;

function addAddress(obj){
 	document.getElementById('hiddenaddress').style.display="block";
 	document.getElementById('submit_newaddress').style.display="none";

}

function addEmployer(obj){
 	document.getElementById('hiddenemployer').style.display="block";
 	document.getElementById('submit_newemployer').style.display="none";

}



function validateForm(obj){
if(obj.className=="prospectus")tempArray=prospectusArray;
if(obj.className=="opendays")tempArray=opendaysArray;
if(obj.className=="preprepopendays")tempArray=preprepopendaysArray;
if(obj.className=="performingarts")tempArray=performingArray;
if(obj.className=="reportrequest3")tempArray=reportrequest3Array;
if(obj.className=="reportrequest6")tempArray=reportrequest6Array;
if(obj.className=="prepreportrequest")tempArray=prepreportArray;
//alert(tempArray);
//if(obj.className=="teachingapp")tempArray=teachingappArray;

  	 for (var i = 0; i < tempArray.length; i++) {
  	 	fieldObj=document.getElementById(tempArray[i]);
  	 	fieldValue=fieldObj.value;	
  	 	if(fieldObj.type=="radio"){
			if (!tmpRadioYes) {tmpRadioYes=document.getElementById(fieldObj.name + "_yes")};
			if (!tmpRadioNo) {tmpRadioNo=document.getElementById(fieldObj.name + "_no")};
			if (!tmpRadio2Yes) { tmpRadio2Yes=document.getElementById(fieldObj.name + "_yes2")};
			if (!tmpRadio2No) {tmpRadio2No=document.getElementById(fieldObj.name + "_no2")};
			tmpScienceAtGcseSingle=document.getElementById(fieldObj.name + "_single");
			tmpScienceAtGcseDual=document.getElementById(fieldObj.name + "_dual");
			tmpScienceAtGcseTriple=document.getElementById(fieldObj.name + "_triple");
					
			if(tmpScienceAtGcseSingle){
			if((!tmpScienceAtGcseSingle.checked) && (!tmpScienceAtGcseDual.checked) && (!tmpScienceAtGcseTriple.checked)){
					fieldValue="";
					}
			}
			if(tmpRadioYes){
				if (tmpRadioYes.checked) {
					fieldValue = tmpRadioYes.value;
				}
				else if (tmpRadioNo.checked) {
					fieldValue = tmpRadioNo.value;
				}
				else {
					fieldValue = '';
				}
				//if((!tmpRadioYes.checked) && (!tmpRadioNo.checked)){
				//	fieldValue="";
				//	}				
			}
			if(tmpRadio2Yes){
				if (tmpRadio2Yes.checked) {
					fieldValue = tmpRadio2Yes.value;
				}
				else if (tmpRadio2No.checked) {
					fieldValue = tmpRadio2No.value;
				}
				else {
					fieldValue = '';
				}				
				//if((!tmpRadio2Yes.checked) && (!tmpRadio2No.checked)){
				//	fieldValue="";
				//	}				
			}
	
			
				
  	 	}
			else if (fieldObj.type == 'checkbox') {
				tmpCertify=document.getElementById(fieldObj.name);
			  tmpCrbCheck=document.getElementById(fieldObj.name);
			  tmpCorrect=document.getElementById(fieldObj.name);					
				
				if (tmpCertify) {
					if(!tmpCertify.checked){
						fieldValue="";
						}				
				}
				
				if (tmpCrbCheck) {
					if(!tmpCrbCheck.checked){
						fieldValue="";
						}				
				}
				
  			if(tmpCorrect){
				  if(!tmpCorrect.checked){
					fieldValue="";
					}
			  }			
			
			}
  	 	if(fieldObj.type=="select-one")fieldValue=fieldObj.options[fieldObj.selectedIndex].value;
			if((fieldValue=="") || 
			(fieldObj.getAttribute("checkphone") && !isNumber(Number(fieldObj.value)) || 
			(fieldObj.getAttribute("checkemail") && !isEmail(fieldValue)) || 
			(!isNumber(Number(fieldValue)) &&  fieldObj.getAttribute("checknumber") ||
			(fieldObj.getAttribute("checkpcode") && !checkPostCode(fieldValue)))
			)
			|| (fieldValue=="") ){
				fieldObj.style.border="solid 1px red";
				fieldObj.style.background="#fcf9b3";
				//alert(fieldObj.getAttribute("altlabel"));
				if(!fieldObj.getAttribute("altlabel")) document.getElementById(fieldObj.id + "_label").style.color="red"; else document.getElementById(fieldObj.getAttribute("altlabel") + "_label").style.color="red";
				if(fieldObj.type=="radio" && tmpRadioNo){
					tmpRadioNo.style.border="solid 1px red";
					tmpRadioNo.style.background="#fcf9b3";
					document.getElementById(fieldObj.getAttribute("altlabel")+"_label").style.color="red";
				}
				if(fieldObj.type=="radio" && tmpRadio2No){
					tmpRadio2No.style.border="solid 1px red";
					tmpRadio2No.style.background="#fcf9b3";
					document.getElementById(fieldObj.getAttribute("altlabel")+"_label").style.color="red";
				}				
				
				if(fieldObj.type=="radio" && tmpScienceAtGcseSingle){
					tmpScienceAtGcseDual.style.border="solid 1px red";
					tmpScienceAtGcseDual.style.background="#fcf9b3";
					tmpScienceAtGcseTriple.style.border="solid 1px red";
					tmpScienceAtGcseTriple.style.background="#fcf9b3";
					document.getElementById(fieldObj.getAttribute("altlabel")+"_label").style.color="red";
				}

				if (fieldObj.type=="radio" && tmpCorrect) {
					tmpCorrect.style.border="solid 1px red";
					tmpCorrect.style.background="#fcf9b3";
					document.getElementById(fieldObj.getAttribute("altlabel")+"_label").style.color="red";					
				}
				
				if (fieldObj.type=="radio" && tmpCrbCheck) {
					tmpCrbCheck.style.border="solid 1px red";
					tmpCrbCheck.style.background="#fcf9b3";
					document.getElementById(fieldObj.getAttribute("altlabel")+"_label").style.color="red";							
				}

				if (fieldObj.type=="radio" && tmpCertify) {
					tmpCertify.style.border="solid 1px red";
					tmpCertify.style.background="#fcf9b3";
					document.getElementById(fieldObj.getAttribute("altlabel")+"_label").style.color="red";					
				}
				
				ScrollToElement(fieldObj);
				fieldObj.onfocus=function resetThis(){
					this.style.border="solid 1px #aeb1b6";
					if(!this.getAttribute("altlabel")) document.getElementById(this.id + "_label").style.color="#4a4f52"; else document.getElementById(this.getAttribute("altlabel")).style.color="#4a4f52";
					this.style.background="#ffffff";
					}
					
				if((fieldObj.type=="radio" &&  tmpRadioNo)){
				tmpRadioNo.onfocus=function resetThis(){
					document.getElementById(this.getAttribute("altlabel")+"_label").style.color="#4a4f52";
					tmpRadioYes.style.border="none";
					tmpRadioYes.style.background="#eff6fd";
					tmpRadioNo.style.border="none";
					tmpRadioNo.style.background="#eff6fd";
					}					
				tmpRadioYes.onfocus=function resetThis(){
					document.getElementById(this.getAttribute("altlabel")+"_label").style.color="#4a4f52";
					tmpRadioYes.style.border="none";
					tmpRadioYes.style.background="#eff6fd";
					tmpRadioNo.style.border="none";
					tmpRadioNo.style.background="#eff6fd";
					}					
				}

				if((fieldObj.type=="radio" &&  tmpRadio2No)){
				tmpRadio2No.onfocus=function resetThis(){
					document.getElementById(this.getAttribute("altlabel")+"_label").style.color="#4a4f52";
					tmpRadio2Yes.style.border="none";
					tmpRadio2Yes.style.background="#eff6fd";
					tmpRadio2No.style.border="none";
					tmpRadio2No.style.background="#eff6fd";
					}					
				tmpRadio2Yes.onfocus=function resetThis(){
					document.getElementById(this.getAttribute("altlabel")+"_label").style.color="#4a4f52";
					tmpRadio2Yes.style.border="none";
					tmpRadio2Yes.style.background="#eff6fd";
					tmpRadio2No.style.border="none";
					tmpRadio2No.style.background="#eff6fd";
					}					
				}				
				

				if(fieldObj.type=="radio" &&  tmpScienceAtGcseSingle){
				tmpScienceAtGcseSingle.onfocus=function resetThis(){
					document.getElementById(this.getAttribute("altlabel")+"_label").style.color="#4a4f52";
					tmpScienceAtGcseSingle.style.border="none";
					tmpScienceAtGcseSingle.style.background="#eff6fd";
					tmpScienceAtGcseDual.style.border="none";
					tmpScienceAtGcseDual.style.background="#eff6fd";
					tmpScienceAtGcseTriple.style.border="none";
					tmpScienceAtGcseTriple.style.background="#eff6fd";					
					}
				tmpScienceAtGcseDual.onfocus=function resetThis(){
					document.getElementById(this.getAttribute("altlabel")+"_label").style.color="#4a4f52";
					tmpScienceAtGcseSingle.style.border="none";
					tmpScienceAtGcseSingle.style.background="#eff6fd";
					tmpScienceAtGcseDual.style.border="none";
					tmpScienceAtGcseDual.style.background="#eff6fd";
					tmpScienceAtGcseTriple.style.border="none";
					tmpScienceAtGcseTriple.style.background="#eff6fd";					
					}
				tmpScienceAtGcseTriple.onfocus=function resetThis(){
					document.getElementById(this.getAttribute("altlabel")+"_label").style.color="#4a4f52";
					tmpScienceAtGcseSingle.style.border="none";
					tmpScienceAtGcseSingle.style.background="#eff6fd";
					tmpScienceAtGcseDual.style.border="none";
					tmpScienceAtGcseDual.style.background="#eff6fd";
					tmpScienceAtGcseTriple.style.border="none";
					tmpScienceAtGcseTriple.style.background="#eff6fd";					
					}				
					
				}							
					
				return false;
			 	break;
  	 		}
  	 }
  	
  	 return true;
  	
}


function isEmail(str){
if (str.search(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/) != -1)
return true;
else
return false;

}



var letback=false;

function swapSection(sectionInt){
theForm=document.getElementById('form_app');
//if(theForm.className=="nonteaching")return;

switch(sectionInt)
{
case 1:
tempArray=teachingapp_1Array;
if(theForm.className=="nonteaching")tempArray=nonapp_1Array;
if(!validateForm(theForm))break;
 window.scrollTo(0,0);
  document.getElementById('step1').style.display="block";
  document.getElementById('step2').style.display="none";
  document.getElementById('step3').style.display="none";
  document.getElementById('step4').style.display="none";
  location.href="#section1";
  letback=true;
 break;

case 2:
tempArray=teachingapp_1Array;
if(theForm.className=="nonteaching")tempArray=nonapp_1Array;
if(!validateForm(theForm))break;
window.scrollTo(0,0);
   document.getElementById('step1').style.display="none";
   document.getElementById('step2').style.display="block";
   document.getElementById('step3').style.display="none";
   document.getElementById('step4').style.display="none";
location.href="#section2";
letback=false;
  break;
  
case 3:
 if(!validateForm(theForm))break; 
 tempArray=teachingapp_1Array.concat(teachingapp_3Array);
if(theForm.className=="nonteaching")tempArray=nonapp_1Array;
 window.scrollTo(0,0);
  document.getElementById('step1').style.display="none";
   document.getElementById('step2').style.display="none";
   document.getElementById('step3').style.display="block";
   document.getElementById('step4').style.display="none";
 location.href="#section3";
 letback=false;
break;  
  
case 4:
//window.onbeforeunload = function () {};
 if(!validateForm(theForm))break; 
 window.scrollTo(0,0);
  tempArray=teachingapp_1Array.concat(teachingapp_3Array);
  tempArray=tempArray.concat(teachingapp_4Array);
 if(theForm.className=="nonteaching")tempArray=nonapp_1Array.concat(nonapp_4Array);
 document.getElementById('step1').style.display="none";
   document.getElementById('step2').style.display="none";
   document.getElementById('step3').style.display="none";
   document.getElementById('step4').style.display="block";
    location.href="#section4";
    letback=false;
 break;  
    
default:
  document.getElementById('step1').style.display="none";
}


}

 
window.onunload=function(){
		if(location.href.match("section2")){
		//location.href="teachingapp.htm";
		}
		if(location.href.match("section3")){
		//location.href="teachingapp.htm#section2";
		}
		if(location.href.match("section4")){
		//location.href="teachingapp.htm#section3";
		}

	//if(location.href.match("#")){		
		//history.forward();
	//	}
        }
        
window.onload=function(){
		if(location.href.match("section2")){
		//swapSection(2);
		}
		if(location.href.match("section3")){
		//swapSection(3);
		}
		if(location.href.match("section4")){
		//swapSection(4);
		}		
		
}




function checkUKTelephone (telephoneNumber) {

  // Convert into a string and check that we were provided with something
  var telnum = telephoneNumber + " ";
  if (telnum.length == 1)  {
     telNumberErrorNo = 1;
     return false
  }
  telnum.length = telnum.length - 1;
  
  // Don't allow country codes to be included (assumes a leading "+")
  var exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = 2;
     return false;
  }
  
  // Remove spaces from the telephone number to help validation
  while (telnum.indexOf(" ")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  }
  
  // Remove hyphens from the telephone number to help validation
  while (telnum.indexOf("-")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
  }  
  
  // Now check that all the characters are digits
  exp = /^[0-9]{10,11}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 3;
     return false;
  }
  
  // Now check that the first digit is 0
  exp = /^0[0-9]{9,10}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 4;
     return false;
  }
  
  // Finally check that the telephone number is appropriate.
  exp = /^(01|02|03|05|070|077|07624|078|079)[0-9]+$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 5;
     return false;
  }
  
  // Telephone number seems to be valid - return the stripped telehone number  
  return telnum;
}
var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";


function isNumber(a) {return typeof a == 'number' && isFinite(a);}


function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  

  var pcexp = new Array ();
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);  
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);  
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  var postCode = toCheck;
  var valid = false;
  
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
      pcexp[i].exec(postCode);
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      valid = true;
      break;
    }
  }
 
  if (valid) {return postCode;} else return false;
}

unloadStr="";

//window.onbeforeunload = function () {return "You have not submitted the form.  If you continue, your form will not be saved."}




