// JavaScript Document

var InternetExplorer = navigator.appName=="Microsoft Internet Explorer";

function WarnTheUser(WarningText) {
	alert(WarningText);
}

function Validate_DataProtection(Selectbox) {
	if (Selectbox.value=="Yes" )
		return true;
	else{
		WarnTheUser("We need you to read and say 'YES' to our data protection act statement - otherwise we cannot process your loan application.");
		return false;
	}
}
	

// Supporting functions
function isNumber(checkObject) {
	var checkOK = "0123456789";
	var checkStr = checkObject.value;
	var allValid = true;
  
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);

		// Is this character not in the validation string
		if (checkOK.indexOf(ch) == -1) {
			allValid = false;
			break;
		}
	} 

	if (!allValid) {
		alert("OOPS: You have typed in something that contains characters which are not numbers.  Please remove all non-number characters (press OK)");
		checkObject.focus();
		return (false);
	}
	return (true);
}

function numbersonly(event) {
	if (event.keyCode < 48 || event.keyCode > 57) 
		return false;
}


function numbersandpointonly(event){
	// Allow point/dot character for decimal placing
	if (event.keyCode == 46) {
		return true; 
	}

	if (event.keyCode < 48 || event.keyCode > 57) 
		return false;
}


function Capitalise(el) {
	try {
		var Val = el.value;
		el.value = Val.substr(0, 1).toUpperCase() + Val.substr(1);
	} catch(e) {
	}
}

function FullyCapitalise(el) {
	try {
		el.value = el.value.toUpperCase();
	} catch(e) {
	}
}

function ConsiderFieldFocus(sField) {
	// If the URL does not contain an Eyeconomy referal code, then it's OK to focus upon a field.
	// Focussing upon a field, aparently, forces the page to the front of the screen.

	// http://www.loanspage.co.uk/?2007032509311398a&version=eyecon-172-home&kwd=&adver=Eyecon&camp=Loanspage-Eyecon&type=cpm
	if ( document.location.toString().toUpperCase().match("EYECON-") || document.location.toString().toUpperCase().match("UTARGET-") ) 
		return;
		
	setTimeout("Form.Element.focus(\"" + sField + "\")", 1000);
}

// If we find a phrase we recognise in the referring keyword (eg "looking for secured loans") then set the loantype according to the keyword.
function SetLoantypeField(_ReferalKeyword) {
	try {
		if(typeof(_ReferalKeyword) != "string")
			return;
			
		var ReferalKeyword = _ReferalKeyword.toLowerCase();
		var sLoantype="";		
		
		// Choose which loantype we might select for the visitor
		if (ReferalKeyword.match(/remortgage/)) {
			sLoantype = "Remortgage";
		} else if(ReferalKeyword.match(/unsecured/)){
			sLoantype = "Unsecured";
		} else if(ReferalKeyword.match(/commercial/)){
			sLoantype = "Commercial";
		}
		
		if (sLoantype != "") {
			setElementValue("dbfield_loantype", sLoantype);		
		}
				
		// Try the homeowner value...
		var sHomeowner = "";
		if (	ReferalKeyword.match(/tenant/) || 
				ReferalKeyword.match(/nonhomeowner/) ||
				ReferalKeyword.match(/non homeowner/)
				) {
			sHomeowner = "No";
		} else if (	ReferalKeyword.match(/homeowner/) ||
						ReferalKeyword.match(/secured/) ||
						ReferalKeyword.match(/home own/)
						) {
			sHomeowner = "Yes";
		}
		if (sHomeowner != "") {
			setElementValue("dbfield_homeowner", sHomeowner);		
		}

		//window.status = ReferalKeyword;
		
	} catch (e) {}
}

function setElementValue(sTargetElement, sValue) {
	try {
		var oTargetElement = $(sTargetElement);
		var A = $A(oTargetElement.options);
					
		var O = A.find( function(option){
							 var OptionText;
							 if (InternetExplorer) {
								 OptionText = option.text;
							 } else {
								OptionText = option.value;
							 }
							 return (OptionText == sValue);
		});
		//debugger;
		//alert("About to change " + fieldName);
		if (typeof(O) != "undefined") {
			oTargetElement.selectedIndex = O.index;
			eval("OpenAccount_" + sTargetElement + "_OnBlur()");
		}
	} catch (e) {}
}