﻿function ajaxServiceCall(requestName, callbackFunction, parameterArray) {
    var xmlHttpRequest = new XMLHttpRequest();
    var currentDate = new Date();
    var requestUrl = ajaxServiceLocation + '?request=' + requestName;
    if(parameterArray)
        for(var key in parameterArray)
            requestUrl += '&' + key + '=' + parameterArray[key];
    requestUrl += '&ts=' + currentDate.getTime(); // because Internet Explorer incorrectly caches these
    
    xmlHttpRequest.onreadystatechange = function() {
            if(xmlHttpRequest.readyState == 4) {
                if(xmlHttpRequest.status == 200) {
                    var xmlDoc = null;
                    
                    if (window.ActiveXObject) {
                      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                      xmlDoc.async="false";
                      xmlDoc.loadXML(xmlHttpRequest.responseText);
                    } else {
                      xmlDoc = (new DOMParser()).parseFromString(xmlHttpRequest.responseText, "text/xml");
                    }
                    
                    var errorMessage = getXPathString(xmlDoc, "/error/text()");
                    if(!errorMessage) callbackFunction(xmlDoc);
                    
                    // change the mouse cursor back.
                    document.body.style.cursor = 'auto';
                }
                else {
                    alert('Unable to contact 180s Ajax Service.\nStatus code was ' + xmlHttpRequest.status);
                    document.body.style.cursor = 'auto'; // change the mouse cursor back.
                }
            }
    };
    
    xmlHttpRequest.open('GET', requestUrl, true); // test
    xmlHttpRequest.send(null);
    document.body.style.cursor = 'wait'; // change the mouse cursor to the hourglass.
}

function getFriendlyDatabaseError(errorMessage) {
    if(/\WCK_relCartToProductInstance_quantity\W/.test(errorMessage)) return 'You cannot have more than 99 of any item in your shopping cart.';
    else if(/^.*INSTANCE_UNAVAILABLE.*$/.test(errorMessage)) return 'This particular size/gender/color combination is not availabe for sale.';
    else if(/^.*PRODUCT_UNAVAILABLE.*$/.test(errorMessage)) return 'This product is not availabe for sale.';
    else if(/^.*INSTANCE_NOW_UNAVAILABLE.*$/.test(errorMessage)) return 'This particular size/gender/color combination is no longer availabe for sale.';
    else if(/^.*PRODUCT_NOW_UNAVAILABLE.*$/.test(errorMessage)) return 'This product is no longer availabe for sale.';
    else if(/^.*CART_WEIGHT.*$/.test(errorMessage)) return 'Adding more of this item would put your cart over the weight limit.';
    else return errorMessage;
}

function getXPathString(xmlDoc, xPathQuery) {
    var element = xmlDoc.selectSingleNode(xPathQuery);
    return element ? element.nodeValue : false;
}

function getSingleXmlTagAttributes(xmlDoc, xPathTag, attributeArray) {
    var resultList = Array();
    for(var i = 0; i < attributeArray.length; ++i) {
        var attribute = attributeArray[i];
        resultList[attribute] = xmlDoc.selectSingleNode(xPathTag + '/@' + attribute).nodeValue;
    }
    
    return resultList;
}

function getMultipleXmlTagAttributes(xmlDoc, xPathTag, identifyingAttribute, attributeArray) {
    var resultTable = Array();
    attributeArray.push(identifyingAttribute);
    
    var identifyingAttributes = xmlDoc.selectNodes(xPathTag + '/@' + identifyingAttribute);
    for(var i = 0; i < identifyingAttributes.length; ++i) {
        var identifyingAttributeValue = identifyingAttributes[i].nodeValue;
        var rowList = Array();
        for(var j = 0; j < attributeArray.length; ++j) {
            var attribute = attributeArray[j];
            rowList[attribute] = xmlDoc.selectSingleNode(xPathTag + '[@' + identifyingAttribute + '=' + identifyingAttributeValue + ']/@' + attribute).nodeValue;
        }
        resultTable.push(rowList);
    }
    
    return resultTable;
}

