<!--
/****************************************************/
//		### Date Function ###
//		1.   InitDate(cmbMonth, cmbDay, cmbYear, bThai, dtDefault)
//		2.   InitLongRangeDate(cmbMonth, cmbDay, cmbYear, bThai, dtDefault, below, more)
//		3.   InitShorterLongRangeDate(cmbMonth, cmbDay, cmbYear, bThai, dtDefault, below, more)
//		4.   dateValidation(cmbDay, cmbMonth, cmbYear)
//		5.   DMYFromNow(cmbDay, cmbMonth, cmbYear, dtmComp)
//		6.   DMYToNow(cmbDay, cmbMonth, cmbYear, dtmComp)
//		7.   GetMaxDay(nMonth, nYear)
//		8.   IsMonthYear(szStr)
//		9.	 ConvertCmbToDate(cmbMonth, cmbDay, cmbYear)
//		10.	 subtdate(date1, date2)
/****************************************************/

/*
	Initialize Day, Month and Year comboboxes with Thai or English style and display the current
	day, month and year if necessary.
	Ex 1.. fill-up the day month and year comboboxes with the english style
		- InitDate(document.frm.cmbMonth, document.frm.cmbDay, document.frm.cmbYear, false, null);

	Ex 2.. fill-up the day month and year comboboxes with the thai style and display the current day month and year
		- InitDate(document.frm.cmbMonth, document.frm.cmbDay, document.frm.cmbYear, true, new Date());
*/
function InitDate(cmbMonth, cmbDay, cmbYear, bThai, dtDefault)
{
	var i, j, n;
	var dt = new Date();
	var arrThMonth = new Array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
	var arrEnMonth = new Array("January", "February", "March", "April", "May", "Jun", "July", "August", "September", "October", "November", "December");

	if(cmbMonth != null) {
		while(cmbMonth.options.length)
			cmbMonth.options[0] = null;
		cmbMonth.options[0] = new Option(" -- ", 0);
		for(i=0; i<arrThMonth.length; i++)
			cmbMonth.options[i + 1] = new Option((bThai)? arrThMonth[i]:arrEnMonth[i], i + 1);
		if(dtDefault != null) {
			cmbMonth.selectedIndex = dtDefault.getMonth() + 1;
		}
	}

	if(cmbDay != null) {
		while(cmbDay.options.length)
			cmbDay.options[0] = null;
		cmbDay.options[0] = new Option(" -- ", 0);
		for(i=1; i<=31; i++)
			cmbDay.options[i] = new Option(i, i);
		if(dtDefault != null)
			cmbDay.selectedIndex = dtDefault.getDate();
	}

	if(cmbYear != null) {
		while(cmbYear.options.length)
			cmbYear.options[0] = null;
		cmbYear.options[0] = new Option(" -- ", 0);
		if(dtDefault != null)
			j = dtDefault.getYear() + ((dtDefault.getYear() < 1900)? 1900:0)
		n = dt.getYear() + ((dt.getYear() < 1900)? 1900:0);
		for(i=n-4; i<n + 3; i++) {
			cmbYear.options[cmbYear.options.length] = new Option((bThai)? i + 543:i, i);
			if(dtDefault != null)
				if(i == j)
					cmbYear.selectedIndex = cmbYear.options.length - 1;
		}
	}
}

/*
		just like InitDate, only some slightly changes
*/
function InitLongRangeDate(cmbMonth, cmbDay, cmbYear, bThai, dtDefault, below, more) 
{
	var i, j, n;
	var dt = new Date();
	var arrThMonth = new Array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
	var arrEnMonth = new Array("January", "February", "March", "April", "May", "Jun", "July", "August", "September", "October", "November", "December");

	if(cmbMonth != null) {
		while(cmbMonth.options.length)
			cmbMonth.options[0] = null;
		cmbMonth.options[0] = new Option("ระบุเดือน", 0);
		for(i=0; i<arrThMonth.length; i++)
			cmbMonth.options[i + 1] = new Option((bThai)? arrThMonth[i]:arrEnMonth[i], i + 1);
		if(dtDefault != null) {
			cmbMonth.selectedIndex = dtDefault.getMonth() + 1;
		}
		else {
			cmbMonth.selectedIndex = 0;
		}
	}

	if(cmbDay != null) {
		while(cmbDay.options.length)
			cmbDay.options[0] = null;
		cmbDay.options[0] = new Option("ระบุวันที่", 0);
		for(i=1; i<=31; i++)
			cmbDay.options[i] = new Option(i, i);
		if(dtDefault != null)
			cmbDay.selectedIndex = dtDefault.getDate();
		else {
			cmbDay.selectedIndex = 0;
		}
	}

	if(cmbYear != null) {
		while(cmbYear.options.length)
			cmbYear.options[0] = null;
		cmbYear.options[0] = new Option("ระบุปี", 0);
		if(dtDefault != null)
			j = dtDefault.getYear() + ((dtDefault.getYear() < 1900)? 1900:0)
		n = dt.getYear() + ((dt.getYear() < 1900)? 1900:0);
		for(i=n-below; i<n+more+1; i++) {
			cmbYear.options[cmbYear.options.length] = new Option((bThai)? i + 543:i, i);
			if(dtDefault != null)
				if(i == j)
					cmbYear.selectedIndex = cmbYear.options.length - 1;
		}
		if(dtDefault == null)
			cmbYear.selectedIndex = 0;
	}
}

