﻿//Javascript function to trim whitespace from the beginning or end of a string
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;
}

//Javascript function to trim whitespace from the begginning and end of a string, and replace multiple spaces by single ones in the middle
function trimm(str) {
str = trim(str).replace(/\s+/g," ");
return str;
}


//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 "&pound;"+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;}
}


//Javascript function to check the validity of a UK phone number
function checkphonenumber(phonenumbertextbox) {
phnum=phonenumbertextbox.value.toString().replace(/[ -()]/g,''); //remove whitespace, brackets or hyphens
phnum=phnum.replace(/^(\+44|0044)/,'0'); //remove UK country code if it's been added
if (phnum.search(/^0[1-9][0-9]{8,9}$/)==-1) {
	//looks like an invalid number
	return 'This appears not to be a valid UK phone number.  Please check and try again. ';
}
if (phnum.search(/^09/)!=-1) {
	//premium rate number - ban it!
	return 'Sorry but we will not accept this number as it appears to be a premium rate line.  Please enter an alternative number. ';
}
//remove country code on form, but leave hyphens, spaces and brackets so the user can recognise their own number clearly
phonenumbertextbox.value=phonenumbertextbox.value.toString().replace(/^(\+44|0044)/,'0');
return 'ok';}




function situation_validate() {
	valid=true; helptext='.'; input=document.getElementById('situation');
	if (input.value==null||input.value=='') {valid=false; helptext+='Please select an item from the list. ';}
	if (valid) {} else {}
	document.getElementById('situation_feedback').innerHTML=helptext;
	return valid;
}

function name_validate(arg) {
	valid=true; helptext='.';
	input_t=document.getElementById('title');
	input_f=document.getElementById('forename');
	input_s=document.getElementById('surname');
	input_t_other=document.getElementById('title_other');
	if (input_t.value=='Other') {input_t_other.style.display='inline'; ti=input_t_other.value; } else {input_t_other.style.display='none'; ti=input_t.value; }
	ti=trim(ti);
	input_f.value=trim(input_f.value);
	input_s.value=trim(input_s.value);
	if (ti==null||ti=='') {valid=false; helptext+='Please enter a title. ';}
	if ((input_f.value==null||input_f.value=='')&&(arg<2)) {valid=false; helptext+='Please enter your first name. ';}
	if ((input_s.value==null||input_s.value=='')&&(arg<1)) {valid=false; helptext+='Please enter your last name. ';}
	if (valid) {} else {}
	document.getElementById('name_feedback').innerHTML=helptext;
	return valid;
}

function email_validate() {
	valid=true; helptext='.'; input=document.getElementById('email');
	input.value=trim(input.value);
	if (input.value==null||input.value=='') {
		valid=false; helptext+='Please enter your email address. ';
	} else {
		if (input.value.search(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)==-1) {
			valid=false; helptext+='This appears to be an invalid email address - please check and try again.  ';
		}
	}
	if (valid) {} else {}
	document.getElementById('email_feedback').innerHTML=helptext;
	return valid;
}

function daytimephone_validate() {
	valid=true; helptext='.'; input=document.getElementById('daytimephone');
	input.value=trim(input.value);
	if (input.value==null||input.value=='') {
		valid=false; helptext+='Please enter your daytime phone number. ';
	} else {
		phonestatus=checkphonenumber(input);
		if (phonestatus!='ok') {valid=false; helptext+=phonestatus;}
	}
	input_ex=document.getElementById('daytimephone_ex');
	input_ex.value=trim(input_ex.value);
	if (!(input_ex.value==null||input_ex.value=='')) {
		if (input.value==null||input.value=='') {
			valid=false; helptext+='Cannot enter an extension without a main number. ';
		}
		if (input_ex.value.search(/^[0-9]+$/)==-1) {
			valid=false; helptext+='This looks like an invalid extension number - please only use numbers. ';
		}
	}
	if (valid) {} else {}
	document.getElementById('daytimephone_feedback').innerHTML=helptext;
	return valid;
}

