﻿/// <reference path="jquery-1.3.vsdoc.js" />

/********  DatePanel v1.0.10  *********

**  Requierments:
  - jquery-1.3.2.min.js
 
** Author: 
  Konrado Pociasko
  konradpociask@gmail.com
 
** IMPORTANT
 Remember not to use it before document is ready.
 
** DatePanel Members:
Name                |   type/return |   description
----------------------------------------------------------------------
dateFrom            |   date        |   DateFrom object
dateTo              |   date        |   DateTo object
sSeparator          |   string      |   String wich is between day and month, and month and year - default '.'
                                    |   e.g. if sSeparator = '*' then dateformat will be: dd*MM*yyyy
                                    |   Default value = '.'
reset()             |   void        |   Restore start condition of the control
getTypeOfDates()    |   string      |   return type of Dates e.g. 'dm' ('d' - day, 'm' - month, 'y' - year)
getFromValue()      |   string      |   return value of DateFrom    (format: dd.MM.yyyy)
getToValue()        |   string      |   return value of DateTo  (format: dd.MM.yyyy)
getIsPeriod()       |   bool        |   true if Period checkbox is checked otherwise false

setFromValue(a_sValue, a_chType, a_sSeparator)                 |   void    | Set From date
setToValue(a_sValue, a_chType, a_sSeparator)                   |   void    | Set To date and set IsPeriod
setValues(a_sFromValue, a_sToValue, a_sTypes,  a_sSeparator)   |   void    | Set both dates and if they are not equal set IsPeriod

setIsPeriod(bool)   |   void        |   check or uncheck Period checkobx

copyFromDP(datePanel)|  void        |   Copy settings and values from another datePanel object

 
** Example of use:
 
var dpFirst;
 
$(document).ready(function() {
    var dpFirst = new datePanel($("[id$=m_divDatePanel1]"), '-');
});
 
var dpFirst = new datePanel(JQueryContainerObject);
var sDateType = dpFirst.getTypeOfDates();
var sDateFrom = dpFirst.getFromValue();
var sDateTo = dpFirst.getToValue();
dpFirst.reset();
 
*/

