
/*
ASSUMPTIONS

there are 2 drop downs one of day and one for month.
The month drop down must have a year formated into the string

if the dep month is changed then dep day is set to 1 and then the ret_mm/ret_dd is set to the date + ReturnDiff
if the dep_day is changed then the return mm/ret_dd is set to the date + ReturnDiff


NOTE: must have the include file NewDateValidity_Format.js included before this one

*/

        var DPT_Date;
    		
		    var RET_Date;
		   var depMatchFound=false;
   		var arvMatchFound=false;
		  var Today_Date = new Date();
	   	var cal = new CalendarPopup(); 
	    var y= Today_Date.getFullYear();
	    var selDepYear= Today_Date.getFullYear();
	    var selRetYear=Today_Date.getFullYear();
	    var departMonth= Today_Date.getMonth();
	    

	    var calDepartValue;
	    var calReturnValue;
	    var DayOfWeekArray;
	    var departDateValue; //making this global so that selected month is available through out
	    var returnDateValue;
        var depSelYear;
        var retSelYear;
        var fromDayDD = false;
  		  //var Today_Date;
				var departMonth;
				var departYear;      
        var depselMonth;
        var retselMonth;
        var IndexDep_Day;
        var IndexArv_Day;
        var currentDD;

// some customers want to start say 7 days from the current dat


			var CurrentDate = new Date();
	
	// these are defined in NewDateValidity_format.js		
//var StartDate=0;  // number of days from current date to start calendar
//var ReturnDiff = 1; // number of days from the depart date to start return


        CurrentDate.setDate(CurrentDate.getDate() + StartDate);
        var firstmonth =  CurrentDate.getMonth();
        var currentDD = CurrentDate.getDate();
        var currentYear = + CurrentDate.getFullYear();




var mydate= new Date()
var yesterdayDate = new Date();
//mydate.setDate(1);
mydate.setDate(mydate.getDate()-1)
var theyear=mydate.getFullYear()
var themonth=mydate.getMonth()+1
var theyesterday=mydate.getDate()
var yesterdayDate = new Date();

yesterdayDate.setDate(yesterdayDate.getDate()-1)


					// problem with getting current dd
	    for(i=0; i<NumberofDisplayMonths; i++){
           Today_Date.setMonth(departMonth); 
           Today_Date.setDate(1);
           departMonth =  Today_Date.getMonth();
           departYear = "" + Today_Date.getFullYear()
           // Make a list of the number of days in the month
           NumberofDaysArrays[i]=daysInMonth(departMonth+1,Today_Date.getFullYear());           
           DatesArrays[i]=FormatMonth(departMonth,departYear);
           departMonth=departMonth+1;        	    
	    }
	    

	    	var Last_Date = new Date();
	      Last_Date.setMonth(Today_Date.getMonth());
        Last_Date.setYear(Today_Date.getFullYear());
	      Last_Date.setDate(Today_Date.getDate()+(NumberofDaysArrays[11]-1));
	      




	    // ignore until later
      		cal.setReturnFunction("setDepartValues"); 		
  	    	var calReturn = new CalendarPopup(); 
          calReturn.setReturnFunction("setReturnValues"); 
          

					cal.addDisabledDates(null,formatDate(yesterdayDate,"yyyy-MM-dd")); 
					cal.addDisabledDates(formatDate(Last_Date,"yyyy-MM-dd"),null); 
					calReturn.addDisabledDates(null,formatDate(yesterdayDate,"yyyy-MM-dd")); 
					calReturn.addDisabledDates(formatDate(Last_Date,"yyyy-MM-dd"),null); 



	        
