//*******************************************************************************************************
//This function handles simple AJAX calls to fetch HTML from utility pages into another HTML Element
//
// ObjID : The ID of the HTML element the response should be added to
// LoadingHTML : The temporary HTML to be shown whilst the call is executing
// ErrorHTML : The HTML to be returned if an error occurs
// remoteURL : The URL of the Utility that returns the HTML

function ajaxInsertRemoteHTML(objID, LoadingHTML, ErrorHTML, remoteURL) {
    var obj = document.getElementById(objID);
     if (obj && obj.innerHTML) {
        obj.innerHTML = LoadingHTML;
        remoteURL = remoteURL + "&dt=" + encodeURIComponent(new Date());
        var xmlhttp= window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	    xmlhttp.open("GET",remoteURL,true);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	    xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                if(xmlhttp.status == 200) {
                    obj.innerHTML = '';
                    var myElement = document.createElement('DIV');
                    myElement.innerHTML = xmlhttp.responseText
                    obj.appendChild(myElement);
                } else {
                    obj.innerHTML = ErrorHTML;
                }
            }
        };
	    xmlhttp.send(null);
    }
}

function ajaxGetRemoteHTML(ErrorHTML, remoteURL, callbackFunction) {
    remoteURL = remoteURL + "&dt=" + encodeURIComponent(new Date());
    var xmlhttp= window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
    xmlhttp.open("GET",remoteURL,true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if(xmlhttp.status == 200) {
                eval(callbackFunction + "('" + xmlhttp.responseText + "');");
            } else {
                eval(callbackFunction + "('" + ErrorHTML +  "');");
            }
        }
    };
    xmlhttp.send(null);
    
}

function ajaxAddToBasket(remoteURL, updateURL) {

    ajaxInsertRemoteHTML('divAddMessageOuter', 'Adding to basket', 'There was an error adding to your basket', remoteURL);
    ajaxInsertRemoteHTML('div_Basket', 'Updating your basket', 'There was an error updating to your basket', updateURL);

    document.getElementById("div_DarkScreen").style.display = "block";
    document.getElementById("divAddMessageOuter").style.display = "block";
    document.getElementById("div_DarkScreen").style.height = document.body.offsetHeight + "px";
}

function closeSignUpBox() {
	document.getElementById("div_DarkScreen").style.display = "none";
	document.getElementById("divAddMessageOuter").style.display = "none";
};
