
//AJX-SEARCH
var req;
var lastq;
var searchTimer = null;
function searchFor(e, query)
{
	if( e != null )
	{
		var keycode = (e.which == null) ? e.keyCode : e.which;
		
		//alert(keycode)
		switch( keycode )
		{
			case 13:
				if (firstMatch[0] != null && firstMatch[1] != null)
				{
					goAjx(firstMatch[0], firstMatch[1]);
					return;
				}
				break;
			case 27:
				document.getElementById('ct1').style.visibility='hidden';
				return;
				break;
		}
	}
	if (query == null)
		return;
		
	if (query.length <= 1 || query == "Search product" || query == "Sök produkt")
	{
		document.getElementById('ct1').style.visibility='hidden';
		document.getElementById("searchloader").src = '/img/btn-search.gif';
		return;
	}
	if (lastq == query)
	{
		if (query.length >= 2)
		{
			document.getElementById("ct1").style.visibility = "visible";
		}
		return;
	}
	
	lastq = query;
	
	
	if (searchTimer != null)
		clearTimeout(searchTimer);
	searchTimer = setTimeout("makeSearch('"+query+"');", 150);
    document.getElementById("searchloader").src = '/img/ajx_search.gif';
}

function makeSearch(query)
{
    var url = "/_ajx_search.adi?q=" + query;
    
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.onreadystatechange = processSearchRequest;
    req.open("GET", url, true);
    req.send(null);
}

function processSearchRequest()
{
    if (req.readyState == 4)
    {
	    document.getElementById("searchloader").src = '/img/btn-search.gif';
        if (req.status == 200)
        {
        	if (req.responseText.length > 2)
	        	document.getElementById("ct1").style.visibility = 'visible';
	        else
	        	document.getElementById("ct1").style.visibility = 'hidden';
	        document.getElementById("ct1").innerHTML = unescape(req.responseText);
        }
    }
}

var firstMatch = new Array(2);
function goAjx(prod)
{
	location.href = '/product_' + prod + ".html";
}




function retailSearch(query)
{
    var url = "/_ajx_storesearch.adi?q=" + query;
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    req.onreadystatechange = function()
	{
	    if (req.readyState == 4)
	    {
	        if (req.status == 200)
	        {
	  			document.getElementById("retailContainer").innerHTML = req.responseText;
	        }
	        else
	        {
	        	document.getElementById("retailContainer").innerHTML = req.responseText;
	        }
	    }
	};	
	
    
	req.open('GET', url, true);
	req.send(null);
}
