﻿var XMLUtilities = (function() {
    function fnXMLUtilitiesConstructor() {
    
    }
    
    fnXMLUtilitiesConstructor.createIEXMLDocument = function() {
        var oXML;
        
        if (window.ActiveXObject) {
		    oXML = Try.these(
			    function() { return new ActiveXObject("MSXML2.DOMDocument.6.0") },
			    function() { return new ActiveXObject("MSXML2.DOMDocument.5.0") },
			    function() { return new ActiveXObject("MSXML2.DOMDocument.4.0") },
			    function() { return new ActiveXObject("MSXML2.DOMDocument.3.0") },
			    function() { return new ActiveXObject("MSXML2.DOMDocument") },
			    function() { return new ActiveXObject("MSXML.DOMDocument") }
		    );
	    }
        
        return oXML;
    }
    
    fnXMLUtilitiesConstructor.createXMLDocument = function() {
        var oXML = fnXMLUtilitiesConstructor.createIEXMLDocument();
	    if (!oXML)
		    oXML = document.implementation.createDocument("", "", null);
		    
	    oXML.async = false;
	    
	    return oXML;
    }
    
    fnXMLUtilitiesConstructor.createXMLDocumentWithRootNode = function(sRootNode, oAttributes) {
        var oXML = fnXMLUtilitiesConstructor.createXMLDocument();
        var oRootNode = oXML.createElement(sRootNode);
        for (var sAttribute in oAttributes) {
            oRootNode.setAttribute(sAttribute, oAttributes[sAttribute]);
        }
        oXML.appendChild(oRootNode);
        return oXML;
    }
    
    fnXMLUtilitiesConstructor.createXMLDocumentFromString = function(sXML) {
        var oXML = fnXMLUtilitiesConstructor.createIEXMLDocument();
        
        if (oXML) {
            oXML.async = false;
		    oXML.loadXML(sXML);
        }
        else {
            var oParser = new DOMParser();
			oXML = oParser.parseFromString(sXML, "text/xml");
        }
	    
	    return oXML;
    }
    
    fnXMLUtilitiesConstructor.getXMLString = function(oXML) {
        return (window.ActiveXObject ? oXML.xml : (new XMLSerializer()).serializeToString(oXML));
    }
    
    return fnXMLUtilitiesConstructor;
})();