steelsquid.js

// Get Browser-Specifc Prefix
function getBrowserPrefix() {

    // Check for the unprefixed property.
    if ('hidden' in document) {
        return null;
    }

    // All the possible prefixes.
    var browserPrefixes = ['moz', 'ms', 'o', 'webkit'];

    for (var i = 0; i < browserPrefixes.length; i++) {
        var prefix = browserPrefixes[i] + 'Hidden';
        if (prefix in document) {
            return browserPrefixes[i];
        }
    }

    // The API is not supported in browser.
    return null;
}

// Get Browser Specific Hidden Property
function hiddenProperty(prefix) {
    if (prefix) {
        return prefix + 'Hidden';
    } else {
        return 'hidden';
    }
}

// Get Browser Specific Visibility State
function visibilityState(prefix) {
    if (prefix) {
        return prefix + 'VisibilityState';
    } else {
        return 'visibilityState';
    }
}

// Get Browser Specific Event
function visibilityEvent(prefix) {
    if (prefix) {
        return prefix + 'visibilitychange';
    } else {
        return 'visibilitychange';
    }
}

var isvis = true;

try {
    var prefix = getBrowserPrefix();
    var hidden = hiddenProperty(prefix);
    var visibilityState = visibilityState(prefix);
    var visibilityEvent = visibilityEvent(prefix);

    document.addEventListener(visibilityEvent, function(event) {
        try {
            if (!document[hidden]) {
                try {
                    isvis = true;
                    onVisible();
                } catch (eeee) {}
            } else {
                try {
                    isvis = false;
                    onHidden();
                } catch (eeee) {}
            }
        } catch (error) {
            isvis = true;
        }
    });
} catch (eeeeee) {
    isvis = true;
}

/**
 * Is this browser/tab visible
 */
function isVisible() {
    return isvis;
}

/**
 * Get a html page from server.
 * file: The file
 */
function changePage(file) {
    window.location.href = file;
}

/**
 * Encode string
 */
function encodePara(paramater) {
    paramater = paramater.split("|").join("#1#");
    paramater = paramater.split("<").join("#2#");
    paramater = paramater.split(">").join("#3#");
    return paramater;
}

/**
 * Decode string
 */
function decodePara(paramater) {
    paramater = paramater.split("#1#").join("|");
    paramater = paramater.split("#2#").join("<");
    paramater = paramater.split("#3#").join(">");
    return paramater;
}

var usePleaseWait = true;
/**
 * The same as submitSync but will not show please wait.
 */
function submitNoPW(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12, e) {
    usePleaseWait = false;
    submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12);
}

/**
 * The same as submitSync but will not show please wait.
 */
function submitNoPW(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12, e) {
    usePleaseWait = false;
    submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12);
}

/**
 * Send command to server and wait for answer.
 * On a success request a function with the sama name as the command will be executed + on, or err if error
 *   function on_command(paramaters){...}
 *   function on_command_err(errorString){...}
 * If no on_command function the reply will be shown in a popup
 * If no on_command_err function the error message will be shown in a popup
 * command: the command
 * paramater1: Can be a list or a string
 * paramater2,3,4...: String
 */
function submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12, e) {
    if (usePleaseWait) {
        showAlert('Working, please wait...');
    }
    usePleaseWait = true;

    try {
        onWork(command);
    } catch (eee) {}
    var plist = null;
    if (paramater1 === undefined) {
        plist = new Array();
    } else if ($.isArray(paramater1)) {
        plist = new Array();
        paramater1.forEach(function(entry) {
            plist.unshift(encodePara(entry));
        });
    } else if (paramater12 !== undefined) {
        plist = new Array(12);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
        plist[7] = encodePara(paramater8);
        plist[8] = encodePara(paramater9);
        plist[9] = encodePara(paramater10);
        plist[10] = encodePara(paramater11);
        plist[11] = encodePara(paramater12);
    } else if (paramater11 !== undefined) {
        plist = new Array(11);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
        plist[7] = encodePara(paramater8);
        plist[8] = encodePara(paramater9);
        plist[9] = encodePara(paramater10);
        plist[10] = encodePara(paramater11);
    } else if (paramater10 !== undefined) {
        plist = new Array(10);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
        plist[7] = encodePara(paramater8);
        plist[8] = encodePara(paramater9);
        plist[9] = encodePara(paramater10);
    } else if (paramater9 !== undefined) {
        plist = new Array(9);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
        plist[7] = encodePara(paramater8);
        plist[8] = encodePara(paramater9);
    } else if (paramater8 !== undefined) {
        plist = new Array(8);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
        plist[7] = encodePara(paramater8);
    } else if (paramater7 !== undefined) {
        plist = new Array(7);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
    } else if (paramater6 !== undefined) {
        plist = new Array(6);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
    } else if (paramater5 !== undefined) {
        plist = new Array(5);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
    } else if (paramater4 !== undefined) {
        plist = new Array(4);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
    } else if (paramater3 !== undefined) {
        plist = new Array(3);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
    } else if (paramater2 !== undefined) {
        plist = new Array(2);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
    } else if (paramater1 !== undefined) {
        plist = new Array(1);
        plist[0] = encodePara(paramater1);
    }
    var commandAndPara = null;
    if (plist.length > 0) {
        commandAndPara = command + "|" + plist.join('|');
    } else {
        commandAndPara = command;
    }
    try {
        e = e || window.event;
        e = e.target || e.srcElement;
        if (e.nodeName == "BUTTON" || e.nodeName == "A") {
            $(e).attr("disabled", "disabled");
        }
    } catch (exe) {}
    $.ajaxSetup({
        cache: false
    });
    $.ajax({
        url: commandAndPara,
        type: 'POST',
        success: function(data) {
            try {
                if (e.nodeName == "BUTTON" || e.nodeName == "A") {
                    $(e).removeAttr("disabled");
                }
            } catch (exe) {}
            try {
                var fn = window["on_" + command];
                li = data.split("|");
                for (var i = 0; i < li.length; i++) {
                    li[i] = decodePara(li[i]);
                }
                fn(li);
            } catch (err) {
                showAlertMedium(data);
            }
            try {
                onSleep(command);
            } catch (eee) {}
            hideAlert();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            try {
                if (e.nodeName == "BUTTON" || e.nodeName == "A") {
                    $(e).removeAttr("disabled");
                }
            } catch (exe) {}
            try {
                var fn = window["on_" + command + "_err"];
                if (XMLHttpRequest.responseText === '') {
                    fn("Connection error");
                } else {
                    fn(XMLHttpRequest.responseText);
                }
            } catch (err) {
                if (XMLHttpRequest.responseText === '') {
                    showAlertLong("Connection error", true);
                } else {
                    showAlertLong(XMLHttpRequest.responseText, true);
                }
            }
            try {
                onSleep(command);
            } catch (eee) {}
            hideAlert();
        }
    });
}

/**
 * The same as submitSync but will show a confirm dialog.
 */
function submitSyncConfirm(confirmText, command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    if (confirm(confirmText)) {
        submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12);
    }
}

/**
 * The same as submitSync but paramaters is id to a input.
 */
function submitSyncFromInput(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    par = paramater1;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater1 = par;

    par = paramater2;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater2 = par;

    par = paramater3;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater3 = par;

    par = paramater4;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater4 = par;

    par = paramater5;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater5 = par;

    par = paramater6;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater6 = par;

    par = paramater7;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater7 = par;

    par = paramater8;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater8 = par;

    par = paramater9;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater9 = par;

    par = paramater10;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater10 = par;

    par = paramater11;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater11 = par;

    par = paramater12;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater12 = par;

    if (paramater12 !== undefined) {
        submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12);
    } else if (paramater11 !== undefined) {
        submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11);
    } else if (paramater10 !== undefined) {
        submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10);
    } else if (paramater9 !== undefined) {
        submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9);
    } else if (paramater8 !== undefined) {
        submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8);
    } else if (paramater7 !== undefined) {
        submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7);
    } else if (paramater6 !== undefined) {
        submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6);
    } else if (paramater5 !== undefined) {
        submitSync(command, paramater1, paramater2, paramater3, paramater4, paramater5);
    } else if (paramater4 !== undefined) {
        submitSync(command, paramater1, paramater2, paramater3, paramater4);
    } else if (paramater3 !== undefined) {
        submitSync(command, paramater1, paramater2, paramater3);
    } else if (paramater2 !== undefined) {
        submitSync(command, paramater1, paramater2);
    } else if (paramater1 !== undefined) {
        submitSync(command, paramater1);
    }
}

/**
 * The same as submitSyncFromInput but will show a confirm dialog.
 */
function submitSyncFromInputConfirm(confirmText, command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    if (confirm(confirmText)) {
        submitSyncFromInput(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12);
    }
}

/**
 * The same as submitSync but paramaters is id to a object, and the value sent is the inner html.
 */
function submitSyncFromHtml(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    if (paramater12 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html(), $('#' + paramater8).html(), $('#' + paramater9).html(), $('#' + paramater10).html(), $('#' + paramater11).html(), $('#' + paramater12).html());
    } else if (paramater11 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html(), $('#' + paramater8).html(), $('#' + paramater9).html(), $('#' + paramater10).html(), $('#' + paramater11).html());
    } else if (paramater10 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html(), $('#' + paramater8).html(), $('#' + paramater9).html(), $('#' + paramater10).html());
    } else if (paramater9 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html(), $('#' + paramater8).html(), $('#' + paramater9).html());
    } else if (paramater8 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html(), $('#' + paramater8).html());
    } else if (paramater7 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html());
    } else if (paramater6 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html());
    } else if (paramater5 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html());
    } else if (paramater4 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html());
    } else if (paramater3 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html());
    } else if (paramater2 !== undefined) {
        submitSync(command, $('#' + paramater1).html(), $('#' + paramater2).html());
    } else if (paramater1 !== undefined) {
        submitSync(command, $('#' + paramater1).html());
    }
}

