// Functions to help with the document object model
var isIE = false;

// Checks to see if the response is a valid OPP2 XML Response
function isValidOPPResponse(objXML) {
	var objRoot = objXML.getElementsByTagName('OPP2XML');
	var blnValid = false;
	if (objRoot.length == 1) {
		blnValid = true;
	}
	return(blnValid);
}

// Creates a new XML Document (IE and FF safe)
function getXMLObject() {
	var objXML;
	// code for IE
	if (window.ActiveXObject) {
		objXML=new ActiveXObject("Microsoft.XMLDOM");
	} else if (document.implementation && document.implementation.createDocument) {
		// code for Mozilla, Firefox, Opera, etc.
		objXML=document.implementation.createDocument("","",null);
	} else { 
		alert('Your browser cannot handle this script');
	}
	objXML.async=false;
	//objXML.load(dname);
	return(objXML);
}

// Creates the XML HTTP Request Object, with checks for browsers
function getXMLHttpRequestObject() {
	var objAJAX = false;
	try {
		// Firefox, Opera 8.0+, Safari
		objAJAX=new XMLHttpRequest();
		isIE = false;
	} catch (e) {
		// Internet Explorer
		isIE = true;
    	try {
      		objAJAX=new ActiveXObject("Msxml2.XMLHTTP");
      	} catch (e) {
      		try {
				objAJAX=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				objAJAX = false;
        	}
      	}
    }
    return(objAJAX);
}


/**
 * Converts a HTML string to an array of nodes, so that they can be inserted into the DOM
 */
function convertHTMLToNodeArray(strHTML) {
	var objDiv = document.createElement('tbody');
	objDiv.innerHTML = strHTML;
	var arrNodes = [];
	
	for(var intX = 0; intX < objDiv.childNodes.length; intX++) {
		alert(objDiv.childNodes[intX]);
		arrNodes[arrNodes.length] = objDiv.childNodes[intX];
	}
	return(arrNodes);
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}



function XMLWriter()
{
    this.XML=[];
    this.Nodes=[];
    this.State="";
    this.FormatXML = function(Str)
    {
        if (Str)
            return Str.replace(/&/g, "&amp;").replace(/\"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
        return ""
    }
    this.BeginNode = function(Name)
    {
        if (!Name) return;
        if (this.State=="beg") this.XML.push(">");
        this.State="beg";
        this.Nodes.push(Name);
        this.XML.push("<"+Name);
    }
    this.EndNode = function()
    {
        if (this.State=="beg")
        {
            this.XML.push("/>");
            this.Nodes.pop();
        }
        else if (this.Nodes.length>0)
            this.XML.push("</"+this.Nodes.pop()+">");
        this.State="";
    }
    this.Attrib = function(Name, Value)
    {
        if (this.State!="beg" || !Name) return;
        this.XML.push(" "+Name+"=\""+this.FormatXML(Value)+"\"");
    }
    this.WriteString = function(Value)
    {
        if (this.State=="beg") this.XML.push(">");
        this.XML.push(this.FormatXML(Value));
        this.State="";
    }
    this.Node = function(Name, Value)
    {
        if (!Name) return;
        if (this.State=="beg") this.XML.push(">");
        this.XML.push((Value=="" || !Value)?"<"+Name+"/>":"<"+Name+">"+this.FormatXML(Value)+"</"+Name+">");
        this.State="";
    }
    this.Close = function()
    {
        while (this.Nodes.length>0)
            this.EndNode();
        this.State="closed";
    }
    this.ToString = function(){return this.XML.join("");}
}

// add item to select element the less
// elegant, but compatible way.
function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}
