// Triple Search Function -  Brian Daley

function UC_Search(object) {
	// Determine Radio Value
	for (i = 0, n = object.sitesearch.length; i < n; i++) {
		if (object.sitesearch[i].checked) {
			RV = object.sitesearch[i].value;
			break;
		}
	}

	// Quick Validation
	if ((object.q.value == '') || (object.q.value == 'Type Search Here')) {
		alert('You must enter your search criteria.');
		object.q.focus();
		return false;
	}

	// Set Variables for People Search (Phonebook)
	if (RV == 'People') {
		object.method = "POST";
		object.action = "http://phonebk.uconn.edu/results.php";
		object.q.name = "basictext";
	}
}

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri(str) {
	var o = parseUri.options, m = o.parser[o.strictMode ? "strict" : "loose"]
			.exec(str), uri = {}, i = 14;

	while (i--)
		uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function($0, $1, $2) {
		if ($1)
			uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode : false,
	key : [ "source", "protocol", "authority", "userInfo", "user", "password",
			"host", "port", "relative", "path", "directory", "file", "query",
			"anchor" ],
	q : {
		name : "queryKey",
		parser : /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser : {
		strict : /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose : /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

/**
 * Search object that handles redirects and parameters for the new google custom search
 */
var Search = {
	href : 'http://www.uconn.edu/search.php?',
	query : null,
	options : {
		web : 'cx=004595925297557218349:88uyjxwpn_0&page_id=160&cof=FORID:10&ie=UTF-8&sa=',
		uconn : 'cx=004595925297557218349:65_t0nsuec8&page_id=160&cof=FORID:10&ie=UTF-8&sa='
	},
	execute : function(type) {
		window.location.href = this.href + this.options[type] + '&q=' + this.query;
	}
};

/**
 * ONLY run this code when the old search page is used
 */
if (window.location.hostname == 'www.googlesyndicatedsearch.com') {

	/**
	 * Parse out GET request parameters
	 */
	var currentURI = parseUri(window.location.href);
	
	/**
	 * Determine the type of search (uconn or web)
	 */
	var type = (currentURI.queryKey['sitesearch'] === 'uconn.edu') ? 'uconn' : 'web';
	
	/**
	 * Set the search query string
	 */
	Search.query = currentURI.queryKey['q'];
	
	/**
	 * Execute the search
	 */
	Search.execute(type);
}