function fvalidator(){

// Global vars
	var maxltv = 80;

// Reset errors
	var errs='';
	document.getElementById("errorfield").innerHTML = '';
	document.getElementById("cf3_field_1_error").innerHTML = '';
	document.getElementById("cf3_field_2_error").innerHTML = '';
	document.getElementById("cf3_field_3_error").innerHTML = '';
	document.getElementById("cf3_field_5_error").innerHTML = '';
	document.getElementById("cf3_field_6_error").innerHTML = '';
	document.getElementById("cf3_field_7_error").innerHTML = '';
	document.getElementById("cf3_field_8_error").innerHTML = '';
	document.getElementById("cf3_field_9_error").innerHTML = '';
	document.getElementById("cf3_field_10_error").innerHTML = '';
	document.getElementById("cf3_field_11_error").innerHTML = '';
	document.getElementById("cf3_field_12_error").innerHTML = '';

	borrowamount = document.getElementById('cf3_field_1').value;
	propertyvalue = document.getElementById('cf3_field_2').value;
	firstname = document.getElementById('cf3_field_5').value;
	surname = document.getElementById('cf3_field_6').value;
	dob = document.getElementById('cf3_field_7').value;
	housename = document.getElementById('cf3_field_8').value;
	postcode = document.getElementById('cf3_field_9').value;
	mobilephone = document.getElementById('cf3_field_10').value;
	homeworkphone = document.getElementById('cf3_field_11').value;
	emailaddress = document.getElementById('cf3_field_12').value;


	ccj = ""
	len = document.cforms3form.cf3_field_3.length;

	for (i = 0; i <len; i++) {
	if (document.cforms3form.cf3_field_3[i].checked) {
		ccj = document.cforms3form.cf3_field_3[i].value;
		}
	}


ltv = (borrowamount/propertyvalue * 100);

//firstname = document.getElementById('cf3_field_1').selectedIndex;

if (borrowamount < 25000){
    errs = '<FONT COLOR="#ff0000">Minimum allowed=25,000GBP</FONT>';
    document.getElementById("cf3_field_1_error").innerHTML = errs;
}

if (borrowamount == ''){
    errs = '<FONT COLOR="#ff0000">Cannot be blank</FONT>';
    document.getElementById("cf3_field_1_error").innerHTML = errs;
}


if (propertyvalue == ''){
    errs = '<FONT COLOR="#ff0000">Cannot be blank</FONT>';
    document.getElementById("cf3_field_2_error").innerHTML = errs;
}


if (borrowamount != '' && propertyvalue != ''){
if (ltv > maxltv){
    errs = "Loan to value must not exceed 80 percent";
    document.getElementById("cf3_field_1_error").innerHTML = '<FONT COLOR="#ff0000"><< Loan to value must</FONT>';
    document.getElementById("cf3_field_2_error").innerHTML = '<FONT COLOR="#ff0000"><< not exceed 80 percent</FONT><br/>&nbsp;<br/>';
}
}

if (ccj == ''){
    errs = '<FONT COLOR="#ff0000">Must select an option</FONT>';
    document.getElementById("cf3_field_3_error").innerHTML = errs;
}


if (firstname == ''){
    errs = '<FONT COLOR="#ff0000">Cannot be blank</FONT>';
    document.getElementById("cf3_field_5_error").innerHTML = errs;
}

if (surname == ''){
    errs = '<FONT COLOR="#ff0000">Cannot be blank</FONT>';
    document.getElementById("cf3_field_6_error").innerHTML = errs;
}

if (dob == '' || dob == ' Date / Month / Year'){
    errs = '<FONT COLOR="#ff0000">Cannot be blank</FONT>';
    document.getElementById("cf3_field_7_error").innerHTML = errs;
}

// check date of birth properly

if (dob != '' && dob != ' Date / Month / Year'){
if (dob.length !== 10) {
    errs = '<FONT COLOR="#ff0000">Should be dd/mm/yyyy</FONT>';
    document.getElementById("cf3_field_7_error").innerHTML = errs;
	}
if (dob.substring(2, 3) != '/' || dob.substring(5, 6) != '/') { 
    errs = '<FONT COLOR="#ff0000">Should be dd/mm/yyyy</FONT>';
    document.getElementById("cf3_field_7_error").innerHTML = errs;
	} 
}


if (housename == ''){
    errs = '<FONT COLOR="#ff0000">Cannot be blank</FONT>';
    document.getElementById("cf3_field_8_error").innerHTML = errs;
}

if (postcode == ''){
    errs = '<FONT COLOR="#ff0000">Cannot be blank</FONT>';
    document.getElementById("cf3_field_9_error").innerHTML = errs;
}

if (postcode != '' && isPostcode(postcode) == false){
    errs = '<FONT COLOR="#ff0000">Invalid postcode detected</FONT>';
    document.getElementById("cf3_field_9_error").innerHTML = errs;
}




if (mobilephone == '' && homeworkphone == ''){
    errs = "No phone given";
    document.getElementById("cf3_field_10_error").innerHTML = '<FONT COLOR="#ff0000"><< Please provide at least</FONT>';
    document.getElementById("cf3_field_11_error").innerHTML = '<FONT COLOR="#ff0000"><< one contact number</FONT><br/>&nbsp;<br/>';

}

if (emailaddress == ''){
    errs = '<FONT COLOR="#ff0000">Cannot be blank</FONT>';
    document.getElementById("cf3_field_12_error").innerHTML = errs;
}

if (emailaddress != '' && IsEmail(emailaddress) == false){
    errs = '<FONT COLOR="#ff0000">Invalid email detected</FONT>';
    document.getElementById("cf3_field_12_error").innerHTML = errs;
}


if (errs == ''){
   cforms_validate('3', false);
} else {
    document.getElementById("errorfield").innerHTML = "<center><font color='#ff0000'>ERRORS DETECTED! Please correct the errors and try again.</font></center>";
return false;
}
}



function isTextKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 96 && charCode < 123 || charCode > 64 && charCode < 91 || charCode == 32 || charCode == 45){
            return true;
	}else {
         return false;
      }
  }

function isDateKey(evt){
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 47 && charCode < 58 || charCode == 47){
            return true;
		}else {
			return false;
      	}
  }

function isNumberKey(evt){
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 47 && charCode < 58 || charCode == 46){
            return true;
		}else {
			return false;
      	}
  }

function isPhoneKey(evt){
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 47 && charCode < 58 || charCode == 40 || charCode == 41 || charCode == 32 || charCode == 43){
            return true;
		}else {
		return false;
			
      	}
  }

function isPostcode(str)
{
	if(str.length > 10 || str.length < 3){
	        return false;    
	} else {
		return true;
	}
}

function isPostcodeKey(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 96 && charCode < 123 || charCode > 64 && charCode < 91 || charCode == 32 || charCode > 47 && charCode < 58){
	        return true;    
	} else {
		return false;
	}
}

function IsEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		    return false 
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   
	        return false 
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false 
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false 
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false 
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false 
		 }

 		 return true					
	}


 function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function format(input){
var num = input.value.replace(/\,/g,'');
if(!isNaN(num)){
if(num.indexOf('.') > -1){ 
num = num.split('.');
num[0] = num[0].toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
if(num[1].length > 2){ 
alert('You may only enter two decimal numbers');
num[1] = num[1].substring(0,num[1].length-1);
} input.value = num[0]+'.'+num[1]; 
} else{ input.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'') };
}
else{ var temp = input.value; 
alert('You may enter only numbers in this field!');
input.value = temp.replace(/[^\$\d\.]*/g,'');
}
}


