 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					
	}

