var ie = (document.all) ? true: false;
var dialogFunc = new Object();

function CheckAll(cb) {
    if (wasDownload()) return;
    var main_form = window.top.mainfrm.document.frm_main ;
    var count = 0;
    var total_space  = 0;
    var num_selected = 0;
    if (typeof(main_form.dparent) != 'undefined') count = 1;

    for (var i=0;i<main_form.elements.length;i++) {
        var e = main_form.elements[i];
        if ((e.name != 'allbox') && (e.type=='checkbox')) count++;
        if ((e.name != 'allbox') && (e.type=='checkbox') && (!e.disabled)) {
            e.checked = main_form.allbox.checked;
            if (e.checked && !isNaN(parseInt(eval('window.top.mainfrm.document.frm_main.size'+count).value)))
                total_space = total_space + parseInt(eval('window.top.mainfrm.document.frm_main.size'+count).value);
            
            if (e.checked) {
                hL(e);
                num_selected++;
            }
            else dL(e);
        }
    }
    top.mainfrm.show_msg(print_filesize(total_space), num_selected);
}

function hL(E){
    if (ie) { 
        while (E.tagName!="TR") { E=E.parentElement; }
    }
    else {
        while (E.tagName!="TR") { E=E.parentNode; }
    }
    E.className = "highlight";
}

function dL(E){
    if (ie) {
        while (E.tagName!="TR") { E=E.parentElement; }
    }
    else {
        while (E.tagName!="TR") { E=E.parentNode; }
    }    
    fillColor();
}

function fillColor () {
    var count = 0;
    var mytable = window.top.mainfrm.document.getElementById('tabMain'); 
    if (!mytable) return;
    
    var rows  = mytable.rows;
    var fmobj = window.top.mainfrm.document.frm_main.c_edit ;
    var j     = 0;
    var tmp   = (typeof(window.top.mainfrm.document.frm_main.dparent) != 'undefined') ? 2 : 1;
    for (var i=tmp; i < rows.length; i++) {
        j = i - tmp;
        if (typeof(fmobj[j]) != 'undefined' && fmobj[j].checked == true) rows[i].className = 'highlight';
        else rows[i].className = (i % 2 == 0) ? 'bg_main' : 'background';
    }
}

function CheckCheckAll(cb) {
    if (wasDownload()) return;

    if (cb.checked) hL(cb);
    else dL(cb);
    
    var main_form   = window.top.mainfrm.document.frm_main;
    var total_boxes = 0;
    var total_on    = 0;
    var total_space = 0;
    var count       = 0;
    if (typeof(main_form.dparent) != 'undefined') count = 1;
    for (var i=0; i<main_form.elements.length; i++) {
        var e = main_form.elements[i];
        if ((e.name != 'allbox') && (e.type=='checkbox')) {
            total_boxes++;
            count++;
            if (e.checked) {
                total_on++;
                if (!isNaN(parseInt(eval('window.top.mainfrm.document.frm_main.size' + count).value)))
                    total_space = total_space + parseInt(eval('window.top.mainfrm.document.frm_main.size'+count).value);
            }
        }
    }
    if (total_boxes == total_on) main_form.allbox.checked = true;
    else main_form.allbox.checked = false;
    top.mainfrm.show_msg(print_filesize(total_space), total_on);
}

function init_chmod() {
    var bottom_form = top.bottomfrm.document.frm_footer;
    var u_mod=0, g_mod=0, w_mod =0, a_mod=0;

    if (bottom_form.ar.checked) a_mod = 4;
    if (bottom_form.ur.checked) u_mod = 4;
    if (bottom_form.gr.checked) g_mod = 4;
    if (bottom_form.wr.checked) w_mod = 4;

    if (bottom_form.aw.checked) a_mod += 2;
    if (bottom_form.uw.checked) u_mod += 2;
    if (bottom_form.gw.checked) g_mod += 2;
    if (bottom_form.ww.checked) w_mod += 2;

    if (bottom_form.ax.checked) a_mod++;
    if (bottom_form.ux.checked) u_mod++;
    if (bottom_form.gx.checked) g_mod++;
    if (bottom_form.wx.checked) w_mod++;

    bottom_form.txt_input.value = a_mod + '' + u_mod + '' + g_mod + '' + w_mod ;
}

