// <== Begin HC3149 MD 20050730
// Functions to try to improve cross-browser compatibility

// <== Begin mozXPath include (license as shown)
// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/
// if( document.implementation.hasFeature("XPath", "3.0") )
if (typeof XMLHttpRequest != 'undefined')
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{	
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}


}
// <== End MozXPath

// if( document.implementation.hasFeature("XPath", "3.0") )
if (typeof XMLHttpRequest != 'undefined')
{


	XMLDocument.prototype.loadXML = function(strXML)
	{
		//create a DOMParser
		var objDOMParser = new DOMParser();
		
		//create new document from string
		var objDoc = objDOMParser.parseFromString(strXML, "text/xml");
		
		//make sure to remove all nodes from the document
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);		
		
		//add the nodes from the new document
		for (var i=0; i<objDoc.childNodes.length; i++) 
		{
			//import the node
			var objImportedNode = this.importNode(objDoc.childNodes[i], true);
			
			//append the child to the current document
			this.appendChild(objImportedNode);		
		}
	}

	XMLDocument.prototype.createNode = function(strElement, strName,strValue)
	{
		return this.createElementNS(strValue,strName);
	}


	XMLDocument.prototype.transformNode = function(strXSL)
	{
		// Wrapper for IE's transformNode function
		var processor = new XSLTProcessor();
		
		processor.importStylesheet(strXSL);	
		
		// alert(processor.transformToDocument(this).xml);
		
		return processor.transformToDocument(this).xml;

	}

	
	function _Node_getXML() 
	{    
		//create a new XMLSerializer
		var objXMLSerializer = new XMLSerializer;
	    
		//get the XML string
		var strXML = objXMLSerializer.serializeToString(this);
	    
		//return the XML string
		return strXML;
	}

	function _Node_getText() 
	{   
			
		if (typeof this.textContent != 'undefined') 
		{
			return this.textContent;
		}
		else if (typeof this.innerText != 'undefined') 
		{
			return this.innerText;
		}
		else if (typeof this.text != 'undefined') 
		{
			return this.text;
		}

	}

	Node.prototype.__defineGetter__("xml", _Node_getXML);
	Node.prototype.__defineGetter__("text", _Node_getText);

}


function getXMLDoc()
{
	var xmlDoc;
	
	// For IE based browsers:
	if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	// For Mozilla based (standards compliant) browsers:
	else if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("","doc",null);
		
	}
		
	return xmlDoc;
}

if (typeof XMLHttpRequest != 'undefined')
{
	// Wrapper to throw away IE third parameter
	XMLHttpRequest.prototype.Open = function(strMethod, strURL, strAsync)
	{
		this.open(strMethod,strURL,strAsync);
	}
}

function getHTTPObject()
{
			
	var xmlhttp;
		
	try 
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");		
	} 
	catch (e) 
	{
		try 
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (E) 
		{
			xmlhttp = false;
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
		try 
		{
			xmlhttp = new XMLHttpRequest();
		} 
		catch (e) 
		{
			xmlhttp = false;
		}
	}
	  
	return xmlhttp;
}

// <== End HC3149 MD 20050730

// <== MD 20051007 - add wrapper for the .innerText property (which is IE only)
var isMozilla = !(document.all);
if(isMozilla)
{
        HTMLElement.prototype.__defineSetter__("innerText", 
			function (sText) 
			{
				this.innerHTML = sText.replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
			}
		);
        
		HTMLElement.prototype.__defineGetter__("innerText", 
			function () 
			{
				var r = this.ownerDocument.createRange();
				r.selectNodeContents(this);
				return r.toString();
			}
		);
}
// <== End MD 20051007

// <== Begin MD 20051009 - Safari support

// var xml = '<?xml version="1.0"?><foo>hello</foo>';
// var url = "data:text/xml;charset=utf-8," + encodeURIComponent(xml);
/*
your standard AJAX stuff
var req = new XMLHttpRequest();
req.open("GET", url);
req.onload = function() { handleResponse(req) }
req.send(null);

function handleResponse(req) {
    // w00t! a bona fide DOM object
    var dom = req.responseXML;
    alert(dom.documentElement.nodeName); // "foo"
}
*/
// <== End MD 20051009

