// Creates an object to retrieve query string values.
function QueryString(search) {
	this.keys = [];
	this.values = {};
	
	if (search != "") {
		var searchValues = search.substring(search.indexOf("?") + 1).split("&");
		
		for (var i = 0; searchValues != "" && i < searchValues.length; i++) {
			var eIndex = searchValues[i].indexOf("=");
			var key = searchValues[i].substr(0, eIndex);
			var value = "";
			
			if (eIndex < searchValues[i].length - 1) {
				value = searchValues[i].substr(eIndex + 1);
				value = value.replace(/\+/g, " ");
				value = decodeURIComponent(value);
			}
			
			if (this.hasKey(key)) {
				this.values[key].push(value);
			}
			else {
				this.values[key] = [value];
				this.keys.push(key);
			}
		}
	}
}

// Checks if a parameter was passed.
QueryString.prototype.hasKey = function(key) {
	for (var i = 0; i < this.keys.length; ++i) {
		if (key == this.keys[i]) {
			return true;
		}
	}
	
	return false;
};

// Retrieves the values and joins them with the specified separator.  If not specified, a comma is used.
QueryString.prototype.getValue = function(key, separator) {
	var res = "";
	
	if (!separator) {
		var separator = ",";
	}
	
	return this.getArray(key).join(separator);
};

// Retrieves the values and returns them as an array.
QueryString.prototype.getArray = function(key) {
	return this.values[key] || [];
};

// Sets the value(s) of the specified key.  Values should not be URI encoded.  If the value is an array, each array
// item is treated as a separate value and converted to a string.  Any other value is converted to a string and is
// the lone value of the key.
QueryString.prototype.setValue = function(key, value) {
	var tmp = [];

	if (({}).toString.call(value) == "[object Array]") {
		for (var i = 0; i < value.length; ++i) {
			if (value[i] && value[i] !== false) {
				tmp.push(value[i].toString());
			}
			else {
				tmp.push("");
			}
		}
	}
	else {
		if (value && value !== false) {
			tmp.push(value.toString());
		}
		else {
			tmp.push("");
		}
	}

	this.values[key] = tmp;
};

// Removes the specified key from the QueryString.
QueryString.prototype.removeKey = function(key) {
	if (this.hasKey(key)) {
		delete this.values[key];
	}
};

// Converts the QueryString to a string.  All components are encoded.
QueryString.prototype.toString = function() {
	var tmp = "";
	var curKey, vals;

	for (var i = 0; i < this.keys.length; ++i) {
		curKey = this.keys[i];
		vals = this.values[curKey];

		for (var j = 0; j < vals.length; ++j) {
			tmp += "&" + curKey + "=" + encodeURIComponent(vals[j]);
		}
	}

	return tmp.substr(1);
};

// Constructs a URL for a page in the same directory as the current or the window/document passed.
function getLocalPage(page, win) {
	var loc = window.location;
	
	if (win) {
		loc = win.location;
	}
	
	return loc.protocol + "//" + loc.host + loc.pathname.substr(0, loc.pathname.lastIndexOf("/") + 1) + page;
}

// Creates a url suitable for filling in the power search form.
function createPowerSearchURL(search, searchtype, sourceString, theDate, indexString, useRel) {
	var data = [];
	
	data.push("sr=" + encodeURIComponent(search || ""));
	data.push("stp=" + encodeURIComponent(searchtype || "boolean"));
	data.push("selectedSources=" + encodeURIComponent(sourceString || ""));
	data.push("indexTerms=" + encodeURIComponent(indexString || ""));
	data.push("useRel=" + encodeURIComponent(useRel || "false"));

	if (theDate && theDate.length > 0) {
		data.push(theDate);
	}
	
	return "form_general_power.asp?" + data.join("&");
}

// Stores and restores the MasterDate state as parameters for a URL.
var MasterDateStore = {
	getElements: function(theForm) {
		return {
			dateSelector: document.getElementById('dateSelector'+theForm.formID),
			fromDate: document.getElementById('fromDate'+theForm.formID),
			toDate: document.getElementById('toDate'+theForm.formID),
			numericUnit: document.getElementById('numericUnit'+theForm.formID),
			calendarUnit: document.getElementById('calendarUnit'+theForm.formID)
		};
	},
	createQueryString: function(theForm) {
		var md = this.getElements(theForm);
		var qs = [
			"ds=" + encodeURIComponent(md.dateSelector.selectedIndex),
			"fd=" + encodeURIComponent(md.fromDate.value),
			"td=" + encodeURIComponent(md.toDate.value),
			"nu=" + encodeURIComponent(md.numericUnit.selectedIndex),
			"cu=" + encodeURIComponent(md.calendarUnit.selectedIndex)
		];
		
		return qs.join("&");
	},
	loadQueryString: function(theForm, query) {
		var md = this.getElements(theForm);
		var qs = new QueryString(query);
		
		md.dateSelector.selectedIndex = parseInt(qs.getValue("ds"), 10);
		md.fromDate.value = qs.getValue("fd");
		md.toDate.value = qs.getValue("td");
		md.numericUnit.selectedIndex = parseInt(qs.getValue("nu"), 10);
		md.calendarUnit.selectedIndex = parseInt(qs.getValue("cu"), 10);
	}
};

// Converts the arrays to a suitable index string.
function indexesToString(indexes, names) {
	if (indexes.length != names.length) {
		throw new Error("The number of indexes does not match the number of names.");
	}
	
	var res = "";
	var con = "AND";
	
	for (var i = 0; i < indexes.length; ++i) {
		res += (["~" + indexes[i], con, names[i], "", "-"]).join("|");
	}
	
	if (res.length > 0) {
		res = res.substr(1);
	}
	
	return res;
}

// Converts the arrays to a suitable source string.
function sourcesToString(srcs, names) {
	if (srcs.length != names.length) {
		throw new Error("The number of sources does not match the number of names.");
	}
	
	var res = "";
	
	for (var i = 0; i < srcs.length; ++i) {
		res += (["~" + names[i], srcs[i]]).join("|");
	}
	
	if (res.length > 0) {
		res = res.substr(1);
	}
	
	return res;
}

function updateBanner() {
	// If the message container is present, try to load the message.
	if ($("#bannerMessage").length == 1) {
		// Fetch the message from the server.
		$.get("message.asp", function(data) {
			// Load the message and display.
			if (data != "NO_MESSAGE") {
				$("#bannerMessage").html(data)
								   .removeClass("hidden");
			}
			else {
				$("#bannerMessage").addClass("hidden");
			}
		});
	}
}

// The initialization for the banner page.
$(document).ready(function() {
	updateBanner();
});
