/*
	Modification Log:
		081023 - [DA] - Amended setUpMonthYears method since incorrect variable
						was being used when more than one year. Copy and Paste fucking coding!!!

		081127 - [DA] - Amended observer check to include null check, this is due to this class being used for other
					  - purposes which may not require an observer

*/
var TravelDate=Class.create(
{
    initialize: function(cmbMonthYear,cmbDateDay,objDateRef,dtStart,dtEnd)
    {
        this.valueSeperator="-";
        this.cmbDateDay=cmbDateDay;
        this.cmbMonthYear=cmbMonthYear;
        this.objDateRef=objDateRef;
        this.dtStartDate=dtStart;
        this.dtEndDate=dtEnd;
        this.setUpMonthYears();
        this.setUpDayDates();
        this.observer=null;
    }, //end initialise
    setObserver: function(observer)
    {
        this.observer=observer;
    },
    disable: function()
    {
        Form.Element.disable($(this.cmbDateDay));
        Form.Element.disable($(this.cmbMonthYear));
    },
    enable: function()
    {
        Form.Element.enable($(this.cmbDateDay));
        Form.Element.enable($(this.cmbMonthYear));
    },
    clearMonthYear: function()
    {
    	clearSelectButTitle(this.cmbMonthYear); //Clears it out except the top
    },
    setUpDayDates: function()
    {
        clearSelectButTitle(this.cmbDateDay); //Clears it out except the top
        if (this.cmbMonthYear.selectedIndex<1)
        {
            this.cmbDateDay.selectedIndex=0;
        }
        else
        {
            var optionText="";
            var optionValue="";
            //Split the option value by the classe's seperator
            var optValueParts=this.cmbMonthYear.value.split(this.valueSeperator);

            var year = optValueParts[0];
            var month = optValueParts[1]-1; //-1 since 0 indexed

            var startOfMonth = new Date(year, month, 1);
            var totalDays = this.objDateRef.getDaysInMonth(year, month);

            if (this.dtStartDate.getMonth()==month)
            {
                startOfMonth=new Date(year,month,this.dtStartDate.getDate()); //set the date to start from the start date
            }
            //check the same for end date
            var dayOfWeek=startOfMonth.getDay();

            var index=1;
            var day=startOfMonth.getDate();
            for (day; day <= totalDays; day++)
            {
                optionText=(day<10) ? '0'+day : day; //just pad the number so it looks neater :)
                optionText+=' '+ this.objDateRef.getDayName(dayOfWeek);
                optionValue=(day<10) ? '0'+day : day;
        		AddToCombo(this.cmbDateDay,index++,new Option(optionText,optionValue));
                dayOfWeek = (dayOfWeek < 6) ? dayOfWeek + 1 : 0; //if its a sat(6) set it back to sun(0)
            }
            this.cmbDateDay.selectedIndex=0; //select the top item
        }
        //if (this.observer!=null)
       // {
       this.checkAndSetUpRequest(); //null check is being done in the method itself now, see code note
        //}
    }, //end setUpDayDates
    setUpMonthYears: function()
    {
        var startYear=this.dtStartDate.getFullYear();
        var endYear=this.dtEndDate.getFullYear();

        var optionText="";
        var optionValue="";
        var startMonth=this.dtStartDate.getMonth();
        var endMonth=this.dtEndDate.getMonth();

        var loopEnd=endMonth+1;
        var needsSecondYear=false;

        if (endYear!=startYear)
        {
            loopEnd=12; //i.e. december
            needsSecondYear=true;
        }
        var index=1;
        for (i=startMonth;i<loopEnd;i++)
        {
            optionText=""
            optionText +=this.objDateRef.getMonthName(i);
            optionText +=' ' + startYear;
            optionValue=startYear + this.valueSeperator + ((i+1)<10 ? '0' + (i+1):(i+1)); //i+1 is the month so no farting around later :), pad it too
            AddToCombo(this.cmbMonthYear,index++,new Option(optionText,optionValue));
        }
        if (needsSecondYear)
        {
            loopEnd=endMonth+1;
            for (i=0;i<loopEnd;i++)
            {
                optionText=""
                optionText +=this.objDateRef.getMonthName(i);
                optionText +=' ' + endYear;
                //081023 - [DA] - Amended line below since it was startYear and not endYear, this was a bug!!
                optionValue=endYear + this.valueSeperator + ((i+1)<10 ? '0' + (i+1):(i+1)); //i+1 is the month so no farting around later :), pad it too
                AddToCombo(this.cmbMonthYear,index++,new Option(optionText,optionValue));
            }
        }

    }, //end setUpMonthYears
    checkAndSetUpRequest: function()
    {
    	//081127 - [DA] - Null check handling on observer, since we are now using this class for other things
    	if (this.observer!=null)
        {
       		this.observer.setObservableEvent(this);
       	}
    },
    hasDates: function()
    {
        return (this.cmbDateDay.selectedIndex>0 && this.cmbMonthYear.selectedIndex>0);
    },
    resetCombos: function()
    {
        this.cmbDateDay.selectedIndex=0;
        this.cmbMonthYear.selectedIndex=0;
    },
    getTripDate: function()
    {
        return this.cmbMonthYear.value + '-' + this.cmbDateDay.value;
    },
    getTripDateAsDate: function()
    {
    	var dateParts=this.getTripDate().split('-');
    	return new Date(dateParts[0],dateParts[1]-1,dateParts[2]);
    },
    resetAndDisable: function()
    {
    	this.resetCombos();
    	this.disable();
    }
}); //end travel date class


