﻿function ODATextBoxVersion(){
    return "1.0";
}

//if(typeof ODACommonLibrary == 'undefined' || ODACommonLibrary() != "1.0"){
//    alert("ODATextBox.js requires CommonLibrary.js which could not be located. on page " + location.href);
//}

//if(typeof ValidationScriptVersion == 'undefined' || ValidationScriptVersion() != "1.0"){
//    alert("ODATextBox.js requires ValidationScripts.js which could not be located. on page " + location.href);
//}

var _ValidationTypes = Class.create();
_ValidationTypes.prototype = {
    initialize: function(){
        this.Double = 0;
        this.Integer = 1;
        this.DateTime = 2;
        this.Time = 3;
        this.Money = 4;
        this.Percent = 5;
        this.None = 6;
    }
};
var ValidationTypes = new _ValidationTypes();

var _DateValidationModes = Class.create();
_DateValidationModes.prototype = {
    initialize: function(){
        this.DateAndOptionalTime = 1;
        this.DateAndRequiredTime = 2;
        this.DateOnly = 3;
        this.Time = 4;
    }
};

var DateValidationModes = new _DateValidationModes();

var currentValues = new Hash();

function setCurrentValue(el) {
    el = $(el);
    currentValues.set(el.id, $F(el));
}
function raiseOnChange(el,mask) {
    if (currentValues.keys().indexOf(el.id) > -1) {
        if (currentValues.get(el.id) != $F(el) && $F(el) != mask) {
            fireEvent(el, "change");
        }
    }
}

var ODATextBox = Class.create();