function Startup()
{

// determine what month to start the count on

  var startmonth = FormatMonth(CurrentDate.getMonth(),"" + CurrentDate.getFullYear()); 
  var start = 0;
  

   for(k=0; k<NumberofDisplayMonths; k++)
   {       
   	  if (startmonth == DatesArrays[k])
   	    start = k;
	    document.forms[0].dpt_mm.options[k] = new Option(DatesArrays[k],DatesArrays[k]);
	    document.forms[0].ret_mm.options[k] = new Option(DatesArrays[k],DatesArrays[k]);	    
   }
   
	 //remove the items from the drop down before filling it
  while (document.forms[0].dpt_dd.length)
    {
	    document.forms[0].dpt_dd.remove(0);
     }

			
// FIRST MONTH IS ALWAYS THE CURRENT MONTH


      
		  PopulateDepDays(start,currentDD-1,currentYear);
	  
			
			// the month for startup is always the first month in the dropdwon
			CalculateNextDay();
			
			
			// set up the calendar			
			// mm-dd-yyy
			SetCalendarDates();
			
			
      
	}	        
	
// populate the days of the depart month
function PopulateDepDays(month,day,year)
{
var tempDate;
var tempDay;
var tempMonth;


    //remove the items from the drop down before filling it
    
    while (document.forms[0].dpt_dd.length)
      {
	    document.forms[0].dpt_dd.remove(0);
      }


// do you have to set todays date to get this properly

	tempMonth = changeMonthNametoString(ReturnMonth(document.forms[0].dpt_mm.value));

      DayOfWeekArray = new Array(NumberofDaysArrays[month]);
        for (i=0; i<NumberofDaysArrays[month]; i++)
        {
            tempDate = new Date(year,tempMonth,i+1);
            tempDay = i+1;
            DayOfWeekArray[i] = FormatDay(tempDate.getDay(),tempDay)
          }


   for (i=0; i<DayOfWeekArray.length; i++)
        {
	    document.forms[0].dpt_dd.options[i] = new Option(DayOfWeekArray[i],DayOfWeekArray[i]);
        }

  	document.forms[0].dpt_dd.selectedIndex = day;
	
}
	
function CalculateNextDay()
{
	
	
	
	var nextday;
	var year
	var month;
	
	month = document.forms[0].dpt_mm.selectedIndex;
	day = document.forms[0].dpt_dd.selectedIndex;
	
	// get the year and month from the drop down
  
  	year =  ReturnYear(document.forms[0].dpt_mm.options[month].value); // 20xx

		
	//simple case
  
  // ReturnDiff is defined in NewDateValidity_format.js
   nextday = day + ReturnDiff
  //nextday = day +1;



  // simplest form next day is in the same month
	if (nextday <= NumberofDaysArrays[month]-1)
	{
   		document.forms[0].ret_mm.selectedIndex =document.forms[0].dpt_mm.selectedIndex;
	  	PopulateReturnDay(document.forms[0].ret_mm.selectedIndex,nextday,year)
	}
	else 
		{			


				nextmonth = month+1;
				
				// if at the end of the array, then populate same day
				if (nextmonth >= document.forms[0].dpt_mm.length)
				{
					nextmonth = month;
					//if (nextday >= (NumberofDaysArrays[month]-1))
					nextday = NumberofDaysArrays[month]-1;
				}
				//now what happens if the nex moth does not have as many days
				else // if (nextday >= NumberofDaysArrays[nextmonth])
				{
					
					nextday = nextday - NumberofDaysArrays[month];
//				  alert('next date is = ' + nextday);
				}

        document.forms[0].ret_mm.selectedIndex = nextmonth;			
				year = ReturnYear(document.forms[0].ret_mm.value);
				
				PopulateReturnDay(nextmonth,nextday,year);
				
			  
	}
	

			  
	
}
	
	
	
function PopulateReturnDay(month,day,year)
{
	var i;
	var tempDate;
	var tempDay;
	
	
	
	    //remove the items from the drop down before filling it
    while (document.forms[0].ret_dd.length)
      {
	    document.forms[0].ret_dd.remove(0);
      }

	
			tempMonth = changeMonthNametoString(ReturnMonth(document.forms[0].ret_mm.value));


	
      DayOfWeekArray = new Array(NumberofDaysArrays[month]);
        for (i=0; i<NumberofDaysArrays[month]; i++)
        {
            tempDate = new Date(year,tempMonth,i+1);
            tempDay = i+1;
            DayOfWeekArray[i] = FormatDay(tempDate.getDay(),tempDay)
        }


   for (i=0; i<DayOfWeekArray.length; i++)
        {
	    		document.forms[0].ret_dd.options[i] = new Option(DayOfWeekArray[i],DayOfWeekArray[i]);
        }


			document.forms[0].ret_dd.selectedIndex = day;	
	
	}


