﻿var hfCallbackRef;

function setCallbackReferrer(p_objElem) {
    hfCallbackRef = p_objElem;
}

function xmlNodeGeneratorSingleInput(p_node, p_arg) {
    var strXMLNode;
    if (p_node != null) {
        strXMLNode = '<' + p_node + '>' + ((p_arg != null) ? ((p_arg.replace(/&/g, '&amp;')).replace(/"/g, '&quot;')).replace(/;amp/g, '') : '') + '</' + p_node + '>';
    }
    return strXMLNode;
}

function xmlNodeGeneratorArryInput(p_nodes, p_args) {
    var XMLNodes = new Array();
    if (p_nodes != null) {
        for (var nodeCnt = 0; nodeCnt < p_nodes.length; nodeCnt++) {
            if (p_nodes[nodeCnt] != null) {
                XMLNodes.push('<' + p_nodes[nodeCnt] + '>' + ((p_args != null && p_args[nodeCnt] != null) ? ((p_args[nodeCnt].toString().replace(/&/g, '&amp;')).replace(/"/g, '&quot;')).replace(/;amp/g, '') : '') + '</' + p_nodes[nodeCnt] + '>');
            }
        }
    }
    return XMLNodes.join().replace(",", "");
}

function xmlNodeGeneratorCSVInput(p_nodes, p_args) {
    var strXMLNodeOpen;
    var strXMLNodeClose;
    var strXMLNode;
    if (p_nodes != null) {
        var arrXMLTag = p_nodes.split(',');
        strXMLNodeOpen = '<' + arrXMLTag.join('><') + '>';
        strXMLNodeClose = '</' + (arrXMLTag.reverse()).join('></') + '>';
        var argJoin = strXMLNodeClose + strXMLNodeOpen;
        strXMLNode = strXMLNodeOpen + ((p_args != null) ? (p_args.split(',')).join(argJoin) : '') + strXMLNodeClose;
    }
    return strXMLNode;
}

function addXMLParentNodeArrayInput(p_parentNodes, p_arg) {
    var strXMLNode;
    if (p_parentNodes != null) {
        //        for (var nodeCnt = 0; nodeCnt < p_parentNodes.length; nodeCnt++) {
        //            if (p_parentNodes[nodeCnt] != null) { XMLNodes.push('<' + p_parentNodes[nodeCnt] + '>'); }
        //        }
        strXMLNode = '<' + p_parentNodes.join('><') + '>' + ((p_arg != null) ? p_arg : '') + '</' + (p_parentNodes.reverse()).join('></') + '>';
    }
    return strXMLNode;
}

function xmlGenerator(p_strXMLContent) {
    var strXML = '<?xml version="1.0" encoding="UTF-8" ?>';
    if (p_strXMLContent != null) { strXML += p_strXMLContent; }
    return strXML;
}

function xmlObjParser(p_strClientCallbackResponse) {
    //XML POROCESSING STARTS HERE//////////
    if (isMozilla) {
        var parser = new DOMParser();
        xmlDOC = parser.parseFromString(p_strClientCallbackResponse, "text/xml");
    }
    else if (!isMozilla) {
        xmlDOC = new ActiveXObject("Microsoft.XMLDOM");
        xmlDOC.async = "false";
        xmlDOC.loadXML(p_strClientCallbackResponse);
    }
    return xmlDOC.documentElement;
}

function xmlValueDecode(strXMLValue) {
    return ((((strXMLValue.replace(/;amp/g, '')).replace(/&lt;/g, '<')).replace(/&gt;/g, '>')).replace(/&quot;/g, '"')).replace(/&amp;/g, '&');
}

function getXMLNodeValue(p_XMLChildNode) {
    return (isMozilla) ? p_XMLChildNode.textContent : p_XMLChildNode.text;
}

function MasterCallbackPreProcessing(p_args, p_subLoader, p_gblLoader) {
    initSiteLoader(p_gblLoader);
    setElementDisplay(siteLoader, true);
    setElementDisplay(p_subLoader, true);
    hfCallbackRef.value = 1;
    MasterCallbackProcessing(p_args);
    return false;
}

function MasterCallbackError() {
    alert('Error');
}

function MasterCallbackPostProcessing(p_strClientCallbackResponse) {
    var strTrkElement;
    xmlObj = xmlObjParser(p_strClientCallbackResponse);
    if (xmlObj != null) {
        if (xmlObj.childNodes.length > 0 && xmlObj.childNodes[0].hasChildNodes) {
            strTrkElement = getXMLNodeValue(xmlObj.childNodes[0].childNodes[0]);
        }
    }
    setElementDisplay(siteLoader, false);
    if (strTrkElement != null) {
        if (cartAction.join('').indexOf(strTrkElement) > -1) { RefreshMiniLists(xmlObj); }
    }
}

function ChildCallbackPreProcessing(p_args, p_subLoader, p_gblLoader) {
    initSiteLoader(p_gblLoader);
    setElementDisplay(siteLoader, true);
    setElementDisplay(p_subLoader, true);
    hfCallbackRef.value = 1;
    ChildCallbackProcessing(p_args);
    return false;
}

function ChildCallbackError() {
    alert('Error');
}

function ChildCallbackPostProcessing(p_strClientCallbackResponse) {
    var strTrkElement;
    xmlObj = xmlObjParser(p_strClientCallbackResponse);
    if (xmlObj != null) {
        if (xmlObj.childNodes.length > 0 && xmlObj.childNodes[0].hasChildNodes) {
            strTrkElement = getXMLNodeValue(xmlObj.childNodes[0].childNodes[0]);
        }
    }
    setElementDisplay(siteLoader, false);
    if (strTrkElement != null) {
        if (cartAction.join('').indexOf(strTrkElement) > -1) { RefreshLists(xmlObj); }
    }
    //if (intCallbackRepCnt == 0) { endSiteProcessIndicator(); }   
}
