var updater = null;
var uploadNumber = null;
var enableElementId = null;

function uploadFile(pForm, pFileObj, pEnableElementId, pUploadNumber, pFileTypes){
    uploadNumber    = pUploadNumber;
    enableElementId = pEnableElementId;
    startUploadMonitoring(pForm, pFileObj, pFileTypes);
}


function checkStatus() {
    uploadProxy.getStatus(function(stat) {
        
        if (stat.status == 2) {
            updateProgressBar(100);
            if(enableElementId != null) {
                if($(enableElementId).disabled) {
                    $(enableElementId).disabled = false;
                }
            }
            alert("File successfully uploaded.");
            return;
        }
        
        if (stat.status == 3) {
            alert("An error has occured! " + stat.message);
            return;
        }
        
        if (stat.status == 4) {
            alert("An error has occured! " + stat.message);
            return;
        }
        
        // do something with the percentage (nice loading bar, simply show the percentage, etc)
        updateProgressBar(stat.percentComplete);
        
        window.setTimeout("checkStatus()", 500);
    });
    
}



function updateProgressBar(pPercentage) {
    // make sure you set the width style property for uploadProgressBar, otherwise progress.style.width won’t work
    var progress = document.getElementById("uploadProgressBar" + uploadNumber);
    var indicator = document.getElementById("uploadIndicator" + uploadNumber);
    var maxWidth = parseIntWithPx(progress.style.width) - 4;
    var width = pPercentage * maxWidth / 100;
    indicator.style.width = width + "px";
    var perc = document.getElementById("uploadPercentage" + uploadNumber);
    perc.innerHTML = pPercentage + "%";
}

function parseIntWithPx(str) {
    var strArray = str.split("p");
    return parseInt(strArray[0]);
}

function checkFileType(pFileName, pFileTypes) {
    
    var lFileTypes = pFileTypes.split("|");
    
    if(pFileName != null && !pFileName.blank()) {
        var pFileNameLowCase = pFileName.toLowerCase();
        var lTypeOk = false;
        var lTypes = "";
        for (var i=0; i < lFileTypes.length; i++){
            if (pFileNameLowCase.endsWith(lFileTypes[i]) ){
                lTypeOk = true;
            }
            
            lTypes += lFileTypes[i];
            
            if (lFileTypes.length == 2 && i == 0){
                lTypes += " or ";
            }else if(lFileTypes.length > 2 && i < (lFileTypes.length - 2)){
                lTypes += ", ";
            }else if(lFileTypes.length > 2 && i == (lFileTypes.length - 2)){
                lTypes += " or ";
            }
        }
        
        if(lTypeOk) {
            return true;
        }
    }
    alert("File type must be " + lTypes);
    return false;
}

function startUploadMonitoring(pForm , pFileObj, pFileTypes) {
    if(!checkFileType(pFileObj.value, pFileTypes)) {
        $(enableElementId).disabled = false;
        return false;
    }
    
    if(enableElementId != null && !enableElementId.blank()) {
        $(enableElementId).disabled = true;
    }
    
    $(pFileObj).style.display='none';
    
    $('uploadStatus' + uploadNumber).style.display='block';
    
    pForm.submit();
    
    window.setTimeout("checkStatus()", 500);
    return true;
}
