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 fillRubrika(el) {
	id = el.options[el.selectedIndex].value;
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){     
    	xmlHttp.open("GET", "../ajax/rubrika.php?id="+id, true);  
    	xmlHttp.onreadystatechange = function () {handleServerResponse(); } ;  	
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){fillRubrika(el)},1000);
	}
}
function handleServerResponse(){
	if (xmlHttp.readyState == 4){
	    if (xmlHttp.status == 200) {
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			idArray = xmlDocumentElement.getElementsByTagName('id');
			nameArray = xmlDocumentElement.getElementsByTagName('name');
   		
                     select = document.getElementById('slct3');
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('--- Vyberte rubriku ---'));
			clearChilds(select,default_opt);

  			for ( var i=0; i<idArray.length; i++ ){
				new_opt = document.createElement("option"); 
				new_opt.setAttribute("value",idArray.item(i).firstChild.data);
				new_opt.appendChild(document.createTextNode(nameArray.item(i).firstChild.data));
				select.appendChild(new_opt); 
			}
    	}
    	else{
   			alert('Nastala chyba.');
		}
	}
}
//vycisteni selectu
function clearChilds(el, def){
	if ( el.hasChildNodes() ){ // pokud ma select options
    	while ( el.childNodes.length >= 1 ){ // pokud je pocet potomku >= 1
        	el.removeChild( el.firstChild ); // odstraneni prvniho potomka
    	}
	}
	if (def) el.appendChild(def); // pripojeni "nerozhoduje"
}
function phraseSearch() {
  if (document.getElementById('phrase').value != "") {
    if (document.getElementById('phrase').value == "např. pilotní průkaz") {
      document.getElementById('phrase').value = "";
    }
  }
  else {
    document.getElementById('phrase').value = "";
  }
}


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;
}

