﻿Type.registerNamespace('BIT.WebControls');


BIT.WebControls.NumericTextBox = function(element) 
{
    BIT.WebControls.NumericTextBox.initializeBase(this,[element]);
    
    this.addProperty('decimalPlaces', -1);
    this.addProperty('allowDecimal', false);
    this.addProperty('allowNegative', false);
    this.addProperty('checkExpression', null);
    this.addProperty('checkValue', null);
    this.addProperty('allowZero', true);
    
    this._pageRequestManager = null;
    this._handleEndRequestDelegate = null;
}

BIT.WebControls.NumericTextBox.prototype =
{
    initialize: function() {
        BIT.WebControls.NumericTextBox.callBaseMethod(this, "initialize");

        this._handleEndRequestDelegate = Function.createDelegate(this, this._handleEndRequest);

        $addHandlers(this.get_element(), { blur: this._ctrlBlur, keyup: this._ctrlKeyUp, keypress: this._ctrlKeyPress }, this);

        if (Sys.WebForms && Sys.WebForms.PageRequestManager)
            this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();

        if (this._pageRequestManager != null)
            this._pageRequestManager.add_endRequest(this._handleEndRequestDelegate);

        this._ctrlBlur();
    },

    _handleEndRequest: function(sender, arg) {
        var dataItem = arg.get_dataItems()[this.get_id()];

        if (dataItem) {
            this.get_element().value = dataItem;
            this._ctrlBlur();
        }
    },

    _ctrlBlur: function(evt) {
        var elm = this.get_element();
        var val = elm.value;

        var allowDecimal = (this.allowDecimal) && (this.decimalPlaces != 0);

        var regex = '[0-9]*';

        if (allowDecimal) {
            if (this.decimalPlaces > 0)
                regex += '\\.?[0-9]{0, ' + this.decimalPlaces + '}';
            else
                regex += '\\.?[0-9]*';
        }

        if (this.allowNegative)
            regex = '-?' + regex;

        var re = new RegExp('^' + regex + '$');

        if (!re.test(val)) {
            val = val.replace(new RegExp('[^0-9' + (allowDecimal ? '.' : '') + (this.allowNegative ? '-' : '') + ']', 'g'), '');

            if (this.allowNegative) {
                var hasNegative = val.length > 0 && val.charAt(0) == '-';
                val = val.replace(/-/g, '');
                if (hasNegative) val = '-' + val;
            }

            if (allowDecimal) {
                var re = /\\./g;
                var reArray = re.exec(val);

                if (reArray != null) {
                    var reRight = val.substring(reArray.index + reArray[0].length);
                    reRight = reRight.replace(re, '');
                    reRight = decimalPlaces > 0 ? reRight.substring(0, decimalPlaces) : reRight;
                    val = val.substring(0, reArray.index) + '.' + reRight;
                }
            }
        }

        if (this.checkExpression && val.length != 0) {
            if (!new RegExp(this.checkExpression).test(val))
                val = this.checkValue
        }

        if (elm.value != val)
            elm.value = val;

        if (parseInt(elm.value)==0 && !this.allowZero)
            elm.value = '1';

    },

    _ctrlKeyUp: function(evt) {
        this._ctrlBlur(evt);
    },

    _ctrlKeyPress: function(evt) {
        var key = evt.charCode;
        var cd = evt.rawEvent.keyCode || evt.rawEvent.which;
        var isCtrl = evt.ctrlKey;
        var keychar;


        if (isNaN(key))
            return;

        if ((key == 8) || (cd == 8) || (cd == 45) || (cd == 46) || (cd == 36) || (cd == 35) || (cd == 0x25) || (cd == 0x26) || (cd == 0x27) || (cd == 9) || (cd == 0x28) || isCtrl)
            return;

        keychar = String.fromCharCode(key);

        var allowDecimal = (this.allowDecimal) && (this.decimalPlaces != 0);

        var isFirstN = this.allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
        var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;

        if (!(isFirstN || isFirstD || /\d/.test(keychar)))
            evt.preventDefault();
    },

    dispose: function() {
        if (this._pageRequestManager != null)
            this._pageRequestManager.remove_endRequest(this._handleEndRequestDelegate);

        $clearHandlers(this.get_element());

        BIT.WebControls.NumericTextBox.callBaseMethod(this, "dispose");
    }
}

BIT.WebControls.NumericTextBox.registerClass('BIT.WebControls.NumericTextBox', Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();