var minchr=2;
var srcprintertxt="Ex : « HP 2110 »";
var srccarttxt="Ex : « 3EBK »";
var waitimg="images/circle_animation.gif";
var autoscolldelay=200;
var wvalid=undefined;
var validtime=1000;
var lastsrc=undefined;

function initsrc(){
         if(document.getElementById('srcprinter')){
             document.getElementById('srcprinter').value=srcprintertxt;
             document.getElementById('srcprinter').style.color="#888888";
             document.getElementById('srccart').value=srccarttxt;
             document.getElementById('srccart').style.color="#888888";
         }
}

function printerWrite(){
        if(wvalid!=undefined) clearTimeout(wvalid);
        wvalid=setTimeout("printerRequest()",validtime);
}

function printerRequest(){
        document.getElementById('results').innerHTML="";
        var src=document.getElementById('srcprinter').value;
        if(src.length<minchr || src==srcprintertxt) return;
        lastsrc="printer";

        document.getElementById('results').innerHTML="<p align=\"center\"><img src=\""+waitimg+"\"></p>";
	document.getElementById('resultsbox').style.display='block';
	if(document.getElementById('prodlists')) document.getElementById('prodlists').style.display='none';
        var marque="";
        if(document.getElementById('fmarque').value!="Tout" && document.getElementById('fmarque').value!="") marque="&marque="+document.getElementById('fmarque').value;

        url="src_ajax.php?group=1&type=printer"+marque+"&src="+document.getElementById('srcprinter').value;

        ajaxrequest(url,printerCallback);
}

function printerCallback(httpRequest){

         //if(!httpRequest.responseXML.documentElement) return;
         var xml=httpRequest.responseXML.documentElement;
         var html="";
         var col=0;
         var nbresults=0;
         var src=document.getElementById('srcprinter').value;
         var printers=new Array();
         if(xml.getElementsByTagName("printer")[0]){

                 for(i=0;i<xml.getElementsByTagName("printer").length;i++){
                     colspan="";
                     printer=xml.getElementsByTagName("printer")[i].firstChild.nodeValue;
                     var tech=xml.getElementsByTagName("printer")[i].getAttribute("technology");
                       // alert(printer+" "+tech);
                     if(!printers[tech]) printers[tech]=new Array();
                     printers[tech].push("<td"+colspan+" width=\"350\"><li class=\"liresult\"><a href=\"cartouches-imprimante-"+stripurlchr(printer)+".html\">"+highlighttxt(printer,src)+"</a></li></td>");
                     //html+="<td"+colspan+" width=\"350\"><li class=\"liresult\"><a href=\"cartouches-imprimante-"+stripurlchr(printer)+".html\" class=\"pink11\">"+highlighttxt(printer,src)+"</a></td>";


                     nbresults++;
                 }

                 var t=0;

                 html+="<table width='100%' style=\"margin-left:22px;\"><tr>";
                 for (tech in printers){
                     if(t>0) html+="<tr><td colspan=\"2\" align=\"center\"><hr style=\"width:80%\"></td></tr>";
                     t++;
                     html+="<tr><td style=\"height:30px; padding-top:15px;\" class=\"black18\">Imprimantes "+tech.toLowerCase()+"</td></tr>";
                     for(i=0;i<printers[tech].length;i++){
                          html+=printers[tech][i];
                         col++;
                         if(col==2){
                             html+="</tr><tr>";
                             col=0;
                         }
                     }
                     html+="</tr>";
                 }
                 html+="</tr></table>";
         }

         if(xml.getElementsByTagName("near")[0]){
                 html+="<table width='100%'>";
                 html+="<tr><td colspan=\"2\" align=\"center\">Résultats proches</td></tr><tr>";
                 for(i=0;i<xml.getElementsByTagName("near").length;i++){
                     colspan="";
                     printer=xml.getElementsByTagName("near")[i].firstChild.nodeValue;


                     html+="<td"+colspan+" width=\"350\"><a href=\"cartouches-imprimante-"+stripurlchr(printer)+".html\">"+printer+"</a></td>";
                     col++;
                     if(col==2){
                         html+="</tr><tr>";
                         col=0;
                     }
                     nbresults++;
                 }
                 html+="</tr></table>";
         }
         html+="<p style=\"padding-left:10px;\"><a href=\"#asrc\" class=\"pink11\" align=\"center\">Retour recherche</a></p>";

         if(nbresults==0) html="<p align=\"center\" class=\"black13b\">Aucune correspondance</p><p align=\"center\">Votre cartouche ou votre imprimante n'est pas référencée ? <a href=\"/index.php?html=contact&type=src\">Cliquez ici</a></p>";

         document.getElementById('results').innerHTML=html;
         //document.location="#aresults";
        // setTimeout("autoscroll()",autoscolldelay);


}


