/*************************************************************
     TEMPLATE BASED WEBSITE DESIGNED AND DEVELOPED BY
           VEBRA GRAPHICS (VEBRA SOLUTIONS LTD.)
                      COPYRIGHT 2006

      AUTHOR: DAVID SWALLOW (david.swallow@vebra.com)
                        21/04/2006

                      www.vebra.info
/*************************************************************/ 

//*************************
//     FORM 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") 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;}
		if (i == 0) {
			thisform.onsubmit = function() {
				return validateQSForm(this);
			}
		} else {
			resetFields(thisform);
			thisform.onsubmit = function() {
				return validateForm(this);
			}
		}
	}
}
addLoadEvent(prepareForms);

function changePrices(minSales, maxSales, minLets, maxLets){
	document.getElementById ("minPrice_sales").className = minSales;
	document.getElementById ("maxPrice_sales").className = maxSales;
	document.getElementById ("minPrice_lets").className = minLets;
	document.getElementById ("maxPrice_lets").className = maxLets;
}

function prepChangePrices() {
	var allInputs = document.getElementsByTagName("input");
	for (var i = 0; i < allInputs.length; i++){
		if (allInputs[i].id == "enq_type_4" || allInputs[i].id == "enq_type_6"){
			allInputs[i].onfocus = function() {
				changePrices("invisible", "invisible", "visible", "visible");
			}
		}
		if (allInputs[i].id == "enq_type_3" || allInputs[i].id == "enq_type_5"){
			allInputs[i].onfocus = function() {
				changePrices("visible", "visible", "invisible", "invisible");
			}
		}
	}
}
addLoadEvent(prepChangePrices);