﻿var ProgressBarID = "progressBar";
var FileListID = "fileList";

var IntervalID = null;
var cnt = 0;
var PreviousBytesTransfered = 0;

// Add the default values to the javascript before we load the information
if (typeof AutoMode != "undefined") {
    if (AutoMode)
    {
        addLoadEvent(function() {
            IntervalID = setInterval(GetUpdateStatus, 1000);
        })
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}    

function GetUpdateStatus()
{
    UploadProcess.GetUploadStatus(PostID, GetUpdateStatusResponse); 
}

function GetUpdateStatusResponse(result)
{        

    if (cnt >= TimeOut)
    {
        // stop the loop. no need to get any more updates.
        clearInterval(IntervalID);
        
        if (typeof OnComplete != "undefined") {
            eval(OnComplete);
        }
        
        document.getElementById(FileListID).innerHTML += 
            "<div class=\"corrupted\"><b>Timeout expired:</b> Automatic uploading of files has been interrupted. <br/> Please, check your Internet connection and firewall settings.</div><br/>";

        return;
    }
    
    cnt++;

    // Update Status Information:
    try
    {

        if( result != null)
        {
            var W =((result.CurrentBytesTransfered / result.TotalBytes) * document.getElementById("progressContainer").offsetWidth) - 4;

            document.getElementById(ProgressBarID).style.width = W + "px";
            document.getElementById(ProgressBarID).innerHTML = parseInt((result.CurrentBytesTransfered / result.TotalBytes) * 100) + "%";
    
            var fileNames = new String(result.FileNames);
            var tmp = "";
            var fileNameList = fileNames.split(";");

            for (var i = 0; i < fileNameList.length - 1; i++)
            {              
                var fileAttr = fileNameList[i].split(":");
                
                tmp += "<div class=\"fileItem\" id=\"fileItem" + i + "\"><span class=\"status_" + fileAttr[1] + "\">" + fileAttr[0] + "</span> " +
                (parseInt(fileAttr[1]) == 0 ? "Uploading..." : "<a href=\"javascript:void(0);\" onClick=\"RemoveFile(" + i + ", '" + fileAttr[0] + "')\">Remove</a>") + "</div>";
            }

            document.getElementById(FileListID).innerHTML = tmp;

            //reset TimeOut counter cnt if PreviousBytesTransfered is growing
            if ((PreviousBytesTransfered != result.CurrentBytesTransfered))
            {
                cnt = 0;
                PreviousBytesTransfered = result.CurrentBytesTransfered;
            }
            
            //if total bytes are uploaded - terminate process
            if (parseInt(result.CurrentBytesTransfered) == parseInt(result.TotalBytes))
            {
                if(result.CurrentBytesTransfered != 0)
                {
                    // stop the loop. no need to get any more updates.
                    clearInterval(IntervalID);                        
                    document.getElementById("progressContainer").style.display = 'none';
                    
                    if (typeof OnComplete != "undefined") {
                        eval(OnComplete);
                    }                   
                }
            }
        }
    }
    catch(e){}
}

function RemoveFile(id, fileName) 
{
    UploadProcess.RemoveUploadedFile(PostID, fileName, id, GetRemoveFileStatusResponse);
}

function GetRemoveFileStatusResponse(result)
{
    if (result != -1)
    {
        var fileList = document.getElementById(FileListID);
        var fileItem = document.getElementById("fileItem" + result);
        
        if (fileList && fileItem)
        {
            fileList.removeChild(fileItem);
            if (fileList.innerHTML.trim() == "")
            {
                //fileList.innerHTML = "All files has been deleted";
            }
        }
    }
}
