// function that works out the number of days in a
// month given the current year and month
function days_in_month (year, month)
{


	      return 32 - new Date(year, month, 32).getDate();

}




function onMonthYearChange(dayObject,monthObject,yearObject)
{
	onMonthYearChange2(dayObject,monthObject,yearObject);
}
//called by scheduler action page.
function onMonthYearChangeExtra(dayObject,monthObject,yearObject){
	onMonthYearChange2(dayObject,monthObject,yearObject);
}

function onMonthYearChange2(dayObject,monthObject,yearObject)
{

	// get the no of days in the new year/month combination

	var monthSelected = monthObject;
	var yearSelected  = yearObject;
	monthSelected     = monthSelected-1;

	var days = days_in_month (yearSelected , monthSelected);

	var currentDays = dayObject.length;

	if(currentDays == days)
		return;

	if(currentDays < days)
	{

		var iSel = dayObject.selectedIndex;

		var diff = days - currentDays;
		for(var i=0; i<diff; i++)
		{
			dayObject.options[currentDays + i] = new Option(currentDays+i+1, currentDays+i+1);
		}
		dayObject.options[iSel].selected = true;

	}

	if(currentDays > days)
	{
		var iSel = dayObject.selectedIndex;

		dayObject.options.length = 0;

		for (var i=0; i<days; i++)
		{
			dayObject.options[dayObject.options.length] = new Option(i+1, i+1);
		}

		if (iSel > dayObject.options.length - 1)
		{
			iSel = dayObject.options.length - 1
		}

		dayObject.options[iSel].selected = true;
	}

}

function theMonthYearChange(dayObject,monthObject,yearObject)
	{


		var year=yearObject;
		if(monthObject==2)
		{

			year=2004;
		}
		onMonthYearChange(dayObject,monthObject,year)
	}
// added by Kishor K SCR ID:483
function monthChange(dayObject,monthYearObject)
{

	var the_string = monthYearObject;
	if(the_string=="None")
	{
		return;
	}
	var broken_up_string = the_string.split("/");

	var monthObject = broken_up_string[0];
	var yearObject = broken_up_string[1];
	//alert("monthObject="+monthObject);
	//alert("yearObject="+yearObject);
	onMonthYearChange3(dayObject,monthObject,yearObject);
}

// added by kishor k populate the days drop down options
function onMonthYearChange3(dayObject,monthObject,yearObject)
{

	// get the no of days in the new year/month combination

	var monthSelected = monthObject;
	var yearSelected  = yearObject;
	//monthSelected     = monthSelected-1;

	var days = days_in_month (yearSelected , monthSelected);
	//alert("days_in_month"+days);

	var currentDays1 = dayObject.length-1;
	var currentDays = dayObject.length;


	if(currentDays1 == days)
		return;

	if(currentDays1 < days)
	{

		var iSel = dayObject.selectedIndex;

		var diff = days - currentDays1;
		for(var i=0; i<diff; i++)
		{
			dayObject.options[currentDays + i] = new Option(currentDays1+i+1, currentDays1+i+1);
		}
		dayObject.options[iSel].selected = true;

	}

	if(currentDays1 > days)
	{
		var iSel = dayObject.selectedIndex;

		dayObject.options.length = 0;
		dayObject.options[dayObject.options.length] = new Option("", -1);

		for (var i=0; i<days; i++)
		{
			dayObject.options[dayObject.options.length] = new Option(i+1, i+1);
		}

		if (iSel > dayObject.options.length - 1)
		{
			iSel = dayObject.options.length - 1
		}

		dayObject.options[iSel].selected = true;
	}

}


function advanceDepartureDateTo7Days(departureDay,countryId,returnDay,currentYear,currentMonth,currentDay,departureYear,departureMonth,returnMonth,returnYear)
{
	var year =currentYear;
 	var month =currentMonth;
	var day =currentDay;
	var dayAdvancedTo7Days=day+7;
	var daysInCurrentMonth = days_in_month(currentYear,currentMonth);
	var newDate = dayAdvancedTo7Days - daysInCurrentMonth ;
	var newMonth = currentMonth;
	if(dayAdvancedTo7Days > daysInCurrentMonth)
	{
		newMonth = currentMonth+1;
	}
	else
	{
		newDate =dayAdvancedTo7Days;
	}

	var departureDateWithin7Days=false;
	var returnDateWithin7Days=false;
	if(countryId == 10)
	{
		departureDateWithin7Days = isWithin7Days (year, month,day,departureYear.value, departureMonth.options[departureMonth.selectedIndex].value-1,departureDay.options[departureDay.selectedIndex].value,newDate,newMonth);
		returnDateWithin7Days = isWithin7Days (year, month,day,returnYear.value, returnMonth.options[returnMonth.selectedIndex].value-1,returnDay.options[returnDay.selectedIndex].value,newDate,newMonth);
	
		if(departureDateWithin7Days)
		{
			advanceDateTo7Days(departureDay,departureMonth,dayAdvancedTo7Days,daysInCurrentMonth,newMonth,newDate);
		}
		if(returnDateWithin7Days)
		{
			advanceDateTo7Days(returnDay,returnMonth,dayAdvancedTo7Days,daysInCurrentMonth,newMonth,newDate);
		}
	}
}


function isWithin7Days (currentYear, currentMonth,currentDay,tripYear, tripMonth,tripDay,newDate,newMonth)
{
		var currentDate=new Date(currentYear, currentMonth,currentDay).getDate();
		var tripDate=new Date(tripYear, tripMonth,tripDay).getDate();
		//var dayAdvancedTo7Days=tripDay+7;
		//var daysInCurrentMonth = days_in_month(tripYear,tripMonth);
		//var newTripDate = dayAdvancedTo7Days - daysInCurrentMonth ;
		
		if(tripYear >= currentYear)
		{
			if(tripMonth > newMonth)
			{

				return false;

			}
			else if(tripMonth == newMonth)
			{
				//alert("tripDate ="+tripDate +"newDate="+newDate);
				if(tripDate > newDate)
				{
					return false;
				}
			}
		}

		return true;

}

function advanceDateTo7Days(day,month,dayAdvancedTo7Days,daysInCurrentMonth,newMonth,newDate)
{
	var newDate = 0;
	//alert("dayAdvancedTo7Days ="+dayAdvancedTo7Days +"daysInCurrentMonth="+daysInCurrentMonth);
	if(dayAdvancedTo7Days > daysInCurrentMonth)
	{
		newDate = dayAdvancedTo7Days - daysInCurrentMonth -1;

	}
	else
	{
		newDate = dayAdvancedTo7Days -1;
		
	}
	//alert("newDate ="+newDate );
	var tripMonth=month.options[month.selectedIndex].value-1;
	if(tripMonth < newMonth)
	{
		month = month.options[month.selectedIndex+1].selected =true;
	
		day.options[newDate].selected   = true;

	}
	else
	{
		day.options[newDate].selected   = true;

	}
}

/**
	Populates the given daySelect with the given number of days. Includes a blank option or not depending on the 
	blankOption parameter.
 **/
function populateDaySelect(daySelect, nrDays, blankOption, selectedValue){
	daySelect.length = 0;
	if (blankOption){
		daySelect.options[daySelect.length] = new Option('', '');
	}
	for (i = 1; i <= nrDays; i++){
		daySelect.options[daySelect.length] = new Option(i, i);
	}
	if ((selectedValue != '') && (selectedValue > nrDays)){
		selectedValue = nrDays;
	}
	daySelect.value = selectedValue;
}