function changeDepart(d)
{
	
	
	
	if (d=='m')
	{
		var year;
		var month;
		year = ReturnYear(document.forms[0].dpt_mm.value);
			
		// need to set up the current day here		
		  PopulateDepDays(document.forms[0].dpt_mm.selectedIndex,0,year);
			CalculateNextDay();
	}
	else
		{
		
		
   	CalculateNextDay();
		}

		SetCalendarDates();	
}

function changeReturn(d)
{
	
	
	
	if (d=='m')
	{
		var year;
		var month
		
				// need to set up the current day here		
			year = ReturnYear(document.forms[0].ret_mm.value);

			PopulateReturnDay(document.forms[0].ret_mm.selectedIndex,0,year)
	}
	
	SetCalendarDates();
}





// calculate the days in the month
function daysInMonth(month,year) {
	
	
var m = [31,28,31,30,31,30,31,31,30,31,30,31];
if (month != 2) return m[month - 1];
if (year%4 != 0) return m[1];
if (year%100 == 0 && year%400 != 0) return m[1];
return m[1] + 1;
} 


// set the dates for the calendars
function SetCalendarDates()
{
	
	
	var departmonth = changeMonthNametoString(ReturnMonth(document.forms[0].dpt_mm.value));	
	var departyear = ReturnYear(document.forms[0].dpt_mm.value); // 20xx
	var returnmonth = changeMonthNametoString(ReturnMonth(document.forms[0].ret_mm.value));
  var returnyear = ReturnYear(document.forms[0].ret_mm.value); // 20xx	

	
			
			document.forms[0].calSelDepartDate.value=(parseInt(departmonth)+ parseInt('1')) +  "-" + (document.forms[0].dpt_dd.selectedIndex + parseInt('1')) +  "-" + departyear;
			document.forms[0].calSelReturnDate.value=(parseInt(returnmonth)+ parseInt('1')) +  "-" + (document.forms[0].ret_dd.selectedIndex + parseInt('1')) +  "-" + returnyear;
			
			
			// these are set up so the functions.js code can use
			
    //document.forms[0].hidFormattedDepDate.value=
		//document.forms[0].hidFormattedArrDate.value=
		document.forms[0].SelDepDate.value= document.forms[0].dpt_dd.value + ' ' + changeMonthNumbertoString(departmonth)+ ' ' + departyear;//Tue 13 Nov 2007
		document.forms[0].SelArvDate.value= document.forms[0].ret_dd.value + ' ' + changeMonthNumbertoString(returnmonth)+ ' ' + returnyear;//Tue 13 Nov 2007
		document.forms[0].hidDpt_yyyy.value= departyear;//yyyy
		document.forms[0].hidRet_yyyy.value=returnyear;//yyyy
		document.forms[0].hidDpt_Month.value= changeMonthNumbertoString(departmonth);///nov
		document.forms[0].hidDpt_MonthIndex.value= parseInt(departmonth)+ parseInt('1');// actual month nov = 11
		if (document.forms[0].hidDpt_MonthIndex.value < 10)
				document.forms[0].hidDpt_MonthIndex.value = '0' + document.forms[0].hidDpt_MonthIndex.value;
		document.forms[0].hidDpt_DayIndex.value=document.forms[0].dpt_dd.selectedIndex + 1;// 12 day
		if (document.forms[0].hidDpt_DayIndex.value < 10)
			document.forms[0].hidDpt_DayIndex.value = '0' + document.forms[0].hidDpt_DayIndex.value;
			
		document.forms[0].hidRet_Month.value= changeMonthNumbertoString(returnmonth);///nov
		if (document.forms[0].hidRet_Month.value < 10)
		 		document.forms[0].hidRet_Month.value= '0' + document.forms[0].hidRet_Month.value;
		document.forms[0].hidRet_MonthIndex.value=(parseInt(returnmonth)+ parseInt('1'));/// actual month nov = 11
		document.forms[0].hidRet_DayIndex.value= document.forms[0].ret_dd.selectedIndex + 1;// 13 day

		if (document.forms[0].hidRet_DayIndex.value < 10)
		{
			
			document.forms[0].hidRet_DayIndex.value = '0' + document.forms[0].hidRet_DayIndex.value;

		}
			// These were left over values used directly by the functions.js - removed the dependence
		//	departDateValue =parseInt(departmonth)+ parseInt('1');// actual month nov = 11
		//	returnDateValue =(parseInt(returnmonth)+ parseInt('1'));/// actual month nov = 11
			
}


