//  Function to retrieve data returned by 'url'
//  - uses the function defined by 'userDisplayFunction' to format output
//  - populates the document element defined by htmlelementid   
function ajaxRead(htmlelementid, url, userDisplayFunction, userDisplayFunctionExtra, htmlelementidextra){
    var xmlObj = null;

    url = url + '&' + 'random=' + Math.random();

    if(window.XMLHttpRequest){
        xmlObj = new XMLHttpRequest();
    } else if(window.ActiveXObject){
        xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        return;
    }

    var wait_text = "Please wait...";

    xmlObj.onreadystatechange = function() {
        if (xmlObj.readyState == 4){
            eval(userDisplayFunction + "(htmlelementid, xmlObj.responseText)" + ";");
            if (userDisplayFunctionExtra && htmlelementidextra) {
                eval(userDisplayFunctionExtra + "(htmlelementidextra, xmlObj)" + ";");
            }
        } else {
            eval(userDisplayFunction + "(htmlelementid, wait_text)" + ";");
        }
    }

    xmlObj.open ('GET',url, true);
    xmlObj.send ('');
}

//  custom function for the shipping estimate that simply inserts data into the element defined by 'obj' on the user's html page
function updateShippingEstimateText(obj, data){
    var text = "<b>" + data + "</b>";
    document.getElementById(obj).innerHTML=text; 
}

//  custom function for the shipping estimate that simply inserts any extra data into the element defined by 'obj' on the user's html page
//  the xmlObj is also sent in so that the responseText can be parsed to ensure that there are no errors
function ShippingEstimateTextExtra(obj, xmlObj){
    var text = 'These costs are estimates based on shipping methods available to your ZIP code. You will be able to choose your shipping method later in the checkout process.';
    var errorRegExp = /error/i;
    if (!errorRegExp.test(xmlObj.responseText)) { 
        document.getElementById(obj).innerHTML=text; 
    }
}

//  custom function for the email me when this is in stock that simply inserts data into the element defined by 'obj' on the user's html page
function updateEmailText(obj, data){
    var text = data;
    document.getElementById(obj).innerHTML=text; 
}
