
if (typeof AjaxTCR == "undefined")
   var AjaxTCR = {};

if (!AjaxTCR.examples)
   AjaxTCR.examples = {};
  
/* object literal wrapper to avoid namespace conflicts */
AjaxTCR.examples.catchErrors = {};

/* URL of your server-side error recording script */
AjaxTCR.examples.catchErrors.errorReportingURL = "http://ajaxref.com/errors/setjavascripterror.php";

AjaxTCR.examples.catchErrors.encodeValue = function(val)
{
 var encodedVal;
 if (!encodeURIComponent)
 {
   encodedVal = escape(val);
   /* fix the omissions */
   encodedVal = encodedVal.replace(/@/g, '%40');
   encodedVal = encodedVal.replace(/\//g, '%2F');
   encodedVal = encodedVal.replace(/\+/g, '%2B');
 }
 else
 {
   encodedVal = encodeURIComponent(val);
   /* fix the omissions */
   encodedVal = encodedVal.replace(/~/g, '%7E');
   encodedVal = encodedVal.replace(/!/g, '%21');
   encodedVal = encodedVal.replace(/\(/g, '%28');
   encodedVal = encodedVal.replace(/\)/g, '%29');
   encodedVal = encodedVal.replace(/'/g, '%27');
 }
 /* clean up the spaces and return */
 return encodedVal.replace(/\%20/g,'+'); 
}	

AjaxTCR.examples.catchErrors.reportJSError = function (errorMessage,url,lineNumber)
{
    function sendRequest(url,payload)
    {
         var img = new Image();
         img.src = url+"?"+payload;
    }
	
    /* form payload string with error data */
    var payload = "url=" + AjaxTCR.examples.catchErrors.encodeValue(url);
    payload += "&message=" + AjaxTCR.examples.catchErrors.encodeValue(errorMessage);
    payload += "&line=" + AjaxTCR.examples.catchErrors.encodeValue(lineNumber);

    /* submit error message  */
    sendRequest(AjaxTCR.examples.catchErrors.errorReportingURL,payload);

    //alert("JavaScript Error Encountered.  \nSite Administrators have been notified.");

    return true; // suppress normal JS errors since we handled
}

AjaxTCR.examples.catchErrors.registerErrorHandler = function () 
{	
    if (window.onerror) // then one exists
      {
       var oldError = window.onerror;
       var newErrorHandler = function (errorMessage,url,lineNumber) { AjaxTCR.examples.catchErrors.reportJSError(errorMessage,url,lineNumber); return oldError(errorMessage,url,lineNumber); }
       window.onerror = newErrorHandler;
      }
    else
      window.onerror = AjaxTCR.examples.catchErrors.reportJSError;
}

/* bind the error handler */
AjaxTCR.examples.catchErrors.registerErrorHandler();
