// JavaScript used to initialize search form and to set the forms action

var siteSearch_action = "/wps/wcm/connect/Bryant/Search/All Search Results";
	
function setSearch (searchForm) {

	var peopleSearch_action = "/bryant/PeopleSearch";
	var searchMode;

	for (i = 0; i < searchForm.search_mode.length; i++) {
		if (searchForm.search_mode[i].checked) {
			searchMode = searchForm.search_mode[i].value;
		}
	}

	if (searchMode == "site") {
		searchForm.action = siteSearch_action;
	} else if (searchMode == "people") {
		searchForm.action = peopleSearch_action;
		searchForm.general.value = searchForm.q.value;
	}
	
	return true;
}

function replace(string, text, by) {
    // Replaces text with by in string
    var i = string.indexOf(text)
		var newstr = '';
		
    if (i == -1) {
       return string;
		}
    newstr += string.substring(0,i) + by;
    if (i+text.length < string.length) {
      newstr += replace(string.substring(i+text.length, string.length), text, by);
		}
    return newstr;
}

function getParameter (parameterName) {
	
	var queryString = window.location.search;
	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 unescape (queryString.substring (begin, end));
		}
		
	}
	// Return "null string" if no parameter has been found
	return "";
}

function initSearch () {
	
	if (document.search_form) {

		var query = replace(getParameter("q"),"+"," ");
		if (query == "" && search_form.search_mode != undefined) {
			query = "Search...";
		}
		document.search_form.q.value = query;
		
		var mode = getParameter("search_mode").toLowerCase();
		if (mode != "" && (mode == "site" || mode == "people")) {
			
			var len = document.search_form.search_mode.length;
			for(var i = 0; i < len; i++) {
				if(document.search_form.search_mode[i].value == mode) {
					document.search_form.search_mode[i].checked = true;
				} else {
					document.search_form.search_mode[i].checked = false;
				}
			}
			
		}
		
	}
	var theFrame = document.getElementById('resultsFrame');
	if (theFrame) {
		var qString = "http://googlemini.bryant.edu/search?q=" + getParameter("q") + "&restrict=" + getParameter("restrict") + "&filter=" + getParameter("filter") + "&spell=" + getParameter("spell") + "&as_sitesearch=" + getParameter("as_sitesearch") + "&start=" + getParameter("start") + "&search_page=" + siteSearch_action + "&ie=&site=my_collection&output=xml_no_dtd&client=my_collection&lr=&proxystylesheet=my_collection&oe=";
		theFrame.src = qString;
	}
}
