var mForm = document.aspnetForm;
var mLang;
var prefix = "ctl00$middleContent$";
var idprefix = "ctl00_middleContent_";

function initPage(lng)
{
    mLang = lng.toString().toUpperCase();    
    pc = document.getElementById("postcode").value;
    if(pc.toString().length == 4)
    {
        showCityProv(pc);
    }
}
/*function showZipName()
{
    //alert("adapt zip");
}

function changePostalCode()
{
    document.getElementById(idprefix + "ddlName").selectedIndex ==  0;document.getElementById(idprefix + "ddlZip").selectedIndex;
}*/

//*************************************************
//zip oncomplete, search city and providers

function showCityProv(txt)
{    
    document.getElementById("selectCity").style.display = "none";
    
    if((txt.length == 4) && (! isNaN(txt) ) && (txt > 999) && (txt < 10000)  )
    { 
        emptyForm("postcode");
        document.getElementById("postcode").className = "ac_input ac_loading";
        loadXMLDoc(txt);         
    }
}


var xmlhttp;

function loadXMLDoc(zip)
{
    document.getElementById("txtErrorZip").style.display="none";
        
    var url = "../postcode.aspx?q=" + zip + "&lang=" + mLang;	
	
    // code for Mozilla, etc.
    if (window.XMLHttpRequest)
    {
	    xmlhttp=new XMLHttpRequest();
	    xmlhttp.onreadystatechange=xmlhttpChange;
	    xmlhttp.open("GET",url,true);
	    xmlhttp.send(null);
    }
    // code for IE
    else if (window.ActiveXObject)
    {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	    if (xmlhttp)
	    {
		    xmlhttp.onreadystatechange=xmlhttpChange;
		    xmlhttp.open("GET",url,true);
		    xmlhttp.send();
	    }
    }
}



function xmlhttpChange()
{
  
  // if xmlhttp shows "loaded"
  if (xmlhttp.readyState==4)
  {
   // if "OK"
   if (xmlhttp.status==200) 
    {
	    if(xmlhttp.responseText.toString() != "")
	    {
	        //alert(xmlhttp.responseText.toString());
	        arrayCities = xmlhttp.responseText.toString().split("\n");
    		
		    cityZip = arrayCities[0].split("|");
		    
            //only 1 city for that postcode
	        if(arrayCities.length == 2)
	        {
                document.getElementById("txtCity").innerHTML = cityZip[0];
                document.getElementById("cityName").value = cityZip[0];
                document.getElementById("cityZip").value = cityZip[1];
            }
            else
            {
                completeDropdownCities(arrayCities);
            }
            
            zipProv = cityZip[1];
            
            //lat long
            document.getElementById(idprefix + "lat1").value = cityZip[2];
            document.getElementById(idprefix + "long1").value = cityZip[3];
            	
		
		 }
		 else
		 {
		    (language==1)?alert("Gelieve het opgegeven postcode na te gaan."):alert("Veuillez corriger le code postal.");
		    document.getElementById("postcode").select();
		 }
	    document.getElementById("postcode").className = "ac_input";	
	    
    }
  }
}


//more than 1 city for that postcode
function completeDropdownCities(arrayCities)
{
    if(arrayCities[0] != "")
    {
        document.getElementById("txtCity").innerHTML = "";
        for(j=0 ; j < arrayCities.length -1 ; j++)
        {
            zipCity = arrayCities[j].split("|");
            document.getElementById("selectCity").options[j] = new Option(zipCity[0],zipCity[1]);
        }    
         document.getElementById("selectCity").style.display = "inline";
         //pre-select 1e choice
         document.getElementById("cityZip").value = zipCity[1];//document.getElementById("selectCity").options[0].value;
         document.getElementById("cityName").value = document.getElementById("selectCity").options[0].text;
     }
}

function emptyForm(sender)
{
    document.getElementById(idprefix + "lat1").value = "";
    document.getElementById(idprefix + "long1").value = "";
    document.getElementById("cityZip").value = "";
    document.getElementById("cityName").value = "";
    document.getElementById("selectCity").style.display = "none";
    
    if(sender.toString().toLowerCase() == "postcode")
    {
        document.getElementById("txtCity").innerHTML = "";
    }
}

//called when chosing city in dropdownlist
function setCity(obj)
{
   document.getElementById("cityZip").value = obj.options[obj.selectedIndex].value; 
   document.getElementById("cityName").value = obj.options[obj.selectedIndex].text;
   //document.getElementById("txtCity").innerHTML = obj.options[obj.selectedIndex].text;
      
}
