// Range_Date3 FOR Search Date in Range and select date search
function setSelectedYear(formobj,begin,end,num)
{	
	//get the current date object
	curDay = new Date();
	//get the current year 
	curYear = parseInt(curDay.getFullYear())+543;
	//this variable will be used to indexed the selection list order
	var curLastIdx = 1;

	//loop to add the year option to the year selection list 
	//the value will be from last 7 year to current year
	for (var cnt = (curYear + begin);cnt <= (curYear + end);cnt++)
	{
		//this case will be done in the last time of loop (add current year)
		//if (cnt == curYear)
		//{
			//these 2 lines below will initialize new option object 
			//which is will be "default selected" and "selected" option 
		//	var tmpOption1 = new Option(cnt.toString(),cnt.toString());
			//add the current year to selection list 
		//	formobj.YEAR.options[curLastIdx] = tmpOption1;
		//}
		//if year which will be added is not current year the enter
		//this case
		//else 
		//{
			//these 2 lines below will initialize new option object 
			var tmpOption1 = new Option(cnt.toString(),cnt.toString());
			//add the current year to selection list 
			formobj.YEAR.options[curLastIdx] = tmpOption1;
		//}
		curLastIdx++;
	}
}
function setSelectedDate(formobj,num)
{
	//get the current date object
	curDay = new Date();
	//get the current date month and year
	curDate = curDay.getDate();
	curMonth = curDay.getMonth();
	curYear = parseInt(curDay.getFullYear())+543;

	//set the date and month to the current date and month
	//formobj.DAY[num].options[curDate].selected = true;  /// array 0 = --,start 1
	//formobj.MONTH.options[curMonth+1].selected = true; /// array 0 = --,start 1
	for (cnt = 0;cnt < formobj.DAY.length;cnt++)
		if (formobj.DAY.options[cnt].value == formobj.H_DAY.value)
			formobj.DAY.options[cnt].selected = true;
	for (cnt = 0;cnt < formobj.MONTH.length;cnt++)
		if (formobj.MONTH.options[cnt].value == formobj.H_MONTH.value){
			formobj.MONTH.options[cnt].selected = true;
	}

	//set the year to the current year
	for (cnt = 0;cnt < formobj.YEAR.length;cnt++)
	//	if (parseInt(formobj.YEAR.options[cnt].value) == curYear)
		if (formobj.YEAR.options[cnt].value == formobj.H_YEAR.value)
			formobj.YEAR.options[cnt].selected = true;
}

function monthChange(formobj,num)
{	
	//these two array hold the number of date in each year type (DO and LDO)
	DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

	yearSelect = formobj.YEAR;
	monthSelect = formobj.MONTH;
	//get the year type by pass the specified year to function getYearType
	yearType = getYearType(parseInt(yearSelect.options[yearSelect.selectedIndex].value));
	//set the monthArray to match the type of year
	if (yearType == "DO")
		monthArray = DOMonth;
	else 
		monthArray = lDOMonth;

	//call function updateDateSelect to up date the value in selection list
	updateDateSelect(formobj,monthArray[monthSelect.selectedIndex-1],num); /// month lst in Array

}

function getYearType(year)
{
	/*This function will get the type of 
	the spicified year
	*/
	year = year-543;
	if ((year % 4) == 0) 
	{
		if ((year % 100) == 0 && (year % 400) != 0)
			return "DO";

		return "lDO";
	} 
	else
		return "DO";
}

function updateDateSelect(formobj,newMaxDate,num)
{
	dateSelect = formobj.DAY;

	//get the current maximum date value of date selection value
	oldMaxDate = parseInt(dateSelect.options[dateSelect.length - 1].value);
	//get the current selected date
	dateSelectedValue = parseInt(dateSelect.options[dateSelect.selectedIndex].value);

	/*Compare the current maximum date value with the new maximum date value
	- if they are equal then return
	- if the current maximum date is less than new maximum 
	date then add some date into the date selection list
	- if the current maximum date is greater than new maximum 
	date then delete some date into the date selection list*/

	if (oldMaxDate == newMaxDate) 
		//return if their values are equal
		return;
	else if(oldMaxDate < newMaxDate)
	{
		//add some date if the current maximum date is less than the new maximum date
		for(cnt = oldMaxDate + 1;cnt <= newMaxDate;cnt++)
		{
			tmpOption = new Option(cnt.toString(),cnt.toString());
			dateSelect.options[dateSelect.length] = tmpOption;
		}
	}
	else if (oldMaxDate > newMaxDate)
	{
		for(cnt = oldMaxDate;cnt > newMaxDate;cnt--)
		{
			//delete some date if the current maximum date is greater than the new maximum date
			dateSelect.options[dateSelect.length - 1] = null;
		}

		//if the current selected date value is greater than the new maximum value 
		//then select the new maximum value as the selected value
		if (dateSelectedValue > newMaxDate)
		{
			dateSelect.options[dateSelect.length - 1].selected = true;
		}
	}

}