/**
 * The same as submitSyncFromInput but will show a confirm dialog.
 */
function submitSyncFromHtmlConfirm(confirmText, command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    if (confirm(confirmText)) {
        submitSyncFromHtml(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12);
    }
}

/**
 * The same as submitSync but paramater is value from a prompt box.
 */
function submitSyncFromPrompt(command, headerText1, defaultText1, headerText2, defaultText2) {
    if (headerText2 !== undefined) {
        var value1 = prompt(headerText1, defaultText1);
        if (value1 != null) {
            var value2 = prompt(headerText2, defaultText2);
            if (value2 != null) {
                submitSync(command, value1, value2);
            }
        }
    } else {
        var value = prompt(headerText1, defaultText1);
        if (value != null) {
            submitSync(command, value);
        }
    }
}

/**
 * The same as checkAsync but with a confirm
 */
function checkAsyncConfirm(confirmText, command, reset) {
    if (confirm(confirmText)) {
        checkAsync(command, reset);
    }
}

/**
 * Read status from async command execut.
 * After checkAsync is send  function with the sama name as the command will be executed + on
 *     function on_command(status, error, paramaters){...}
 *     The command is executing if executing = true.
 *     status: idle, busy, err, ok
 *             idle=No work has executed
 *             busy=Working
 *             err=Work finish with error
 *             ok=Work finish OK
 *     error: only if status=='err'
 *
 * command: the command
 * reset: no, pre, post
 *        no=  Do not reset async data
 *        pre= Reset before return data to clent
 *        post=Reset after return data to server
 * paramater2,3,4...: String
 */
function checkAsync(command, reset) {
    $.ajaxSetup({
        cache: false
    });
    var commandAndPara = command + "|command_async_check";
    if (reset && reset == 'pre') {
        commandAndPara = command + "|command_async_clear_pre";
    }
    if (reset && reset == 'post') {
        commandAndPara = command + "|command_async_clear_post";
    }
    $.ajax({
        url: commandAndPara,
        type: 'POST',
        success: function(data) {
            try {
                var fn = window["on_" + command];
                li = data.split("|");
                status = "";
                is_exe = decodePara(li[0]);
                if (is_exe == "True") {
                    status = "busy";
                } else {
                    status = "idle";
                }
                li.splice(0, 1);
                has_ex = decodePara(li[0]);
                if (status == "idle" && has_ex == "True") {
                    status = "ok";
                }
                li.splice(0, 1);
                errr = decodePara(li[0]);
                if (status == "idle" && errr != "") {
                    status = "err";
                }
                li.splice(0, 1);
                for (var i = 0; i < li.length; i++) {
                    li[i] = decodePara(li[i]);
                }
                fn(status, errr, li);
            } catch (err) {}
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            try {
                var fn = window["on_" + command + "_err"];
                if (XMLHttpRequest.responseText === '') {
                    fn("Connection error");
                } else {
                    fn(XMLHttpRequest.responseText);
                }
            } catch (err) {
                if (XMLHttpRequest.responseText === '') {
                    showAlertLong("Connection error", true);
                } else {
                    showAlertLong(XMLHttpRequest.responseText, true);
                }
            }
        }
    });
}

/**
 * Send command to server and do not wait for answer (but will throw exceptions).
 * If exception function on_command_err(errorString){...}
 * If no on_command_err function the error message will be shown in a popup
 * Use checkAsync(command) to read status.
 * After checkAsync is send  function with the sama name as the command will be executed + on
 *   function on_command(executing, paramaters){...}
 * The command is executing if executing = true.
 * If no on_command function the reply will be shown in a popup
 * command: the command
 * paramater1: Can be a list or a string
 * paramater2,3,4...: String
 */
