﻿//define log status constants
PAT.logStatus = {Comment:"Comment", Warning:"Warning", Error:"Error", Event:"Event", Debug:"Debug"};

PAT.log = Class.create(
{
    addLog: function(status, message, errorLocation, debugInfo)
    {
        if (PAT.enableLog)
        {
            //debugInfo is optional
            if (debugInfo == null)
                debugInfo = "";

            if (errorLocation == null)
                errorLocation == "";


            var parms = { "action": "ADD", "status": status, "message": message, "errorLocation": errorLocation, "debugInfo": debugInfo };
            new Ajax.Request(currLocation.getDirectory() + 'Log/Log.aspx', {
                method: 'post',
                parameters: parms,
                onSuccess: function(transport)
                {
                    var data = eval('(' + transport.responseText + ')');
                    if (data.result && data.result == "failed")
                    {
                        alert(data.message);
                    }
                },
                onFailure: function(e)
                {
                    alert(e.responseText);
                },
                onComplete: function()
                {
                    //alert("complete");
                }
            });
        }
    },
    addDebugLog: function(message)
    {
        this.addLog(PAT.logStatus.Debug, message);
    },
    getLogs: function(currentPage, resultsPerPage, sessionID, statusList, showLogFunction)
    {
        var parms = { "action": "GET", "sessionId": sessionID, "page": currentPage, "resultsPerPage": resultsPerPage, "statusList": statusList };
        new Ajax.Request(currLocation.getDirectory() + 'Log/Log.aspx', {
            method: 'post',
            parameters: parms,
            onSuccess: function(transport)
            {
                var data = eval('(' + transport.responseText + ')');
                if (data.result && data.result == "failed")
                {
                    alert(data.message);
                }
                else
                {
                    //if we specify a showLogFunction, we call that function and pass in the result html
                    if (showLogFunction)
                        showLogFunction(data.html, data.totalResults);
                }
            },
            onFailure: function(e)
            {
                alert(e.responseText);
            },
            onComplete: function()
            {
                //alert("complete");
            }
        });
    },
    deleteLogs: function(showLogFunction)
    {
        var parms = { "action": "DELETE" };
        new Ajax.Request(currLocation.getDirectory() + 'Log/Log.aspx', {
            method: 'post',
            parameters: parms,
            onSuccess: function(transport)
            {
                var data = eval('(' + transport.responseText + ')');
                if (showLogFunction)
                    showLogFunction(data.html, data.totalResults);
            },
            onFailure: function(e)
            {
                alert(e.responseText);
            }
        });
    }
});
