// JavaScript Document
var test = false;
if(test) {
	var dataPath = "http://krystof-shop.cz";
}
else {
	var dataPath = "http://www.hracky-krystof.cz";
}

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject() {	
	// zde bude reference na objek XMLHttpRequest
	var xmlHttp;
	// toto by melo fungovat ve vsech prohlizecich mimo IE6 a starsi
	try 
	{
		// pokusi se vyvorit objekt XMLHttpRequest
		xmlHttp = new XMLHttpRequest();		
	}
	catch (e) 
	{
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
										'MSXML2.XMLHTTP.5.0',
										'MSXML2.XMLHTTP.4.0',
										'MSXML2.XMLHTTP.3.0',
										'MSXML2.XMLHTTP',
										'Microsoft.XMLHTTP');
		//vyzkousej vsechna prog id, dokud nektere nebude funkcni
		for(var i=0; i < XmlHttpVersions.lenght && !xmlHttp; i++) {
			try {
				//pokusi se vytvorit objekt XmlHttpRequest
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e) {}
		}
	}
	// vrati vytvoreny objekt nebo zobrazi chybovou zpravu
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else 
		return xmlHttp;
}

function process1() 
{
	//pokracuj jen neni-li xmlHttp prazdne
	if(xmlHttp) 
	{
		try 
		{
			//pokus se pripojit k serveru
			var value1 = document.getElementById("hlavni_kategorie").value;
			if(value1 != 0)
			{
			  //vytvori retezec parametru
			  var params = "value1=" + value1;
			  var path = dataPath + "/ajax/podkategorie.php?" + params;
			  //inicijuje asynchronni pozadavek HTTP
			  xmlHttp.open("GET", path, true);
			  
			  xmlHttp.onreadystatechange = handleRequestStateChange1;
			  
			  xmlHttp.send(null);
			}
			else
			{
			  //generuje vystup HTML
			  var html="<select name=\"podkategorie\" id=\"podkategorie\">";
			  html += "<option value='0' selected='selected'>podkategorie</option>";
			  html += "</select>";
			  //ziska odkaz na element <div> na strance		
			  myDiv = document.getElementById("divPodKategorie");
			  
			  //pridej obsah do elementu <div>
			  myDiv.innerHTML = html;	
			  process2();
			  	  
			}
			
		}
		// v pripade selhani zobraz chybu
		catch(e) 
		{
			alert("Can`t connect to server: \n" + e.toString());
		}
	}
}

function handleRequestStateChange1() 
{
	//je li readyState 4 muzeme precist odpoved ze serveru
	if(xmlHttp.readyState == 4) 
	{		
		//pokracuj pouze, je-li stav HTTP "OK"
		if(xmlHttp.status == 200) 
		{			
			try 
			{
				//zpracuj odpoved ze serveru
				handleServerResponse1();
			}
			catch(e) 
			{
				//zobraz chybovou zpravu
				alert ("Error reading the response: " + e.toString());
			}
		}
		else 
		{
			// zobraz zpravu o stavu
			alert("There was a problem retrieving the data: \n" + xmlHttp.statusText);
		}
	}
}

function handleServerResponse1() 
{
	//precte zpravu ze serveru
	var xmlResponse = xmlHttp.responseXML;
	
	//ziska element dokumentu XML
	xmlRoot = xmlResponse.documentElement;
	
	// ziska pole se jmeny knih a jejich  ISBN
	nazevArray = xmlRoot.getElementsByTagName("nazev");
	valueArray = xmlRoot.getElementsByTagName("pozice");
	//autorArray = xmlRoot.getElementsByTagName("autor");
	
	var sendedPodKategorie = document.getElementById("sendedPodKategorie").value;
	
	//generuje vystup HTML
	var html="<select name=\"podkategorie\" id=\"podkategorie\" onchange=\"process4();\">";
	html += "<option value='0' selected='selected'>podkategorie</option>";
	
	// prochazi polem a tvori strukturu HTML
	for(var i=0; i < nazevArray.length; i++) 
	{
	  if(valueArray.item(i).firstChild.data == sendedPodKategorie) {
		html += "<option value='" + valueArray.item(i).firstChild.data + "' selected=\"selected\">" + nazevArray.item(i).firstChild.data + "</option>";  
	  }
	  else {		  
	  	html += "<option value='" + valueArray.item(i).firstChild.data + "'>" + nazevArray.item(i).firstChild.data + "</option>";
	  }
	}
	html += "</select>";
	//ziska odkaz na element <div> na strance		
	myDiv = document.getElementById("divPodKategorie");
	
	//pridej obsah do elementu <div>
	myDiv.innerHTML = html;	
	
	process2();
}

