﻿//********************* XML Document *********************\\
//Date Create : 1:30 PM 6/25/2007
//Author : Mostafa Houshmand
//Ver : 0.5 / Full ie support
//********************* XML Document *********************\\
function XMLDoc()
{    
    var doc;
    if(window.ActiveXObject)
    {
        //IE
	    doc = new ActiveXObject("Microsoft.XMLDOM");
	    doc.async = "false";
    }
    else
    {
        //Mozilla, Firefox, Opera, etc.
	    doc = new DOMParser();
	    //var doc = parser.parseFromString(node,"text/xml");
    }
    this.xmlDoc = doc;

    this.Load = function XMLDoc_Load(fileName)
    {
        var doc;
        if(window.ActiveXObject)
        {
            //IE
            this.xmlDoc.load(fileName);
            return this.xmlDoc;
        }
        else
        {
            //Mozilla, Firefox, Opera, etc.
	        return this.xmlDoc.load(fileName);
        }
    }

    this.LoadXml = function XMLDoc_LoadXml(value)
    {
        var doc;
        if(window.ActiveXObject)
        {
            //IE
            this.xmlDoc.loadXML(value);
            return this.xmlDoc;
        }
        else
        {
            //Mozilla, Firefox, Opera, etc.
	        return this.xmlDoc.parseFromString(value,"text/xml");
        }
    }
    
    this.CreateElement = function XMLDoc_CreateElement(nodeName)
    {
        return this.xmlDoc.createElement(nodeName);
    }

    this.SetAttribute = function XMLDoc_SetAttribute(node,name,value)
    {
        if(value == null) value = "";
        node.setAttribute(name,value);
    }

    this.GetAttribute = function XMLDoc_GetAttribute(node,name)
    {
        return node.getAttribute(name);
    }

    this.SetNodeValue = function XMLDoc_SetNodeValue(node,value)
    {
        var nodeValue = this.xmlDoc.createTextNode(value);
        node.appendChild(nodeValue);
    }

    this.GetNodeValue = function XMLDoc_GetNodeValue(node)
    {
        var nodeValue = node.nodeValue;
        if(nodeValue != null)
        {
            return nodeValue;
        }
        return "";
    }
    
    this.GetXml = function XMLDoc_GetXml(node)
    {
        return node.xml;
    }
    
    this.GetDocumentElement = function XMLDoc_GetDocumentElement()
    {
        if(this.xmlDoc != null)
            return this.xmlDoc.documentElement; 
        return null;
    }

    this.GetElement = function XMLDoc_GetElement(node,nodeName)
    {
        var elem = node.getElementsByTagName(nodeName);
        if(elem != null && elem.length > 0)
        {
            return elem[0];
        }
        return null;
    }

    this.GetElementByAttribute = function XMLDoc_GetElementByAttribute(node,nodeName,attributeName,attributeValue)
    {
        var elem = node.getElementsByTagName(nodeName);
        if(elem != null && elem.length > 0)
        {
            for(var i = 0; i < elem.length; i++)
            {
                if(this.GetAttribute(elem[i],attributeName) == attributeValue)
                {
                    return elem[i];
                }
            }
        }
        return null;
    }
    
    this.GetElements = function XMLDoc_GetElements(node,nodeName)
    {
        var elems = node.getElementsByTagName(nodeName);
        return elems;
    }

    this.AppendChild = function XMLDoc_AppendChild(node,childNode)
    {
        node.appendChild(childNode);
    }

    this.ReplaceChild = function XMLDoc_ReplaceChild(node,replaceNode)
    {
        node.parentNode.replaceChild(replaceNode,node);
    }
    
    this.CloneElement = function XMLDoc_CloneElement(node)
    {
        var cloned = node.cloneNode(true);
        return cloned;
    }

    this.RemoveChild = function XMLDoc_RemoveChild(parent, node)
    {
        var deleted = parent.removeChild(node);
        return deleted;
    }
}
//********************* XML Document *********************\\
