﻿Type.registerNamespace('WebControls');

WebControls.BaseInput = function(element) {
    WebControls.BaseInput.initializeBase(this, [element]);
}
WebControls.BaseInput.prototype = {
    initialize: function() {
        WebControls.BaseInput.callBaseMethod(this, 'initialize');

        // if (this._inputEl) {
            $addHandlers(
                // this._inputEl, {
                this._element, {
                    // We'll put this code in here rather than in 'onBlur'
                    // so that we only need to provide an onBlur if we need it
                    blur: function() {
                        this.onBlur();
                        this.validate();                
                        this._raiseEvent('focusLost', Sys.EventArgs.Empty);
                    },
                    keypress: function(evt) {
                        return this.onKeyPress(evt);
                    }               
                },
                this
            );
        // }

//        if (!this._enabled) {
//            this.disable();
//        }

//        if (this._title != null) {
//            this.get_element().title = this._title;
//        }

    },
    dispose: function() {
        WebControls.BaseInput.callBaseMethod(this, 'dispose');
    },

    isValid: function() {
        return true;  // Default behaviour
    },

    onBlur: function() {
      
    },
   
    onKeyPress: function(e, htmlInput) {
        var evt = e || window.event;
        // var keyNum = evt.keyCode || evt.which;
        var keyNum = evt.charCode || evt.which;
        var keyChar = String.fromCharCode(keyNum);

        if (
            this.isCharAllowed(keyChar, this._element)
         && (this._maximumLength == null || this._element.value.length < this._maximumLength)
        ) {
            return true;
        } else {
            this.blinkInvalidHighlighting();
            return false;
        }
    },
    isCharAllowed: function(keyChar, htmlInputControl) {
        return true;
    },
    validate: function() {
        if (this._validationOn) {
            if (this.isValid()) {
                this.clearInvalidHighlighting();
                return true;
            } else {
                this.displayInvalidHighlighting();
                return false;
            }
        } else {
            return true;
        }
    },
    disable: function() {
        this._element.disabled = true;
    },
    enable: function() {
        this._element.disabled = false;
    },
    getInvalidHighlightEl: function() {
        var element = this._element;
        for (var i = 0; i < this._numElementsUpForValidationHighlight; i++) {
            element = element.parentNode;
        }

        return element;
    },
    displayInvalidHighlighting: function() {    
        this.getInvalidHighlightEl().style.backgroundColor = '#cc7777';
    },
    clearInvalidHighlighting: function() {
        this.getInvalidHighlightEl().style.backgroundColor = "";
    },
    blinkInvalidHighlighting: function() {
        var that = this;  
        this.displayInvalidHighlighting();
        // setTimeout("$find('" + this.get_id() + "').clearInvalidHighlighting()", 160);
        setTimeout(function() { that.clearInvalidHighlighting(); }, 160);
    }
};

WebControls.BaseInput.createProperty("enabled");
WebControls.BaseInput.createProperty("validationOn");
WebControls.BaseInput.createProperty("allowBlankEntry");
WebControls.BaseInput.createProperty("numElementsUpForValidationHighlight");
// WebControls.BaseInput.createProperty("inputEl");
WebControls.BaseInput.createProperty("title");

WebControls.BaseInput.createEvent("focusLost");

WebControls.BaseInput.registerClass('WebControls.BaseInput', Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();