var xmlHttp = createXmlHttpRequestObject();
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;
}
//filtr ZNACKA --> MODEL
function fillForm(el) {
	id = el.options[el.selectedIndex].value;
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
      type = document.getElementById('ftyp').value;     
      /*if (id == "rozsirit") {
        alert('Nyní vyberte značku z rozšířeného seznamu.');
    	  xmlHttp.open("GET", "ajax.php?alles=1&typyvozidel="+type, true);    	
    	  xmlHttp.onreadystatechange = function () {innerujHandle(); } ;
      }
      else {*/
    	  xmlHttp.open("GET", "ajax.php?choose_type="+type+"&id="+id, true);  
    	  xmlHttp.onreadystatechange = function () {handleServerResponse(); } ;
      //}  	
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){process(el, id)},1000);
	}
}
//filtr fillForm ZNACKA --> MODEL
function handleServerResponse(el){
	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('fmodel');
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('---Nerozhoduje---'));
			clearChilds(select,default_opt);
			select.disabled = false;
  			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.');
		}
	}
}

//filtr ZNACKA --> MODEL
function fillFormAddCars(el) {
	id = el.options[el.selectedIndex].value;
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
      type = document.getElementById('ftyp').value;     
      if (id == "OSTATNI" || id == "jiny") {
        //if (id == "rozsirit") alert('Nyní vyberte značku z rozšířeného seznamu.');
        document.getElementById('jinaznacka').className="";
        document.getElementById('jinymodel').className="";
        document.getElementById('jine').className="";
    	  xmlHttp.open("GET", "ajax.php?alles=1&typyvozidel="+type, true);    	
    	  xmlHttp.onreadystatechange = function () {showOthers(); } ;
      }
      else {
        document.getElementById('jinaznacka').className="hidden";
        document.getElementById('jinymodel').className="hidden";
        document.getElementById('jine').className="hidden";
    	  xmlHttp.open("GET", "ajax.php?choose_type="+type+"&id="+id, true);  
    	  xmlHttp.onreadystatechange = function () {handleServerResponse(); } ;
      }  	
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){process(el, id)},1000);
	}
}

function showOthers(el){
	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('fznacka');
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'jiny');
			default_opt.appendChild(document.createTextNode('OSTATNÍ'));
			clearChilds(select,default_opt);
			select.disabled = false;
      document.getElementById('fmodel').disabled = true;
  			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);
			}
    	}
	}
}

function innerujHandle(el){
	if (xmlHttp.readyState == 4){
	    if (xmlHttp.status == 200){
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			// nacteni idcek a nazvu z XML odpovedi
			idArray = xmlDocumentElement.getElementsByTagName('id');
			nameArray = xmlDocumentElement.getElementsByTagName('name');
			// select ktery budem vyplnovat
   		select = document.getElementById('fznacka');
   		// vytvoreni defaultni option "nerozhoduje"
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('---Nerozhoduje---'));
			// vyprazdneni selectu
			clearChilds(select,default_opt);
			// enable selectu
			select.disabled = false; // pri nacteni stranky je select disabled,
			// vyplneni selectu
  			for ( var i=0; i<idArray.length; i++ ){
				new_opt = document.createElement("option"); // vytvoreni elementu OPTION
				new_opt.setAttribute("value",idArray.item(i).firstChild.data); // nastaveni atributu value
				new_opt.appendChild(document.createTextNode(nameArray.item(i).firstChild.data)); // pripojeni popisu jako potomka
				select.appendChild(new_opt); // pripojeni option k selectu
			}
    	}
    	else{
   			alert('Nastala chyba.');
		}
	}
}
function innerujHandle2(el){
	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('nznacka');
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('---Nerozhoduje---'));
			clearChilds(select,default_opt);
			select.disabled = false; 
  			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.');
		}
	}
}

