window.onload = initialise;

function initialise()
{
	if (!document.getElementById)
		return;

	if(document.forms[0].id == "outrider_loan_app_confirm")
	{
    document.getElementById("daytel").focus();
  }
	else if(document.forms[0].id == "outrider_loan_app_confirm")
  	document.getElementById("loanamount").focus();
			
		
	var objPartner = document.getElementById('jointdetails');
	var objCheck = document.getElementById('jointyes');
	var objUnCheck = document.getElementById('jointno');
	
	if (objPartner && objCheck)
	{
		if (!objCheck.checked)
			objPartner.style.display = 'none';

		objCheck.onclick = function(event){return displayPartner(this, event);};
		objCheck.onkeypress = function(event){return displayPartner(this, event);};
		objUnCheck.onclick = function(event){return displayPartner(this, event);};
		objUnCheck.onkeypress = function(event){return displayPartner(this, event);};
	}
}

function displayPartner(objCheck, objEvent)
{
	var iKeyCode;

	if (objEvent && objEvent.type == 'keypress')
	{
		if (objEvent.keyCode)
			iKeyCode = objEvent.keyCode;
		else if (objEvent.which)
			iKeyCode = objEvent.which;

		if (iKeyCode != 13 && iKeyCode != 32)
			return true;
	}

	if (objCheck.id == 'jointyes')
		document.getElementById('jointdetails').style.display = 'block';
	else
		document.getElementById('jointdetails').style.display = 'none';
	
	return true;
}

//------------------------------

var blnShortEmployment = false;

function hideprevaddress(){	
	//Hide ExtraAddress Fields
	ExtraAddress.style.display = "none";
	
}

function hideselfemployed(){
	//Hide Self Employment Accountant details
	selfEmployed.style.display = "none";
}

function showselfemployed(){
	//Hide Self Employment Accountant details
	selfEmployed.style.display = "block";
}

function checkEmpStatus(){
	alert(document.Qform3.elements(1).name + '\n' + document.Qform3.elements(1).value);
	if(document.Qform3.SelfEmployed.value == "Yes"){
		showselfemployed();
		
	}else{
		hideselfemployed();
	}

}

// The functions "hidepartner" and "showpartner" allow the switchin of the 
// red required field blobs for the partner details if the first applicant
// indicates that they have a partner.
function hidepartner() {
		var eElem, aDivs = document.all.tags("DIV");
		var iDivsLength = aDivs.length;
		for(i=0; i<iDivsLength; i++) {
			eElem = aDivs[i];
			if (eElem.id.indexOf('on') != -1) eElem.style.display = "none";
			if (eElem.id.indexOf('off') != -1) eElem.style.display = "";
		}
}

function showpartner() {
		var eElem, aDivs = document.all.tags("DIV");
		var iDivsLength = aDivs.length;
		for(i=0; i<iDivsLength; i++) {
			eDiv = aDivs[i];
			if (eDiv.id.indexOf('off') != -1) eDiv.style.display = "none";
			if (eDiv.id.indexOf('on') != -1) eDiv.style.display = "";
		}
}


// Added 27th August 2004 - JDA
// Ask the applicant to fill in employment history where appropriate.

var boolFirst = true;

function checkTimeAtEmployer()
{
	var intMain = document.Qform3.Job_Length_app_years.value;
	var intJoint = document.Qform3.Job_length_partner.value;
	
	if (((0 < intMain && 3 > intMain) || (0 < intJoint && 3 > intJoint)) && boolFirst)
	{
		alert("Please supply previous employer details for each applicant (where applicable).")
		blnShortEmployment = true;			
		ExtraAddress.style.display = "block";
		
		boolFirst = false;
	}
	else if (2 < intMain && 2 < intJoint && (0 != intMain  && 0 != intJoint))
	{
		ExtraAddress.style.display = "none";
		blnShortEmployment = false;
		
		boolFirst = true;
	}
	
	
	/*
	if (document.Qform3.Job_Length_app_years.value<3){
		alert("Please supply previous employer details.")
		blnShortEmployment = true;			
		ExtraAddress.style.display = "block";
	}else{
		ExtraAddress.style.display = "none";
		blnShortEmployment = false;
	}
	*/
}

