function AutoSearchControl(oTextbox,oProvider,sResultDiv,oProdProvider) {
		this.provider = oProvider;
		this.productProvider = oProdProvider;
		this.resultDiv = document.getElementById(sResultDiv);
		this.textbox = oTextbox;
		this.userText = oTextbox.value;
		this.timeoutId = null;
		this.timeoutId2 = null;
		this.produktDiv = document.getElementById("produktDiv");
		this.produktBild = document.getElementById("produktBild");
		this.container = document.getElementById("extra");
		this.sokheadertext = document.getElementById("sokheadertext");
		//this.sokbild = document.getElementByID("snurrbild");
		this.init();
			
}



/**
 * Initierar textrutan med event handlers.
 */
AutoSearchControl.prototype.init = function() {
    //save a reference to this object
    var oThis = this;
	this.createSearchDiv(); 
	this.getCookie();
	this.textbox.onblur = function () {
		oThis.setCookie(false);
	};
	this.textbox.onfocus = function () {
		document.getElementById("sokdiv").style.display = "";
		document.getElementById("sokheaderdiv").style.display = "";
	};
	this.textbox.onkeyup = function (oEvent) {
    
        //check for the proper location of the event object
        if (!oEvent) {
            oEvent = window.event;
        }    
        
        //call the handleKeyUp() method with the event object
        oThis.handleKeyUp(oEvent);
    };
};
AutoSearchControl.prototype.getCookie = function() {
	var sRE = "(?:; )?soktext=([^;]*);?";
    var oRE = new RegExp(sRE);
               
    if (oRE.test(document.cookie)) {
		this.textbox.value = (decodeURIComponent(RegExp["$1"]));
    }
};
AutoSearchControl.prototype.setCookie = function(bDelete) {
	var sCookie = "soktext" + "=" + encodeURIComponent(this.userText);
    if (bDelete) {
    	sCookie += "; expires=" + (new Date(0)).toGMTString();
    }
    document.cookie = sCookie;
};
AutoSearchControl.prototype.createSearchDiv = function() {
	this.layer = document.createElement("div");
	this.layer.style.width = 810 + "px";
	this.layer.style.visibility = "hidden";
	this.resultDiv.appendChild(this.layer);
	var oThis = this;
	this.layer.onmouseover = this.layer.onmousedown = function (oEvent) {
		oEvent = oEvent || window.event;
		oTarget = oEvent.target || oEvent.srcElement;

		
		if (oEvent.type == "mouseover") {
			clearTimeout(this.timeoutId2);
			if (oTarget.tagName == "DIV") {
				
			} else if (oTarget.parentNode.tagName == "DIV") {
				oTarget = oTarget.parentNode;
				
			}
			oThis.highlightArtikel(oTarget);
			// Anropet skall endast göras om Div-namnet är ett artnr
			var artnrtyp = /^\d{4,5}$/;
			if (artnrtyp.test(oTarget.id)) {
				this.timeoutId2 = setTimeout( function () {
            	oThis.hamtaProdukt(oTarget);
        		}, 500);
			}
		} else if (oEvent.type == "mousedown") {
			if (oTarget.tagName == "DIV") {
				oThis.produktSida(oTarget.id);
			} else if (oTarget.parentNode.tagName == "DIV") {
				oTarget = oTarget.parentNode;
				oThis.produktSida(oTarget.id);
			}
			
		}
	};
};

AutoSearchControl.prototype.produktSida = function (sArtnr) {
	var iSida = 8000 + parseInt(sArtnr);
	location.href = "product44.asp?id=" + iSida;
};

AutoSearchControl.prototype.hamtaProdukt = function (oProdDiv) {
		var sDivID = oProdDiv.id;
		this.produktDiv.style.top = (this.getTop(oProdDiv) + oProdDiv.offsetHeight) +  "px";
		this.produktDiv.style.left = (this.getLeft(oProdDiv) - 30) +  "px";
		this.productProvider.getProduct(this,sDivID);
		this.produktBild.src = "prod/"+sDivID+"-180.jpg";
		this.produktBild.style.width = "120px";
};

