
addLoadEvent(function() {
	formActions(document.getElementsByTagName("input"));
	formActions(document.getElementsByTagName("textarea"));
	oPage = new Page();
	oPage.liveSearch();
});

function formActions(aElements){
	var sAgent = navigator.userAgent.toLowerCase();
	if(sAgent.indexOf("msie") != -1 && sAgent.indexOf("mac") != -1){
		return;
	};
	for(var i=0; i<aElements.length; i++){
		aElements[i].sValue = aElements[i].value;
		aElements[i].onfocus = function(){
			if(this.value == "keywords"){
				this.value = "";
			};
			this.className = this.className + " focused";
		};
		aElements[i].onblur = function(){
			this.className = this.className.replace(" focused", "").replace("focused", "");
		};
	};
};

function Page(){}


Page.prototype.liveSearch = function(){
	this.oSearchField = document.getElementById("fQuery");
	if(!this.oSearchField){
		return false;
	};
	this.oSearchField.parentNode.style.position = "relative";
	var oCont = document.createElement("div");
	oCont.id = "LiveSearch";
	oCont.style.top = this.oSearchField.offsetHeight + this.oSearchField.offsetTop + 1 + "px";
	oCont.style.left = this.oSearchField.offsetLeft + "px";
	this.oSearchField.parentNode.appendChild(oCont);
	this.oSearchField.setAttribute("autocomplete", "off");
	oCont = null;
	this.oSearchField.onkeyup = function(e){
		var oEvent = window.event || e;
		if(this.value.length > 2 && (oEvent.keyCode > 40 || oEvent.keyCode == 8)){
			var oXMLHTTP = oPage.xmlHTTP();
			oXMLHTTP.open("GET", "Default.aspx?page=346&fQuery=" + this.value, true);
			oXMLHTTP.onreadystatechange = function(){
				if(oXMLHTTP.readyState == 4){
					oPage.__liveSearched(oXMLHTTP.responseXML.xml);
				};
			};
			oXMLHTTP.send(null);
		};
	};
	this.oSearchField.onkeypress = function(e){
		var oEvent = window.event || e;
		if(oEvent.keyCode == 13 && document.getElementById("LiveSearchHighLight")){
			if(document.getElementById("LiveSearchHighLight").getAttribute("href")){
				return false;
			};
		};
	};
	this.oSearchField.onkeydown = function(e){
		var oEvent = window.event || e;
		oHighLight = false;
		if(this.value.length > 2){
			if(oEvent.keyCode == 13){
				if(document.getElementById("LiveSearchHighLight")){
					window.location = document.getElementById("LiveSearchHighLight").getAttribute("href");
					return false;
				};
			};
			if(oEvent.keyCode == 40){//down
				oHighLight = document.getElementById("LiveSearchHighLight");
				if(!oHighLight){
					oHighLight = document.getElementById("LiveSearch").getElementsByTagName("a")[0];
				}else{
					oHighLight.removeAttribute("id");
					oHighLight = oHighLight.nextSibling;
				};
			}else if(oEvent.keyCode == 38 ){//up
				oHighLight = document.getElementById("LiveSearchHighLight");
				if(!oHighLight){
					oHighLight = document.getElementById("LiveSearch").getElementsByTagName("a")[document.getElementById("LiveSearch").getElementsByTagName("a").length-1];
				}else{
					oHighLight.removeAttribute("id");
					oHighLight = oHighLight.previousSibling;
				};
			}else if(oEvent.keyCode == 27){//esc
				document.getElementById("LiveSearch").style.visibility = "hidden";
			};
			if(oHighLight){
				oHighLight.setAttribute("id","LiveSearchHighLight");
			};
		};
	};
	this.oSearchField.onblur = function(){
		setTimeout('document.getElementById("LiveSearch").style.visibility = "hidden"', 150);
	};
};
Page.prototype.__liveSearched = function(sXML){
	var oXML = this.xmlDocument();
	sXML = sXML.replace(RegExp("<em>", "g"), "");
	sXML = sXML.replace(RegExp("</em>", "g"), "");
	oXML.loadXML(sXML);
	var aLinks = oXML.getElementsByTagName("a");
	if(!aLinks.length){
		return document.getElementById("LiveSearch").style.visibility = "hidden";
	};
	document.getElementById("LiveSearch").innerHTML = "<strong>Quick results</strong>";
	var a;
	for(var i=0; i<aLinks.length; i++){
		a = document.createElement("a");
		a.setAttribute("href", aLinks[i].getAttribute("href"));
		a.appendChild(document.createTextNode(aLinks[i].firstChild.nodeValue));
		document.getElementById("LiveSearch").appendChild(a);
		a = null;
	};
	oXML = null;
	document.getElementById("LiveSearch").style.visibility = "visible";
};
Page.prototype.xmlHTTP = function(){
	if(window.ActiveXObject){
		if(this.__MSXML()){
			return new ActiveXObject(this.sMSXMLtype + ".XMLHTTP");
		}else{
			return false;
		};
	};
	if(window.XMLHttpRequest){
		var oXMLHTTP = new XMLHttpRequest();
		if(oXMLHTTP.readyState == null){
			oXMLHTTP.readyState = 1;
			oXMLHTTP.addEventListener("load", function(){
				oXMLHTTP.readyState = 4;
				if(typeof oXMLHTTP.onreadystatechange == "function")
					oXMLHTTP.onreadystatechange();
			}, false);
		};
		return oXMLHTTP;
	};
	return false;	
};
Page.prototype.xmlDocument = function(){
	try{
		if(document.implementation && document.implementation.createDocument){
			var oDoc = document.implementation.createDocument("", "", null);
			if(oDoc.readyState == null){
				oDoc.readyState = 1;
				oDoc.addEventListener("load", function(){
					oDoc.readyState = 4;
					if(typeof oDoc.onreadystatechange == "function")
						oDoc.onreadystatechange();
					}, false);
			};
			return oDoc;
		};
		if(window.ActiveXObject){
			if(this.__MSXML()){
				return new ActiveXObject(this.sMSXMLtype + ".DOMDocument");
			}else{
				return false;
			};
		};
	}catch(e){};
	return false;	
};
Page.prototype.__MSXML = function(){
	if(!this.sMSXMLHTTPtype){
		this.aMSXML = new Array("MSXML2", "Microsoft", "MSXML", "MSXML3");
		var oTest;
		for(var i=0;i<this.aMSXML.length; i++){
			try{
				oTest = new ActiveXObject(this.aMSXML[i] + ".DOMDocument");
				oTest = new ActiveXObject(this.aMSXML[i] + ".XMLHTTP");
				this.sMSXMLtype = this.aMSXML[i];
				return this.sMSXMLtype;
			}catch(e){};
		};
	}else{
		return this.sMSXMLtype;
	};
};
/****************************************************************************************
DOM additions
****************************************************************************************/

if(window.DOMParser && window.XMLSerializer && window.Node && Node.prototype && Node.prototype.__defineGetter__){
	Document.prototype.loadXML = function(s){
		var oDoc = (new DOMParser()).parseFromString(s, "text/xml");
		while(this.hasChildNodes())
	    	this.removeChild(this.lastChild);
		for(var i = 0; i < oDoc.childNodes.length; i++) {
			this.appendChild(this.importNode(oDoc.childNodes[i], true));
		};
	};
	Document.prototype.__defineGetter__("xml", function(){
		return (new XMLSerializer()).serializeToString(this);
	});
};