﻿function rozbal(strURL, divID, typ) {
    var xmlHttpReq = false;
    var self = this;
    var data = '';
    
    //strURL = 'http://localhost/In-pocasi-new' + strURL;
    strURL = 'http://www.in-pocasi.cz' + strURL;
    //alert(strURL);
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    self.xmlHttpReq.open('POST', strURL, true);    
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {          
            if (typ == "update") update(self.xmlHttpReq.responseText, divID);           
            else if (typ == "kraje") getAsArray(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(null);
    //if (typ == "kraje") return data;
    
}

function update(str, divID){
    document.getElementById(divID).innerHTML = str;
    //alert(divID);
    
    var id = parseInt(divID.replace('mestaLister', '')); 
    
    var sel = document.getElementsByTagName("select");
    //alert(id);
    createPseudoSelect(sel[2*id - 1]);
}

var pole;
rozbal('/pocasi-na-web/ajax/kraje.ajax.php', '-', 'kraje');
function getAsArray(str){    
  pole = str.split(":", 13);
  //alert(str);  
}


 function setInnerText(obj, text)
 {
  browserName = getNavigatorName();
  if (browserName =="Microsoft Internet Explorer"){ 
     obj.innerText = text;
   }
   else{
     obj.textContent = text;
   }
 }


	function getNavigatorName(){
		if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {   
			browserName = "Microsoft Internet Explorer";
			return browserName;
		}
		return "";
	}
	
	function getNavigatorVersion(){
		if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {   
			fullVersion = nAgt.substring(verOffset+5);
			fullVersion = fullVersion.substring(0,3);
			return fullVersion;
		}
		return "";
	}

	var nAgt = navigator.userAgent;
  var browserName  = navigator.appName; 
  //var majorVersion = parseInt(navigator.appVersion,10);
  var verOffset;

  var id = 2;
  
function pridejMesto(divID) { 	
	//alert(divID);
	var table = document.getElementById(divID); 
	var rowCount = table.rows.length;	
			
	var selectKraj = document.createElement('select');
	selectKraj.setAttribute('name', 'krajID' + id ); 
	//alert(navigator.appName);
	

	  
   browserName = getNavigatorName();
   fullVersion = getNavigatorVersion();   
   //alert(fullVersion);
    //alert(browserName);

  if (browserName =="Microsoft Internet Explorer"){ 
    if (fullVersion == "6.0"){  
    	var param = '/pocasi-na-web/ajax/mesta.ajax.php?radka=' + id + '';
    	var param2 = 'mestaLister'+id;
      selectKraj.onchange = function(){rozbal(param + '&kraj='+this.options[this.selectedIndex].value, param2, 'update');};
    }
    else {
      selectKraj.setAttribute('onchange', 'rozbal(\'/pocasi-na-web/ajax/mesta.ajax.php?radka=' + id + '&kraj=\'+this.options[this.selectedIndex].value, \'mestaLister'+id+'\', \'update\')');
    }
  }
	else {     
    selectKraj.setAttribute('onchange', 'rozbal(\'/pocasi-na-web/ajax/mesta.ajax.php?radka=' + id + '&kraj=\'+this.options[this.selectedIndex].value, \'mestaLister'+id+'\', \'update\')');
  }
	//selectKraj.setAttribute('onchange', 'rr()')
	/**/
	
	var option = document.createElement('option'); 
	setInnerText(option, "---- Vyberte kraj ----");	
   
  option.value = 0;		
	selectKraj.appendChild(option);
  
  for(i=0;i<pole.length;i++){
    var option = document.createElement('option'); 
	  setInnerText(option, pole[i]);	 	  
    option.value = i + 1;		
	  selectKraj.appendChild(option);
  }
  
	/**/
	
	var select = document.createElement('select');
	select.setAttribute('name', 'mestaLister' + id ); 
	var option = document.createElement('option'); 
	setInnerText(option, '---- Zvolte nejdříve kraj ----');	
  option.value = '0'; 		
	select.appendChild(option);
	
	//var text = document.createTextNode('Přidat fotografii  #'+ id + ': ');
	
  var d = document.createElement('div');
  d.id = "mestaLister" + id;
  //alert("sss" + d.id);
  d.appendChild(select);
   
	var row = table.insertRow(rowCount);
	var cell_1 = row.insertCell(0);
	cell_1.appendChild(selectKraj);

	var cell_2 = row.insertCell(1);
	//cell_2.id = "mestaLister" + id;
	cell_2.appendChild(d);
  //cell_2.appendChild(select);
			 
    id++; 
    
    var sel = document.getElementsByTagName("select");
    createPseudoSelect(sel[sel.length-2]);
    createPseudoSelect(sel[sel.length-1]);

    
 } 