/*
 * Copyright 2005-2007 WSO2, Inc. (http://wso2.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
log.js contains scripts need to handle log information.
*/

var LOGGING_SERVICE_ADMIN = "LogViewAdmin"
// Adding some colors to the logs
//var TRACE_color = "#FFFFFFFF";
//var DEBUG_color = "#C6FFFF";
//var INFO_color = "#B4F3FF";
//var WARN_color = "#FEE3A1";
//var ERROR_color = "#F7B761";
//var FATAL_color = "#EA8823";
//
//var VIWE_LOGS_REFRESHING_TIME= 10000; //Default refreshing time is every 10 Sec
//
//function showViewLogs(thisObj,index) {
//
//    if (thisObj && index) {
//        renderSelectedMenuItem(thisObj,index);
//        gatherHistoryObject(thisObj.id, index);
//    }
//    //initial firing
//    viewLogs();
//
//}
//
//function viewLogs() {
//
//    var body_xml = '<ns1:getLogsRequest xmlns:ns1="http://logging.service.admin.wsas.wso2.org">' +
//                   '<amount>100</amount>' +
//                   '<layoutPattern></layoutPattern>' +
//                   '</ns1:getLogsRequest>';
//
//    var callURL = serverURL + "/" + LOGGING_SERVICE_STRING + "/" + "getLogs";
//    new wso2.wsf.WSRequest(callURL,"getLogs",body_xml,viewLogsCallback);
//
//}
//
//function viewLogsCallback() {
//
//    var objDiv = document.getElementById("viewLogs");
//    var data = this.req.responseXML;
//    var strHTML = '<h2>System Logs</h2>' +
//                  '<div id="formset"><form><fieldset>' +
//                  "<div style='overflow: auto; height: 480px'>"; //textarea cols="90" rows="30"
//    var rets = data.getElementsByTagName("return");
//    var len = rets.length;
//    var count;
//
//	var str_line_type = "";
//	var str_line_color = "";
//    for (count = 0; count < len; count++) {
//		str_line_type = rets[count].firstChild.nodeValue;
//		str_line_type = str_line_type.substring(0, 6);
//		str_line_color = getLogLineColor(str_line_type);
//        strHTML += "<p style='background-color: " + str_line_color + ";border:1px solid #D0D0D0;'>"+ rets[count].firstChild.nodeValue + "</p>";
//    }
//    strHTML += "</div></fieldset></form></div>"
//    objDiv.innerHTML = strHTML;
//    wso2.wsf.Util.showOnlyOneMain(objDiv);
//}
//
//function getLogLineColor(str_line_type){
//	if (str_line_type == "TRACE ") return TRACE_color;
//	if (str_line_type == "DEBUG ") return DEBUG_color;
//	if (str_line_type == " INFO ") return INFO_color;
//	if (str_line_type == " WARN ") return WARN_color;
//	if (str_line_type == "ERROR ") return ERROR_color;
//	if (str_line_type == "FATAL ") return FATAL_color;
//}
//

function viewLogs(thisObj,index) {

    if (thisObj && index) {
        renderSelectedMenuItem(thisObj, index);
        gatherHistoryObject(thisObj.id, index);
    }


    var body_xml = '<ns1:getLogsRequest xmlns:ns1="http://org.apache.axis2/xsd">' +
                   '</ns1:getLogsRequest>';
    var callURL = serverURL + "/" + LOGGING_SERVICE_ADMIN + "/" + "getLogs";
    new wso2.wsf.WSRequest(callURL, "getLogs", body_xml, viewLogsCallback);
}

function viewLogsCallback() {
    wso2.wsf.Util.callbackhelper(this.req.responseXML, "viewlog.xsl", document.getElementById("viewLogs"));
}


function viewSingleLogLevel() {
    var loglevel = document.getElementById("logLevelID");
    var loglevel_index = null;
    var loglevel_value = null;
    if (loglevel != null)
    {
        loglevel_index = loglevel.selectedIndex;
        if (loglevel_index != null) {
            loglevel_value = loglevel.options[loglevel_index].value;
        }
    }
    if (loglevel_value != null && loglevel_value != "") {
        var body_xml = '<ns1:getLogsForSingleLogLevel xmlns:ns1="http://org.apache.axis2/xsd">' +
                       '<type>' + loglevel_value + '</type>' +
                       '</ns1:getLogsForSingleLogLevel>';
        var callURL = serverURL + "/" + LOGGING_SERVICE_ADMIN + "/" + "getLogsForSingleLogLevel";
        new wso2.wsf.WSRequest(callURL, "getLogsForSingleLogLevel", body_xml, searchLogCallback);
    } else {
        return;
    }

}
function submitenter(e)
{
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    }
    else if (e) {
        keycode = e.which;
    }
    if (keycode == 13)
    {
        searchLog();
        return true;
    }
    else {
        return true;
    }
}
function searchLog() {
    var loglevel = document.getElementById("logLevelID");
    var loglevel_index = null;
    var loglevel_value = null;
    if (loglevel != null)
    {
        loglevel_index = loglevel.selectedIndex;
        if (loglevel_index != null) {
            loglevel_value = loglevel.options[loglevel_index].value;
        }
    }
    var keyword = document.getElementById("logkeyword");
    if (keyword != null && keyword != undefined && keyword.value != null && keyword.value != undefined) {
        if (keyword.value == "") {
            viewLogs();
        } else {
            var body_xml = '<ns1:searchLogRequest xmlns:ns1="http://org.apache.axis2/xsd">' +
                           '<type>' + loglevel_value + '</type>' +
                           '<kerword>' + keyword.value + '</kerword>' +
                           '</ns1:searchLogsRequest>';
            var callURL = serverURL + "/" + LOGGING_SERVICE_ADMIN + "/" + "searchLog";
            new wso2.wsf.WSRequest(callURL, "searchLog", body_xml, searchLogCallback);
        }
    } else {
        return;
    }
}
function searchLogCallback() {
    var objDiv = document.getElementById("viewLogs");
    var keyword = document.getElementById("logkeyword");
    var loglevel = document.getElementById("logLevelID");
    var data = getNodeFromPath("return", this.req.responseXML);
    var loglevel_index = null;
    var loglevel_value = null;
    if (loglevel != null)
    {
        loglevel_index = loglevel.selectedIndex;
        if (loglevel_index != null) {
            loglevel_value = loglevel.options[loglevel_index].value;
        }
    }
    if (loglevel_value != null && loglevel_value != "") {
        data.setAttribute("level", loglevel_value)
    }
    if (keyword != null && keyword.value != undefined) {
        data.setAttribute("keyword", keyword.value)
    }
    var logdiv = document.getElementById("logdiv");
    wso2.wsf.Util.processXML(data, "viewlog.xsl", objDiv);
    wso2.wsf.Util.showOnlyOneMain(objDiv);
}
function clearLogEntries() {
    var body_xml = '<ns1:clearLogsRequest xmlns:ns1="http://org.apache.axis2/xsd">' +
                   '</ns1:clearLogsRequest>';
    var callURL = serverURL + "/" + LOGGING_SERVICE_ADMIN + "/" + "clearLogs";
    new wso2.wsf.WSRequest(callURL, "clearLogs", body_xml, clearLogEntriesCallback);
}
function clearLogEntriesCallback() {
    wso2.wsf.Util.callbackhelper(this.req.responseXML, "viewlog.xsl", document.getElementById("viewLogs"));
}