var xmlHttp = createXmlHttpRequestObject();
var chyba = 'Nastala chyba.';
function createXmlHttpRequestObject(){	
	var xmlHttp;
	if ( window.ActiveXObject ){
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		xmlHttp = new XMLHttpRequest();
  }
  if ( !xmlHttp )
	  alert("Chyba při vytváření objektu.");
	else 
    return xmlHttp;
}
function preloader() {
 //heavyImage = new Image(); 
 //heavyImage.src = "images/submit_big_over.png";
}
function getRating(ncount, id, ip, units) {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    	xmlHttp.open("GET", "http://"+location.hostname+"/ajax/product_vote.php?ncount="+ncount+"&id="+id+"&ip="+ip+"&units="+units, true);  
    	xmlHttp.onreadystatechange = function () {rateChanged(); } ;  	
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){getRating(ncount, id, ip, units)},1000);
	}
}
function rateChanged() {
	if (xmlHttp.readyState == 4){
	    if (xmlHttp.status == 200){
        document.getElementById("rating_of_products").innerHTML=xmlHttp.responseText;
    	}
	}
}
function checkProductVariantData(productid, el) {
	id = el.options[el.selectedIndex].value;
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){     
    	xmlHttp.open("GET", "http://"+location.hostname+"/ajax/ajax_product_data.php?product="+productid+"&variant="+id, true);  
    	xmlHttp.onreadystatechange = function () {handleCheckProductVariantData(); } ;  	
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){checkProductVariantData(productid, el)},300);
	}
}
function handleCheckProductVariantData(){
	if (xmlHttp.readyState == 4){
	    if (xmlHttp.status == 200) {
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			pv_priceArray = xmlDocumentElement.getElementsByTagName('pv_price');
			pv_deliveryArray = xmlDocumentElement.getElementsByTagName('pv_delivery');
			s_nameArray = xmlDocumentElement.getElementsByTagName('s_name');
			pv_numberArray = xmlDocumentElement.getElementsByTagName('pv_number');
			submit_buttonArray = xmlDocumentElement.getElementsByTagName('submit_button');
			  jQuery(document).ready(function(){
          jQuery("#detail_store").fadeTo(0, 0);
          jQuery("#detail_store").fadeTo(500, 1);
          jQuery("#detail_number").fadeTo(500, 1);
        }); 
			  jQuery(document).ready(function(){
          jQuery("#detail_price").fadeTo(0, 0);
          jQuery("#detail_price").fadeTo(500, 1);
          jQuery("#detail_number").fadeTo(500, 1);
        }); 
      document.getElementById("detail_store").innerHTML = s_nameArray.item(0).firstChild.data;
      document.getElementById("detail_price").innerHTML = pv_priceArray.item(0).firstChild.data;
      document.getElementById("detail_number").innerHTML = pv_numberArray.item(0).firstChild.data;
      document.getElementById("detail_submit").innerHTML = submit_buttonArray.item(0).firstChild.data;
      if (pv_deliveryArray.item(0).firstChild.data != 0)
        document.getElementById("detail_delivery").innerHTML = pv_deliveryArray.item(0).firstChild.data;
    	}
	}
}
function getVote(anketa, odpoved) {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    	xmlHttp.open("GET", "http://"+location.hostname+"/ajax/poll_vote.php?odpoved="+odpoved+"&anketa="+anketa, true);  
    	xmlHttp.onreadystatechange = function () {stateChanged(anketa); } ;  	
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){getVote(anketa, odpoved)},1000);
	}
}
function stateChanged(anketa) {
	if (xmlHttp.readyState == 4){
	    if (xmlHttp.status == 200){
        jQuery(document).ready(function(){
          jQuery("#anketa_"+anketa).fadeTo(0, 0);
          jQuery("#anketa_"+anketa).fadeTo(2500, 1);
        });
        document.getElementById("anketa_"+anketa).innerHTML=xmlHttp.responseText;
    	}
    	else{
   			alert(chyba);
		}
	}
}
function recountBasket() { 
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    	xmlHttp.open("GET", "http://"+location.hostname+"/ajax/recount_basket.php", true);  
    	xmlHttp.onreadystatechange = function () {stateRecountBasket(); } ;  	
    	xmlHttp.send(null);
	}
}
function stateRecountBasket() {
	if (xmlHttp.readyState == 4){
	    if (xmlHttp.status == 200){
			  xmlResponse = xmlHttp.responseXML;
			  xmlDocumentElement = xmlResponse.documentElement;
			  pocet = xmlDocumentElement.getElementsByTagName('pocet');
			  cena = xmlDocumentElement.getElementsByTagName('cena');
        document.getElementById("basket_pocet").innerHTML=pocet.item(0).firstChild.data;
        document.getElementById("basket_cena").innerHTML=cena.item(0).firstChild.data;
    	}
	}
}
function typeOfNavigation(id) {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    	xmlHttp.open("GET", "http://"+location.hostname+"/ajax/navigation.php?id="+id, true);  
    	xmlHttp.onreadystatechange = function () {stateNavigation(id); } ;  	
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){typeOfNavigation(id)},1000);
	}
}