function show_chmod(name,perm) {
    if (top.wasDownload()) return;

    js_cmd_chmod(); // Print the chmod form
    var bottom_form = window.top.bottomfrm.document.frm_footer;
    var main_form   = window.top.mainfrm.document.frm_main;
    for (var i=0;i<main_form.elements.length;i++) {
        var e = main_form.elements[i];
        if (e.checked && ie) dL(e);

        if ((e.name != 'allbox') && (e.type=='checkbox') && (e.value == name)) {
            e.checked = true;
            if (ie) hL(e);
        }
        else {
            e.checked = false;
        }
    }
    window.top.bottomfrm.document.getElementById('d_status').innerHTML  = '<font color=green>chmod of <b>' + name + '</font></b>';

    if (perm.length == 16) {
        if (perm.substr(0,1) == 'r') bottom_form.ar.checked = true;
        if (perm.substr(1,1) == 'w') bottom_form.aw.checked = true;
        if (perm.substr(2,1) == 'x') bottom_form.ax.checked = true;
        if (perm.substr(4,1) == 'r') bottom_form.ur.checked = true;
        if (perm.substr(5,1) == 'w') bottom_form.uw.checked = true;
        if (perm.substr(6,1) == 'x') bottom_form.ux.checked = true;
        if (perm.substr(8,1) == 'r') bottom_form.gr.checked = true;
        if (perm.substr(9,1) == 'w') bottom_form.gw.checked = true;
        if (perm.substr(10,1)== 'x') bottom_form.gx.checked = true;
        if (perm.substr(12,1)== 'r') bottom_form.wr.checked = true;
        if (perm.substr(13,1)== 'w') bottom_form.ww.checked = true;
        if (perm.substr(14,1)== 'x') bottom_form.wx.checked = true;
    }
    else {
        if (perm.substr(0,1) == 'r') bottom_form.ur.checked = true;
        if (perm.substr(1,1) == 'w') bottom_form.uw.checked = true;
        if (perm.substr(2,1) == 'x') bottom_form.ux.checked = true;
        if (perm.substr(4,1) == 'r') bottom_form.gr.checked = true;
        if (perm.substr(5,1) == 'w') bottom_form.gw.checked = true;
        if (perm.substr(6,1) == 'x') bottom_form.gx.checked = true;
        if (perm.substr(8,1) == 'r') bottom_form.wr.checked = true;
        if (perm.substr(9,1) == 'w') bottom_form.ww.checked = true;
        if (perm.substr(10,1)== 'x') bottom_form.wx.checked = true;
    }
    init_chmod();
    top.mainfrm.show_msg('0',1);
}

function checkSelected(multi) {
    var main_form = window.top.mainfrm.document.frm_main;
    var count = 0, _checked;
    for (var i=0; i<main_form.elements.length; i++) {
        var e = main_form.elements[i];
        if ((e.name != 'allbox') && (e.type=='checkbox')&& (e.checked)) {
            _checked = true;
            count++;
        }
        if (count>1) break;
    }
    if (!_checked) {
        return (multi) ? ('Please select the files or directories.') : ('Please select a file.');
    }
    if (count > 1 && multi == false) return 'Can not execute the command because the selected file more than one.';
    return;
}

function checkFMCommand(error, isnum) {
    if (top.wasDownload()) return; 
    if (!window.top.bottomfrm.document.frm_footer.txt_input.value) return error;

    if (isnum && isNaN(window.top.bottomfrm.document.frm_footer.txt_input.value)) return 'This field is a number';
    return;
}

function checkSearch() {
    var bottom_form = top.bottomfrm.document.frm_footer;
    if (!bottom_form.txt_input.value) return 'Please enter file name or content you wish to search for';
    if (bottom_form.scope.selectedIndex == 1) return checkSelected(true);
}

function checkReplace() {
    if (top.wasDownload()) return;     
    
    var bottom_form = top.bottomfrm.document.frm_footer;
    if (!bottom_form.txt_input.value) return 'Please enter the content you wish to search for';
    if (!bottom_form.txt_with.value) return 'Please enter the content you wish to replace with';
    if (bottom_form.scope.selectedIndex == 1) return top.checkSelected(true);
}
    
function checkUpload(){
    if (top.wasDownload()) return; 
    
    var bottom_form = top.bottomfrm.document.frm_footer;
    if (!bottom_form.txt_input.value) alert('Please select the file you wish to upload');
    else {
        bottom_form.work_path.value = top.mainfrm.document.frm_main.work_path.value;
        bottom_form.submit();
    }
}
                 