AutoSearchControl.prototype.visaProdukt = function (sSvar) {
	//this.produktDiv.innerHTML += sSvar;
	// Nedanstående visar popupen. Gömd 070102
	//this.produktDiv.style.visibility = "visible";
};

AutoSearchControl.prototype.getLeft = function (oNode) {
	var iLeft = 0;
	while(oNode.tagName != "BODY") {
		iLeft += oNode.offsetLeft;
		oNode = oNode.offsetParent;
	}
	return iLeft;
};

AutoSearchControl.prototype.getTop = function (oNode) {
	var iTop = 0;
	while(oNode.tagName != "BODY") {
		iTop += oNode.offsetTop;
		oNode = oNode.offsetParent;
	}
	return iTop;
};

AutoSearchControl.prototype.highlightArtikel = function (oMouseOverNode) {
	if (oMouseOverNode.id.length > 0) {
	for (var i = 0; i < this.layer.childNodes.length; i++) {
		var oNode = this.layer.childNodes[i];
		if (oNode == oMouseOverNode) {
			oNode.className = "ProduktContainerCurrent";
		} else if (oNode.className == "ProduktContainerCurrent") {
			oNode.className = "ProduktContainer";
		}
	}
	}

};


AutoSearchControl.prototype.handleKeyUp = function (oEvent) {
    var iKeyCode = oEvent.keyCode;
    var oThis = this;
    
    //get the currently entered text
    this.userText = this.textbox.value;
    if (this.userText.length == 0) {
		this.container.style.display = "none";
		this.taBortResultat();
	} else if (this.userText.length == 1) {
		// ingen sökning på 1 tecken
	} else {
	
    clearTimeout(this.timeoutId);

    
    //for backspace (8) and delete (46), shows suggestions
    if (iKeyCode == 8 || iKeyCode == 46) {
		this.sokheadertext.innerHTML = "<img src='img/snurra.gif' id='snurrbild'>S&ouml;kning p&aring;g&aring;r. Du kan forts&auml;tta att skriva upp till tre ord.";
		document.getElementById("sokdiv").style.display = "";
		document.getElementById("sokheaderdiv").style.display = "";
        this.timeoutId = setTimeout( function () {
            oThis.provider.requestSearch(oThis);
        }, 250);
        
    //make sure not to interfere with non-character keys
    } else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignore
    } else {
        //request suggestions from the suggestion provider
		this.sokheadertext.innerHTML = "<img src='img/snurra.gif' id='snurrbild'>S&ouml;kning p&aring;g&aring;r. Du kan forts&auml;tta att skriva upp till tre ord.";
		document.getElementById("sokdiv").style.display = "";
		document.getElementById("sokheaderdiv").style.display = "";
		this.timeoutId = setTimeout( function () {
            oThis.provider.requestSearch(oThis);
        }, 250);
    }
	}
};

AutoSearchControl.prototype.taBortResultat = function () {
	var oCTR = null;
	for (var i = (this.layer.childNodes.length-1); i >= 0; i--) {
		oCTR = this.layer.childNodes[i];
		this.layer.removeChild(oCTR);
	}	
};

AutoSearchControl.prototype.noHits = function () {
	this.sokheadertext.innerHTML = "Inga tr&auml;ffar hittades på din s&ouml;kning.";
};

