	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 viewProperty(propertyId) {
		document.view_form.property_id.value = propertyId;
		document.view_form.submit();
	}
	
	function addFavourite(propertyId) {
		document.view_form.property_id.value = propertyId;
		document.view_form.action = "add_favourite.php";
		document.view_form.submit();
	}
	
	function deleteFavourite(propertyId) {
		document.view_form.property_id.value = propertyId;
		document.view_form.action = "delete_favourite.php";
		document.view_form.submit();
	}
	
	function confirmDelete(propertyId, address) {
		if (confirm("Are you sure want to delete "+address+"?")) {
			document.admin_form.action = "delete_property.php";
			document.admin_form.property_id.value = propertyId;
			document.admin_form.submit();
		}
	}
	
	function editProperty(propertyId) {
		document.admin_form.action = "edit_property.php";
		document.admin_form.property_id.value = propertyId;
		document.admin_form.submit();
	}
	
	function adminViewProperty(propertyId) {
		document.admin_form.action = "view_property.php";
		document.admin_form.property_id.value = propertyId;
		document.admin_form.submit();
	}
	
	function viewPropertyStats(propertyId) {
		document.admin_form.action = "property_stats.php";
		document.admin_form.property_id.value = propertyId;
		document.admin_form.submit();
	}
	
	function viewViewingsReport(propertyId) {
		document.admin_form.action = "viewings_report.php";
		document.admin_form.property_id.value = propertyId;
		document.admin_form.submit();
	}
	
	function saleProgress(propertyId) {
		document.admin_form.action = "sale_progress.php";
		document.admin_form.property_id.value = propertyId;
		document.admin_form.submit();
	}
	
	function viewBids(propertyId) {
		document.admin_form.action = "bid_history.php";
		document.admin_form.property_id.value = propertyId;
		document.admin_form.submit();
	}
	
	function filterTown() {
		document.admin_form.action = "index.php";
		document.admin_form.submit();
	}
	
	function cancelAdmin() {
		document.forms[0].action = "index.php";
		document.forms[0].submit();
	}
	
	function cancelUserAdmin() {
		document.forms[0].action = "users_index.php";
		document.forms[0].submit();
	}
	
	function cancelSmsAdmin() {
		document.forms[0].action = "sms_index.php";
		document.forms[0].submit();
	}
	
	function validateAddForm() {
		address = document.add_form.address.value;
		heading = document.add_form.heading.value;
		description = document.add_form.description.value;
		price = document.add_form.price.value;
		
		if (address == "") {
			alert("You must enter the address.");
			document.add_form.address.focus();
			return false;
		}
		if (heading == "") {
			alert("You must enter a property heading.");
			document.add_form.heading.focus();
			return false;
		}
		if (description == "") {
			alert("You must enter the property description.");
			document.add_form.description.focus();
			return false;
		}
		
		if (price != "" && (!isNumeric(price))) {
			alert("The price must only contain numeric characters.");
			document.add_form.price.focus();
			return false;
		}
		
		return true;
	}
	
	function validateEditForm() {
		tab = document.edit_form.tab.value;
		if (tab == "main") {
			address = document.edit_form.address.value;
			heading = document.edit_form.heading.value;
			description = document.edit_form.description.value;
			price = document.edit_form.price.value;
		
			if (address == "") {
				alert("You must enter the address.");
				document.edit_form.address.focus();
				return false;
			}
			if (heading == "") {
				alert("You must enter a property heading.");
				document.edit_form.heading.focus();
				return false;
			}
			if (description == "") {
				alert("You must enter the property description.");
				document.edit_form.description.focus();
				return false;
			}
			
			if (price != "" && (!isNumeric(price))) {
				alert("The price must only contain numeric characters.");
				document.edit_form.price.focus();
				return false;
			}
		}
		if (tab == "private") {
			ownerPhone = document.edit_form.owner_phone.value;
			ownerMobile = document.edit_form.owner_mobile.value;
			ownerEmail = document.edit_form.owner_email.value;
		
			if ((ownerPhone != "") && (!isNumeric(ownerPhone))) {
				alert("Phone numbers can only contain numeric characters.");
				document.edit_form.owner_phone.focus();
				return false;
			}
			if ((ownerMobile != "") && (!isNumeric(ownerMobile))) {
				alert("Phone numbers can only contain numeric characters.");
				document.edit_form.owner_mobile.focus();
				return false;
			}
			if (ownerEmail != "") {
				// 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=ownerEmail.indexOf("@"); 
				dotpos=ownerEmail.lastIndexOf(".");
				lastpos=ownerEmail.length-1;
				if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
					alert("You must enter a valid email address.");
						document.edit_form.owner_email.focus();
					return false;
				}
			}
		}
		return true;
	}
	
	function validateContactForm() {
		fullName = document.contact_form.full_name.value;
		emailAddress = document.contact_form.email.value;
		query= document.contact_form.query.value;
		
		if (fullName == "") {
			alert("You must enter your name");
			document.contact_form.full_name.focus();
			return false;
		}
		if (emailAddress == "") {
			alert("You must enter your email address");
			document.contact_form.email.focus();
			return false;
		}

		// 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=emailAddress.indexOf("@"); 
		dotpos=emailAddress.lastIndexOf(".");
		lastpos=emailAddress.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("Please enter your query");
			document.contact_form.query.focus();
			return false;
		}
	}
	
	function validateTellFriendForm() {
		yourName = document.tell_form.your_name.value;
		yourEmail = document.tell_form.your_email.value;
		friendsName = document.tell_form.friends_email.value;
		friendsEmail = document.tell_form.friends_email.value;
		
		if (yourName == "") {
			alert("You must enter your name.");
			document.tell_form.your_name.focus();
			return false;
		}
		if (yourEmail != "") {
			// 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=yourEmail.indexOf("@"); 
			dotpos=yourEmail.lastIndexOf(".");
			lastpos=yourEmail.length-1;
			if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
				alert("You must enter a valid email address.");
					document.tell_form.your_email.focus();
				return false;
			}
		}
		if (friendsEmail == "") {
			alert("You must enter your friend's email address.");
			document.tell_form.friends_email.focus();
			return false;
		}
		apos=friendsEmail.indexOf("@"); 
		dotpos=friendsEmail.lastIndexOf(".");
		lastpos=friendsEmail.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
			alert("You must enter a valid email address.");
		    document.tell_form.friends_email.focus();
			return false;
		}
		
		return true;
	}
	
	function validateArrangeViewingForm() {
		fullName = document.arrange_form.full_name.value;
		phone = document.arrange_form.phone.value;
		
		if (fullName == "") {
			alert("You must enter your name.");
			document.arrange_form.full_name.focus();
			return false;
		}
		if (phone == "") {
			alert("You must enter a contact number.");
			document.arrange_form.phone.focus();
			return false;
		}
		if (phone != "" && (!isNumeric(phone))) {
			alert("The phone number must only contain numeric characters.");
			document.arrange_form.phone.focus();
			return false;
		}
		return true;
	}
	
	function sortSearch(field, order) {
		document.search_form.sort_field.value = field;
		document.search_form.sort_order.value = order;
		document.search_form.submit();
	}
	
	function newDevelopments() {
		document.search_form.sector_id.value = 4;
		document.search_form.submit();
	}
	
	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 containsInvalidChars(strString) {
			var strInvalidChars = " /:,;?#";
			for (i=0; i<strInvalidChars.length; i++) {
					badChar = strInvalidChars.charAt(i);
					if (strString.indexOf(badChar,0) > -1) {
						return true;
					}
			}
			return false;
	}
	
	function displayImage()  {
		photoField = "document.edit_form.photo_pick.value";
		photoNameTemp = eval(photoField);
		
		// Check file extension
		if (!checkImageFileExtension(photoNameTemp)) {			
			return false;
		}
																
		photoName = "file://"+replaceChars(photoNameTemp);
		photoDisplay = "document.edit_form.photo_pick_display.src = '"+photoName+"'";
		if (photoNameTemp != "") {
			photoDisplay = "document.edit_form.photo_pick_display.src = '"+photoName+"'";
			document.edit_form.add_photo.disabled = false;
		} else {
			photoDisplay = "document.edit_form.photo_pick_display.src = '../images/no-image.gif'";
		}
		eval(photoDisplay);
	}
	
	function addImage()  {
		propertyId = document.edit_form.property_id.value;
		document.edit_form.new_tab.value = "photos";
		document.edit_form.action = "edit_property.php";
		document.edit_form.submit();
	}
	
	function checkImageFileExtension(filename) {
		if((filename.lastIndexOf(".jpg") == -1) 
				&& (filename.lastIndexOf(".JPG") == -1) 
				&& (filename.lastIndexOf(".jpeg") == -1) 
				&& (filename.lastIndexOf(".JPEG") == -1) 
				&& (filename.lastIndexOf(".gif") == -1) 
				&& (filename.lastIndexOf(".GIF") == -1)) {
			alert("You can upload only GIF and JPG files");
			return false;
		}
		return true;
	}
	
	function deleteImage(imageNo)  {
		if (confirm("Are you sure you want to delete this image?")) {
			propertyId = document.edit_form.property_id.value;
			document.edit_form.new_tab.value = "photos";
			document.edit_form.photo_id.value = imageNo;
			document.edit_form.action = "edit_property.php";
			document.edit_form.submit();
		}
	}
	
	function addBrochure()  {
		propertyId = document.edit_form.property_id.value;
		document.edit_form.new_tab.value = "brochure";
		document.edit_form.action = "edit_property.php";
		document.edit_form.submit();
	}
	
	function checkBrochureFileExtension() {
		brochureField = "document.edit_form.brochure.value";
		brochureFilename = eval(brochureField);
		if((brochureFilename.lastIndexOf(".doc")==-1) && (brochureFilename.lastIndexOf(".pdf")==-1)) {
			alert("You can upload only DOC and PDF files");
			return false;
		}
		return true;
	}
	
	function deleteBrochure()  {
		if (confirm("Are you sure you want to delete this brochure?")) {
			propertyId = document.edit_form.property_id.value;
			document.edit_form.new_tab.value = "brochure";
			document.edit_form.delete_flag.value = "true";
			document.edit_form.action = "edit_property.php";
			document.edit_form.submit();
		}
	}	

	function swapEditTab(newTab) {
		// Distinction between update and edit because edit doesn't mean save.
		document.edit_form.new_tab.value = newTab;
		document.edit_form.swap.value = "true";
		if (!confirm("Do you want to save changes before leaving this tab?")) {
			document.edit_form.save.value = "false";
		}
		document.edit_form.submit();
	}
	
	function swapAddTab(newTab) {
		currentTab = document.add_form.tab.value;
		if ((currentTab == "main") && (validateAddForm() == false)) {
				return;
		}
		document.add_form.new_tab.value = newTab;
		document.add_form.swap.value = "true";
		document.add_form.submit();
	}
	
	function swapViewTab(newTab) {
		document.view_form.new_tab.value = newTab;
		document.view_form.swap.value = "true";
		document.view_form.submit();
	}
	
	function showPhoto(id, width, height) {
	  newWindow = window.open('view_photo.php?id='+id, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width='+width+', height='+height+', left=150, top=150');
	}
	
	function showBrochure(id) {
	  newWindow = window.open('view_brochure.php?id='+id, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=600, height=500');
	}
	
	function tellAFriend(id) {
	  newWindow = window.open('tell_friend.php?property_id='+id, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=350, height=300, left=150, top=150');
	}
	
	function setFeaturedProperty() {
		document.admin_form.action = "featured_property.php";
		document.admin_form.featured_action.value = "set";
		document.admin_form.submit();
	}
	
	function unsetFeaturedProperty() {
		document.admin_form.action = "featured_property.php";
		document.admin_form.featured_action.value = "unset";
		document.admin_form.submit();
	}
	
	function validateRegisterForm() {
		first_name = document.register_form.first_name.value;
		surname = document.register_form.surname.value;
		username = document.register_form.username.value;
		password = document.register_form.password.value;
		email = document.register_form.email.value;
		verify_email = document.register_form.verify_email.value;
		
		if (username == "") {
			alert("You must enter your username.");
			document.register_form.username.focus();
			return false;
		}
		if (containsInvalidChars(username)) {
			alert("Your username cannot contain spaces or symbols. Please choose another.");
			document.register_form.username.focus();
			return false;
		}
		if (password == "") {
			alert("You must enter your password.");
			document.register_form.password.focus();
			return false;
		}
		if (first_name == "") {
			alert("You must enter your first name.");
			document.register_form.first_name.focus();
			return false;
		}
		if (surname == "") {
			alert("You must enter your surname.");
			document.register_form.surname.focus();
			return false;
		}
		if (email == "") {
			alert("You must enter your email address.");
			document.register_form.email.focus();
			return false;
		}
		
		// 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.register_form.email.focus();
			return false;
		}
		
		if (verify_email == "" || verify_email != email) {
			alert("You have entered non-matching email addresses.");
			document.register_form.verify_email.focus();
			return false;
		}
		
		password = document.register_form.password.value;
		hash = hex_md5(password);
		document.register_form.password.value = hash;
		return true;
  }
	
	function validateUpdateDetailsForm() {
		first_name = document.update_form.first_name.value;
		surname = document.update_form.surname.value;
		password = document.update_form.password.value;
		verify_password = document.update_form.verify_password.value;
		email = document.update_form.email.value;
		phone = document.update_form.phone.value;

		if (first_name == "") {
			alert("You must enter your first name.");
			document.update_form.first_name.focus();
			return false;
		}
		if (surname == "") {
			alert("You must enter your surname.");
			document.update_form.surname.focus();
			return false;
		}
		if (phone != "" && (!isNumeric(phone))) {
			alert("The phone number must only contain numeric characters.");
			document.update_form.phone.focus();
			return false;
		}
		if (email == "") {
			alert("You must enter your email address.");
			document.update_form.email.focus();
			return false;
		}
		
		// 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.update_form.email.focus();
			return false;
		}
		
		if (password != "") {		
			if (verify_password == "" || verify_password != password) {
				alert("You have entered non-matching passwords.");
				document.update_form.verify_password.focus();
				return false;
			}
			password = document.update_form.password.value;
			hash = hex_md5(password);
			document.update_form.password.value = hash;
		}
		
		return true;
  }
	
	function resetSearch() {
		var town_count = document.search_form.town_id.length;
		var minprice_count = document.search_form.min_price.length;
		var maxprice_count = document.search_form.max_price.length;
		var sector_count = document.search_form.sector_id.length;
		var saleterms_count = document.search_form.saleterms_id.length;
		var type_count = document.search_form.type_id.length;
		var bedroom_count = document.search_form.bedrooms.length;
		
		var reset_town_id = -1;
		var reset_min_price = 0;
		var reset_max_price = -1;
		var reset_type_id = -1;
		var reset_sector_id = -1;
		var reset_saleterms_id = 1;
		var reset_bedrooms = -1;
		
		for (x=0; x<town_count; x++) {
			if (document.search_form.town_id.options[x].value == reset_town_id) {
				document.search_form.town_id.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<minprice_count; x++) {
			if (document.search_form.min_price.options[x].value == reset_min_price) {
				document.search_form.min_price.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<maxprice_count; x++) {
			if (document.search_form.max_price.options[x].value == reset_max_price) {
				document.search_form.max_price.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<sector_count; x++) {
			if (document.search_form.sector_id.options[x].value == reset_sector_id) {
				document.search_form.sector_id.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<saleterms_count; x++) {
			if (document.search_form.saleterms_id.options[x].value == reset_saleterms_id) {
				document.search_form.saleterms_id.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<type_count; x++) {
			if (document.search_form.type_id.options[x].value == reset_type_id) {
				document.search_form.type_id.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<bedroom_count; x++) {
			if (document.search_form.bedrooms.options[x].value == reset_bedrooms) {
				document.search_form.bedrooms.options[x].selected = true;
				break;
			}
		}
	
		document.search_form.text_string.value = "";
	}
	
	function resetPreferences() {
		var town_count = document.prefs_form.town_id.length;
		var minprice_count = document.prefs_form.min_price.length;
		var maxprice_count = document.prefs_form.max_price.length;
		var type_count = document.prefs_form.type_id.length;
		var sector_count = document.prefs_form.sector_id.length;
		var saleterms_count = document.prefs_form.saleterms_id.length;
		var bedroom_count = document.prefs_form.bedrooms.length;
		
		var reset_town_id = -1;
		var reset_min_price = 0;
		var reset_max_price = -1;
		var reset_sector_id = -1;
		var reset_saleterms_id = -1;
		var reset_type_id = -1;
		var reset_bedrooms = -1;
		
		for (x=0; x<town_count; x++) {
			if (document.prefs_form.town_id.options[x].value == reset_town_id) {
				document.prefs_form.town_id.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<minprice_count; x++) {
			if (document.prefs_form.min_price.options[x].value == reset_min_price) {
				document.prefs_form.min_price.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<maxprice_count; x++) {
			if (document.prefs_form.max_price.options[x].value == reset_max_price) {
				document.prefs_form.max_price.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<sector_count; x++) {
			if (document.prefs_form.sector_id.options[x].value == reset_sector_id) {
				document.prefs_form.sector_id.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<saleterms_count; x++) {
			if (document.prefs_form.saleterms_id.options[x].value == reset_saleterms_id) {
				document.prefs_form.saleterms_id.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<type_count; x++) {
			if (document.prefs_form.type_id.options[x].value == reset_type_id) {
				document.prefs_form.type_id.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<bedroom_count; x++) {
			if (document.prefs_form.bedrooms.options[x].value == reset_bedrooms) {
				document.prefs_form.bedrooms.options[x].selected = true;
				break;
			}
		}
	
		document.prefs_form.search_string.value = "";
		document.prefs_form.newsletter_consent.checked = true;
		document.prefs_form.email_consent.checked = true;
		document.prefs_form.sms_consent.checked = true;
	}
	
	function reloadPreferences() {
		document.search_form.action = "reload_prefs.php";
		document.search_form.submit();
	}
	
	function enablePriceField() {
		priceTermsId = document.forms[0].priceterms_id.value;
		if (priceTermsId != 1) {
			document.forms[0].price.disabled = false;
		} else {
				document.forms[0].price.disabled = true;
		}
	}
	
	function backupDatabase() {
		document.admin_form.action = "data_backup.php";
		document.admin_form.submit();
	}
	
	function editSmsDetails(propertyId) {
		document.forms[0].action = "edit_sms_details.php";
		document.forms[0].property_id.value = propertyId;
		document.forms[0].submit();
	}
	
	function validateSmsEditForm() {
		smsStatus = document.edit_form.sms_status.value;
		smsCode = document.edit_form.sms_code.value;
		originalSmsCode = document.edit_form.original_sms_code.value;
		smsText = document.edit_form.sms_text.value;
		
		if (smsStatus == "Y" && smsCode == "") {
			alert("You must enter an SMS code for an active listing");
			document.edit_form.sms_code.focus();
			return false;
		}
		if (smsStatus == "Y" && smsText == "") {
			alert("You must enter an SMS description for an active listing");
			document.edit_form.sms_text.focus();
			return false;
		}
		if (smsStatus == "Y" && !isNumeric(smsCode)) { 
			alert('SMS Code must be a numeric value');
			document.edit_form.sms_code.focus(); 
			document.edit_form.sms_code.value = originalSmsCode;
			return false;
	  }
		return true;
  }
	
	function updateSmsCode() {
		newFlag = document.edit_form.new_flag.value;
		smsStatus = document.edit_form.sms_status.value;
		originalSmsCode = document.edit_form.original_sms_code.value;
		availableSmsCode = document.edit_form.available_sms_code.value;
		smsCode = document.edit_form.sms_code.value;
		
		if (smsStatus == "N") {
			document.edit_form.sms_code.value = "";
		} else {
			if (newFlag == "Y") {
				document.edit_form.sms_code.value = availableSmsCode;
			} else {
				document.edit_form.sms_code.value = originalSmsCode;
			}
		}
  }
	
	function limitEditChars(element, maxChars){
		if ( document.edit_form(element).type == "textarea" ) {
			if (document.edit_form(element).innerText.length > maxChars ) {
				document.edit_form(element).innerText = document.edit_form(element).innerText.substring(0,maxChars);
			}
		} else if ( document.edit_form(element).type = "text") {
			if (document.edit_form(element).value.length > maxChars ) {
				document.edit_form(element).value = document.edit_form(element).value.substring(0,maxChars);
			}
		}
	}
	
	function viewUserDetails(userId) {
		document.forms[0].action = "view_user.php";
		document.forms[0].user_id.value = userId;
		document.forms[0].submit();
	}
	
	function generateErrorReport() {
		document.reports_form.action = "sms_error_report.php";
		document.reports_form.submit();
	}
	
	function arrangeViewing(id) {
	  newWindow = window.open('arrange_viewing.php?property_id='+id, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=350, height=230, left=150, top=150');
	}
	
	function processViewingRequest(viewingId) {
		document.request_form.viewing_id.value = viewingId;
		document.request_form.action = "process_viewing_request.php";
		document.request_form.submit();
	}
	
	function cancelProcessViewing() {
		document.forms[0].action = "viewings_index.php";
		document.forms[0].submit();
	}
	
	function confirmDeleteViewing(viewingId, address) {
		if (confirm("Are you sure want to delete this viewing at "+address+"?")) {
			document.viewing_form.action = "delete_viewing.php";
			document.viewing_form.viewing_id.value = viewingId;
			document.viewing_form.submit();
		}
	}
	
	function editViewing(viewingId) {
		document.viewing_form.action = "edit_viewing.php";
		document.viewing_form.viewing_id.value = viewingId;
		document.viewing_form.submit();
	}
	
	function reloadDiary(action) {
		if (action == "edit") {
			document.request_form.action = "edit_viewing.php";
		} else {
			document.request_form.action = "process_viewing_request.php";
		}
		document.request_form.save.value = "reload";
		document.request_form.viewing_date.value = document.request_form.request_date.value;
		document.request_form.submit();
	}
	
	function removeChar(aString, aCharToBeRemoved) {
		while(aString.indexOf(aCharToBeRemoved)!=-1) {
			aString = aString.substring(0,aString.indexOf(aCharToBeRemoved)) + 
				aString.substring(aString.indexOf(aCharToBeRemoved)+1,aString.length)
		}
		return aString;
	}
	
	function calculateMortgage(price) {
		numericPrice = removeChar(price, ',');
		numericPrice = removeChar(numericPrice, '£');
		newWindow = window.open('calculate_mortgage.php?price='+numericPrice, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=500, height=475, left=150, top=150');
	}
	
	function resetMortgageForm() {
		document.mortgage_form.mortgage.value = document.mortgage_form.mortgage_required.value;
		document.mortgage_form.term.value = document.mortgage_form.mortgage_term.value;
		document.mortgage_form.rate.value = document.mortgage_form.mortgage_rate.value;
		computePayment();
		document.mortgage_form.mortgage.focus();
	}
	
	function computePayment() {
		mortgage = document.mortgage_form.mortgage.value;
		term = document.mortgage_form.term.value;
		rate = document.mortgage_form.rate.value;
		
		if (isNaN(mortgage) || mortgage < 1) {
			document.mortgage_form.payment.value = '';
			document.mortgage_form.error.value = '-- Please fill in MORTGAGE VALUE --';
			return false;
		}
		if (isNaN(term) || term < 1) {
			document.mortgage_form.payment.value = '';
			document.mortgage_form.error.value = '-- Please fill in MORTGAGE TERM --';
			return false;
		}
		if (isNaN(rate) || rate < .1) {
			document.mortgage_form.payment.value = '';
			document.mortgage_form.error.value = '-- Please fill in INTEREST RATE --';
			return false;
		}
		document.mortgage_form.error.value = '';
		var payment = 0;
		var payment12 = 0;
		rate = 1 + (document.mortgage_form.rate.value/100);
		rate = Math.pow(rate,(1/12));
		term = document.mortgage_form.term.value*12;
		mortgage = document.mortgage_form.mortgage.value;
		var x = 0;
		x = Math.pow(rate,term);
		rate12 = Math.pow(1.12,(1/12));
		x12 = Math.pow(rate12,term);
		if (x==1) {
			payment = mortgage/term;
		} else {
			payment = ((mortgage*x)*(1-rate))/(1-x);
		}
		document.mortgage_form.payment.value = Math.round(payment*100)/100;
	}
	
	function cancelAdd() {
		history.back();
	}
	
	function cancelAddBid() {
		document.add_form.action = "sale_progress.php";
		document.add_form.save.value = false;
		document.add_form.submit();
	}
	
	function validateAddBidForm() {
		bidderName = document.add_form.bidder_name.value;
		bidderPhone = document.add_form.bidder_phone.value;
		bidValue = document.add_form.bid.value;
		
		if (bidderName == "") {
			alert("You must enter the bidder's name");
			document.add_form.bidder_name.focus();
			return false;
		}
		if (bidderPhone == "") {
			alert("You must enter the bidder's phone number");
			document.add_form.bidder_phone.focus();
			return false;
		}
		if (bidValue == "") {
			alert("You must enter a bid value");
			document.add_form.bid.focus();
			return false;
		}
		if (bidValue != "" && !isNumeric(bidValue)) { 
			alert('The bid value must be numeric');
			document.add_form.bid.focus(); 
			return false;
	  }
		return true;
  	}
	
	function confirmDeleteBid(bidId, bidValue, bidder) {
		if (confirm("Are you sure want to delete this bid of £"+bidValue+" by "+bidder+"?")) {
			document.bids_form.action = "delete_bid.php";
			document.bids_form.bid_id.value = bidId;
			document.bids_form.submit();
		}
	}
	
	function editBid(bidId) {
		document.sale_form.action = "edit_bid.php";
		document.sale_form.bid_id.value = bidId;
		document.sale_form.submit();
	}
	
	function updateSaleProgress() {
		document.sale_form.action = "sale_progress.php";
		document.sale_form.save.value = true;
		document.sale_form.submit();
	}
	
	function bidLetter(propertyId, bidId) {
		newWindow = window.open('bid_confirm_letter.php?property_id='+propertyId+'&bid_id='+bidId, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes');
	}
	
	function addContact() {
		document.user_filters_form.action = "add_contact.php";
		document.user_filters_form.submit();
	}
	
	function confirmDeleteContact(userId, userName) {
		if (confirm("Are you sure want to delete "+userName+"?")) {
			document.user_filters_form.action = "delete_contact.php";
			document.user_filters_form.user_id.value = userId;
			document.user_filters_form.submit();
		}
	}
	
	function editUserDetails(userId) {
		document.user_filters_form.action = "edit_contact.php";
		document.user_filters_form.user_id.value = userId;
		document.user_filters_form.submit();
	}
	
	function matchPreferences() {
		document.user_filters_form.action = "match_prefs.php";
		document.user_filters_form.submit();
	}
	
	function resetMatchPreferences() {
		var town_count = document.match_form.town_id.length;
		var minprice_count = document.match_form.min_price.length;
		var maxprice_count = document.match_form.max_price.length;
		var type_count = document.match_form.type_id.length;
		var bedroom_count = document.match_form.bedrooms.length;
		
		var reset_town_id = -1;
		var reset_min_price = 0;
		var reset_max_price = -1;
		var reset_type_id = -1;
		var reset_bedrooms = -1;
		
		for (x=0; x<town_count; x++) {
			if (document.match_form.town_id.options[x].value == reset_town_id) {
				document.match_form.town_id.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<minprice_count; x++) {
			if (document.match_form.min_price.options[x].value == reset_min_price) {
				document.match_form.min_price.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<maxprice_count; x++) {
			if (document.match_form.max_price.options[x].value == reset_max_price) {
				document.match_form.max_price.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<type_count; x++) {
			if (document.match_form.type_id.options[x].value == reset_type_id) {
				document.match_form.type_id.options[x].selected = true;
				break;
			}
		}
		for (x=0; x<bedroom_count; x++) {
			if (document.match_form.bedrooms.options[x].value == reset_bedrooms) {
				document.match_form.bedrooms.options[x].selected = true;
				break;
			}
		}
		document.match_form.exact_price.value = "";
	}
	
	function setTownName() {
		var townSelection = document.config_form.town_id.selectedIndex;	
		if (townSelection != 0) {
			var townName = document.config_form.town_id.options[townSelection].text;
			document.config_form.town_name.disabled = false;
			document.config_form.update_button.disabled = false;
			document.config_form.delete_button.disabled = false;
			document.config_form.town_name.value = townName;
		} else {
				document.config_form.town_name.value = "";
				document.config_form.town_name.disabled = true;
				document.config_form.update_button.disabled = true;
			document.config_form.delete_button.disabled = true;
		}
	}
	
	function addTown() {
		var townName = document.config_form.new_town_name.value;
		if (townName == "") {
			alert("You must enter a town name");
			document.config_form.new_town_name.focus();
			return false;
		}
		document.config_form.action_flag.value = "add";
		document.config_form.submit();
	}
	
	function confirmDeleteTown() {
		var townSelection = document.config_form.town_id.selectedIndex;	
		var townName = document.config_form.town_id.options[townSelection].text;
		if (confirm("Are you sure want to delete "+townName+"?")) {
			document.config_form.action_flag.value = "delete";
			document.config_form.submit();
		}
		return false;
	}
	
	function validateUpdateTownForm() {
		var townName = document.config_form.town_name.value;
		if (townName == "") {
			alert("You must enter a town name");
			document.config_form.town_name.focus();
			return false;
		}
		return true;
	}
	
	function setSearchTownName()  {
		var townSelection = document.search_form.town_id.selectedIndex;	
		var townName = document.search_form.town_id.options[townSelection].text;
		document.search_form.town_name.value = townName;
	}
	
	function setPriceValues() {
		var termsSelection = document.search_form.saleterms_id.selectedIndex;	
		var termsId = document.search_form.saleterms_id.options[termsSelection].value;
		// Treat "Any" as "For Sale"
		if (termsId == -1) {
			termsId = 1;
		}
		
		minPriceSelect = document.getElementById('min_price');
		minPriceSelect.length=0;
		
		minValueCount = minPriceValues[termsId].length - 1;
		for (x=0; x<minValueCount; x++) {
			priceId = minPriceValues[termsId][x+1][0];
			priceText = minPriceValues[termsId][x+1][1];
			var newPriceOption = document.createElement('option');
			newPriceOption.value = priceId;
			newPriceOption.text = priceText;
			
			// position
			var insertAt = minPriceSelect.options.length;
        
            // create the space
            minPriceSelect.options.length=minPriceSelect.options.length + 1;
              // add the option
            minPriceSelect.options[insertAt] = newPriceOption;
		}
		
		maxPriceSelect = document.getElementById('max_price');
		maxPriceSelect.length=0;
		
		maxValueCount = maxPriceValues[termsId].length - 1;
		for (x=0; x<maxValueCount; x++) {
			priceId = maxPriceValues[termsId][x+1][0];
			priceText = maxPriceValues[termsId][x+1][1];
			var newPriceOption = document.createElement('option');
			newPriceOption.value = priceId;
			newPriceOption.text = priceText;
			
			// position
			var insertAt = maxPriceSelect.options.length;
        
            // create the space
            maxPriceSelect.options.length=maxPriceSelect.options.length + 1;
              // add the option
            maxPriceSelect.options[insertAt] = newPriceOption;
		}
	}
	
	function encodePassword() {
		password = document.login_form.password.value;
		hash = hex_md5(password);
		document.login_form.password.value = hash;
		document.login_form.submit();
	}
	
	function populateBidderDetails() {
		document.add_form.save.value = false;
		document.add_form.submit();
	}
	
	function allProperties() {
		document.search_form.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;
	}