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 = "white";
        document.getElementById('typeOfNavigation0').style.color = "";
      }
      else {
        document.getElementById('typeOfNavigation1').style.color = "";
        document.getElementById('typeOfNavigation0').style.color = "white";
      }
    	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 nextPhotos(product, photo, sequence, variant) {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    	xmlHttp.open("GET", "http://"+location.hostname+"/ajax/photos.php?photo="+photo+"&product="+product+"&sequence="+sequence+"&variant="+variant, true);  
    	xmlHttp.onreadystatechange = function () {stateNextPhotos(); } ;  	
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){nextPhotos(product, photo, sequence, variant)},100);
	}
}

function stateNextPhotos() {
	if (xmlHttp.readyState == 4){
	    if (xmlHttp.status == 200) {
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			main_foto = xmlDocumentElement.getElementsByTagName('main_foto');
			button_next_photos = xmlDocumentElement.getElementsByTagName('button_next_photos');
      
      document.getElementById("main_foto_div").innerHTML= main_foto.item(0).firstChild.data;
      document.getElementById("button_next_photos_div").innerHTML= button_next_photos.item(0).firstChild.data;
      initLytebox();
      }
    	else{
   			alert('Nastala chyba.');
		}
	}
}

function showPhotoVariant(product, varianta) {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
      document.getElementById('varianta').value = varianta;
    	xmlHttp.open("GET", "http://"+location.hostname+"/ajax/photo_variant.php?changing=1&varianta="+varianta+"&product="+product, true);  
    	xmlHttp.onreadystatechange = function () {stateShowPhotoVariant(); } ;  	
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){showPhotoVariant(product, varianta)},100);
	}
}

function stateShowPhotoVariant() {
	if (xmlHttp.readyState == 4){
	    if (xmlHttp.status == 200) {
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			
			main_foto = xmlDocumentElement.getElementsByTagName('main_foto');
      document.getElementById("main_foto_div").innerHTML= main_foto.item(0).firstChild.data;
      
			button_next_photos = xmlDocumentElement.getElementsByTagName('button_next_photos');
      document.getElementById("button_next_photos_div").innerHTML = button_next_photos.item(0).firstChild.data;
      initLytebox();
      }
    	else{
   			alert('Nastala chyba.');
		}
	}
}

function nextPhotosMain(product, photo, sequence, variant) {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    	xmlHttp.open("GET", "http://"+location.hostname+"/ajax/photosMain.php?photo="+photo+"&product="+product+"&sequence="+sequence+"&variant="+variant, true);  
    	xmlHttp.onreadystatechange = function () {stateNextPhotosMain(); } ;  	
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){nextPhotosMain(product, photo, sequence, variant)},100);
	}
}

function stateNextPhotosMain() {
	if (xmlHttp.readyState == 4){
	    if (xmlHttp.status == 200) {
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			main_foto = xmlDocumentElement.getElementsByTagName('main_foto');
			button_next_photos = xmlDocumentElement.getElementsByTagName('button_next_photos');
      
      document.getElementById("main_foto_div").innerHTML= main_foto.item(0).firstChild.data;
      document.getElementById("button_next_photos_div").innerHTML= button_next_photos.item(0).firstChild.data;

      }
    	else{
   			alert('Nastala chyba.');
		}
	}
}

function phraseSearch() {
  if (document.getElementById('phrase').value != "") {
    if (document.getElementById('phrase').value == "Hledaný produkt...") {
      document.getElementById('phrase').value = "";
    }
    document.getElementById('phrase').style.color = "white";
  }
  else {
    document.getElementById('phrase').value = "";
    document.getElementById('phrase').style.color = "white";
  }
}
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 changeTab(id) {
  document.getElementById('tabika1').style.background = "url(http://www.aemotiv.com/images/tab.png) repeat-x";
  document.getElementById('tabika2').style.background = "url(http://www.aemotiv.com/images/tab.png) repeat-x";
  document.getElementById('tabika3').style.background = "url(http://www.aemotiv.com/images/tab.png) repeat-x";
  document.getElementById('tabika'+id).style.background = "#d8efff";
}
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="";
    return false
  }
  else if (document.getElementById(id).className != "hidden") {
    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;
}

