// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();

// retrieves the XMLHttprequest object
function createXmlHttpRequestObject()
{
    
    // will store the reference to the XMLHttpRequest object
    var xmlHttp;
    // if running Internet Explorer
    if(window.ActiveXObject)
    {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            xmlHttp = false;
        }
    }
    
    // if running mozilla or other browsers
    
    else {
        
        try {
            
            xmlHttp = new XMLHttpRequest();
            
        } catch (e) {
            
            xmlHttp = false;
            
        }
        
    }
    
    // return the created object or display an error message
    if(!xmlHttp) {
        alert("Error creating the XMLHttpRequest object.");
    } else {
        return xmlHttp;
    }
    
}

// make asynchronous HTTP request using the XMLHttpRequest object
function processpostcode() {
    
    
    // proceed only if the xmlHttp object isn't busy
    if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
        
        // retrieve the country and post code typed by the user on the form
        country = encodeURIComponent(document.getElementById("Country").value);
        postcode = encodeURIComponent(document.getElementById("PostCode").value);
        // execute the postcodechecker.php page from the server
        xmlHttp.open("GET", "formvalidation/postcodechecker.php?postcode="+postcode+"&country="+country, true);
        //define the method to handle server response
        xmlHttp.onreadystatechange = handlePostCodeServerResponse;
        // post variables
        xmlHttp.send(null);
    } else {
        // if the connection is busy, try again after one second
        setTimeout('processpostcode()', 1000);
    }
    
    
}

// executed automatically when a message is receieved from the server
function handlePostCodeServerResponse() {
    
    // move forward only if the transaction has completed
    if(xmlHttp.readyState == 4) {
        
        // status of 200 indicates the transaction completed successfully
        if(xmlHttp.status == 200) {
            
            // extract the XML retrieved from the server
            xmlResponse = xmlHttp.responseXML;
            // obtain the document element (the root element) of the XML structure
            xmlDocumentElement = xmlResponse.documentElement;
               // get the text message which is in the first child of the document element
            var responseMessage = xmlDocumentElement.firstChild.nodeValue;
            // update the client display using the data received from the server
            var divContent = document.getElementById("divPostCode");
            divContent.childNodes[0].nodeValue = responseMessage;
            
            // We need to update the color of the text box border and the submit button activity for visual representation of validity
            var PostCode = document.getElementById("PostCode");
            var color = xmlDocumentElement.getAttribute("color");
            var disabled = xmlDocumentElement.getAttribute("disabled");
            if(disabled == true) {
                        divContent.setAttribute("title", "true");  
            }  else {
                        divContent.setAttribute("title", "false");
            }
            
            PostCode.style.border = color;
                
            // restart sequence
            setTimeout('processpostcode()', 1000);
        } else {
            
            alert("There was a problem accessing the server: " + xmlHttp.statusText);
            
        }
        
    }
    
}