function submitAsync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    var plist = null;
    if (paramater1 === undefined) {
        plist = new Array();
    } else if ($.isArray(paramater1)) {
        plist = encodePara(paramater1);
    } else if (paramater12 !== undefined) {
        plist = new Array(12);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
        plist[7] = encodePara(paramater8);
        plist[8] = encodePara(paramater9);
        plist[9] = encodePara(paramater10);
        plist[10] = encodePara(paramater11);
        plist[11] = encodePara(paramater12);
    } else if (paramater11 !== undefined) {
        plist = new Array(11);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
        plist[7] = encodePara(paramater8);
        plist[8] = encodePara(paramater9);
        plist[9] = encodePara(paramater10);
        plist[10] = encodePara(paramater11);
    } else if (paramater10 !== undefined) {
        plist = new Array(10);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
        plist[7] = encodePara(paramater8);
        plist[8] = encodePara(paramater9);
        plist[9] = encodePara(paramater10);
    } else if (paramater9 !== undefined) {
        plist = new Array(9);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
        plist[7] = encodePara(paramater8);
        plist[8] = encodePara(paramater9);
    } else if (paramater8 !== undefined) {
        plist = new Array(8);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
        plist[7] = encodePara(paramater8);
    } else if (paramater7 !== undefined) {
        plist = new Array(7);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
        plist[6] = encodePara(paramater7);
    } else if (paramater6 !== undefined) {
        plist = new Array(6);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
        plist[5] = encodePara(paramater6);
    } else if (paramater5 !== undefined) {
        plist = new Array(5);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
        plist[4] = encodePara(paramater5);
    } else if (paramater4 !== undefined) {
        plist = new Array(4);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
        plist[3] = encodePara(paramater4);
    } else if (paramater3 !== undefined) {
        plist = new Array(3);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
        plist[2] = encodePara(paramater3);
    } else if (paramater2 !== undefined) {
        plist = new Array(2);
        plist[0] = encodePara(paramater1);
        plist[1] = encodePara(paramater2);
    } else if (paramater1 !== undefined) {
        plist = new Array(1);
        plist[0] = encodePara(paramater1);
    }
    var commandAndPara = null;
    if (plist.length > 0) {
        commandAndPara = command + "|" + plist.join('|');
    } else {
        commandAndPara = command;
    }
    try {
        e = e || window.event;
        e = e.target || e.srcElement;
        if (e.nodeName == "BUTTON" || e.nodeName == "A") {
            $(e).attr("disabled", "disabled");
        }
    } catch (exe) {}
    $.ajaxSetup({
        cache: false
    });
    $.ajax({
        url: commandAndPara,
        type: 'POST'
    });
    setTimeout(function() {
        checkAsync(command);
    }, 200);
}

/**
 * The same as submitAsync but will show a confirm dialog.
 */
function submitAsyncConfirm(confirmText, command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    if (confirm(confirmText)) {
        submitAsync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12);
    }
}

/**
 * The same as submitAsync but paramaters is id to a input.
 */
function submitAsyncFromInput(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    par = paramater1;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater1 = par;

    par = paramater2;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater2 = par;

    par = paramater3;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater3 = par;

    par = paramater4;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater4 = par;

    par = paramater5;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater5 = par;

    par = paramater6;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater6 = par;

    par = paramater7;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater7 = par;

    par = paramater8;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater8 = par;

    par = paramater9;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater9 = par;

    par = paramater10;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater10 = par;

    par = paramater11;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater11 = par;

    par = paramater12;
    if (par !== undefined) {
        if ($('#' + par).is(':checkbox')) {
            if ($('#' + par).is(":checked")) {
                par = "True";
            } else {
                par = "False";
            }
        } else {
            par = $('#' + par).val();
        }
    }
    paramater12 = par;

    if (paramater12 !== undefined) {
        submitAsync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12);
    } else if (paramater11 !== undefined) {
        submitAsync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11);
    } else if (paramater10 !== undefined) {
        submitAsync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10);
    } else if (paramater9 !== undefined) {
        submitAsync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9);
    } else if (paramater8 !== undefined) {
        submitAsync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8);
    } else if (paramater7 !== undefined) {
        submitAsync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7);
    } else if (paramater6 !== undefined) {
        submitAsync(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6);
    } else if (paramater5 !== undefined) {
        submitAsync(command, paramater1, paramater2, paramater3, paramater4, paramater5);
    } else if (paramater4 !== undefined) {
        submitAsync(command, paramater1, paramater2, paramater3, paramater4);
    } else if (paramater3 !== undefined) {
        submitAsync(command, paramater1, paramater2, paramater3);
    } else if (paramater2 !== undefined) {
        submitAsync(command, paramater1, paramater2);
    } else if (paramater1 !== undefined) {
        submitAsync(command, paramater1);
    }
}

/**
 * The same as submitAsyncFromInput but will show a confirm dialog.
 */
function submitAsyncFromInputConfirm(confirmText, command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    if (confirm(confirmText)) {
        submitAsyncFromInput(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12);
    }
}

/**
 * The same as submitAsync but paramaters is id to a object, and the value sent is the inner html.
 */
function submitAsyncFromHtml(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    if (paramater12 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html(), $('#' + paramater8).html(), $('#' + paramater9).html(), $('#' + paramater10).html(), $('#' + paramater11).html(), $('#' + paramater12).html());
    } else if (paramater11 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html(), $('#' + paramater8).html(), $('#' + paramater9).html(), $('#' + paramater10).html(), $('#' + paramater11).html());
    } else if (paramater10 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html(), $('#' + paramater8).html(), $('#' + paramater9).html(), $('#' + paramater10).html());
    } else if (paramater9 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html(), $('#' + paramater8).html(), $('#' + paramater9).html());
    } else if (paramater8 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html(), $('#' + paramater8).html());
    } else if (paramater7 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html(), $('#' + paramater7).html());
    } else if (paramater6 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html(), $('#' + paramater6).html());
    } else if (paramater5 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html(), $('#' + paramater5).html());
    } else if (paramater4 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html(), $('#' + paramater4).html());
    } else if (paramater3 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html(), $('#' + paramater3).html());
    } else if (paramater2 !== undefined) {
        submitAsync(command, $('#' + paramater1).html(), $('#' + paramater2).html());
    } else if (paramater1 !== undefined) {
        submitAsync(command, $('#' + paramater1).html());
    }
}