AutoSearchControl.prototype.visasokresultat = function (sSvar) {
	this.taBortResultat();
	var oXmlDom = null;
	oXmlDom = zXmlDom.createDocument();
	if (sSvar.length == 15) {
		this.noHits();
	} else {
	oXmlDom.loadXML(sSvar);
	//if (oXmlDom.parseError.errorCode == 0) {
		//alert("ok")
	//} else {
		//var str = "Fel!\n" + "Descr: " + oXmlDom.parseError.reason + "\n" +
		//"Line: " + oXmlDom.parseError.line + "\n" +
		//"Pos: " + oXmlDom.parseError.linePos + "\n" +
		//"Src: " + oXmlDom.parseError.srcText;
		
		//alert(str);
	//}
	var oRoot = oXmlDom.documentElement;
	var aArtiklar = oRoot.getElementsByTagName("artikel");
	var iHits = parseInt(aArtiklar[0].getAttribute("hits")) + 1;
	//var sokHeaderDiv = document.createElement("div");
	//sokHeaderDiv.className = "sokheader";
	
	
			
	for (var i = 0; i < aArtiklar.length; i++) {
		var sArtnr = aArtiklar[i].getAttribute("nr");
		
		var sNamn, sKortis, sPris;
		
		for (var j = 0; j < aArtiklar[i].childNodes.length; j++) {
			switch (aArtiklar[i].childNodes[j].tagName) {
				case "namn":
					sNamn = aArtiklar[i].childNodes[j].text;
				break;
				case "kortis":
					sKortis = aArtiklar[i].childNodes[j].text;
				break;
				case "pris":
					sPris = aArtiklar[i].childNodes[j].text;
				break;
				default:
				break;
			}
		}
		//alert(sNamn + " " + sKortis);
		
		// Ny rad efter tre artiklar
		if (i%3 == 0) {
			var radbryt = document.createElement("div");
			radbryt.className = "radbryt";
			this.layer.appendChild(radbryt);
		}

		var divContainer = document.createElement("div");
        var imgProd = document.createElement("img");
        
		divContainer.className = "produktContainer";
		divContainer.id = sArtnr;
        
        imgProd.src = "produkter/" + sArtnr + "-80.jpg";
		divContainer.appendChild(imgProd);
        
        var h3Title = document.createElement("h3");
        h3Title.appendChild(document.createTextNode(sNamn));
        divContainer.appendChild(h3Title);
        
        divContainer.appendChild(document.createTextNode(sKortis));
        divContainer.appendChild(document.createElement("br"));
        divContainer.appendChild(document.createTextNode(sPris+":-"));
        
        this.layer.appendChild(divContainer);
		
	}

	this.layer.style.visibility = "visible";
	this.container.style.display = "block";
	this.sokheadertext.innerHTML = "Din s&ouml;kning gav " +iHits+ " tr&auml;ffar";
	}
};

function SearchProvider() {
    this.http = zXmlHttp.createRequest();
}
SearchProvider.prototype.requestSearch = function (oAutoSearchControl) {
	//oAutoSearchControl.sokheadertext.innerHTML = "<img src='img/snurra.gif' id='snurrbild'>";
	//document.getElementByID("snurrbild").style.display = "none";
	var oHttp = this.http;
	
	// avbryt pågående request
	if (oHttp.readyState != 0) {
		oHttp.abort();
	}
	
	var sData = oAutoSearchControl.userText;
	var sURL = "getSearchSQL.asp";
	var aData = sData.split(" ");
	sData = "q="+aData.join("+")+"&l=10";

	oHttp.open("post",sURL,true);
	oHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	oHttp.onreadystatechange = function () {
		// alert(oHttp.readyState);
		if (oHttp.readyState == 4) {
			// if (oHttp.status == 200) {
				//alert(oHttp.responseText);
				oAutoSearchControl.visasokresultat(oHttp.responseText);
				
			// }
		}
	};
	oHttp.send(sData);
};

function getProductProvider() {
	this.http = zXmlHttp.createRequest();
}

getProductProvider.prototype.getProduct = function (oAutoSearchControl, sArtnr) {
	var oHttp = this.http;
	// avbryt pågående request
	if (oHttp.readyState != 0) {
		oHttp.abort();
	}
	var sURL = "getProduct.asp?nr="+sArtnr;
	//alert(sURL);
	oHttp.open("get",sURL,true);
	oHttp.onreadystatechange = function () {
		if (oHttp.readyState == 4) {
			oAutoSearchControl.visaProdukt(oHttp.responseText);
		}
	};
	oHttp.send();
};
