/** XHConn - Simple XMLHTTP Interface - brad@xkr.us - 2005-01-24             **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4) {
        fnDone(xmlhttp); }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function switchClass(obj, className) {
	obj.className = className
}

function jsSearch() {
	
	var myConn = new XHConn();
	if (!myConn) {
	  alert('Fel');
	}
	else {
		if(document.forms.Searchprod.search.value.length > 0){
			var fnWhenDone = function (oXML) { showresults(oXML.responseText); };
			myConn.connect("http://www.blekkpatron.no/html/printerSearch.asp", "GET", "search="+document.forms.Searchprod.search.value, fnWhenDone);
		}else{
			my_hide_div(my_getbyid('searchDiv'));
		}
	}
}

function showresults(results) {

	var myArray = results.split(',');
	var temp;
	var resultRow;
	var i, j;
	var iResult = myArray.length;
	
	 temp = myArray[0];
	 resultRow = temp.split('%');
	 if (typeof resultRow[1] == "undefined") {
	   for (j=0; j<1; j++) {
	     //document.getElementById('row_0_cell_'+j).innerHTML = "&nbsp;";
	   }
	   my_hide_div(my_getbyid('row_0'));
	   my_hide_div(my_getbyid('searchDiv'));
	   my_hide_div(my_getbyid('searchRubrik'));
	 }
	 else {

	   document.getElementById('row_0_cell_0').innerHTML = "<a class\=\"searchResult\" href=\"/html/default.asp?Productgroup="+ resultRow[0] +"&ProductMiddlegroup="+ resultRow[1] +"&printerId="+ resultRow[2] +"&a=spp\" title=\"Visa detaljerad information om "+ resultRow[3] +"\">"+resultRow[3]+"</a>";
	   //document.getElementById('row_0_cell_1').innerHTML = "<a href=\"http://www.prisjakt.nu/produkt.php?p="+resultRow[0]+"\" title=\"Visa priser\">"+resultRow[0]+":-</a>";
	   my_show_div(my_getbyid('row_0'));
	   my_show_div_block(my_getbyid('searchDiv'));
	   my_show_div(my_getbyid('searchRubrik'));
	   for (i=1; i<9; i++) {
	 		temp = myArray[i];
	 		if (typeof temp == "undefined") {
	   	  for (j=0; j<1; j++) {
	        //document.getElementById('row_'+i+'_cell_'+j).innerHTML = "&nbsp;";
	   		}
				my_hide_div(my_getbyid('row_'+i));
	 		}
	 		else {
	   		resultRow = temp.split('%');
	 		document.getElementById('row_'+i+'_cell_0').innerHTML = "<a class\=\"searchResult\" href=\"/html/default.asp?Productgroup="+ resultRow[0] +"&ProductMiddlegroup="+ resultRow[1] +"&printerId="+ resultRow[2] +"&a=spp\" title=\"Visa detaljerad information om "+resultRow[3]+"\">"+resultRow[3]+"</a>";
	   		//document.getElementById('row_'+i+'_cell_1').innerHTML = "<a href=\"http://www.prisjakt.nu/produkt.php?p="+resultRow[0]+"\" title=\"Visa priser\">"+resultRow[0]+":-</a>";
	 			my_show_div(my_getbyid('row_'+i));
	 		}

	 	 }
	 	 temp = myArray[9];
	 	 if (typeof temp == "undefined") {
	 	 	 my_hide_div(my_getbyid('row_16'));
	 	 }else {	 	
       resultRow = temp.split('%');
       
       document.getElementById('row_16_cell_0').innerHTML = "<center><a class\=\"searchResult\" href=\"/html/default.asp?Searchtype=Searchproduct&sok=S%F8k&a=search&search="+document.forms.Searchprod.search.value+"\">Visa alla "+ iResult +"</a></center>";
       my_show_div(my_getbyid('row_16'));
     }
	}

}