/*
AJAX a procedury pro podobnostní vyhledávání
spolupracuje s knihovnou class/plosina.class.php
autor Vladimír Kapic obchod@kapic.cz
napsáno na zakázku VLADO studio vlado@vlado.cz
*/

var kap_domena='http://dev.rothlehner.cz';
var kap_xmlHttp = kap_createXmlHttpRequestObject();
var kap_showErrors = false;

// konstruktor a procedury objektu xmlhttprequest
function kap_createXmlHttpRequestObject()
{
	var kap_xmlHttp;
	try {
		kap_xmlHttp = new XMLHttpRequest();
	}
	catch(e) {
		var kap_XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
		'MSXML2.XMLHTTP.5.0',
		'MSXML2.XMLHTTP.4.0',
		'MSXML2.XMLHTTP.3.0',
		'MSXML2.XMLHTTP',
		'Microsoft.XMLHTTP');
		for (var i = 0; i < kap_XmlHttpVersions.length && !kap_xmlHttp; i++) {
			try {
				kap_xmlHttp = new ActiveXObject(kap_XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	if (!kap_xmlHttp)
		kap_dispErr("ERR");
	else
		return kap_xmlHttp;
}

function kap_handleRequestStateChange()
{
	// je-li readyState 4, můžeme přečíst odpověď serveru
	if (kap_xmlHttp.readyState == 4)
	{
		// pokračuj pouze, je-li stav HTTP "OK"
		if (kap_xmlHttp.status == 200){
			try{
				kap_handleServerResponse();
			}
			catch(e){
				kap_dispErr("Error reading the response 1: " + e.toString());
			}
		} 
		else {
		// zobraz zprávu o stavu
			kap_dispErr("There was a problem retrieving the data 2:\n" + kap_xmlHttp.statusText);
		}
	}
}

function kap_dispErr($message){
      if(kap_showErrors){
            alert($message);
      }
}

function kap_handleServerResponse()
{
	var kap_xmlResponse = kap_xmlHttp.responseXML;
	// zachycení možných chyb u IE a Opery
	if (!kap_xmlResponse || !kap_xmlResponse.documentElement)
	throw("Structure XML ERROR: IE\n" + kap_xmlHttp.responseText);
	var rootNodeName = kap_xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") throw("Structure XML ERROR: FF");
	
	var xmlRoot=kap_xmlResponse.documentElement;
	var typArray=xmlRoot.getElementsByTagName("typek");
	for (var i=0; i<typArray.length; i++) {
		var typ=typArray.item(i).firstChild.data;
      	if(i==0) var co=String(typ);
      	if(i==1){
      		if(co=="fuzzy"){
      			document.getElementById("pvni_nadpis_det_plos").innerHTML=URLDecode(String(typ));
			}			
		}
      	if(i==2){
      		if(co=="fuzzy"){
      			var tabulka=URLDecode(String(typ)); 
			}
		}
      	if(i==3){
      		if(co=="fuzzy"){
      			document.getElementById("velky_obr").innerHTML=URLDecode(String(typ));
			}
		}
      	if(i==4){
      		if(co=="fuzzy"){
      			document.getElementById("vnitrnicast").innerHTML=URLDecode(String(typ));
			}
		}
      	if(i==5){
      		if(co=="fuzzy"){
      			document.getElementById("append_file").innerHTML=URLDecode(String(typ));
			}
		}
      	if(i==6){
      		if(co=="fuzzy"){
      			document.getElementById("nejcastejsi_det_plos").innerHTML=URLDecode(String(typ));
			}
		}
      	if(i==7){
      		if(co=="fuzzy"){
      			document.getElementById("det_pop_det_plos").innerHTML=URLDecode(String(typ));
			}
		}
      	if(i==8){
      		if(co=="fuzzy"){
      			document.getElementById("box1_det_plos").innerHTML=URLDecode(String(typ));
			}
		}
      	if(i==9){
      		if(co=="fuzzy"){
      			document.getElementById("box2_det_plos").innerHTML=URLDecode(String(typ));
			}
		}
      	if(i==10){
      		if(co=="fuzzy"){
      			document.getElementById("box3_det_plos").innerHTML=URLDecode(String(typ));
			}
		}
      	if(i==11){
      		if(co=="fuzzy"){
      			document.getElementById("box4_det_plos").innerHTML=URLDecode(String(typ));
			}
		}
      	if(i==12){
      		if(co=="fuzzy"){
      		tabulka+= URLDecode(String(typ));
			}
		}
      	if(i==13){
      		if(co=="fuzzy"){
      		tabulka+= URLDecode(String(typ));
			}
		}
      	if(i==14){
      		if(co=="fuzzy"){
      		tabulka+= URLDecode(String(typ));
			}
		}
   	}
   	tabulka.replace('\n\r ','');
      tabulka.replace('\r\n ','');
   	document.getElementById("hledani_plosiny_detail").innerHTML=tabulka;
}

function URLDecode(psEncodeString)
{
  var vystup= decodeURI(psEncodeString);
  vystup=String(vystup).replace(/\+/g, " ");
  vystup=String(vystup).replace(/%26/g, "&");
  vystup=String(vystup).replace(/%40/g, "@");
  vystup=String(vystup).replace(/%2C/g, ",");
  vystup=String(vystup).replace(/%2F/g, "/");
  vystup=String(vystup).replace(/%3A/g, ":");
  vystup=String(vystup).replace(/%3B/g, ";");
  vystup=String(vystup).replace(/%3D/g, "=");
  vystup=String(vystup).replace(/%3F/g, "?");
  vystup=String(vystup).replace(/%2B/g, "+");

  return vystup;
}

/*funkce odesílající požadavek*/
function fuzz(typ, hodnota, plosina){
      if (kap_xmlHttp) {
            try {
            	// fuzz 0 je -, 1 je + a 2 je asi, hdnota je pole tabulky a id je id stávající plošiny
                  kap_xmlHttp.open("GET", "detail-plosiny.php?fuzz="+typ+"&hodnota="+hodnota+"&id="+plosina, true);       
                  kap_xmlHttp.onreadystatechange = kap_handleRequestStateChange;
                  kap_xmlHttp.send(null);
            }

            catch (e) { 
                  kap_dispErr("Can't connect to server:\n" + e.toString()); 
            }
      }
}

