/**
 * Upload Javascript Class to Upload files to a remote server
 */
function Upload(varName) {
    this.placeHolder = undefined;
    this.form = undefined;
    this.uploadTimerId = undefined;
    this.varName = varName;
    this.upc_progresskey = undefined;
    this.statusPending = false;
    this.uploading = false;
    this.fileName = undefined;
    this.onUploadReady = undefined;
}

    function uniqid()
        {
        var newDate = new Date;
        return newDate.getTime();
        }


/**
 * Retrieves the logged in userId from the server
 */
Upload.prototype.postForm=function(formName,okPlaceHolderId) {
   this.form = $('form[name='+formName+']');

   //Remove Old Inputs if they exist
   this.form.find('input[name=APC_UPLOAD_PROGRESS]').remove();
   this.form.find('input[name=varName]').remove();
   this.form.find('input[name=ajaxFunction]').remove();
   this.form.find('input[name=ajaxModule]').remove();

   //add the ajaxModule and ajaxFunction fields to the form
   this.form.append('<input type="hidden" name="ajaxFunction" value="upload" >');
   this.form.append('<input type="hidden" name="ajaxModule" value="upload" >');

   //Create a uniq upload progress id and add it to the form before the file input
   this.upc_progresskey = uniqid();
   var fileInput = this.form.find('input[type=file]');
   $('<input type=hidden name="APC_UPLOAD_PROGRESS" value="'+this.upc_progresskey+'" >').insertBefore(fileInput);

   //Add the name of the upload variable to the form
   this.form.append('<input type="hidden" name="varName" value="'+this.varName+'" >');

   this.placeHolder = $('#'+okPlaceHolderId);
   this.placeHolder.html('<iframe width=1px height=1px name="uploadTarget" id="uploadTarget" style="display:none"></iframe>');
   this.form.prop('target','uploadTarget');
   this.form.find('input[type=submit]').click();
   this.startUpload();
   return false;
}

Upload.prototype.getStatus=function() {
    if (this.statusPending) return false;
    this.statusPending = true;
    var tempData = undefined;
    var pending = false;
    $.ajax({
        type: 'POST',
        url: '/ajax.php',
        data: "ajaxModule=upload&ajaxFunction=status&progressKey="+this.upc_progresskey,
        error: function () {
            pending = false;
        },
        success: function (data) {
            pending = false;
            tempData = data;
        },
        async: false
    });
    this.statusPending = pending;
    if (tempData !== undefined) this.showStatus(tempData);
    if (this.uploading === false) clearInterval(this.uploadTimerId);
    return false;
}

Upload.prototype.showStatus=function(data) {
    if (this.placeHolder.find('iframe').get(0) !== undefined) {
        if (isNaN(data)) {
            this.placeHolder.find('div').html('<table><tr><td>'+data+'</td></tr></table>');
        } else {
            this.placeHolder.find('div').html('<table bgcolor=white width="'+data+'%"><tr><td></td></tr></table>');
        }
    }
}

Upload.prototype.startUpload=function() {
    this.statusPending = false;
    this.placeHolder.append('<div>Uploading File...</div>');
    this.uploadTimerId = setInterval(this.varName+'.getStatus();',1000);
    this.uploading = true;
}

Upload.prototype.stopUpload=function(status) {
    clearInterval(this.uploadTimerId);
    this.uploading = false;
    this.placeHolder.find('iframe').remove();
    this.placeHolder.find('div').html(status);
    this.fileName = status;
    if (this.onUploadReady != undefined) {
        this.onUploadReady();
    }
}