// need to support these functions for the function.js
function GetDepSelDate()
{
	var tmp
	var departmonth = changeMonthNametoString(ReturnMonth(document.forms[0].dpt_mm.value));	
	var departyear = ReturnYear(document.forms[0].dpt_mm.value); // 20xx
	//var returnmonth = changeMonthNametoString(ReturnMonth(document.forms[0].ret_mm.value));
  //var returnyear = ReturnYear(document.forms[0].ret_mm.value); // 20xx	
	
	tmp =document.forms[0].dpt_dd.value + ' ' + changeMonthNumbertoString(departmonth)+ ' ' + departyear;//Tue 13 Nov 2007
	
	selDepYear = departyear;
		return tmp;
	
	}

// need to support these functions for the function.js
function GetArvSelDate()
{
	var tmp
	//var departmonth = changeMonthNametoString(ReturnMonth(document.forms[0].dpt_mm.value));	
	//var departyear = ReturnYear(document.forms[0].dpt_mm.value); // 20xx
	var returnmonth = changeMonthNametoString(ReturnMonth(document.forms[0].ret_mm.value));
  var returnyear = ReturnYear(document.forms[0].ret_mm.value); // 20xx	
	
	tmp =document.forms[0].ret_dd.value + ' ' + changeMonthNumbertoString(returnmonth)+ ' ' + returnyear;//Tue 13 Nov 2007
	
	 selRetYear=returnyear;
	return tmp;
	
	}

function GetDepSelDatemmddyyyy()
{
	var depDate;
 			depDate = document.forms[0].calSelDepartDate.value
			

 
 return depDate;
	
	}

// need to support these functions for the function.js
function GetArvSelDatemmddyyyy()
{

	var retDate;
 			retDate = document.forms[0].calSelReturnDate.value
			

 
 return retDate;
	
	}

// return dated from the calendar

function setDepartValues(y,m,d) 
{ 
y = "" + y;	

var date1;
var date2;
var result;




date1= new Date(y,m-1,d);  // current day
date2= yesterdayDate;



if(date1>yesterdayDate)
  ;
else
	return;

if(date1>Last_Date)
  return;

	var SetTodayDepartMonth= FormatMonth(m-1,y);


	 for (var i=0; i<document.forms[0].dpt_mm.options.length; i++) { 
	 
		 if (document.forms[0].dpt_mm.options[i].text==SetTodayDepartMonth) { 
				// need to set up the current day here		
				  document.forms[0].dpt_mm.selectedIndex = i;
				  				  
				  PopulateDepDays(i,d-1,y);
					CalculateNextDay();
		  } 
	 }
	
	SetCalendarDates();
} 

// return dated from the calendar
function setReturnValues(y,m,d) 
{ 



var date1;
var date2;
var result;



date1= new Date(y,m-1,d);  // current day

	y = "" + y;
	
if(date1>yesterdayDate)
  ;
else
	return;

if(date1>Last_Date)
  return;
	
	var SetTodayReturnMonth= FormatMonth(m-1,y);
  
    
	 for (var i=0; i<document.forms[0].ret_mm.options.length; i++) { 
	 
		 if (document.forms[0].ret_mm.options[i].text==SetTodayReturnMonth) { 
				
				document.forms[0].ret_mm.selectedIndex = i;
				PopulateReturnDay(i,d-1,y);
		  } 
	 }
	 
	 
	 SetCalendarDates();
} 





