/*
 *  Dokument:       Funkce pro pro plneni combo boxu se seznamem obci a katastru
 *  Posledni zmena: 13.03.2009
 */
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;
}

function changeRegion(source)
{
  if(source.id!='selectRegion')
    return false;

  index = source.selectedIndex;
  region_id = source.options[index].value;

  if(!send_xmlhttprequest(updateCountry, 'GET', 'ajax/get_country.php?region_id=' + region_id))
    return false;

  return true;
}
function changeCountry(source)
{
  if(source.id!='selectCountry')
    return false;

  index = source.selectedIndex;
  country_id = source.options[index].value;
  if(!send_xmlhttprequest(updateCA, 'GET', 'ajax/get_ca.php?country_id=' + country_id))
    return false;

  if(country_id == 0)
    this.document.getElementById('selectCA').disabled = true;

  return true;
}
function changeCA(source)
{
}

function updateCountry(xmlHttp)
{
  if(xmlHttp.readyState == 4)
  {
    if(xmlHttp.status == 200)
    {
      selectCountry = document.getElementById('selectCountrydiv');
      if(selectCountry)
      {
        selectCountry.innerHTML = "<select name=\"okres_kod\" id=\"selectCountry\" onchange=\"changeCountry(this)\">" + xmlHttp.responseText + "</select>";
      }
    }
  }
}
function updateCA(xmlHttp)
{
  if(xmlHttp.readyState == 4)
  {
    if(xmlHttp.status == 200)
    {
      selectCA = document.getElementById('selectCAdiv');
      if(selectCA)
      {
        selectCA.innerHTML = "<select name=\"ku_kod\" id=\"selectCA\" onchange=\"changeCA(this)\">" + xmlHttp.responseText + "</select>";
      }
    }
  }
}



function changeCountryMulti(source)
{
  if(source.id!='selectCountry')
    return false;
  index = source.selectedIndex;
  country_id = source.options[index].value;
  if(!send_xmlhttprequest(updateCAMulti, 'GET', 'ajax/get_ca_multi.php?country_id=' + country_id))
    return false;

  if(country_id == 0)
    this.document.getElementById('selectCA').disabled = true;

  return true;
}
function updateCAMulti(xmlHttp)
{
  if(xmlHttp.readyState == 4)
  {
    if(xmlHttp.status == 200)
    {
      selectCA = document.getElementById('selectCAdiv');

      if(selectCA)
      {
        selectCA.innerHTML = "<select name=\"ku_kod\" id=\"selectCA\" size=\"10\" multiple=\"multiple\">" + xmlHttp.responseText + "</select>";
      }
      selectSel2 = document.getElementById('sel2');
      selectSel2.disabled = false;
    }
  }
}

function verifyCA(From, To) {
  var selLength = From.length;
  var selectedCount = 0;
  var i;
  var selectedItems = new Array();
  for(i=selLength-1; i>=0; i--) {
    if(From.options[i].selected) {    
      selectedItems[selectedCount] = From.options[i].value;     
	    selectedCount++;
    }
  }     
      if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
        xmlHttp.open("GET", "ajax/get_verify.php?id="+selectedItems.join("."), true);  
    	  xmlHttp.onreadystatechange = function () {updateVerifyCA(From, To); } ;  	
    	  xmlHttp.send(null);
        selectedCount++;      
      }
	    else{ 
		    setTimeout(function(){updateVerifyCA(From, To); },1000);
	    } 
}
function updateVerifyCA(From, To){
  if (xmlHttp.readyState == 4){
	  if (xmlHttp.status == 200) {
      try {
        var XMLRes = xmlHttp.responseXML;
        if(!XMLRes || !XMLRes.documentElement){
          throw("Chybná struktura XML:\n"+xmlHttp.responseText);
        }
        var rootNodeName = XMLRes.documentElement.nodeName;
        if(rootNodeName == "parsereerror"){
          throw("Chybná struktura XML:\n"+xmlHttp.responseText);
        }
        xmlRoot = XMLRes.documentElement;
        id = xmlRoot.getElementsByTagName("status");
        smazane_katastry = xmlRoot.getElementsByTagName("smazane_katastry");
        okresy = xmlRoot.getElementsByTagName("okresy");
     
      if (id.item(0).firstChild.data == "OK") {
        moveOptions (From, To);
      }
      if (okresy.item(0).firstChild.data != "undefined") {
        var pole = okresy.item(0).firstChild.data; //pole
        pole.toString();
        var selLength = To.length;    
        var i;         
        for(i=selLength-1; i>=0; i--) {
          var promenna = To.options[i].value;
          if (pole.search(promenna) >= 0) {
            To.options[i] = null;      
          }        
        } 
        alert('Výběr všech k.ú. daného okresu se zrušil.');
      }
      else if (smazane_katastry.item(0).firstChild.data != "undefined") {
        var pole = smazane_katastry.item(0).firstChild.data; //pole
        pole.toString();
        var selLength = To.length;    
        var i;         
        for(i=selLength-1; i>=0; i--) {
          var promenna = To.options[i].value;
          if (pole.search(promenna) >= 0) {
            To.options[i] = null;      
          }        
        }
        alert('Byly vybrány všechny k.ú. z okresu.'); 
      }      
    }
    catch(e){
        alert("Chyba při čtení odpovědi:"+e.toString());
      }
    }
    else {
      alert("Požadavek HTTP není v pořádku.")
    }
  }
}
function verifyCARemove(CA, Sel2)
{
  var selLength = Sel2.length;
  var id = new Array();
  var selectedCount = 0;
  var i;
  var selectedItems = new Array();
  for(i=selLength-1; i>=0; i--) {
    if(Sel2.options[i].selected) {    
      selectedItems[selectedCount] = Sel2.options[i].value;     
	    selectedCount++;
    }
  }     
      if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
        xmlHttp.open("GET", "ajax/get_verify_remove.php?id="+selectedItems.join("."), true);  
    	  xmlHttp.onreadystatechange = function () {updateVerifyCARemove(CA, Sel2); } ;  	
    	  xmlHttp.send(null);
        selectedCount++;      
      }
	    else{ 
		    setTimeout(function(){updateVerifyCARemove(CA, Sel2); },1000);
	    } 
}
function updateVerifyCARemove(CA, Sel2){
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			id = xmlDocumentElement.getElementsByTagName('status');
      if (id.item(0).firstChild.data == "OK")
        moveOptions (Sel2, CA);
}
//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"
}


//Multi select - poptavka
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
function addOption(theSel, theText, theValue) {
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}
function deleteOption(theSel, theIndex) { 
  var selLength = theSel.length;
  if(selLength>0)
    theSel.options[theIndex] = null;
}
function moveOptions(theSelFrom, theSelTo) {
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  var i;
  for(i=selLength-1; i>=0; i--) {
    if(theSelFrom.options[i].selected) {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  for(i=selectedCount-1; i>=0; i--) {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
  if(NS4) history.go(0);
}


