function validateLogin() {
	var email = document.login_form.email.value;
	var password = document.login_form.password.value;
	
	if (email == "") {
		alert("You must enter your registered email address.");
		document.login_form.email.focus();
		return false;
	}
	if (password == "") {
		alert("You must enter a password.");
		document.login_form.password.focus();
		return false;
	}
	
	encodedPassword = encodePassword(password);
	document.login_form.password.value = encodedPassword;
}

function encodePassword(password) {
    var hash = hex_md5(password);
	return hash;
}

function showPhoto(id) {
    destUrl = baseUrl + "/home/photo/" + id;
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, left=150, top=150');
}

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

function displayImage()  {
	photoField = "document.forms[0].userfile.value";
	photoNameTemp = eval(photoField);
	
	// Check file extension
	if (!checkImageFileExtension(photoNameTemp)) {			
		return false;
	}
															
	photoName = "file://"+replaceChars(photoNameTemp);
	photoDisplay = "document.forms[0].userfile_display.src = '"+photoName+"'";
	if (photoNameTemp != "") {
		photoDisplay = "document.forms[0].userfile_display.src = '"+photoName+"'";
		document.forms[0].add_photo.disabled = false;
	} else {
		photoDisplay = "document.forms[0].userfile_display.src = '../images/no-image.gif'";
	}
	eval(photoDisplay);
}

function addImage()  {
    var imageName = document.edit_form.image_name.value;
    if (imageName == "") {
        alert("You must specify an image file.");
    } else {
    	var propertyId = document.edit_form.property_id.value;
    	document.edit_form.action = baseUrl+"/property/addImage/"+propertyId;
    	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 deleteImages()  {
    var propertyId = document.edit_form.property_id.value;
	document.edit_form.action = baseUrl+"/property/deleteImages/"+propertyId;
	document.edit_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;
}

function addFeature() {
    if (document.edit_form.feature_description.value != "") {
        newAction = baseUrl+"/property/addFeature";
        document.edit_form.action = newAction;
        return true;
    } else {
        alert("You must provide a feature description");
        document.edit_form.feature_description.focus();
        return false;
    }
}

function addBullet() {
    var validateFlag = false;
    if (document.edit_form.bullet_point.value != "") {
        newAction = baseUrl+"/property/addBullet";
        document.edit_form.action = newAction;
        validateFlag = true;
    } 
    if (validateFlag) {
        document.edit_form.submit();
    } else {
        alert("You must provide bullet point text.");
        document.edit_form.bullet_point.focus();
    }
}

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

function confirmDeleteFeature(featureId) {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		document.edit_form.feature_id.value = featureId;
		document.edit_form.action = baseUrl+"/property/deleteFeature"
		return true;
	} else {
	    return false;
	}
}

function confirmDeleteBullet(bulletId) {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		document.edit_form.bullet_id.value = bulletId;
		document.edit_form.action = baseUrl+"/property/deleteBullet"
		return true;
	} else {
	    return false;
	}
}

function toggleDataView() {
    propertyId = document.edit_form.property_id.value;
    document.edit_form.toggle_view.value = "true";
    document.edit_form.save.value = "false";    
    document.edit_form.action = baseUrl+"/property/edit/"+propertyId;
    document.cookie = "maintab=tcontent1";
    document.edit_form.submit();
}

function vendorTab() {
    document.edit_form.new_tab.value = "vendor";
	document.edit_form.submit();	
}

function bidsTab() {
    document.edit_form.new_tab.value = "bids";
	document.edit_form.submit();	
}

function progressTab() {
    document.edit_form.new_tab.value = "progress";
	document.edit_form.submit();	
}

function viewingsTab() {
    document.edit_form.new_tab.value = "viewings";
	document.edit_form.submit();	
}

function correspondenceTab() {
    document.edit_form.new_tab.value = "correspondence";
	document.edit_form.submit();	
}

