
//==============================================================================
// 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');});
    
	//secondly when changing search opt
//	var aRadios = document.getElementById('search-options').getElementsByTagName('input');
//	var nRadiosCount = aRadios.length;
//	for (nCount = 0; nCount < nRadiosCount; nCount++) { 
//		var sRadioId = aRadios[nCount].id.toLowerCase();
//		if (sRadioId.match('site')) {
//			var sSiteRadioId = aRadios[nCount].id;
//		} else if(sRadioId.match('gp')) {
//			var sGpRadioId = aRadios[nCount].id;
//		} else if(sRadioId.match('hospital')) {
//			var sHospitalRadioId = aRadios[nCount].id;
//		} else if(sRadioId.match('dentist')) {
//			var sDentistRadioId = aRadios[nCount].id;
//		}
//		fAddEvent(aRadios[nCount], 'click', fRadioCheck);
//	}
		
	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/,'');
	}	

	// predictive search code
	//=========================================================================

	//attach events to check the word for changes, check for typing and check for search matches
	fAddEvent(mainSearch, 'keydown', fOnControlKeyDown);
//	fAddEvent(mainSearch, 'keydown', fPredSearchShow); 
	fAddEvent(mainSearch, 'keyup', fGetPredictiveTerms);
	//fAddEvent(mainSearch, 'keypress', fCheckWord);

