window.onload = function() {
	//initNav();
	setFormDefaults('searchSite');
}

// Initialise navigation by changing class names to modify presentation
function initNav() {
	if (document.getElementById && document.getElementsByTagName) {
		if (document.getElementById('navcontainer')) {
			els = document.getElementById('navcontainer').getElementsByTagName( 'div' );
			for( var i=0; i < els.length; i++ ){
				if (els[i].className.indexOf('subNav') > -1) {
					els[i].className = 'ddMenu';
				}
			}
		}
	}
}

// Highlight function used to highlight tab when hovering over dropdown menu
function highlight(id,state) {
	var nav,args=highlight.arguments;
	nav = document.getElementById('nav_' + id);
	if (nav && nav.className.indexOf('current') < 0) {
		if (args.length < 2 || state.length == 0) {
			nav.className = 'current';
		}
		else {
			nav.className = state;
		}
	}			
}

// Initialise form by checking each text field for a title element
// and using this to populate the field if no value is supplied
function setFormDefaults(id){
	if( document.getElementById && document.getElementsByTagName ){
		if( document.getElementById(id) ){
			var f = document.getElementById(id);
			var inputs = f.getElementsByTagName( 'input' );
			for( var i=0; i < inputs.length; i++ ){
				if (inputs[i].type.indexOf('text') > -1) {
					if (inputs[i].title) {
						// If field has no value then set value equal to title.	
						// Otherwise set title equal to value 
						if (inputs[i].value == '') {
							inputs[i].value = inputs[i].title;
						}
						else {
							inputs[i].title = inputs[i].value; 
						}

						inputs[i].onfocus = function() {
							return clearField(this);
						}
					}
				}
			}
			f.onsubmit = function() {
				return clearDefaults(this,inputs);
			}
		}
	}
}

function clearField(field) {
	if (field.value == field.title) field.value = '';
}

function clearDefaults(form) {
	var inputs = form.getElementsByTagName( 'input' );
    for( var i=0; i < inputs.length; i++ ){
		if (inputs[i].type.indexOf('text') > -1) {
			clearField(inputs[i]);
		}
	}
}
