// Keep track of the # of xupload objects on a page
var xupload_objs = new Array();
function xupload (formName, showProgress, successUrl, errorUrl, successScript, errorScript) {
    this.formName = formName;
    // Pop up the progress window?
    this.showProgress = (showProgress == null ? true : showProgress);
    // Post to this URL when the file is uploaded successfully
    this.successUrl = (successUrl == null ? window.location.href : successUrl);
    // Post to this URL if there is an error uploading the file
    this.errorUrl = (errorUrl == null ? window.location.href : errorUrl);
    // Run this javascript on the host page if the upload is successful
    this.successScript = successScript;
    // Run this javascript on the host page if the upload fails; 
    // set this to 1 to use the default error handler.
    this.errorScript = errorScript;
    
    // Default to 10MB upload size
    this.maxUploadSize = 10240;
    
    // Give this object a name to use for the iframe
    this.name = 'xupload_'+xupload_objs.length;
    this.progressWinName = this.name+'_progress';
        
    // Give the upload a unique ID for the temp directory that the uploaded files will go into
    this.UID = Math.round(10000*Math.random())+'0'+Math.round(10000*Math.random());
    
    // If the main window doesn't have a name, give it one based on the UID.
    // This is needed for the progress window to be able to communicate back to the main window
    if (window.name == '') {
        window.name = 'win'+this.UID;
    }

    this.num = xupload_objs.length;
    xupload_objs.push(this);
    
}

xupload.prototype.init = function() {
    // Create the iframe for the form to be directed to
    var iframe = document.createElement('iframe');
    iframe.src = '/blank.php';
    iframe.style.width = '1px';
    iframe.style.height = '1px';
    iframe.style.visibility = 'hidden';
    iframe.name = this.name;
    iframe.id = this.name; // For IE

    // Attach the iframe to the body
    document.body.appendChild(iframe);

    // For IE
    if (document.all) {
        window.frames[this.name].name = this.name;
    } 
    
    // Direct the form to the iframe
    document[this.formName].target = this.name;
    document[this.formName].setAttribute('target',this.name);

    // Set the form method and encoding
    document[this.formName].method = 'post';
    document[this.formName].enctype = 'multipart/form-data';
    document[this.formName].encoding = 'multipart/form-data'; // For IE
        
    // If we don't want to show progress, create an iframe that can be used to send
    // messages back to the main window
    if (this.showProgress == false) {
        var iframe = document.createElement('iframe');
        iframe.name = this.progressWinName;
        iframe.style.width = '1px';
        iframe.style.height = '1px';
        iframe.style.visibility = 'hidden';

        // Attach the iframe to the body
        document.body.appendChild(iframe);
    }
}

xupload.prototype.submit = function() {
    document[this.formName].action  = '/cgi-bin/xupload/upload.cgi?';
    document[this.formName].action += 'upload_id='+this.UID;
    document[this.formName].action += '&xupload=1';
    document[this.formName].action += '&max_upload_size='+this.maxUploadSize;
    document[this.formName].action += '&errorPage='+this.errorUrl;
    document[this.formName].action += '&url_post='+this.successUrl;
    document[this.formName].action += '&errorScript='+this.errorScript;
    document[this.formName].action += '&successScript='+this.successScript;
    document[this.formName].action += '&target=_parent';   
    document[this.formName].action += '&showProgress='+this.showProgress;   
    document[this.formName].action += '&xuploadNum='+this.num;   
    document[this.formName].action += '&openerName='+window.name;
    
    // Add any user-defined options
    document[this.formName].action += '&'+this.query;
    
    // Remove any # symbols in the action
    document[this.formName].action = document[this.formName].action.replace(/[#]/,'');
  
    // If we're showing progress, open a popup for the progress bar
    if (this.showProgress == true) {
        window[this.progressWinName] = window.open('/cgi-bin/xupload/upload_status.cgi?upload_id='+this.UID+'&num_files=1&css_name=esession&tmpl_name=esession','win1','width=360,height=210,resizable=1');
    } 
    // Otherwise open the progress bar in the hidden iframe
    else {
        window[this.progressWinName].location = '/cgi-bin/xupload/upload_status.cgi?upload_id='+this.UID+'&num_files=1&css_name=esession&tmpl_name=esession';
    }
    
    // Submit the form
    document[this.formName].submit();

}   

xupload.prototype.error = function(error_str) {
    
    var error = new Object;
    var error_arr = error_str.split('/');
    error.msg = error_arr[0];
    for (var i = 0; i < error_arr.length; i++) {
        var param = error_arr[i].split(':');
        error[param[0]] = param[1];
    }
    return error;
}

xupload.prototype.cancel = function() {
    window[this.name].location = '/blank.php';
}

function initXuploads() {
    for (var i = 0; i < xupload_objs.length; i++) {
        xupload_objs[i].init();
    }
}

function xuploadDefaultErrorHandler(error_str, objNum) {
    //window.toggleWaitMask(false);
    var xupload_obj = xupload_objs[objNum];
    var error = xupload_obj.error(error_str);
    switch (error.msg) {
        case 'MAX_UPLOAD_EXCEEDED':
            var maxSize = (error.MAX_SIZE > 1024) ? Math.round(error.MAX_SIZE*100)/102400+'MB' : error.MAX_SIZE+'kb';
            alert('Please upload a file smaller than '+maxSize+'.');
            xupload_obj.cancel();
        break;	        
    }
}  

window.eventsRouter.addListener('onload',initXuploads);     