/*	function fCheckWord(e)	{	//looks to see if the word had changed before anything else is done
		var keynum;		
		keynum = e.keyCode || e.which;	//any browser
		if	(keynum>=32 && keynum<=126)	{	//any 'letter' key
			wordChanged=true;
		}
	}*/
	
	function fOnControlKeyDown(e)	{	//checks which key has been pressed and fires relevant function
		var keynum;		
		keynum = e.keyCode || e.which;	//any browser		
		switch(keynum)	{
			case DOWN:
				fMoveCursor('down');	//selects next row down
				break;			
			case UP:
				fMoveCursor('up');	//selects next row up
				break;			
			case ENTER:
				if(selectedRow)	{
					fSetSelectedValue(selectedRow.getElementsByTagName('a')[0].innerHTML);	//sets searchbox value to selected value
					selectedRow=null;
				}
				break;				
			case ESCAPE:
				fPredSearchShow("");	//closes box
				break;
			default:
				if	(keynum>=32 && keynum<=126)	{	//any 'letter' key
					wordChanged=true;
				}
		}
	}
	function fMoveCursor(direction)	{	//moves the selected row up or down accordingly
		selectedRow = null; //deselects any row
		table = document.getElementById("pred-terms");	//assign the <UL> to table		
		if	(table != null)	{	//if the above was a success	
			rows = table.getElementsByTagName("li");	//put all links into an array		
			switch(direction)	{
				case 'down':
					if(rows.length > 0 && index < rows.length -1)	{	//if there are links AND the last one ISN'T selected
						index++;	//increase index
						for(var i = FIRSTINDEX; i < rows.length; i++)	{	//strip class off all rows
							rows[i].className = '';
						}
						selectedRow = rows[index];	//set selected row
						selectedRow.className = 'selected';	//add class to indicate it
					}
					break;
				case 'up':
					if(index > FIRSTINDEX)	{	//if the first one ISN'T selected
						index--;	//decrease index
						for(var i = FIRSTINDEX; i < rows.length; i++)	{	//strip class off all rows
							rows[i].className = '';
						}
						selectedRow = rows[index]; //set selected row
						selectedRow.className = 'selected';	//add class to indicate it
					}
					break;
			}			
		}
	}
	function fSetSelectedValue(selectedValue)	{	//sets value of searchbox to selected term
		if(selectedValue != null)	{	//if there is a value
			selectedValue = selectedValue.replace(/^\s+|\s+$/g, '');	//cuts out rubbish			
			if(selectedValue != '')	{	//if the value isn't nothing
				mainSearch.value = selectedValue;	//set value of searchbox
				index = FIRSTINDEX;	//reset index
				fPredSearchShow("");	//closes box
			}
		}		
		return false;
	}
	function fClickedOption(e)	{
		var oTarget = e.srcElement || e.target;
		if (oTarget.tagName=='A')	{
			mainSearch.value = oTarget.innerHTML;
			fPredSearchShow("");	//closes box
			theSearchForm.submit();
		}		
	}
	function timedCount() {
		if(callfuncFlag && wordChanged) {
			wordChanged = false;	//reset var
			clearTimeout(t);
			fCallServer(mainSearch.value.replace(/^\s\s*/, '').replace(/\s\s*$/, ''), '');}
		else {
			wordChanged = true;
			callfuncFlag = true;
			t=setTimeout(timedCount,timeDelay);}
	}
	function fGetPredictiveTerms(e) {
		var keynum;
		clearTimeout(t);
		callfuncFlag = false;
		keynum = e.keyCode || e.which;	//any browser
		if ((wordChanged || keynum == BACKSPACE || keynum == DELETE) && (keynum != UP && keynum != DOWN))	{	//if the word has changed
			timedCount();}
	}
	function fCallServer(arg, context)	{	//initiates AJAX 
		index = FIRSTINDEX - 1;	//reset index
		if(arg != "")	{
			//the following two vars preserve the original action and method
			var formAction = theSearchForm.action;
			var formMethod = theSearchForm.method;			
			//the following two lines redirect the form to a temp page which loads v quickly
			theSearchForm.action = "/Pages/GetPredictiveTerms.aspx";
			theSearchForm.method = "POST";			
			fSearchWebForm_DoCallback("getpredictivesearchterms", arg, fPredSearchShow, context, null, false); //call AJAX
			//the following two lines return the original action and method
			theSearchForm.action = formAction;
			theSearchForm.method = formMethod;
		} else	{
			fPredSearchShow("");
		}
	}
	function fPredSearchShow(response)	{	//constructs the predictive text box and displays/hides it accordingly
		var sPredCode = response;
		if (sPredCode != "")	{	//if there has been a server response
			if (document.getElementById('results') != null)	{
				mainSearchBox.removeChild(document.getElementById('results'));	//remove the box, if it exists	
			}
			if (IE6) {
			    // Calculate offset for IE6 - zoom mode means not needed for other browsers.
		        var nInputHeight = document.getElementById('q').offsetHeight;
		        var nInputLeft = document.getElementById('q').offsetLeft;
		        var nContainerHeight = document.getElementById('IdSearchBoxContainer').offsetHeight;
		        var sTop = ((nContainerHeight - nInputHeight) +5) + 'px';
			      var sLeft = nInputLeft + 'px';
			}	
			//make it appear!
			//create container elements
			var oResults=document.createElement("div");
			oResults.id="results";
			//if (IE6) {
			//    oResults.style.left=sLeft;
			//    oResults.style.top=sTop;
			//}
			var oPar = {type:'p'};
			oPar = fCreateElement(oPar);
			var oLink = {type:'a', id:'hide-link', title:'hide search suggestions'};
			oLink = fCreateElement(oLink);
			var sText = document.createTextNode('hide');
			oLink.appendChild(sText);
			var oSpan = {type:'span', className:'hidden'};
			oSpan = fCreateElement(oSpan);
			sText = document.createTextNode('search suggestions');
			oSpan.appendChild(sText);
			oLink.appendChild(oSpan);
			sText = document.createTextNode(' [x]');
			oLink.appendChild(sText);
			oPar.appendChild(oLink);
			oList = document.createElement("ul");
			oList.id = "pred-terms";
			oList.innerHTML = sPredCode;
			//construct object */
			oResults.appendChild(oPar);
			oResults.appendChild(oList);
			//add to page */
			mainSearchBox.appendChild(oResults);
			//add ability to click on link and add to input box			
			fAddEvent(oList, 'click', fClickedOption);
			//add ability to remove it
	        fAddEvent(oLink, 'click', function(){fRemoveElement('IdSearchBoxContainer', 'results');});	
		} else	{ //if no server response, or other exit call, close box
			if (document.getElementById('results') != null)	{
				fRemoveElement('IdSearchBoxContainer', 'results');	//remove the box	
			}
		}
	}

	// .NET AJAX code
	//=========================================================================

	function SearchWebForm_PostBackOptions(eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit) {
		this.eventTarget = eventTarget;
		this.eventArgument = eventArgument;
		this.validation = validation;
		this.validationGroup = validationGroup;
		this.actionUrl = actionUrl;
		this.trackFocus = trackFocus;
		this.clientSubmit = clientSubmit;
	}

	function SearchWebForm_DoPostBackWithOptions(options) {
		var validationResult = true;
		if (options.validation) {
			if (typeof(Page_ClientValidate) == 'function') {
				validationResult = Page_ClientValidate(options.validationGroup);
			}
		}
		if (validationResult) {
			if ((typeof(options.actionUrl) != "undefined") && (options.actionUrl != null) && (options.actionUrl.length > 0)) {
				theSearchForm.action = options.actionUrl;
			}
			if (options.trackFocus) {
				var lastFocus = theSearchForm.elements["__LASTFOCUS"];
				if ((typeof(lastFocus) != "undefined") && (lastFocus != null)) {
					if (typeof(document.activeElement) == "undefined") {
						lastFocus.value = options.eventTarget;
					}
					else {
						var active = document.activeElement;
						if ((typeof(active) != "undefined") && (active != null)) {
							if ((typeof(active.id) != "undefined") && (active.id != null) && (active.id.length > 0)) {
								lastFocus.value = active.id;
							}
							else if (typeof(active.name) != "undefined") {
								lastFocus.value = active.name;
							}
						}
					}
				}
			}
		}
		if (options.clientSubmit) {
			__doPostBack(options.eventTarget, options.eventArgument);
		}
	}

	var __searchPendingCallbacks = new Array();
	var __searchSynchronousCallBackIndex = -1;

	function fSearchWebForm_DoCallback(eventTarget, eventArgument, eventCallback, context, errorCallback, useAsync) {
		var postData = __searchTheFormPostData +
			"__CALLBACKID=" + SearchWebForm_EncodeCallback(eventTarget) +
			"&__CALLBACKPARAM=" + SearchWebForm_EncodeCallback(eventArgument);
		
		if (theSearchForm["__EVENTVALIDATION"]) {
			postData += "&__EVENTVALIDATION=" + SearchWebForm_EncodeCallback(theSearchForm["__EVENTVALIDATION"].value);
		}
		var xmlRequest,e;
		try {
			xmlRequest = new XMLHttpRequest();
		}
		catch(e) {
			try {
				xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
			}
		}
		var setRequestHeaderMethodExists = true;
		try {
			setRequestHeaderMethodExists = (xmlRequest && xmlRequest.setRequestHeader);
		}
		catch(e) {}
		var callback = new Object();
		callback.eventCallback = eventCallback;
		callback.context = context;
		callback.errorCallback = errorCallback;
		callback.async = useAsync;
		var callbackIndex = SearchWebForm_FillFirstAvailableSlot(__searchPendingCallbacks, callback);

		if (!useAsync) {
			if (__searchSynchronousCallBackIndex != -1) {
				__searchPendingCallbacks[__searchSynchronousCallBackIndex] = null;
			}
			__searchSynchronousCallBackIndex = callbackIndex;
		}
		if (setRequestHeaderMethodExists) {
			xmlRequest.onreadystatechange = SearchWebForm_CallbackComplete;
			callback.xmlRequest = xmlRequest;
			xmlRequest.open("POST", theSearchForm.action, true);
			xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlRequest.send(postData);
			return;
		}

		callback.xmlRequest = new Object();
		var callbackFrameID = "__CALLBACKFRAME" + callbackIndex;
		var xmlRequestFrame = document.frames[callbackFrameID];
		if (!xmlRequestFrame) {
			xmlRequestFrame = document.createElement("IFRAME");
			xmlRequestFrame.width = "1";
			xmlRequestFrame.height = "1";
			xmlRequestFrame.frameBorder = "0";
			xmlRequestFrame.id = callbackFrameID;
			xmlRequestFrame.name = callbackFrameID;
			xmlRequestFrame.style.position = "absolute";
			xmlRequestFrame.style.top = "-100px"
			xmlRequestFrame.style.left = "-100px";
			try {
				if (callBackFrameUrl) {
					xmlRequestFrame.src = callBackFrameUrl;
				}
			}
			catch(e) {}
			document.body.appendChild(xmlRequestFrame);
		}
		var interval = window.setInterval(function() {
			xmlRequestFrame = document.frames[callbackFrameID];
			if (xmlRequestFrame && xmlRequestFrame.document) {
				window.clearInterval(interval);
				xmlRequestFrame.document.write("");
				xmlRequestFrame.document.close();
				xmlRequestFrame.document.write('<html><body><div><form method="post"><input type="hidden" name="__CALLBACKLOADSCRIPT" value="t"></form></div></body></html>');
				xmlRequestFrame.document.close();
				xmlRequestFrame.document.forms[0].action = theSearchForm.action;
				var count = __seatchTheFormPostCollection.length;
				var element;
				for (var i = 0; i < count; i++) {
					element = __seatchTheFormPostCollection[i];
					if (element) {
						var fieldElement = xmlRequestFrame.document.createElement("INPUT");
						fieldElement.type = "hidden";
						fieldElement.name = element.name;
						fieldElement.value = element.value;
						xmlRequestFrame.document.forms[0].appendChild(fieldElement);
					}
				}
				var callbackIdFieldElement = xmlRequestFrame.document.createElement("INPUT");
				callbackIdFieldElement.type = "hidden";
				callbackIdFieldElement.name = "__CALLBACKID";
				callbackIdFieldElement.value = eventTarget;
				xmlRequestFrame.document.forms[0].appendChild(callbackIdFieldElement);
				var callbackParamFieldElement = xmlRequestFrame.document.createElement("INPUT");
				callbackParamFieldElement.type = "hidden";
				callbackParamFieldElement.name = "__CALLBACKPARAM";
				callbackParamFieldElement.value = eventArgument;
				xmlRequestFrame.document.forms[0].appendChild(callbackParamFieldElement);
				if (theSearchForm["__EVENTVALIDATION"]) {
					var callbackValidationFieldElement = xmlRequestFrame.document.createElement("INPUT");
					callbackValidationFieldElement.type = "hidden";
					callbackValidationFieldElement.name = "__EVENTVALIDATION";
					callbackValidationFieldElement.value = theSearchForm["__EVENTVALIDATION"].value;
					xmlRequestFrame.document.forms[0].appendChild(callbackValidationFieldElement);
				}
				var callbackIndexFieldElement = xmlRequestFrame.document.createElement("INPUT");
				callbackIndexFieldElement.type = "hidden";
				callbackIndexFieldElement.name = "__CALLBACKINDEX";
				callbackIndexFieldElement.value = callbackIndex;
				xmlRequestFrame.document.forms[0].appendChild(callbackIndexFieldElement);
				xmlRequestFrame.document.forms[0].submit();
			}
		}, 10);
	}
	function SearchWebForm_CallbackComplete() {
		for (i = 0; i < __searchPendingCallbacks.length; i++) {
			callbackObject = __searchPendingCallbacks[i];
			if (callbackObject && callbackObject.xmlRequest && (callbackObject.xmlRequest.readyState == 4)) {
				SearchWebForm_ExecuteCallback(callbackObject);
				if (!__searchPendingCallbacks[i].async) {
					__searchSynchronousCallBackIndex = -1;
				}
				__searchPendingCallbacks[i] = null;
				var callbackFrameID = "__CALLBACKFRAME" + i;
				var xmlRequestFrame = document.getElementById(callbackFrameID);
				if (xmlRequestFrame) {
					xmlRequestFrame.parentNode.removeChild(xmlRequestFrame);
				}
			}
		}
	}
	function SearchWebForm_ExecuteCallback(callbackObject) {
		var response = callbackObject.xmlRequest.responseText;
		callbackObject.eventCallback(response.substring(1), callbackObject.context);
		if (response.charAt(0) == "s") {
			if ((typeof(callbackObject.eventCallback) != "undefined") && (callbackObject.eventCallback != null)) {
				callbackObject.eventCallback(response.substring(1), callbackObject.context);
			}
		}
		else if (response.charAt(0) == "e") {
			if ((typeof(callbackObject.errorCallback) != "undefined") && (callbackObject.errorCallback != null)) {
				callbackObject.errorCallback(response.substring(1), callbackObject.context);
			}
		}
		else {
			var separatorIndex = response.indexOf("|");
			if (separatorIndex != -1) {
				var validationFieldLength = parseInt(response.substring(0, separatorIndex));
				if (!isNaN(validationFieldLength)) {
					var validationField = response.substring(separatorIndex + 1, separatorIndex + validationFieldLength + 1);
					if (validationField != "") {
						var validationFieldElement = theSearchForm["__EVENTVALIDATION"];
						if (!validationFieldElement) {
							validationFieldElement = document.createElement("INPUT");
							validationFieldElement.type = "hidden";
							validationFieldElement.name = "__EVENTVALIDATION";
							theSearchForm.appendChild(validationFieldElement);
						}
						validationFieldElement.value = validationField;
					}
					if ((typeof(callbackObject.eventCallback) != "undefined") && (callbackObject.eventCallback != null)) {
						callbackObject.eventCallback(response.substring(separatorIndex + validationFieldLength + 1), callbackObject.context);
					}
				}
			}
		}
	}

	function SearchWebForm_FillFirstAvailableSlot(array, element) {
		var i;
		for (i = 0; i < array.length; i++) {
			if (!array[i]) break;
		}
		array[i] = element;
		return i;
	}

	var __searchNonMSDOMBrowser = (window.navigator.appName.toLowerCase().indexOf('explorer') == -1);
	var __searchTheFormPostData = "";
	var __seatchTheFormPostCollection = new Array();

	function SearchWebForm_InitCallback() {
		var count = theSearchForm.elements.length;
		var element;
		for (var i = 0; i < count; i++) {
			element = theSearchForm.elements[i];
			var tagName = element.tagName.toLowerCase();
			if (tagName == "input") {
				var type = element.type;
				if ((type == "text" || type == "hidden" || type == "password" ||
					((type == "checkbox" || type == "radio") && element.checked)) &&
					(element.id != "__EVENTVALIDATION")) {
					SearchWebForm_InitCallbackAddField(element.name, element.value);
				}
			}
			else if (tagName == "select") {
				var selectCount = element.options.length;
				for (var j = 0; j < selectCount; j++) {
					var selectChild = element.options[j];
					if (selectChild.selected == true) {
						SearchWebForm_InitCallbackAddField(element.name, element.value);
					}
				}
			}
			else if (tagName == "textarea") {
				SearchWebForm_InitCallbackAddField(element.name, element.value);
			}
		}
	}
	function SearchWebForm_InitCallbackAddField(name, value) {
		var nameValue = new Object();
		nameValue.name = name;
		nameValue.value = value;
		__seatchTheFormPostCollection[__seatchTheFormPostCollection.length] = nameValue;
		__searchTheFormPostData += name + "=" + SearchWebForm_EncodeCallback(value) + "&";
	}
	function SearchWebForm_EncodeCallback(parameter) {
		if (encodeURIComponent) {
			return encodeURIComponent(parameter);
		}
		else {
			return escape(parameter);
		}
	}
	var __disabledControlArray = new Array();
	function SearchWebForm_ReEnableControls() {
		if (typeof(__enabledControlArray) == 'undefined') {
			return false;
		}
		var disabledIndex = 0;
		for (var i = 0; i < __enabledControlArray.length; i++) {
			var c;
			if (__searchNonMSDOMBrowser) {
				c = document.getElementById(__enabledControlArray[i]);
			}
			else {
				c = document.all[__enabledControlArray[i]];
			}
			if ((typeof(c) != "undefined") && (c != null) && (c.disabled == true)) {
				c.disabled = false;
				__disabledControlArray[disabledIndex++] = c;
			}
		}
		setTimeout("SearchWebForm_ReDisableControls()", 0);
		return true;
	}
	function SearchWebForm_ReDisableControls() {
		for (var i = 0; i < __disabledControlArray.length; i++) {
			__disabledControlArray[i].disabled = true;
		}
	}
	function SearchWebForm_FireDefaultButton(event, target) {
			if (event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
			var defaultButton;
			if (__searchNonMSDOMBrowser) {
				defaultButton = document.getElementById(target);
			}
			else {
				defaultButton = document.all[target];
			}
			if (defaultButton && typeof(defaultButton.click) != "undefined") {
				defaultButton.click();
				event.cancelBubble = true;
				if (event.stopPropagation) event.stopPropagation();
				return false;
			}
		}
		return true;
	}
	function SearchWebForm_GetScrollX() {
		if (__searchNonMSDOMBrowser) {
			return window.pageXOffset;
		}
		else {
			if (document.documentElement && document.documentElement.scrollLeft) {
				return document.documentElement.scrollLeft;
			}
			else if (document.body) {
				return document.body.scrollLeft;
			}
		}
		return 0;
	}
	function SearchWebForm_GetScrollY() {
		if (__searchNonMSDOMBrowser) {
			return window.pageYOffset;
		}
		else {
			if (document.documentElement && document.documentElement.scrollTop) {
				return document.documentElement.scrollTop;
			}
			else if (document.body) {
				return document.body.scrollTop;
			}
		}
		return 0;
	}
	function SearchWebForm_SaveScrollPositionSubmit() {
		if (__searchNonMSDOMBrowser) {
			theSearchForm.elements['__SCROLLPOSITIONY'].value = window.pageYOffset;
			theSearchForm.elements['__SCROLLPOSITIONX'].value = window.pageXOffset;
		}
		else {
			theSearchForm.__SCROLLPOSITIONX.value = SearchWebForm_GetScrollX();
			theSearchForm.__SCROLLPOSITIONY.value = SearchWebForm_GetScrollY();
		}
		if ((typeof(this.oldSubmit) != "undefined") && (this.oldSubmit != null)) {
			return this.oldSubmit();
		}
		return true;
	}
	function SearchWebForm_SaveScrollPositionOnSubmit() {
		theSearchForm.__SCROLLPOSITIONX.value = SearchWebForm_GetScrollX();
		theSearchForm.__SCROLLPOSITIONY.value = SearchWebForm_GetScrollY();
		if ((typeof(this.oldOnSubmit) != "undefined") && (this.oldOnSubmit != null)) {
			return this.oldOnSubmit();
		}
		return true;
	}
	function SearchWebForm_RestoreScrollPosition() {
		if (__searchNonMSDOMBrowser) {
			window.scrollTo(theSearchForm.elements['__SCROLLPOSITIONX'].value, theSearchForm.elements['__SCROLLPOSITIONY'].value);
		}
		else {
			window.scrollTo(theSearchForm.__SCROLLPOSITIONX.value, theSearchForm.__SCROLLPOSITIONY.value);
		}
		if ((typeof(theSearchForm.oldOnLoad) != "undefined") && (theSearchForm.oldOnLoad != null)) {
			return theSearchForm.oldOnLoad();
		}
		return true;
	}
	function SearchWebForm_TextBoxKeyHandler(event) {
		if (event.keyCode == 13) {
			var target;
			if (__searchNonMSDOMBrowser) {
				target = event.target;
			}
			else {
				target = event.srcElement;
			}
			if ((typeof(target) != "undefined") && (target != null)) {
				if (typeof(target.onchange) != "undefined") {
					target.onchange();
					event.cancelBubble = true;
					if (event.stopPropagation) event.stopPropagation();
					return false;
				}
			}
		}
		return true;
	}
	function SearchWebForm_AppendToClassName(element, className) {
		var current = element.className;
		if (current) {
			if (current.charAt(current.length - 1) != ' ') {
				current += ' ';
			}
			current += className;
		}
		else {
			current = className;
		}
		element.className = current;
	}
	function SearchWebForm_RemoveClassName(element, className) {
		var current = element.className;
		if (current) {
			if (current.substring(current.length - className.length - 1, current.length) == ' ' + className) {
				element.className = current.substring(0, current.length - className.length - 1);
				return;
			}
			if (current == className) {
				element.className = "";
				return;
			}
			var index = current.indexOf(' ' + className + ' ');
			if (index != -1) {
				element.className = current.substring(0, index) + current.substring(index + className.length + 2, current.length);
				return;
			}
			if (current.substring(0, className.length) == className + ' ') {
				element.className = current.substring(className.length + 1, current.length);
			}
		}
	}
	function SearchWebForm_GetElementById(elementId) {
		if (document.getElementById) {
			return document.getElementById(elementId);
		}
		else if (document.all) {
			return document.all[elementId];
		}
		else return null;
	}
	function SearchWebForm_GetElementByTagName(element, tagName) {
		var elements = SearchWebForm_GetElementsByTagName(element, tagName);
		if (elements && elements.length > 0) {
			return elements[0];
		}
		else return null;
	}
	function SearchWebForm_GetElementsByTagName(element, tagName) {
		if (element && tagName) {
			if (element.getElementsByTagName) {
				return element.getElementsByTagName(tagName);
			}
			if (element.all && element.all.tags) {
				return element.all.tags(tagName);
			}
		}
		return null;
	}
	function SearchWebForm_GetElementDir(element) {
		if (element) {
			if (element.dir) {
				return element.dir;
			}
			return SearchWebForm_GetElementDir(element.parentNode);
		}
		return "ltr";
	}
	function SearchWebForm_GetElementPosition(element) {
		var result = new Object();
		result.x = 0;
		result.y = 0;
		result.width = 0;
		result.height = 0;
		if (element.offsetParent) {
			result.x = element.offsetLeft;
			result.y = element.offsetTop;
			var parent = element.offsetParent;
			while (parent) {
				result.x += parent.offsetLeft;
				result.y += parent.offsetTop;
				var parentTagName = parent.tagName.toLowerCase();
				if (parentTagName != "table" &&
					parentTagName != "body" && 
					parentTagName != "html" && 
					parentTagName != "div" && 
					parent.clientTop && 
					parent.clientLeft) {
					result.x += parent.clientLeft;
					result.y += parent.clientTop;
				}
				parent = parent.offsetParent;
			}
		}
		else if (element.left && element.top) {
			result.x = element.left;
			result.y = element.top;
		}
		else {
			if (element.x) {
				result.x = element.x;
			}
			if (element.y) {
				result.y = element.y;
			}
		}
		if (element.offsetWidth && element.offsetHeight) {
			result.width = element.offsetWidth;
			result.height = element.offsetHeight;
		}
		else if (element.style && element.style.pixelWidth && element.style.pixelHeight) {
			result.width = element.style.pixelWidth;
			result.height = element.style.pixelHeight;
		}
		return result;
	}
	function SearchWebForm_GetParentByTagName(element, tagName) {
		var parent = element.parentNode;
		var upperTagName = tagName.toUpperCase();
		while (parent && (parent.tagName.toUpperCase() != upperTagName)) {
			parent = parent.parentNode ? parent.parentNode : parent.parentElement;
		}
		return parent;
	}
	function SearchWebForm_SetElementHeight(element, height) {
		if (element && element.style) {
			element.style.height = height + "px";
		}
	}
	function SearchWebForm_SetElementWidth(element, width) {
		if (element && element.style) {
			element.style.width = width + "px";
		}
	}
	function SearchWebForm_SetElementX(element, x) {
		if (element && element.style) {
			element.style.left = x + "px";
		}
	}
	function SearchWebForm_SetElementY(element, y) {
		if (element && element.style) {
			element.style.top = y + "px";
		}
	}
}

fAddEvent(window, 'load', fHeaderSetup); //to run all header scripts