function replaceSpecialChars(sString,sNewChar,sOldChar)
{
	while (sString.indexOf(sOldChar)>0)
	{
		sString=sString.replace(sOldChar, sNewChar)
	}
	return sString;
}

function refreshGrid()
{
	try
	{
		if (document.all.item("frmMainList").style.display=='none')
		{
			frmSubMainList.refreshPreserve();
		}
		else
		{
			frmMainList.refreshPreserve();
		}
	}	
	catch(e)
	{
		try
		{
			if (document.all.item("fraMainList").style.display=='none')
			{
				fraSubMainList.refreshPreserve();
			}
			else
			{
				fraMainList.refreshPreserve();
			}
		}
		catch(e)
		{
			alert('ClientUtil.refreshGrid(): ' + e);
		}
	}
}

function openModal(sURL, sArgumentValues, sHeight,sWidth)
{
	var sFeatures='dialogHeight:' + sHeight + '; dialogWidth: ' + sWidth;
	sFeatures=sFeatures + 'center: Yes; resizable: Yes; status: No; scroll: Yes;';
    window.showModalDialog(sURL,sArgumentValues,sFeatures);
}


function disableAllEntryBoxes()
{
  try
  {	
	var iElementIndex;
	var oElement;
	for (iElementIndex=0; iElementIndex < document.all.length; iElementIndex++)
	{
		oElement=document.all.item(iElementIndex);
		switch (oElement.tagName)
		{
			case "INPUT":
				oElement.disabled=true;
				break;

			case "TEXTAREA":
				oElement.disabled=true;
				break;
			case "SELECT":
				oElement.disabled=true;
				break;
				
			default:
		}
	}
   }
   catch(e)
   {
		 alert("compasswebtuil.disableAllEntryBoxes: " +e);
   }

}

function enableAllEntryBoxes()
{
  try
  {	
	var iElementIndex;
	var oElement;
	for (iElementIndex=0; iElementIndex < document.all.length; iElementIndex++)
	{
		oElement=document.all.item(iElementIndex);
		switch (oElement.tagName)
		{
			case "INPUT":
				oElement.disabled=false;
				break;

			case "TEXTAREA":
				oElement.disabled=false;
				break;
			case "SELECT":
				oElement.disabled=false;
				break;
				
			default:
		}
	}
   }
   catch(e)
   {
		 alert("compasswebtuil.disableAllEntryBoxes: " +e);
   }

}

function hightlightGrid()
{
	try
	{
		setGridBackGroundColor('#003366');
	}
	catch(e)
	{
		alert("hightlightGrid(): " + e);
	}

}

function setSelectBoxByCaption(oSelectBox, varCaption)
{
	try
	{
		var oElement=oSelectBox;
		var varValue=varCaption;
		oElement.selectedIndex=0; //init 
		var iOptionIndex=0;
		for (iOptionIndex=0; iOptionIndex < oElement.options.length; iOptionIndex++)
		{
			var oOption=oElement.options(iOptionIndex);
			if (oOption.value==varValue)
			{
				oElement.selectedIndex=iOptionIndex;
			}
		}
	}
	catch(e)
	{
	   alert("SetSelectBoxByCaption: " + e);
	}

}

function ThrowMsg(sMsg)
{
	try
	{

		var attributes
		var currentHeight = document.body.clientHeight;
		var currentWidth  = document.body.clientWidth;
		var currentTop    = window.screenTop;
		var currentLeft   = window.screenLeft;
		var leftNew, topNew;

		if (currentHeight > 100)
			topNew = (currentHeight - 100)/2 + currentTop;
                else    topNew = 0;

		if (currentWidth > 450)
			leftNew = (currentWidth - 450)/2 + currentLeft;
                else    leftNew = 0;

		attributes = "left=" + leftNew + ",top=" + topNew + ",height=480,width=480";

		var newWindow = window.open("about:blank", null , attributes);
		var sHTML;
		sHTML="<HEAD><TITLE>Query Status</TITLE></HEAD>"
		sHTML=sHTML + "<BODY style='background-color:silver;'><p><hr><center>"
		sHTML=sHTML + "<SPAN style='color:blue;font: bold 11pt;'>"
		sHTML=sHTML + sMsg
		sHTML=sHTML + "</SPAN><hr></BODY>"
		newWindow.document.write(sHTML);	
	}
	catch(e)
	{
		alert("ThrowMsg:  " + sMsg + " " + e);
	}
}

