﻿var intervalID = 0;
var progressBar;
var fileUpload;
var form;
var token;

function register(form, fileUpload, token)
{
    this.form = form;
    this.fileUpload = fileUpload;
    this.token = token;
}        

function pageLoad()
{
    $addHandler($get('upload'), 'click', onUploadClick);
    $get('upload').disabled = '';    
    progressBar = $find('_ctl7_progress');
    if(progressBar == null)
        progressBar = $find('_ctl3_progress');   
}

function onUploadClick(){
    
    //  todo: insert any regex to do any simple validation checks on
    //  the client.
    if(fileUpload.value.length > 0)
    {
        //  disable the upload button
        $get('upload').disabled = 'disabled';
        
        //  update the message
        updateMessage('info', '');
        
        //  submit the form containing the fileupload
        form.submit();
        
        //  hide the frame
        Sys.UI.DomElement.addCssClass($get('uploadFrame'), 'hidden');
        
        //  zero out the progressbar and show it
        progressBar.set_percentage(0);
        progressBar.show();                

        intervalID = window.setInterval(function()
        {
            var request = new Sys.Net.WebRequest();
            request.set_url('UploadProgress.ashx?UploadStatus=' + token + '&ts=' + new Date().getTime());
            request.set_httpVerb('GET');                
            request.add_completed(function(executor)
            {
                if (executor.get_responseAvailable())            
                {
                    var e = executor.get_xml().documentElement;                   
                    var empty = e.getAttribute('empty');

                    if(!empty)
                    {
                        var empty = e.getAttribute('empty');                    
                        var percent = e.getAttribute('progress');
                        var file = e.getAttribute('file');
                        var kbs = Math.round(parseInt(e.getAttribute('bytes')) / 1024);    
                        var size = Math.round(parseInt(e.getAttribute('size')) / 1024);                        
                    
                        progressBar.set_percentage(percent);
                        updateMessage('info', '');
                        
                        if(percent == 100)
                            window.clearInterval(intervalID);                        
                   }                    
            }
        });  
            
        request.invoke();
        }, 500);
    }
    else
        onComplete('error', 'You need to select a file.');
}

function onComplete(type, msg, content)
{
    window.clearInterval(intervalID);
    updateMessage(type, msg);
    progressBar.hide();
    progressBar.set_percentage('0');
    $get('upload').disabled = '';
    Sys.UI.DomElement.removeCssClass($get('uploadFrame'), 'hidden');
}

function updateMessage(type, value)
{
    var status = $get('_ctl7_status');
    if(status == null)
        status = $get('_ctl3_status');

    //if(type == 'success')
    //    value = '<span style="color:#F00000">' + value + '</span>'
    status.style.color = '#666';        
    if(status != null)
    {
        if(status.nextSibling == null)
        {
            status.innerText = value;        
            status.style.color = '#F00000';
            }
        else
            status.nextSibling.nodeValue = value;

        status.className = type;        
        Sys.UI.DomElement.addCssClass(status, type);        
    }
}

function changeItem(id, action) {
    if (action=="hide") 
        document.getElementById(id).style.display = "none";
    else
        document.getElementById(id).style.display = "block";
}
