function VVUpload_UploadManager(clientId, uploadId, progressAreaId, progressLocation)
{
    // TODO: null circular reference in dispose
    this.id = clientId;
    this.iframe = document.getElementById(clientId);
    this.iframe.uploadManager = this;
           
    //this.isComplete = false;
    //this.inProgress = false;

    this.submittedElement = null;
        
    this.HookSubmitButtons();
    
    // TODO: hook up dispose
}

VVUpload_UploadManager.prototype.HookSubmitButtons = function()
{
    var inputs = document.getElementsByTagName("input");
    
    for (var i = 0; i < inputs.length; i++)
    {
        var input = inputs[i];
        
        switch (input.type.toLowerCase())
        {
            case "submit":
            case "image":
                input.ElementSubmitted = VVUpload_SubmitElementClicked;
                VVUpload_AddHandler(input, "click", "ElementSubmitted", this);
                
                break;
        }
    }
    
    var buttons = document.getElementsByTagName("button");

    for (var i = 0; i < buttons.length; i++)
    {
        var button = buttons[i];
        
        button.ElementSubmitted = VVUpload_SubmitElementClicked;
        VVUpload_AddHandler(button, "click", "ElementSubmitted", this);
    }
}

VVUpload_UploadManager.prototype.OnSubmit = function()
{
    if (this.iframe.contentWindow != null)
        this.iframeDoc = this.iframe.contentWindow.document;
    else
        this.iframeDoc = window.frames[clientId].document;
    
    this.iframeDoc.getElementsByTagName("form")[0].submit();
    
    if (!VVUpload_IsSafari())
    {
        this.iframe.style.display = "none";
    }
    else
    {
        this.iframe.style.width = "1px";
        this.iframe.style.height = "1px";        
    }
    
    //Disable everything on the screen to prevent other links 
    //from interrupting the upload process
    initUploadMask();
    
}

VVUpload_UploadManager.prototype.SubmitParent = function()
{
    Auersoft.VVUpload.IsUploadInProcess = false;
    
    document.getElementsByTagName("form")[0].onsubmit = null;
    
    if (this.submittedElement == null)    
        document.getElementsByTagName("form")[0].submit();
    else
        this.submittedElement.click();
}

function VVUpload_SubmitElementClicked(e, uploadManager)
{
    uploadManager.submittedElement = this;
}

function VVUpload_UploadManagerSetup()
{
    // Envelop the onsubmit function
       
    var form = document.getElementsByTagName("form")[0];
    
    if (form.onsubmit != null && VVUpload_OriginalOnSubmit == null)// && (form.onsubmit.length > 0 || typeof form.onsubmit == "function"))
        VVUpload_OriginalOnSubmit = form.onsubmit;
    
    form.onsubmit = VVUpload_Submit;
}

