// the functions registry is a list of functions which will run when a form is inititialised
var jsFunctionsRegistry = new Array;

// as every page carries a search form we'll add the relevant function (defined on this page) here 
jsFunctionsRegistry['searchFields'] = 'searchFields';

// form defaults sets default values for sitewide forms
var formDefaults = new Array;
formDefaults['searchFields'] = 'Search Site'; // create the default value for search text

$(document).ready(function(){

	// on document load get a list of the forms and
	// then run initialiseForm(thisFormID) on each
	// any form can be initialised at any time

	var formsOnPage = document.getElementsByTagName("form");
	
 	for (i=0; i< formsOnPage.length; i++) {
 	 	var thisFormID = '#' + $(formsOnPage[i]).attr('id');
		initialiseForm(thisFormID);
 	}

});


function initialiseForm(thisFormID) {

	// clear actions to buttons and reapply
	$(thisFormID + ' .formSubmit').unbind('click');
	$(thisFormID + ' .formCancel').unbind('click');
	$(thisFormID + ' .formReset').unbind('click');
	
	$(thisFormID + ' .formSubmit').bind('click', function(e) {
		e.preventDefault();
		$(thisFormID).submit();
	});
	
	$(thisFormID + ' .formCancel').bind('click', function(e) {
		e.preventDefault();
		history.go(-1);
	});
	
	$(thisFormID + ' .formSubmit').bind('click', function(e) {
		e.preventDefault();
		$(thisFormID).reset();
	});
	
	
	// if any other functions have been added 
	// to the registry run them this form

	for (var i in jsFunctionsRegistry) {
		eval(jsFunctionsRegistry[i] + "('" + thisFormID + "')");
	}
		
}

						
/* ===== Search Forms Functions ===== */ 

function searchFields(thisFormID) {
	
	$(thisFormID + ' .searchText').unbind('blur');
	$(thisFormID + ' .searchText').unbind('focus');
	
	$(thisFormID + ' .searchText').each( function(i) {
		
		// apply the default text and grey out if it is the default
	
		var thisItem = $(this);
		
		if (thisItem.attr('value') == '') {
			thisItem.attr('value', formDefaults['searchFields']);
		} else {
			thisItem.css('color', '#000000');
		}
		
		// if the search box contains the default text, disable the 'Go' button
		if (thisItem.attr('value') == formDefaults['searchFields']) {
			thisItem.next('a').unbind('click');
			thisItem.css('color', '');
		} else {
			thisItem.next('a').bind('click', function(e){
				$(thisFormID).submit();
			});
		}
		
	});
	
	$(thisFormID + ' .searchText').bind('focus', function(e) {
		
		// clear the text if its the default and convert the string colour to black

		var thisItem = $(this);
		thisItem.css('color', '#000000');
		if (thisItem.attr('value') == formDefaults['searchFields']) thisItem.attr('value', '');
		
	});
	
	$(thisFormID + ' .searchText').bind('blur', function(e) {
		
		// if there's no text, reset to the default value and reset the colour
	
		var thisItem = $(this);
		var inputValue = trim(thisItem.attr('value'));
		
		if (inputValue == '') {
			thisItem.attr('value', formDefaults['searchFields']);
		}
		
		// if the search box contains the default text, disable the 'Go' button
		if (thisItem.attr('value') == formDefaults['searchFields']) {
			thisItem.next('a').unbind('click');
			thisItem.css('color', '');
		} else {
			thisItem.next('a').bind('click', function(e){
				$(thisFormID).submit();
			});
		}
		
		
	});
	
}

