function createRequestObject() {
    var tmpXmlHttpObject;
    
    //depending on what the browser supports, use the right way to create the XMLHttpRequest object
    if (window.XMLHttpRequest) { 
        // Mozilla, Safari would use this method ...
        tmpXmlHttpObject = new XMLHttpRequest();
	
    } else if (window.ActiveXObject) { 
        // IE would use this method ...
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return tmpXmlHttpObject;
}

//call the above function to create the XMLHttpRequest object
var http = createRequestObject();

function makeGetRequest(myRemoteCall) {
    //make a connection to the server ... specifying that you intend to make a GET request 
    //to the server. Specifiy the page name and the URL parameters to send
    http.open('get', this.remoteUrl);
	
    //assign a handler for the response
    http.onreadystatechange = bind(myRemoteCall, "processResponse");
	
    //actually send the request to the server
    http.send(null);
}

function remoteCall(remoteUrl, errorDiv, form){
    this.remoteUrl=remoteUrl;
    this.errorDiv=errorDiv;
    this.form=form;
}

remoteCall.prototype.makeGetRequest=makeGetRequest;
remoteCall.prototype.processResponse=processResponse;

function makeRequest(remoteAction){
    newsletter="";
    if(remoteAction == 'subscribeNew' || remoteAction == 'subscribeUpdate'){
        email=document.subscribe.email.value;
        newsletterBox=document.subscribe['newsletter[]'];
    
        for(var i=0; i < newsletterBox.length; i++){
            if(newsletterBox[i].checked)
                newsletter +=newsletterBox[i].value + ",";
        }

        url='/scripts/checkSubscribeInput.php?action='+remoteAction + '&email=' + email + '&newsletter=' + newsletter ;
        form=document.subscribe;
        myRemoteCall= new remoteCall(url, 'errorMesg', form);
    }
    else {
        old_email=document.changeEmail.old_email.value;
        new_email=document.changeEmail.new_email.value;
        
        url='scripts/checkSubscribeInput.php?action='+remoteAction + '&old_email=' + old_email + '&new_email=' + new_email ;
        form=document.changeEmail;
        myRemoteCall= new remoteCall(url, 'changeErrorMesg', form)
    }
    myRemoteCall.makeGetRequest(myRemoteCall);
}

function bind(toObject, methodName){
    return function(){return toObject[methodName]()}
}

function processResponse() {
    //check if the response has been received from the server
    if(http.readyState == 4){
        //read and assign the response from the server
        var response = http.responseText;
        if(response == "valid"){
            this.form.submit();
        }
        else{
            document.getElementById(this.errorDiv).innerHTML = response; 
        }
    }
}
