/***************************Confidential and Proprietary************************
* File Name: CreateObjectElement.js
********************Copyright (c) Hyland Software, Inc. 1991-2008********************/
// JScript source code
// CreateObjectElement.js - used to create object element for ActiveX Controls
var myObject = null;
try
{
	// Get the div for where the object will go and the XML
	// Adding the object to the HTML Document allows for the Codebase 
	var objectHolder = document.getElementById("objectHolder");
	
	// Each ActiveX control is represented by a <xml> element and all 
	// <xml> elements are placed in a single objectHolder div element.
	var objectXmlCollection = objectHolder.getElementsByTagName("xml");

	// Loop through the collection and generate <object> elements
	for (var k = 0; k < objectXmlCollection.length; k++)
	{
		myObject = document.createElement("object");
		objectHolder.appendChild(myObject);
		
		var objectXML = objectXmlCollection[k];
		var oXmlDoc = new ActiveXObject("MSXML.DomDocument");
		var xmlLoaded = oXmlDoc.loadXML(objectXML.innerHTML);

		if(xmlLoaded)
		{
			// Create the object element and add the correct attributes
			var objectNode = oXmlDoc.selectSingleNode("/hsi:object");

			var objectNodeAttributes = objectNode.attributes;
			var attributeCount = objectNodeAttributes.length;

			// Create regular expression for codebase and classid
			var regex = /codebase/i;
			var regex2 = /classid/i;

			var classID = null;

			for(var i=0; i<attributeCount; i++)
			{
				if(objectNodeAttributes[i].nodeName != "xmlns:hsi")
				{
					// test to see if this is a codebase attribute and add it appropriately
					if(regex.test(objectNodeAttributes[i].nodeName))
					{
						myObject.codeBase = objectNodeAttributes[i].nodeValue;
					}
					else if (regex2.test(objectNodeAttributes[i].nodeName))
					{
						classID = objectNodeAttributes[i].nodeValue;
						// Check to see if this is AEXCommServer and handle as attribute
						if(classID == "clsid:143A07D3-30E5-425F-96DE-2F41589D1310")
						{
							myObject.setAttribute(objectNodeAttributes[i].nodeName, objectNodeAttributes[i].nodeValue);
						}
					}
					else
					{
						myObject.setAttribute(objectNodeAttributes[i].nodeName, objectNodeAttributes[i].nodeValue);
					}
				}
			}

			// Get Children elements
			var node = objectNode.firstChild;
			while(node != null)
			{
				if(node.nodeName != "hsi:param")
				{
					var newError = new Error();
					newError.message = "Invalid Node Name: " + node.nodeName;
					throw(newError);
				}

				var paramElement = document.createElement("param");

				var nodeAttributes = node.attributes;
				attributeCount = nodeAttributes.length;
				for(var j=0; j<attributeCount; j++)
				{
					paramElement.setAttribute(nodeAttributes[j].nodeName, nodeAttributes[j].nodeValue);
				}

				myObject.appendChild(paramElement);

				node = node.nextSibling;
			}

			if(classID != null && myObject.classid == "")
			{
				myObject.classid = classID;
			}
			else if(myObject.classid == "")
			{
				var newError = new Error();
				newError.message = "Failed to Load ClassID for Control";
				throw(newError);
			}
		}
		else
		{
			// This should only happen if the control xml was not implemented properly.
			// Verify that the control xml is in the hsi namespace and that the root node has a xmlns declaration
			//oXmlDoc.loadXML("<hsi:root xmlns:hsi='http://hsi'/>");

			alert("Failed to load XML for the control");
		}
	}
}
catch(e)
{
	// Remove the control since it failed to load
	if(myObject != null)
	{
		myObject.removeNode(true);
	}

	alert("Failed to create object element for control:\n" + e.message);
}
