var blnADDITIONAL_FIELDS = false;

//***********************************************************************************************************************
//	Code for GOOGLE TOOLBAR
//***********************************************************************************************************************
if(window.attachEvent){
	window.attachEvent("onload",setListeners);
}
function setListeners(){
	inputList = document.getElementsByTagName("INPUT");
	for(i=0;i<inputList.length;i++){
		inputList[i].attachEvent("onpropertychange",restoreStyles);
		inputList[i].style.backgroundColor = "";
	}
	selectList = document.getElementsByTagName("SELECT");
	for(i=0;i<selectList.length;i++){
		selectList[i].attachEvent("onpropertychange",restoreStyles);
		selectList[i].style.backgroundColor = "";
	}
}
function restoreStyles(){
	if(event.srcElement.style.backgroundColor != ""){
		event.srcElement.style.backgroundColor = "";
	}
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	Trim Function
//***********************************************************************************************************************
function Trim(strVal) {
    var strMatch = strVal.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    result = (strMatch == null) ? "" : strMatch[1];
    return result;
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	Email Validation Function
//***********************************************************************************************************************
function isEmail(objObject){
	var regEmail, strValue;	
	regEmail = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	strValue = objObject.value;
	if (objObject.value == "") return true;
	if(! regEmail.test(strValue)){
		//objObject.focus();
		return false;
	}
	return true;
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	UK Zipcode Function
//***********************************************************************************************************************
function isValidUKZip(objObject){
	var regZip, strValue;	
	regZip1 = /^[a-zA-Z]{1,2}\d{1,2}[a-zA-Z]?\s\d[a-zA-Z]{2}$/;
	regZip2 = /^[a-zA-Z]{1,2}\d{1,2}[a-zA-Z]?\d[a-zA-Z]{2}$/;
	strValue = Trim(objObject.value);	
	if(Trim(objObject.value).length <= 0){
		return true;
	}
	if(Trim(objObject.value).length < 5){
		return false;
	}else{
		if((!regZip1.test(strValue)) && (!regZip2.test(strValue))){
			return false;
		}
	}
	return true;
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	Open New Window in Center of the screen Function
//***********************************************************************************************************************
function NewWindow(url,ht,wd,sc,rs,name){
	l = (screen.width) ? (screen.width-parseInt(wd))/2 : 0;
	t = (screen.height) ? (screen.height-parseInt(ht))/2 : 0;	
	param = "height=" + parseInt(ht) + ",width=" + parseInt(wd) + ",top=" + t + ",left=" + l + ",location=no,toolbar=no,status=no,menubar=no,scrollbars=" + sc + ",resizable=" + rs + ",copyhistory=no";			
	oc_infowin = window.open(url,name,param);
	if(oc_infowin != null){
		oc_infowin.opener = self;
	}
	oc_infowin.focus();
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	Validation for DO REFRESH FORM
//***********************************************************************************************************************
function doREFRESHFORM(int_form_id){
	if(parseInt(int_form_id) == 1){
		var theForm = document.frmAPPLICATION;
		if(theForm.cmbLOAN_PURPOSE.value == "Remortgage"){
			//document.location.href = "remortgages.html";	
		}
	}
	if(parseInt(int_form_id) == 2){
		var theForm = document.frmREMORTGAGES;
		if((theForm.cmbLOAN_PURPOSE.value != "Remortgage") && (theForm.cmbLOAN_PURPOSE.value != "")){
			document.location.href = "index.php";	
		}
	}	
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	ENABLE REQUIRED FIELDS
//***********************************************************************************************************************
function doCHECKADDFIELDS(int_form_id, objFIELD){
	if(parseInt(int_form_id) == 1){
		var theForm = document.frmAPPLICATION;
	}
	if(parseInt(theForm.hidFORM_ID.value) == 2){
		mny_loan_amount = parseInt(theForm.cmbLOAN_AMOUNT.value.replace(",", ""));
		if((parseInt(mny_loan_amount) > 7500) && ((theForm.cmbLOAN_PURPOSE.value == "Consolidation") || (theForm.cmbLOAN_PURPOSE.value == "Bill Consolidation")) && ((theForm.cmbHOME_STATUS.value == "Tenant") || (theForm.cmbHOME_STATUS.value == "Live With Parents"))){
			blnADDITIONAL_FIELDS = true;
			document.getElementById("additional_detail").style.display = "block";
		}else{
			blnADDITIONAL_FIELDS = false;
			document.getElementById("additional_detail").style.display = "none";
		}
	}
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	Validation for ENABLE DISABLE FIELDS
//***********************************************************************************************************************
function doENABLEFIELDSET(objFIELD, int_fieldset){
	var theForm = document.frmAPPLICATION;
	if(parseInt(int_fieldset) == 2){
		if(objFIELD.value == "Less than 3 Years"){
			document.getElementById("previous_address_info").style.display = "block";
		}else{
			document.getElementById("previous_address_info").style.display = "none";
		}
	}

	if(parseInt(int_fieldset) == 3){
		if(objFIELD.value == "Yes"){
			document.getElementById("arrears_info").style.display = "block";
		}else{
			document.getElementById("arrears_info").style.display = "none";
		}
	}

	if(parseInt(int_fieldset) == 4){
		if(objFIELD.value == "Flat"){
			document.getElementById("flat_floors_info").style.display = "block";
		}else{
			document.getElementById("flat_floors_info").style.display = "none";
		}
	}

	if(parseInt(int_fieldset) == 5){
		if(objFIELD.value == "Mrs"){
			document.getElementById("your_maiden_name_info").style.display = "block";
		}else{
			document.getElementById("your_maiden_name_info").style.display = "none";
		}
	}

	if(parseInt(int_fieldset) == 6){
		if(objFIELD.value == "Mrs"){
			document.getElementById("partners_maiden_name_info").style.display = "block";
		}else{
			document.getElementById("partners_maiden_name_info").style.display = "none";
		}
	}
	
	if(parseInt(int_fieldset) == 7){
		if((objFIELD.value != "Mortgaged Home") && (objFIELD.value != "")){	
			document.getElementById("property_value").style.display = "none";
			document.getElementById("mortgage_outstanding").style.display = "none";
		}else{
			document.getElementById("property_value").style.display = "block";
			document.getElementById("mortgage_outstanding").style.display = "block";
		}
	}
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	Validation for HOME STATUS FIELD
//***********************************************************************************************************************
function doVALIDATE_HOME_STATUS(objHOME_STATUS){
	var theForm = document.frmAPPLICATION;
	if((objHOME_STATUS.value != "Mortgaged Home") && (objHOME_STATUS.value != "")){	
		doENABLEFIELDSET(objHOME_STATUS, 7);
	}else{
		doENABLEFIELDSET(objHOME_STATUS, 7);
	}
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	Validation for RESET FORM
//***********************************************************************************************************************
function doRESETFORM(str_form_name){
	var theForm = document.getElementById(str_form_name);
	theForm.reset();
	return false;
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	Validation for SUBMIT APPLICATION FORM
//***********************************************************************************************************************
function doSUBMITAPPLICATION(){
	var theForm = document.frmAPPLICATION;	
	if(theForm.cmbLOAN_AMOUNT.selectedIndex <= 0){
		alert("Please select loan amount information.");				
		theForm.cmbLOAN_AMOUNT.focus();
		document.getElementById("lable1").className = "name_o";
		return false;
	}else{
		document.getElementById("lable1").className = "name";	
	}
	if((theForm.cmbLOAN_PURPOSE.selectedIndex < 0) || (theForm.cmbLOAN_PURPOSE.value == "")){
		alert("Please select loan purpose information.");			
		theForm.cmbLOAN_PURPOSE.focus();
		document.getElementById("lable2").className = "name_o";	
		return false;
	}else{
		document.getElementById("lable2").className = "name";	
	}
	if((theForm.cmbHOME_STATUS.selectedIndex <= 0) || (theForm.cmbHOME_STATUS.value == "")){
		alert("Please select home status information.");			
		theForm.cmbHOME_STATUS.focus();
		document.getElementById("lable3").className = "name_o";	
		return false;
	}else{
		document.getElementById("lable3").className = "name";	
	}
	if(Trim(theForm.txtFIRST_NAME.value).length == 0){
		alert("Please enter your first name.");		
		theForm.txtFIRST_NAME.focus();		
		document.getElementById("txtFIRST_NAME").className = "textboxhighlighted";
		document.getElementById("lable4").className = "name_o";	
		return false;
	}else{
		document.getElementById("lable4").className = "name";	
	}
	if(Trim(theForm.txtLAST_NAME.value).length == 0){
		alert("Please enter your last name.");
		theForm.txtLAST_NAME.focus();
		document.getElementById("lable4").className = "name_o";
		document.getElementById("txtLAST_NAME").className = "textboxhighlighted";
		return false;
	}else{
		document.getElementById("lable4").className = "name";	
	}
	if(Trim(theForm.txtHOME_PHONE.value).length == 0){
		alert("Please enter your home/work phone number.");
		theForm.txtHOME_PHONE.focus();
		document.getElementById("txtHOME_PHONE").className = "textboxhighlighted";
		document.getElementById("lable6").className = "name_o";
		return false;
	}else{
		document.getElementById("lable6").className = "name";	
	}
	if(checkUKTelephone(theForm.txtHOME_PHONE.value) == false){
		alert(telNumberErrors[telNumberErrorNo]);
		theForm.txtHOME_PHONE.focus();
		document.getElementById("txtHOME_PHONE").className = "textboxhighlighted";
		document.getElementById("lable6").className = "name_o";
		return false;		
	}
	if(Trim(theForm.txtMOBILE_PHONE.value).length > 0){
		if(checkUKTelephone(theForm.txtMOBILE_PHONE.value) == false){
			alert(telNumberErrors[telNumberErrorNo]);
			theForm.txtMOBILE_PHONE.focus();
			document.getElementById("txtMOBILE_PHONE").className = "textboxhighlighted";
			document.getElementById("lable7").className = "name_o";
			return false;		
		}else{
			document.getElementById("lable7").className = "name";	
		}
	}
	if(Trim(theForm.txtEMAIL_ADDRESS.value).length == 0){
		alert("Please enter your email address.");
		theForm.txtEMAIL_ADDRESS.focus();
		document.getElementById("txtEMAIL_ADDRESS").className = "textboxhighlighted";
		document.getElementById("lable8").className = "name_o";
		return false;
	}else{	
		if(isEmail(theForm.txtEMAIL_ADDRESS) == false) {
			alert("Please enter your valid email address.\nFor Example: xyz@xyz.com");
			theForm.txtEMAIL_ADDRESS.focus();
			document.getElementById("txtEMAIL_ADDRESS").className = "textboxhighlighted";
			document.getElementById("lable8").className = "name_o";
			return false;
		}else{
			document.getElementById("lable8").className = "name";	
		}
	}
	// START CODE
	if(parseInt(theForm.hidFORM_ID.value) == 2){
		if(blnADDITIONAL_FIELDS == true){
			if(theForm.cmbNO_OF_DEBTS.selectedIndex <= 0){
				alert("Please select loan amount information.");				
				theForm.cmbNO_OF_DEBTS.focus();
				document.getElementById("lable9").className = "name_o";
				return false;
			}else{
				document.getElementById("lable9").className = "name";	
			}
			if(Trim(theForm.txtMONTHLY_INCOME.value).length == 0){
				alert("Please enter your monthly income.");
				theForm.txtMONTHLY_INCOME.focus();
				document.getElementById("txtMONTHLY_INCOME").className = "textboxhighlighted";
				document.getElementById("lable10").className = "name_o";
				return false;
			}else{
				document.getElementById("lable10").className = "name";	
			}
			if(theForm.cmbEMPLOYMENT_STATUS.selectedIndex <= 0){
				alert("Please select your employment status.");				
				theForm.cmbEMPLOYMENT_STATUS.focus();
				document.getElementById("lable11").className = "name_o";
				return false;
			}else{
				document.getElementById("lable11").className = "name";	
			}
		}
	}
	// END CODE
	if(Trim(theForm.txtPROPERTY_STREET_ADDRESS.value).length == 0){
		alert("Please enter house no. & street address information.");
		theForm.txtPROPERTY_STREET_ADDRESS.focus();
		document.getElementById("txtPROPERTY_STREET_ADDRESS").className = "textboxhighlighted";
		document.getElementById("lable12").className = "name_o";
		return false;
	}else{
		document.getElementById("lable12").className = "name";	
	}
	if(Trim(theForm.txtPROPERTY_ZIP_CODE.value).length == 0){
		alert("Please enter postcode information.");
		theForm.txtPROPERTY_ZIP_CODE.focus();
		document.getElementById("txtPROPERTY_ZIP_CODE").className = "textboxhighlighted";
		document.getElementById("lable13").className = "name_o";
		return false;
	}else{
		document.getElementById("lable13").className = "name";	
	}
	if(isValidUKZip(theForm.txtPROPERTY_ZIP_CODE) == false){		
		alert("Please enter valid postcode with following instructions:\n\n1. Must contain minimum 5 characters.\n2. Must contain appropriate format. (e.g BB6 2AA or BB62AA)");
		theForm.txtPROPERTY_ZIP_CODE.focus();
		document.getElementById("txtPROPERTY_ZIP_CODE").className = "textboxhighlighted";
		document.getElementById("lable13").className = "name_o";
		return false;
	}else{
		document.getElementById("lable13").className = "name";
	}
	if((theForm.cmbHOME_STATUS.value == "Mortgaged Home") && (theForm.cmbHOME_STATUS.value != "")){	
		if(theForm.cmbPROPERTY_VALUE.selectedIndex <= 0){
			alert("Please select your property value information.");	
			theForm.cmbPROPERTY_VALUE.focus();
			document.getElementById("lable14").className = "name_o";
			return false;
		}else{
			document.getElementById("lable14").className = "name";	
		}
		if(theForm.cmbMORTGAGE_OUTSTANDING.selectedIndex <= 0){
			alert("Please select your mortgage outstanding information.");	
			theForm.cmbMORTGAGE_OUTSTANDING.focus();
			document.getElementById("lable15").className = "name_o";
			return false;
		}else{
			document.getElementById("lable15").className = "name";	
		}
	}
	theForm.hidACTION.value = "SubmitApplication";
	theForm.action = "formaction.php";
	theForm.submit();
	return true;	
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	Validation for SUBMIT DEBT APPLICATION FORM
//***********************************************************************************************************************
function doSUBMITDEBTAPPLICATION(){
	var theForm = document.frmAPPLICATION;	
	if(theForm.cmbTOTAL_OWED.selectedIndex <= 0){
		alert("Please select total amount you owed.");				
		theForm.cmbTOTAL_OWED.focus();
		document.getElementById("lable1").className = "name_o";
		return false;
	}else{
		document.getElementById("lable1").className = "name";	
	}
	if(theForm.cmbEMPLOYMENT_STATUS.selectedIndex <= 0){
		alert("Please select your employment status.");			
		theForm.cmbEMPLOYMENT_STATUS.focus();
		document.getElementById("lable2").className = "name_o";	
		return false;
	}else{
		document.getElementById("lable2").className = "name";	
	}
	if(theForm.cmbNO_OF_DEBTS.selectedIndex <= 0){
		alert("Please select no. of debts information.");			
		theForm.cmbNO_OF_DEBTS.focus();
		document.getElementById("lable3").className = "name_o";	
		return false;
	}else{
		document.getElementById("lable3").className = "name";	
	}
	if(theForm.cmbCONTACT_TIME.selectedIndex <= 0){
		alert("Please select your contact time..");			
		theForm.cmbCONTACT_TIME.focus();
		document.getElementById("lable4").className = "name_o";	
		return false;
	}else{
		document.getElementById("lable4").className = "name";	
	}
	if(Trim(theForm.txtMONTHLY_INCOME.value).length == 0){
		alert("Please enter your monthly income.");		
		theForm.txtMONTHLY_INCOME.focus();		
		document.getElementById("txtMONTHLY_INCOME").className = "textboxhighlighted";
		document.getElementById("lable5").className = "name_o";	
		return false;
	}else{
		document.getElementById("lable5").className = "name";	
	}
	if(Trim(theForm.txtFULL_NAME.value).length == 0){
		alert("Please enter your full name.");
		theForm.txtFULL_NAME.focus();		
		document.getElementById("txtFULL_NAME").className = "textboxhighlighted";
		document.getElementById("lable6").className = "name_o";
		return false;
	}else{
		document.getElementById("lable6").className = "name";	
	}
	if(Trim(theForm.txtEMAIL_ADDRESS.value).length == 0){
		alert("Please enter your email address.");
		theForm.txtEMAIL_ADDRESS.focus();
		document.getElementById("txtEMAIL_ADDRESS").className = "textboxhighlighted";
		document.getElementById("lable7").className = "name_o";
		return false;
	}	
	if(Trim(theForm.txtHOME_PHONE.value).length == 0){
		alert("Please enter your home/work phone number.");
		theForm.txtHOME_PHONE.focus();
		document.getElementById("txtHOME_PHONE").className = "textboxhighlighted";
		document.getElementById("lable8").className = "name_o";
		return false;
	}else{
		document.getElementById("lable8").className = "name";	
	}
	if(checkUKTelephone(theForm.txtHOME_PHONE.value) == false){
		alert(telNumberErrors[telNumberErrorNo]);
		theForm.txtHOME_PHONE.focus();
		document.getElementById("txtHOME_PHONE").className = "textboxhighlighted";
		document.getElementById("lable8").className = "name_o";
		return false;		
	}
	if(Trim(theForm.txtMOBILE_PHONE.value).length > 0){
		if(checkUKTelephone(theForm.txtMOBILE_PHONE.value) == false){
			alert(telNumberErrors[telNumberErrorNo]);
			theForm.txtMOBILE_PHONE.focus();
			document.getElementById("txtMOBILE_PHONE").className = "textboxhighlighted";
			document.getElementById("lable9").className = "name_o";
			return false;		
		}else{
			document.getElementById("lable9").className = "name";	
		}
	}
	if(Trim(theForm.txtHOUSE_NO_ADDRESS.value).length == 0){
		alert("Please enter your house no. information.");
		theForm.txtHOUSE_NO_ADDRESS.focus();
		document.getElementById("txtHOUSE_NO_ADDRESS").className = "textboxhighlighted";
		document.getElementById("lable10").className = "name_o";
		return false;
	}else{
		document.getElementById("lable10").className = "name";	
	}
	if(Trim(theForm.txtZIP_CODE.value).length == 0){
		alert("Please enter your post code information.");
		theForm.txtZIP_CODE.focus();
		document.getElementById("txtZIP_CODE").className = "textboxhighlighted";
		document.getElementById("lable11").className = "name_o";
		return false;
	}else{
		document.getElementById("lable11").className = "name";	
	}
	if(isValidUKZip(theForm.txtZIP_CODE) == false){		
		alert("Please enter valid postcode with following instructions:\n\n1. Must contain minimum 5 characters.\n2. Must contain appropriate format. (e.g BB6 2AA or BB62AA)");
		theForm.txtZIP_CODE.focus();
		document.getElementById("txtZIP_CODE").className = "textboxhighlighted";
		document.getElementById("lable11").className = "name_o";
		return false;
	}else{
		document.getElementById("lable11").className = "name";
	}
	theForm.hidACTION.value = "SubmitDebtApplication";
	theForm.action = "formaction.php";
	theForm.submit();
	return true;	
}
//***********************************************************************************************************************

//***********************************************************************************************************************
//	Validation for SUBMIT FEEDBACK FORM
//***********************************************************************************************************************
function doSUBMITFEEDBACK(){
	var theForm = document.frmCONTACT;
	if(Trim(theForm.txtNAME.value).length == 0){
		alert("Please enter your name.");
		theForm.txtNAME.focus();
		return false;
	}
	if(Trim(theForm.txtEMAIL.value).length == 0){
		alert("Please enter your email address.");
		theForm.txtEMAIL.focus();
		return false;
	}
	if(isEmail(theForm.txtEMAIL) == false) {
		alert("Please enter your valid email address.\n\nFor Example: xyz@xyz.com");
		theForm.txtEMAIL.focus();
		return false;
	}
	if(Trim(theForm.txaMESSAGE.value).length == 0){
		alert("Please enter your message.");
		theForm.txaMESSAGE.focus();
		return false;
	}	
	if(Trim(theForm.txaMESSAGE.value).length > 0){		
		if(Trim(theForm.txaMESSAGE.value).length > 250){
			alert("Please enter only 250 characters in message.");
			theForm.txaMESSAGE.focus();
			return false;
		}
	}	
	theForm.hidACTION.value = "SubmitFeedback";
	theForm.action = "formaction.php";
	theForm.submit();					
	return true;	
}
//***********************************************************************************************************************