// JavaScript Document
var xmlHttpGetMessages = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
    // will store the reference to the XMLHttpRequest object
    var xmlHttp;
    // this should work for all browsers except IE6 and older
    try
    {
        // try to create XMLHttpRequest object
        xmlHttp = new XMLHttpRequest();
    }
    catch(e)
    {
        // assume IE6 or older
        var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
        "MSXML2.XMLHTTP.5.0",
        "MSXML2.XMLHTTP.4.0",
        "MSXML2.XMLHTTP.3.0",
        "MSXML2.XMLHTTP",
        "Microsoft.XMLHTTP");
        // try every prog id until one works
        for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
        {
            try
            {
                // try to create XMLHttpRequest object
                xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
            }
            catch (e) {}
        }
    }
    // return the created object or display an error message
    if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
    else
    return xmlHttp;
}


function getAllAdresses()
{
	var adresUrl = "adresses.php";
	
	if(xmlHttpGetMessages)
    {
        
		// don't start another server operation if such an operation
		// is already in progress
		if (xmlHttpGetMessages.readyState == 4 || xmlHttpGetMessages.readyState == 0)
		{
			// call the server page to execute the server-side operation
			xmlHttpGetMessages.open("GET", "adresses.php", true);
			//xmlHttpGetMessages.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttpGetMessages.onreadystatechange = setAdresses;
			xmlHttpGetMessages.send(null);
			
		}
		else
		{
			// we will check again for new messages
			setTimeout("getAdresses();", 1000);
		}
    }
}

function getAdresses()
{
	var pcode = document.getElementById("postcode").value;
	if(pcode == 'UW POSTCODE')
	{
		getAllAdresses();
	}
	
	pcode = pcode.replace(' ', '');
	alert(pcode);
	var adresUrl = "adresses.php?pcode="+pcode;
	
	if(xmlHttpGetMessages)
    {
        
		// don't start another server operation if such an operation
		// is already in progress
		if (xmlHttpGetMessages.readyState == 4 || xmlHttpGetMessages.readyState == 0)
		{
			// call the server page to execute the server-side operation
			
			xmlHttpGetMessages.open("GET", "adresses.php?pcode="+pcode, true);
			//xmlHttpGetMessages.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttpGetMessages.onreadystatechange = setAdresses;
			xmlHttpGetMessages.send(null);
			
		}
		else
		{
			// we will check again for new messages
			setTimeout("getAdresses();", 1000);
		}
    }
}


function setAdresses()
{
	if(xmlHttpGetMessages.readyState == 4)
	{
		//document.getElementById("tekstBlok").innerHTML = "";
		document.getElementById("tekstBlok").innerHTML = xmlHttpGetMessages.responseText;
	}
}
