
function validate_step1(){

	var err1=false;	

	var mortgageSize=el_fi('Mortgage_Size');//alert('Start Validate');
	if(!mortgageSize){show_error('Mortgage_Size','Please enter a value for the amount to borrow');err1=true;}
	else if(mortgageSize<25000){show_error('Mortgage_Size','Loan must be for at least &pound;25,000');err1=true;}
	else if(mortgageSize && mortgageSize != parseInt(mortgageSize)){show_error('Mortgage_Size','Please enter numbers only for Loan Amount (eg 50000)');err1=true;}	
	else{hide_error('Mortgage_Size');}

	var propVal=el_fi('Property_Value');
	if(!propVal){show_error('Property_Value','Please enter a value for the property');err1=true;}
	else if(propVal<25000 || propVal>5000000){show_error('Property_Value','Property value must be &pound;25,000 to &pound;5,000,000');err1=true;}
	else if(propVal && propVal != parseInt(propVal)){show_error('Property_Value','Please enter numbers only for Property Value (eg 250000)');err1=true;}
	else{hide_error('Property_Value');}
	
	if(mortgageSize && propVal){
		ltv=(mortgageSize / propVal)*100;
		if(ltv>85 && mortgageSize>24999 && propVal>24999){max_loan=parseInt(propVal*0.85);if(max_loan<25000){min_comment=" (must be at least &pound;25,000)";}else{min_comment="";}show_error('Mortgage_Size','Maximum Loan for this property value is &pound;' + max_loan + min_comment);err1=true;}else{hide_error('Mortgage_Size');}

	}	

if(el_val('Mortgage_Type')==""){show_error('Mortgage_Type','Please select a reason for your loan');err1=true;}else{hide_error('Mortgage_Type');}

el_get('Property_Value_update').value=addCommas(propVal);
el_get('Mortgage_Size_update').value=addCommas(mortgageSize);
return err1;
}
	
function validate_step2(){
	var err2=false;
	//if(!el_get('FTB').checked && !el_get('FTB_1').checked){show_error('FTB','Please tell us if you are a first time buyer');err2=true;}else{hide_error('FTB');}
if(!el_get('SelfCert').checked && !el_get('SelfCert_1').checked){show_error('SelfCert','Please tell us if you need to Self-Certify your income');err2=1;}else{hide_error('SelfCert');}
//if(el_val('Provider')=='other'){show_error('Provider','Please tell us your current mortgage lender');err2=1;}else{hide_error('Provider');}
//if(el_val('Emp_Stat')=='other'){show_error('Emp_Stat','Please tell us your employment status');err2=1;}else{hide_error('Emp_Stat');}
//if(el_get('LB_MissedPayments').checked && el_val('3month_m_miss')==0 && el_val('10month_m_miss')==0 ){show_error('LB_MissedPayments','Please enter the number of payments missed or if none select "No" to Missed Payments');err2=1;}else{hide_error('LB_MissedPayments');}
	var propVal=el_fi('Property_Value_update');
	var mortgageSize=el_fi('Mortgage_Size_update');
	var self_cert=getRadioButtonListSelectedValue('SelfCert');
	var bad_cred=customerHasBadCredit();
	var mLTV=90;
	if(self_cert==1){mLTV=85;}
	if(bad_cred){mLTV=80;}
	el_get('maxloan_allowed').innerHTML=mLTV;
	el_get('Property_Value').value=addCommas(propVal);
	el_get('Mortgage_Size').value=addCommas(mortgageSize);
	if(mortgageSize && propVal){
		ltv=(mortgageSize / propVal)*100;
		if(ltv>mLTV && mortgageSize>24999 && propVal>24999){max_loan=parseInt(propVal*mLTV/100);if(max_loan<25000){min_comment=" (must be at least &pound;25,000)";}else{min_comment="";}show_error('Mortgage_Size','Maximum Loan for this property value is &pound;' + max_loan + min_comment);err1=true;err2+=2;}//else{hide_error('Mortgage_Size');}
	}


return err2;
}
	
