// JScript source code
function ItemSearchBox_ClientOnKeyUp(event)
{
	search = document.getElementById("ItemSearchBox");
	searchResultsLayer = document.getElementById("ItemSearchResultsLayer");
	if (search.value.length >= 3)
	{
		SearchBox.Refresh(search.value);

		var ResultsList = SearchBox.SearchItem(search.value);
		
		if (ResultsList.List.length > 0)
		{
			searchResultsLayer.style.display = "block";               
			searchResultsLayer.style.left = getOffsetLeft(search) + "px";
			searchResultsLayer.style.top = (getposOffset(search, "top") + search.offsetHeight + 2) + "px";
			searchResultsLayer.style.zindex = 800000;

			document.getElementById("ItemSearchResultsLayer").innerHTML = ResultsList.Render();
		}
		else
		{
			if (event.keyCode == 13)
			{
				var linkhref = SearchBox.href;
				linkhref = linkhref.replace("id", "0");                
				window.location = linkhref;
			}
			else
			{
				searchResultsLayer.style.display = "block";               
				searchResultsLayer.style.left = getOffsetLeft(search) + "px";
				searchResultsLayer.style.top = (getposOffset(search, "top") + search.offsetHeight + 2) + "px";
				searchResultsLayer.style.zindex = 800000;

				if (Langage == "En")
				{
					document.getElementById("ItemSearchResultsLayer").innerHTML = "No result found<hr />" + SearchBox.RenderLink("0", "Reset search");
				}
				else
				{
					document.getElementById("ItemSearchResultsLayer").innerHTML = "Aucun résultat<hr />" + SearchBox.RenderLink("0", "Autre recherche");
				}
			}
		}
	}
	if (search.value.length == 0)
	{
		searchResultsLayer.style.display = "none";
	}
}

function getposOffset(obj, offsettype){
    var totaloffset = (offsettype=="left") ? obj.offsetLeft : obj.offsetTop;
    var parentEl = obj.offsetParent;
    while (parentEl != null){
        totaloffset = (obj=="left") ? totaloffset+obj.offsetLeft : totaloffset + parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }
    return totaloffset;
}
        
function getOffsetLeft(elm) {
    var mOffsetLeft = elm.offsetLeft;
    var mOffsetParent = elm.offsetParent;

    while(mOffsetParent) {
        mOffsetLeft += mOffsetParent.offsetLeft;
        mOffsetParent = mOffsetParent.offsetParent;
    }

    return mOffsetLeft;
}

function SearchBoxEngine()
{
    this.List = new Array();
    this.href = "/iiix/shop/search/";
    this.searchPage = "/ext/js/itemsearchbox.asp?search=";
    this.searchFragment = "";
    
    this.Refresh = function(searchFragment)
    {
        if (searchFragment.length == 3 || searchFragment.indexOf(this.searchFragment) == -1)
        {
            xmlhttp = getHTTPObject();
            xmlhttp.open("GET", this.searchPage + searchFragment, false); 
            xmlhttp.send(null); 

            this.List = new Array();
            eval(xmlhttp.responseText);
        }
        this.searchFragment = searchFragment;
    }
    
    this.SearchItem = function(textSearch)
    {
		var itemsFound = new SearchBoxEngine();
		var searchExp = new RegExp(textSearch, "gi");
		var index = 0;
		
		if (this.List.length != 0)
		{
			for (index=0;index<this.List.length;index++)
			{
				itemMatch = (this.List[index].name.search(searchExp) > -1);
				if (itemMatch || this.List[index].type == "categorie")
				{
					itemsFound.List.push(new Item(this.List[index].type, this.List[index].code, FindAndMakeBold(this.List[index].name, textSearch)));
				}
			}
		}

		return itemsFound;
    }
    
    this.Render = function()
    {
		var item = 0;
        renderText = "";

        for (item=0;item<this.List.length;item++)
        {
            renderText = renderText + "<div>" + this.RenderLink(this.List[item].type, this.List[item].code, this.List[item].name) + "</div>";
        }

        if (Langage == "En")
        {
          renderText = renderText + this.RenderLink("", "0", "<hr />Other location");
        }
        else
        {
          renderText = renderText + this.RenderLink("", "0", "<hr />Autre recherche");
        }
        return renderText;
    }
    
    this.RenderLink = function(type, id, text)
    {
        //link = this.href + type + "/";
		//link = this.href
		//link = link.replace("/iiix/shop/search/", "/iiix/")
		//link = this.href + "/iiix/shop/products/details/?id=idItem";
	//	link = "/iiix/shop/products/details/?id=idItem";
    //    link = link.replace("idItem", id);

    //    return "<a href=\"" + link + "\">" + text + "</a>"; 
		
		
		if (type == "categorie")
		{
		link = "/iiix/shop/search/cleCAT/?id=idItem";
        link = link.replace("idItem", id);
		return "<a href=\"" + link + "\">" + text + "</a>"; 
		}
		else if (type == "produit")
		{
		link = "/iiix/shop/products/details/?id=idItem";
        link = link.replace("idItem", id);
		return " &nbsp; - <a href=\"" + link + "\">" + text + "</a>"; 
		}
		else
		{
		link = this.href
	//	return "<a href=\"" + link + "\">" + text + "</a>"; 
		//return "<a href=\"" + link + "\">" + 'Cherchez dans notre catalogue' + "</a>"; 
				if (text == undefined)
				{return "<a href=\"" + link + "\">" + "Autre recherche" + "</a>";}
				else 
				{return "<a href=\"" + link + "\">" + text + "</a>";}
		}

        //return "<a href=\"" + link + "\">" + text + "</a>"; 
    }
}

function Item(type, code, name)
{
    this.type = type;
    this.code = code;
    this.name = name;
}

function FindAndMakeBold(text, textSearch)
{
    var result = "";
    
    searchExp = new RegExp(textSearch, "gi");
    
    if (text.indexOf(textSearch) == 0)
		return text;
    
    while (text.search(searchExp) > -1)
    {
        result = result + text.substr(0, text.search(searchExp));
        result = result + "<b>" + text.substr(text.search(searchExp), textSearch.length) + "</b>";
        text = text.substr(text.search(searchExp)+textSearch.length);
    }
    result = result + text;
    
    return result;
}

function getHTTPObject()
{
  var xmlhttp = false;

  /* Compilation conditionnelle d'IE */
  /*@cc_on
  @if (@_jscript_version >= 5)
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end @*/

  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }

  if (xmlhttp)
  {
     /* on définit ce qui doit se passer quand la page répondra */
     xmlhttp.onreadystatechange=function()
     {
        if (xmlhttp.readyState == 4) /* 4 : état "complete" */
        {
           if (xmlhttp.status == 200) /* 200 : code HTTP pour OK */
           {
              /*
              Traitement de la réponse.
              Ici on affiche la réponse dans une boîte de dialogue.
              */
              //alert(xmlhttp.responseText);
           }
        }
     }
  }
  return xmlhttp;
}
        
var SearchBox = new SearchBoxEngine();
var Langage = "Fr";