﻿Xml = {

    _MSXML_PROGIDS:["Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"],
    parse:(function(xmlString) {
        var xmlRetDoc = null;
	    try {
		    // non ie browser
		    var parser = new DOMParser();
		    xmlRetDoc = parser.parseFromString(xmlString, "text/xml");
		    var roottag = xmlRetDoc.documentElement;
            if ((roottag.tagName == "parserError") || (roottag.namespaceURI == "http://www.mozilla.org/newlayout/xml/parsererror.xml")){
                throw "Parsing Error!\n\n" + this.toString(xmlRetDoc);
                return null;
            }
	    }
	    catch(e) {
		    for (var i = 0; i < this._MSXML_PROGIDS.length; ++i) {
			    try {
				    xmlRetDoc = new ActiveXObject(this._MSXML_PROGIDS[i]);
				    xmlRetDoc.async = false;
                    xmlRetDoc.loadXML(xmlString);
				    if (xmlRetDoc.parseError != 0) 
                    {
	                    throw xmlRetDoc.parseError.errorCode + " " + xmlRetDoc.parseError.reason;
	                    return null;
	                }
				    break;
			    }
			    catch(e){}
		    }
	    }
	    finally {
	        if(xmlRetDoc == null) {
	            alert("Unable to create an Xml Parser, your browser does not support this feature");
	        }
		    return xmlRetDoc;
	    }
    }),
    toString:(function(xmlDoc) {
        if(xmlDoc.xml)
        {
            return xmlDoc.xml; 
        }
        else
        {
            var s = new XMLSerializer();
            return s.serializeToString(xmlDoc); 
        }
        return "Unable to convert document to string";
    })
};