function statsTab() {
    document.edit_form.new_tab.value = "stats";
	document.edit_form.submit();	
}

function addDiaryWindow() {
    destUrl = baseUrl + "/diary/add";
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=400, height=575, left=150, top=150');
}

function editDiaryWindow(id) {
    destUrl = baseUrl + "/diary/edit/" + id;
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=400, height=525, left=150, top=150');
}

function editViewingRequest(id) {
    destUrl = baseUrl + "/diary/editViewingRequest/" + id;
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=400, height=500, left=150, top=150');
}

function editValuationRequest(id) {
    destUrl = baseUrl + "/diary/editValuationRequest/" + id;
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=400, height=550, left=150, top=150');
}

function clearImage() {
    document.edit_form.image_name.value = "";
}

function loadBidderDetails() {
    document.edit_form.save.value = "false";
    document.edit_form.bid_action.value = "load_personal";
    document.edit_form.submit();
}

function loadSolicitorDetails() {
    document.edit_form.save.value = "false";
    document.edit_form.bid_action.value = "load_solicitor";
    document.edit_form.submit();
}

function refreshParent() {
    window.opener.focus();
    window.opener.location.reload(false);
    self.close();
}

function addPropertyCorrespondence() {
    document.edit_form.add_correspondence.value = "true";
    document.edit_form.submit();
}

function addCorrespondence() {
    document.filter_form.action = baseUrl + "/correspondence/add";
    document.filter_form.submit();
}

function todayDiary(direction) {
    document.diary_form.direction.value = direction;
    document.diary_form.submit();
}

function toggleOnlineSubscriberSection() {
    toggleField = document.details_form.online;
   
    if (toggleField.checked) {
    	document.getElementById("email_no").style.display = 'none';
    	document.getElementById("email_yes").style.display = 'inline'; 
        showBlock('onlineText');  
          
    } else {
    	document.getElementById("email_yes").style.display = 'none'; 
		document.getElementById("email_no").style.display = 'inline';   	
        hideBlock('onlineText');
        
    }
}

function toggleEditOnlineSubscriberSection() {
    toggleField = document.details_form.online;
   
    if (toggleField.checked) {
        showBlock('onlineText');  
          
    } else {  	
        hideBlock('onlineText');
    }
}

function toggleVendorAddress() {
    toggleField = document.getElementById('same_address');
    if (toggleField.checked) {
        hideBlock('vendorAddressDiv');
    } else {
        showBlock('vendorAddressDiv');
        document.getElementById('vendor_address1').focus();
    }
}

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

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

function addContactWindow() {
    propertyId = document.edit_form.property_id.value;
    destUrl = baseUrl + "/property/addContact/"+propertyId;
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=400, height=300, left=150, top=150');
}

function editContactWindow(contactId) {
    destUrl = baseUrl + "/property/editContact/"+contactId;
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=400, height=300, left=150, top=150');
}

function editFeature(featureId) {
    destUrl = baseUrl + "/property/editFeature/"+featureId;
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=450, height=200, left=150, top=150');
}

function validateVendorForm() {
    var validateFlag = true;
    var firstName = document.getElementById('vendor_first_name').value;
	var surname = document.getElementById('vendor_surname').value;
	
	if (firstName == "" && surname == "") {
	    if (!confirm("If a full name is not stored, all vendor details will be deleted.\n\nAre you sure you want to continue?")) {
	       validateFlag = false;
	       document.edit_form.vendor_first_name.focus();
	    }
	    document.edit_form.delete_vendor.value = "true";
	}
	
	var sameAddress = document.getElementById('same_address');
    if (!sameAddress.checked) {
        var address = document.getElementById('vendor_address1').value;
        var town = document.getElementById('vendor_town').value;
        var county = document.getElementById('vendor_county').value;
        var postcode = document.getElementById('vendor_postcode').value;
        
        if (address == "") {
            alert ("You must supply complete vendor address details");
            validateFlag = false;
        }
        if (town == "") {
            alert ("You must supply complete vendor address details");
            validateFlag = false;
        }
        if (county == "") {
            alert ("You must supply complete vendor address details");
            validateFlag = false;
        }
        if (postcode == "") {
            alert ("You must supply complete vendor address details");
            validateFlag = false;
        }
    }
    
	if (validateFlag) {
	    document.edit_form.submit();
	}
}