function JSTrim(inString) 
{
  var retVal = "";
  var start = 0;
  try
  {
	while ((start < inString.length) && (inString.charAt(start) == ' ')) 
	{
		++start;
	}
	var end = inString.length;
	while ((end > 0) && (inString.charAt(end - 1) == ' ')) 
	{
		--end;
	}
	retVal = inString.substring(start, end);
	return retVal;
  }
  catch(e)
  {
	alert("JSTrim() " + inString + " " + e)
	return null;
  }
	
}


//function mouseOver(src,colorOver)		// ==> MD 20050918
function mouseOver(src,colorOver, evt)	// <== MD 20050918
{ 
	if (window.event) 
		var ev = window.event;
	else if (evt) 
		var ev = evt;			

	try
	{
		if (src.contains)
		{
			if (!src.contains(ev.fromElement))
			{ 
				src.style.cursor = 'hand'; 
				src.style.backgroundColor = colorOver; 
			}
		}
		else
		{
			src.style.cursor = 'hand'; 
			src.style.backgroundColor = colorOver; 
		}
		 
	}
	catch(e)
	{
		alert("mouseOver: " + e );
		return;
	}
} 

// function mouseOut(src,colorIn)	// ==> MD 20050918
function mouseOut(src,colorIn,evt )	// <== MD 20050918
{ 
	if (window.event) 
		var ev = window.event;
	else if (evt) 
		var ev = evt;
		
	try
	{
		if (src.contains)
		{
			if (!src.contains(ev.toElement))
			{ 
				src.style.cursor = 'default'; 
				src.style.backgroundColor = colorIn; 
			} 
		}
		else
		{
			src.style.cursor = 'default'; 
			src.style.backgroundColor = colorIn;
		}
	}
	catch(e)
	{
		alert("mouseOut: " + e );
		return;
	}
} 

function returnParentTable(src)
{
	try
	{
		var oParent;
		var oTemp;
		oParent=src.parentNode;
		while(oParent.tagName!="TABLE")
		{
			oTemp=oParent;
			oParent=oTemp.parentNode;
		}
	}
	catch(e)
	{
		alert("returnParentTable(): " + e );
		oParent=null;
	}
	return oParent;

}

function returnParentRow(src)
{
	try
	{
		var oParent;
		var oTemp;
		oParent=src.parentNode;
		while(oParent.tagName!="TR")
		{
			oTemp=oParent;
			oParent=oTemp.parentNode;
		}
	}
	catch(e)
	{
		alert("returnParentRow: " + e );
		oParent=null;
	}
	return oParent;

}

function displayLocation(displayText)
{
	try
	{
		navText.innerHTML = "<font class='main-nav-display-text'>&nbsp &nbsp " + displayText + "&nbsp &nbsp  </font>";
	}
	catch(e)
	{
		alert("Nothing to display");
	}
}


function setEntryBoxStyle()
{
  try
  {	
	var iElementIndex;
	var oElement;
	if (document.all.length==0) 
	{
		return;
	}
	
	for (iElementIndex=0; iElementIndex < document.all.length; iElementIndex++)
	{
		oElement=document.all.item(iElementIndex);
		switch (oElement.tagName)
		{
			case "INPUT":
				if (oElement.type=='text')
				{
					oElement.className='inputbox';
				}
				break;

			case "TEXTAREA":
				oElement.className='textarea';
				break;
			case "SELECT":
				oElement.className='selectbox';
				break;
			default:
		}
	}
   }
   catch(e)
   {
		 alert("compasswebtuil.setEntryBoxeStyle: " +e);
   }

}
function showMsg(sMsg)
{
	divErrorSummary.innerHTML='<h3>' + sMsg + '</h3>';
	divErrorSummary.style.color='green';
}
//showMsg("This runs");
function clearError()
{
	divErrorSummary.innerHTML='';
	divErrorSummary.style.color='red';
}
//clearError("");
function clearMsg()
{
	divErrorSummary.innerHTML='';
	divErrorSummary.style.color='green';
}
//clearMsg("");
function resetError()
{

	divErrorSummary.innerHTML='';
}