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];}
}
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 validateLogin() {
	var username = document.login_form.username.value;
	var password = document.login_form.password.value;
	
	if (username == "") {
		alert("You must enter a username.");
		document.login_form.username.focus();
		return false;
	}
	if (password == "") {
		alert("You must enter a password.");
		document.login_form.password.focus();
		return false;
	}
	
	encodePassword(password);
}

function encodePassword(password) {
    var hash = hex_md5(password);
	document.login_form.password.value = hash;
	return true;
}

function validateContactForm() {
    var fullName = document.contact_form.full_name.value;
	var location = document.contact_form.location.value;
	var phone = document.contact_form.phone.value;
	var query = document.contact_form.query.value;
	var email = document.contact_form.email.value;
	
	if (fullName == "") {
		alert("You must enter your full name.");
		document.contact_form.full_name.focus();
		return false;
	}
	if (location == "") {
		alert("You must enter your location.");
		document.contact_form.location.focus();
		return false;
	}
	if (phone == "" && email == "") {
		alert("You must enter either your phone number or email address.");
		document.contact_form.phone.focus();
		return false;
	}
	if (phone != "" && !isNumeric(phone)) {
		alert("Your phone number can only contain numeric characters");
		document.contact_form.phone.focus();
		return false;
	}
	if (email != "") {
		// E-mail Validation by Henrik Petersen / NetKontoret
		// Explained at www.echoecho.com/jsforms.htm
		// Please do not remove this line and the two lines above.
		apos=email.indexOf("@"); 
		dotpos=email.lastIndexOf(".");
		lastpos=email.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
			alert("You must enter a valid email address");
				document.contact_form.email.focus();
			return false;
		}
	}
	if (query == "") {
		alert("You must enter your comments or query.");
		document.contact_form.query.focus();
		return false;
	}
	return true;
}

function isNumeric(strString) {
	var strValidChars = "0123456789+(). ";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function cancelAdmin(section) {
	document.forms[0].action = baseUrl+"/"+section;
	document.forms[0].submit();	
}

function validateContactForm() {
	var fullName = document.contact_form.full_name.value;
	var email = document.contact_form.email.value;
	var phone = document.contact_form.phone.value;
	var query = document.contact_form.query.value;
	
	if (fullName == "") {
		alert("You must enter your full name.");
		document.contact_form.full_name.focus();
		return false;
	}
	if (email == "" && phone == "") {
		alert("You must enter either your phone number or email address.");
		document.contact_form.phone.focus();
		return false;
	}
	if (phone != "" && !isNumeric(phone)) {
		alert("Your phone number can only contain numeric characters");
		document.contact_form.phone.focus();
		return false;
	}
	if (email != "") {
		// E-mail Validation by Henrik Petersen / NetKontoret
		// Explained at www.echoecho.com/jsforms.htm
		// Please do not remove this line and the two lines above.
		apos=email.indexOf("@"); 
		dotpos=email.lastIndexOf(".");
		lastpos=email.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
			alert("You must enter a valid email address");
				document.contact_form.email.focus();
			return false;
		}
	}
	if (query == "") {
		alert("You must enter some comments.");
		document.contact_form.query.focus();
		return false;
	}
}