//filtr opet ZNACKA --> MODEL (nove vozy - box 2 na uvodni strance)
function fillForm2(el) {
	id = el.options[el.selectedIndex].value; // nacteni value vybranyho option
  	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
      type = document.getElementById('ntyp').value;
      /*if (id == "rozsirit") {
        alert('Nyní vyberte značku z rozšířeného seznamu.');
    	  xmlHttp.open("GET", "ajax.php?alles=1&typyvozidel="+type, true);    	
    	  xmlHttp.onreadystatechange = function () {innerujHandle2(); } ;
      }
      else {*/
    	  xmlHttp.open("GET", "ajax.php?choose_type="+type+"&id="+id, true);    	
    	  xmlHttp.onreadystatechange = function () {handleServerResponse2(); } ;
    	//}
    	xmlHttp.send(null);
	}
	else{
		setTimeout(function(){process(el, id)},1000);
	}
}
function handleServerResponse2(el){
	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('nmodel');			
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('---Nerozhoduje---'));
			clearChilds(select,default_opt);
			select.disabled = false; 
  			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.');
		}
	}
}
//KAROSERIE
function fillForm3(el) {
	  id = el.options[el.selectedIndex].value; 
  	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    	xmlHttp.open("GET", "ajax.php?karoserie=1&id="+id, true);
    	xmlHttp.onreadystatechange = function () {handleServerResponse3(); } ;
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){process(el, id)},1000);
	}
}
function handleServerResponse3(el){
	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('karoserie');
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('---Nerozhoduje---'));
			clearChilds(select,default_opt);
			select.disabled = false; 
  			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.');
		}
	}
}
function filtrujTypy(el){
	id = el.options[el.selectedIndex].value; 
  	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    	xmlHttp.open("GET", "ajax.php?typy=1&id="+id, true);
    	xmlHttp.onreadystatechange = function () {handleServerResponse4(); } ;
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){process(el, id)},1000);
	}
}
function handleServerResponse4(el){
	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('foznaceni');
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('---Nerozhoduje---'));
			clearChilds(select,default_opt);
			select.disabled = false; 
  			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.');
		}
	}
} 
//znacky
function fillForm5(el){
  id = el.options[el.selectedIndex].value;
  	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    	xmlHttp.open("GET", "ajax.php?marques=1&id="+id, true);
    	xmlHttp.onreadystatechange = function () {handleServerResponse5(); } ;
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){process(el, id)},1000);
	}
}
function handleServerResponse5(el){
	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('fznacka');
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('---Nerozhoduje---'));
			clearChilds(select,default_opt);
			select.disabled = false; 
			new_opt = document.createElement("option"); 
			new_opt.setAttribute("value","OSTATNI"); 
			new_opt.appendChild(document.createTextNode("OSTATNÍ"));
			select.appendChild(new_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); 
			}
			//karoserie	- filtr
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			idArray = xmlDocumentElement.getElementsByTagName('bid');
			nameArray = xmlDocumentElement.getElementsByTagName('bname');
      select = document.getElementById('karoserie');
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('---Nerozhoduje---'));
			clearChilds(select,default_opt);
			select.disabled = false; 
  			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.');
		}
	}
}
function fillForm7(el){
  id = el.options[el.selectedIndex].value; 
  	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    	xmlHttp.open("GET", "ajax.php?marques=1&id="+id, true);
    	xmlHttp.onreadystatechange = function () {handleServerResponse7(); } ;
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){process(el, id)},1000);
	}
}
function handleServerResponse7(el){
	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('fznacka');
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('---Nerozhoduje---'));
			clearChilds(select,default_opt);
			select.disabled = false; 
			new_opt = document.createElement("option");
			new_opt.setAttribute("value","OSTATNI"); 
			new_opt.appendChild(document.createTextNode("OSTATNÍ")); 
			select.appendChild(new_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.');
		}
	}
}

function fillForm6(el){
  id = el.options[el.selectedIndex].value; 
  	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    	xmlHttp.open("GET", "ajax.php?marques=1&id="+id, true);
    	xmlHttp.onreadystatechange = function () {handleServerResponse6(); } ;
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){process(el, id)},1000);
	}
}
function fillForm6b(el){
  id = el.options[el.selectedIndex].value;
  	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    	xmlHttp.open("GET", "ajax.php?marques=1&id="+id, true);
    	xmlHttp.onreadystatechange = function () {handleServerResponse6(); } ;
    	xmlHttp.send(null);
	}
	else{ 
		setTimeout(function(){process(el, id)},1000);
	}
}
function handleServerResponse6(el){
	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('nznacka');
			default_opt = document.createElement("option");
			default_opt.setAttribute("value",'');
			default_opt.appendChild(document.createTextNode('---Nerozhoduje---'));
			clearChilds(select,default_opt);
			select.disabled = false; 
			new_opt = document.createElement("option"); 
			new_opt.setAttribute("value","OSTATNI");
			new_opt.appendChild(document.createTextNode("OSTATNÍ"));
			select.appendChild(new_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"
}