function checkOutriderApp() {
	//var theForm = document.outrider_loan_app;
	var theForm = document.getElementById("outrider_loan_app");
	
	var msg = "Please enter the following required information\n\n";
	var blnValid = true;
	
	if(!(theForm.bUKResident[0].checked || theForm.bUKResident[1].checked)){
		blnValid = false;
		msg += "\tAre you a permanent UK resident?\n";
	} 
	
	if(!(theForm.incomeyes[0].checked || theForm.incomeyes[1].checked)){
		blnValid = false;
		msg += "\tDo you have a regular income?\n";
	} 
	
	if(!(theForm.bover18[0].checked || theForm.bover18[1].checked)){
		blnValid = false;
		msg += "\tAre you over 18 years old?\n";
	} 
	
	if(!(theForm.bhomeowner[0].checked || theForm.bhomeowner[1].checked)){
		blnValid = false;
		msg += "\tAre you a homeowner?\n";
	} 
 	
	if(isBlank(theForm.iloanamount.value)){
		blnValid = false;
		msg += "\tLoan amount(£)\n";
	} 
	
	if(!(theForm.bmortgage[0].checked || theForm.bmortgage[1].checked)){
		blnValid = false;
		msg += "\tDo you have a mortgage?\n";
	} 
	
	if(isBlank(theForm.strtitle.value)){
		blnValid = false;
		msg += "\tTitle\n";
	} 
	
	if(isBlank(theForm.strforename.value)){
		blnValid = false;
		msg += "\tFirst name\n";
	}
	
	if(isBlank(theForm.strsurname.value)){
		blnValid = false;
		msg += "\tSurname\n";
	}
	
	if(theForm.strdobday.selectedIndex<1 || theForm.strdobmonth.selectedIndex<1|| theForm.strdobyear.selectedIndex<1){
		blnValid = false;
		msg += "\tA Date of birth\n";
	}
	
	if(isBlank(theForm.strdaytel.value || theForm.strevetel.value || theForm.strmobile.value)){
			blnValid = false;
			msg += "\tA home, work, or mobile telephone number\n";
	}
	
	if (!isBlank(theForm.strdaytel.value))
	{
		var tilter=/^0\d+$/i
		if(!tilter.test(theForm.strdaytel.value)){
			blnValid = false;
			msg += "\tA valid home telephone number no spaces or dashes\n";
		}
	}
	
	if (!isBlank(theForm.strevetel.value))
	{
		var tilter=/^0\d+$/i
		if(!tilter.test(theForm.strevetel.value)){
			blnValid = false;
			msg += "\tA valid work telephone number no spaces or dashes\n";
		}
	}
	
	if (!isBlank(theForm.strmobile.value))
	{
		var tilter=/^0\d+$/i
		if(!tilter.test(theForm.strmobile.value)){
			blnValid = false;
			msg += "\tA valid mobile telephone number no spaces or dashes\n";
		}
	}

	if(!isBlank(theForm.stremail.value)){
		
		
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		
		if (!filter.test(theForm.stremail.value)){
			blnValid = false;
			msg += "\tA valid E-mail Address\n";
		}
		
	}
	
	if(isBlank(theForm.housenumber.value)){
		blnValid = false;
		msg += "\tHouse number\n";
	}
	
	if(isBlank(theForm.street.value)){
		blnValid = false;
		msg += "\tStreet\n";
	}
	
	if(isBlank(theForm.town.value)){
		blnValid = false;
		msg += "\tTown\n";
	}
	
	if(isBlank(theForm.strpostcode.value)){
		blnValid = false;
		msg += "\tPostcode\n";
	}
	else if (!isUKPostCode(theForm.strpostcode.value))
	{
		blnValid = false;
		msg += "\tA valid Post Code\n";
	}
	
	if(!(theForm.bjoint[0].checked || theForm.bjoint[1].checked)){
		blnValid = false;
		msg += "\tIs this a joint application?\n";
	} 
	
	if(theForm.bjoint[0].checked){
		if(isBlank(theForm.strpartnertitle.value)){
			blnValid = false;
			msg += "\tPartner's title\n";
		}
		
		if(isBlank(theForm.strpartnerforename.value)){
			blnValid = false;
			msg += "\tPartner's forename\n";
		}
		
		if(isBlank(theForm.strpartnersurname.value)){
			blnValid = false;
			msg += "\tPartner's surname\n";
		}
		
		if(isBlank(theForm.strpartnerphone.value)){
			blnValid = false;
			msg += "\tPartner's phone number no spaces or dashes\n";
		}
		
		if(!isInteger(theForm.strpartnerphone.value)){
			blnValid = false;
			msg += "\tPartner's valid phone number no spaces or dashes\n";
		}	
		
		if(theForm.strpartnerdobday.selectedIndex<1 || theForm.strpartnerdobmonth.selectedIndex<1|| theForm.strpartnerdobyear.selectedIndex<1){
		blnValid = false;
		msg += "\tPartner's Date of birth\n";
	}
	}
	if (blnValid) 
	{
	  // if not a homeowner send them to the tenant details page
		// else if fail other screening questions send to decline
		// else they are a homeowner so send them to confim.
	
	  if((theForm.bhomeowner[1].checked) && (theForm.bUKResident[0].checked && theForm.incomeyes[0].checked && theForm.bover18[0].checked))
		{
		  theForm.action='tenantdetails.asp';
		}
		else if (theForm.bUKResident[1].checked || theForm.incomeyes[1].checked || theForm.bover18[1].checked)
		{
		  theForm.action='decline.asp';
		}
		else
		{
		  theForm.action='confirm.asp';
		}	
		theForm.submit();
	}else{
		alert(msg);
	}
}
function checkOutriderAppConfirm() 
{
	var theForm = document.getElementById("outrider_loan_app_confirm");
	var msg = "Please enter the following required information\n\n";
	var blnValid = true;
	
	if (!theForm.agree.checked) {
		blnValid = false;
		msg += "\Please tick the agreement checkbox to confirm you have read our privacy policy\n";
	}
	
	if(theForm.LinkedFrom.selectedIndex<1){
		blnValid = false;
		msg += "\Please select where you heard about Freedom Finance\n";
	}
	
	if (blnValid) 
	{
		theForm.action='confirmed.asp';
		theForm.submit();
	}
	else
	{
		alert(msg);
	}
}
function checkOutriderAppConfirmTenantDetails()
{
  var theForm = document.getElementById("tenanants_details");
	var msg = "Please enter the following required information\n\n";
	var blnValid = true;
	
	
	if(isBlank(theForm.TimeAtAddYear.value))
	{
		blnValid = false;
		msg += "\tTime at address years\n";
	}
	else if(!isInteger(theForm.TimeAtAddYear.value))
	{
	  blnValid = false;
    msg += "\tA Valid Time at address years\n";	
	}
	
	if(isBlank(theForm.TimeAtAddMonth.value))
	{
		blnValid = false;
		msg += "\tTime at address month\n";
	}
	else if(!isInteger(theForm.TimeAtAddMonth.value))
	{
	  blnValid = false;
    msg += "\tA Valid Time at address month\n";	
	}
	
	if(isBlank(theForm.IncomeM.value))
	{
		blnValid = false;
		msg += "\tIncome\n";
	}
	else if(!isInteger(theForm.IncomeM.value))
	{
	  blnValid = false;
    msg += "\tA Valid income\n";	
	}
	
	if(isBlank(theForm.EmpStatus.value))
	{
		blnValid = false;
		msg += "\tEmployment status\n";
	}

	if(isBlank(theForm.EmployersName.value))
	{
		blnValid = false;
		msg += "\tEmployers name\n";
	}

	if(isBlank(theForm.EmpAdd1.value))
	{
		blnValid = false;
		msg += "\tEmployers address 1\n";
	}

	if(isBlank(theForm.EmpAdd2.value))
	{
		blnValid = false;
		msg += "\tEmployers address 2\n";
	}

	if(isBlank(theForm.EmpAdd3.value))
	{
		blnValid = false;
		msg += "\tEmployers address 3\n";
	}

	if(isBlank(theForm.EmpAddPC.value))
	{
		blnValid = false;
		msg += "\tEmployer postcode\n";
	}
	else if (!isUKPostCode(theForm.EmpAddPC.value))
	{
		blnValid = false;
		msg += "\tA valid employer postcode\n";
	}

	if(isBlank(theForm.TimeAtJobYear.value))
	{
		blnValid = false;
		msg += "\tTime at job years\n";
	}
	else if(!isInteger(theForm.TimeAtJobYear.value))
	{
	  blnValid = false;
    msg += "\tA Valid time at job years\n";	
	}
	
	if(isBlank(theForm.TimeAtJobMonth.value))
	{
		blnValid = false;
		msg += "\tTime at job month\n";
	}
	else if(!isInteger(theForm.TimeAtJobMonth.value))
	{
	  blnValid = false;
    msg += "\tA Valid time at job month\n";	
	}

	if(isBlank(theForm.MRent.value))
	{
		blnValid = false;
		msg += "\tMonthly rent\n";
	}

	if(isBlank(theForm.BankName.value))
	{
		blnValid = false;
		msg += "\tBank name\n";
	}

	if(isBlank(theForm.mobile.value))
	{
		blnValid = false;
		msg += "\tMobile\n";
	}
	
	if(isBlank(theForm.BankYear.value))
	{
		blnValid = false;
		msg += "\tTime at bank years\n";
	}
	else if(!isInteger(theForm.BankYear.value))
	{
	  blnValid = false;
    msg += "\tA Valid time at bank years\n";	
	}
		
	if(isBlank(theForm.BankMonth.value))
	{
		blnValid = false;
		msg += "\tTime at bank month\n";
	}
	else if(!isInteger(theForm.BankMonth.value))
	{
	  blnValid = false;
    msg += "\tA Valid time at bank month\n";	
	}
	
	if (blnValid) 
	{
		theForm.action='confirmtenants.asp';
		theForm.submit();
	}
	else
	{
		alert(msg);
	}
}
function checkOutriderAppConfirmTenant() 
{
	var theForm = document.getElementById("tenanants_confirm");
	var msg = "Please enter the following required information\n\n";
	var blnValid = true;
	
	if (!theForm.agree.checked) 
	{
		blnValid = false;
		msg += "\Please tick the agreement checkbox to confirm you have read our privacy policy\n";
	}
	
	if(theForm.LinkedFrom.selectedIndex<1){
		blnValid = false;
		msg += "\Please select where you heard about Freedom Finance\n";
	}
	
	if (blnValid) 
	{
		theForm.action='confirmedTenant.asp';
		theForm.submit();
	}
	else
	{
		alert(msg);
	}
}