var sessionID = "-1";
var errorStatus = "";

/**/
function initializeSession(appName, rootLocation)
{
    try
    {
        var url = encodeURIComponent(rootLocation + 'WebAppsMonitor_WebSite/WebApplicationsMonitor.asmx/ApplicationStartUp');
        var timeStamp = getTimeStamp();
        
        new Ajax.Request(url,
            {   
                parameters: {webApplication:appName, clientTimeStamp:timeStamp},
                method: 'post',
                onSuccess: assignID,
                onFailure: recordError
            });
    }
    catch(ex)
    {
        recordException(appName, ex, rootLocation);
    }
}

/**/
function assignID(val)   
{
    var guidPattern = /\b[A-F0-9]{32}\b/i;
    sessionID = val.responseText.match(guidPattern);
} 

/**/
function recordError(error)   
{   
    errorStatus = error.responseText;
}

/**/
function getTimeStamp()
{
    var dt = new Date();
    var yr = dt.getFullYear();
    var mn = padTimeComponent(dt.getMonth() + 1); // months are 0-based
    var da = padTimeComponent(dt.getDate());
    var hr = padTimeComponent(dt.getHours());
    var mi = padTimeComponent(dt.getMinutes());
    var se = padTimeComponent(dt.getSeconds());
    var gmtHours = padZoneComponent(dt.getTimezoneOffset() / 60);
    var together = yr + "-" + mn + "-" + da + "T" + hr + ":" + mi + ":" + se + "%GMT" + gmtHours;
    return together;
}

/**/
function padTimeComponent(s)
{
    var t = s.toString();
    if(t.length == 1)
        return "0" + t;
    else
        return t;
}

/**/
function padZoneComponent(s)
{
    var t = s.toString();
    var negPattern = /^-(\d{1})$/;
    var posPattern = /^(\d+)$/;
    if(t.search(negPattern) != -1)
    {
        return "-0" + t.match(negPattern)[1];
    }
    else if(t.search(posPattern) != -1)
    {
        var temp = t.match(posPattern)[1];
        if(temp.length == 2)
        {
            return "+" + temp;
        }
        else
        {
            return "+0" + temp;
        }
    }   
    else
    {
        return t;
    }
}

/**/
function recordEvent(appName, message, rootLocation)
{
    try
    {
        var url = encodeURIComponent(rootLocation + 'WebAppsMonitor_WebSite/WebApplicationsMonitor.asmx/RecordEvent');
        var timeStamp = getTimeStamp();
        
        new Ajax.Request(url,
            {   
                parameters: {id:sessionID, webApplication:appName, clientMessage:message, clientTimeStamp:timeStamp},
                method: 'post',
                onFailure: recordError
            });
    }
    catch(ex)
    {
        recordException(appName, ex, rootLocation);
    }
}

/**/
function recordException(appName, exception, rootLocation)
{
    try
    {
        var message = exception.description;
        errorStatus = message;
        var url = encodeURIComponent(rootLocation + 'WebAppsMonitor_WebSite/WebApplicationsMonitor.asmx/RecordException');
        var timeStamp = getTimeStamp();
        
        new Ajax.Request(url,
            {   
                parameters: {id:sessionID, webApplication:appName, exception:message, clientTimeStamp:timeStamp},
                method: 'post',
                onFailure: recordError
            });
    }
    catch(ex)
    {
        errorStatus = ex.description;
    }
}