function validateBidForm() {
    var validateFlag = true;
    var firstName = document.edit_form.bidder_first_name.value;
	var surname = document.edit_form.bidder_surname.value;
	var bidValue = document.edit_form.bid_value.value;
	
	if (validateFlag && firstName == "") {
	    validateFlag = false;
	    alert("You must provide the bidder's full name");
	    document.edit_form.bidder_first_name.focus();
	}
	if (validateFlag && surname == "") {
	    validateFlag = false;
	    alert("You must provide the bidder's full name");
	    document.edit_form.bidder_surname.focus();
	}
	if (validateFlag && bidValue == "") {
	    validateFlag = false;
	    alert("You must provide the bid value");
	    document.edit_form.bid_value.focus();
	}
	if (validateFlag && !isNumeric(bidValue)) {
	    validateFlag = false;
	    alert("The bid value must only contain numeric characters");
	    document.edit_form.bid_value.focus();
	}
	if (validateFlag) {
	    document.edit_form.bid_action.value = "add";
	    document.edit_form.submit();
	}
}

function updateBidder() {
    var validateFlag = true;
    var bidderSelection = document.edit_form.bidder_id.selectedIndex;	
	var bidderId = document.edit_form.bidder_id.options[bidderSelection].value;
    var firstName = document.edit_form.bidder_first_name.value;
	var surname = document.edit_form.bidder_surname.value;
	
	if (bidderId == -1) {
	    validateFlag = false;
	    alert("You must select a bidder to update.");
		document.edit_form.bidder_id.focus();		
	}
	
	if (validateFlag && firstName == "") {
	    validateFlag = false;
	    alert("You must provide the bidder's full name");
	    document.edit_form.bidder_first_name.focus();
	}
	if (validateFlag && surname == "") {
	    validateFlag = false;
	    alert("You must provide the bidder's full name");
	    document.edit_form.bidder_surname.focus();
	}
	if (validateFlag) {
	    document.edit_form.bid_action.value = "update";
	    document.edit_form.submit();
	}
}

function resetCorrespondenceFilter() {
    document.filter_form.correspondence_type.value = -1;
    document.filter_form.town_id.value = -1;
    document.filter_form.property_id.value = -1;
    document.filter_form.submit();
}

function resetPropertyFilter() {
    document.filter_form.town_id.value = -1;
    document.filter_form.sale_status_id.value = -1;
    document.filter_form.owner_id.value = -1;
    document.filter_form.submit();
}

function loadFilterProperties() {
    new Ajax.Updater('property_div', baseUrl+'/property/ajaxTownProperties', { 
        method:'post', postBody:'town_id='+$F('town_id')
    });
}

