// 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 processemail() {
    
    
    // proceed only if the xmlHttp object isn't busy
    if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
        
        // retrieve the e-mail address typed by the user on the form
        email = encodeURIComponent(document.getElementById("Email").value);
        // execute the emailcheck.php page from the server
        xmlHttp.open("GET", "formvalidation/emailchecker.php?email="+email, true);
        //define the method to handle server response
        xmlHttp.onreadystatechange = handleEmailServerResponse;
        // post variables
        xmlHttp.send(null);
    } else {
        // if the connection is busy, try again after one second
        setTimeout('processemail()', 1000);
    }
    
    
}

// executed automatically when a message is receieved from the server
function handleEmailServerResponse() {
    
    // 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
            var xmlResponse = xmlHttp.responseXML;
            // obtain the document element (the root element) of the XML structure
            var 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("divEmail");
            divContent.childNodes[0].nodeValue = responseMessage;
            
            // We need to update the color of the text box border for visual representation of validity
            var Email = document.getElementById("Email");
            var color = xmlDocumentElement.getAttribute("color");
            var disabled =  xmlDocumentElement.getAttribute("disabled");
            
            if(disabled == true) {
                        divContent.setAttribute("title", "true");  
            }  else {
                        divContent.setAttribute("title", "false");
            }
                
            Email.style.border = color;
            
            // restart sequence
            setTimeout('processemail()', 500);
        } else {
            
            alert("There was a problem accessing the server: " + xmlHttp.statusText);
            
        }
        
    }
    
}