/*************************************************************
     TEMPLATE BASED WEBSITE DESIGNED AND DEVELOPED BY
           VEBRA GRAPHICS (VEBRA SOLUTIONS LTD.)
                      COPYRIGHT 2006

      AUTHOR: DAVID SWALLOW (david.swallow@vebra.com)
                        13/06/2006

                      www.vebra.info
/*************************************************************/ 

//*************************
//     RE-USABLE FUNCTIONS
//*************************

//A means of adding multiple events to the onload event handler
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function clearChildNodes(element){
while (element.hasChildNodes())
	{
	  element.removeChild(element.firstChild);
	}
}

//*************************
//     SITE-SPECIFIC FUNCTIONS
//*************************

//Brings the associated form field into focus when the text within a label is clicked
function focusLabels() {
	if (!document.getElementsByTagName) return false;
	var labels = document.getElementsByTagName("label");
	for (var i=0; i<labels.length; i++) {
		if (!labels[i].getAttribute("for")) continue;
		labels[i].onclick = function() {
			var id = this.getAttribute("for");
			if (!document.getElementById(id)) return false;
			var element = document.getElementById(id);
			element.focus();
		}
	}
}
addLoadEvent(focusLabels);

//Creates default placeholder text, which disappears and reappears automatically
function resetFields(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {
		var element = whichform.elements[i];
		if (element.type == "reset"||element.type == "submit"||element.type == "radio"||element.type == "checkbox") continue;
		if (!element.defaultValue) continue;
		element.onfocus = function() {
			if (this.value == this.defaultValue) {
				this.value = "";
			}
		}
		element.onblur = function() {
			if (this.value == "") {
				this.value = this.defaultValue;
			}
		}
	}
}

//Determines whether the input field has been completed, based on whether the default still exists
function isFilled(field) {
	if (field.value.length < 1 || field.value == field.defaultValue) {
		return false;
	} else {
		return true;
	}
}

//Determines whether the email field has been completed correctly, based on the existence of certain characters
function isEmail(field) {
	if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1)
	{
		return false;
	} else {
		return true;
	}
}

//Draws upon the previous two functions to validate all form fields, based on whether the class "required" or "email" is present
function validateForm(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {
		var element = whichform.elements[i];
		if (element.className.indexOf("required") != -1) {
			if (!isFilled(element)) {
				alert("Please fill in your "+element.name+".");
				return false;
			}
		}
		if (element.className.indexOf("email") != -1) {
			if (!isEmail(element)) {
				alert("Please enter a valid e-mail address.");
				return false;
			}
		}
	}
	return true;
}

//The function to prepare the previous form enhancements for each form on a page
function prepareForms() {
	for (var i=0; i<document.forms.length; i++) {
		var thisform = document.forms[i];
		if (thisform.name == "searchform"){continue;}
		resetFields(thisform);
		thisform.onsubmit = function() {
			return validateForm(this);
		}
	}
}
addLoadEvent(prepareForms);

var sales_min = new Array(0, 25000, 50000, 75000, 100000, 125000, 150000, 175000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000, 3000000, 5000000, 6000000);
var sales_min_labels = new Array("£0", "£25,000", "£50,000", "£75,000", "£100,000", "£125,000", "£150,000", "£175,000", "£200,000", "£300,000", "£400,000", "£500,000", "£600,000", "£700,000", "£800,000", "£900,000", "£1,000,000", "£3,000,000", "£5,000,000", "£6,000,000");
var lets_min = new Array(0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 700, 900, 1000, 2000, 2500);
var lets_min_labels = new Array("£0 p.w.", "£50 p.w.", "£100 p.w.", "£150 p.w.", "£200 p.w.", "£250 p.w.", "£300 p.w.", "£350 p.w.", "£400 p.w.", "£450 p.w.", "£500 p.w.", "£700 p.w.", "£900 p.w.", "£1,000 p.w.", "£2,000 p.w.", "£2,500 p.w.");
var sales_max = new Array(25000, 50000, 75000, 100000, 125000, 150000, 175000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000, 3000000, 5000000, 6000000);
var sales_max_labels = new Array("£25,000", "£50,000", "£75,000", "£100,000", "£125,000", "£150,000", "£175,000", "£200,000", "£300,000", "£400,000", "£500,000", "£600,000", "£700,000", "£800,000", "£900,000", "£1,000,000", "£3,000,000", "£5,000,000", "£6,000,000+");
var lets_max = new Array(50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 700, 900, 1000, 2000, 2500);
var lets_max_labels = new Array("£50 p.w.", "£100 p.w.", "£150 p.w.", "£200 p.w.", "£250 p.w.", "£300 p.w.", "£350 p.w.", "£400 p.w.", "£450 p.w.", "£500 p.w.", "£700 p.w.", "£900 p.w.", "£1,000 p.w.", "£2,000 p.w.", "£2,500+ p.w.");

