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 validateEmail(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) {
		return false;
	} else {
		return true;
	}
}

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

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

function replaceChars(entry) {
	out = "\\"; // replace this
	add = "/";  // with this
	temp = "" + entry; // temporary holder

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

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 isPhone(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 confirmDelete(path) {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		window.location = baseUrl+path;
	}
}

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

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

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;
	}
}

function validateContactForm() {
	var fullName = document.getElementById('full_name');
	var personalAddress1 = document.getElementById('personal_address1');
	var personalTown = document.getElementById('personal_town');
	var personalCounty = document.getElementById('personal_county');
	var personalPostcode = document.getElementById('personal_postcode');
	var phone = document.getElementById('phone');
	var mobile = document.getElementById('mobile');
	
	var sameAddress = document.getElementById('same_address');
	
	if (fullName.value == "") {
		alert("You must enter your full name.");
		fullName.focus();
		return false;
	}
	if (personalAddress1.value == "") {
		alert("You must enter your address.");
		personalAddress1.focus();
		return false;
	}
	if (personalTown.value == "") {
		alert("You must enter your town.");
		personalTown.focus();
		return false;
	}
	if (personalCounty.value == "") {
		alert("You must enter your county.");
		personalCounty.focus();
		return false;
	}
	if (personalPostcode.value == "") {
		alert("You must enter your postcode.");
		personalPostcode.focus();
		return false;
	}
	if (email.value == "" && phone.value == "") {
		alert("You must enter either a phone number or email address.");
		phone.focus();
		return false;
	}
	if (email.value != "") {
		if (!validateEmail(email.value)) {
    		alert("You must enter a valid email address");
    	    email.focus();
    		return false;
    	}
	}
	if (phone.value != "" && !isPhone(phone.value)) {
		alert("The phone number contains invalid characters.");
		phone.focus();
		return false;
	}
	
	if (sameAddress.checked == false) {
		var propertyAddress1 = document.getElementById('property_address1');
		var propertyTown = document.getElementById('property_town');
		var propertyCcounty = document.getElementById('property_county');
		var propertyPostcode = document.getElementById('property_postcode');
		
		if (propertyAddress1.value == "") {
			alert("You must enter the property address.");
			propertyAddress1.focus();
			return false;
		}
		if (propertyTown.value == "") {
			alert("You must enter the property town.");
			propertyTown.focus();
			return false;
		}
		if (propertyCcounty.value == "") {
			alert("You must enter the property county.");
			propertyCcounty.focus();
			return false;
		}
		if (propertyPostcode.value == "") {
			alert("You must enter the property postcode.");
			propertyPostcode.focus();
			return false;
		}
	}
	
	var typeSelection = document.getElementById('property_type').selectedIndex;
    var typeSelectionId = document.getElementById('property_type').options[typeSelection].value;

    if (typeSelectionId == -1) {
		alert("You must select the property type.");
		document.getElementById('property_type').focus();
		return false;
	}
	
	var bedroomSelection = document.getElementById('bedrooms').selectedIndex;
    var bedroomSelectionId = document.getElementById('bedrooms').options[bedroomSelection].value;

    if (bedroomSelectionId == -1) {
		alert("You must select the number of bedrooms.");
		document.getElementById('bedrooms').focus();
		return false;
	}
	
	var receptionSelection = document.getElementById('receptions').selectedIndex;
    var receptionSelectionId = document.getElementById('receptions').options[receptionSelection].value;

    if (receptionSelectionId == -1) {
		alert("You must select the number of reception rooms.");
		document.getElementById('receptions').focus();
		return false;
	}
	
	var parkingSelection = document.getElementById('offroad_parking').selectedIndex;
    var parkingSelectionId = document.getElementById('offroad_parking').options[parkingSelection].value;

    if (parkingSelectionId == -1) {
		alert("Please specify if there is off-road parking.");
		document.getElementById('offroad_parking').focus();
		return false;
	}
	
	var garageSelection = document.getElementById('garage').selectedIndex;
    var garageSelectionId = document.getElementById('garage').options[garageSelection].value;

    if (garageSelectionId == -1) {
		alert("Please specify if there is a garage.");
		document.getElementById('garage').focus();
		return false;
	}
	
	var tenureSelection = document.getElementById('tenure').selectedIndex;
    var tenureSelectionId = document.getElementById('tenure').options[tenureSelection].value;

    if (tenureSelectionId == -1) {
		alert("Please specify the tenure type.");
		document.getElementById('tenure').focus();
		return false;
	}
	
	var approxValue = document.getElementById('approx_value');
	if (approxValue.value == "") {
		alert("You must enter the approximate value of the property.");
		approxValue.focus();
		return false;
	}
	
	if (approxValue.value != "") {
		if (!isNumeric(approxValue.value)) {
			alert("The approximate value of the property must be numeric.");
			approxValue.focus();
			return false;
		}
	}
	
	var problemSelection = document.getElementById('problems').selectedIndex;
    var problemSelectionId = document.getElementById('problems').options[problemSelection].value;

    if (problemSelectionId == -1) {
		alert("Please specify if there are any problems with the structure.");
		document.getElementById('problems').focus();
		return false;
	}
	
	if (problemSelectionId == 1) {
		var problemDetails = document.getElementById('problem_details');
		if (problemDetails.value == "") {
			alert("Please specify the nature of these problems.");
			document.getElementById('problem_details').focus();
			return false;
		}
	}
	
	var decorSelection = document.getElementById('decor').selectedIndex;
    var decorSelectionId = document.getElementById('decor').options[decorSelection].value;

    if (decorSelectionId == -1) {
		alert("You must select the decor condition.");
		document.getElementById('decor').focus();
		return false;
	}
	
	var onSaleSelection = document.getElementById('on_market').selectedIndex;
    var onSaleSelectionId = document.getElementById('on_market').options[onSaleSelection].value;

    if (onSaleSelectionId == -1) {
		alert("Please specify if the property is currently on sale with an estate agent.");
		document.getElementById('on_market').focus();
		return false;
	}
	
	if (onSaleSelectionId == 1) {
		var salePeriodSelection = document.getElementById('on_market_period').selectedIndex;
		var salePeriodSelectionId = document.getElementById('on_market_period').options[salePeriodSelection].value;
		
		if (salePeriodSelectionId == -1) {
			alert("Please specify how long the property has been on sale.");
			document.getElementById('on_market_period').focus();
			return false;
		}
	}
	
	var anotherHomeSelection = document.getElementById('found_another').selectedIndex;
    var anotherHomeSelectionId = document.getElementById('found_another').options[anotherHomeSelection].value;

    if (anotherHomeSelectionId == -1) {
		alert("Please specify if you have found another home.");
		document.getElementById('found_another').focus();
		return false;
	}
	
	var sellPeriodSelection = document.getElementById('sell_period').selectedIndex;
    var sellPeriodSelectionId = document.getElementById('sell_period').options[sellPeriodSelection].value;

    if (sellPeriodSelectionId == -1) {
		alert("Please specify how quickly you are looking to sell.");
		document.getElementById('sell_period').focus();
		return false;
	}
	
	var securedLoanSelection = document.getElementById('secured_loans').selectedIndex;
    var securedLoanSelectionId = document.getElementById('secured_loans').options[securedLoanSelection].value;

    if (securedLoanSelectionId == -1) {
		alert("Please specify if you have any secured loans against the property.");
		document.getElementById('secured_loans').focus();
		return false;
	}
	
	if (securedLoanSelectionId == 1) {
		var securedLoanValue = document.getElementById('secured_loan_value')
		if (securedLoanValue.value == "") {
			alert("You must enter the secured loan value.");
			securedLoanValue.focus();
			return false;
		}
		if (securedLoanValue.value != "") {
			if (!isNumeric(securedLoanValue.value)) {
				alert("The secured loan value must be numeric.");
				securedLoanValue.focus();
				return false;
			}
		}
	}
	
	return true;
}

function togglePropertyAddress() {
	toggleField = document.getElementById('same_address');

    if (toggleField.checked) {
        hideBlock('property_address_div');
    } else {
        showBlock('property_address_div');
    }
}

function toggleSecuredLoan() {
	var xSelection = document.getElementById('secured_loans').selectedIndex;
    var xSelectionId = document.getElementById('secured_loans').options[xSelection].value;

    if (xSelectionId == 1) {
        showBlock('secured_loan_div');
    } else {
        hideBlock('secured_loan_div');
    }
}

function toggleProblemDiv() {
	var xSelection = document.getElementById('problems').selectedIndex;
    var xSelectionId = document.getElementById('problems').options[xSelection].value;

    if (xSelectionId == 1) {
        showBlock('problem_div');
    } else {
        hideBlock('problem_div');
    }
}

function toggleOnSaleAlready() {
	var xSelection = document.getElementById('on_market').selectedIndex;
    var xSelectionId = document.getElementById('on_market').options[xSelection].value;

    if (xSelectionId == 1) {
        showBlock('onsale_div');
    } else {
        hideBlock('onsale_div');
    }
}