function validate_step3(){
	var err3=false;var DOB_err=false;
	var valid_Name=/^\D{2,25}$/;var valid_address=/^([a-zA-Z0-9 _-]+){2,25}$/;
	var firstname=trim_input('First_Name');
	if(!valid_Name.test(firstname)){show_error('First_Name','Please enter your Firstname');err3=true;}else{hide_error('First_Name');}
	var surname=trim_input('Surname');
	if(!valid_Name.test(surname)){show_error('Surname','Please enter your Surname');err3=true;}else{hide_error('Surname');}
		
	num_phones=0;
	if(el_val('Home_Phone')){if(validate_phone('Home_Phone')){err3=true;}}
	if(el_val('Work_Phone')){if(validate_phone('Work_Phone')){err3=true;}}
	//if(el_val('Mobile_Phone')){if(!validate_phone('Mobile_Phone')){num_phones++;}}
	//if(num_phones<2){alert("Please enter 2 Phone Numbers");err3=true;}		  
	if(!el_val('Home_Phone')){show_error('Home_Phone','Please enter your Home Phone Number');err3=true;}
	if(!el_val('Work_Phone')){show_error('Work_Phone','Please enter a 2nd Phone Number');err3=true;}
	if(!el_get('Email').value || !isValidEmail(el_get('Email').value)){show_error('Email','Please enter a valid email');err3=true;}else{hide_error('Email');}
	//if(!el_get('Email2').value || !isValidEmail(el_get('Email2').value)){show_error('Email2','Please confirm your email');err3=true;}else{hide_error('Email2');}
	//if(el_get('Email').value != el_get('Email2').value){show_error('Email2','Email address&acute;s do not match');err3=true;}
	//if(!el_val('DOBDay')|| !el_val('DOBMonth') || !el_val('DOBYear')){show_error('LB_DateOfBirth','Please select your Date of Birth');err3=true;}else{hide_error('LB_DateOfBirth');}
			if(!el_val('DOBDay')){highlite(el_get('DOBDay'));DOB_err=true;}else{lowlite(el_get('DOBDay'));}
			if(!el_val('DOBMonth')){highlite(el_get('DOBMonth'));DOB_err=true;}else{lowlite(el_get('DOBMonth'));}
			if(!el_val('DOBYear')){highlite(el_get('DOBYear'));DOB_err=true;}else{lowlite(el_get('DOBYear'));}
			if(DOB_err){show_error('LB_DateOfBirth','Please select your Date of Birth');err3=true;}else{hide_error('LB_DateOfBirth');}
	var address=trim_input('Adr1');
	if(!valid_address.test(address)){show_error('Adr1','Please enter the 1st line of your address');err3=true;}else{hide_error('Adr1');}
	var town=trim_input('Town');
	if(!valid_Name.test(town)){show_error('Town','Please enter your Town');err3=true;}else{hide_error('Town');}
	var county=trim_input('County');
	if(!valid_Name.test(county)){show_error('County','Please enter your County');err3=true;}else{hide_error('County');}




	if(postcodeTest(el_val('Postcode'))!=0){el_get('Postcode').value=postcodeTest(el_val('Postcode'));hide_error('Postcode')}else{show_error('Postcode','Please enter a valid Postcode');err3=true;}
	
if(err3){alert("Please correct the indicated items");}	

return err3;
}


function show_error(el,e_message){
	err_area=el_get(el + '_ErrorAdvice');
	err_area.style.visibility="visible";
	err_area.innerHTML=e_message;
	if(el!='LB_DateOfBirth'){highlite(el_get(el));}
	}	
	
	
function hide_error(el){
	err_area=el_get(el + '_ErrorAdvice');
	err_area.style.visibility="hidden";
	err_area.innerHTML='';
	if(el!='LB_DateOfBirth'){lowlite(el_get(el));}
	}	

