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 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){
        document.getElementById("anketa_"+anketa).innerHTML=xmlHttp.responseText;
    	}
    	else{
   			alert(chyba);
		}
	}
}
function typeOfNavigation(id) {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
      if (id == "1") {
        document.getElementById('typeOfNavigation1').style.color = "#2a0026";
        document.getElementById('typeOfNavigation0').style.color = "";
      }
      else {
        document.getElementById('typeOfNavigation1').style.color = "";
        document.getElementById('typeOfNavigation0').style.color = "#2a0026";
      }
    	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){
        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 = "";
    }
    document.getElementById('phrase').style.color = "#5b2255";
  }
  else {
    document.getElementById('phrase').value = "";
    document.getElementById('phrase').style.color = "#333333";
  }
}
function pridatDoVybranych(idecko) {
  var policko = document.getElementById('choose'+idecko);
  datum = new Date(); // aktuální čas
	if (policko.checked==true) {	
      window.alert("Produkt byl přidán do Vašeho výběru.");
      datum.setTime(datum.getTime() + 30*24*60*60*1000) ;//za rok      
  }    
	else if (policko.checked==false){
      datum.setTime(datum.getTime() - 100*30*24*60*60*1000) ;//za rok
  }
  document.cookie = idecko+"=added;expires="+ datum.toGMTString()+";path=/";
}

function goTo(adresa, select, select_value) { 
  adresa = adresa+"&"+select+"="+select_value;
  self.location.href=adresa;
}

function presmeruj (id) { 
  datum = new Date(); // aktuální čas
  datum.setTime(datum.getTime() + 24*60*60*1000) ;//za rok      
  document.cookie = "last_choose="+id+";expires="+ datum.toGMTString();
  //self.location.href=adresa; window.open(adresa, 'menubar=no,width=888,height=760,scrollbars,left=150,top=0');
}

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
  }
}
/*
 * Zkontroluje platny format emailove adresy
 */
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;
}