function cartWrite(){
        if(wvalid!=undefined) clearTimeout(wvalid);
        wvalid=setTimeout("cartRequest()",validtime);
}

function cartRequest(){
        var src=document.getElementById('srccart').value;
        if(src=="" || src==srccarttxt) return;
        lastsrc="cartridge";
        document.getElementById('results').innerHTML="<p align=\"center\"><img src=\""+waitimg+"\"></p>";
	document.getElementById('resultsbox').style.display='block';
	if(document.getElementById('prodlists')) document.getElementById('prodlists').style.display='none';
        //if(document.getElementById('srccart').value.length<minchr) return;
        var marque="";
        if(document.getElementById('fmarque').value!="Tout" && document.getElementById('fmarque').value!="") marque="&marque="+document.getElementById('fmarque').value;
        url="src_ajax.php?group=1&type=cartridge&"+marque+"&src="+document.getElementById('srccart').value;
        ajaxrequest(url,cartridgeCallback);

}

function cartridgeCallback(httpRequest){
         xml=httpRequest.responseXML.documentElement;
         html="<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"resultslist\"><tr>";
         phtml="";
         shtml="";
         var groups=new Array();
         col=0;
	 var displayed=false;
         var resultnb=xml.getElementsByTagName("cartridge").length;
         var src=document.getElementById('srccart').value;

         if(xml.getElementsByTagName("cartridge")[0]){
                 for(i=0;i<xml.getElementsByTagName("cartridge").length;i++){
                     var cartridge=xml.getElementsByTagName("cartridge")[i].firstChild.nodeValue;
                     var id=xml.getElementsByTagName("cartridge")[i].getAttribute("id");
                     var pack=xml.getElementsByTagName("cartridge")[i].getAttribute("pack");
                     var manufacturer=xml.getElementsByTagName("cartridge")[i].getAttribute("manufacturer");
                     if(manufacturer=="") manufacturer="gen";

                     if(!groups[manufacturer.toLowerCase()+pack]) groups[manufacturer.toLowerCase()+pack]=new Array();
                     groups[manufacturer.toLowerCase()+pack].push("<td style=\"text-align:right; width:100px; height:20px; padding-right:5px;\"><img src=\"coloricon.php?color="+xml.getElementsByTagName("cartridge")[i].getAttribute("color")+"&type=2\" border=\"0\" align=\"absmiddle\"></td><td style=\"padding-right:5px;text-align:left;\"><a href=\"index.php?article="+id+"&src="+src+"\" onmouseover=\"cartPreview('"+id+"')\" onmouseout=\"cartPHide()\">"+nl2br(highlighttxt(cartridge,src))+"</a></td>");
		     
                 }

                 if(groups['gen1']){
		 displayed=true;
                 html+="<h3>Packs et lots garantis 1 an</h3><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"resultslist\"><tr>";
                 col=0;
                 for(i=0;i<groups['gen1'].length;i++){
                      html+=groups['gen1'][i];
                      col++;
                     if(col==2){
                         html+="</tr><tr>";
                         col=0;
                     }
                 }
                 html+="</tr></table>";
                 }

		if(groups['gen0']){
		 if(displayed) html+="<hr style=\"width:80%\">";
                 html+="<h3>Cartouches à l'unité garanties 1 an</h3><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"resultslist\"><tr>";
                 col=0;
                 for(i=0;i<groups['gen0'].length;i++){
                      html+=groups['gen0'][i];
                      col++;
                     if(col==2){
                         html+="</tr><tr>";
                         col=0;
                     }
                 }
                 html+="</tr></table>";
                 }
                 if(groups['uprint1']){
		 if(displayed) html+="<hr style=\"width:80%\">";
                 html+="<h3>Packs et lots garantis 3 ans</h3><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"resultslist\"><tr>";
                 col=0;
                 for(i=0;i<groups['uprint1'].length;i++){
                      html+=groups['uprint1'][i];
                      col++;
                     if(col==2){
                         html+="</tr><tr>";
                         col=0;
                     }
                 }
                 html+="</tr></table>";
                 }
                 if(groups['uprint0']){
		 if(displayed) html+="<hr style=\"width:80%\">";
                 html+="<h3>Cartouches à l'unité garanties 3 ans</h3><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"resultslist\"><tr>";
                 col=0;
                 for(i=0;i<groups['uprint0'].length;i++){
                      html+=groups['uprint0'][i];
                      col++;
                     if(col==2){
                         html+="</tr><tr>";
                         col=0;
                     }
                 }
                 html+="</tr></table>";
                 }



                 html+="<p style=\"margin-left:10px;\"><a href=\"#asrc\" class=\"pink11\" align=\"center\">Retour recherche</a></p>";
                 //html="<div>Pack uprint</div>"+groups['uprint1']+"<div>Packs génériques</div>"+groups['gen1']+"<div>Cartouches uprint</div>"+groups['uprint0']+"<div>Cartouches génériques</div>"+groups['gen0']
         }else{
             html="<p align=\"center\" class=\"black13b\">Aucune correspondance</p><p align=\"center\">Votre cartouche ou votre imprimante n'est pas référencée ? <a href=\"/index.php?html=contact&type=src\">Cliquez ici</a></p>";
         }
         document.getElementById('results').innerHTML=html;
         //document.location="#aresults";
         //setTimeout("autoscroll()",autoscolldelay);
}

