/* First table displaying statistics per report */
function DisplayTablePerReport() {
    var tableContent = '';
    // Browse each report
    var reportCount = reportStatArray.length;
    for (var i = 0; i < reportCount; i++) { // Display columns of the current report
        var currentReport = reportStatArray[i];
        // Color first line for a new month
        if (isColorLine(i)) {
            tableContent += '<tr bgcolor="#E8E8FF" >';
        } else {
            tableContent += '<tr>';
        }

        tableContent += '<td class="reportTable"><strong>' + currentReport['reportCode'] + '</strong></td>';
        tableContent += '<td class="reportTable">' + currentReport['executionCount'] + '</td>';
        tableContent += '<td class="reportTable">' + currentReport['failedFuncCount'] + '</td>';
        tableContent += '<td class="reportTable">' + currentReport['failedTechCount'] + '</td>';
        tableContent += '<td class="reportTable">' + currentReport['minDuration'] + '</td>';
        tableContent += '<td class="reportTable">' + currentReport['avgDuration'] + '</td>';
        tableContent += '<td class="reportTable">' + currentReport['maxDuration'] + '</td>';
        tableContent += '</tr>';
    }
    document.write(tableContent);
}

// Every [nbReports] line, return true/ Warning, [nbReports] must exist as a global js variable
function isColorLine(i) {
    if (i == 0) {
        return false;
    } else {
        return i % nbReports === 0
    }
}

/* Second table displaying statistics per user */
function DisplayTablePerUser() {
    var tableContent = '';
    // Browse each report
    var reportUserCount = userStatArray.length;
    for (var i = 0; i < reportUserCount; i++) {
        var stat = userStatArray[i];
        tableContent += '<tr>';
        tableContent += '<td class="reportTable"><strong>' + stat['name'] + '</strong></td>';
        tableContent += '<td class="reportTable">' + stat['R01'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R03'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R05'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R06'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R07'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R10'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R11'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R13'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R15'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['W15'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R16'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R17'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R19'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R20'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R21'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R22'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['W22'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R23'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R24'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R25'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R26'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R27'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['R28'] + '</td>';
        tableContent += '<td class="reportTable">' + stat['Global'] + '</td>';
        tableContent += '</tr>';
    }
    document.write(tableContent);
}

function downloadTable(tableNum) {
    var startPeriod = document.getElementById("startPeriod").value;
    var endPeriod = document.getElementById("endPeriod").value;
    var url = "/kurt-web/downloadStats" + tableNum + "?startPeriod=" + startPeriod + "&endPeriod=" + endPeriod;
    window.open(url);
}

/* Callback method after AJAX request */
function ServerResponseForPostForm() {
    if (objPostRequest.readyState === 4) {
        if (objPostRequest.status === 200) {
            parent.Dialog("Success", "Fichier g&eacute;n&eacute;r&eacute;");
        } else if (objPostRequest.status === 500) {
            parent.Dialog("Erreur", "Erreur interne du serveur d&eacute;tect&eacute;e.");
        } else {
            parent.Dialog("Erreur", "La connexion avec le site semble interrompue.");
        }
    }
}

/* Extract the value of a GET parameter set in the URL */
function parseURL(val, defaultVal) {
    var result = defaultVal;
    var urlParam = window.location.search;
    if (urlParam != '') {
        paramArray = urlParam.substr(1).split("&");
        for (var i = 0; i < paramArray.length; i++) {
            var tmpArray = paramArray[i].split("=");
            if ((tmpArray.length === 2) && (tmpArray[0] === val)) {
                result = decodeURIComponent(tmpArray[1]);
                break;
            }
        }
    }
    return result;
}