﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("WebControls");

// This 'element' here should be the inputEl
WebControls.FileUpload = function(element) {
    WebControls.FileUpload.initializeBase(this, [element]);
    
    this._fileName = null;
}

WebControls.FileUpload.prototype = {
    initialize: function() {
        WebControls.FileUpload.callBaseMethod(this, 'initialize');

        var that = this;

        $addHandlers(this._iframe, { load: this._iframeLoaded }, this);
        
        if (this._removeButton) {
            this._removeButton.add_buttonClicked(
                function(sender, e) {
                    // alert("Remove button clicked");
                    // Only do this if there is already a filename
                    if (that._fileName) {
                        that._fileName = null;
                        if (that._imageControl) {
                            that._imageControl.set_value(null);
                        }
                        // SHOULD WE RAISE THE EVENT?
                        // this._raiseEvent("fileUploaded", { fileName: null });
                        // Move the control to 'Ready to Save'
                        var isrc = that._iframe.src;
                        that._iframe.src = isrc + "&rts=true";
                    }
                    
                }
            );
        }
    },
    dispose: function() {
        //Add custom dispose actions here
        WebControls.FileUpload.callBaseMethod(this, 'dispose');
    },
    
    _iframeLoaded: function() {
        var oDoc = (this._iframe.contentWindow || this._iframe.contentDocument);
        if (oDoc.document) oDoc = oDoc.document;
          
        // THIS SHOULDN'T BE HARDCODED...
        // (But it will do for now)
        // var fileName = oDoc.getElementById('FileUploadPageControl$UploadFileName').value;
        var fileName = oDoc.getElementById('FileUploadPageControl_UploadFileName').value;
        if (fileName.length > 0) {        
            this._fileName = fileName;   
            if (this._imageControl) {
                this._imageControl.set_value(fileName);
            }
            this._raiseEvent("fileUploaded", { fileName: fileName });
        }
    },
    
    get_value : function() {
        // return this._inputEl.value;
        return this._fileName;
    },
 
    set_value : function(value) {
        // Need to store the fileName internally in case a new file is not uploaded
        this._fileName = value;
        
        // Need to reset control to its initial state
        var isrc = this._iframe.src;
        // this._iframe.src = isrc;
        this._iframe.src = isrc.replace('&rts=true', '');  // Remove rts param if it's there
        
        if (this._imageControl) {
            this._imageControl.set_value(value);            
        }
    },
    
    isValid : function() {
        if (this._fileName) {
            return true;
        } else {
            return false;
        }
    },
    
    reset: function() {
        var isrc = this._iframe.src;
        // this._iframe.src = isrc;    
        this._iframe.src = isrc.replace('&rts=true', '');  // Remove rts param if it's there
    }
 
}
WebControls.FileUpload.createProperty('iframe');
WebControls.FileUpload.createProperty('imageControl');
WebControls.FileUpload.createProperty('removeButton');
WebControls.FileUpload.createEvent('fileUploaded');
WebControls.FileUpload.registerClass('WebControls.FileUpload', WebControls.BaseInput);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();