//This is a global variable that contains 
//the URL to the search-page.
var baseURL;
/**
* This method transmits the user to the same page, but adds the search-querystring-parameters
**/
function search(){
	if(baseURL == null) alert('You need to run the Setup-method before running the Search-method');
	//Get the search-string and Url-encode it
	var searchString = document.getElementById("txtTerms").value;
	searchString = encodeURI(searchString);
	//Get the type.
	var type = document.getElementById("hidType").value;	

	sendToUrl(baseURL, searchString, type);
}
/**
*Transmits the user to the supplied Url.
*/
function sendToUrl(url, searchString, type){
	//If the Url contains other parameters, we add the querystring-parameters with &, otherwise
	//we add them with a ?
	var startParam;
	
	if(url.indexOf("?") != -1){
		startParam = "&";
	} else {
		startParam = "?";
	}	
	
	//Set the location to add the querystring-parameters for the search.
	if (url.search("t=") > 0) {
		document.location.href = url  + startParam + 'q=' + searchString;
	} else {
		document.location.href = url  + startParam + 'q=' + searchString + '&t=' + type;
	}
	
}
/**
*This method is used for Quick-search.
**/
function fastSearch(url, searchString){
	sendToUrl(url, searchString, 0);
}

function employeesearch(url, searchString, radioAll, radioCurrent) {
	var startParam;
	
	if(url.indexOf("?") != -1){
		startParam = "&";
	} else {
		startParam = "?";
	}	
	
	if	(radioAll) {
		document.location.href = url  + startParam + 'q=' + searchString + '&office=all';
	} else {
		document.location.href = url  + startParam + 'q=' + searchString + '&office=current';
	}
	
}

/**
*This method changes the value of hidType to the supplied type, and changes
*the css-class of the sender to 'active', and removes all other css-classes in the list.
**/
function changeType(type){
	document.getElementById("hidType").value = type;		
	//If we have a search-term entered, run the search()-method immidiately
	if(document.getElementById("txtTerms").value != ''){
		search();
		return;
	} else {
		activateType(type);
	}	
}

function activateType(type){
	//The list is the first-child of the SearchTypes-div
	var list = $('SearchTypeList');
	
	//Loop through all the nodes in the list of
	//search-types and inactivate all nodes but the header 
	//and the selected type.	
	for(var i = 0, imax = list.childNodes.length; i < imax; i++){
		
		var li = list.childNodes[i];
		//Do not change the class on the header
		if(li.className != 'header'){
			//The id of the list-items are searchTypes[searchtype], 
			//so if the current types id contains the type,
			//we can set the class to 'active'.
			if(li.id == ("searchTypes"+type)){
				li.className = 'active';
			} else {
				li.className = '';
			}			
		}
	}
}

/**
*this method sets up global vars and controls.
**/
function setup(searchString, searchType, url){
	document.getElementById("txtTerms").value = searchString;
	document.getElementById("hidType").value = searchType;		
	baseURL = url;
	activateType(searchType);
}

/**
*This method executes the supplied function
*if the key that was pressed was Enter.
**/
function executeOnEnter(e, func){	
	if(isEnterPress(e)){
			func();
			return false;			
	}
    return true;    
}
/*
*INdicates whether the supplied event
* is an enter-press
*/
function isEnterPress(e){
	if(window.event){
        if((window.event.keyCode == 13) ){
			return true;			
		}
    }else{
        if((e.which == 13) ){
			return true;
		}
    }
    return false;   
}