function highlite(field){
field.style.background="#FFFF66";
}
function lowlite(field){
field.style.background="#FFFFFF";
}
function clr_bk(field){
field.style.background="#FFFFFF";
}
function calculateLtvPercent( amountToBorrow, propertyValue )
{
	return ( amountToBorrow / propertyValue ) * 100.0;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function trim_input(el){
input_val=trim(el_get(el).value);el_get(el).value=input_val;
return input_val;
 }

function validate_phone(phone){
	var UkPhoneNumberErrorMessages = ["Please enter Telephone No. without country code or +", "Telephone No. should be 10 or 11 digits", "Telephone No. should start with a zero", "06 / 070 Telephone Nos. not accepted", "Please enter home, work or mobile Telephone No.","Telephone No. should contain numbers only","Please enter at least 1 Alternative No."];
	errorcode=UkPhoneNumber(el_get(phone).value);
	if(errorcode!=-1){
		show_error(phone,UkPhoneNumberErrorMessages[errorcode]);return true;
	}else{hide_error(phone);return false;}	
	
	
}
	
function UkPhoneNumber(field_val)
{
	// Get the phone number as a string from the text box
	var telephoneNumber = field_val;
	
	// Don't allow country codes to be included (assumes a leading "+")
	if ( telephoneNumber.match(/^\+.+$/) )
	{
		return 0;
	}

	if (telephoneNumber.match(/^\+?[0-9 ()-]+[0-9]$/) == null )
	{
		return 5;
	}

	// Remove everything but numeric digits from the telephone number to help validation
	telephoneNumber = telephoneNumber.replace(/[^0-9]*([0-9]*)/g, "$1");

	// Now check that it is 10 or 11 digits long
	if( telephoneNumber.match(/^[0-9]{10,11}$/) == null )
	{
		return 1;
	}

	// Now check that the first digit is 0
	if ( telephoneNumber.match(/^0[0-9]{9,10}$/) == null )
	{
		return 2;
	}

	// Finally check that the telephone number is appropriate.
	if ( telephoneNumber.match(/^(06|070)[0-9]+$/) )
	{
		return 3;
	}

	// Check that the telephone number is appropriate.
	if (telephoneNumber.match(/^(01|02|03|05|07|08)[0-9]+$/) == null )
	{
		return 4;
	}

	// Check for too many repeating digits
	if (telephoneNumber.match(/^\d*(\d)\1{7}\d*$/) )
	{
		return 4;
	}
	if (telephoneNumber.match(/^\+?[0-9 ()-]+[0-9]$/) == null )
	{
		return 4;
	}
	return -1;
}


var postcodeOutcodes = new Array();
postcodeOutcodes[0]="AB";
postcodeOutcodes[1]="DG";
postcodeOutcodes[2]="DD";
postcodeOutcodes[3]="FK";
postcodeOutcodes[4]="EH";
postcodeOutcodes[5]="KY";
postcodeOutcodes[6]="KA";
postcodeOutcodes[7]="IV";
postcodeOutcodes[8]="KW";
postcodeOutcodes[9]="PA";
postcodeOutcodes[10]="PH";
postcodeOutcodes[11]="ML";
postcodeOutcodes[12]="HS";
postcodeOutcodes[13]="ZE";
postcodeOutcodes[14]="CF";
postcodeOutcodes[15]="LD";
postcodeOutcodes[16]="LL";
postcodeOutcodes[17]="NP";
postcodeOutcodes[18]="SA";
postcodeOutcodes[19]="SY";
postcodeOutcodes[20]="BD";
postcodeOutcodes[21]="DH";
postcodeOutcodes[22]="DL";
postcodeOutcodes[23]="DN";
postcodeOutcodes[24]="HD";
postcodeOutcodes[25]="HG";
postcodeOutcodes[26]="HU";
postcodeOutcodes[27]="HX";
postcodeOutcodes[28]="LN";
postcodeOutcodes[29]="LS";
postcodeOutcodes[30]="NE";
postcodeOutcodes[31]="SR";
postcodeOutcodes[32]="TS";
postcodeOutcodes[33]="WF";
postcodeOutcodes[34]="YO";
postcodeOutcodes[35]="BB";
postcodeOutcodes[36]="BL";
postcodeOutcodes[37]="CA";
postcodeOutcodes[38]="CH";
postcodeOutcodes[39]="CW";
postcodeOutcodes[40]="FY";
postcodeOutcodes[41]="LA";
postcodeOutcodes[42]="M";
postcodeOutcodes[43]="OL";
postcodeOutcodes[44]="PR";
postcodeOutcodes[45]="SK";
postcodeOutcodes[46]="TF";
postcodeOutcodes[47]="WA";
postcodeOutcodes[48]="WN";
postcodeOutcodes[49]="B";
postcodeOutcodes[50]="CV";
postcodeOutcodes[51]="DE";
postcodeOutcodes[52]="DY";
postcodeOutcodes[53]="LE";
postcodeOutcodes[54]="NG";
postcodeOutcodes[55]="NN";
postcodeOutcodes[56]="ST";
postcodeOutcodes[57]="WS";
postcodeOutcodes[58]="WV";
postcodeOutcodes[59]="AL";
postcodeOutcodes[60]="CB";
postcodeOutcodes[61]="CM";
postcodeOutcodes[62]="CO";
postcodeOutcodes[63]="EN";
postcodeOutcodes[64]="IG";
postcodeOutcodes[65]="IP";
postcodeOutcodes[66]="LU";
postcodeOutcodes[67]="MK";
postcodeOutcodes[68]="NR";
postcodeOutcodes[69]="PE";
postcodeOutcodes[70]="RM";
postcodeOutcodes[71]="SG";
postcodeOutcodes[72]="SS";
postcodeOutcodes[73]="WD";
postcodeOutcodes[74]="BA";
postcodeOutcodes[75]="BH";
postcodeOutcodes[76]="BS";
postcodeOutcodes[77]="DT";
postcodeOutcodes[78]="EX";
postcodeOutcodes[79]="GL";
postcodeOutcodes[80]="HR";
postcodeOutcodes[81]="PL";
postcodeOutcodes[82]="TA";
postcodeOutcodes[83]="TQ";
postcodeOutcodes[84]="TR";
postcodeOutcodes[85]="WR";
postcodeOutcodes[86]="GU";
postcodeOutcodes[87]="HA";
postcodeOutcodes[88]="HP";
postcodeOutcodes[89]="OX";
postcodeOutcodes[90]="PO";
postcodeOutcodes[91]="RG";
postcodeOutcodes[92]="SL";
postcodeOutcodes[93]="SN";
postcodeOutcodes[94]="SO";
postcodeOutcodes[95]="SP";
postcodeOutcodes[96]="UB";
postcodeOutcodes[97]="BN";
postcodeOutcodes[98]="BR";
postcodeOutcodes[99]="CR";
postcodeOutcodes[100]="CT";
postcodeOutcodes[101]="DA";
postcodeOutcodes[102]="KT";
postcodeOutcodes[103]="ME";
postcodeOutcodes[104]="RH";
postcodeOutcodes[105]="SM";
postcodeOutcodes[106]="TN";
postcodeOutcodes[107]="TW";
postcodeOutcodes[108]="E";
postcodeOutcodes[109]="EC";
postcodeOutcodes[110]="N";
postcodeOutcodes[111]="NW";
postcodeOutcodes[112]="SE";
postcodeOutcodes[113]="SW";
postcodeOutcodes[114]="W";
postcodeOutcodes[115]="WC";
postcodeOutcodes[116]="G";
postcodeOutcodes[117]="TD";
postcodeOutcodes[118]="GY";
postcodeOutcodes[119]="JE";
postcodeOutcodes[120]="BT";
postcodeOutcodes[121]="IM";
postcodeOutcodes[122]="S";
postcodeOutcodes[123]="L";

function postcodeTest( postcode )
{	postcode=stripInput(postcode,"-");
	// 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

	// Array holds the regular expressions for the valid postcodes
	var pcexp = new Array ();

	// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
	pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
	
	// Expression for postcodes: ANA NAA
	pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

	// Expression for postcodes: AANA  NAA
	pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

	// Assume we're not going to find a valid postcode
	var valid = false;
	
	// Check the string against the types of post codes
	for ( var i=0; i<pcexp.length; i++ )
	{
		if (pcexp[i].test(postcode))
		{
		  // The post code is valid - split the post code into component parts
		  pcexp[i].exec(postcode);
		  
		  // Copy it back into the original string, converting it to uppercase and
		  // inserting a space between the inward and outward codes
		  postcode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
		  
		  // Load new postcode back into the form element
		  valid = true;
		  
		  // Remember that we have found that the code is valid and break from loop
		  break;
		}
	}
  
	if( valid )
	{
		var postcodeOutcode = '';
		
		// Pull just the Outcode from the Postcode
		if( isFinite( postcode.charAt(1) ) )
		{
			postcodeOutcode = postcode.charAt(0);
		}
		else
		{
			postcodeOutcode = postcode.substring(0, 2);
		}

		// Check the Outcode is in the list
		for( index in postcodeOutcodes )
		{
			if( postcodeOutcode == postcodeOutcodes[index] )
			{
				// Appears valid
				return postcode;
			}
		}
	}

	// Is invalid
	return 0;
}

function stripInput(Str,sStr){
	sStr=eval("/"+sStr+"/gi");
	Str=Str.replace(sStr, "");
	//tmpStr=tmpStr.replace(/-/gi, "");
	return Str;
	}

function isValidEmail(strEmail) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	if (strEmail.length < 5) { return false; }
	if (!strEmail.match(re)) { return false; }
	return true;
}
