// ********************START VALIDATION****************************************
function chkFrm(type){
//Tab 1
if ((type == 1)||(type == 0))
	{

		if (document.form1.acceptance.value=="#"){
			alert("More Information Required:\nPlease make a selection in the \"Which of the following sounds familiar?\" menu")
			document.form1.acceptance.focus()
			return false;
			}
		//The falling catches how important the response should be	
		document.form1.acceptanceNdx.value = document.form1.acceptance.selectedIndex;				
		//if (document.form1.how_soon.value=="#"){
			//alert("More Information Required:\nPlease make a selection in the \"How soon do you require a new mortgage\" menu")
			//document.form1.how_soon.focus()
			//return false;
			//}
		//if (document.form1.reason_remortgage.value=="#"){
			//alert("More Information Required:\nPlease make a selection in the \"What is the purpose of your new mortgage\" menu")
			//document.form1.reason_remortgage.focus()
			//return false;
			//}		

		if (document.form1.app1_firstname.value==""){
			alert("More Information Required Please:\nYour forename: is a required field")
			document.form1.app1_firstname.focus()
			return false;
			}
		if (document.form1.app1_surname.value==""){
			alert("More Information Required Please:\nYour surname: is a required field")
			document.form1.app1_surname.focus()
			return false;
			}
		if (((document.form1.app2_firstname.value!="") && (document.form1.app2_surname.value==""))||((document.form1.app2_surname.value!="") && (document.form1.app2_firstname.value==""))){
			alert("More Information Required Please:\nFull details for second applicant must be included")
			if (document.form1.app2_firstname.value=="")document.form1.App2_firstname.focus();
			else
			if (document.form1.app2_surname.value=="") document.form1.app2_surname.focus();
			return false;
			}
		if((document.form1.home_tel_no.value=="") && (document.form1.mobile_tel_no.value=="")){
			alert("More Information Required:\nPlease supply at least one phone number as a contact.");
			document.form1.home_tel_no.focus();
			return false;
			}	
// START checks the format of the home tel number
			var checkOK = "0123456789-., ";
			var checkStr = document.form1.home_tel_no.value;
			var allValid = true;
			var validGroups = true;
			var decPoints = 0;
			var allNum = "";
			for (i = 0;  i < checkStr.length;  i++)
			{
				ch = checkStr.charAt(i);
				for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
				if (j == checkOK.length)
    			{
					allValid = false;
	      			break;
    			}
			if (ch == ".")
    			{
    	  			allNum += ".";
      				decPoints++;
    			}
    			else if (ch == "," && decPoints != 0)
    			{
      				validGroups = false;
      				break;
    			}
    			else if (ch != ",")
      			allNum += ch;
  			}
  		if (!allValid)
  			{
				alert("More Information Required:\nPlease enter only digits in the \"Daytime Tel No\" field")
    			document.form1.home_tel_no.focus();
    			return (false);
  			}
		if (decPoints > 1 || !validGroups)
  			{
    			alert("More Information Required:\nPlease enter a valid number in the \"Daytime Tel No\" field.");
				document.form1.home_tel_no.focus();
				return (false);
  			}
// END checks the format of the MOBILE tel number
// START checks the format of the MOBILE tel number
			var checkOK = "0123456789-., ";
			var checkStr = document.form1.mobile_tel_no.value;
			var allValid = true;
			var validGroups = true;
			var decPoints = 0;
			var allNum = "";
			for (i = 0;  i < checkStr.length;  i++)
			{
				ch = checkStr.charAt(i);
				for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
				if (j == checkOK.length)
    			{
					allValid = false;
	      			break;
    			}
			if (ch == ".")
    			{
    	  			allNum += ".";
      				decPoints++;
    			}
    			else if (ch == "," && decPoints != 0)
    			{
      				validGroups = false;
      				break;
    			}
    			else if (ch != ",")
      			allNum += ch;
  			}
  		if (!allValid)
  			{
				alert("More Information Required:\nPlease enter only digits in the \"Mobile Tel No\" field")
    			document.form1.mobile_tel_no.focus();
    			return (false);
  			}
		if (decPoints > 1 || !validGroups)
  			{
    			alert("More Information Required:\nPlease enter a valid number in the \"Mobile Tel No\" field.");
				document.form1.mobile_tel_no.focus();
				return (false);
  			}
// END checks the format of the MOBILE tel number


		if (document.form1.email.value==""){
			alert("More Information Required Please:\nYour email: is a mandatory field")
			document.form1.email.focus()
			return false;
			}			
		if (document.form1.email.value!=""){
			if ((document.form1.email.value.indexOf("@") == -1 ||document.form1.email.value.indexOf(".") == -1 ))  {
				alert("More Information Required:\nPlease supply a valid email address.");
			 	document.form1.email.focus();
             	return false;
				}
             }			
		
		if (document.form1.postcode.value==""){
			alert("More Information Required Please:\nPostcode: is a mandatory field")
			document.form1.postcode.focus()
			return false;
			}
//Checkl postcode		
		 test = document.form1.postcode.value; size = test.length
		 test = test.toUpperCase(); //Change to uppercase
		 while (test.slice(0,1) == " ") //Strip leading spaces
			  {test = test.substr(1,size-1);size = test.length
		  }
		 while(test.slice(size-1,size)== " ") //Strip trailing spaces
			  {test = test.substr(0,size-1);size = test.length
		  }
		 document.form1.postcode.value = test; //write back to form field
		 if (size < 6 || size > 8){ //Code length rule
			alert(test + " is not a valid postcode - wrong length");
			document.form1.postcode.focus();
			  return false;
		  }
		 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
		   alert(test + " is not a valid postcode - cannot start with a number");
			 document.form1.postcode.focus();
			  return false;
		  }
		 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
			alert(test + " is not a valid postcode - alpha character in wrong position");
			document.form1.postcode.focus();
			return false;
		  }
		 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
			alert(test + " is not a valid postcode - number in wrong position");
			document.form1.postcode.focus();
			return false;
		  }
		 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
			alert(test + " is not a valid postcode - number in wrong position");
			document.form1.postcode.focus();
			return false;
		  }
		 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
			alert(test + " is not a valid postcode - no space or space in wrong position");
			document.form1.postcode.focus();
			return false;
		   }
		 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
		 if (count1 != count2){//only one space rule
			alert(test + " is not a valid postcode - only one space allowed");
			document.form1.postcode.focus();
			return false;
		  }
	}
	if ((type == 2)||(type == 0)){
		if (document.form1.property_type.value=="#"){
			alert("More Information Required:\nPlease make a selection in the \"What is the property type?\"")
			document.form1.property_type.focus();
			return false;
			}
		}
	if ((type == 3)||(type == 0)){
		}
	return true;
	}
	
	
	
function show_div(div_id, type) {
	if (chkFrm(type)){
	// hide all the divs
    document.getElementById('form01').style.display = 'none';
	document.getElementById('form02').style.display = 'none';
	document.getElementById('form03').style.display = 'none';

    // show the requested div
	document.getElementById(div_id).style.display = 'block';
	}
	return true;
}

function show_div_3from1() {
	if (chkFrm(1)&&chkFrm(2)){
	// hide all the divs
    document.getElementById('form01').style.display = 'none';
	document.getElementById('form02').style.display = 'none';
	document.getElementById('form03').style.display = 'none';

    // show the requested div
	document.getElementById('form03').style.display = 'block';
	}
	return true;
}