// DatePanel class


 
function datePanel(a_jqoContainer, a_sSeparator) {
    this.sSeparator = '.';
    if (a_sSeparator != null) {
        this.sSeparator = a_sSeparator;
    }
    // From and To date
    this.dateFrom = new date(a_jqoContainer.children('div').children().eq(0)), this.sSeparator;
    this.dateTo = new date(a_jqoContainer.children('div').children().eq(1), this.sSeparator);
   

    
    // Type of Dates
    this.getTypeOfDates = function() {
            return this.dateFrom.sType + this.dateTo.sType;
    }

    // Value of FromDate
    this.getFromValue = function() {
//         if(this.dateFrom.sType == "n")
//            return this.dateTo.getValue();

         return this.dateFrom.getValue();
    }
    // Value of ToDate
    this.getToValue = function() {
         if(this.dateTo.sType == "n")
            return this.dateFrom.getValue();

         return this.dateTo.getValue();
        
    }
    // Set Date From value
    this.setFromValue = function(a_sValue, a_chType, a_sSeparator) {
        this.dateFrom.setValue(a_sValue, a_chType, a_sSeparator);
    };
    // Set Date To value
    this.setToValue = function(a_sValue, a_chType, a_sSeparator) {
        this.dateTo.setValue(a_sValue, a_chType, a_sSeparator);
        this.setIsPeriod(true);
    };
    // Set both values
    this.setValues = function(a_sFromValue, a_sToValue, a_sTypes, a_sSeparator) {
        if(a_sTypes.charAt(0) != "n")
        {
            this.setFromValue(a_sFromValue, a_sTypes.charAt(0), a_sSeparator);
        }

        if(a_sTypes.charAt(1) != "n")
        {
            this.setToValue(a_sToValue, a_sTypes.charAt(1), a_sSeparator);
        }
    };

    // Return true if Period checkbox is checked otherwise false
    this.getIsPeriod = function() { return chbPeriod[0].checked };
    // Set Checkboxes checked value
    this.setIsPeriod = function(a_bValue) {
        chbPeriod[0].checked = a_bValue;
        chbPeriod.trigger('click');
        chbPeriod[0].checked = a_bValue;
    };
    
    // Reset
    this.reset = function() {
        chbPeriod[0].checked = false;
        this.dateTo.dateObject.hide();
        this.dateFrom.reset();
        this.dateTo.reset();
        this.dateFrom.labCaption.text('Data:');
    }

    // Copy settings and values from another datePanel object
    this.copyFromDP = function(a_dpAnotherDP) {
        this.sSeparator = a_dpAnotherDP.sSeparator;

        this.dateFrom.copyFrom(a_dpAnotherDP.dateFrom);
        this.dateTo.copyFrom(a_dpAnotherDP.dateTo);

        this.setIsPeriod(a_dpAnotherDP.getIsPeriod());
    };
    
    
    //init
    this.dateTo.dateObject.hide();
     var chbPeriod = $('input:checkbox', a_jqoContainer);
    //var chbPeriod= $('input:checkbox', a_jqoContainer.children('div').children().eq(0));
    //chbPeriod.eq(4).attr('name', a_jqoContainer[0].id + '_' + chbPeriod.eq(4).attr('name'));
    
    
    chbPeriod.bind('click',{ dpInstance:this }, timeSpanToggle);
    
    function timeSpanToggle(event) {
        if(this.checked)
        {
                event.data.dpInstance.dateTo.dateObject.show();
                event.data.dpInstance.dateFrom.labCaption.text('Od:');
                event.data.dpInstance.dateTo.labCaption.text('Do:');
        }
        else
        {
                event.data.dpInstance.dateTo.dateObject.hide();
                event.data.dpInstance.dateFrom.dateObject.show();
                event.data.dpInstance.dateFrom.labCaption.text('Data:');
        }
    }


    // date class
    function date(a_divDate, a_sSeparator) {
        this.sSeparator = '.'
        if (a_sSeparator != null) {
            this.sSeparator = a_sSeparator;
        }
        var divInputs = a_divDate.children();
        
        // Date Container
        this.dateObject = a_divDate;
        // Years input (DDL)
        var year = this.years = divInputs.eq(1);
       // this.years[0].value = 'rok';
        // Months input (DDL)
        var  month = this.months = divInputs.eq(2);
        // Days input (DDL)
        var day = this.days = divInputs.eq(3);

        // type of date
        this.sType = 'n';
        // Caption label
        this.labCaption = divInputs.eq(0);

            
        // Reset
        this.reset = function() {
            this.sType = 'n';
            this.years.val("");
            this.months[0].selectedIndex = 0;
            this.days[0].selectedIndex = 0;
        };

        // On Day or Month or Year changed
        this.months.bind('change', { dateInst: this }, onDayMonthYearChanged);
        this.years.bind('change', { dateInst: this }, onDayMonthYearChanged);
        this.days.bind('change',{ dateInst: this }, onDayMonthYearChanged);
        
     
         
        this.years.keypress(function(e) {
                var iYear = year[0].value.toString();
                if((e.which ==0 || e.which ==8) && iYear.length > 1)
                    return true;
                if((e.which ==0 || e.which ==8) && iYear.length == 1){
                     month[0].selectedIndex = 0;
                     month.attr('disabled','disabled');
                     day[0].selectedIndex = 0;
                     day.attr('disabled','disabled');
                    return true;
                }
                if(e.which > 57)
                    return false;
                else if(e.which < 48)
                    return false;
                divInputs.eq(2).removeAttr('disabled','disabled');
                if(this.value == 'rok')
                    this.value = null;              
            });

        // Value of a date
        this.getValue = function() {
            
                
            switch (this.sType) {
                case 'd':
                    if(this.days[0].value.toString().indexOf("dzień") != -1)
                        return null;
                    var sDay = this.days[0].value.toString();
                    if (sDay.length == 1) {
                        sDay = "0" + sDay;
                    }
                    return sDay + this.sSeparator + this.months[0].value + this.sSeparator + this.years[0].value;
                    break;
                case 'm':
                    if(this.months[0].value.toString().indexOf("miesiąc") != -1)
                        return null;
                    return '01' + this.sSeparator + this.months[0].value + this.sSeparator + this.years[0].value;
                    break;
                case 'y':
                    if(this.years[0].value.toString().indexOf("rok") != -1 )
                        return null;
                    return '01' + this.sSeparator + '01' + this.sSeparator + this.years[0].value;
                    break;
            }

        }

        // Copy settings and data from given date object
        this.setValue = function(a_sValue, a_chType, a_sSeparator) {
            if (a_sSeparator != null) {
                this.sSeparator = a_sSeparator;
            }
            this.sType = a_chType;

            var iRBindex = 0;
            if (a_sValue != null)
            {
                var asValue = a_sValue.split(this.sSeparator);
                switch (a_chType) {
                    case 'd':
                        iRBindex = 0;
                        this.days[0].value = asValue[0];
                        this.months[0].value = asValue[1];
                        this.years[0].value = asValue[2];
                        this.days.removeAttr('disabled','disabled');
                        this.months.removeAttr('disabled','disabled');
                        break;
                    case 'm':
                        iRBindex = 1;
                        this.months[0].value = asValue[1];
                        this.years[0].value = asValue[2];
                        this.days.removeAttr('disabled','disabled');
                        this.months.removeAttr('disabled','disabled');
                        break;
                    case 'y':
                        iRBindex = 2;
                        this.months.removeAttr('disabled','disabled');
                        this.years.val(asValue[2]);
                        break;
                }
            }

        }

        // Copy settings and data from given date object
        this.copyFrom = function(a_dateSource) {
            this.setValue(a_dateSource.getValue(), a_dateSource.sType, a_dateSource.sSeparator);
        }

        function onDayMonthYearChanged(event) {
            var iMonth = parseInt(event.data.dateInst.months[0].value) - 1;
            var iYear = parseInt(event.data.dateInst.years[0].value);
            
            
            if (event.data.dateInst.years[0].selectedIndex == 0 || isNaN(iYear))
            {
                event.data.dateInst.months[0].selectedIndex = 0;
                event.data.dateInst.months.attr('disabled','disabled');
                event.data.dateInst.days[0].selectedIndex = 0;
                event.data.dateInst.days.attr('disabled','disabled');
                event.data.dateInst.sType  = "n";
            }
            else if (event.data.dateInst.months[0].selectedIndex == 0)
            {
                event.data.dateInst.months.removeAttr('disabled','disabled');
                event.data.dateInst.days[0].selectedIndex = 0;
                event.data.dateInst.days.attr('disabled','disabled');
                event.data.dateInst.sType = "y";
            }
            else if (event.data.dateInst.days[0].selectedIndex == 0)
            {
                event.data.dateInst.days.removeAttr('disabled','disabled');
                event.data.dateInst.sType = "m";
            }
            else 
            {
                event.data.dateInst.sType = "d";
            }
            
            var iDaysInMonth = 32 - new Date(iYear, iMonth, 32).getDate();

                switch (iDaysInMonth) {
                    case 29:
                        addDay(29);
                        break;
                    case 30:
                        addDay(29);
                        addDay(30);
                        break;
                    case 31:
                        addDay(29);
                        addDay(30);
                        addDay(31);
                        break;
                };

                switch (iDaysInMonth) {
                    case 28:
                        removeDay(29);
                    case 29:
                        removeDay(30);
                    case 30:
                        removeDay(31);
                        break;          
                };
                
            function addDay(iDay) {
                if (event.data.dateInst.days.find("option[value=" + iDay + "]")[0] == null) {
                    event.data.dateInst.days.append(" <option value=\"" + iDay + "\">" + iDay + "</option>")
                }
            };
            function removeDay(iDay) {
                event.data.dateInst.days.find("option[value=" + iDay + "]").remove();
            };
        };
        

    };
}

