function checkemail(email)
{
	isEmail = /^[-_.0-9a-zA-Z]+@((([0-9a-zA-Z]|[0-9a-zA-Z][0-9a-zA-Z-]*[0-9a-zA-Z])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
	if(email.match(isEmail)){return true;}else{return false;}
}



function noNumbers(e)
{
var keynum;
var keychar;
var numcheck;if(window.event) // IE
  {
  keynum = e.keyCode;
  }
else if(e.which) // Netscape/Firefox/Opera
  {
  keynum = e.which;
  }
keychar = String.fromCharCode(keynum);
numcheck = /\d/;

if(e.srcElement.name!="adress"){return !numcheck.test(keychar);}
}

function onlyNumbers(e)
{
var keynum;
var keychar;
var numcheck;if(window.event) // IE
  {
  keynum = e.keyCode;
  }
else if(e.which) // Netscape/Firefox/Opera
  {
  keynum = e.which;
  }
keychar = String.fromCharCode(keynum);
numcheck = /\d/;

if(e.keyCode!=8){return numcheck.test(keychar);}
//return numcheck.test(keychar);
}


function test(id,msg)
{
msg=msg*1;
ii=0;
id1=0;
if(id==1){id1=1;}

f=window.document.forma;

//1
if(id=='purpose' || id1==1){

id='purpose';

if(f.elements[id].value=="" || f.elements[id].value==0){//f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please select the purpose of your mortgage from the list";}
	else{window.document.getElementById("err_"+id).innerHTML="";}

if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}

//2
if(id=='value' || id1==1){
id='value';

if(f.elements[id].value=="" || f.elements[id].value==0 || !f.elements[id].value.match(/[0-9]/i) || f.elements[id].value.match(/[a-zA-Z]/i))
	{
	f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please enter Property Value, e.g. &pound;225,000";
	}
	else{

if(f.elements[id].value<25000 || f.elements[id].value>25000000){f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Must be between &pound;25,000 and &pound;25,000,000";}
	else{window.document.getElementById("err_"+id).innerHTML="";}

	    }

if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}

//3
if(id=='amount' || id1==1){
id='amount';

if(f.elements[id].value=="" || f.elements[id].value==0 || !f.elements[id].value.match(/[0-9]/i) || f.elements[id].value.match(/[a-zA-Z]/i))
	{
	f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please enter Property Value, e.g. &pound;225,000";
	}
	else{

if(f.elements[id].value<25000 || f.elements[id].value>25000000){f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Must be between &pound;25,000 and &pound;25,000,000";}
	else{window.document.getElementById("err_"+id).innerHTML="";}

	    }
v_a=f.elements[id].value*1;
v_v=window.document.getElementById("value").value*0.8;

if(v_a>v_v){f.elements[id].focus();
window.document.getElementById("err_"+id).innerHTML="Must be <= "+v_v.toFixed(2);}


if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}

//4
if(id=='monthly' || id1==1){
id='monthly';

if(f.elements[id].value=="" || f.elements[id].value==0 || !f.elements[id].value.match(/[0-9]/i) || f.elements[id].value.match(/[a-zA-Z]/i))
	{
	f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please enter Monthly rental income";
	}

if((f.amount.value*6/100)/12>f.elements[id].value){f.elements[id].focus();
	aaa=(f.amount.value*6/100)/12;
	window.document.getElementById("err_"+id).innerHTML="Monthly Rental Income must more than "+aaa.toFixed(2);}
	else{window.document.getElementById("err_"+id).innerHTML="";}


if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}

//5
if(id=='long' || id1==1){
id='long';

if(f.elements[id].value=="" || f.elements[id].value==0){//f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please enter Number of Years";}
	else{window.document.getElementById("err_"+id).innerHTML="";}

if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}

//6
if(id=='arrears' || id1==1){
id='arrears';
if(window.document.getElementById("arrears1").checked==false && window.document.getElementById("arrears2").checked==false){//f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="You must indicate if you, or your partner (if applicable) have any CCJ's, Bankruptcy, IVAs or mortgage Arrears";}
	else{window.document.getElementById("err_"+id).innerHTML="";}

if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}

//7
if(id=='title2' || id1==1){
id='title2';

if(f.elements[id].value=="" || f.elements[id].value==0){//f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please select your Title from the list";}
	else{window.document.getElementById("err_"+id).innerHTML="";}

if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}


//8
if(id=='name' || id1==1){
id='name';

if(f.elements[id].value=="" || f.elements[id].value==0 || f.elements[id].value.match(/[0-9]/i) || !f.elements[id].value.match(/[a-zA-Z]/i))
	{
	f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please enter your Forename";
	}
	else{window.document.getElementById("err_"+id).innerHTML="";}
if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}

//9
if(id=='surname' || id1==1){
id='surname';

if(f.elements[id].value=="" || f.elements[id].value==0 || f.elements[id].value.match(/[0-9]/i) || !f.elements[id].value.match(/[a-zA-Z]/i))
	{
	f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please enter your Surname";
	}
	else{window.document.getElementById("err_"+id).innerHTML="";}

if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}


//10
if(id=='day' || id=='month' || id=='year' || id1==1){
a=0;
id='day'; if(f.elements[id].value=="" || f.elements[id].value==0){a++;}
id='month'; if(f.elements[id].value=="" || f.elements[id].value==0){a++;}
id='year'; if(f.elements[id].value=="" || f.elements[id].value==0){a++;}

if(a>0){window.document.getElementById("err_dmy").innerHTML="Please select your Date of Birth from the list";}
	else{window.document.getElementById("err_dmy").innerHTML="";}

if(window.document.getElementById("err_dmy").innerHTML!=""){ii++;}

}

//11
if(id=='adress' || id1==1){
id='adress';

if(f.elements[id].value=="" || f.elements[id].value==0 || !f.elements[id].value.match(/[0-9]/i) || !f.elements[id].value.match(/[a-zA-Z]/i))
	{
	f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please enter your Address";
	}
	else{window.document.getElementById("err_"+id).innerHTML="";}


if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}
}


//12 - postcode_zip
if(id=='postcode_zip' || id1==1){
id='postcode_zip';


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 )
{
	// 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 -1;
			}
		}
	}

	// Is invalid
	return 0;
}

function PostcodeValidator()
{
	// Get the postcode as a string from the text box
	var postcode = f.elements[id].value;

if(postcode=="" || postcode==0){window.document.getElementById("err_"+id).innerHTML="Please enter your Postcode";}

else{

$return_key=postcodeTest(postcode);

if($return_key==0){window.document.getElementById("err_"+id).innerHTML="Please enter your correct Postcode";}
if($return_key==-1){window.document.getElementById("err_"+id).innerHTML="";}
	
    }

}

window.document.getElementById("err_"+id).innerHTML="";

PostcodeValidator();

if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}


//13
if(id=='tel' || id1==1){
id='tel';
window.document.getElementById("err_"+id).innerHTML="";

if(f.elements[id].value=="" || f.elements[id].value==0 || !f.elements[id].value.match(/[0-9]/i) || f.elements[id].value.match(/[a-zA-Z]/i))
	{
	f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please enter your Telephone Number";
	}
	else{

if(f.elements[id].value.length<10 || f.elements[id].value.length>11){f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="UK telephone numbers should contain 10 or 11 digits";}

if((f.elements[id].value.length==10 || f.elements[id].value.length==11) && f.elements[id].value.charAt(0)!=0){f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="The telephone number should start with a 0";}

if((f.elements[id].value.length==10 || f.elements[id].value.length==11) && f.elements[id].value.charAt(0)!=0 && f.elements[id].value.match(/123456789/)!=null){f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="The telephone number is either invalid or inappropriate";}
	
}

if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}

}

//14
if(id=='tel2' || id1==1){
id='tel2';
window.document.getElementById("err_"+id).innerHTML="";

if(f.elements[id].value=="" || f.elements[id].value==0 || !f.elements[id].value.match(/[0-9]/i) || f.elements[id].value.match(/[a-zA-Z]/i))
	{
	f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please enter your Telephone Number";
	}
	else{

if(f.elements[id].value.length<10 || f.elements[id].value.length>11){f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="UK telephone numbers should contain 10 or 11 digits";}

if((f.elements[id].value.length==10 || f.elements[id].value.length==11) && f.elements[id].value.charAt(0)!=0){f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="The telephone number should start with a 0";}

if((f.elements[id].value.length==10 || f.elements[id].value.length==11) && f.elements[id].value.charAt(0)!=0 && f.elements[id].value.match(/123456789/)!=null){f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="The telephone number is either invalid or inappropriate";}
}

if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}
}


//15
if(id=='email' || id1==1){
id='email';

window.document.getElementById("err_"+id).innerHTML="";

if(f.elements[id].value=="" || f.elements[id].value==0 || !checkemail(f.elements[id].value)){f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="Please enter your e-mail address";}
	
if(f.elements[id].value!="" && f.elements[id].value!=0 && !checkemail(f.elements[id].value)){f.elements[id].focus();
	window.document.getElementById("err_"+id).innerHTML="The e-mail address is either invalid or inappropriate, please enter your correct e-mail address";}

if(window.document.getElementById("err_"+id).innerHTML!=""){ii++;}
}

	return ii;
}


function Send()
{

i=test(1);

if(i>0){alert("Please correct the errors and press submit button"); return false;}else{return true;}
}