var disp_jserr	= true;
var log_js		= false; 
var log_val		= true;

function lib_BrowserCheck()
{
	this.ver=navigator.appVersion;
	this.agent=navigator.userAgent;
	this.mac=this.agent.indexOf("Mac")>-1;
	this.dom=document.getElementById;
	this.opera5=this.agent.indexOf("Opera 5")>-1;
	this.ie4=(document.all && !this.dom && !this.opera5);
	this.ie5=(this.ver.indexOf("MSIE 5")>-1&&this.dom&&!this.opera5);
	this.ie6=(this.ver.indexOf("MSIE 6")>-1&&this.dom&&!this.opera5);
	this.ie5plus=this.ie5||this.ie6;
	this.ie=this.ie4||this.ie5||this.ie6;
	this.ns4=(document.layers&&!this.dom);
	this.ns6plus=(this.dom&&parseInt(this.ver)>=5);
	this.bw=(this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera5);
	return this;
}

var bw = new lib_BrowserCheck();
var oLogClientErr = new lib_LogErrors();

window.onerror = function(sMsg,sUrl,sLine)
{
	//check if logging js errors is enabled
	if(!log_js)
	{	//check if should dispplay error
		return !disp_jserr;
	}
	//check browser version
	if(bw.ie)
	{	//build error desc string
		var sErrInfo = "ErrMsg=" + sMsg + ";line=" + sLine;
	}
	else
	{	//build error desc string
		var sErrInfo = "targetEl=" + sMsg.target;
	}
	if(typeof oLogClientErr == 'object' && oLogClientErr != null)
	{
		oLogClientErr.iErrorSource = 480; //ClientSideJavaScriptError
		oLogClientErr.iErrorType = 60;	  //SystemError
		oLogClientErr.sErrorDetails = sErrInfo;
		oLogClientErr.sScriptName = window.location.href;
		oLogClientErr.LogError();
	}
	//check if should display error
	return !disp_jserr;
}


