﻿(function() {
    if (!Ext) return;
    Ext.onReady(function() {
        //	Bind Panels
        Ext.select("div.panel", true).each(function(el) {
            if (!el._bind) el._bind = new Ext.Panel({
                applyTo: el,
                layout: "fit",
                autoHeight: true,
                collapsible: false,
                baseCls: "panel"
            });
        });

        //	Bind dateFields
        Ext.select("input[type=text].dateField", true).each(function(el) {
         
            if (!el._bind) el._bind = new Ext.form.DateField({
                applyTo: el,
                layout: "fit",
                autoHeight: true,
                collapsible: true,
                baseCls: "panel",
                width: 130
                ,
                validator: function(dtStr) {
              
                    if (dtStr == "" && this.allowBlank)
                        return true;


                    var reg = /(\d{1,2})[- \/.](\d{1,2})[- \/.]\d\d\d\d/;

                    if (reg.test(dtStr)) {


                        var dateValue = /(\d{1,2})[- \/.](\d{1,2})[- \/.](\d\d\d\d)/i.exec(dtStr);
                        var intMonth = parseInt(dateValue[1],10);
                        var Month = dateValue[1];
                        var Day = parseInt(dateValue[2],10);
                        var Year = parseInt(dateValue[3],10);
                        if (intMonth > 12 || intMonth <= 0)
                            return false;


                        if (Month == '2' || Month == '02') {


                            if (Day > (((Year % 4 == 0) && ((!(Year % 100 == 0)) || (Year % 400 == 0))) ? 29 : 28))
                                return false;
                        }
                        var DaysInMonth = [];
                        for (var i = 1; i <= Month; i++) {
                            DaysInMonth[i] = 31
                            if (i == 4 || i == 6 || i == 9 || i == 11) { DaysInMonth[i] = 30 }
                            if (i == 2) { DaysInMonth[i] = 29 }
                        }

                        if (Day > DaysInMonth[intMonth])

                            return false;

                        return true;
                    }
                    else

                        return false;
                }

            });
        });

        //	Bind numberFields
        Ext.select("input[type=text].numberField", true).each(function(el) {
            if (!el._bind) el._bind = new Ext.form.NumberField({
                applyTo: el
            });
        });


    });
})();
