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

//Form
//Javascript function to format a number as £ sterling (rounded to the nearest pound)
function sterling(num) {
num=Math.round(num).toString(); right=""
while (num.length>3) {right=","+num.substr(num.length-3)+right; num=num.substr(0,num.length-3);}
return num+right;
}

//Javascript function to remove currencyformat from a currency string - leaving just a numeric value
function sterling2num(str) {
	// * 1  at the end converts it to a number
	num = str.replace(/[£, ]/g,'') * 1;
	if (isNaN(num)) {return NaN;} else {return num;}
}

function trim(str) {
while (str.substring(0,1) == ' ') {str = str.substring(1, str.length);}
while (str.substring(str.length-1, str.length) == ' ') {str = str.substring(0,str.length-1);}
return str;
}


var discountprice = 0;

function marketvalue_validate() {
	discountprice=0;
	valid=true;
	helptext='';
	input=document.getElementById('marketvalue');
	input.value=trim(input.value);
	if (input.value==null||input.value=='') {
		valid=false; helptext+='Please enter your estimate of the current market value of this property. ';
	} else {
		temp=input.value.replace(/[, ]/g,'');
		temp=temp.replace(/([k]|thousand)/gi,'000');
		if (temp.substr(0,1)=='£') {temp=temp.substr(1);}
		if (temp.search(/^[0-9]+(\.[0-9]+)?$/)==-1) {valid=false; helptext+='Invalid number'; }
		if (temp<1000) {valid=false; helptext+='This price seems extremely low.  Are you sure you don\'t mean ' + sterling(temp * 1000) + '?'; }
		else if (temp<5000) {valid=false; helptext+='This price seems extremely low.  Are you sure your property is worth just ' + sterling(temp) + '?'; }
		if (temp>1999999) {valid=false; helptext+='Sorry, but we are not in a position to buy such a high value property. '; }
	}
	if (valid) {
		input.value=sterling(temp);
		discountprice = sterling(temp * 0.75);
		//It's ok, so ask the 75% question
		document.getElementById('discountprice_value').innerHTML=' ('+discountprice+' GBP) ';
	} else {
		document.getElementById('discountprice_value').innerHTML='';
		alert(helptext);
	}
	return valid;
}

function acceptprice_validate() {
	valid=true;
	helptext=''; input=document.getElementById('acceptprice');
	input.value=trim(input.value);
	if (input.value==null||input.value=='') {
		valid=false; helptext+='Please enter the minimum price you would be prepared to accept. ';
	} else {
		temp=input.value.replace(/[, ]/g,'');
		temp=temp.replace(/(k|thousand)/gi,'000');
		if (temp.substr(0,1)=='£') {temp=temp.substr(1);}
		if (temp.search(/^[0-9]+(\.[0-9]+)?$/)==-1) {valid=false; helptext+='Invalid number'; }
		if (temp<1000) {valid=false; helptext+='This price seems extremely low.  Are you sure you don\'t mean ' + sterling(temp * 1000) + '?'; }
		else if (temp<5000) {valid=false; helptext+='This price seems extremely low.  Would you really accept just ' + sterling(temp) + '?'; }
		if (temp>1999999) {valid=false; helptext+='Sorry, but we are not in a position to buy such a high value property. '; }
		//compare against marketvalue:
		input2=document.getElementById('marketvalue');
		if (input2.value!=null&&input2.value!='') {
			temp2=input2.value.replace(/[£, ]/g,'');
			//if ((temp/temp2)>0.99) {valid=false; helptext+='We cannot help you get a quick CASH sale unless you are willing to sell at least 1% below market value. ';}
			if ((temp/temp2)>1) {valid=false; helptext+='Cannot sell property for more than market value. ';}
		}
	}
	if (valid) {input.value=sterling(temp);} else { alert(helptext); }	
	return valid;
}

function mortgageamount_validate() {
	valid=true;
	helptext=''; input=document.getElementById('mortgageamount');
	input.value=trim(input.value);
	if (input.value==null||input.value=='') {} else {
		temp=input.value.replace(/[, ]/g,'');
		temp=temp.replace(/([k]|thousand)/gi,'000');
		if (temp.substr(0,1)=='£') {temp=temp.substr(1);}
		if (temp.search(/^[0-9]+(\.[0-9]+)?$/)==-1) {valid=false; helptext+='Not a valid number'; }
		if (temp<0) {valid=false; helptext+='Cannot enter a negative number'; }
		if (temp>1999999) {valid=false; helptext+='Number too big. '; }
		if (valid) {input.value=sterling(temp);}
	}
	//check LTV
	input1=document.getElementById('acceptprice'); input2=document.getElementById('securedloans');
	price=input1.value.replace(/[£, ]/g,'');
	if (price!=null&&price!='') {
		loans=input2.value.replace(/[£, ]/g,''); mort=temp;
		if (loans==null||loans=='') {loans=0;} if (mort==null||mort=='') {mort=0;} debt=Number(loans)+Number(mort);
		if (debt > price) {valid=false; helptext+='Sorry but you cannot sell your house for ' + sterling(price) + ' as it is less than yout total debts of ' + sterling(debt) + '. '; }
	}
	if (valid) {} else { alert(helptext); }
	return valid;
}

function securedloans_validate() {
	valid=true;
	helptext=''; input=document.getElementById('securedloans');
	input.value=trim(input.value);
	if (input.value==null||input.value=='') {} else {
		temp=input.value.replace(/[, ]/g,'');
		temp=temp.replace(/([k]|thousand)/gi,'000');
		if (temp.substr(0,1)=='£') {temp=temp.substr(1);}
		if (temp.search(/^[0-9]+(\.[0-9]+)?$/)==-1) {valid=false; helptext+='Not a valid number'; }
		if (temp<0) {valid=false; helptext+='Cannot enter a negative number. '; }
		if (temp>1999999) {valid=false; helptext+='Number too big. '; }
		if (valid) {input.value=sterling(temp);}
	}
	//check LTV
	input1=document.getElementById('acceptprice'); input2=document.getElementById('mortgageamount');
	price=input1.value.replace(/[£, ]/g,'');
	if (price!=null&&price!='') {
		mort=input2.value.replace(/[£, ]/g,''); loans=temp;
		if (loans==null||loans=='') {loans=0;} if (mort==null||mort=='') {mort=0;} debt=Number(loans)+Number(mort);
		if (debt > price) {valid=false; helptext+='Sorry but you cannot sell your house for ' + sterling(price) + ' as it is less than yout total debts of ' + sterling(debt) + '. '; }
	}
	if (valid) {} else { alert(helptext); }
	return valid;
}

function showAcceptAmount(showBool)
{
	if(showBool)
	{
		document.getElementById('minPriceAcc').style.display = '';
	}else
	{
		document.getElementById('minPriceAcc').style.display = 'none';
	}
}