// JavaScript Document

//<![CDATA[
//var http_request;
var gselpowiatnr;
function getpowiatName(selnr) {
	var red=/^\d+$/;
	
	//alert(window.event.srcElement.name);
	if(red.test(selnr+''))
		gselpowiatnr=selnr;
		//alert("jest");
	else
	{
			//alert(window.event.srcElement.name);
//			return;
		var re=/\D/g;
		gselpowiatnr=window.event.srcElement.name.replace(re,'');
	}
	
	frm=document.forms[0];
	//alert(frm.elements['hurtoponregionid'+gselpowiatnr].value);
	query="&hurtoponregionid="+frm.elements['hurtoponregionid'+gselpowiatnr].value;
	
	if(!document.getElementById('hurtoponpowiatid'+gselpowiatnr))return;
	load_getpowiatName("/ssi/getpowiatName.asp?data=get",encodeURI(query));
	//document.write(query);
}



//var http_request = false;
   function load_getpowiatName(url, query) {
      http_request = false;
	  
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = StateChangepowiatname;
      http_request.open('GET', url+query, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //  http_request.setRequestHeader("Content-length", query.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(query);
   }



function StateChangepowiatname() {

	if(http_request.readyState == 1)
		{
	 		//document.getElementById('loader-icon').style.display= "block";
			//document.getElementById('hurtoponpowiatid').value= "";	
			//document.getElementById('link1Box').innerHTML="";
			//document.getElementById('link2Box').innerHTML="";
		}		
  		
	if (http_request.readyState == 4) { // Complete
			//document.getElementById('loader-icon').style.display= "none";
	
	if (http_request.status == 200) { // OK response
		
		// instrukcje...
		http_content = http_request.responseText;	
		
		// budowanie pola select
		results = http_content.split(",");
		
		theSelect = document.getElementById("hurtoponpowiatid"+gselpowiatnr);
		
		//theSelect.name="miasto"
		//theSelect.id="miasto"
		//theSelect.setAttribute = ("name","select1")
		//theSelect.setAttribute = ("id","select1")
		while (theSelect.firstChild) {
		     theSelect.removeChild(theSelect.firstChild);
		}
		
		theOption=document.createElement("option");
		theText=document.createTextNode("(wybierz z listy)");
		theOption.value="";
		theOption.appendChild(theText);
		theSelect.appendChild(theOption);
		theSelect.style.width="200px";
		
		for ( i = 0; i < results.length-1; i++ ) {
		theOption=document.createElement("option");
		aresposp=results[i].split('|');
		tkp=aresposp[1];
		vlp=aresposp[0];
		theText=document.createTextNode(tkp);
		theOption.appendChild(theText);
		
		theOption.setAttribute("value",vlp);
		theSelect.appendChild(theOption);
		}	
		
		
		
		} 
		
		else {
		// obsługa błędów
		http_content = http_request.statusText;
		//document.getElementById("urlContent").innerHTML = http_content;
       	}
    }
  }
 //]]>