/*
		just like InitDate, only some slightly changes, special for premium offer and policy service in is
*/
function InitShorterLongRangeDate(cmbMonth, cmbDay, cmbYear, bThai, dtDefault, below, more) 
{
	var i, j, n;
	var dt = new Date();
	var arrThMonth = new Array("มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
	var arrEnMonth = new Array("January", "February", "March", "April", "May", "Jun", "July", "August", "September", "October", "November", "December");

	if(cmbMonth != null) {
		while(cmbMonth.options.length)
			cmbMonth.options[0] = null;
		cmbMonth.options[0] = new Option("เดือน", 0);
		for(i=0; i<arrThMonth.length; i++)
			cmbMonth.options[i + 1] = new Option((bThai)? arrThMonth[i]:arrEnMonth[i], i + 1);
		if(dtDefault != null) {
			cmbMonth.selectedIndex = dtDefault.getMonth() + 1;
		}
		else {
			cmbMonth.selectedIndex = 0;
		}
	}

	if(cmbDay != null) {
		while(cmbDay.options.length)
			cmbDay.options[0] = null;
		cmbDay.options[0] = new Option("วันที่", 0);
		for(i=1; i<=31; i++)
			cmbDay.options[i] = new Option(i, i);
		if(dtDefault != null)
			cmbDay.selectedIndex = dtDefault.getDate();
		else {
			cmbDay.selectedIndex = 0;
		}
	}

	if(cmbYear != null) {
		while(cmbYear.options.length)
			cmbYear.options[0] = null;
		cmbYear.options[0] = new Option("ปี", 0);
		if(dtDefault != null)
			j = dtDefault.getYear() + ((dtDefault.getYear() < 1900)? 1900:0)
		n = dt.getYear() + ((dt.getYear() < 1900)? 1900:0);
		for(i=n-below; i<n+more+1; i++) {
			cmbYear.options[cmbYear.options.length] = new Option((bThai)? i + 543:i, i);
			if(dtDefault != null)
				if(i == j)
					cmbYear.selectedIndex = cmbYear.options.length - 1;
		}
		if(dtDefault == null)
			cmbYear.selectedIndex = 0;
	}
}

/*
	Validate the selected day month and year for leap year, worng date and correct them if necessary.
	Ex 1..
		- <select name="cmbDay" onChange="dateValidation(this, this.form.cmbMonth, this.form.cmbYear);"></select>
*/
function dateValidation(cmbDay, cmbMonth, cmbYear) 
{	
	var nDay = cmbDay.options[cmbDay.options.selectedIndex].value;
	var nMonth = cmbMonth.options[cmbMonth.options.selectedIndex].value;
	var nYear = cmbYear.options[cmbYear.options.selectedIndex].value;

	if(nDay == 0 || nMonth == 0 || nYear == 0)
		return;

	var nMaxDay = 0;

	if(nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11)
		nMaxDay = 30;
	else if(nMonth == 2) {
		if(nYear % 4 == 0 && (nYear % 100 != 0 || nYear % 400 == 0))
			nMaxDay = 29;
		else
			nMaxDay = 28;
	}
	else
		nMaxDay = 31;

	if(nDay > nMaxDay)
		for(var i = 0; i < 31; i++)
			if(cmbDay[i].value == nMaxDay)
				cmbDay.options.selectedIndex = i;
}

//--- by Tot 2/1/45
function dateValidation2(index, frm, Day, Month, Year) 
{	
	var cmbDay = eval("document."+frm+"."+Day+"["+index+"];");
	var cmbMonth = eval("document."+frm+"."+Month+"["+index+"];");
	var cmbYear = eval("document."+frm+"."+Year+"["+index+"];");
	var nDay = cmbDay.options[cmbDay.options.selectedIndex].value;
	var nMonth = cmbMonth.options[cmbMonth.options.selectedIndex].value;
	var nYear = cmbYear.options[cmbYear.options.selectedIndex].value;

	if(nDay == 0 || nMonth == 0 || nYear == 0)
		return;

	var nMaxDay = 0;

	if(nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11)
		nMaxDay = 30;
	else if(nMonth == 2) {
		if(nYear % 4 == 0 && (nYear % 100 != 0 || nYear % 400 == 0))
			nMaxDay = 29;
		else
			nMaxDay = 28;
	}
	else
		nMaxDay = 31;

	if(nDay > nMaxDay)
		for(var i = 0; i < 31; i++)
			if(cmbDay[i].value == nMaxDay)
				cmbDay.options.selectedIndex = i;
}

/*
	Force the specified Day, Month and Year couldn't be selects date before the specified Date or Now!
	Ex ..
		- DMYFromNow(cmbDay, cmbMonth, cmbYear, new Date('Jan 1, 2000'));
		- DMYFromNow(cmbDay, cmbMonth, cmbYear, null);
*/
function DMYFromNow(cmbDay, cmbMonth, cmbYear, dtmComp) 
{
	if (cmbDay == null || cmbMonth == null || cmbYear == null)
		return;
	if (dtmComp == null)
		dtmComp = new Date();

	var dtmtmp = new Date(cmbYear.options[cmbYear.selectedIndex].value, cmbMonth.options[cmbMonth.selectedIndex].value - 1, cmbDay.options[cmbDay.selectedIndex].value);
	if (dtmtmp < dtmComp) {
		cmbDay.selectedIndex = dtmComp.getDate();
		cmbMonth.selectedIndex = dtmComp.getMonth() + 1;
		for (var i=0; i<cmbYear.options.length; i++) {
			if (cmbYear.options[i].value == dtmComp.getYear()) {
				cmbYear.selectedIndex = i;
				break;
			}
		}
	}
}

function DMYToNow(cmbDay, cmbMonth, cmbYear, dtmComp) 
{
	if (cmbDay == null || cmbMonth == null || cmbYear == null)
		return;
	if (dtmComp == null)
		dtmComp = new Date();

	var dtmtmp = new Date(cmbYear.options[cmbYear.selectedIndex].value, cmbMonth.options[cmbMonth.selectedIndex].value - 1, cmbDay.options[cmbDay.selectedIndex].value);
	if (dtmtmp > dtmComp) {
		cmbDay.selectedIndex = dtmComp.getDate();
		cmbMonth.selectedIndex = dtmComp.getMonth() + 1;
		for (var i=0; i<cmbYear.options.length; i++) {
			if (cmbYear.options[i].value == dtmComp.getYear()) {
				cmbYear.selectedIndex = i;
				break;
			}
		}
	}
}

/*
Get Max Day of the given month and year
*/
function GetMaxDay(nMonth, nYear) 
{
	var nMaxDay = 0;

	if(nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11)
		nMaxDay = 30;
	else if(nMonth == 2) {
		if(nYear % 4 == 0 && (nYear % 100 != 0 || nYear % 400 == 0))
			nMaxDay = 29;
		else
			nMaxDay = 28;
	}
	else
		nMaxDay = 31;

	return nMaxDay;
}

function IsMonthYear(szStr) 
{
	if(szStr.length != 7)
		if(szStr.length != 6)
			return false;

    var szMonth;
	var szSlash;
	var szYear;

	if(szStr.length == 7) {
		szMonth = szStr.substring(0, 2);
		szSlash = szStr.substring(2, 3);
		szYear = szStr.substring(3,  7);
	}
	else if(szStr.length == 6) {
		szMonth = szStr.substring(0, 1);
		szSlash = szStr.substring(1, 2);
		szYear = szStr.substring(2, 6);
	}

	var nMonth = parseInt(szMonth);
	if(nMonth < 1 || nMonth > 12)
		return false;

	 var x = IsInteger(szYear) && (szSlash == "/");
	 return x;
}

function ConvertCmbToDate(cmbMonth, cmbDay, cmbYear){
	var nDay = parseFloat(cmbDay.options[cmbDay.options.selectedIndex].value);
	var nMonth = parseFloat(cmbMonth.options[cmbMonth.options.selectedIndex].value) - 1;
	var nYear = parseFloat(cmbYear.options[cmbYear.options.selectedIndex].value);
	var jsDate = new Date(nYear, nMonth, nDay);

		return jsDate;
}

function subtdate(date1, date2){	
	secsleft = (date1.getTime()) - (date2.getTime());
	daysleft = (secsleft / (86400000));				
	return Math.round(daysleft);
}

function SetNow(cmbMonth, cmbDay, cmbYear) {
	var dtNow = new Date();

	cmbDay.selectedIndex = dtNow.getDate();
	cmbMonth.selectedIndex = dtNow.getMonth() + 1;

	for (var i=0; i<cmbYear.options.length; i++) {
		if (cmbYear.options[i].value == dtNow.getYear()) {
			cmbYear.selectedIndex = i;
			break;
		}
	}

	return;
}

-->