function process2() 
{
	//pokracuj jen neni-li xmlHttp prazdne
	if(xmlHttp) 
	{
		try 
		{
		  //pokus se pripojit k serveru
		  var value1 = document.getElementById("hlavni_kategorie").value;
		  //vytvori retezec parametru
		  var params = "value1=" + value1;
		  var path = dataPath + "/ajax/znacka.php?" + params;
		  //inicijuje asynchronni pozadavek HTTP
		  xmlHttp.open("GET", path, true);

		  xmlHttp.onreadystatechange = handleRequestStateChange2;
		  
		  xmlHttp.send(null);	
		}
		// v pripade selhani zobraz chybu
		catch(e) 
		{
			alert("Can`t connect to server: \n" + e.toString());
		}
	}
}

function handleRequestStateChange2() 
{
	//je li readyState 4 muzeme precist odpoved ze serveru
	if(xmlHttp.readyState == 4) 
	{		
		//pokracuj pouze, je-li stav HTTP "OK"
		if(xmlHttp.status == 200) 
		{			
			try 
			{
				//zpracuj odpoved ze serveru
				handleServerResponse2();
			}
			catch(e) 
			{
				//zobraz chybovou zpravu
				alert ("Error reading the response: " + e.toString());
			}
		}
		else 
		{
			// zobraz zpravu o stavu
			alert("There was a problem retrieving the data: \n" + xmlHttp.statusText);
		}
	}
}

function handleServerResponse2() 
{
	//precte zpravu ze serveru
	var xmlResponse = xmlHttp.responseXML;
	
	//ziska element dokumentu XML
	xmlRoot = xmlResponse.documentElement;
	
	// ziska pole se jmeny knih a jejich  ISBN
	nazevArray = xmlRoot.getElementsByTagName("nazev");
	valueArray = xmlRoot.getElementsByTagName("pozice");
	//autorArray = xmlRoot.getElementsByTagName("autor");
	
	//generuje vystup HTML
	var html="<select name=\"znacky\" id=\"znacky\" onchange=\"process6();\">";
	html += "<option value='0' selected='selected'>všechny značky</option>";
	
	var sendedZnacky = document.getElementById("sendedZnacky").value;
	
	// prochazi polem a tvori strukturu HTML
	for(var i=0; i < nazevArray.length; i++) 
	{
	  if(sendedZnacky == valueArray.item(i).firstChild.data) {
		html += "<option value='" + valueArray.item(i).firstChild.data + "' selected=\"selected\">" + nazevArray.item(i).firstChild.data + "</option>";
	  }
	  else {
	  	html += "<option value='" + valueArray.item(i).firstChild.data + "'>" + nazevArray.item(i).firstChild.data + "</option>";	
	  }
	}
	html += "</select>";
	//ziska odkaz na element <div> na strance		
	myDiv = document.getElementById("divZnacky");
	
	//pridej obsah do elementu <div>
	myDiv.innerHTML = html;
	
	process3();
}

function process3() 
{
	//pokracuj jen neni-li xmlHttp prazdne
	if(xmlHttp) 
	{
		try 
		{
			//pokus se pripojit k serveru
		  var value1 = document.getElementById("hlavni_kategorie").value;
		  //vytvori retezec parametru
		  var params = "value1=" + value1;
		  var path = dataPath + "/ajax/licence.php?" + params;
		  //inicijuje asynchronni pozadavek HTTP
		  xmlHttp.open("GET", path, true);
		  xmlHttp.onreadystatechange = handleRequestStateChange3;
		  xmlHttp.send(null);
			
		}
		// v pripade selhani zobraz chybu
		catch(e) 
		{
			alert("Can`t connect to server: \n" + e.toString());
		}
	}
}

function handleRequestStateChange3() 
{
	//je li readyState 4 muzeme precist odpoved ze serveru
	if(xmlHttp.readyState == 4) 
	{		
		//pokracuj pouze, je-li stav HTTP "OK"
		if(xmlHttp.status == 200) 
		{			
			try 
			{
				//zpracuj odpoved ze serveru
				handleServerResponse3();
			}
			catch(e) 
			{
				//zobraz chybovou zpravu
				alert ("Error reading the response: " + e.toString());
			}
		}
		else 
		{
			// zobraz zpravu o stavu
			alert("There was a problem retrieving the data: \n" + xmlHttp.statusText);
		}
	}
}