ODATextBox.prototype = {
    initialize: function(_txtBox, _validationType, _dateValidationMode, _initCalendar, _currencySymbol) {
        this.txtBox = $(_txtBox);
        this.validationType = _validationType == null ? ValidationTypes.None : _validationType;
        this.dateValidationType = _dateValidationMode == null ? DateValidationModes.DateAndOptionalTime : _dateValidationMode;
        this.InitCalendar = _initCalendar == null ? false : _initCalendar;
        this.txtBox.odaTextBox = this;
        this.currencySymbol = _currencySymbol == null ? "$" : _currencySymbol;

        this.BindEvents();
    },

    BindEvents: function() {
        switch (this.validationType) {
            case ValidationTypes.Double:
                setMask(this.txtBox, "xx.xx");

                Event.observe(this.txtBox, "keydown", function(e) {
                    if (e.element().readOnly) { Event.stop(e); return false; }
                    validateDouble(e.element(), e);
                });

                Event.observe(this.txtBox, "focus", function(e) {
                    clearMask(e.element(), 'xx.xx');
                    setCurrentValue(e.element());
                    setFocus(e.element());
                });

                Event.observe(this.txtBox, "blur", function(e) {
                    if ($F(e.element()) == '.') {
                        $(e.element()).value = '';
                    }
                    setMask(e.element(), 'xx.xx');
                    raiseOnChange(e.element(), 'xx.xx');
                });
                break;

            case ValidationTypes.Integer:
                setMask(this.txtBox, 'xx');
                Event.observe(this.txtBox, "keydown", function(e) {
                    if (e.element().readOnly) { Event.stop(e); return false; }
                    validateInteger(e.element(), e);
                });

                Event.observe(this.txtBox, "focus", function(e) {
                    clearMask(e.element(), 'xx');
                    setCurrentValue(e.element());
                    setFocus(e.element());
                });

                Event.observe(this.txtBox, "blur", function(e) {
                    setMask(e.element(), 'xx');
                    raiseOnChange(e.element(), 'xx');
                });

                break;

            case ValidationTypes.DateTime:
                var mask = "mm/dd/yyyy hh:mm am/pm";
                if (this.dateValidationType == DateValidationModes.DateOnly) {
                    mask = "mm/dd/yyyy";
                }
                var dateMode = this.dateValidationType;

                setMask(this.txtBox, mask);
                Event.observe(this.txtBox, "focus", function(e) {
                    clearMask(e.element(), mask);
                    setCurrentValue(e.element());
                    setFocus(e.element());
                });

                Event.observe(this.txtBox, "blur", function(e) {
                validateBlurHandler(e.element(), e.element().id, mask, dateMode);
                    raiseOnChange(e.element());
                });

                Event.observe(this.txtBox, "keydown", function(e) {
                    if (e.element().readOnly) { Event.stop(e); return false; }
                    validateDateTimeKeyDown(e.element(), dateMode, e);
                });

                if (this.InitCalendar) {
                    this.InitializeCalendarLookup();
                }

                break;

            case ValidationTypes.Time:
                var mask = "hh:mm am/pm";
                setMask(this.txtBox, mask);
                Event.observe(this.txtBox, "focus", function(e) {
                    clearMask(e.element(), mask);
                    setCurrentValue(e.element());
                    setFocus(e.element());
                });
                this.blurHandler = this.ValidateBlurHandler.bindAsEventListener(this, "hh:mm am/pm");
                Event.observe(this.txtBox, "blur", this.blurHandler);

                this.keyDownHandler = this.ValidateTimeKeyDown.bindAsEventListener(this);
                Event.observe(this.txtBox, "keydown", this.keyDownHandler);

                if (this.InitCalendar) {
                    this.InitializeCalendarLookup();
                }

                break;

            case ValidationTypes.Money:
                var curSymb = "$";
                if (this.currencySymbol && this.currencySymbol != '') {
                    curSymb = this.currencySymbol;
                }
                setMask(this.txtBox, curSymb + 'xx.xx');
                Event.observe(this.txtBox, "keydown", function(e) {
                    if (e.element().readOnly) { Event.stop(e); return false; }
                    validateMoney(e.element(), 2, e, curSymb);
                });

                Event.observe(this.txtBox, "focus", function(e) {
                    clearMask(e.element(), curSymb + 'xx.xx');
                    setCurrentValue(e.element());
                    setFocus(e.element());
                });

                Event.observe(this.txtBox, "blur", function(e) {
                    if (e.element().value == curSymb) {
                        e.element().value = '';
                    }
                    setMask(e.element(), curSymb + 'xx.xx');
                    raiseOnChange(e.element(), curSymb + 'xx.xx');
                });

                break;

            case ValidationTypes.Percent:
                setMask(this.txtBox, 'xx.xx%');
                Event.observe(this.txtBox, "focus", function(e) {
                    clearMask(e.element(), 'xx.xx%');
                    setCurrentValue(e.element());
                    setFocus(e.element());
                });

                Event.observe(this.txtBox, "blur", function(e) {
                    if (e.element().value == '%') {
                        e.element().value = '';
                    }
                    setMask(e.element(), 'xx.xx%');
                    raiseOnChange(e.element(), 'xx.xx%');
                });

                Event.observe(this.txtBox, "keydown", function(e) {
                    if (e.element().readOnly) { Event.stop(e); return false; }
                    return validatePercent(e.element(), 2, e);
                });
                break;

            case ValidationTypes.None:
                break;
        }
    },
    ValidateBlurHandler: function(_event) {
        var data = $A(arguments);
        data.shift();
        validateBlurHandler(this.txtBox, this.txtBox.id, data[0], this.dateValidationType);
        raiseOnChange(_event.element());
    },

    ValidateTimeKeyDown: function(_event) {
        _event = ensureEvent(_event);

        if (this.txtBox.readOnly) { Event.stop(_event); return false; }
        validateTimeKeyDown(this.txtBox, this.dateValidationType, _event);
    },

    SetValue: function(_value) {
        this.txtBox.value = _value;
        if (ODATech.visible(this.txtBox)) {
            this.txtBox.focus();
            this.txtBox.blur();
        }
    },

    GetValue: function() {
        var value = $F(this.txtBox);

        switch (this.validationType) {
            case ValidationTypes.Double:
                if (value == "xx.xx") {
                    value = "0";
                }
                break;

            case ValidationTypes.Integer:
                if (value == 'xx') {
                    value = "0";
                }
                break;

            case ValidationTypes.DateTime:
                var mask = "mm/dd/yyyy hh:mm am/pm";
                if (this.dateValidationType == DateValidationModes.DateOnly) {
                    mask = "mm/dd/yyyy";
                }

                if (value == mask) {
                    return "";
                }
                break;

            case ValidationTypes.Time:
                var mask = "hh:mm am/pm";
                if (value == mask) {
                    return "";
                }
                break;

            case ValidationTypes.Money:
                var curSymb = "$";
                if (this.currencySymbol && this.currencySymbol != '') {
                    curSymb = this.currencySymbol;
                }
                if (value == curSymb + 'xx.xx') {
                    value = "0";
                }
                else {
                    value = value.replace(curSymb, "");
                }
                break;

            case ValidationTypes.Percent:
                setMask(this.txtBox, 'xx.xx%');
                if (value == "xx.xx%") {
                    value = "0";
                }
                else {
                    value = value.replace("%", "");
                }
                break;
        }
        return value;
    },

    InitializeCalendarLookup: function() {
        if (this.validationType == ValidationTypes.DateTime) {
            var img = new Element("IMG", { src: "/images/DatePicker.jpg", alt: "Date Picker" });
            img.id = this.txtBox.id + "_DatePicker";
            img.name = img.id;

            var txtBoxID = this.txtBox.id;
            this.txtBox.insert({ after: img });

            if (this.dateValidationType == DateValidationModes.DateOnly) {
                Event.observe(img, 'click', function(event) { var cds = new CalendarDateSelect(txtBoxID, { time: false, popup: 'force' }); setZIndex(txtBoxID, cds.calendar_div); });
            }
            else {
                Event.observe(img, 'click', function(event) { var cds = new CalendarDateSelect(txtBoxID, { time: true, popup: 'force' }); setZIndex(txtBoxID, cds.calendar_div); });
            }
        }
        else if (this.validationType == ValidationTypes.Time) {
            var img = new Element("IMG", { src: "/images/DatePicker.jpg", alt: "Time Picker" });
            var txtBoxID = this.txtBox.id;
            this.txtBox.insert({ after: img });
            var mask = this.mask;

            Event.observe(img, "click", function() { timePickerClickHandler(txtBoxID, mask); });
        }
    }
};

