// Overrides Cantoche XML Callback
function LA_AS_CallbackXML(xml){
    // First check if xml exists and is not empty
    if(typeof xml == 'undefined')
        return;
    if(xml == "")
        return;

    // Parse the XML string (First unescape!), create DOM tree
    var lingubot = getDomAdapter().parseXml(unescape(xml));
    
    // Get jsonload that should be executed with this response
    var jsOnload = getXmlParameter(lingubot, "jsonload");
    if (jsOnload != "") {
        // Get jsGeneral that should be included in case it is not empty
        var jsGeneral = getXmlParameter(lingubot, "jsgeneral");
        
        // Remove if exists previous dynamically added onload script
        var oldScript = document.getElementById('onloadScript');
        if (oldScript != null) { 
            oldScript.parentNode.removeChild(oldScript);
        }
        
        // Dynamically add new script part that executes functionOnload, including also jsgeneral scripts
        var newScript = document.createElement('script');
        newScript.text = jsGeneral + jsOnload;
        newScript.id = 'onloadScript';
        var headTag = document.getElementsByTagName('head')[0];
        headTag.appendChild(newScript);
    }
    
}

// Returns the element <parameterName> and "" if empty
// parameterName must exist!
function getXmlParameter(xmlobject, parameterName) {
    var xmlParameterObject = xmlobject.getElementsByTagName(parameterName)[0];
    if(xmlParameterObject == null || xmlParameterObject.firstChild == null || typeof xmlParameterObject == 'undefined' || typeof xmlParameterObject.firstChild == 'undefined')
       return ""
    return xmlParameterObject.firstChild.data;
}

// Cross-Browser XML DOM Parser
function getDomAdapter() {
    var adapter = '';
    if ('undefined' != typeof ActiveXObject) {
        return new (function () {
            this.createDocument = function () {
                var names = ["Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"];
                for (var key in names) {
                    try {
                        return new ActiveXObject(names[key]);
                    } catch (e) {}
                }
                throw new Error('Unable to create DOMDocument');
            };
            this.serialize = function (doc) {
                return doc.xml;
            };
            this.parseXml = function (xml) {
                var doc = this.createDocument();
                if (!doc.loadXML(xml)) {
                    throw new Error('Parse error');
                }
                return doc;
            };
        })();
    } else if ('undefined' != typeof document && document.implementation && document.implementation.createDocument && 'undefined' != typeof DOMParser) {
        return new (function () {
            this.createDocument = function () {
                return document.implementation.createDocument("", "", null);
            };
            this.serialize = function (doc) {
                return new XMLSerializer().serializeToString(doc);
            };
            this.parseXml = function (xml) {
                var doc = new DOMParser().parseFromString(xml, "text/xml");
                if ("parsererror" == doc.documentElement.nodeName) {
                    throw new Error('Parse error');
                }
                return doc;
            };
        })();
    } else {
        throw new Error('Unable to select the DOM adapter');
    }
}