// JavaScript Document
	function validatecalculator() {
		var loandetailsresult
		var personaldetailsresult
		var residentialdetailsresult
		
		
		loandetailsresult = validateloandetails();
		if (loandetailsresult == false)
		{
			return false;
		}
		
		personaldetailsresult = validatepersonaldetails();
		if (personaldetailsresult == false)
		{
			return false;
		}
		
		residentialdetailsresult = validateresidentialdetails();
		if (residentialdetailsresult == false)
		{
			return false;
		}
		
				
		return true;
	}
	
	function validateloandetails() {
		if (document.frmCalculator.homeowner.value == "")
		{
			alert("Please select whether you own your own home");
			document.frmCalculator.homeowner.focus();
			return false;
		}
		if ((document.frmCalculator.txtMortgageAmount.value == "")&& (document.frmCalculator.homeowner.value == "1"))
				{
			alert("Please provide your Mortgage Amount");
			document.frmCalculator.txtMortgageAmount.focus();
			return false;
		}
		if ((document.frmCalculator.txtHomeValue.value == "") && (document.frmCalculator.homeowner.value == "1"))
				{
			alert("Please provide the value of your home");
			document.frmCalculator.txtHomeValue.focus();
			return false;
		}
		
		if (document.frmCalculator.txtDebtValue.value == "")
		{
			alert("Please provide an estimate of your debt");
			document.frmCalculator.txtDebtValue.focus();
			return false;
		}
		
		if ((document.frmCalculator.txtDebtValue.value < 3000) || (document.frmCalculator.txtDebtValue.value > 250000))
		{
			alert("Your debt needs to be between £3000 and £250000");
			document.frmCalculator.txtDebtValue.focus();
			return false;
		}

		if (document.frmCalculator.txtDisposableValue.value == "")
		{
			alert("Please provide your monthly disposable income");
			document.frmCalculator.txtDisposableValue.focus();
			return false;
		}
		
		
		if (document.frmCalculator.Companies.value == "")
		{
			alert("Please select how many companies you owe money to");
			document.frmCalculator.Companies.focus();
			return false;
		}
		return true;
	}
	
	function validatepersonaldetails() {
		if (document.frmCalculator.cboTitle.value == "")
		{
			alert("Please provide your Title");
			document.frmCalculator.cboTitle.focus();
			return false;
		}
		if (document.frmCalculator.first_name.value == "")
		{
			alert("Please provide your first name");
			document.frmCalculator.first_name.focus;
			return false;
		}
		if (document.frmCalculator.last_name.value == "")
		{
			alert("Please provide your Surname");
			document.frmCalculator.last_name.focus();
			return false;
		}
		
		
		if ((document.frmCalculator.phone.value == "") && (document.frmCalculator.mobile.value == ""))
		{
			alert("Please provide at least one Telephone Number");
			document.frmCalculator.phone.focus();
			return false;
		}
		
		if (document.frmCalculator.phone.value != "")
		{
			var HomeTelResult = checkHomeTel();
			if (HomeTelResult == false)
			{
				return false;
			}
		}
		
		if (document.frmCalculator.mobile.value != "")
		{
			var MobileTelResult = checkMobileTel();
			if (MobileTelResult == false)
			{
				return false;
			}
		}
		if (document.frmCalculator.txtEmail.value == "")
		{
			alert("Please provide your email");
			document.frmCalculator.txtEmail.focus();
			return false;
		}
		
		if (document.frmCalculator.txtEmail.value != "")
		{
			var EmailCheckResult = validateemail();
			if (EmailCheckResult == false)
			{
				return false;
			}
		}
				
		
		return true;
	}
	
	function validateresidentialdetails() {
				
		
                
        if (document.frmCalculator.address_1.value == "")
		{
			alert("Please provide the first line of your address eg house no and street ")
			document.frmCalculator.address_1.focus();
			return false;
		}
		if (document.frmCalculator.city.value == "")
		{
			alert("Please provide the name of your City where you live")
			document.frmCalculator.city.focus();
			return false;
		}
		
		if (document.frmCalculator.postcode.value == "")
		{
			alert("Please provide the Postcode for where you live")
			document.frmCalculator.postcode.focus();
			return false;
		}
		
		
				
		return true;
	}
	
	
	
		
	
	
	
	function checkHomeTel () {
		var TelNo = document.frmCalculator.phone.value
		// If invalid number, report back error
		if (!checkUKTelephone (TelNo))
		{
			alert ("Home Telephone Number provided is invalid");
			document.frmCalculator.phone.focus();
			return false;
		}	
		return true;
	}
	
	function checkMobileTel () {
		var TelNo = document.frmCalculator.mobile.value
		  // If invalid number, report back error
		  if (!checkUKTelephone (TelNo)) {
			 alert ("Mobile Telephone Number provided is invalid")
		  	 document.frmCalculator.mobile.focus();
			 return false;
		  }
	}
	
	function checkUKTelephone (telephoneNumber) {
		var telnum;
		// Convert into a string and check that we were provided with a number
		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 "+")
	  	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;
		}
			  
	  	// Now check that the telephone number is appropriate.
	  	exp = /^(01|02|05|070|075|077|078|079)[0-9]+$/;
	  	if (exp.test(telnum) != true) 
		{
			telNumberErrorNo = 5;
			return false;
	  	}
			  
	  	// Seems to be valid
	  	return true;
	}
	
	function validateemail () {
		var emailStr = document.frmCalculator.txtEmail.value;
		
		/* The following pattern is used to check if the entered e-mail address
   		fits the user@domain format.  It also is used to separate the username
   		from the domain. */
		var emailPat=/^(.+)@(.+)$/
		/* The following string represents the pattern for matching all special
		   characters.  We don't want to allow special characters in the address. 
		   These characters include ( ) < > @ , ; : \ " . [ ]    */
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		/* The following string represents the range of characters allowed in a 
		   username or domainname.  It really states which chars aren't allowed. */
		var validChars="\[^\\s" + specialChars + "\]"
		/* The following pattern applies if the "user" is a quoted string (in
		   which case, there are no rules about which characters are allowed
		   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
		   is a legal e-mail address. */
		var quotedUser="(\"[^\"]*\")"
		/* The following pattern applies for domains that are IP addresses,
		   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
		   e-mail address. NOTE: The square brackets are required. */
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		/* The following string represents an atom (basically a series of
		   non-special characters.) */
		var atom=validChars + '+'
		/* The following string represents one word in the typical username.
		   For example, in john.doe@somewhere.com, john and doe are words.
		   Basically, a word is either an atom or quoted string. */
		var word="(" + atom + "|" + quotedUser + ")"
		// The following pattern describes the structure of the user
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		/* The following pattern describes the structure of a normal symbolic
		   domain, as opposed to ipDomainPat, shown above. */
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		
		/* Finally, let's start trying to figure out if the supplied address is
		   valid. */
		
		/* Begin with the coarse pattern to simply break up user@domain into
		   different pieces that are easy to analyze. */
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null)
		{
			/* Too many/few @'s or something; basically, this address doesn't
			even fit the general mould of a valid e-mail address. */
			alert("Email address seems incorrect (check @ and .'s)");
			return false;
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		
		// See if "user" is valid 
		if (user.match(userPat)==null)
		{
			// user is not valid
			alert("Email address error: The username doesn't seem to be valid.");
			return false;
		}

		/* if the e-mail address is at an IP address (as opposed to a symbolic
		   host name) make sure the IP address is valid. */
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) 
		{
			// this is an IP address
			for (var i=1;i<=4;i++)
			{
				if (IPArray[i]>255)
				{
					alert("Email Address Error: Destination IP address is invalid!");
					return false;
				}
			}
		}
		
		// Domain is symbolic name
		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
			alert("Email Address Error: The domain name doesn't seem to be valid.");
			return false;
		}
		
		/* domain name seems valid, but now make sure that it ends in a
		   three-letter word (like com, edu, gov) or a two-letter word,
		   representing country (uk, nl), and that there's a hostname preceding 
		   the domain or country. */
		
		/* Now we need to break up the domain to get a count of how many atoms
		   it consists of. */
		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || 
			domArr[domArr.length-1].length>3) {
		   // the address must end in a two letter or three letter word.
		   alert("Email Address Error: The address must end in a three-letter domain, or two letter country.");
		   return false;
		}

		// Make sure there's a host name preceding the domain.
		if (len<2) {
		   var errStr="Email Address Error: This address is missing a hostname!"
		   alert(errStr)
		   return false;
		}
		
		// If we've gotten this far, everything's valid!
		return true;
	}
		
		
		function validatepostcode(){ //check postcode format is valid
		 var sPostcode = document.frmCalculator.postcode.value; size = sPostcode.length
		 sPostcode = sPostcode.toUpperCase(); //Change to uppercase
		 while (sPostcode.slice(0,1) == " ") //Strip leading spaces
		  {sPostcode = sPostcode.substr(1,size-1);size = sPostcode.length
		  }
		 while(sPostcode.slice(size-1,size)== " ") //Strip trailing spaces
		  {sPostcode = sPostcode.substr(0,size-1);size = sPostcode.length
		  }
		 document.frmCalculator.txtPostcode.value = sPostcode; //write back to form field
		 if (size < 6 || size > 8){ //Code length rule
		  alert(sPostcode + " is not a valid postcode - wrong length");
		  document.frmCalculator.txtPostcode.focus();
		  return false;
		  }
		 if (!(isNaN(sPostcode.charAt(0)))){ //leftmost character must be alpha character rule
		   alert(sPostcode + " is not a valid postcode - cannot start with a number");
		   document.frmCalculator.txtPostcode.focus();
		   return false;
		  }
		 if (isNaN(sPostcode.charAt(size-3))){ //first character of inward code must be numeric rule
		   alert(sPostcode + " is not a valid postcode - alpha character in wrong position");
		   document.frmCalculator.txtPostcode.focus();
		   return false;
		  }
		 if (!(isNaN(sPostcode.charAt(size-2)))){ //second character of inward code must be alpha rule
		   alert(sPostcode + " is not a valid postcode - number in wrong position");
		   document.frmCalculator.txtPostcode.focus();
		   return false;
		  }
		 if (!(isNaN(sPostcode.charAt(size-1)))){ //third character of inward code must be alpha rule
		   alert(sPostcode + " is not a valid postcode - number in wrong position");
		   document.frmCalculator.txtPostcode.focus();
		   return false;
		  }
		 if (!(sPostcode.charAt(size-4) == " ")){//space in position length-3 rule
		   alert(sPostcode + " is not a valid postcode - no space or space in wrong position");
		   document.frmCalculator.txtPostcode.focus();
		   return false;
		   }
		 count1 = sPostcode.indexOf(" ");count2 = sPostcode.lastIndexOf(" ");
		 if (count1 != count2){//only one space rule
		   alert(sPostcode + " is not a valid postcode - only one space allowed");
		   document.frmCalculator.txtPostcode.focus();
		   return false;
		  }
		return true;
		}
	
//}


