﻿FileUpload = function(rootEl, Form)
{
    this.Form = Form;

    var fileDom = rootEl.select('input[type=file]').elements[0];
    this.fileField = Form.findField(fileDom.name);

    var texts = rootEl.select('input[type=text]').elements;

    this.urlField = null;
    for (var i = 0; i < texts.length; i++)
    {
        if (texts[i].name.match(/^url*/))
        {
            this.urlField = Form.findField(texts[i].name);
        }
        if (this.urlField != null)
        {
            break;
        }
    }
    

    var radios = rootEl.select('input[type=radio]', true).elements;
    this.sourceTypes = { url: radios[0], file: radios[1] }
    for (var i = 0; i < radios.length; i++)
    {
        var fakeObject = this;
        radios[i].dom.onclick = function()
        {
            fakeObject.SetSourceType(Ext.get(this));
        }
        if (radios[i].dom.checked)
        {
            this.SetSourceType(radios[i]);
        }
    }
}

FileUpload.prototype.ContainerDivSuffix = ".file_upload";

//set source type
FileUpload.prototype.SetSourceType = function(el) { //el, sourceType) {
    if (el == this.sourceTypes.url) {
        this.Form.add(this.urlField);
        this.urlField.clearInvalid();
        this.urlField.show();

        this.fileField.hide();
        this.Form.remove(this.fileField);
    } else {
        this.urlField.hide();
        this.Form.remove(this.urlField);

        this.Form.add(this.fileField);
        this.fileField.clearInvalid();
        this.fileField.show();
        
        //displaying fix for IE 7:
        this.fileField.focus();
    }
}

//Static Methods:

//gets rating input name from the star image name
FileUpload.SendForm = function(form, onSuccessAction, onSuccessActionParameter, scope)
{
    var tempFormId = 'temp_form_for_file_uploading'
    var submitForm = null;
    var aliasField = null;
    for (var i = 0; i < form.items.length; i++)
    {
        var item = form.items.items[i];
        if (item.getName().match(/^alias*/))
        {
            aliasField = item.getEl().dom;
            break;
        }
    }
    for (var i = 0; i < form.items.length; i++)
    {
        //if (form.items.items[i].type == 'file_upload')
        if (form.items.items[i].getXType() == 'fileuploadfield')
        {
            if (form.items.items[i].value != null)
            {
                if (submitForm == null)
                {
                    // new DOM element <form> should be added
                    var tempFormEl = document.createElement('form');
                    tempFormEl.setAttribute('id', tempFormId);
                    tempFormEl.setAttribute('style', 'display:none');
                    document.body.appendChild(tempFormEl);

                    // Ext form rendered to the form (if BasicForm rendered to <form> element, it could be submitted) 
                    submitForm = new Ext.BasicForm(tempFormEl, { fileUpload: true });
                }

                //get file input field (DOM object)
                var fileInputFieldId = form.items.items[i].getFileInputId();
                var fileInputField = document.getElementById(fileInputFieldId);

                //clone input field (new cloned field will be added instead of the current)
                var newFileInputField = fileInputField.cloneNode(true);

                //add new input field instead of the current
                fileInputField.parentNode.appendChild(newFileInputField);

                //add current input field to the new form (it will be deleted from the current FileInputField)
                tempFormEl.appendChild(fileInputField);

                var newAliasField = aliasField.cloneNode(true);
                aliasField.parentNode.appendChild(newAliasField);
                tempFormEl.appendChild(aliasField);
                //clear old file url
                form.items.items[i].setValue("");

                //set new input to FileInputField element
                var extFileInput = Ext.get(newFileInputField);
                form.items.items[i].setFileInput(extFileInput);
            }
        }
    }

    if (submitForm != null)
    {

        submitForm.submit({
            // params: parameters,
            url: '../file-upload.aspx',
            // waitMsg: 'Uploading your photo...',
            success: function(form, action)
            {
                FileUpload.OnSuccess(form, action);

                var scp = scope;
                scp.method = onSuccessAction;
                scp.method(onSuccessActionParameter, action.result)
            },
            failure: function(form, action)
            {
                FileUpload.OnFailure(form, action, onSuccessActionParameter)
            }
        });
    }
    else
    {
        var scp = scope;
        scp.method = onSuccessAction;
        scp.method(onSuccessActionParameter, null)
    }
};

FileUpload.OnSuccess = function(form, action) {
        var tempFormToDelete = document.getElementById(form.id);
        document.body.removeChild(tempFormToDelete)
        //alert('Processed files on the server - ' + action.result.FilesParameters[0].Link);
    };

    FileUpload.OnFailure = function(form, action, actionElement) {
        var tempFormToDelete = document.getElementById(form.id);
        document.body.removeChild(tempFormToDelete)
        actionElement.basicForm.el.unmask();
        alert('Processed failed on the server');
    };

FileUpload.FindLinkInFilesParameters = function(filesParameters, inputFieldName) {
        var link = null;
        for (var i = 0; i < filesParameters.length; i++) {
            if (filesParameters[i].InputFieldName == inputFieldName) {
                link = filesParameters[i].Link;
                break;
            }
        }
        return link;
    };