function stateNavigation(id) {
	if (xmlHttp.readyState == 4){
	    if (xmlHttp.status == 200){
        jQuery(document).ready(function(){
          jQuery("#navigation").fadeTo(0, 0);
          jQuery("#navigation").fadeTo(500, 1);
        });
        document.getElementById("navigation").innerHTML=xmlHttp.responseText;
    	}
    	else{
   			alert(chyba);
		}
	}
}
function phraseSearch() {
  if (document.getElementById('phrase').value != "") {
    if (document.getElementById('phrase').value == "Hledaný produkt...") {
      document.getElementById('phrase').value = "";
    }
  }
  else {
    document.getElementById('phrase').value = "";
  }
}
function showBlock(id) {
  if (document.getElementById(id).className == "hidden") {
    document.getElementById(id).className="";
    document.getElementById("firma").checked=true;
    return false
  }
  if (document.getElementById(id).className != "hidden") {
    document.getElementById("firma").checked=false;
    document.getElementById(id).className="hidden";
    return false
  }
}
function showBlockDiv(id) {
  if (document.getElementById(id).className == "hidden") {
    document.getElementById(id).className="";
    return false
  }
  if (document.getElementById(id).className != "hidden") {
    document.getElementById(id).className="hidden";
    return false
  }
}
function checkInput(id, id2) {
  if (document.getElementById(id).checked === true) {
    document.getElementById(id).checked=false;
    document.getElementById(id2).className = "noactive";
  }
  else if (document.getElementById(id).checked === false) {
    document.getElementById(id).checked=true;
    document.getElementById(id2).className = "active";
  }
}
function checkDiv(id, id2) {
  if (document.getElementById(id).checked === false) {
    document.getElementById(id2).className = "noactive";
  }
  else if (document.getElementById(id).checked === true) {
    document.getElementById(id2).className = "active";
  }
}
function validEmail(email)
{
  invalidChars = " /:,;"

  if(email == "")
  {
    return false
  }
  for(i=0; i < invalidChars.length; i++)
  {
    badChar = invalidChars.charAt(i)
    if (email.indexOf(badChar,0) > -1)
    {
      return false
    }
  }
  atPos = email.indexOf("@",1)
  if(atPos == -1)
  {
    return false
  }
  if(email.indexOf("@",atPos+1) > -1)
  {
    return false
  }
  periodPos = email.indexOf(".",atPos)
  if(periodPos == -1)
  {
    return false
  }
  if(periodPos+3 > email.length)
  {
    return false
  }
  return true
}
function validPhone(phonenumber) {
  vyraz = /^[+]?[()/0-9. -]{9,}$/
  if (vyraz.test(phonenumber)==true)
    return true;
  else 
    return false;
}