function lib_LogErrors()
{
	//public properties
	this.sReferrer		= null;
	this.iErrorType		= null;
	this.sInputData		= null;
	this.sScriptName	= null;
	this.iErrorSource	= null;
	this.sErrorDetails	= null;
	this.QueryStringVal = null;
	this.bUseSSL		= false;
	
	this.bASynchronous	= true;
 	this.sRemoteHost	= 'ClientSideValidation';
	this.sServerName	= 'ClientSideValidation';
	this.sSSLServer		= 'https://secure.loopnet.com';

	

	//check for valid browser version [only supports IE5+ and NS6+]
	if(bw.ns6plus) //NetScape 6+
	{	//set the create XML function for NS
		this.createXMLFromString = function(sRawXML)
		{
			var xmlParser, xmlDocument;
			try
			{	//load XML string into a new XML document
				var xmlParser = new DOMParser();
				var xdNew = xmlParser.parseFromString(sRawXML, 'text/xml');
				return xdNew;
			}
			catch(e)
			{	//Unable to create XML document
				
				return null;
			}
		}
		//set the post XML function for NS
		this.postXML = function(sURL, xdToPost)
		{
			try
			{	//create XMLHttpRequest object and post XML
				var oHttpRequest = new XMLHttpRequest();
				oHttpRequest.open('POST', sURL, this.bASynchronous);
				oHttpRequest.send(xdToPost);
				return oHttpRequest;
			}
			catch(e)
			{	//Unable to post XML document
				
				return null;
			}
		}
		//set the serialize XML function for NS
		this.serializeXML = function(xdToSerialize)
		{
			try
			{
				//serialize XML document
				var oXmlSerializer;
				oXmlSerializer = new XMLSerializer();
				return oXmlSerializer.serializeToString(xdToSerialize);
			}
			catch(e)
			{
				//add entry into the error list
				
				return null;
			}
		}
	}
	else if(bw.ie5plus) //Internet Explorer 5+
	{
		//set the create XML function for IE
		this.createXMLFromString = function(sRawXML)
		{
			try 
			{	//load XML string into a new XML document
				if(document.implementation && document.implementation.createDocument)
				{
					xdNew = document.implementation.createDocument("", "WebStatistics", null);
				}
				else
				{
					var xdNew = new ActiveXObject('Microsoft.XMLDOM');										
					xdNew.async = false;
					xdNew.loadXML(sRawXML);
				}
				return xdNew;
			}
			catch(e)
			{	//Unable to create XML document
				
				return null;
			}
		}
		//set the post XML function for IE
		this.postXML = function(sURL, xdToPost)
		{
			try
			{	//create XMLHTTP object and post XML
				var oHttpRequest;
				oHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
				oHttpRequest.open('POST', sURL, this.bASynchronous);
				oHttpRequest.send(xdToPost);
				return oHttpRequest;
			}
			catch(e)
			{	//Unable to post XML document
				
				return null;
			}
		}
		//set the serialize XML function for IE
		this.serializeXML = function(xdToSerialize)
		{		
			return xdToSerialize.xml;
		}
	}
	else
	{	//XML post is not supported in the current browser
		this.createXMLFromString = null;
	}
	//this function will send an XML string to an asp page for logging
	this.LogError = function()
	{
		//check if XML post is supported
		if(this.createXMLFromString == null)
		{	//XML post is not supported, exit function
			return;
		}
		//check if error type is set
		if(typeof this.iErrorType != 'number')
		{
			if(isNaN(this.iErrorType))
			{
				//Missing Valid iErrorType can not log error
				return;
			}
			else
			{	//change data type from string to integer
				this.iErrorType = parseInt(this.iErrorType);
			}
		}
		//check if error source is set
		if(typeof this.iErrorSource != 'number')
		{
			if(isNaN(this.iErrorSource))
			{
				//Missing Valid iErrorSource can not log error
				return;
			}
			else
			{	//change data type from string to integer
				this.iErrorSource = parseInt(this.iErrorSource);
			}
		}
		bLogThisErr = CheckSettings(this.iErrorSource);
		//create root XML node
		if(bLogThisErr)
		{
			var xdRoot = this.createXMLFromString('<WebStatistics><\/WebStatistics>');
			if(typeof xdRoot == 'object' && xdRoot != null)//add new error message
			{
				if(typeof this.sScriptName == 'string' && this.sScriptName.length > 0)
				{
					this.sScriptName = this.sScriptName.replace(/\&/g,'#amp;');
					xdRoot.documentElement.setAttribute('ScriptName',this.sScriptName);
				}
				if(typeof this.sRemoteHost == 'string' && this.sRemoteHost.length > 0)
				{
					xdRoot.documentElement.setAttribute('RemoteHost',this.sRemoteHost);
				}
				if(typeof this.sServerName == 'string' && this.sServerName.length > 0)
				{
					xdRoot.documentElement.setAttribute('ServerName',this.sServerName);
				}
				if(typeof this.sReferrer == 'string' && this.sReferrer.length > 0)
				{
					this.sReferrer = this.sReferrer.replace(/\&/g,'#amp;');
					xdRoot.documentElement.setAttribute('Referrer',this.sReferrer);
				}
				var xnErrorList = xdRoot.createElement('ErrorList');
				if(typeof xnErrorList == 'object' && xnErrorList != null)
				{
					var xnError = xdRoot.createElement('Error');
					if(typeof xnError == 'object' && xnError != null)
					{
						if(typeof this.iErrorType == 'number')
						{
							xnError.setAttribute('ErrorType',this.iErrorType);
						}
						if(typeof this.iErrorSource == 'number')
						{
							xnError.setAttribute('ErrorSource',this.iErrorSource);
						}
						if(typeof this.sErrorDetails == 'string' && this.sErrorDetails.length > 0)
						{
							var xnErrorDetails = xdRoot.createElement('ErrorDetails');
							if(xnErrorDetails)
							{
								var xnCDATASection = xdRoot.createCDATASection(this.sErrorDetails);
								xnErrorDetails.appendChild(xnCDATASection);
								xnError.appendChild(xnErrorDetails)
							}
						}
						if(typeof this.sInputData == 'string' && this.sInputData.length > 0)
						{
							var xnInputData = xdRoot.createElement('InputData');
							if(xnInputData)
							{
								var xnCDATASection = xdRoot.createCDATASection(this.sInputData);
								xnInputData.appendChild(xnCDATASection);
								xnError.appendChild(xnInputData)
							}
						}
						xnErrorList.appendChild(xnError);
					}
				}
				xdRoot.documentElement.appendChild(xnErrorList);

				//post XML to the asp page for logging
				if(this.bUseSSL && (this.sSSLServer.length > 0))
				{
					var sURL = (this.sSSLServer+'/LogError/LogError.asp')+((this.QueryStringVal != null)?'?'+this.QueryStringVal:'');
					
					oHttpRequest = this.postXML(sURL, xdRoot);
					this.bUseSSL = false; //must be set for each post
				}
				else
				{
					var sURL = '/LogError/LogError.asp'+((this.QueryStringVal != null)?'?'+this.QueryStringVal:'');
					
					oHttpRequest = this.postXML(sURL, xdRoot);
				}

				if(typeof oHttpRequest == 'object' && oHttpRequest != null)
				{
					
				}
			}
		}
	}
}

function CheckSettings(iErrSource)
{	
	switch(iErrSource)
	{
		case 480:
		{	//client-side javascript error
			return log_js; break;
		}
		default: //validation errors
		{
			return log_val; break;
		}
	}
}

function TrackWebErrors(sModule,sMethod,iErrorType,iErrorSource,sInputData,sErrorDetails)
{
	if((typeof oLogClientErr) == 'object')
	{
		oLogClientErr.iErrorType = iErrorType;
		oLogClientErr.iErrorSource = iErrorSource;
		oLogClientErr.sErrorDetails = sErrorDetails;
		if(window.location.href.toLowerCase().indexOf('/stealth/pfui.asp') != -1)
		{
			oLogClientErr.sScriptName = RequestQueryString('url');
		}
		else
		{
			oLogClientErr.sScriptName = window.location.href;
		}
		oLogClientErr.Referrer = document.referrer;
		oLogClientErr.sInputData = sInputData;
		oLogClientErr.LogError();
	}
}

function RequestQueryString(FieldName) 
{
	var QueryString = '' 
	var FieldValue = '' 
	var Start = 0 
	var End = 0 
	
	QueryString = window.location.search;
	FieldName = FieldName.toLowerCase();
	QueryString = QueryString.toLowerCase();
	Start = QueryString.indexOf(FieldName + '=');
	
	if(Start != 1)
	{ 
		Start = QueryString.indexOf('&' + FieldName + '=');
		if(Start == -1)
		{
			return FieldValue;
		} 
		Start += FieldName.length + 2;
	}
	else
	{
		Start = FieldName.length + 2;
	}
	End = QueryString.indexOf('&', Start + 1);
	if(End == -1)
	{
		End=QueryString.length;
	} 
	FieldValue = window.location.search.substring(Start, End);
	FieldValue = unescape(FieldValue); 
	return FieldValue;
} 
