function trim(s) {
  s = s.replace(/^\s*/,'').replace(/\s*$/, '');
  return s;
}

function CommaRem(str) {
	re = /,/gi;
	RetVal = str.replace(re, "")
	return RetVal
}

function CurrencyStringToNumber(str)
{
	str = trim(str);

	idxDecimal = str.lastIndexOf(".");
	if (idxDecimal>-1 && (idxDecimal==str.length-3 || idxDecimal==str.length-2))
	{
		str = str.substring(0,idxDecimal)
	}

	// strip out spaces, commas and decimal points
	str = str.replace(/[\,\.\ \£]/g, '');

	return parseInt(str);
}

function Validate(appformat) {
	// for checking minimum income required for loan:
	var _income = -1.0;
	var _partner_income = -1.0;

	Obj = document.applicationForm.home_status;
	if (trim(Obj.value) == "") { alert("Please select Home Status!");Obj.focus();return false; }
	/*
	*/
		Obj = document.applicationForm.loan_purpose;
	if (trim(Obj.value) == "") { alert("Please select Loan Purpose!");Obj.focus();return false; }
	/*
	*/
	Obj = document.applicationForm.loan_amount;
	if (trim(Obj.value) == "") { alert("Please select Loan Amount!");Obj.focus();return false; }
	Obj = document.applicationForm.mortgage_outstanding;
	if (trim(Obj.value) == "") { alert("Please select Mortgage Outstanding!");Obj.focus();return false; }
	if (appformat=="long")
	{
		Obj = document.applicationForm.monthly_payments;
		if (trim(Obj.value) == "") { alert("Please select Monthly Payments!");Obj.focus();return false; }
	}
	Obj = document.applicationForm.mortgage_company;
	if (trim(Obj.value) == "") { alert("The Mortgage Company is empty!");Obj.focus();return false; }
	if (appformat=="long")
	{
		Obj = document.applicationForm.term_year;
		if (trim(Obj.value) == "") { alert("Please select Year for Term Remaining On Mortgage!");Obj.focus();return false; }
		Obj = document.applicationForm.term_month;
		if (trim(Obj.value) == "") { alert("Please select Month for Term Remaining On Mortgage!");Obj.focus();return false; }
		Obj = document.applicationForm.mortgage_type;
		if (trim(Obj.value) == "") { alert("Please select Type Of Mortgage!");Obj.focus();return false; }
	}
	Obj = document.getElementById('have_arrears');
	if (Obj.checked) {
		Obj = document.applicationForm.present_arrears;
		if (trim(Obj.value) == "") { alert("The Present Arrears is empty!");Obj.focus();return false; }
		Obj = document.applicationForm.highest_arrears;
		if (trim(Obj.value) == "") { alert("The Highest Arrears is empty!");Obj.focus();return false; }
	}
	Obj = document.applicationForm.title;
	if (trim(Obj.value) == "") { alert("The Title is empty!");Obj.focus();return false; }
	Obj = document.applicationForm.firstname;
	if (trim(Obj.value) == "") { alert("The First Name is empty!");Obj.focus();return false; }
	Obj = document.applicationForm.lastname;
	if (trim(Obj.value) == "") { alert("The Last Name is empty!");Obj.focus();return false; }
	Obj = document.applicationForm.dob_day1;
	if (trim(Obj.value) == "") { alert("Please select Day for Date Of Birth!");Obj.focus();return false; }
	Obj = document.applicationForm.dob_month1;
	if (trim(Obj.value) == "") { alert("Please select Month for Date Of Birth!");Obj.focus();return false; }
	Obj = document.applicationForm.dob_year1;
	if (trim(Obj.value) == "") { alert("Please select Year for Date Of Birth!");Obj.focus();return false; }
	if (appformat=="long")
	{
		Obj = document.applicationForm.occupation;
		if (trim(Obj.value) == "") { alert("The Occupation is empty!");Obj.focus();return false; }
	}
	Obj = document.applicationForm.yearly_income;
	str = trim(Obj.value);
	if (str == "")
	{
		alert("The Yearly Income is empty!");Obj.focus();return false;
	}
	else
	{
		var num = CurrencyStringToNumber(str);
		if (isNaN(num))
		{
			alert("Yearly Income is not a valid number.");
			document.applicationForm.yearly_income.focus();
			return false;
		}
		else
		{
			_income = num;
		}
	}
	if (appformat=="long")
	{
		Obj = document.applicationForm.job_length;
		if (trim(Obj.value) == "") { alert("Please select Job Length!");Obj.focus();return false; }
		Obj = document.applicationForm.maiden_name;
		if (trim(Obj.value) == "") { alert("The Mother's Maiden Name is empty!");Obj.focus();return false; }
		Obj = document.applicationForm.dependents;
		if (trim(Obj.value) == "") { alert("The Dependents (Under 18) is empty!");Obj.focus();return false; }
	}
	Obj = document.applicationForm.marital_status;
	if (trim(Obj.value) == "") { alert("Please select Marital Status!");Obj.focus();return false; }
	if (trim(Obj.value) == "Married" || trim(Obj.value) == "Living with Partner") {
		Obj = document.applicationForm.partner_title;
		if (trim(Obj.value) == "") { alert("The Partner Title is empty!");Obj.focus();return false; }
		Obj = document.applicationForm.partner_firstname;
		if (trim(Obj.value) == "") { alert("The Partner First Name is empty!");Obj.focus();return false; }
		Obj = document.applicationForm.partner_lastname;
		if (trim(Obj.value) == "") { alert("The Partner Last Name is empty!");Obj.focus();return false; }
		Obj = document.applicationForm.dob_day2;
		if (trim(Obj.value) == "") { alert("Please select Day for Partner Date Of Birth!");Obj.focus();return false; }
		Obj = document.applicationForm.dob_month2;
		if (trim(Obj.value) == "") { alert("Please select Month for Partner Date Of Birth!");Obj.focus();return false; }
		Obj = document.applicationForm.dob_year2;
		if (trim(Obj.value) == "") { alert("Please select Year for Partner Date Of Birth!");Obj.focus();return false; }
		Obj = document.applicationForm.partner_yearly_income;
		str = trim(Obj.value);
		if (str == "")
		{
			alert("The Partner Yearly Income is empty!");Obj.focus();return false;
		}
		else
		{
			var num = CurrencyStringToNumber(str);
			if (isNaN(num))
			{
				alert("Partner Yearly Income is not a valid number.");
				document.applicationForm.partner_yearly_income.focus();
				return false;
			}
			else
			{
				_partner_income = num;
			}
		}
		if (appformat=="long")
		{
			Obj = document.applicationForm.partner_job_length;
			if (trim(Obj.value) == "") { alert("Please select Partner Job Length!");Obj.focus();return false; }
		}
	}

	// check income requirements:
	if (appformat=="short")
	{
		if (_income<17000.0)
		{
			if (_partner_income==-1.0)
			{
				alert("Unfortunately, at this time we cannot supply loans to applicants who have incomes of less than £17,000 per year.");
				document.applicationForm.yearly_income.focus();
				return false;
			}
			else if (_partner_income<17000.0)
			{
				alert("Unfortunately, at this time we cannot supply loans to applicants who have incomes of less than £17,000 per year.");
				document.applicationForm.yearly_income.focus();
				return false;
			}
		}
	}

	Obj = document.applicationForm.no_street;
	if (trim(Obj.value) == "") { alert("The No & Street is empty!");Obj.focus();return false; }
	Obj = document.applicationForm.town_city;
	if (trim(Obj.value) == "") { alert("The Town / City is empty!");Obj.focus();return false; }
	Obj = document.applicationForm.postcode;
	if (trim(Obj.value) == "") { alert("The Postcode is empty!");Obj.focus();return false; }
	str = document.applicationForm.phone.value;
	if (str != "")
	{
		// strip out spaces, dashes, dots and parentheses
		var stripped = str.replace(/[\(\)\.\-\ ]/g, '');
		// is what's left a number?
		if (isNaN(parseInt(stripped)))
		{
			alert("Phone Number contains non-numerical characters.  Please input a valid Phone Number!");
			document.applicationForm.phone.focus();
			return false;
		}
		if (stripped.length < 11)
		{
			alert("Phone Number does not contain enough digits.  Please input a valid Phone Number!");
			document.applicationForm.phone.focus();
			return false;
		}
	}
	else
	{
		alert("The Phone Number is empty!");
		document.applicationForm.phone.focus();
		return false;
	}
	str = trim(document.applicationForm.email.value);
	if (str != "") {
		var filter=/^.+@.+\..{2,3}$/
		if (filter.test(str)) {
			results = str.split("@");
			sum = 0
			for(var i =0; i < results.length; i++) { sum = sum + 1 }
			if (sum > 2) {
				alert("Please input a valid Email Address!");
			  document.applicationForm.email.focus();
				return false;
			}
		} else {
			alert("Please input a valid Email Address!");
		  document.applicationForm.email.focus();
			return false;
		}
	} else {
		alert("The Email Address is empty !");
		document.applicationForm.email.focus();
		return false;
	}
	Obj = document.applicationForm.property_value;
	if (trim(Obj.value) == "") { alert("The Current Property Value is empty!");Obj.focus();return false; }
	if (appformat=="long")
	{
		Obj = document.applicationForm.time_at_house;
		if (trim(Obj.value) == "") { alert("The Time At House is empty!");Obj.focus();return false; }
		if (trim(Obj.value) == "Less than 3 Years") {
			Obj = document.applicationForm.prev_no_street;
			if (trim(Obj.value) == "") { alert("The No & Street for Previous Address is empty!");Obj.focus();return false; }
			Obj = document.applicationForm.prev_town_city;
			if (trim(Obj.value) == "") { alert("The Town / City for Previous Address is empty!");Obj.focus();return false; }
			Obj = document.applicationForm.prev_postcode;
			if (trim(Obj.value) == "") { alert("The Postcode for Previous Address is empty!");Obj.focus();return false; }
		}
		Obj = document.applicationForm.purchase_day;
		if (trim(Obj.value) == "Day") { alert("The Day for Purchase Date is empty!");Obj.focus();return false; }
		Obj = document.applicationForm.purchase_month;
		if (trim(Obj.value) == "Month") { alert("The Month for Purchase Date is empty!");Obj.focus();return false; }
		Obj = document.applicationForm.purchase_year;
		if (trim(Obj.value) == "") { alert("The Year for Purchase Date is empty!");Obj.focus();return false; }
		Obj = document.applicationForm.purchase_price;
		if (trim(Obj.value) == "") { alert("The Purchase Price is empty!");Obj.focus();return false; }
		Obj = document.applicationForm.year_built;
		if (trim(Obj.value) == "") { alert("The Year Built is empty!");Obj.focus();return false; }
		Obj = document.applicationForm.property_type;
		if (trim(Obj.value) == "") { alert("Please Select the Property Type");Obj.focus();return false; }
		Obj = document.applicationForm.no_bedrooms;
		if (trim(Obj.value) == "") { alert("Please Select the # of Bedrooms");Obj.focus();return false; }
	}
}

