﻿//This file has a dependency on jQuery.js!
function DatePicker(dateField, minusDays, plusDays, buttonText)
{
    ///<summary>DatePicker creates a small calendar next to the right of the date field.  It's designed to work with the three text box date format (i.e. one for dd, one for mm, one for yyyy).
    ///         The position of the icon would benefit from including the following styles in your Common.css:
    ///             .ui-datepicker-trigger
    ///             {
	///                 position: absolute;
	///                 margin-top: 2px;
	///                 cursor: hand;
    ///             }
    ///</summary>
	///<param name="dateField">This is suffix for the date fields.  The assumption is that you have 3 text boxes on your page e.g. txtMyDate_Day, txtMyDate_Month and txtMyDate_year.
	///                        The _Day, _Month and _Year formats are mandatory.
	///</param>
	///<param name="minusDays">This is the number of days in the past that you would like the user to be able to select from.</param>
	///<param name="plusDays">This is the number of days in the future that you would like the user to be able to select from.</param>
	
	// Local variable representing this object that can be used where this is used in the context of events or setTimeouts.
	var me = this;
	this.changeCallBack = null;
	
	this.DateField = dateField;
	this.MinusDays = minusDays;
	this.PlusDays = plusDays;
	this.ButtonText = buttonText;

	if (buttonText && typeof buttonText == "function") {
	    this.changeCallback = buttonText;
	    this.ButtonText = null;
	}

	this.DayField = null;
	this.MonthField = null;
	this.YearField = null;
	this.TargetField = null;
	
	// Take the date returned by the calendar and split it between the date fields.
    this.DeconstructDate = function(dateText)
    {
        if (dateText.length > 4)
        {   
            this.DayField.value = dateText.substring(0, 2);
            this.MonthField.value = dateText.substring(3, 5);
            this.YearField.value = dateText.substring(6, 10);
        }
        if (typeof this.changeCallback == "function") { // Focus Issue 5 and 23 - Aliya Aziz 29/09/11
            this.changeCallback();
        }
    }

    this.Initialise = function() {
        ///<summary>The constructor for this class.</summary>

        this.DayField = $get(this.DateField + "_Day");
        this.MonthField = $get(this.DateField + "_Month");
        this.YearField = $get(this.DateField + "_Year");
        this.TargetField = $("#" + this.YearField.id);

        if (!this.ButtonText)
            this.ButtonText = "Select a date from the calendar";

        this.TargetField.datepicker
        ({

            firstDay: 1,
            showOn: 'button',
            buttonText: this.ButtonText,
            buttonImage: '/aspnet_client/system_web/2_0_50727/Themes/ENDSLEIGH/Images/btn_Calendar.gif',
            buttonImageOnly: true,
            onSelect: function(dateText, inst) {
                me.DeconstructDate(dateText.substring(0, 10));
            },
            onClose: function(dateText, inst) {
                me.DeconstructDate(dateText.substring(0, 10));
            },
            minDate: '-' + this.MinusDays + 'D',
            maxDate: '+' + this.PlusDays + 'D',
            showAnim: false,
            duration: 0,
            dateFormat: 'dd/mm/yyyy',
            beforeShow: function(input, inst) {
                var day = me.DayField.value;
                var month = me.MonthField.value;
                var year = me.YearField.value;
                var currentDate = new Date(year, parseInt(month, 10) - 1, year);
                if (!isNaN(currentDate)) {
                    var timeoutFunction = "$(\"#" + me.YearField.id + "\").datepicker('setDate', new Date(" + year + ", parseInt(" + month + ") - 1, " + day + ")); $get('" + me.YearField.id + "').value = " + year + ";";
                    setTimeout(timeoutFunction, 1);
                }
            }
        });
    }

	this.Initialised = this.Initialise();
}