/**
 * The same as submitAsyncFromInput but will show a confirm dialog.
 */
function submitAsyncFromHtmlConfirm(confirmText, command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12) {
    if (confirm(confirmText)) {
        submitAsyncFromHtml(command, paramater1, paramater2, paramater3, paramater4, paramater5, paramater6, paramater7, paramater8, paramater9, paramater10, paramater11, paramater12);
    }
}

/**
 * The same as submitAsync but paramater is value from a prompt box.
 */
function submitAsyncFromPrompt(command, headerText1, defaultText1, headerText2, defaultText2) {
    if (headerText2 !== undefined) {
        var value1 = prompt(headerText1, defaultText1);
        if (value1 != null) {
            var value2 = prompt(headerText2, defaultText2);
            if (value2 != null) {
                submitAsync(command, value1, value2);
            }
        }
    } else {
        var value = prompt(headerText1, defaultText1);
        if (value != null) {
            submitAsync(command, value);
        }
    }
}

var showing = false;
/**
 * Hide alert popup.
 */
function hideAlert() {
    showing = false;
    $("#alert123").remove();
}

lastAlertMessage = "";
/**
 * Thow a alert popup.
 * If you leav the timeMs it will  show untill hideAlert()
 * Text : Text to show
 * timeMs: show this long (ms)
 * isError: Is this a error popup
 * doNotShowRepetition: Dont show same message ower and over again
 */
function showAlert(text, timeMs, isError, doNotShowRepetition) {
    if (!doNotShowRepetition || lastAlertMessage != text) {
        lastAlertMessage = text;
        if (timeMs == undefined) {
            showing = true;
            timerVar = setTimeout(function() {
                var w = $(window).width();
                var h = $(window).height();
                var left = 10;
                var top = 10;
                if (w > 700) {
                    left = 200;
                } else {
                    left = (w / 2) - 100;
                }
                if (h > 700) {
                    top = 200;
                } else {
                    top = (h / 2) - 60;
                }
                $("#alert123").remove();
                if (showing) {
                    $("body").append("<div onClick='hideAlert()' id='alert123' style='display:none;position:fixed;top:" + top + "px;left:" + left + "px;background-color:#e1efc5;color:#272727;padding:20px;border:1px solid #666666;'>" + text + "</div>");
                    $("#alert123").show();
                }
            }, 1000);
        } else {
            var uuid = "show_alert" + Math.floor((Math.random() * 100000) + 1);
            var w = $(window).width();
            var h = $(window).height();
            var left = 10;
            var top = 10;
            if (w > 700) {
                left = 200;
            } else {
                left = (w / 2) - 100;
            }
            if (h > 700) {
                top = 200;
            } else {
                top = (h / 2) - 60;
            }
            $("#" + uuid).remove();
            if (isError === true) {
                $("body").append("<div onClick='hideAlert()' id='" + uuid + "' style='display:none;position:fixed;top:" + top + "px;left:" + left + "px;background-color:#efc5dd;color:#272727;padding:20px;border:1px solid #666666;'>" + text + "</div>");
            } else {
                $("body").append("<div onClick='hideAlert()' id='" + uuid + "' style='display:none;position:fixed;top:" + top + "px;left:" + left + "px;background-color:#e1efc5;color:#272727;padding:20px;border:1px solid #666666;'>" + text + "</div>");
            }
            $("#" + uuid).show();
            setTimeout(function() {
                $("#" + uuid).remove();
            }, timeMs);
        }
    }
}

/**
 * Thow a alert popup.
 * Text : Text to show
 * isError: Is this a error popup
 * doNotShowRepetition: Dont show same message ower and over again
 */
function showAlertShort(text, isError, doNotShowRepetition) {
    showAlert(text, 2000, isError, doNotShowRepetition);
}

/**
 * Thow a alert popup.
 * Text : Text to show
 * isError: Is this a error popup
 * doNotShowRepetition: Dont show same message ower and over again
 */
function showAlertMedium(text, isError, doNotShowRepetition) {
    showAlert(text, 4000, isError, doNotShowRepetition);
}

/**
 * Thow a alert popup.
 * Text : Text to show
 * isError: Is this a error popup
 * doNotShowRepetition: Dont show same message ower and over again
 */
function showAlertLong(text, isError, doNotShowRepetition) {
    showAlert(text, 6000, isError, doNotShowRepetition);
}