function handleServerResponse3() 
{
	//precte zpravu ze serveru
	var xmlResponse = xmlHttp.responseXML;
	
	//ziska element dokumentu XML
	xmlRoot = xmlResponse.documentElement;
	
	// ziska pole se jmeny knih a jejich  ISBN
	nazevArray = xmlRoot.getElementsByTagName("nazev");
	valueArray = xmlRoot.getElementsByTagName("pozice");
	//autorArray = xmlRoot.getElementsByTagName("autor");
	
	//generuje vystup HTML
	var html="<select name=\"licence\" id=\"licence\">";
	html += "<option value='0' selected='selected'>všechny licence</option>";
	var sendedLicence = document.getElementById("sendedLicence").value;
	
	// prochazi polem a tvori strukturu HTML
	for(var i=0; i < nazevArray.length; i++) 
	{
	  if(sendedLicence == valueArray.item(i).firstChild.data) {
		html += "<option value='" + valueArray.item(i).firstChild.data + "' selected=\"selected\">" + nazevArray.item(i).firstChild.data + "</option>";
	  }
	  else {
	  	html += "<option value='" + valueArray.item(i).firstChild.data + "'>" + nazevArray.item(i).firstChild.data + "</option>";	
	  }
	}
	html += "</select>";
	//ziska odkaz na element <div> na strance		
	myDiv = document.getElementById("divLicence");
	
	//pridej obsah do elementu <div>
	myDiv.innerHTML = html;	
}

function process4()
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // retrieve the name typed by the user on the form
    name = encodeURIComponent(document.getElementById("reg_login").value);
    // execute the quickstart.php page from the server
    if(name.length > 0) {
    var path = dataPath + "/ajax/znacka.php?name=" + name;
	xmlHttp.open("GET", path, true);  
	//alert(name);
	}
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse4;
    // make the server request
    xmlHttp.send(null);
  }
}

// executed automatically when a message is received from the server
function handleServerResponse4() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
      helloMessage = xmlDocumentElement.firstChild.data;
      // update the client display using the data received from the server
      document.getElementById("divMessage").innerHTML = 
                                            '<i>' + helloMessage + '</i>';
      // restart sequence
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

function zobrzeniCenyDopravy()
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // retrieve the name typed by the user on the form
    var doprava = document.getElementById("doprava").value;
	//var VyrobekCenaCelkem = parseFloat(document.getElementById("VyrobekCenaCelkem").value);
	var VyrobekCenaCelkem = document.getElementById("VyrobekCenaCelkem").value;
	VyrobekCenaCelkem = parseFloat(VyrobekCenaCelkem);
	
	if(VyrobekCenaCelkem > 3499) {
      cenaDopravy = 0;
	  VyrobekCenaCelkem = VyrobekCenaCelkem + cenaDopravy;
	  VyrobekCenaCelkem = formatNumber(VyrobekCenaCelkem,2,' ','.','','','-','');
      // update the client display using the data received from the server
	  document.getElementById("DoplnCenu").innerHTML = "<strong>" + cenaDopravy + ",- Kč</strong>";
	  document.getElementById("cenaCelkem").innerHTML = "<strong>" + VyrobekCenaCelkem + ",- Kč</strong>";		
	}
	else {
    // execute the quickstart.php page from the server
	xmlHttp.open("GET", "../ajax/cenaDopravy.php?doprava=" + doprava, true);  
	// define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponseZobrzeniCenyDopravy;
    // make the server request
    xmlHttp.send(null);
	}
  }
}

// executed automatically when a message is received from the server
function handleServerResponseZobrzeniCenyDopravy() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
      cenaDopravy = parseFloat(xmlDocumentElement.firstChild.data);
	  var VyrobekCenaCelkem = parseFloat(document.getElementById("VyrobekCenaCelkem").value);
	  VyrobekCenaCelkem = VyrobekCenaCelkem + cenaDopravy;
	  VyrobekCenaCelkem = formatNumber(VyrobekCenaCelkem,2,' ','.','','','-','');
      // update the client display using the data received from the server
	  document.getElementById("DoplnCenu").innerHTML = "<strong>" + cenaDopravy + ",- Kč</strong>";
	  document.getElementById("cenaCelkem").innerHTML = "<strong>" + VyrobekCenaCelkem + ",- Kč</strong>";
      
      // restart sequence
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}


