// ----------------------------------------------------------
// XHRScript.js
//
// Author        : Thomas E. George
// Creation Date : March 06, 2007
//
// History       : 03/06/2007 - TEG - created
//              
// ----------------------------------------------------------

// user agent
var _uagt_ = navigator.userAgent.toLowerCase();

//// set the window's onerror event to call our ExceptionHandler() method
//window.onerror = ExceptionHandler;
//
//// Displays an exception to the user.
//function ExceptionHandler(message, url, line)
//{
//    alert("An exception occurred:\n\n" + Trim(message) + "\n\nin " + Trim(url) + ":line " + Trim(line) + ".");
//    
//    return(true);	// true if handled and JavaScript takes no further action
//}

// exception processing (optional - addendumStr [unless exceptionObj is null, then display only addendumStr if present])
function ProcessException(exceptionObj, callerStr, addendumStr)
{
    try
    {
		// process optional param
		var addendum = "";
		if(addendumStr && (addendumStr != null)) // && (addendumStr.length > 0) 
		{
			addendum = addendumStr;
		}
    
		if((callerStr == null) || (callerStr.length == 0))
		{
			callerStr = "unknown caller";
		}
		
	    if(exceptionObj != null)
	    {
			var msgStr = "";
			
			if(exceptionObj.description && (exceptionObj.description != null) && (exceptionObj.description.length != 0))
			{
				msgStr = exceptionObj.description;
			}
			else
			{
				msgStr = "no error description available";
			}
			
			if(_bDebugModeXHR)
			{
				alert(callerStr + " : ERROR - \'" + exceptionObj.name + "\' Exception caught [" + msgStr + "]" + ((addendum.length > 0) ? (" --- " + addendum) : "") + ".");
			}
	    }
	    else
	    {
			if(addendum.length > 0)
			{
				if(_bDebugModeXHR)
				{
					alert(callerStr + " : " + addendum);
				}
			}
			else
			{
				alert(callerStr + " : ANOMALY - null Exception object and empty addendumStr passed in --- Nothing to display.");
			}
		}
	}
	catch(e)
	{
	    alert("ProcessException() : ERROR - \'" + e.name + "\' Exception caught [" + e.description + "]");
	}
	finally
	{
	}
    
    return(exceptionObj);
}

// helper function
function reParseXMLText(xmlHttpIn)
{
	// debug alerts
	//alert(_uagt_);
	//alert(xmlHttpIn.responseText);
	
	// +++ as of July 2007, Firefox strips out the leading <!--METADATA TYPE="ASP_DEBUG_INFO" !!!
	var rc = !isCleanXMLDocText(xmlHttpIn.responseText) && (_uagt_.indexOf("firefox/") >= 0);
	
	if(rc)
	{
		// debug alert
		//alert("reParseXMLText() - Must re-parse. [" + xmlHttpIn.responseText + "]");
		ProcessException(null, "reParseXMLText()", "Must re-parse. [" + xmlHttpIn.responseText + "]");
	}
	//else
	//{
	//	alert("reParseXMLText() - Good to go. [" + xmlHttpIn.responseText + "]");
	//}
	
	return(rc);
}

function isCleanXMLDocText(sXmlDocText)
{
	var bClean = false;
	
	var methodName = "isCleanXMLDocText()";
	
	try
	{
		if((sXmlDocText != null) && (sXmlDocText.length > 0))
		{
			var i = sXmlDocText.indexOf(_sXmlDocTag);
					
			if(i == 0)
			{
				bClean = true;
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, methodName);
	}
	finally
	{
	}
	
	return(bClean);
}

function cleanXMLDocText(sXmlDocText)
{
	var sCleanXDT = ""; //sXmlDocText;
	
	var methodName = "cleanXMLDocText()";
	
	try
	{
		if((sXmlDocText != null) && (sXmlDocText.length > 0))
		{
			var i = sXmlDocText.indexOf("<?xml");
					
			if(i > 0)
			{
				sCleanXDT = sXmlDocText.substring(i);
			}
			else if(i == 0)
			{
				sCleanXDT = sXmlDocText;
			}
			else
			{
				// invalid
				sCleanXDT = "";
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, methodName);
	}
	finally
	{
	}
	
	return(sCleanXDT);
}

// ------------------------------------------------------------------------------------------------------------------------
// XMLHttpRequest Object functions

// XMLHttpRequest Object Creation
function CreateXMLHttpRequestObject()
{
	var xmlHttp = null;
	
	var methodName = "CreateXMLHttpRequestObject()";
	
    try
    {
		if(window.ActiveXObject)
		{
			// Windows IE browser
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			
            //xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //+++ causes same bug seen with Firefox!!!
            //
            //if(xmlHttp == null)
            //{
			//	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			//}
		}
		else if(window.XMLHttpRequest)
		{
			// all other browsers
			xmlHttp = new XMLHttpRequest();
		}
	}
	catch(e)
	{
	    //if(e.description.length != 0) { alert("CreateXMLHttpRequestObject() : \'" + e.name + "\'Exception caught [" + e.description + "]"); }
	    ProcessException(e, methodName);
	}
	finally
	{
		//if(xmlHttp == null)
		//{
		//	throw new Error("Browser does not support XMLHttpRequest.")
		//}
	}

    return(xmlHttp);
}

function ParseXMLTextToXMLDoc(xmlText)
{
	var xmlDom = null;
	
	var methodName = "ParseXMLTextToXMLDoc()";
	
	try
	{
		if((xmlText != null) && (xmlText.length > 0))
		{
			if(window.ActiveXObject)
			{
				xmlDom = new ActiveXObject("Microsoft.XMLDOM");
				
				xmlDom.async = false;
		
				xmlDom.loadXML(xmlText);
				//xmlDom.load("note.xml");
			}
			else
			{
				var parser = new DOMParser();
				
				xmlDom = parser.parseFromString(xmlText, "text/xml");
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, methodName);
	}
	finally
	{
	}
	
	return(xmlDom);
}