function changeSelect(type){
	var qs_minPrice =  document.getElementById("qs_minPrice");
	clearChildNodes(qs_minPrice);
	var qs_MaxPrice =  document.getElementById("qs_maxPrice");
	clearChildNodes(qs_MaxPrice);
	
	if(type == "sales"){
		minPriceArray = sales_min;
		minLabelsArray = sales_min_labels;
		maxPriceArray = sales_max;
		maxLabelsArray = sales_max_labels;
		for (var i=0; i < minPriceArray.length; i++){
			var option = document.createElement("option");
			option.value = minPriceArray[i];
			if(minPriceArray[i] == 50000){option.selected = "selected"}
			var option_label = document.createTextNode(minLabelsArray[i]);
			option.appendChild(option_label);
			qs_minPrice.appendChild(option);
		}
		for (var i=0; i < maxPriceArray.length; i++){
			var option = document.createElement("option");
			option.value = maxPriceArray[i];
			if(maxPriceArray[i] == 1000000){option.selected = "selected"}
			var option_label = document.createTextNode(maxLabelsArray[i]);
			option.appendChild(option_label);
			qs_MaxPrice.appendChild(option);
		}
	}
	if(type == "lets"){
		minPriceArray = lets_min;
		minLabelsArray = lets_min_labels;
		maxPriceArray = lets_max;
		maxLabelsArray = lets_max_labels;
		for (var i=0; i < minPriceArray.length; i++){
			var option = document.createElement("option");
			option.value = minPriceArray[i];
			if(minPriceArray[i] == 100){option.selected = "selected"}
			var option_label = document.createTextNode(minLabelsArray[i]);
			option.appendChild(option_label);
			qs_minPrice.appendChild(option);
		}
		for (var i=0; i < maxPriceArray.length; i++){
			var option = document.createElement("option");
			option.value = maxPriceArray[i];
			if(maxPriceArray[i] == 2500){option.selected = "selected"}
			var option_label = document.createTextNode(maxLabelsArray[i]);
			option.appendChild(option_label);
			qs_MaxPrice.appendChild(option);
		}
	}
}

function insertSelect(){
	var qs_container =  document.getElementById("qs_container");
	var qsInputs = qs_container.getElementsByTagName("input");
	for (var i=0; i<qsInputs.length; i++){
		if(isFilled(qsInputs[i])){
			qsInputs[i].value = "";
		}		
	}
	var qs_sales_btn =  document.getElementById("qs_sales_btn");
	qs_sales_btn.onclick = function() {
		document.getElementById("dbt").value = "1";
		var salesMin = changeSelect("sales");
	}
	var qs_lets_btn =  document.getElementById("qs_lets_btn");
	qs_lets_btn.onclick = function() {
		document.getElementById("dbt").value = "2";
		var letsMin = changeSelect("lets");
	}
}
addLoadEvent(insertSelect);		

//Adjusts navigation highlight and page title on Lettings Search due to limitations of Vebra software
function nav_highlight(){
	if (document.getElementById){
		var prop_link = document.getElementById("property_search");
		var lets_link = document.getElementById("lettings_search");
		var page_title = document.getElementById("page_title").firstChild;
		prop_link.setAttribute("class","");
		prop_link.setAttribute("className", "")
		lets_link.setAttribute("class","current");
		lets_link.setAttribute("className","current");
		page_title.data = "Renting";
	}
}
if(location.href == "http://www.vebra.com/home/quick/PFselect.asp?firmid=1099&branchid=1&dbtype=2&norefine=1"){
	//addLoadEvent(nav_highlight);
}