function validate(){
	var frm = document.f1;

	
	//valdate title
	if(document.f1.title.options[document.f1.title.selectedIndex].value == 0){
		alert("Please select Title.");
		document.f1.title.focus();
		return false;}
	
	
	//validate first name
	if( document.f1.fname.value == "" ){
		alert("Please fill First name.");
		document.f1.fname.focus();
		return false;}

	if(!ValidateName(document.f1.fname.value,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-.")){
	    alert("First name can contain letters only");
		document.f1.fname.focus();
	        return false;}
			
	
	//validate surname
	if( document.f1.sname.value == "" ){
		alert("Please fill Surname.");
		document.f1.sname.focus();
		return false;}
		
	if(!ValidateName(document.f1.sname.value,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-.")){
        alert("Surname can contain letters only");
		document.f1.sname.focus();
	        return false;}
			
	//validate first line
	if( document.f1.address1.value == "" ){
		alert("Please fill in your house name/number.");
		document.f1.address1.focus();
		return false;}
		
	
	//validate postal code
	if( document.f1.pcode.value == "" ){
		alert("Please fill Postal code number.");
		document.f1.pcode.focus();
		return false;
	}

	//validate phone number
	if(document.f1.pnumberday.value == ""){
		alert("Please fill in a day contact number.");
		document.f1.pnumberday.focus();
		return false;
	}
	if(ValidateName(document.f1.pnumberday.value,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.")){
		alert("Please use numbers only.");
		document.f1.pnumberday.focus();
		return false;
	}
	
	//validate email
	var emailID=document.f1.email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID");
		emailID.focus();
		return false;
	}
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}

	//validate estimated property value
	if(document.f1.eproperty.value == ""){
		alert("Please fill in the estimated value of the property.");
		document.f1.eproperty.focus();
		return false;
	}
	if(ValidateName(document.f1.eproperty.value,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@-_")){
		alert("Please use numbers only.");
		document.f1.eproperty.focus();
		return false;
	}


	//validate amount to borrow
	if(document.f1.mborrow.value == ""){
		alert("Please fill the amount you wish to borrow");
		document.f1.mborrow.focus();
		return false;
	}
	if(ValidateName(document.f1.mborrow.value,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@-_")){
		alert("Please use numbers only.");
		document.f1.mborrow.focus();
		return false;
	}
	
  var maxltv;
  var mborrow;
  var eproperty;
  var maxcanborrow;
  var maxcanborrowcommas;

  maxltv = (document.f1.maxltv.value);
  wantborrow = (document.f1.mborrow.value);
  eproperty = (document.f1.eproperty.value);


  wantborrow=wantborrow.replace(/\,/g,'');
  eproperty=eproperty.replace(/\,/g,'');
  
  wantborrow = parseFloat(wantborrow);
  eproperty = parseFloat(eproperty);
  maxltv = parseFloat(maxltv);
  
   
  maxcanborrow = eproperty * (maxltv/100);
  maxcanborrowcommas = addCommas(maxcanborrow);
  
   if (wantborrow > maxcanborrow){
   alert("The product you have selected allows " + maxltv + "% LTV. Your maximum borrowing would be £"+ maxcanborrowcommas);
   document.f1.mborrow.value=addCommas(maxcanborrow);
   document.f1.mborrow.focus();
   return (false);
  }


	//validate number of properties
	if(document.f1.numportfolio.options[document.f1.numportfolio.selectedIndex].value == 0){
		alert("Please select number of properties in portfolio");
		document.f1.numportfolio.focus();
		return false;}
	

	//validate agreement
	if (document.f1.agreement.checked == false){
		alert("Please check the box to agree to be contacted by The Buy To Let Business");
		document.f1.agreement.focus();
		return false;}
		
	loading.style.visibility = "visible";
	return true;
		

	}
	
function ValidateName(NumStr, String)
	{    
	for(var Idx=0; Idx<NumStr.length; Idx++)    
	{        
	var Char = NumStr.charAt(Idx);        
	var Match = false;        
	for(var Idx1=0; Idx1<String.length; Idx1++)        
	{            
	   if(Char == String.charAt (Idx1))                
	       Match = true;        
	}        
	if (!Match)           
 	    return false;    
	}    
	return true;
	}
 
 
 
 
 
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