function ShowSectionArrearsInfo(appformat) {
		document.getElementById("divArrears1").style.display = 'block';
		document.getElementById("divArrears2").style.display = 'block';
		document.getElementById("divArrears3").style.display = 'block';
		document.getElementById("divArrears4").style.display = 'block';
		document.getElementById("divArrears5").style.display = 'block';
}
function CreditHistory(appformat) {
		document.getElementById("CreditHistory1").style.display = 'block';

}
function HideSectionArrearsInfo(appformat) {
		document.getElementById("divArrears1").style.display = 'none';
		document.getElementById("divArrears2").style.display = 'none';
		document.getElementById("divArrears3").style.display = 'none';
		document.getElementById("divArrears4").style.display = 'none';
		document.getElementById("divArrears5").style.display = 'none';
}
function HideCreditHistory(appformat) {
		document.getElementById("CreditHistory1").style.display = 'none';

}
function ShowSectionPartnerInfo(appformat) {
	// show Partner Information Section
	if (document.applicationForm.marital_status.value == "Married" || document.applicationForm.marital_status.value == "Living with Partner" ) {
		document.getElementById("divPartnerInfo").style.display = 'block';
	}
	else {
		document.getElementById("divPartnerInfo").style.display = 'none';
	}
	// show Current Address & Contact Details Section; for short format, always open
	if (appformat=="long")
	{
		if (document.applicationForm.marital_status.value == "" ) {
			document.getElementById("divCurrentAddress").style.display = 'none';
		}
		else {
			document.getElementById("divCurrentAddress").style.display = 'block';
		}
	}
}
function ShowSectionPrevAddressInfo(appformat) {
	if (document.applicationForm.time_at_house.value == "Less than 3 Years" || document.applicationForm.marital_status.value == "Living with Partner" ) {
		document.getElementById("divPreviousAddress1").style.display = 'block';
		document.getElementById("divPreviousAddress2").style.display = 'block';
	}
	else
	{
		document.getElementById("divPreviousAddress1").style.display = 'none';
		document.getElementById("divPreviousAddress2").style.display = 'none';
	}
}
function OnHomeStatus(appformat) {
	if (document.applicationForm.home_status.value == "Own Home Outright") {
		document.applicationForm.mortgage_outstanding.selectedIndex = 1;
		document.applicationForm.mortgage_outstanding.disabled = true;
		document.applicationForm.mortgage_company.value = "n/a";
		document.applicationForm.mortgage_company.disabled = true;
		if (appformat=="long") {
		document.applicationForm.monthly_payments.selectedIndex = 1;
		document.applicationForm.monthly_payments.disabled = true;
		document.applicationForm.term_year.selectedIndex = 1;
		document.applicationForm.term_year.disabled = true;
		document.applicationForm.term_month.selectedIndex = 1;
		document.applicationForm.term_month.disabled = true;
		document.applicationForm.mortgage_type.selectedIndex = 9;
		document.applicationForm.mortgage_type.disabled = true;
		}
	}
	else if (document.applicationForm.home_status.value == "Tenant") {
		window.location='tenant_loans.php';
	}
	else {
		document.applicationForm.mortgage_outstanding.selectedIndex = 0;
		document.applicationForm.mortgage_outstanding.disabled = false;
		document.applicationForm.mortgage_company.value = "";
		document.applicationForm.mortgage_company.disabled = false;
		if (appformat=="long") {
		document.applicationForm.monthly_payments.selectedIndex = 0;
		document.applicationForm.monthly_payments.disabled = false;
		document.applicationForm.term_year.selectedIndex = 0;
		document.applicationForm.term_year.disabled = false;
		document.applicationForm.term_month.selectedIndex = 0;
		document.applicationForm.term_month.disabled = false;
		document.applicationForm.mortgage_type.selectedIndex = 0;
		document.applicationForm.mortgage_type.disabled = false;
		}
		if (document.applicationForm.home_status.value == "") {
			document.applicationForm.home_status.selectedIndex = 0;
		}
	}
}
function InitPage(appformat) {
	//debugger
}

function fredir(){
	if(document.applicationForm.loan_purpose.selectedIndex==3){
		document.applicationForm.action='remortgage.html';
		document.applicationForm.submit();
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}