function eveningphone_validate() {
	valid=true; helptext='.'; input=document.getElementById('eveningphone');
	input.value=trim(input.value);
	if (input.value==null||input.value=='') {} else {
		phonestatus=checkphonenumber(input);
		if (phonestatus!='ok') {valid=false; helptext+=phonestatus;}
	}
	input_ex=document.getElementById('eveningphone_ex');
	input_ex.value=trim(input_ex.value);
	if (!(input_ex.value==null||input_ex.value=='')) {
		if (input.value==null||input.value=='') {
			valid=false; helptext+='Cannot enter an extension without a main number. ';
		}
		if (input_ex.value.search(/^[0-9]+$/)==-1) {
			valid=false; helptext+='This looks like an invalid extension number - please only use numbers. ';
		}
	}
	if (valid) {} else {}
	document.getElementById('eveningphone_feedback').innerHTML=helptext;
	return valid;
}

function mobilephone_validate() {
	valid=true; helptext='.'; input=document.getElementById('mobilephone');
	input.value=trim(input.value);
	if (input.value==null||input.value=='') {} else {
		phonestatus=checkphonenumber(input);
		if (phonestatus!='ok') {valid=false; helptext+=phonestatus;}
	}
	if (valid) {} else {}
	document.getElementById('mobilephone_feedback').innerHTML=helptext;
	return valid;
}

function contacttime_validate() {return true;}

function address_validate(arg) {
	valid=true; helptext='.';
	
	// address1
	input=document.getElementById('address1');
	input.value=trim(input.value);
	if (input.value==null||input.value=='') {valid=false; helptext+='Please enter the first line of your address. ';}
	
	// postcode
	if (arg<1) {
		input=document.getElementById('postcode');
		// put postcode in uppercase, and remove any spaces
		temp=input.value.toUpperCase().replace(/\s+/g,"");
		if (temp==null||temp=='') {
			valid=false; helptext+='Please enter your postcode. ';
		} else {
			// NOTE some central London postcodes are weird like EC1A 2BZ- be careful!
			if (temp.search(/^[A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]?[0-9][ABD-HJLN-UW-Z]{2}$/)==-1) {
				valid=false; helptext+='This appears to be an invalid postcode - please check and try again.  ';
			}
			else {
			// postcode is ok, so replace the value on the form with the sanitized version (add the space in the right place too)
			temp=temp.substring(0,temp.length-3)+" "+temp.substring(temp.length-3,temp.length);
			input.value=temp;
			}
		}
	}
	
	if (valid) {} else {}
	document.getElementById('address_feedback').innerHTML=helptext;
	
	return valid;
}
	
function propertytype_validate() {
	valid=true; helptext='.'; input=document.getElementById('propertytype');
	if (input.value==null||input.value=='') {valid=false; helptext+='Please select an item from the list. ';}
	if (valid) {} else {}
	document.getElementById('propertytype_feedback').innerHTML=helptext;
	return valid;
}

function exla_validate() { return true; }

function tenure_validate() { return true; }

function noofbedrooms_validate() {
	valid=true; helptext='.'; input=document.getElementById('noofbedrooms');
	input.value=trim(input.value);
	temp=input.value;
	temp=temp.replace(/one/i,'1'); temp=temp.replace(/two/i,'2'); temp=temp.replace(/three/i,'3'); temp=temp.replace(/four/i,'4'); temp=temp.replace(/five/i,'5');
	input.value=temp;
	if (input.value==null||input.value=='') {
		valid=false; helptext+='Please enter the number of bedrooms. ';
	} else {
		if ((input.value.search(/^[0-9]*$/)==-1)||(input.value<0)||(input.value>12)) {
			valid=false; helptext+='Invalid number. ';
		}
	}
	if (valid) {} else {}
	document.getElementById('noofbedrooms_feedback').innerHTML=helptext;
	return valid;
}

function noofbathrooms_validate() {
	valid=true; helptext='.'; input=document.getElementById('noofbathrooms');
	input.value=trim(input.value);
	temp=input.value;
	temp=temp.replace(/one/i,'1'); temp=temp.replace(/two/i,'2'); temp=temp.replace(/three/i,'3'); temp=temp.replace(/four/i,'4'); temp=temp.replace(/five/i,'5');
	input.value=temp;
	if (input.value==null||input.value=='') {} else {
		if ((input.value.search(/^[0-9]*$/)==-1)||(input.value<0)||(input.value>12)) {
			valid=false; helptext+='Invalid number. ';
		}
	}
	if (valid) {} else {}
	document.getElementById('noofbathrooms_feedback').innerHTML=helptext;
	return valid;
}