function clearPrinterSrc(){
         if(document.getElementById('srcprinter').value=srcprintertxt){
             document.getElementById('srcprinter').value="";
             document.getElementById('srcprinter').style.color="black";
         }
         if(document.getElementById('srccart').value!=srccarttxt){
             document.getElementById('srccart').value=srccarttxt;
             document.getElementById('srccart').style.color="#888888";
         }
}

function clearCartridgeSrc(){
         if(document.getElementById('srccart').value=srccarttxt){
             document.getElementById('srccart').value="";
             document.getElementById('srccart').style.color="black";
         }
         if(document.getElementById('srcprinter').value!=srcprintertxt){
             document.getElementById('srcprinter').value=srcprintertxt;
             document.getElementById('srcprinter').style.color="#888888";
         }
}

function definemanufacturer(){
         switch(lastsrc){
         case "printer":
         printerRequest();
         break;

         case "cartridge":
         cartRequest();
         break;

         case undefined:
         return;
         }
}


function autoscroll(){
         aspos=document.documentElement.scrollTop;
         windowsize();
         var maxscroll=docheight-winheight;
         if(aspos<maxscroll && aspos<300){
             document.documentElement.scrollTop=aspos+10;
             setTimeout("autoscroll()",50);
         }
}

function getmanufacturers(){

         document.getElementById('results').innerHTML="";

         var technology=document.getElementById('type').value;

         selectman=document.getElementById('marque');
         selectman.options.length=0;
         selectman.options[0]=new Option('','');

         document.getElementById('serie').options.length=0;

         url="src_ajax.php?type=manufacturers&technology="+technology+"&src=";
         ajaxrequest(url,manufacturersCallback);

}

function manufacturersCallback(httpRequest){
        xml=httpRequest.responseXML.documentElement;

        if(xml.getElementsByTagName("manufacturer")[0]){
                var marque="";
                var strman="";
                selectman=document.getElementById('marque');
                selectman.options.length=0;
                selectman.options[0]=new Option('','');

                var tman=new Array();
                for(i=0;i<xml.getElementsByTagName("manufacturer").length;i++){
                     marque=xml.getElementsByTagName("manufacturer")[i].firstChild.nodeValue;
                     tman[i]=marque;
                     strman+=','+marque;
                 }

                for(i=0;i<topmanufacturers.length;i++)
                     if(strman.indexOf(topmanufacturers[i])>=0)
                         selectman.options[selectman.options.length]=new Option(topmanufacturers[i],topmanufacturers[i]);

                 selectman.options[selectman.options.length]=new Option("-----------------------","");

                for(i=0;i<tman.length;i++)
                    selectman.options[selectman.options.length]=new Option(tman[i],tman[i]);

        }

}

function getseries(){
         printerRequest();
         document.getElementById('results').innerHTML="";
         var marque=document.getElementById('marque').value;
         if(marque=="Tout" || marque=="") return;

         var technology=document.getElementById('type').value;
         if(technology!="") technology="&technology="+technology;

         selectserie=document.getElementById('serie');
         selectserie.options.length=0;
         selectserie.options[0]=new Option('','');

         url="src_ajax.php?type=series&src="+marque+technology;
         ajaxrequest(url,seriesCallback);

}

function seriesCallback(httpRequest){
        xml=httpRequest.responseXML.documentElement;

        if(xml.getElementsByTagName("serie")[0]){
                var serie="";
                selectserie=document.getElementById('serie');
                selectserie.options.length=0;
                selectserie.options[0]=new Option('','');
                 for(i=0;i<xml.getElementsByTagName("serie").length;i++){
                     serie=xml.getElementsByTagName("serie")[i].firstChild.nodeValue;
                     selectserie.options[selectserie.options.length] = new Option(serie,serie);
                 }
        }

}

function serieChange(){

            var marque="";
            if(document.getElementById('marque').value!="Tout" && document.getElementById('marque').value!="")
                   marque="&marque="+document.getElementById('marque').value;
            else
                return;

            var technology=document.getElementById('type').value;
            if(technology!=""){
                technology="&technology="+technology;
            }else
                return;

            var serie=document.getElementById('serie').value;
            if(serie!=""){
                serie="&serie="+serie;
            }/*else{
                return;
            }  */


            document.getElementById('results').innerHTML="<p align=\"center\"><img src=\""+waitimg+"\"></p>";
	    document.getElementById('resultsbox').style.display='block';
	    if(document.getElementById('prodlists')) document.getElementById('prodlists').style.display='none';
            url="src_ajax.php?group=1&type=serie"+marque+"&src="+technology+serie;
            ajaxrequest(url,printerCallback);

}

