

function callSearch(){
	var request = createRequest();
	request.open("GET", "/MicrositeSearchAction.bsci?langId=" + getParameter('langId') + "&resultsPerPage=" + getParameter('resultsPerPage') + "&currentPage=1&searchDimension=" + getParameter('dimension') + "&searchTerm=" + getParameter('search_term'));
	request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");    // prevent IE caching
	request.onreadystatechange = function(){loadSearchResults(request);}
	request.send(null);
}




// Callback function for loading the results of the search action onto the results page
function loadSearchResults(request){

	if (request.readyState == 4){
		if (request.status != 200) {
			alert("Error!  Server unavailable.");
			return;
		}

		var responseXML = request.responseXML.documentElement;
		responseXML.normalize();

		var totalPages = responseXML.getElementsByTagName("total-pages")[0].firstChild.nodeValue;
		var totalResults = responseXML.getElementsByTagName("total-results")[0].firstChild.nodeValue;	
		var currentPage = responseXML.getElementsByTagName("current-page")[0].firstChild.nodeValue;
		var resultsPerPage = responseXML.getElementsByTagName("results-per-page")[0].firstChild.nodeValue;

		// Update feedback div
		document.getElementById("retrieved_search_term").innerHTML = decodeURIComponent(getParameter('search_term'));
		document.getElementById("number_results").innerHTML = totalResults;

		// Update pulldown and menu
		document.getElementById("paging_pulldown_1").innerHTML = "";
		generatePulldownOptions(currentPage, totalPages, resultsPerPage, totalResults);
		document.getElementById("paging_pulldown_2").firstChild.nodeValue = totalResults;
		document.getElementById("paging_menu").innerHTML = generatePagingMenu(currentPage, totalPages);

		// Update results
		if (responseXML.getElementsByTagName("result-text")[0].childNodes.length > 0){
			var response = responseXML.getElementsByTagName("result-text")[0].childNodes[0];
			document.getElementById("results").innerHTML = response.nodeValue;
		}
				
	}
}

function generatePulldownOptions (currentPage, totalPages, resultsPerPage, resultsTotal) {

   var select = document.getElementById("paging_pulldown_1");
   for (var i = 1; i < totalPages; i++){
   	var option = document.createElement("option");
   	var optionValue = document.createAttribute("value");
   	option.nodeValue = i;
   	option.setAttributeNode(optionValue);
   	if (i == currentPage){
   	   var selectNode = document.createAttribute("selected");
   	   selectNode.nodeValue = "true";
   	   option.setAttributeNode(selectNode);
   	}
   	var optionText = document.createTextNode((i-1) * resultsPerPage + 1 + ' - ' + i * resultsPerPage);
   	option.appendChild(optionText);
   	select.appendChild(option);

   }
   	var option = document.createElement("option");
   	var optionValue = document.createAttribute("value");
   	option.nodeValue = totalPages;
   	option.setAttributeNode(optionValue);
   	if (totalPages == currentPage){
   	   var selectNode = document.createAttribute("selected");
   	   selectNode.nodeValue = "true";
   	   option.setAttributeNode(selectNode);
   	}
   	if (resultsTotal != 0){
	   	var optionText = document.createTextNode((totalPages - 1) * resultsPerPage + 1 + ' - ' + resultsTotal);
	} else {
	   	var optionText = document.createTextNode('0 - 0');	
	}
   	option.appendChild(optionText);
   	select.appendChild(option);
   
}

// Functions that fire when paging event occurs

function pulldownPageChange(){
	var currentPage = "";
	var selectObject = document.getElementById("paging_pulldown_1");
	for (var i = 0; i < selectObject.length; i++){
		if (selectObject[i].selected){
			currentPage = i + 1;
		}
	}
	updatePage(currentPage);
}

// Function called by paging event which calls correct page

function updatePage(currentPage){
	var request = createRequest();
	request.open("GET", "/MicrositeSearchAction.bsci?langId=" + getParameter('langId') + "&resultsPerPage=" + getParameter('resultsPerPage') + "&currentPage=" + currentPage + "&searchDimension=" + getParameter('dimension') + "&searchTerm=" + getParameter('search_term'));
	request.onreadystatechange = function(){loadSearchResults(request);}
	request.send(null);
}

// Functions for generating the paging menu and pulldown

function generatePagingMenu (currentPage, pagesTotal) {
	var previous = 1;
	var next = pagesTotal;
	if (currentPage > 1){
		previous = (currentPage * 1) - 1;	
	}
	if (currentPage < (pagesTotal * 1)){
		next = (currentPage  * 1) + 1;
	}
	var output = "<a href='#' onClick='updatePage(1);'><<</a> <a href='#' onClick='updatePage(" + previous + ");'>Previous</a> | ";
	for (var i = 1; i <= pagesTotal; i++){
		if (currentPage == i){
			output = output + "<span>" + i + "</span> ";
		} else {
			output = output + "<a href='#' onClick='updatePage(" + i + ");' class='link'>" + i + "</a> ";
		}
	}
	output = output + "| <a href='#' onClick='updatePage(" + next + ");'>Next</a> <a href='#' onClick='updatePage(" + pagesTotal + ");'>>></a>";
	return output;
}


// Standard AJAX create request function
function createRequest(){
	var ajaxRequest; 
	try {
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)	{
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e)	{
				alert("Your browser does not support Ajax.  Please update your browser");
			}
		}
	}
	return ajaxRequest;
}

function getParameter(parameterName){
	return getGetParameter(window.top.location.search.substring(1), parameterName);
}


function getGetParameter ( queryString, parameterName ) {
	// Add "=" to the parameter name (i.e. parameterName=value)
	var parameterName = parameterName + "=";
	if ( queryString.length > 0 ) {
		// Find the beginning of the string
		begin = queryString.indexOf ( parameterName );
		// If the parameter name is not found, skip it, otherwise return the value
		if ( begin != -1 ) {
			// Add the length (integer) to the beginning
			begin += parameterName.length;
			// Multiple parameters are separated by the "&" sign
			end = queryString.indexOf ( "&" , begin );
			if ( end == -1 ) {
				end = queryString.length
			}
		// Return the string
		return queryString.substring ( begin, end );
		}
		// Return "null" if no parameter has been found
		return "null";
	}
} 


