// Created by Chris Miles 2007.
// (c) Copyright Locayta Limited 2007.  All rights reserved.

// Returns true if field is a file input field.
function is_fileinput(field) {
    inputtype = getNodeAttribute(field, 'type');
    if ( (inputtype == null) || (inputtype != 'file') ) {
        return false;
    }
    else {
        return true;
    }
}

// Returns the filename of the current upload
function gimme_filename() {
    return flvio_filename;
}

// Load the iframe containing the progress bar
function init_progress(progressdiv) {
    progressdiv.innerHTML = '<iframe id="progressframe" name="progressframe" src="progressbar" frameborder="0" style="width:430px;height:6em;border:0;overflow:hidden;"></iframe>';
}

// This is the 'onsubmit' handler for forms with file input field.
var start_progressbars = function(form, fileinputfield) {
    return function(evt) {
        flvio_filename = fileinputfield.value;
        
        // Load the iframe which will show the progress bar and handle updating it
        // progressdiv = getElement("uploadprogress");
        progressdiv = getElement("upload_field");
        
        // Must load iframe _after_ the POST starts otherwise it will
        //      cancel the iframe body loading.
        callLater(0.25, init_progress, progressdiv);
    }
}

// Initialise events on the form
function init_form() {
    var file_loaded = getElement("form_videofile_info");
    if (! file_loaded) {
        var form = getFirstElementByTagAndClassName("form", null);
        var allinputfields = getElementsByTagAndClassName("input", null);
        var allfileinputfields = filter(is_fileinput, allinputfields);
        var formfileinputfields = new Array();
        for (var i in allfileinputfields) {
            if (isParent(allfileinputfields[i], form)) {
                formfileinputfields.push(allfileinputfields[i]);
            }
        }
        if (formfileinputfields.length > 0) {
            // we only have one file upload field
            connect(form, 'onsubmit', start_progressbars(form, formfileinputfields[0]));
        }
    }
}

addLoadEvent(init_form);