function stripurlchr(url){
        var reg=new RegExp("/","g");
        return url.replace(reg,"--");
}

function initpreview(){
         var dcal=document.createElement('div');
         dcal.setAttribute('id','cpreview');
         dcal.onclick=function(){ cartPHide2(); }
         dcal.style.visibility='hidden';
         dcal.style.position='absolute';
         document.body.appendChild(dcal);
}

function cartPreview(id){
         wvalid=setTimeout("cartPreview2('"+id+"')",500);
}

function cartPreview2(id){
         windowsize()
        if(wvalid!=undefined) clearTimeout(wvalid);
        if(!document.getElementById('cpreview')) initpreview();
        pdiv=document.getElementById('cpreview');
        pdiv.style.backgroundColor='white';
        pdiv.innerHTML="";
        pdiv.style.visibility='hidden';
        pdiv.style.left=(Mouse_X+40)+"px";
        pdiv.style.top=(Mouse_Y+10)+"px";
        if(Mouse_X>280) pdiv.style.left=(winwidth/2-100)+'px';

        var url="src_ajax.php?type=preview&src="+id;
        ajaxrequest(url,previewcb);
}

function previewcb(httpRequest){
        xml=httpRequest.responseXML.documentElement;
        var html="<table width=\"450\" height=\"200\" style=\"border:solid thin black;\" class=\"black11\">";

        var id=xml.getElementsByTagName("id")[0].firstChild.nodeValue;
        var description=xml.getElementsByTagName("description")[0].firstChild.nodeValue;
        var detail=xml.getElementsByTagName("detail")[0].firstChild.nodeValue;
        var color=xml.getElementsByTagName("color")[0].firstChild.nodeValue;
        var capacity=xml.getElementsByTagName("capacity")[0].firstChild.nodeValue;
        var refaci=xml.getElementsByTagName("refaci")[0].firstChild.nodeValue;
        html+="<tr><td width=\"150\" align=\"center\" valign=\"top\"><img src=\"image.php?id="+id+"\"></td><td valign=\"top\" width=\"300\"><p>"+detail+"<br>"+nl2br(description)+"</p>";
        html+="<p>Couleur : <img src=\"coloricon.php?color="+color+"&type=2\" align=\"absmiddle\"></p>";
        html+="<p>Capacité : "+capacity+"</p>";
        html+="<p>Réf. Easy : "+refaci+"</p>";
        html+="</td></tr></table>";

        document.getElementById('cpreview').innerHTML=html;

        document.getElementById('cpreview').style.visibility='visible';
}

function showhelp(id){
        if(wvalid!=undefined) clearTimeout(wvalid);
        if(!document.getElementById('cpreview')) initpreview();
        pdiv=document.getElementById('cpreview');
        pdiv.style.backgroundColor='white';
        pdiv.style.visibility='hidden';
        pdiv.style.left=(Mouse_X-200)+"px";
        pdiv.style.top=(Mouse_Y+40)+"px";
        pdiv.style.zOrder=10;
        pdiv.innerHTML='';

        var url="src_ajax.php?type=help&src="+id;
        ajaxrequest(url,helpcb);
}

function helpcb(httpRequest){
         var html="<table style=\"border: solid thin #7b0651;\" class=\"black11\">";
         html+="<tr><td><img src=\"images/help.png\"></td><td>"+httpRequest.responseText+"</td></tr>";
         html+="</table>";

         pdiv=document.getElementById('cpreview');
         pdiv.style.visibility='visible';
         pdiv.innerHTML=html;

}

function cartPHide(){
        clearTimeout(wvalid);
        if(document.getElementById('cpreview') && document.getElementById('cpreview').style.visibility=='hidden') return false;
        wvalid=setTimeout("cartPHide2()",500);
}

function cartPHide2(){
         wvalid=undefined;
        if(document.getElementById('cpreview')) document.getElementById('cpreview').style.visibility='hidden';
}
function closesearchbox(){
	 document.getElementById('resultsbox').style.display='none';
	if(document.getElementById('prodlists')) document.getElementById('prodlists').style.display='block';
}

function highlighttxt(txt,src){
         if(src=="") return txt;
         var ret=txt;
         pos=txt.indexOf(src);
         if(pos==-1) return txt;

         while(pos>-1){
             ret=ret.substr(0,pos)+"<b>"+src+"</b>"+ret.substr(pos+src.length);
             pos=ret.indexOf(ret,pos+src.length);
         }

         return ret;
}