function checkDownload(srv_soft) {
    if (wasDownload()) return false;

    var main_form   = window.top.mainfrm.document.frm_main;
    var bottom_form = window.top.bottomfrm.document.frm_footer;
    if (!isMainForm(true)) return false;

    var name, count = 0;
    for (var i=0;i<main_form.elements.length;i++) {
        var e = main_form.elements[i];
        if ((e.name != 'allbox') && (e.type=='checkbox')&& (e.checked)) {
            name = e.value;
            count++;
        }
        if (count > 1) { break; }
    }
    if (typeof(name) == 'undefined') {
        alert("Please select files and directories to download");
        return false;
    }
    if ( count > 1 && ( bottom_form.zip_type.selectedIndex == 0 || bottom_form.type[0].checked || bottom_form.type[2].checked ) ) {
        alert("If you want to download multiple files, it requires either 'Binary' and 'Compressed' mods");
        return false;
    }
    main_form.cmd_do.value = 'cmd_download';
    if (!srv_soft) {
        if ( main_form.action == '') main_form.action = 'fileman.cgi';
        main_form.action +=  "/" + name;
    }
    if      ( bottom_form.type.selectedIndex == 0 ) { main_form.chmode.value = 'ascii'; }
    else if ( bottom_form.type.selectedIndex == 1 ) { main_form.chmode.value = 'binary'; }
    else if ( bottom_form.type.selectedIndex == 2 ) { main_form.chmode.value = 'auto'; }
    main_form.opt_gz.value = bottom_form.zip_type.options[bottom_form.zip_type.selectedIndex].value;
    main_form.submit();
    workPath = main_form.work_path.value;
    return false;
}

function checkCommand(funcReturn, not_required) {
    var bottom_form = window.top.bottomfrm.document.frm_footer;
    var main_form   = window.top.mainfrm.document.frm_main;

    if (funcReturn) dialogFunc.returnFunc = funcReturn;

    if (top.wasDownload()) return false;
    if (typeof(main_form) == 'unknown' || typeof(main_form) == 'undefined') {
        alert("Please return to main screen and execute the command again");
        return false;
    }
    var msg;
    var act    = new Array(10);
    var cmd_do = bottom_form.cmd_do.value;
    var input  = bottom_form.txt_input.value;
    act[0] = 'cmd_copy';
    act[1] = 'cmd_delete';
    act[2] = 'cmd_move';
    act[3] = 'cmd_chmod';
    act[5] = 'cmd_perl';
    act[6] = 'cmd_tar';
    act[7] = 'cmd_download';
    act[4] = 'cmd_tail';
    act[8] = 'cmd_diff';
    act[9] = 'cmd_command';
    if ( !not_required ) {
        for (ii=0; ii< act.length; ii++) {
            if (act[ii] == cmd_do) {
                if (cmd_do == 'cmd_tail' || cmd_do == 'cmd_diff' || cmd_do == 'cmd_perl') {
                    if (typeof(main_form.type) == 'undefined') msg = checkSelected(false);
                }
                else if (cmd_do == 'cmd_command') {
                    msg = checkSelected(false);
                }
                else {
                    msg = checkSelected(true)
                }
                if (msg) {
                    alert(msg);
                    return false;
                }
                break
            }
        }
    }
    if (cmd_do != 'cmd_perl' && cmd_do != 'cmd_uncompress' && funcReturn) {
        msg = dialogFunc.returnFunc;
    }

    if (msg) {
        alert(msg); return false;
    }
    switch (cmd_do) {
        case 'cmd_uncompress':
            var uncomp_option = bottom_form.uncom_option;
            main_form.uncomp_option.value = uncomp_option.options[uncomp_option.selectedIndex].value;
            break;
        case 'cmd_tar':
            if (typeof(bottom_form.opt_gz) != 'undefined' && bottom_form.opt_gz.checked) {
                main_form.opt_gz.value = 1;
            }
            else if (typeof(bottom_form.archive_type) != 'undefined') {
                var archive_type = bottom_form.archive_type;
                main_form.opt_gz.value = archive_type.options[archive_type.selectedIndex].value;
            }
            break;
        case 'cmd_search':
            main_form.scope.value = 0;
            if (bottom_form.scope.selectedIndex == 1) main_form.scope.value     = 1;
            if (bottom_form.c_case.checked)           main_form.c_case.value    = 1;
            if (bottom_form.c_regex.checked)          main_form.c_regex.value   = 1;
            if (bottom_form.c_content.checked)        main_form.c_content.value = 1;
            break;
        case 'cmd_replace':
            main_form.scope.value = 0;
            if (bottom_form.scope.selectedIndex == 1) main_form.scope.value     = 1;
            if (bottom_form.c_case.checked)           main_form.c_case.value    = 1;
            if (bottom_form.c_regex.checked)          main_form.c_regex.value   = 1;
            if (bottom_form.c_word.checked)           main_form.c_word.value    = 1;
            if (bottom_form.c_bak.checked)            main_form.c_bak.value     = 1;
            main_form.txt_with.value = bottom_form.txt_with.value;
            break;
        case 'cmd_tail':
            var objtime = bottom_form.retime.options;
            main_form.retime.value = objtime[objtime.selectedIndex].value;
            break;
        case 'cmd_chmod':
            if (typeof(bottom_form.recursive) != 'undefined' && bottom_form.recursive.checked) main_form.opt_gz.value = 1;
    }
    if (cmd_do != 'cmd_tail') bottom_form.txt_input.value = '';
    main_form.cmd_do.value    = cmd_do;
    main_form.txt_input.value = input;
    main_form.submit();
    if (cmd_do != 'cmd_tail') bottom_form.reset();
    return false;
}

