// ********************START VALIDATION****************************************
function chkFrm(type){
//Tab 1
if ((type == 1)||(type == 0))
	{			

		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.mob_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;
		  }
		 
		 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;
		  }
		
		}
	return true;
}