function parking_validate() { return true; }

function garden_validate() { return true; }

function features_validate() { return true;}

function condition_validate() { return true; }

function workneeded_validate() { return true; }

function howlongonmarket_validate() { return true; }

//global variable for the discounted price
var discountprice=0;

function marketvalue_validate() {
	discountprice=0;
	valid=true; 
	helptext=''; 
	input=document.getElementById('q118');
	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 {
		input.className = 'errorInput';
		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.';
			if (temp > 0) helptext+=' 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.className = 'text';
		//input.value=sterling(temp);
		discountprice = sterling(temp * 0.75);
		//It's ok, so ask the 75% question
		document.getElementById('discountprice_value').innerHTML=' ('+discountprice+') ';
	} else {
		document.getElementById('discountprice_value').innerHTML='';
	}
	document.getElementById('marketvalue_feedback').innerHTML=helptext;
	return valid;
}

function valuebasis_validate() { return true; }

function acceptbmv_click() {
	//Choices are [0] Yes, [1] No and [2] Maybe
	//If they've clicked on no then we warn them
	if (document.q_form_92535217242.q121_Acceptanofferofupto7[1].checked) { // no
		alert("Please note: We will still do our best to find you a buyer, but if you are still unwilling to accept an offer of 75% of your property's market value, then we may not be able to guarantee you a QUICK CASH SALE");
		document.getElementById('tr_acceptprice').style.display='';
	} else { // yes or maybe
		document.getElementById('tr_acceptprice').style.display='none';
		if (!document.getElementById('q118').value)
			document.q_form_92535217242.q122_Ifnowhatwouldbethemi.value = clearNumber(discountprice);
	}
}

function clearNumber(num) {
	if (num != null)
		num = num.replace(/[&pound;,\. ]/g,'');	
	return num;
}

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('q118');
		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=temp; input.className='text';} else {input.className='errorInput';}
	document.getElementById('acceptprice_feedback').innerHTML=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 {}
	document.getElementById('mortgageamount_feedback').innerHTML=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 {}
	document.getElementById('securedloans_feedback').innerHTML=helptext;
	return valid;
}

function sellwhen_validate(arg) { return true; }

function rentback_validate(arg) { return true; }



function validatepage1() {
	allvalidsofar=true;
	thisonevalid=situation_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=name_validate(0); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=email_validate(0); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=daytimephone_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=eveningphone_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=mobilephone_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=contacttime_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	if (allvalidsofar) {} else {alert('Please complete all required fields before you continue'); }
	return allvalidsofar;
}

function validatepage2() {
	allvalidsofar=true;
	thisonevalid=address_validate(0); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=propertytype_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=exla_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=tenure_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=noofbedrooms_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=noofbathrooms_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=garden_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	if (allvalidsofar) {} else {alert('Please complete all required fields before you continue'); }
	return allvalidsofar;
}

function validatepage3() {
	allvalidsofar=true;
	thisonevalid=features_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=condition_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=workneeded_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	if (allvalidsofar) {} else {alert('Please complete all required fields before you continue'); }
	return allvalidsofar;
}

function validatepage4() {
	allvalidsofar=true;
	thisonevalid=howlongonmarket_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=marketvalue_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=acceptprice_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=mortgageamount_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=securedloans_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=sellwhen_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	thisonevalid=rentback_validate(); allvalidsofar=allvalidsofar&&thisonevalid;
	if (allvalidsofar) {} else {alert('Please complete all required fields before you continue'); }
	return allvalidsofar;
}



//global variable for the current page number
var currentpageno = 1;

function formback() {
	prevpageno=currentpageno-1;
	document.getElementById('formpage' + currentpageno).style.display='none';
	document.getElementById('formpage' + prevpageno).style.display='block';
	//window.scrollTo(0,0);
	currentpageno = prevpageno;
}

function formnext() {
	eval('pageok = validatepage' + currentpageno + '();');
	if (pageok) {
		nextpageno=currentpageno+1;
		document.getElementById('formpage' + currentpageno).style.display='none';
		document.getElementById('formpage' + nextpageno).style.display='block';
		//window.scrollTo(0,0);
		currentpageno = nextpageno;
	}
}
