﻿function file_change(sender,args) { }

function add_file(ctrl,el) {
    ctrl.AddFile();
    if (ctrl.FileCount == ctrl.MaximumFileCount) el.className = "add-d";
}

function remove_files(ctrl) {
    if (ctrl.FileCount < ctrl.MaximumFileCount) el.className = "add";
}

function move_caret(el) {
    var i = el.value.length + 1;
    if (el.createTextRange) {
        var c = document.selection.createRange().duplicate();
        while (c.parentElement() == el && c.move("character",1) == 1) --i;
    }
}


function upload_begin(sender,args) { }

function upload_end(sender,args) {
}


//    File size formatting function
//    n = size in bytes
//    fmt = output format ("b","kb","mb","gb") [optional; if omitted it will return the best match]
function format_file_size(n,fmt) {
    if (!fmt) {        //    no formatting specified; automatically select the best format
        if (n < 1000) fmt = "b";
        else if (n < 1000000) fmt = "kb";
        else if (n < 1000000000) fmt = "mb";
        else fmt = "gb";
    }

    switch(fmt.toLowerCase()) {
        case "kb": return String((n * 0.001).toFixed(2)) + " KB";break;
        case "mb": return String((n * 0.000001).toFixed(2)) + " MB";break;
        case "gb": return String((n * 0.000000001).toFixed(2)) + " GB";break;
        default: return String(n.toFixed(2)) + " B";
    }
}

function get_percentage(n) { return String(Math.round(n * 100)); }

//    Time formatting function
//    t = time in seconds
//    returns a string formatted thusly: hh:mm:ss [hours is only present if needed]
function format_time(t) {
    var s = Math.floor(t);
    var m = Math.floor(s / 60);
    var h = Math.floor(m / 60);

    s = pad_time(s % 60);
    m = pad_time(m % 60) + ":";
    h = (h == 0) ? "" : pad_time(h % 60) + ":";

    return (h + m + s);
}

function pad_time(t) { return String(((t > 9) ? "" : "0") + t); }

function init_upload(ctrl) {
    if (ctrl.GetFiles().length > 0 && !ctrl.Uploading) {
        ctrl.Upload();
        UploadDialog.Show();
    }
}