function process4() 
{
	//pokracuj jen neni-li xmlHttp prazdne
	if(xmlHttp) 
	{
		try 
		{
		  //pokus se pripojit k serveru
		  var value1 = document.getElementById("podkategorie").value;
		  var value2 = document.getElementById("hlavni_kategorie").value;
		  //vytvori retezec parametru
		  var params = "value1=" + value1 + "&value2=" + value2;
		  var path = dataPath + "/ajax/znacka2.php?" + params;
		  //inicijuje asynchronni pozadavek HTTP
		  xmlHttp.open("GET", path, true);

		  xmlHttp.onreadystatechange = handleRequestStateChange4;
		  
		  xmlHttp.send(null);	
		}
		// v pripade selhani zobraz chybu
		catch(e) 
		{
			alert("Can`t connect to server: \n" + e.toString());
		}
	}
}

function handleRequestStateChange4() 
{
	//je li readyState 4 muzeme precist odpoved ze serveru
	if(xmlHttp.readyState == 4) 
	{		
		//pokracuj pouze, je-li stav HTTP "OK"
		if(xmlHttp.status == 200) 
		{			
			try 
			{
				//zpracuj odpoved ze serveru
				handleServerResponse4();
			}
			catch(e) 
			{
				//zobraz chybovou zpravu
				alert ("Error reading the response: " + e.toString());
			}
		}
		else 
		{
			// zobraz zpravu o stavu
			alert("There was a problem retrieving the data: \n" + xmlHttp.statusText);
		}
	}
}

function handleServerResponse4() 
{
	//precte zpravu ze serveru
	var xmlResponse = xmlHttp.responseXML;
	
	//ziska element dokumentu XML
	xmlRoot = xmlResponse.documentElement;
	
	// ziska pole se jmeny knih a jejich  ISBN
	nazevArray = xmlRoot.getElementsByTagName("nazev");
	valueArray = xmlRoot.getElementsByTagName("pozice");
	//autorArray = xmlRoot.getElementsByTagName("autor");
	
	//generuje vystup HTML
	var html="<select name=\"znacky\" id=\"znacky\" onchange=\"process6();\">";
	html += "<option value='0' selected='selected'>všechny značky</option>";
	var sendedZnacky = document.getElementById("sendedLicence").sendedZnacky;
	
	// prochazi polem a tvori strukturu HTML
	for(var i=0; i < nazevArray.length; i++) 
	{
	  if(sendedZnacky == valueArray.item(i).firstChild.data) {
		html += "<option value='" + valueArray.item(i).firstChild.data + "' selected=\"selected\">" + nazevArray.item(i).firstChild.data + "</option>";
	  }
	  else {
		html += "<option value='" + valueArray.item(i).firstChild.data + "'>" + nazevArray.item(i).firstChild.data + "</option>";	
	  }
	}
	html += "</select>";
	//ziska odkaz na element <div> na strance		
	myDiv = document.getElementById("divZnacky");
	
	//pridej obsah do elementu <div>
	myDiv.innerHTML = html;
	
	process5();
}


function process5() 
{
	//pokracuj jen neni-li xmlHttp prazdne
	if(xmlHttp) 
	{
		try 
		{
			//pokus se pripojit k serveru
		  var value1 = document.getElementById("podkategorie").value;
		  var value2 = document.getElementById("hlavni_kategorie").value;
		  //vytvori retezec parametru
		  var params = "value1=" + value1 + "&value2=" + value2;
		  var path = dataPath + "/ajax/licence2.php?" + params;
		  //alert(params);
		  //inicijuje asynchronni pozadavek HTTP
		  xmlHttp.open("GET", path, true);
		  xmlHttp.onreadystatechange = handleRequestStateChange3;
		  xmlHttp.send(null);
			
		}
		// v pripade selhani zobraz chybu
		catch(e) 
		{
			alert("Can`t connect to server: \n" + e.toString());
		}
	}
}

function process6() 
{
	//pokracuj jen neni-li xmlHttp prazdne
	if(xmlHttp) 
	{
		try 
		{
			//pokus se pripojit k serveru
		  var value1 = document.getElementById("znacky").value;
		  var value2 = document.getElementById("podkategorie").value;
		  var value3 = document.getElementById("hlavni_kategorie").value;
		  
		  //vytvori retezec parametru
		  var params = "value1=" + value1 + "&value2=" + value2 + "&value3=" + value3;
		  //alert(params);
		  //inicijuje asynchronni pozadavek HTTP
		  var path = dataPath + "/ajax/licence3.php?" + params;
		  xmlHttp.open("GET", path, true);
		  xmlHttp.onreadystatechange = handleRequestStateChange3;
		  xmlHttp.send(null);
			
		}
		// v pripade selhani zobraz chybu
		catch(e) 
		{
			alert("Can`t connect to server: \n" + e.toString());
		}
	}
}

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0'); if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}