/**
 * Show a div by id.
 * Hide all other of class = layer
 * When layer is showing function onLayerShown(layer){..} will be executed
 */
function showLayer(layer) {
    $(".layer").hide();
    $("#" + layer).show();
    try {
        onLayerShown(layer);
    } catch (err) {}
}

/**
 * Is layer showing
 */
function isLayerShowing(layer) {
    if ($('#' + layer).is(':visible')) {
        return true;
    } else {
        return false;
    }
}


upload_buttons_name = [];
upload_buttons_desc = [];
upload_buttons_sub = [];

function fixUploadAgain() {
    for (var i = 0; i < upload_buttons_name.length; i++) {
        button_name = upload_buttons_name[i];
        button_desc = upload_buttons_desc[i];
        button_sub = upload_buttons_sub[i];
        var t_iframe = $("#iframe_" + button_sub + "");
        if (t_iframe) {
            b_html = t_iframe.contents().find('body').html();
            if (b_html.indexOf("Upload complete!") != -1) {
                t_body = t_iframe.contents().find('body');
                t_head = t_iframe.contents().find('head');
                t_body.css({
                    'border': '0px',
                    'padding': '0px',
                    'margin': '0px'
                });
                t_head.css({
                    'border': '0px',
                    'padding': '0px',
                    'margin': '0px'
                });
                head_s = '<link rel="stylesheet" type="text/css" href="steelsquid.css"/>'
                t_head.html(head_s);
                t_string = "";
                t_string = '<form action="' + button_sub + '" method="post" style="padding:0px;margin:0px;" enctype="multipart/form-data">';
                t_string = t_string + "<input type=\"file\" name=\"" + button_sub + "\" id=\"" + button_sub + "\" style=\"display:none;\" onchange=\"javascript:document.getElementById('" + button_sub + "_button').innerHTML='Uploading...';document.getElementById('" + button_sub + "_button').disabled=true;this.form.submit();\"/>";
                t_string = t_string + '</form>';
                t_string = t_string + "<button title=\"" + button_desc + "\" id=\"" + button_sub + "_button\" onclick=\"document.getElementById('" + button_sub + "').click();\">" + button_name + "</button>";
                t_body.html(t_string);
                he = t_iframe.contents().find('body').height() + 'px';
                t_iframe.css({
                    'height': he
                });
                try {
                    var fn = window["on_upload_ok"];
                    fn();
                } catch (err) {}
            } else if (b_html.indexOf('style="padding:0px;margin:0px;" enctype="multipart/form-data') == -1) {
                t_body = t_iframe.contents().find('body');
                t_head = t_iframe.contents().find('head');
                t_body.css({
                    'border': '0px',
                    'padding': '0px',
                    'margin': '0px'
                });
                t_head.css({
                    'border': '0px',
                    'padding': '0px',
                    'margin': '0px'
                });
                head_s = '<link rel="stylesheet" type="text/css" href="steelsquid.css"/>'
                t_head.html(head_s);
                t_string = "";
                t_string = '<form action="' + button_sub + '" method="post" style="padding:0px;margin:0px;" enctype="multipart/form-data">';
                t_string = t_string + "<input type=\"file\" name=\"" + button_sub + "\" id=\"" + button_sub + "\" style=\"display:none;\" onchange=\"javascript:document.getElementById('" + button_sub + "_button').innerHTML='Uploading...';document.getElementById('" + button_sub + "_button').disabled=true;this.form.submit();\"/>";
                t_string = t_string + '</form>';
                t_string = t_string + "<button title=\"" + button_desc + "\" id=\"" + button_sub + "_button\" onclick=\"document.getElementById('" + button_sub + "').click();\">" + button_name + "</button>";
                t_body.html(t_string);
                he = t_iframe.contents().find('body').height() + 'px';
                t_iframe.css({
                    'height': he
                });
                showAlertMedium(b_html, true);
            }
        }
    }
}

function fixUpload() {
    $('.upload').each(function(i, obj) {
        button_name = $(obj).html();
        button_desc = $(obj).attr("title");
        button_sub = $(obj).attr("id");
        upload_buttons_name.push(button_name);
        upload_buttons_desc.push(button_desc);
        upload_buttons_sub.push(button_sub);
        var t_iframe = $('<iframe id="iframe_' + button_sub + '" width="100%" frameborder="0" scrolling="no" style="border:0px;padding:0px;margin:0px;margin-bottom:-3px;overflow: visible;"></iframe>')
        $(obj).parent().append(t_iframe);
        t_body = t_iframe.contents().find('body');
        t_head = t_iframe.contents().find('head');
        t_body.css({
            'border': '0px',
            'padding': '0px',
            'margin': '0px'
        });
        t_head.css({
            'border': '0px',
            'padding': '0px',
            'margin': '0px'
        });
        head_s = '<link rel="stylesheet" type="text/css" href="steelsquid.css"/>'
        t_head.html(head_s);
        t_string = "";
        t_string = '<form action="' + button_sub + '" method="post" style="padding:0px;margin:0px;" enctype="multipart/form-data">';
        t_string = t_string + "<input type=\"file\" name=\"" + button_sub + "\" id=\"" + button_sub + "\" style=\"display:none;\" onchange=\"javascript:document.getElementById('" + button_sub + "_button').innerHTML='Uploading...';document.getElementById('" + button_sub + "_button').disabled=true;this.form.submit();\"/>";
        t_string = t_string + '</form>';
        t_string = t_string + "<button title=\"" + button_desc + "\" id=\"" + button_sub + "_button\" onclick=\"document.getElementById('" + button_sub + "').click();\">" + button_name + "</button>";
        t_body.html(t_string);
        he = t_iframe.contents().find('body').height() + 'px';
        t_iframe.css({
            'height': he
        });
        $(obj).remove();
    });
}