function loadCorrespondenceProperties() {
    new Ajax.Updater('property_div', baseUrl+'/property/ajaxCorrespondenceProperties', {
        method:'post', postBody:'town_id='+$F('town_id')
    });
    //alert("setting property selection");
    townId = document.getElementById('town_id').value;
    if (document.getElementById('property_id') != null) {
        propertyId = document.getElementById('property_id').value;
    } else {
        propertyId = -1;
    }
    if (townId == -1) {
        hideBlock('contact_label');
        hideBlock('contact_div');
        showBlock('new_contact_label');
        showBlock('new_contact_div');
    } else {
        showBlock('contact_label');
        showBlock('contact_div');
        hideBlock('new_contact_label');
        hideBlock('new_contact_div');
    }
    if (townId == -1 && propertyId == -1) {
        hideBlock('contact_label');
        hideBlock('contact_div');
        showBlock('new_contact_label');
        showBlock('new_contact_div');
    }
    if (townId != -1 && propertyId == -1) {
        hideBlock('contact_label');
        hideBlock('contact_div');
        hideBlock('new_contact_label');
        hideBlock('new_contact_div');
    }
}
function loadPropertyContacts() {
    new Ajax.Updater('contact_div', baseUrl+'/property/ajaxPropertyContacts', {
        method:'post', postBody:'property_id='+$F('property_id')
    });
    
    //alert("setting property contacts");
    townId = document.getElementById('town_id').value;
    propertyId = document.getElementById('property_id').value;
    
    if (townId == -1 || propertyId == -1) {
        hideBlock('contact_label');
        hideBlock('contact_div');
        hideBlock('new_contact_label');
        hideBlock('new_contact_div');
    } else {
        showBlock('contact_label');
        showBlock('contact_div');
    }
}

function ajaxListProperties() {
    new Ajax.Autocompleter('search_string', 'autocomplete_choices', baseUrl+'/property/ajaxProperties', {minChars: 2, indicator: 'indicator1'});
}

function toggleNewContact() {
    var contactSelection = document.forms[0].contact_id.selectedIndex;	
	var contactId = document.forms[0].contact_id.options[contactSelection].value;
	
	if (contactId == 0) {
	    showBlock('new_contact_div');
        document.getElementById('new_contact').focus();
    } else {
        hideBlock('new_contact_div');
    }
}

function validateCorrespondenceForm() {
    var validateFlag = true;
    var details = document.getElementById('details').value;
    var propertyId = document.getElementById('property_id').value;
    var newContactField = document.getElementById('new_contact');
    
	var surname = document.correspondence_form.bidder_surname.value;
	var bidValue = document.correspondence_form.bid_value.value;
	
	if (validateFlag && productId == "-1") {
	    validateFlag = false;
	    alert("You must select a property.");
	    document.correspondence_form.property_id.focus();
	}
	if (validateFlag && details == "") {
	    validateFlag = false;
	    alert("You must summarise the correspondence.");
	    document.correspondence_form.details.focus();
	}
	if (newContactField.style.display == "block") {
    	if (validateFlag && newContactField.style == "") {
    	    validateFlag = false;
    	    alert("You must complete the new contact name.");
    	    document.correspondence_form.new_contact.focus();
    	}
	}
	if (validateFlag) {
	    document.correspondence_form.submit();
	}
}

function setPropertySelection() {
    var townSelection = document.forms[0].town_id.selectedIndex;	
	var townId = document.forms[0].town_id.options[townSelection].value;
	if (townId != -1) {
	    loadCorrespondenceProperties();
	    // Change property selection
	    origPropertyId = document.getElementById('orig_property_id').value;
	    if (origPropertyId != "") {
	        var propertyCount = document.forms[0].property_id.length;
	        for (x=0; x<propertyCount; x++) {
    			if (document.forms[0].property_id.options[x].value == origPropertyId) {
    				document.forms[0].property_id.options[x].selected = true;
    				break;
    			}
    		}
	    }
	    loadPropertyContacts();
	}	
}

function pause(ms) {
    var date = new Date();
    var curDate = null;
    do { 
        curDate = new Date();
    }
    while(curDate-date < ms);
}

function validateAppointmentForm() {
    townId = document.getElementById('town_id').value;
    if (document.getElementById('property_id') != null) {
        propertyId = document.getElementById('property_id').value;
    } else {
        propertyId = -1;
    }
    if (document.getElementById('new_contact') != null) {
        newContactField = document.getElementById('new_contact');
        newContact = newContactField.value;
        notes = document.getElementById('notes').value;
        
        if (newContact == "" && notes == "") {
            alert("You must provide either a contact name or some notes.");
            document.getElementById('notes').focus();
            return false;
        }
    }
	return true;
}

