﻿// JScript File
function errorHandler(message, url, line)
{
    
    var indexCr = url.indexOf('cr.aspx');
    var indexCs = url.indexOf('cs.aspx');
    var indexS = url.indexOf('s.aspx');
    
    if(indexCr > -1 || indexCs > -1 || indexS > -1)
        return true;
    //alert(" Message :- " + message + "\n URL :- " + url + "\n Line :- " + line);
   return writeLogs(message, url, line, 2);
}
function warningHandler(message, url, line)
{
    return writeLogs(message, url, line, 1);
}
function traceHandler(message, url, line)
{
    return writeLogs(message, url, 1, 0);
}

function writeLogs(message, url, line, type)
{
    if(username)
    {
        var uname = username;
       
        //-------------------------------------------------------------------
        // Log the Error
        //-------------------------------------------------------------------
        if(isToLog)
	    {
            var xmlHttpRequest;
            // a variable to hold our instance of an XMLHTTP object.
            var bHookTheEventHandler = true;
            // used to switch on/off the handling
            // of any response from the server.

            // Checking if IE-specific document.all collection exists 
            // to see if we are running in IE 
            if (document.all)
            { 
                xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
            } else { 
                xmlHttpRequest = new XMLHttpRequest(); 
            }

            //----------------------------------------------------------------        
            // Roll the error information into an xml string:
            //----------------------------------------------------------------
            var xmlDoc;
            var moz = (typeof document.implementation != 'undefined') && (typeof
            document.implementation.createDocument != 'undefined');
            var ie = (typeof window.ActiveXObject != 'undefined');

            if (moz) {
            xmlDoc = document.implementation.createDocument("", "", null)
            } else if (ie) {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            while(xmlDoc.readyState != 4) {};
            }
            xmlDoc.async = false;
            
            xmlHttpRequest.onreadystatechange=function()
              {
                 if(xmlHttpRequest.readyState==4)
                   {
                              
                   }
              }
            //prepare the call, http method=GET, false=asynchronous call
            xmlHttpRequest.open("post", serverName + "/logerror.ashx", bHookTheEventHandler);
            
             var xmlContent = "<JavaScriptError line='" + line + "' url='" 
                           + url + "' userName = '" + uname + "' type = '" + type + "'>" + message  +"</JavaScriptError>";
            if(moz)
            {
                //finally send the call
                xmlHttpRequest.send(xmlContent );
            }
            else if(ie)
            {
                xmlDoc.loadXML(xmlContent);
                //finally send the call
                xmlHttpRequest.send(xmlDoc);
            }
        }
    }              
    return true;
}
window.onerror = errorHandler;
    
