/***
Filename:		searchformfunctions.js
Author:			Andrei Vais
Date:			12/04/2009
***/

//these must match the names given in the database
var searchForm = "search";
var searchFormTextField = "searchterm";

/*************************************
Initialise function. Called first.
**************************************/
function pageinit()
{	
	
	// Make sure our search field is styled correctly
	if (!document.getElementById) return false;
	if (!document.getElementById(searchForm)) return false;
	if (!document.getElementById(searchFormTextField)) return false;
	
	document.getElementById(searchForm).onsubmit = function() {return validateSearchForm(document.getElementById(searchForm));}
	
	fieldcheck(document.getElementById("searchterm"));
	document.getElementById(searchFormTextField).onfocus = function() {fieldfocus(document.getElementById(searchFormTextField));}
	document.getElementById(searchFormTextField).onblur = function() {fieldblur(document.getElementById(searchFormTextField));}

}

/*************************************
Clear and restore the form field on focus change
**************************************/
function fieldcheck( fld )
{
	//alert(fld.defaultValue);
	if (fld.value!=fld.defaultValue && fld.value!="")
	{
		fld.style.textAlign="left";
		fld.style.color="#000000";
	}
	else
	{
		fld.style.textAlign="left";
		fld.style.color="#CCCCCC";
		fld.value=fld.defaultValue;
	}
}

function fieldfocus( fld )
{
	if (fld.value==fld.defaultValue)
	{
		fld.value="";
		fld.style.textAlign="left";
		fld.style.color="#000000";
	}	
}

function fieldblur( fld )
{
	if (fld.value=="")
	{		
		fld.style.textAlign="left";
		fld.style.color="#CCCCCC";
		fld.value=fld.defaultValue;
	}	
}

/*************************************
Javascript form validation
**************************************/
function validateSearchForm( fld )
{
	//alert(fld.elements.item(0).value);
	if ( fld.elements[searchFormTextField].value=="" || fld.elements[searchFormTextField].value==fld.elements[searchFormTextField].defaultValue)
	{
		alert(fld.elements[searchFormTextField].defaultValue);
		return false;
	}
}

addLoadEvent(pageinit);