
/*these functions are for filling the values of day drop down list according to the month selected*/
/*final changed on 05/06/02006*/
var currentIndex;
var dayName = "day";
function CheckDateValue(sId, synch)
{
	//alert(sId);
	//alert('inside check date value');
	//debugger
	//var c=sId + "_ddlMonth";
	//alert(c);
	var thisMonth = document.getElementById(sId +"_ddlMonth");
	var thisDay = document.getElementById(sId + "_ddlDay");
	//alert('inside check date value');
	//alert(thisMonth);
	//alert(thisDay);
	if(thisDay.selectedIndex == 0 && thisMonth.selectedIndex != 0)
	{
		var curMonth =  dtCurrMonth - 1;
		
		var year =  dtCurrYear;	

		if( curMonth > (thisMonth.selectedIndex -1) )  
		{
			year += 1;
		}	
			
		var syncDay = new Date( year, thisMonth.selectedIndex - 1 ,1,0,0,0,0);		
		
		SyncDayByMonth( thisDay,syncDay );
	}
	
	if( thisDay.selectedIndex == 0 || thisMonth.selectedIndex == 0 )
	{
		return true;
	}
	
	var day = new Date(dtCurrYear, thisMonth.selectedIndex - 1 ,thisDay.selectedIndex ,0,0,0,0);
	
	//get today date	
	var today = new Date(dtCurrYear, dtCurrMonth - 1, dtCurrDay, 0, 0, 0, 0);
	
	var isNextYear = false;

	if(day < today)
	{
		day = new Date(dtCurrYear + 1, thisMonth.selectedIndex -1 ,thisDay.selectedIndex,0,0,0,0);
		isNextYear = true;
	}
		
	if(day.getMonth() != thisMonth.selectedIndex -1 )
	{
		if( window.event != null && window.event.srcElement.id.indexOf( "_thisMonth" )!=-1 )
		{
			window.event.srcElement.selectedIndex = thisMonth.selectedIndex;//currentIndex - 1;
		}
		day = new Date( isNextYear ? dtCurrYear + 1 : dtCurrYear , thisMonth.selectedIndex - 1 ,1 ,0,0,0,0);
		
		var numDaysInMonth = get_LeapYear(day);
		day.setDate(numDaysInMonth);
		
		SyncDayByMonth( thisDay,day );
		
		if (typeof(SyncDates) == 'function' && synch)
		{
			//alert('inside the first if sId :: ' + sId + " day :: " + day);
			SyncDates(sId,day);
			
		}
		
		return true;
	}
	//Fill the days ddl by the days in the month
	//window.event = null in case that we changed the date from the calender
	else if((window.event == null) || (window.event != null && window.event.srcElement.id.indexOf( "_ddlDay" )==-1))
	{
		//alert('inside else syncdayby month parameters thisDay :: ' + thisDay + " day :: " + day);
		SyncDayByMonth( thisDay,day );
	}
	
	if (typeof(SyncDates) == 'function' && synch)
	{
		//alert('inside last typeof check sId :: ' + sId + " day :: " + day);
		SyncDates(sId, day);
	}
	
	return true;
}

//this function is not used anywhere
/*function OpenCalendar(sId)
{
	g_Calendar.show(event,null, sId + '_ddlMonth', sId +'_ddlDay',true, 'mm/dd/yyyy', new Date() );
	return false;
}*/

function SyncDayByMonth( ddlDay,day ) 
{
	//debugger
	var IsLeap;
	IsLeap = get_LeapYear(day);
	
	//store the current day - reduce it if the new month does not have enough days
	var storedDate = ( ddlDay.selectedIndex == -1 || ddlDay.selectedIndex > IsLeap ) ? ( IsLeap  ) : ddlDay.selectedIndex;
	
	//empty days box then refill with correct number of days
	while( ddlDay.options.length ) 
	{ 
		ddlDay.options[0] = null; 
	} 
	
	//Dummy item ("day")
	ddlDay.options[0] = new Option( dayName,-1 );
	
	//Fill the days
	for( var x = 1; x <= IsLeap; x++ ) 
	{ 
		ddlDay.options[x] = new Option( x, x ); 
	}
	
	//select the number that was selected before
	ddlDay.options[storedDate].selected = true; 
}
	
function get_LeapYear(day)
{
	var IsLeap = parseInt( day.getFullYear() );
	IsLeap = !( IsLeap % 4 ) && ( ( IsLeap % 100 ) || !( IsLeap % 400 ) );
	//find the number of days in that month
	var currentMonth = day.getMonth();
	IsLeap = [31,(IsLeap?29:28),31,30,31,30,31,31,30,31,30,31][currentMonth];
	return IsLeap;
}
