var cityArea;
var postalArea;
var http;
var postalContent;
var cityContent;
var txtZip;
var txtCity;

window.onload=function(){
	if(postalArea == null)
		postalArea = getObjectById('postalArea');
	
	if(cityArea == null)
		cityArea = getObjectById('cityArea');
	
	if(postalContent == null)
		postalContent = postalArea.innerHTML;
		
	if(cityContent == null)
		cityContent = cityArea.innerHTML;
}

function create_http_object()
{
    // This array is a list of all/most possible XMLHTTP types
    var ActiveXTypes = [
        "Microsoft.XMLHTTP",
        "MSXML2.XMLHTTP.5.0",
        "MSXML2.XMLHTTP.4.0",
        "MSXML2.XMLHTTP.3.0",
        "MSXML2.XMLHTTP"
    ];

    // Loop through the ActiveXTypes array and 
    for( var i = 0; i < ActiveXTypes.length; i++ )
    {
        // try to use the current XMLHTTP object,
        // if not it's not possible try the next
        try
        {
            return new ActiveXObject( ActiveXTypes[i] );
        }
        catch( e )
        { }
    }

    // none of them worked, try the function XMLHttpRequest()
    try
    {
        return new XMLHttpRequest();
    }
    catch( e )
    { }

    // It seems that the browser doesn't support AJAX Stuff
    return false;
}



function getPostalByCity(width){
	
	var txtCity = getObjectById('txtCity')
		
	var cityName = txtCity.name;
	var cityValue = txtCity.value;
	
	if(trim(cityValue) == "")
		return;
		
	if(http == null)
   		http = create_http_object();
		
    if(!http)
		return;
		
	postalArea.innerHTML = '<div style="width: '+width+'px; text-align: center;">' + 
						   '<img src="'+SITE_URL+'/images/body/working.gif" alt="bezig..." /></div>';
		
    var url = SITE_URL+'/script/postal/getPostalByCity.php';
	
	http.open("post", url, true);

	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 

	http.onreadystatechange = function(){

		if (http.readyState == 4){
			if (http.status && http.status == 200){
				var result = http.responseText;	
				postalArea.innerHTML = result;
			} 
		}

	}
	http.send(cityName+'='+cityValue+'&width='+(width+10));
}


function getCityByPostal(width){

	var txtZip = getObjectById('txtZip');
		
	var zipName = txtZip.name;
	var zipValue = txtZip.value;
	
	if(trim(zipValue) == "")
		return;

	if(http == null)
   		http = create_http_object();
		
    if(!http)
		return;
	
	cityArea.innerHTML = '<div style="width: '+width+'px; text-align: center;">' + 
						 '<img src="'+SITE_URL+'/images/body/working.gif" alt="bezig..." /></div>';
	
    var url = SITE_URL+'/script/postal/getCityByPostal.php';
		
	http.open("post", url, true);

	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 

	http.onreadystatechange = function(){

		if (http.readyState == 4){
			if (http.status && http.status == 200){
				var result = http.responseText;
				cityArea.innerHTML = result;
			} 
    	}

	}

	http.send(zipName+'='+zipValue+'&width='+width);
}

//Function to get elements by ID

function getObjectById(id) {
  var obj = false;
  if(document.getElementById) 
  	obj = document.getElementById(id);
  else if(document.all) 
  	obj = document.all[id];
  return obj;
}

function initCity(){
	var txtZip = getObjectById('txtZip');
	
	if(trim(txtZip.value) == "")
		cityArea.innerHTML = cityContent;
}

function initPostal(){	
	var txtCity = getObjectById('txtCity')

	if(trim(txtCity.value) == "")
		postalArea.innerHTML = postalContent;
}

function trim(s) 
{ 
    var l=0; var r=s.length -1; 
    while(l < s.length && s[l] == ' ') 
    {     l++; } 
    while(r > l && s[r] == ' ') 
    {     r-=1;     } 
    return s.substring(l, r+1); 
} 