//var IE4 = document.all;
//var NS4 = document.layers;

//var doc_core = (IE4 != null ? IE4 : level1DOM);

//function Doc(name) { return doc_core(name); }

//LCR: DOM Level 1 is supported by all modern browsers.  Change this function to DOM Level 1.
//Need to ensure that all uses of Doc function refer to elements with unique IDs.
function Doc(name) { return document.getElementById(name); }

function isNullParam( param ){ return (null == param || "undefined" == typeof(param));}

function getXMLDoc( XML, ProgID )
{
	var objXML = null;
	if(ProgID == "" || String(ProgID) == "undefined") 
	{ 
		// ProgID = "Microsoft.XMLDOM"; 
		// ProgID = "MSXML2.DOMDocument.3.0"; 
		// ProgID = "Microsoft.FreeThreadedXMLDOM";
		// ProgID = "MSXML.DOMDocument"; 
		ProgID = "MSXML2.FreeThreadedDOMDocument";
		//ProgID = "MSXML2.FreeThreadedDOMDocument30";
	}

    if (typeof DOMParser != "undefined") 
	{
	// Mozilla, Firefox, and related browsers
		if (XML != null && "undefined" != typeof(XML) && XML != "" && String(XML) != "undefined")
		{
			if (XML.indexOf("<") != -1)
			{
				objXML = (new DOMParser()).parseFromString(XML, "application/xml");
			}
			else
			{
				if (document.load || window.opera)
				{
					objXML = document.implementation.createDocument("","",null);
					objXML.async=false;
					objXML.load(XML);
				}
				else
				{
					var xmlhttp = new XMLHttpRequest();
					xmlhttp.open("GET",XML,false);
					xmlhttp.send(null); 

					objXML = (new DOMParser()).parseFromString(xmlhttp.responseText, "application/xml");
				}
			}
		}
		else
		{
			objXML = document.implementation.createDocument("","",null);
		}
		
		return objXML;
    }
    else if (typeof ActiveXObject != "undefined") {
		if("object" == typeof(XML))	{ objXML = XML; }
		else
		{
			//NEW -> Use dynamic progid
			objXML = new ActiveXObject(ProgID);
			objXML.async = false;
			
			if(XML != null && "undefined" != typeof(XML) && 
			XML != "" && String(XML) != "undefined")
			{
				if(XML.indexOf("<") != -1)
				{ 
					//This is a string because < is not valid in a filename
					objXML.loadXML(XML)
					if(objXML.parseError != 0)
					{
						alert(objXML.parseError.reason)
					}
				}
				else if(XML.length != 0)
				{
					//This is assumed to be a relative path to a file
					objXML.load(XML);
				}
			}
		}
		return objXML;
	}
}

function XMLToHTML( strXML )
{
	var regAmp= /&/g;
	var regLT = /</g;
	var regGT = />/g;
	return String(strXML).replace(regAmp, "&amp;").replace(regLT,"&lt;").replace(regGT, "&gt;");;
}

function HTMLToXML( strHTML )
{
	var regAmp= /&amp;/g;
	var regLT = /&lt;/g;
	var regGT = /&gt;/g;
	return String(strHTML).replace(regAmp, "&").replace(regLT,"<").replace(regGT, ">");
}

function HideDoubleQuotes(str)
{
	var regDblQt= /"/g;
	return str.replace(regDblQt, '&dblqt;');
}
function ExpandDoubleQuotes(str)
{
	var regDblQt= /&dblqt;/g;
	return str.replace(regDblQt, '"');
}

function HideObject(obj)
{
	if(null != obj)
	{
		obj.style.display 		= 'none';
		obj.style.visibility 	= 'hidden';
	}
}

function ShowObject(obj)
{
	if(null != obj)
	{
		obj.style.display 		= 'inline';
		obj.style.visibility 	= 'visible';
	}
}

function LoadXMLIsland(strIsland)
{
	var oXML = getXMLDoc();
	var oIsland = Doc(strIsland);
	if(null != oIsland)
	{
		oXML.loadXML(oIsland.innerHTML);
	}	
	return oXML;
}

