// -----------------------------------------------------------
// - genericxml.js
// -
// - Generic XML functions
// -----------------------------------------------------------

// Tell Firefox to create an XML getter on XML documents so we can retrieve the XML string
if (document.implementation && document.implementation.createDocument)
{
    Document.prototype.__defineGetter__("xml", function () { return (new XMLSerializer()).serializeToString(this);});
    Element.prototype.__defineGetter__("xml", function () { return (new XMLSerializer()).serializeToString(this);});
    Node.prototype.__defineGetter__("xml", function () { return (new XMLSerializer()).serializeToString(this);});
}

function gxml_createdocument()
{
    if (document.implementation && document.implementation.createDocument)
    {
		newXmlDoc = document.implementation.createDocument("", "root", null);
		return newXmlDoc;
	}
	else if (window.ActiveXObject)
	{
		newXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        newXmlDoc.loadXML("<root></root>");
        return newXmlDoc;
 	}
	else
	{
		xmlError = new Error("Error creating XML Document. Your browser does not support XML.");
        throw xmlError;
	}
}

function gxml_setnodetext(document, node, text)
{
    textnode = document.createTextNode(text);
    node.appendChild(textnode);
}

function gxml_getnodetext(node)
{
    if(node.firstChild)
    {
        return node.firstChild.nodeValue;
    }
    else
    {
        return null;
    }
}

function gxml_urlencode(str) 
{
    str = escape(str);
    str = str.replace('+', '%2B');
    str = str.replace('%20', '+');
    str = str.replace('*', '%2A');
    str = str.replace('/', '%2F');
    str = str.replace('@', '%40');
    return str;
}    

function gxml_getelementsbyclassname(classname, node)  
{
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function gxml_createnamedelement(type, name) 
{
    var element = null;
    // Try the IE way; this fails on standards-compliant browsers
    try 
    {
      element = document.createElement('<'+type+' name="'+name+'">');
    } 
    catch (e) 
    { }

    if (!element || element.nodeName != type.toUpperCase()) 
    {
      // Non-IE browser; use canonical method to create named element
      element = document.createElement(type);
      element.name = name;
    }
    return element;
}

function gxml_loadxml(xmltext)
{
    try
    {
        xmlDocument = gxml_createdocument();
        xmlDocument.setProperty("ServerHTTPRequest", true);
        xmlDocument.loadXML(xmltext);
    }
    catch(e)
    {
        parser=new DOMParser();
        xmlDocument=parser.parseFromString(xmltext,"text/xml");
    }
    
    return xmlDocument;
}