// Function to change the background image of the google text field when appropriate
function setBackground (field, fragRoot) {
	if (field.value == "") {
		field.style.background = "rgb(255, 255, 255) url(" + fragRoot + "www_file_store/images/google_search_watermark.gif) no-repeat scroll left center";
	}
	else {
		field.style.background = "white";
	}
}

// Checks if enter has been pressed and goes to search page
function googleSearchRedirect (e, serverRoot) {
	// SR: If 'GO' button was pressed, or enter was keyed in text field then show results
	if (e == "button" || e.keyCode == 13) {
		window.location = serverRoot + "Sandbox/GoogleSearch/index.htm?searchTerm=" + document.getElementById('googleSearchTextField').value;
	}
}

var searchControl;
var drawOptions;
// NOTE: this only works if the google search script is included before this script
function OnLoad() {
	// SR: Create a search control
	searchControl = new google.search.SearchControl();

	// SR: site restricted web search with custom label
	// and class suffix
	var siteSearch = new GwebSearch();
	siteSearch.setUserDefinedLabel("Thiess");
	siteSearch.setUserDefinedClassSuffix("siteSearch");
	siteSearch.setSiteRestriction("thiess.com.au");

	// SR: set default to show expanded version and add searcher to search control
	var options = new GsearcherOptions();
	options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
	searchControl.addSearcher(siteSearch, options);

	// SR: set the draw mode for the Google search and
	// set the input field (instead of the default one)
	drawOptions = new GdrawOptions();
	drawOptions.setInput(document.getElementById("googleSearchTextField"));
	
	// SR: parse the search term from the URL
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	
	if(urlHalves[1]){
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == 'searchTerm') {
					document.getElementById('googleSearchTextField').value = unescape(urlVarPair[1]);
					googleSearch('button');
				}
			}
		}
	}
}
	
// NOTE: this only works if the google search script is included before this script
function googleSearch (e) {
	// SR: The search results/main content div
	var resultsDiv = document.getElementById("search-results");

	// SR: If button was pressed, or enter in text field then show results
	if (e == "button" || e.keyCode == 13) {
		// SR: draw and execute search
		searchControl.draw(resultsDiv, drawOptions);
		searchControl.execute();
	}
}