function validateInstructionForm() {
	var btn = valButton(document.instruction_form.application_type);
	if (btn == null) {
		alert('No radio button selected');
		return false;
	}
	
	if (btn == "individual") {
		var clientName = document.instruction_form.your_name.value;
		var email = document.instruction_form.your_email.value;
		var phoneWork = document.instruction_form.your_phone_work.value;
		var phoneHome = document.instruction_form.your_phone_home.value;
		var mobile = document.instruction_form.your_mobile.value;
		
		if (clientName == "") {
			alert("You must provide your full name.");
			document.instruction_form.your_name.focus();
			return false;
		}
		if (phoneWork == "" && phoneHome == "" && mobile == "") {
			alert("You must provide at least one contact phone number.");
			document.instruction_form.your_phone_home.focus();
			return false;
		}
		if (phoneHome != "" && !isNumeric(phoneHome)) {
			alert("Your phone number can only contain numeric characters");
			document.instruction_form.your_phone_home.focus();
			return false;
		}
		if (phoneWork != "" && !isNumeric(phoneWork)) {
			alert("Your phone number can only contain numeric characters");
			document.instruction_form.your_phone_work.focus();
			return false;
		}
		if (mobile != "" && !isNumeric(mobile)) {
			alert("Your phone number can only contain numeric characters");
			document.instruction_form.your_mobile.focus();
			return false;
		}
		if (email != "") {
			// E-mail Validation by Henrik Petersen / NetKontoret
			// Explained at www.echoecho.com/jsforms.htm
			// Please do not remove this line and the two lines above.
			apos=email.indexOf("@"); 
			dotpos=email.lastIndexOf(".");
			lastpos=email.length-1;
			if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
				alert("You must enter a valid email address");
					document.instruction_form.your_email.focus();
				return false;
			}
		}
	} else {
		var brokerName = document.instruction_form.broker_name.value;
		var contactName = document.instruction_form.contact_name.value;
		var clientName = document.instruction_form.client_name.value;
		var email = document.instruction_form.client_email.value;
		var phoneWork = document.instruction_form.client_phone_work.value;
		var phoneHome = document.instruction_form.client_phone_home.value;
		var mobile = document.instruction_form.client_mobile.value;
		
		if (brokerName == "") {
			alert("You must provide your broker name.");
			document.instruction_form.broker_name.focus();
			return false;
		}
		if (contactName == "") {
			alert("You must provide your contact name.");
			document.instruction_form.contact_name.focus();
			return false;
		}
		if (clientName == "") {
			alert("You must provide your client name.");
			document.instruction_form.client_name.focus();
			return false;
		}
		if (phoneWork == "" && phoneHome == "" && mobile == "") {
			alert("You must provide at least one phone number.");
			document.instruction_form.client_phone_home.focus();
			return false;
		}
		if (phoneHome != "" && !isNumeric(phoneHome)) {
			alert("Your phone number can only contain numeric characters");
			document.instruction_form.client_phone_home.focus();
			return false;
		}
		if (phoneWork != "" && !isNumeric(phoneWork)) {
			alert("Your phone number can only contain numeric characters");
			document.instruction_form.client_phone_work.focus();
			return false;
		}
		if (mobile != "" && !isNumeric(mobile)) {
			alert("Your phone number can only contain numeric characters");
			document.instruction_form.client_mobile.focus();
			return false;
		}
		if (email != "") {
			// E-mail Validation by Henrik Petersen / NetKontoret
			// Explained at www.echoecho.com/jsforms.htm
			// Please do not remove this line and the two lines above.
			apos=email.indexOf("@"); 
			dotpos=email.lastIndexOf(".");
			lastpos=email.length-1;
			if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
				alert("You must enter a valid email address");
					document.instruction_form.client_email.focus();
				return false;
			}
		}
	}
}

function showIndividualFields() {
	hideBlock('broker_div');
	showBlock('individual_div');
}

function showBrokerFields() {
	showBlock('broker_div');
	hideBlock('individual_div');
}

function hideBlock(divId) {
	document.getElementById(divId).style.display = "none";
}

function showBlock(divId) {
	document.getElementById(divId).style.display = "block";
}

function toggleSections() {
    toggleField_1 = document.getElementById('type_remortgage');
	toggleField_2 = document.getElementById('type_sale');
	toggleField_3 = document.getElementById('type_purchase');
	
    if (toggleField_1.checked) {
        showBlock('remortgage_sale');
		showBlock('remortgage_purchase');
    }
	if (!toggleField_1.checked) {
		if (!toggleField_2.checked) {
			hideBlock('remortgage_sale');
		}
		if (!toggleField_3.checked) {
			hideBlock('remortgage_purchase');
		}
    }
	
	if (toggleField_2.checked) {
        showBlock('remortgage_sale');
		showBlock('sales');
    }
	if (!toggleField_2.checked) {
		if (!toggleField_1.checked) {
			hideBlock('remortgage_sale');
		}
		hideBlock('sales');
    }
	
	if (toggleField_3.checked) {
        showBlock('remortgage_purchase');
		showBlock('purchases');
    }
	if (!toggleField_3.checked) {
		if (!toggleField_1.checked) {
			hideBlock('remortgage_purchase');
		}
		hideBlock('purchases');
    }
}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) {
		return btn[cnt].value;
	}  else {
		return null;
	}
}