function timePickerClickHandler(element, mask){
    element = $(element);
    
    if($(element.id + "_timePicker")){
        $(element.id + "_timePicker").show();
    }
    else{
        var newWindow = new divWindow(element.id + "_timePicker", "Time Picker");
        newWindow.clonePosition(element, {setHeight:false,setWidth:false,offsetTop:20});
        
        // add the dropdown with the time entries
        
        var ddl = new Element("SELECT");
        ddl.writeAttribute("onchange", "clearMask($('" + element.id + "'),'" + mask + "'); $('" + element.id + "').value = $F(this); $('" + element.id + "_timePicker').hide();");
        
        var hours = new Array();
        for(var j=0;j<12;j++)
        {
            if(j== 0){hours.push(12);}
            else if(j>0 && j<10){hours.push('0' + j);}
            else {hours.push(j);}
        }
        var minutes = new Array();
        minutes.push("00");
        minutes.push("15");
        minutes.push("30");
        minutes.push("45");
        
        for(var j=0;j<24;j++){
            var meridiem = "AM";
            if(j>11){meridiem = "PM";}
            
            for(var x=0;x<4;x++){
                var option = new Element("OPTION");
                option.text = hours[j%12].toString() + ':' + minutes[x].toString() + ' ' + meridiem;
                option.value = option.text;
                ddl.options.add(option);
            }
        }
        
        $(element.id + "_timePicker_content").update(ddl);
    }
}