/*
The SoftAd Group
Copyright (c) 2000-2004 The SoftAd Group, Inc.  All rights reserved
*/
//To use this object the following files must be also included.
// CNObjectDOM.js
// HTMLDom.js (prototype inheritance, see [CCNObjectDOM.prototype = new HTMLDom;] at bottom.)

// -------------------------------------------------------------------------------
// File name        : CNObjectDOM.js
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : June 29, 2003
// 
// This file contains the CNObjectDOM, CNObjectNode, and CNObjectAttribute objects.
// To be used to create a browser neutral DOM.  The basic switch is the DOMaware
// property which relies on the HTMLDom object.  If DOMaware, then the typical DOM
// functionality exists, otherwise we build an object tree.
// -------------------------------------------------------------------------------
// Last Updated     : 
// Updated by       : 
// -------------------------------------------------------------------------------
// Copyright (c) 2003 The SoftAd Group, Inc.


// -------------------------------------------------------------------------------
// Function         : CNObjectDOM()
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : June 29, 2003
// 
// This function creates the CNObjectDOM
// -------------------------------------------------------------------------------
// Last Updated     : 
// Updated by       : 
// -------------------------------------------------------------------------------

/*Begin Class*/
function CNObjectDOM() {
	//	********************** Public properties *******************
	//	************************************************************	
	this.DOMaware = ((this.ie || this.isDom) && !this.isNS6);
	this.nodeList = new Array();
	this.sXML = "";
	//	********************** Public methods **********************
	//	************************************************************
	this.create = create;
	this.createNode = createNode;
	this.getXML = null;
	this.buildXML = null;
	
	//	************************** Globals *************************
	//	************************************************************
	var bReturn;
	var iReturn;
	var sReturn = "";
	var sXMLSTARTSYMBOL = "<";
	var sXMLENDSYMBOL = ">";
	var sXMLEMPTYSYMBOL = "/";
	
	// -------------------------------------------------------------------------------
	// Function         : create()
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 28, 2003
	// 
	// This function returns a new CNObjectDOM object.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME				REQUIRED	TYPE        DESCRIPTION
	// -------------------------------------------------------------------------------	
	// sSysVarKey		Required	string	
	// sSysVarValue		Required	string	
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function create() {
		var oTemp = new CNObjectDOM();
		return oTemp;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : createNode(sElement)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : August 1, 2003
	// 
	// This function returns a new CNObjectNode object.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME				REQUIRED	TYPE        DESCRIPTION
	// -------------------------------------------------------------------------------	
	// sElement			Required	string		Element name
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function createNode(sElement){
		var oNewNode = new CNObjectNode();
		oNewNode.elementName = sElement;
		oNewNode.nodeList = new Array();
		oNewNode.attributeList = new Array();
		if(oNewNode.DOMaware){
			oNewNode.node = document.createElement(sElement);
		}
		return oNewNode;
	}	
}
/*End Class*/

/*Begin Sub Class*/
// -------------------------------------------------------------------------------
// Function         : XMLNode(sElement,oParent)
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : July 28, 2003
// 
// sub-class object
// -------------------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------------------
// NAME				TYPE        DESCRIPTION
// sElement			string		element name
// -------------------------------------------------------------------------------
// Last Updated     : 
// Updated by       : 
// -------------------------------------------------------------------------------
function CNObjectNode(){ 	
	//	********************** Public properties *******************
	//	************************************************************	
	this.DOMaware = ((this.ie || this.isDom) && !this.isNS6);
	this.elementName = null;
	this.node = null;
	this.nodeList = null;
	this.attributeList = null;
	this.parent = null;
	this.value = "";
	this.id = "";
	
	//	********************** Public methods **********************
	//	************************************************************
	this.appendChild = appendChild;
	this.setAttribute = setAttribute;
	this.getAttribute = getAttribute;
	this.setValue = setValue;
	this.getInnerHTML = getInnerHTML;
	this.getOuterHTML = getOuterHTML;
	this.getElementsByTagName = getElementsByTagName;
	this.item = item;
	
	//	************************** Globals *************************
	//	************************************************************
	var sReturn = "";
	var inodeListLength;
	var bReturn;
	var oReturn;
	var sXMLSTARTSYMBOL = "<";
	var sXMLENDSYMBOL = ">";
	var sXMLEMPTYSYMBOL = "/";	
	
	// -------------------------------------------------------------------------------
	// Function         : appendChild(oCNObjectNode)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function appends a new child node as the last child of the node.
	// -------------------------------------------------------------------------------
	// Returns			: Returns the new child node successfully appended to the list.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME				TYPE    DESCRIPTION
	// oCNObjectNode	object	A CNObjectNode object.  Address of the new child node to be appended
	//							at the end of the list of children belonging to this node. 
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function appendChild(oCNObjectNode){
		oReturn = oCNObjectNode;
		var iLength = this.nodeList.length;
		this.nodeList[iLength] = oCNObjectNode;		
		oCNObjectNode.parent = this;
		if(this.DOMaware){
			this.node.appendChild(oCNObjectNode.node);
		} else {
		}
		return oReturn;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : setAttribute(sKey, sValue)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function sets the value of the named attribute.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// sKey			string		The string specifying the name of the attribute. 
	//							If the attribute with that name already exists, its value is changed.
	//							If the attribute with that name does not exist, it is created. 
	// sValue		string		The variant that supplies the value for the named attribute.
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function setAttribute(sKey, sValue){
		bReturn = false;
		if(this.DOMaware){
			this.node.setAttribute(sKey, sValue);
			bReturn = true;
		} else {
			//----------------------------------------
			// NON DOMaware                      
			//----------------------------------------
			var existingAttrib = this.getAttribute(sKey);
			if(existingAttrib == null){
				var oAttrib = new CNObjectAttribute(sKey, sValue);
				var iLength = this.attributeList.length;
				this.attributeList[iLength] = oAttrib;
				bReturn = true;
			} else {
				existingAttrib.value = sValue;
				bReturn = true;
			}
			//----------------------------------------
		}
		return bReturn;
	}
		
	// -------------------------------------------------------------------------------
	// Function         : getAttribute(sKey)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function gets the value of the named attribute.  For NON DOMaware browsers,
	// returns null or the oAttrib.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// sKey			string		A string specifying the name of the attribute to return. 
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function getAttribute(sKey){
		sReturn = false;
		if(this.DOMaware){
			sReturn = this.node.getAttribute(sKey);
		} else {
			//----------------------------------------
			// NON DOMaware returns null or oAttrib 
			//----------------------------------------
			oReturn = null;
			var aLength = this.attributeList.length;
			var a = 0;
			while(a < aLength){
				if(this.attributeList[a].key == sKey){
					oReturn = this.attributeList[a];
					break;
				}
				a++;
			}			
			return oReturn;
			//----------------------------------------
		}
		return sReturn;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : setValue(sValue)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function sets the text content of the node or the concatenated text representing the node and its descendants.	
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// sValue		string		A string specifying the text content 
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function setValue(sValue){
		bReturn = false;
		if(this.DOMaware){
			this.node.innerHTML = sValue;
			bReturn = true;
		} else {			
			this.value = sValue;
			bReturn = true;
		}
		return bReturn;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : getInnerHTML()
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function returns the XML/HTML representation of the node and all its descendants.	
	// -------------------------------------------------------------------------------
	// For NON DOMaware browsers, this function recursively calls itself to build a string.
	// GOTCHAS!:  sINNERHTML, oCOMPARE and bCOMPARE are globals and live outside the scope of this object.  
	// setGLOBALS() MUST BE CALLED TO RESET THESE GLOBALS!
	// like this:
	// 
	// setGLOBALS();
	// oCNObjectNode.getXML();
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// sValue		string		A string specifying the text content 
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function getInnerHTML(){
		sReturn = "";
		if(this.DOMaware){
			sINNERHTML = this.node.innerHTML;
		} else {
			//----------------------------------------
			// NON DOMaware                      
			//----------------------------------------
						
			if(bCOMPARE) oCOMPARE = this;
			bCOMPARE = false;
			for(node in this.nodeList){
				sINNERHTML += sXMLSTARTSYMBOL;
				sINNERHTML += this.nodeList[node].elementName;
				if(this.nodeList[node].attributeList != 0){
					var sAttributes = " ";
					for(attribute in this.nodeList[node].attributeList){
						sAttributes += this.nodeList[node].attributeList[attribute].key + "='";
						sAttributes += this.nodeList[node].attributeList[attribute].value + "' ";
					}
					sINNERHTML += sAttributes;
				}
				
				if(this.nodeList[node].nodeList.length != 0){
					sINNERHTML += sXMLENDSYMBOL;
					sINNERHTML += this.nodeList[node].value;
					this.nodeList[node].getInnerHTML();
				} else {
					if(this.nodeList[node].value == ""){
						sINNERHTML += sXMLEMPTYSYMBOL;
						sINNERHTML += sXMLENDSYMBOL;
					} else {
						sINNERHTML += sXMLENDSYMBOL;
						sINNERHTML += this.nodeList[node].value;
						sINNERHTML += sXMLSTARTSYMBOL;
						sINNERHTML += sXMLEMPTYSYMBOL;
						sINNERHTML += this.nodeList[node].elementName;
						sINNERHTML += sXMLENDSYMBOL;
					}					
				}
			}
			if(this.nodeList.length >= 1 && this != oCOMPARE){
				sINNERHTML += sXMLSTARTSYMBOL;
				sINNERHTML += sXMLEMPTYSYMBOL;
				sINNERHTML += this.elementName;
				sINNERHTML += sXMLENDSYMBOL;
			}
			
			//----------------------------------------
		}		
		sReturn = sINNERHTML;
		return sReturn;
	}
	
	
	
	// -------------------------------------------------------------------------------
	// Function         : getOuterHTML()
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function returns the XML/HTML representation of the node and all its descendants.	
	// -------------------------------------------------------------------------------
	// For NON DOMaware browsers, this function recursively calls itself to build a string.
	// GOTCHAS!:  sINNERHTML, oCOMPARE and bCOMPARE are globals and live outside the scope of this object.  
	// setGLOBALS() MUST BE CALLED TO RESET THESE GLOBALS!
	// like this:
	// 
	// setGLOBALS();
	// oCNObjectNode.getXML();
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// sValue		string		A string specifying the text content 
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function getOuterHTML(){
		sReturn = "";
		if(this.DOMaware){
			sOUTERHTML = this.node.outerHTML;
		} else {
			//----------------------------------------
			// NON DOMaware                      
			//----------------------------------------
			
			//----------------------------------------
		}		
		sReturn = sOUTERHTML;
		return sReturn;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : getElementsByTagName(sTagName)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function returns a list of all descendant elements that match the supplied name.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// sTagName		string		A string specifying the name of the element to find. 
	//							The tagName value "*" matches all descendant elements of this element. 
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function getElementsByTagName(sTagName){
		oReturn = null;
		if(this.DOMaware){
			oReturn = this.node.getElementsByTagName(sTagName);
		} else {			
			alert("getElementsByTagName: functionality does not exist for this browser.");
			return false;
		}
		return oReturn;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : item(iIndex)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : July 31, 2003
	// 
	// This function allows random access to individual nodes within the collection
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// iIndex		number		A long integer. An index of the item within the collection. The first item is zero.
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function item(iIndex){
		oReturn = null;
		oReturn = this.nodeList[iIndex];
		return oReturn;	
	}
}

function CNObjectAttribute(sKey, sValue){
	this.key = sKey;
	this.value = sValue;		
}
/*End Sub Class*/
CNObjectNode.prototype = new HTMLDom;

//	*********************** Page Globals ***********************
//	************************************************************
var sINNERHTML = "";
var sOUTERHTML = "";
var oCOMPARE = null;
var bCOMPARE = true;

function setGLOBALS(){
  sINNERHTML = "";
  sOUTERHTML = "";
  oCOMPARE = null;
  bCOMPARE = true;
}

CNObjectDOM.prototype = new HTMLDom;
var oCNObjectDOM = new CNObjectDOM();


