/* Ziskat prehled regionu pro dany kraj */
function subSetSearchButton(){
   var idCountry = $('id_country_s').get('value');
   var idGroup = $('id_group_s').get('value');

   if((idCountry == 0) || (idGroup == 0)) $('submit_subs').setProperty('disabled','disabled');
   else $('submit_subs').removeProperty('disabled');
}

/* Ziskat prehled regionu pro dany kraj */
function subGetRegion(urlmodul){
   var url = urlmodul;
   var timeStamp = "casotisk=" + new Date().getTime();
   var urlPost = url + '?' + timeStamp;
   var postData = 'idcountry=' + $('id_country_s').get('value') + '&idregion=' + $('id_region_s').get('value') + '&idcity=' + $('id_city_s').get('value');

   // Disable select
   $('id_region_s').setProperty('disabled','disabled');

   // Disable select
   $('id_city_s').setProperty('disabled','disabled');

   // Info box
   ShowAppBox.showBox($('app_work'));

   var myAjax = new Request({method: 'post', url: urlPost,data:postData,onComplete: resultSubGetRegion}).send();
}
/* Zachytit pozadavek */
function resultSubGetRegion(){
   //alert(this.response.text);
   workSubGetRegion(this.response.xml);
}
/* Zpracovat pozadavek */
function workSubGetRegion(xmlDoc){
   // Info box
   ShowAppBox.showBox($('app_work'));

   // Indikace zda jsou regiony
   var isRegion = xmlDoc.getElementsByTagName("isregion")[0].firstChild.data;

   // Smazat select mest
   removeElement('id_city_s');

   // Enable select
   if(isRegion == 1) $('id_region_s').removeProperty('disabled');
   // Smazat select
   removeElement('id_region_s');

   // Vytvorit select
   var items = xmlDoc.getElementsByTagName('region');
   subBuildSelect('id_region_s',items,'idreg');
}

/* Ziskat prehled mest pro dany region */
function subGetCity(urlmodul){
   var url = urlmodul;
   var timeStamp = "casotisk=" + new Date().getTime();
   var urlPost = url + '?' + timeStamp;
   var postData = 'idcountry=' + $('id_country_s').get('value') + '&idregion=' + $('id_region_s').get('value') + '&idcity=' + $('id_city_s').get('value');

   // Disable select
   $('id_city_s').setProperty('disabled','disabled');

   var myAjax = new Request({method: 'post', url: urlPost,data:postData,onComplete: resultSubGetCity}).send();
}
/* Zachytit pozadavek */
function resultSubGetCity(){
   //alert(this.response.text);
   workSubGetCity(this.response.xml);
}
/* Zpracovat pozadavek */
function workSubGetCity(xmlDoc){
   // Indikace zda jsou regiony
   var isCity = xmlDoc.getElementsByTagName("iscity")[0].firstChild.data;

   // Enable select
   if(isCity == 1) $('id_city_s').removeProperty('disabled');
   // Smazat select
   removeElement('id_city_s');

   // Vytvorit select
   var items = xmlDoc.getElementsByTagName('city');
   subBuildSelect('id_city_s',items,'idcity');
}

/* Ziskat prehled kategorii pro danou skupinu */
function subGetGroupCat(urlmodul){
   var url = urlmodul;
   var timeStamp = "casotisk=" + new Date().getTime();
   var urlPost = url + '?' + timeStamp;
   var postData = 'idgroup=' + $('id_group_s').get('value');

   // Disable select
   $('id_cat_s').setProperty('disabled','disabled');

   // Info box
   ShowAppBox.showBox($('app_work'));

   var myAjax = new Request({method: 'post', url: urlPost,data:postData,onComplete: resultSubGetGroupCat}).send();
}
/* Zachytit pozadavek */
function resultSubGetGroupCat(){
   //alert(this.response.text);
   workSubGetGroupCat(this.response.xml);
}

/* Zpracovat pozadavek */
function workSubGetGroupCat(xmlDoc){
   // Info box
   ShowAppBox.showBox($('app_work'));

   // Indikace zda jsou regiony
   var isCat = xmlDoc.getElementsByTagName("iscat")[0].firstChild.data;

   // Enable select
   if(isCat == 1) $('id_cat_s').removeProperty('disabled');
   // Smazat select
   removeElement('id_cat_s');

   // Vytvorit select
   var items = xmlDoc.getElementsByTagName('categorie');
   subBuildSelect('id_cat_s',items,'idcat');
}

/* Vytvorit polozky selectu */
function subBuildSelect(elementSelect,elementItem,idprefix){
   //projit polozky
   for(var i=0;i < elementItem.length;i++){
      var nameItem = getContent(getElByName(elementItem[i], 'name')[0]);
      var idItem = getContent(getElByName(elementItem[i], 'id')[0]);

      option = document.createElement("option");
      option.appendChild(document.createTextNode(nameItem));
      option.setAttribute("value",idItem);
      option.setAttribute("id",idprefix+idItem);
      //pridat option do selectu
      //var selectmark = document.getElementById(elementSelect);
      $(elementSelect).appendChild(option);
   }
}