/**
 * Execute method onPageLoad when the page loads
 */
document.addEventListener("DOMContentLoaded", function() {
    setInterval(onInternalInterval, 1000);
    try {
        do_it = true;
        if (window.location.search.length > 1) {
            lay_id = window.location.search.substring(1);
            if (document.getElementById(lay_id) !== null) {
                try {
                    showLayer(window.location.search.substring(1));
                    do_it = false;
                } catch (eee) {}
            }
        }
        if (do_it) {
            onPageLoad();
            fixUpload();
        }
    } catch (err) {
        if (err.name !== 'ReferenceError') {
            alert(err);
        }
    }
});

var intervalCounter = 1;
/**
 * Execute method onEvery01s every 1 second
 * Execute method onEvery05s every 5 second
 * Execute method onEvery10s every 10 second
 * Execute method onEvery20s every 20 second
 * Execute method onEvery30s every 30 second
 * Execute method onEvery60s every 60 second
 */
function onInternalInterval() {
    fixUploadAgain();
    try {
        onEvery01s();
    } catch (err) {
        if (err.name !== 'ReferenceError') {
            alert(err);
        }
    }
    if (intervalCounter == 5 || intervalCounter == 10 || intervalCounter == 15 || intervalCounter == 20 || intervalCounter == 25 || intervalCounter == 30 || intervalCounter == 35 || intervalCounter == 40 || intervalCounter == 45 || intervalCounter == 50 || intervalCounter == 55 || intervalCounter == 60) {
        try {
            onEvery05s();
        } catch (err) {
            if (err.name !== 'ReferenceError') {
                alert(err);
            }
        }
    }
    if (intervalCounter == 10 || intervalCounter == 20 || intervalCounter == 30 || intervalCounter == 40 || intervalCounter == 50 || intervalCounter == 60) {
        try {
            onEvery10s();
        } catch (err) {
            if (err.name !== 'ReferenceError') {
                alert(err);
            }
        }
    }
    if (intervalCounter >= 20 || intervalCounter >= 40 || intervalCounter >= 60) {
        try {
            onEvery20s();
        } catch (err) {
            if (err.name !== 'ReferenceError') {
                alert(err);
            }
        }
    }
    if (intervalCounter >= 30 || intervalCounter >= 60) {
        try {
            onEvery30s();
        } catch (err) {
            if (err.name !== 'ReferenceError') {
                alert(err);
            }
        }
    }
    if (intervalCounter >= 60) {
        intervalCounter = 0;
        try {
            onEvery60s();
        } catch (err) {
            if (err.name !== 'ReferenceError') {
                alert(err);
            }
        }
    }
    intervalCounter = intervalCounter + 1;
}

/**
 * Execute method onPageLoad when press esc
 */
$(document).keydown(function(e) {
    if (e.which === 27) {
        try {
            onPageLoad();
        } catch (err) {
            if (err.name !== 'ReferenceError') {
                alert(err);
            }
        }
    }
});

/**
 * Execute method onPageLoad when press middle mouse button
 */
$(document).click(function(e) {
    if (e.which === 2) {
        try {
            onPageLoad();
        } catch (err) {
            if (err.name !== 'ReferenceError') {
                alert(err);
            }
        }
        e.preventDefault();
    }
});

/**
 * Execute method onResize and fill screen hight
 */
$(document).ready(fixContent);
$(window).resize(sizeContent);

function fixContent() {
    var $obj = $(".fill");
    if ($obj.length > 0) {
        $("body").css("overflow", "hidden");
        sizeContent();
    }
    $(".bar").find("thead").find("tr").click(function() {
        $(".bar").find("tbody").find("td").toggle();
    })

}

function sizeContent() {
    var $obj = $(".fill");
    if ($obj.length > 0) {
        var newHeight = ($(window).height() - 2) + "px";
        $obj.css("height", newHeight);
    }
    try {
        onResize($(window).width(), $(window).height());
    } catch (err) {
        if (err.name !== 'ReferenceError') {
            alert(err);
        }
    }
}

/**
 * Set work in progress
 */