function run_command () {
    if (top.wasDownload()) return;

    var bottom_form = window.top.bottomfrm.document.frm_footer;
    var main_form   = window.top.mainfrm.document.frm_main;
    var msg;
    if (typeof(main_form.type) == 'undefined') msg = checkSelected(false);
    if (msg) { alert(msg); return; }
    main_form.cmd_do.value    = bottom_form.cmd_do.value;
    main_form.txt_input.value = bottom_form.txt_input.value;
    main_form.submit();
}

function load_progress_bar (pxs, percent, msg) {
    var bottom_doc = window.top.bottomfrm.document
    var sp_msg = bottom_doc.getElementById('msg');
    var sp_per = bottom_doc.getElementById('percent');
    var sp_img = bottom_doc.getElementById('progress');
    if (typeof(sp_img) != 'undefined') {
        var wimg = sp_img.width;
        if ( pxs == 0 ) {
            sp_per.innerHTML = "0";
            sp_msg.innerHTML = "The selected files has been copied";
        }
        else {
            sp_img.width = Number(pxs);
            sp_per.innerHTML = percent;
            sp_msg.innerHTML = "Processing... " + msg;
        }
    }
}

function additem(objright, objleft, flag){
//--------------------------------
    if (flag == 1) {// add all
        var src  = eval(objleft);
        var rows = src.options.length
        for (ii=1; ii< rows ; ii++) {
            moveitem(eval(objleft), eval(objright), 1);
        }
    }
    else {
        moveitem(eval(objleft), eval(objright));
    }
}

function removeitem(objright, objleft, flag){
//------------------------------------
    if (flag == 1) {// move all
        var src  = eval(objright);
        var rows = src.options.length
        for (ii=1; ii< rows ; ii++) {
            moveitem(eval(objright), eval(objleft), 1);
        }
    }
    else {
        moveitem(eval(objright),eval(objleft));
    }
}

function moveitem (src, tar, index) {
    var li_rows  = tar.options.length
    var li_index = (index > 0) ? index : src.selectedIndex;
    var objs = 0;

    if (li_index <= 0) { alert("Please select an item."); return; }

    // get value & text from objright
    var text = src.options[li_index].text
    var value = src.options[li_index].value

    // add item into lbleft from lbright.
    if (li_rows==0) {
        tar.options[0] = new Option(text, value);
    } 
    else {
        tar.options[li_rows] = new Option(text,value);
        tar.options[li_rows].selected = true
    }

    for (m=src.options.length-1;m>=0;m--){
        if (src.options[m].value==value) src.options[m]=null;
    }
}

function setFocus(object) {
    if (object) {
        if (typeof(object) != 'undefined') object.focus();
    }
    else {
        var top_form = top.topfrm.document.frm_top;
        if (typeof(top_form) != 'undefined') top_form.txt_input.focus();
    }
}

function rs(n, u, w, h, l, t, s) {
    var args ="width=" + w + ",height=" + h + ",top=" + t + ",left=" + l + ",resizable=no,scrollbars=" + s + ",status=0,titlebar=0";
    var remote =window.open(u, n, args);
    if (remote != null)  {
        if (remote.opener == null)
            remote.opener = self;
    }
}

function outputComma(number) {
    number = '' + number
    if (number.length > 3) {
        var mod = number.length%3;
        var output = (mod > 0 ? (number.substring(0,mod)) : '');
        for (i=0 ; i < Math.floor(number.length/3) ; i++) {
            if (mod == 0 && i == 0) output+= number.substring(mod + 3 * i, mod + 3 * i + 3);
            else  output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
        }
        return (output);
    }
    else return number;
}

function print_filesize(size) {
    var formatted_size = 0;

    if (size > 0) formatted_size = parseInt(size / 1000);
    else return 0;

    if (formatted_size == 0) return size + 'b';
    else return outputComma(formatted_size) + 'kb';
}