function orderBrochure() {
    brochureField = document.getElementById('brochure_order');
    brochureField.value = "true";
    document.edit_form.submit();
}

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.keywords.value = "";
}

function searchBidders() {
    propertyId = document.getElementById('property_id').value;
    destUrl = baseUrl + "/property/searchBidders/"+propertyId;
	newWindow = window.open(destUrl, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=500, height=300, left=150, top=150');
}

function addBidderToProperty() {
    document.search_form.search.value = "false";
    document.search_form.save.value = "true";
	document.search_form.submit();	
}

function mainImage() {
	document.edit_form.action = baseUrl+"/property/mainImage";
	document.edit_form.submit();	
}

function addBrochure() {
    filename = document.getElementById('userfile');
    if (filename.value != "") {
        document.edit_form.action = baseUrl+"/property/addBrochure";
        document.edit_form.submit();
    } else {
        alert('You must specify a filename.');
    }
}

function deleteBrochure() {
    if (confirm("Are you sure you want to delete this brochure?")) {
        document.edit_form.action = baseUrl+"/property/deleteBrochure";
        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 resetMatch() {
    document.match_form.town_id.value = -1;
    document.match_form.type_id.value = -1;
    document.match_form.sector_id.value = -1;
    document.match_form.bedrooms.value = -1;
    document.match_form.online.value = -1;
    document.match_form.saleterms_id.value = -1;
    document.match_form.price.value = "";
    document.match_form.save.value = "false";
    document.match_form.submit();
}

function resetPreferences() {
	document.prefs_form.town_id.value = -1;
    document.prefs_form.type_id.value = -1;
    document.prefs_form.sector_id.value = 1;
    document.prefs_form.bedrooms.value = -1;
    document.prefs_form.saleterms_id.value = 1;
	document.prefs_form.min_price.value = "";
	document.prefs_form.max_price.value = "";
}


/*
 * @deprecated
 *
function changeDisplaySettings() {    
    accessLevel = document.getElementById('access_level').value;
    if (accessLevel == 0) {
        document.getElementById('consentValues').style.display = 'block';
        document.getElementById('preferences').style.display = 'block';
        document.getElementById('favourites').style.display = 'block';
    } else {
        document.getElementById('consentValues').style.display = 'none';
        document.getElementById('preferences').style.display = 'none';
        document.getElementById('favourites').style.display = 'none';
    }

}
 */

function setFeaturedStatus(propertyId) {
    var featureCheckBox = document.getElementById('check_' + propertyId);
    document.main_form.property_id.value = propertyId;

    if (featureCheckBox.checked) {
        document.main_form.featured_status.value = 1;
    } else {
        document.main_form.featured_status.value = 0;
    }
    new Ajax.Updater('property_div', baseUrl+'/property/setFeatured', {
        method:'post', postBody:'property_id='+ $F('property_id') +'&featured='+$F('featured_status')
    });
}

function toggleMatchFields(matchType) {
    if (matchType == "U" || matchType == "") {
        showBlock('subscriberText');
        showBlock('subscriberField');
        hideBlock('exclude');
        hideBlock('excludeFields');

    } else {
        hideBlock('subscriberText');
        hideBlock('subscriberField');
        showBlock('exclude');
        showBlock('excludeFields');
    }
}

function userMatch() {
	document.details_form.action = baseUrl+"/user/match";
	document.details_form.match.value = "true";
	document.details_form.submit();	
}


function performMatch() {
	document.match_form.action = baseUrl+"/user/match";
	document.match_form.report.value = "false";
	document.match_form.target = "_self";
	document.match_form.submit();	
}

function matchReport() {
	document.match_form.action = baseUrl+"/user/match";
	document.match_form.report.value = "true";
	document.match_form.target = "_blank";
	document.match_form.submit();	
}


function viewPeriodStats(period) {
    document.edit_form.period.value = period;
    document.edit_form.submit();
}

function toggleNewProperties() {
    fieldValue = getStyle(document.getElementById('new_properties'), "display");
    if (fieldValue == "none") {
        document.getElementById('show_new_properties').innerHTML = "Hide";
        showBlock('new_properties');
    } else {
        document.getElementById('show_new_properties').innerHTML = "Show";
        hideBlock('new_properties');
    }
}

function toggleNewUsers() {
    fieldValue = getStyle(document.getElementById('new_users'), "display");
    if (fieldValue == "none") {
        document.getElementById('show_new_users').innerHTML = "Hide";
        showBlock('new_users');
    } else {
        document.getElementById('show_new_users').innerHTML = "Show";
        hideBlock('new_users');
    }
}

function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function arrangeViewing(id) {
	  newWindow = window.open(baseUrl+'/home/arrangeViewing/'+id, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=350, height=230, left=150, top=150');
}

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 tellFriend(id) {
	  newWindow = window.open(baseUrl+'/home/tellFriend/'+id, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=350, height=230, left=150, top=150');
}

function toggleCorrespondencePane(type) {
    if (type == "template") {
        showBlock('template');
        showBlock('generate_button');
        hideBlock('free_text');
    } else {
        showBlock('free_text');
        hideBlock('template');
        hideBlock('generate_button');
    }
}

function generateDoc() {
    templateId = document.getElementById('template_id').value;
    if (templateId == 0) {
        alert('Please select a template');
        return;
    }
    document.correspondence_form.action = baseUrl+"/correspondence/generateDocument";
    document.correspondence_form.submit();
}

function addProgressNote() {
    if (document.edit_form.progress_notes.value != "") {
        newAction = baseUrl+"/property/addProgressNote";
        document.edit_form.action = newAction;
        document.edit_form.submit();
    } else {
        alert("You must provide a note");
        document.edit_form.progress_notes.focus();
        return false;
    }
}

function confirmDeleteProgress(noteId) {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		document.edit_form.note_id.value = noteId;
		document.edit_form.action = baseUrl+"/property/deleteProgressNote"
		return true;
	} else {
	    return false;
	}
}

function toggleAppointmentFields(type) {
    var typeSelection = document.forms[0].diary_event_id.selectedIndex;	
	var typeId = document.forms[0].diary_event_id.options[typeSelection].value;
	
    if (typeId == 2) {
        hideBlock('existing_property_selection');
        hideBlock('contact_label');
        hideBlock('contact_div');
        hideBlock('new_contact_label');
        hideBlock('new_contact_div');
        showBlock('new_property');
        document.forms[0].address1.focus();
    } else {
        showBlock('existing_property_selection');
        showBlock('new_contact_label');
        showBlock('new_contact_div');
        hideBlock('new_property');
    }
}

function confirmDeleteRequest() {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		document.diary_form.action = baseUrl+"/diary/deleteRequest"
		document.diary_form.submit();
	} else {
	    return;
	}
}

function confirmDeleteDiary() {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		document.diary_form.action = baseUrl+"/diary/deleteDiary"
		document.diary_form.submit();
	} else {
	    return;
	}
}

function createValuation() {
    document.diary_form.action = baseUrl+"/property/createValuation";
    document.diary_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, currencySymbol) {
	numericPrice = removeChar(price, ',');
	numericPrice = removeChar(numericPrice, currencySymbol);
	newWindow = window.open(baseUrl+'/home/calculateMortgage/'+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 validateVendorView() {
    var propertySelection = document.vendor_form.property_id.selectedIndex;	
	var propertyId = document.vendor_form.property_id.options[propertySelection].value;
	
	if (propertyId == -1) {
	    alert("You must select a property.");
	    return false;
	} else {
	    return true;
	}
}
function toggleBidPersonalDetails(state) {
    if (state == 1) {
        showBlock('tr_hide_personal');
        hideBlock('tr_show_personal');
        
        showBlock('tr_address1');
        showBlock('tr_address2');
        showBlock('tr_town');
        showBlock('tr_county');
        showBlock('tr_postcode');
        showBlock('tr_phone');
        showBlock('tr_mobile');
        showBlock('tr_email');
        
        document.getElementById('show_personal').value = "Y";
    } else {
        showBlock('tr_show_personal');
        hideBlock('tr_hide_personal');
        
        hideBlock('tr_address1');
        hideBlock('tr_address2');
        hideBlock('tr_town');
        hideBlock('tr_county');
        hideBlock('tr_postcode');
        hideBlock('tr_phone');
        hideBlock('tr_mobile');
        hideBlock('tr_email');
        
        document.getElementById('show_personal').value = "N";
    }
}

function toggleBidSolicitorDetails(state) {
    if (state == 1) {
        showBlock('tr_hide_solicitor');
        showBlock('tr_new_solicitor');
        hideBlock('tr_show_solicitor');
        
        // Enable solicitor selection
        document.getElementById('solicitor_id').disabled = false;
        
        document.getElementById('show_solicitor').value = "Y";
    } else {
        showBlock('tr_show_solicitor');
        hideBlock('tr_new_solicitor');
        hideBlock('tr_hide_solicitor');
        
        hideBlock('tr_solicitor_company');
        hideBlock('tr_solicitor_address1');
        hideBlock('tr_solicitor_address2');
        hideBlock('tr_solicitor_town');
        hideBlock('tr_solicitor_county');
        hideBlock('tr_solicitor_postcode');
        hideBlock('tr_solicitor_phone');
        hideBlock('tr_solicitor_mobile');
        hideBlock('tr_solicitor_email');
        hideBlock('tr_add_solicitor');
        
        hideBlock('solicitor_result');
        hideBlock('tr_solicitor_contact');
        hideBlock('tr_add_solicitor_contact');
        
        document.getElementById('show_solicitor').value = "N";
    }
}

function addSolicitor() {
    hideBlock('solicitor_result');
    
    showBlock('tr_solicitor_company');
    showBlock('tr_solicitor_address1');
    showBlock('tr_solicitor_address2');
    showBlock('tr_solicitor_town');
    showBlock('tr_solicitor_county');
    showBlock('tr_solicitor_postcode');
    showBlock('tr_solicitor_phone');
    showBlock('tr_solicitor_mobile');
    showBlock('tr_solicitor_email');
    showBlock('tr_add_solicitor');
    
    hideBlock('tr_solicitor_contact');
    hideBlock('tr_add_solicitor_contact');
    
    document.getElementById('solicitor_company').focus();
    
    // Disable solicitor selection
    document.getElementById('solicitor_id').disabled = true;
    hideBlock('solicitor_contacts_div');
}

function cancelAddSolicitor() {
    hideBlock('solicitor_result');
    
    showBlock('tr_show_solicitor');
    hideBlock('tr_hide_solicitor');
    
    hideBlock('tr_solicitor_company');
    hideBlock('tr_solicitor_address1');
    hideBlock('tr_solicitor_address2');
    hideBlock('tr_solicitor_town');
    hideBlock('tr_solicitor_county');
    hideBlock('tr_solicitor_postcode');
    hideBlock('tr_solicitor_phone');
    hideBlock('tr_solicitor_mobile');
    hideBlock('tr_solicitor_email');
    hideBlock('tr_add_solicitor');
    
    document.getElementById('show_solicitor').value = "Y";
    
    // Disable solicitor selection
    document.getElementById('solicitor_id').disabled = false;
    loadSolicitorContacts();
}

function loadSolicitorContacts() {
    hideBlock('solicitor_result');
    showBlock('solicitor_contacts_div');
    
    if (document.getElementById('solicitor_id') != -1) {
        new Ajax.Updater('solicitor_contacts_div', baseUrl+'/property/ajaxSolicitorContacts', { 
            method:'post', postBody:'solicitor_id='+$F('solicitor_id')+'&bidder_id='+$F('bidder_id')
        });
    }
}

function addSolicitorContact() {
    hideBlock('solicitor_result');
    
    showBlock('tr_solicitor_contact');
    showBlock('tr_add_solicitor_contact');
    
    document.getElementById('solicitor_contact').focus();
    
    // Disable solicitor contact selection
    document.getElementById('solicitor_contact_id').disabled = true;
}

function cancelAddSolicitorContact() {
    hideBlock('solicitor_result');
    
    hideBlock('tr_solicitor_contact');
    hideBlock('tr_add_solicitor_contact');
    
    document.getElementById('show_solicitor').value = "Y";
    
    // Disable solicitor selection
    document.getElementById('solicitor_contact_id').disabled = false;
}

function saveSolicitor() {
    companyName = document.getElementById('solicitor_company').value;
    
    if (companyName == "") {
        alert("You must provide a company name.");
        document.getElementById('solicitor_company').focus();
        return;
    } else {
        new Ajax.Updater('solicitor_result', baseUrl+'/property/ajaxAddSolicitor', { 
            method:'post', postBody:'solicitor_company='+$F('solicitor_company')+'&solicitor_address1='+$F('solicitor_address1')+'&solicitor_address2='+$F('solicitor_address2')+'&solicitor_town='+$F('solicitor_town')+'&solicitor_county='+$F('solicitor_county')+'&solicitor_postcode='+$F('solicitor_postcode')+'&solicitor_phone='+$F('solicitor_phone')+'&solicitor_mobile='+$F('solicitor_mobile')+'&solicitor_email='+$F('solicitor_email')
        });
        
        showBlock('solicitor_result');
        hideBlock('tr_solicitor_company');
        hideBlock('tr_solicitor_address1');
        hideBlock('tr_solicitor_address2');
        hideBlock('tr_solicitor_town');
        hideBlock('tr_solicitor_county');
        hideBlock('tr_solicitor_postcode');
        hideBlock('tr_solicitor_phone');
        hideBlock('tr_solicitor_mobile');
        hideBlock('tr_solicitor_email');
        hideBlock('tr_add_solicitor');
        document.getElementById('solicitor_contact').value = "";
        
        // Update solicitor list
        /*new Ajax.Updater('solicitor_contacts_div', baseUrl+'/property/ajaxSolicitorContacts', { 
            method:'post', postBody:'solicitor_id='+$F('solicitor_id')
        });*/
    }
}

function saveSolicitorContact() {
    contactName = document.getElementById('solicitor_contact').value;
    
    if (contactName == "") {
        alert("You must provide a contact name.");
        document.getElementById('solicitor_contact').focus();
        return;
    } else {
        new Ajax.Updater('solicitor_result', baseUrl+'/property/ajaxAddSolicitorContact', { 
            method:'post', postBody:'solicitor_contact='+$F('solicitor_contact')+'&solicitor_id='+$F('solicitor_id')
        });
        
        showBlock('solicitor_result');
        hideBlock('tr_solicitor_contact');
        hideBlock('tr_add_solicitor_contact');
        document.getElementById('solicitor_contact').value = "";
        
        new Ajax.Updater('solicitor_contacts_div', baseUrl+'/property/ajaxSolicitorContacts', { 
            method:'post', postBody:'solicitor_id='+$F('solicitor_id')
        });
    }
}

function togglePriceRange() {
    toggleField = document.match_form.price_range;
    if (toggleField.checked) {
        showBlock('price_range_div');
    } else {
        hideBlock('price_range_div');
    }
}

/*
 * Calls unlock record record function to remove lock from record and re-assigns lock to current
 * record editor.
 */
function unlockRecord(){
	document.edit_form.save.value = "false"; 
	document.edit_form.action = baseUrl+"/property/unlockRecord";
	document.edit_form.submit();
}