function status_busy(id, message) {
    $("#" + id).addClass("flash");
    $("#" + id).addClass("status");
    if (message) {
        $("#" + id).html(message);
    } else {
        $("#" + id).html("Work in progress, please wait...");
    }
}

/**
 * Set no work in progress
 */
function status_idle(id, message) {
    $("#" + id).removeClass("flash");
    $("#" + id).removeClass("error");
    $("#" + id).removeClass("ok");
    $("#" + id).addClass("status");
    if (message) {
        $("#" + id).html(message);
    } else {
        $("#" + id).html("No work in progress (idle)");
    }
}

/**
 * Work completed ok
 */
function status_ok(id, message) {
    $("#" + id).removeClass("flash");
    $("#" + id).removeClass("error");
    $("#" + id).addClass("ok");
    $("#" + id).addClass("status");
    if (message) {
        $("#" + id).html(message);
    } else {
        $("#" + id).html("Work completed OK");
    }
}

/**
 * Work completed with error
 */
function status_err(id, message) {
    $("#" + id).removeClass("flash");
    $("#" + id).addClass("error");
    $("#" + id).removeClass("ok");
    $("#" + id).addClass("status");
    if (message) {
        $("#" + id).html(message);
    } else {
        $("#" + id).html("Error occurred!");
    }
}

/**
 * Flash content
 */
function flash(id) {
    $("#" + id).addClass("flash");
    $("#" + id).show();
}

/**
 * Hide content
 */
function hide(id) {
    $("#" + id).hide();
    $("#" + id).removeClass("flash");
}

/**
 * Show content
 */
function show(id) {
    $("#" + id).show();
    $("#" + id).removeClass("flash");
}

/**
 * Toggle show and hide
 */
function toggle(id) {
    var e = document.getElementById(id);
    if (e.style.display == 'block')
        e.style.display = 'none';
    else
        e.style.display = 'block';
}

/**
 * Enable input
 */
function enable(id) {
    $("#" + id).removeAttr('disabled')
}

/**
 * Disable input
 */
function disable(id) {
    $("#" + id).attr("disabled", "disabled");
}

/**
 * Sett value of object
 */
function setValue(id, value) {
    $('#' + id).val(value);
}

/**
 * Get value of object
 */
function getValue(id) {
    return $('#' + id).val();
}

/**
 * Scroll to bottom of div
 */
function scrollToBottom(id) {
    $("#" + id).scrollTop($("#" + id)[0].scrollHeight);
}

var lastScrollLength = 0;
/**
 * Scroll to bottom of div if change since last
 */
function scrollToBottomIfChanged(id) {
    var newlength = $("#" + id).html().length;
    if (lastScrollLength != newlength) {
        lastScrollLength = newlength;
        $("#" + id).scrollTop($("#" + id)[0].scrollHeight);
    }
}

/**
 * CLean html inside a object
 */
function clearContent(id) {
    $('#' + id).html("");
}

/**
 * Sett html content of object
 * If the html is a list a <br> wil be inserter between every iteration
 */
function setContent(id, html, isError) {
    if ($.isArray(html)) {
        $('#' + id).html("");
        for (var i = 0; i < html.length; i++) {
            if (isError) {
                $('#' + id).append("<span class='error'>" + html[i] + "</span>");
            } else {
                $('#' + id).append(html[i]);
            }
            $('#' + id).append("<br/>");
        }
    } else {
        $('#' + id).html("");
        if (isError) {
            $('#' + id).append("<span class='error'>" + html + "</span>");
        } else {
            $('#' + id).append(html);
        }
    }
}

/**
 * Appnend html content to a object
 * If the html is a list a <br> wil be inserter between every iteration
 */
function appendContent(id, html, isError) {
    if ($.isArray(html)) {
        for (var i = 0; i < html.length; i++) {
            if (isError) {
                $('#' + id).append("<span class='error'>" + html[i] + "</span>");
            } else {
                $('#' + id).append(html[i]);
            }
            $('#' + id).append("<br/>");
        }
    } else {
        if (isError) {
            $('#' + id).append("<span class='error'>" + html + "</span>");
        } else {
            $('#' + id).append(html);
        }
        $('#' + id).append("<br/>");
    }
}

/**
 * Prepend html content to a object
 * Insert at beginning
 * If the html is a list a <br> wil be inserter between every iteration
 * The array will be inverted
 */
function prependContent(id, html, isError) {
    if ($.isArray(html)) {
        for (var i = 0; i < html.length; i++) {
            $('#' + id).prepend("<br/>");
            if (isError) {
                $('#' + id).prepend("<span class='error'>" + html[i] + "</span>");
            } else {
                $('#' + id).prepend(html[i]);
            }
        }
    } else {
        $('#' + id).prepend("<br/>");
        if (isError) {
            $('#' + id).prepend("<span class='error'>" + html + "</span>");
        } else {
            $('#' + id).prepend(html);
        }
    }
}

/**
 * Get html content of object
 */
function getContent(id) {
    return $('#' + id).html();
}