
//==============================================================================
// THIS SCRIPT CONTROLS THE HEADER AS FOLLOWS:
// - populates searchbox depending on checkbox checked
// - autoclears the searchbox
// - handles the predictive search (using out-of-the-box .NET AJAX code
// IT IS A COMBINATION OF NEW CODE AND THE PRE R4 (APRIL 2008) SCRIPT 'search.js'
//==============================================================================

//header script setup function
function fHeaderSetup()	{

	// initiate variables, including relevant keycodes
	//=========================================================================
	var UP = 38;
	var DOWN = 40;
	var ENTER = 13;
	var BACKSPACE = 8;
	var DELETE = 46;
	var ESCAPE = 27;					 	
	var FIRSTINDEX = 0;		
	var index = FIRSTINDEX - 1;						// this sets the initial selected term as none	
	var table = null;								// contains the PredS values
	var rows = null;								// for individual PredS values
	var selectedRow = null;							// selected row
	var wordChanged = false;						// records whether word has changed	
	
	// variables for time delay ajax call on predictive search
	var timeDelay = 800;
	var callfuncFlag = false;						
	var t;
	//
	var mainSearchBox = document.getElementById('IdSearchBoxContainer');
	var aMainSearch = mainSearchBox.getElementsByTagName('input');
	var nSearchLength = aMainSearch.length;
	// Looping through inputs to find a type="text" - assumption and should revisit for robustness
	for (nSearchCount = 0; nSearchCount < nSearchLength; nSearchCount++) {
		if (aMainSearch[nSearchCount].type == 'text') {
			var sMainSearchId = aMainSearch[nSearchCount].id;
		}
	}
	var mainSearch = document.getElementById(sMainSearchId);
	var theSearchForm = document.forms['gs'];
    
	// now the code itself
	//=========================================================================
	
	//treatment for population of the searchbox, firstly for straightforward focus
	var aSearchTerms = new Array("Enter a search term","Enter postcode, location, or practice name","Enter postcode, location, or hospital name","Enter postcode, location, or practice name");
	mainSearch.value=aSearchTerms[0]; //add default value
	if(mainSearch.setAttribute) mainSearch.setAttribute('autocomplete','off'); // turn off autocomplete
	fAddEvent(mainSearch, 'focus', function(){fSearchFocus('focus');});
	fAddEvent(mainSearch, 'blur', function(){fSearchFocus('blur');});
    
	function fSearchFocus(e)
	{	//handles the value switching when focus changes
		switch(e)
		{
			case 'focus':
				//if the text is just default text, remove it
				var nSearchTerms=aSearchTerms.length;
				for (nCount = 0; nCount < nSearchTerms ; nCount++ )
				{
					if (mainSearch.value==aSearchTerms[nCount])	{mainSearch.value=""; break;}					
				}
				break;
			case 'blur':
				//only reset if no text has been entered
				if (mainSearch.value=="")	
				{
					//check to see which radio button is checked
					mainSearch.value=aSearchTerms[0];
				}
				break;
		}
	}
		
	//to make the dropdown menu appear in full in FF when tabbing
	//this is done by giving a class 'menufocus' to the <a> tags that sit within the
	//menu <div>s, and not to the other ones
	var aDDLinks = document.getElementById('main-navigation').getElementsByTagName('a');
	var nDDLinksCount = aDDLinks.length;		
	for (nCount = 0; nCount < nDDLinksCount ; nCount++ ) {
		var oParentDiv=aDDLinks[nCount].parentNode.parentNode.parentNode;
		//ensure that the <a>s are only sub-links, not main menu links, which have the parent 'nav-bar'
		if (oParentDiv.id!='nav-bar')	{
			fAddEvent(aDDLinks[nCount], 'focus', fMenuGainFocus);
			fAddEvent(aDDLinks[nCount], 'blur', fMenuLoseFocus);
		}		
	}

	function fMenuGainFocus(e,oParentDiv)	{	//enables smooth non-IE menu tabbing
		var e = e || window.event;
		var oTargetElement = e.target || e.srcElement;
		oTargetElement.className += ' menufocus';
		oTargetElement.parentNode.parentNode.parentNode.className += ' menufocus';
	}
	function fMenuLoseFocus(e)	{	//see previous
		var e = e || window.event;
		var oTargetElement = e.target || e.srcElement;
		oTargetElement.className = oTargetElement.className.replace(/\bmenufocus\b/,'');
		oTargetElement.parentNode.parentNode.parentNode.className = oTargetElement.parentNode.parentNode.parentNode.className.replace(/\bmenufocus\b/,'');
	}	
}

fAddEvent(window, 'load', fHeaderSetup); //to run all header scripts