var	FromToDateController=Class.create(
{
	initialize:function(FromDateObj,ToDateObj)
	{
		this.FromDate=FromDateObj;
		this.ToDate=ToDateObj;
		this.FromDate.setObserver(this);
		this.ToDate.setObserver(this);
		this.ToDate.resetAndDisable();
		this.DependantControl=null;
		this.ResponseDiv=null;
		this.url=location.href;
	},//end initialize
	setObservableEvent:	function(sender,args)
	{
		if (sender===this.FromDate.cmbMonthYear)
		{
			this.FromDate.setUpDayDates();
			this.disableControls();
		}
		else if(sender===this.FromDate.cmbDateDay)
		{
			this.ToDate.enable();
			 //sets the start date of To control to be the current date selected
			this.ToDate.dtStartDate=this.FromDate.getTripDateAsDate(); //all 3 below shld be combined in 1 method
			this.ToDate.clearMonthYear();
			this.ToDate.setUpMonthYears();
		}
		else if(sender===this.ToDate.cmbMonthYear)
		{
			this.ToDate.setUpDayDates();
			Form.Element.disable($(this.DependantControl)); //disable button etc
		}
		else if(sender===this.ToDate.cmbDateDay)
		{
			Form.Element.enable($(this.DependantControl));
		}
		else if(sender===this.DependantControl)
		{
	//		if (this.canRequest())
			return (this.canRequest());
//			this.setRequest();
		}
	},
	setDependantControl: function(ctl)
	{
		this.DependantControl=ctl;
		//$(this.DependantControl).addClassName('disable'); //$ is needed due to ie 6 bug - in theory not needed
		Form.Element.disable($(this.DependantControl));
	},
	setResponseDiv: function(div)
	{
		this.ResponseDiv=div;
	},
	disableControls: function(ctl)
	{
		this.ToDate.resetAndDisable();
		Form.Element.disable($(this.DependantControl));
	},
	canRequest: function()
	{
		return (this.FromDate.hasDates() && this.ToDate.hasDates());
	}
	/*,setRequest: function()
	{
		try
		{
			if (this.canRequest())
			{
				new Ajax.Updater(this.ResponseDiv, this.url,
				{
					method: 'post',
					evalScripts: true,
					parameters:
					{
						ajaxLoad:	REQUESTTYPE['R_VOYOAGETRIP'],
						fromDate: 	this.FromDate.getTripDate(),
						toDate: 	this.ToDate.getTripDate()
					}
				});
			}
		}
		catch (err)
		{
			alert(err); //should not really do this...for debugging only but...
		}
	},//end setRequest
	*/
}); //end FromToDateController

function FromToDateSubmit(event)
{

}