/*************************************************************
     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
/*************************************************************/ 

//*************************
//    QUICK-SEARCH FUNCTIONS
//*************************

var dimDelay;

//Prepares the highlights for each of the Quick Search form inputs and relevant labels.
function prepareQSInputs() {
	var qsFieldset = document.getElementById("qs_fieldset"); //Get the relevant fieldset, and relevant inputs.
	var qsInputs = qsFieldset.getElementsByTagName("input");
	var salesLabel = document.getElementById("qsSalesLabel");
	var lettingsLabel = document.getElementById("qsLettingsLabel");
	
	for (var i=0; i<qsInputs.length; i++){
		clearInput(qsInputs[i]); //If an input is already filled on page load, clear the value (looks neater). 
		if(qsInputs[i].id == "qs_sales"||qsInputs[i].id == "qs_lettings"){ //Ignore the Sales / Lettings Inputs.
			qsInputs[i].onclick = function() {
				if(undefined != window.dimDelay){clearTimeout(dimDelay);}
				if(this.id == "qs_sales"){
					lettingsLabel.style.color = "#C0C0C0";
					salesLabel.style.color = "#02543D";
					document.getElementById("fid").value = "11560";
					document.getElementById("bid").value = "1";
					document.getElementById("dbt").value = "1";
				}
				else if(this.id == "qs_lettings"){
					salesLabel.style.color = "#C0C0C0";
					lettingsLabel.style.color = "#02543D";
					document.getElementById("fid").value = "17560";
					document.getElementById("bid").value = "0";
					document.getElementById("dbt").value = "2";
				}
				highlightGoBtn("activate");
				dimDelay = setTimeout('highlightGoBtn("deactivate");', 3000);
			}
			continue;
		} 		
		qsInputs[i].onmouseover = function() {
			clearTimeout(dimDelay);
			if(this.id != "go_btn"){highlight("activate", this);}
			highlightGoBtn("activate");
		}
		qsInputs[i].onmouseout = function() {
			if((this.hasFocus != true)&&(!isFilled(this))){
				if(this.id != "go_btn"){highlight("deactivate", this);}
				dimDelay = setTimeout('highlightGoBtn("deactivate");', 3000);
			}
		}
		qsInputs[i].onfocus = function() {
			this.hasFocus = true;
			if(this.id != "go_btn"){highlight("activate", this);}
			highlightGoBtn("activate");
		}
		qsInputs[i].onblur = function() {
			if(!isFilled(this)){
				this.hasFocus = false;
				if(this.id != "go_btn"){highlight("deactivate", this);}
				highlightGoBtn("deactivate");
			}
		}
	}
}
addLoadEvent(prepareQSInputs);

function clearInput(input) {	
	if(isFilled(input)){
		input.value = "";
	}
}

//Highlights or unhighlights the individual elements of the Quick Search form.
function highlight(action, element){
	if(action=="activate"){
		element.className="on_state";
		var qsLabel = element.parentNode;
		qsLabel.style.color = "#02543D";
	}
	if(action=="deactivate"){
		element.className="";
		var qsLabel = element.parentNode;
		qsLabel.style.color = "#C0C0C0";
	}
}

//Highlights or unhighlights the GO button according to the status of the form.
function highlightGoBtn(action){
	var goBtn = document.getElementById("go_btn");
	var goBtnLabel = document.getElementById("legend");
	
	var salesInput= document.getElementById("qs_sales");
	var lettingsInput = document.getElementById("qs_lettings");
	
	var salesLabel = document.getElementById("qsSalesLabel");
	var lettingsLabel = document.getElementById("qsLettingsLabel");
	
	if(action=="activate"){
		if(undefined != window.fadeDelay){
			for(var z=0; z<fadeDelay.length; z++){
				clearTimeout(fadeDelay[z]);
			}
		}
		if(salesInput.checked){
			salesLabel.style.color = "#02543D";
		}
		else if(lettingsInput.checked){
			lettingsLabel.style.color = "#02543D";
		}
		goBtnLabel.style.color = "#02543D";	
		goBtn.src = "http://www.gascoines.com/images/go_btn_on.gif";
	}
	if(action=="deactivate"){
		var qsFieldset = document.getElementById("qs_fieldset");
		var qsInputs = qsFieldset.getElementsByTagName("input");
		
		//Check each input to see whether they are filled, and only if none of them are filled, unhighlight the GO button.
		var counter = 0;
		for (var i=0; i<qsInputs.length; i++){
			if(qsInputs[i].id == "qs_sales" || qsInputs[i].id == "qs_lettings"){continue;}
			if(isFilled(qsInputs[i])||(qsInputs[i].hasFocus == true)){counter++}
		}
		if (counter == 0){
			if(salesInput.checked){
				changeColour(salesLabel.id, '02543d', 'c0c0c0', "fg", 10, 10000);
			}
			else if(lettingsInput.checked){
				changeColour(lettingsLabel.id, '02543d', 'c0c0c0', "fg", 10, 10000);
			}
			goBtn.src = "http://www.gascoines.com/images/go_btn.gif";
			changeColour(goBtnLabel.id, '02543d', 'c0c0c0', "fg", 10, 10000);
			counter = 0;
		}
	}
}

function validateQSForm(form) {
	var lop = document.getElementById("lop");
	if(lop.value.length < 1){
		lop.value = 0;
	}
	var hip = document.getElementById("hip");
	if(hip.value.length < 1){
		hip.value = 6000000;
	}
	var bed = document.getElementById("bed");
	if(bed.value.length < 1){
		bed.value = 1;
	}
	return true;
}