function Trim(str)
{
	if(null == str || 'undefined' == typeof(str)){ str = ''; }
	var reLeft  = /^ */
	var reRight = / *$/
	return str.replace(reLeft, '').replace(reRight, ''); 
}


function ZeroPad2(num)	{ return (num > 9 ? String(num) : String("0" + num)); }
function ZeroPad3(num)	{ return (num > 99 ? String(num) : "0" + ZeroPad2(num)); }


function GetCurrTime()
{
	var now = new Date();
	var strNow =	ZeroPad2(now.getHours())	+ ":" + 
					ZeroPad2(now.getMinutes())	+ ":" + 
					ZeroPad2(now.getSeconds()); 
	return strNow;
}


function ParseAndTransformToXML(src, style)
{
    var oResult = null;
    var result = "";
    if(src.parseError.errorCode != 0)
    {
		result = reportParseError(src.parseError);
		alert(result + "\n");
	}
    else
    {
		if (style.parseError.errorCode != 0)
		{
			result = reportParseError(style.parseError);
		    alert(result + "\n");
		}
		else
		{
			try 
			{
	            oResult = getXMLDoc();
	            src.transformNodeToObject(style, oResult);
			}
			catch (exception) 
			{
				result = reportRuntimeError(exception);
		        alert(result + "\n");
			}
		}
    }
    return oResult;
}

function ParseAndTransform(src, style)
{
    if(src.parseError.errorCode != 0)
    {
		result = reportParseError(src.parseError);
		alert(result + "\n");
	}
    else
    {
		if (style.parseError.errorCode != 0)
		{
			result = reportParseError(style.parseError);
		    alert(result + "\n");
		}
		else
		{
			try 
			{
				result = src.transformNode(style);
			}
			catch (exception) 
			{
				result = reportRuntimeError(exception);
		        alert(result + "\n");
			}
		}
    }
    return result;
}

// Parse error formatting function
function reportParseError(error)
{
	var s = "";
	for (var i=1; i<error.linepos; i++) 
	{
		s += " ";
	}
	r = "<font face=Verdana size=2><font size=4>XML Error loading '" + 
	    error.url + "'</font>" +
	    "<P><B>" + error.reason + 
	    "</B></P></font>";
	    
	if (error.line > 0)
	{
		r += "<font size=3><XMP>" +
		"at line " + error.line + ", character " + error.linepos +
		"\n" + error.srcText +
		"\n" + s + "^" +
		"</XMP></font>";
	}
	return r;
}

// Runtime error formatting function
function reportRuntimeError(exception)
{
  return "<font face=Verdana size=2><font size=4>XSL Runtime Error</font>" +
      "<P><B>" + exception.description + "</B></P></font>";
}

function SelectSingleNode(oXML, elementPath)
{
	if(window.ActiveXObject)
	{
		return oXML.selectSingleNode(elementPath);
	}
	else
	{
	   var xpe = new XPathEvaluator();
	   var nsResolver = xpe.createNSResolver( oXML.ownerDocument == null ? oXML.documentElement : oXML.ownerDocument.documentElement);
	   var results = xpe.evaluate(elementPath,oXML,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
	   return results.singleNodeValue; 
	}
}

function GetTextContent(oElement)
{
	if (oElement.textContent == null)
	{
		return oElement.text;
	}
	else
	{
		return oElement.textContent;
	}
}

function SetTextContent(oElement, strText)
{
	if (oElement.textContent == null)
	{
		oElement.text = strText;
	}
	else
	{
		oElement.textContent = strText;
	}
}

function TransformNode(oXML, oXSL)
{
	if (window.ActiveXObject)
	{
		return oXML.transformNode(oXSL);
	}
	else
	{
		var oProcessor = new XSLTProcessor()
		oProcessor.importStylesheet(oXSL);
		var oResult = oProcessor.transformToFragment(oXML,document);
		var oSerializer = new XMLSerializer();
		return oSerializer.serializeToString(oResult);
	}
}

function GetXMLString(oXML)
{
	if (window.ActiveXObject)
	{
		return oXML.xml;
	}
	else
	{
		return (new XMLSerializer()).serializeToString(oXML);
	}	
}