﻿if (typeof eCommerce == "undefined") {
    var eCommerce = {};
}

eCommerce.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=eCommerce;

        for (j=(d[0] == "eCommerce") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

// Initial global eCommerce namespaces
(function() {
    eCommerce.namespace("ajax");
})();

// AJAX namespace
eCommerce.ajax.manager =
{
    init:function() {
        eCommerce.namespace("ajax.calls");
        // Register standard services
        //var ajaxpath = "/eCommerce.sample/WebServices/eCommerceStoreProxy.asmx";
        //this.register("GetComments",path,"GET","guid","page","sortDir");
        this.register("GetCategories",ajaxpath,"POST","StoreID","CartID");
        this.register("GetProductsByCategory",ajaxpath,"POST","StoreID","categoryID");
        this.register("GetProductDetails",ajaxpath,"POST","StoreId","productID");
        this.register("GiveProductReview",ajaxpath,"POST","StoreId","productID");
        this.register("GetShoppingCart",ajaxpath,"POST","StoreId","cartId");
        this.register("AddItemToCart",ajaxpath,"POST","StoreId","productId","cartId");
        this.register("AddItemQuantityToCart",ajaxpath,"POST","StoreId","productId","Quantity","cartId");
        this.register("UpdateCartItem",ajaxpath,"POST","StoreId","productId","cartId","quantity");
        this.register("GetSearchResults",ajaxpath,"POST","StoreId","SearchString");
        this.register("GetFeaturedProductsByStore",ajaxpath,"POST","StoreId","ShowPrice","Caption");
        this.register("GetFeaturedProductsByProductList",ajaxpath,"POST","StoreId","ProductList","ShowPrice","Caption","PostPage");
    },
    
    register:function(func, path, method) {
        if (!method){method="GET";}
        var obj = eCommerce.namespace.calls;
        var argCount = arguments.length - 3;
        var args = arguments;
        eCommerce.ajax.calls[func] = function() {
            var C = "";
            for (var i=0; i<argCount; i++) {
                if (i>0){C+="&";}
                var param = [];
                param[param.length] = args[i+3];
                param[param.length] = escape(arguments[i]);
                C += param.join("=");
            }
            var p = path + "/" + func;
            var onComplete = arguments[arguments.length-2];
            var onError = arguments[arguments.length-1];
            var vals = [];
            vals[vals.length] = p;
            vals[vals.length] = C;
            vals[vals.length] = eCommerce.ajax.manager.completeHandler;
            vals[vals.length] = eCommerce.ajax.manager.errorHandler;
            vals[vals.length] = onComplete;
            vals[vals.length] = onError;
            vals[vals.length] = method;
            return eCommerce.ajax.manager.call.apply(null,vals);            
        }
    },
    
    completeHandler:function(z, l0) {
        if (z.Xml && z.Xml.childNodes.length > 0){            
            var i0 = z.Xml.lastChild;
            var I0 = i0.tagName;
            var o1 = new eCommerce.ajax.manager.responseParser;
            var O1 = o1.ParseNode(i0);
            if (typeof l0 == "function") {
                l0(O1, z.Xml, z.Text);
            }
        } else {
            l0(null, null, null);
        }
    },
    
    errorHandler:function(z, l1) {
        if (typeof l1 == "function") { l1(z); } 
    },
    
    call:function(url,z,Ic,onError,l0,l1,method) {
        try {
            var od = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
            if (od == null) {
                return;
            }
            if (method=="GET")
                url = url + "?" + z;            
            od.open(method, url, true);
            od.onreadystatechange = function () {eCommerce.ajax.manager.onreadystatechange(od, Ic, onError, l0, l1);};
            if (method=="POST") {
                od.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                od.send(z);
            }
            else
                od.send(null);
        } catch (ex) {
            if (typeof onError == "function") {
                var e = {ErrorCode:"", ErrorText:ex.message, message:ex.message, Text:"", Xml:""};
                onError(e);
            }
        }
    },
    
    onreadystatechange:function(oe, Ic, onError, l0, l1) { 
        ld = function (oe) { try { if (oe && oe.status == 404) { var Oe; Oe = "Ajax callback error: source url not found! \n\r\n\rPlease verify if you are using any URL-rewriting code and set the AjaxUrl property to match the URL you need."; var error = new Error(Oe); throw error; return; } } catch (ex) { } };
        try { if (oe == null || oe.readyState != 4) { return; } ld(oe); if (oe.status != 200 && typeof onError == "function") { var e = {ErrorCode:oe.status, ErrorText:oe.statusText, message:oe.statusText, Text:oe.responseText, Xml:oe.le}; onError(e, l1); return; } if (typeof Ic == "function") { var e = {Text:oe.responseText, Xml:oe.responseXML}; Ic(e, l0); } } catch (ex) { if (typeof onError == "function") { var e = {ErrorCode:"", ErrorText:ex.message, message:ex.message, Text:"", Xml:""}; onError(e); } } if (oe != null) { oe.onreadystatechange = function(){}; } 
    },
    
    responseParser:function(){eCommerce.ajax.manager.responseParser.prototype.ParseNode = function (node) { if (this.i1(node)) { return this.I1(node, "", node.tagName); } else if (this.o2(node)) { var O2 = []; for (var i = 0; i < node.childNodes.length; i++) { if (this.l2(node.childNodes[i])) { continue; } var nodeValue = this.i2(node.childNodes[i]); O2[O2.length] = nodeValue; } return O2; } else if (this.I2(node)) { var o3 = this.O3(node, "xs", "schema")[0]; var l3 = this.O3(node, "diffgr", "diffgram")[0]; var i3 = this.I3(o3); return this.o4(l3, i3); } else { var O4 = {}; var propertyName = null; var l4 = null; for (var i = 0; i < node.childNodes.length; i++) { if (this.l2(node.childNodes[i])) { continue; } propertyName = node.childNodes[i].tagName; l4 = this.ParseNode(node.childNodes[i]); O4[propertyName] = l4; } return O4; } };
        eCommerce.ajax.manager.responseParser.prototype.ParseDateTimeISO8601 = function (l8) { var i8 = /([\x30-\x39]{4})(\x2d([\x30-\x39]{2})(\x2d([\x30-\x39]{2})(\x54([\x30-\x39]{2})\x3a([\x30-\x39]{2})(\x3a([\x30-\x39]{2})(\x2e([\x30-\x39]+))?)?(\x5a|(([-\x2b])([\x30-\x39]{2})\x3a([\x30-\x39]{2})))?)?)?)?/i; var I8 = l8.match(i8); var o9 = 0; var O9 = new Date(I8[1], 0, 1); if (I8[3]) { O9.setMonth(I8[3] - 1); } if (I8[5]) { O9.setDate(I8[5]); } if (I8[7]) { O9.setHours(I8[7]); } if (I8[8]) { O9.setMinutes(I8[8]); } if (I8[10]) { O9.setSeconds(I8[10]); } if (I8[12]) { O9.setMilliseconds(Number("0." + I8[12]) * 1000); } if (I8[14]) { o9 = Number(I8[16]) * 60 + Number(I8[17]); o9 *= I8[15] == "-" ? 1 : -1; } o9 -= O9.getTimezoneOffset(); time = Number(O9) + o9 * 60 * 1000; var l9 = new Date; l9.setTime(Number(time)); return l9; };
        eCommerce.ajax.manager.responseParser.prototype.I1 = function (node, I7, tagName) { if (I7.indexOf(":int") != -1 || tagName == "int") { return parseInt(this.o8(node)); } else if (I7.indexOf(":float") != -1 || tagName == "float" || I7.indexOf(":double") != -1 || tagName == "double") { return parseFloat(this.o8(node)); } else if (I7.indexOf(":boolean") != -1 || tagName == "boolean") { return this.o8(node) == "true"; } else if (I7.indexOf(":dateTime") != -1 || tagName == "dateTime") { return this.ParseDateTimeISO8601(this.o8(node)); } else if (!this.i1(node)) { return this.ParseNode(node); } else { return this.o8(node); } };
        eCommerce.ajax.manager.responseParser.prototype.I2 = function (node) { var ia = this.O3(node, "xs", "schema"); var Ia = this.O3(node, "diffgr", "diffgram"); if (ia.length == 1 && Ia.length == 1 && ia[0].parentNode == node && Ia[0].parentNode == node) { return true; } else { return false; } };
        eCommerce.ajax.manager.responseParser.prototype.I3 = function (node) { var I5 = this.O3(node, "xs", "choice")[0]; var o6 = {}; var O6 = null; var l6 = null; var O5 = null; for (var i = 0; i < I5.childNodes.length; i++) { if (this.i1(I5.childNodes[i])) { continue; } O6 = I5.childNodes[i]; l6 = this.i6(O6); O5 = O6.getAttribute("name"); o6[O5] = l6; } return o6; };
        eCommerce.ajax.manager.responseParser.prototype.I9 = function (node) { if (node == null) { return null; } if (this.l2(node)) { return this.I9(node.nextSibling); } else { return node; } };
        eCommerce.ajax.manager.responseParser.prototype.O3 = function (node, Oa, tagName) { if (document.all && !window.opera) { var la = Oa + ":" + tagName; var elements = node.getElementsByTagName(la); } else { var elements = node.getElementsByTagName(tagName); } return elements; };
        eCommerce.ajax.manager.responseParser.prototype.i1 = function (node) { if (node.childNodes.length == 0) { return true; } for (var i = 0; i < node.childNodes.length; i++) { if (node.childNodes[i].nodeType != 3) { return false; } } return true; };
        eCommerce.ajax.manager.responseParser.prototype.i2 = function (node) { var I7 = "" + node.getAttribute("xsi:type"); var tagName = node.tagName; return this.I1(node, I7, tagName); };
        eCommerce.ajax.manager.responseParser.prototype.i5 = function (node, l7) { var i7 = {}; var o7 = null; var I7 = null; for (var i = 0; i < node.childNodes.length; i++) { if (this.l2(node.childNodes[i])) { continue; } o7 = node.childNodes[i].tagName; I7 = l7[o7]; i7[o7] = this.I1(node.childNodes[i], I7); } return i7; };
        eCommerce.ajax.manager.responseParser.prototype.i6 = function (node) { var I6 = this.O3(node, "xs", "element"); var l6 = {}; var o7 = null; var O7 = null; for (var i = 0; i < I6.length; i++) { o7 = I6[i].getAttribute("name"); O7 = I6[i].getAttribute("type"); l6[o7] = O7; } return l6; };
        eCommerce.ajax.manager.responseParser.prototype.l2 = function (node) { if (this.i1(node) && typeof node.tagName == "undefined") { return true; } else { return false; } };
        eCommerce.ajax.manager.responseParser.prototype.o2 = function (node) { var firstChild = node.firstChild; var i9 = this.I9(firstChild); var tagName = i9.tagName; tagName = tagName.toLowerCase(); if (node.childNodes.length <= 1) { return false; } for (var i = 0, oa = node.childNodes.length; i < oa; i++) { if (this.l2(node.childNodes[i])) { continue; } if (typeof node.childNodes[i].tagName == "undefined" || node.childNodes[i].tagName.toLowerCase() != tagName) { return false; } } return true; };
        eCommerce.ajax.manager.responseParser.prototype.o4 = function (node, i3) { var i4 = {}; var I4 = null; var o5 = null; for (var O5 in i3) { if (typeof O5 != "string") { continue; } o5 = []; I4 = node.getElementsByTagName(O5); for (var i = 0; i < I4.length; i++) { var l5 = I4[i].parentNode.tagName; if (l5 != "diffgr:before" && l5 != "diffgr:error") { o5[o5.length] = this.i5(I4[i], i3[O5]); } } i4[O5] = o5; } return i4; };        
        eCommerce.ajax.manager.responseParser.prototype.o8 = function (O8) { 
            if (O8.firstChild != null) { 
                var nodeValue = ""; 
                for (var i = 0; i < O8.childNodes.length; i++) { 
                    nodeValue += O8.childNodes[i].nodeValue; 
                } 
                return nodeValue; 
            } else { 
                return O8.nodeValue; 
            } 
        }
    }
}
eCommerce.ajax.manager.init();