﻿function createXHR()
{
  var xmlhttp;
  try {
      // Mozilla / Safari / IE7
      xmlhttp = new XMLHttpRequest();
  }
  catch (e)
  {
    // IE
    var XMLHTTP_IDS = new Array(
        'MSXML2.XMLHTTP.5.0',
        'MSXML2.XMLHTTP.4.0',
        'MSXML2.XMLHTTP.3.0',
        'MSXML2.XMLHTTP',
        'Microsoft.XMLHTTP');
    var success = false;
    for (var i=0;i < XMLHTTP_IDS.length && !success; i++)
    {
        try
        {
           xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
           success = true;
        }
        catch (e) {}
    }
    if (!success)
        throw new Error('Unable to create XMLHttpRequest.');
    }
    return xmlhttp;
}


function prepareMessage(msg, url, line, type, component)
{
    var message = "";
    if(msg!=undefined)
        message+="&msg="+escape(msg);
    if(type!=undefined)
        message+="&type="+escape(type);    
    if(url!=undefined)    
        message+="&url="+escape(url);
    if(line!=undefined)    
        message+="&line="+line;
    if(component!=undefined)    
        message+="&component="+component;    
    return message;    
}


function logInfo(msg, url, line, type, component)
{
    try
    {
        if(type==undefined)
            type="Exception";
        if(component==undefined)
            component="JS";    
        if(type==undefined)
            type="Exception";    
        var xhr = createXHR();
        var message = prepareMessage(msg, url, line, type, component);
        xhr.open('POST', 'HttpHandlers/Logging/ClientLogging.ashx?ts='+Math.random().toString(), true);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.setRequestHeader("Content-length", message.length);
        xhr.setRequestHeader("Connection", "close");
        xhr.send(message);
    }
    catch(e){
    //This alert was causing errors in the LivePerson system
    //and UFXBank couldn't use the PageViewer feature.
    //27.05.2010 10:22 Erez Maadani 
    //  alert(e.message);
    }
}

window.onerror = logInfo;
