var tempdate = new Date();
var dtCurrentDate = tempdate.getDate();
var dtCurrentMonth = tempdate.getMonth() + 1;
var dtCurrentYear = tempdate.getFullYear();
var dtSetCurrentDate = false;
var dtYearMinOffset = 10;
var dtYearMaxOffset = 10;
var dtDOW = new Array("", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var dtMonthEng = new Array("", "January","February","March","April","May","June","July","August","September","October","November","December");
var dtMonthThai = new Array("", "มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
var dtAMPM = new Array("AM", "PM");

function setCurrentDate()
{
  dtSetCurrentDate = true;
}

function unsetCurrentDate()
{
  dtSetCurrentDate = false;
}

function WriteDOWOption()
{
  for(i = 1; i <= 7; i++)
    document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', dtDOW[i]);
}

function WriteAMPMOption()
{
  for(i = 0; i <= 1; i++)
    document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', dtAMPM[i]);
}

function writeDateOption()
{
  if(isNaN(arguments[0]) && dtSetCurrentDate)
    arguments[0] = dtCurrentDate;
  for(i = 1; i <= 31; i++)
    document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', i);
}

function writeMonthOption()
{
  if(isNaN(arguments[0]) && dtSetCurrentDate)
    arguments[0] = dtCurrentMonth;
  for(i = 1; i <= 12; i++)
    document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', i);
}

function writeMonthEngOption()
{
  if(isNaN(arguments[0]) && dtSetCurrentDate)
    arguments[0] = dtCurrentMonth;
  for(i = 1; i <= 12; i++)
    document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', dtMonthEng[i]);
}

function writeMonthThaiOption()
{
  if(isNaN(arguments[0]) && dtSetCurrentDate)
    arguments[0] = dtCurrentMonth;
  for(i = 1; i <= 12; i++)
    document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', dtMonthThai[i]);
}

function writeYearOption()
{
  if(isNaN(arguments[0]) && dtSetCurrentDate)
    arguments[0] = dtCurrentYear;
  if(!isNaN(arguments[0]) && (arguments[0] < dtCurrentYear - dtYearMinOffset))
    document.write('<option value="', arguments[0], '" selected>', arguments[0] + 543);
  for(i = dtCurrentYear - dtYearMinOffset; i <= dtCurrentYear + dtYearMaxOffset; i++)
    document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', i + 543);
  if(!isNaN(arguments[0]) && (arguments[0] > dtCurrentYear + dtYearMaxOffset))
    document.write('<option value="', arguments[0], '" selected>', arguments[0] + 543);
}

function writeYearBirthOption()
{
  if(isNaN(arguments[0]) && dtSetCurrentDate)
    arguments[0] = dtCurrentYear;
  if(!isNaN(arguments[0]) && (arguments[0] < dtCurrentYear - dtYearMinOffset))    document.write('<option value="', arguments[0], '" selected>', arguments[0] + 543);
  for(i = dtCurrentYear ; i >= dtCurrentYear - 70; i--)     document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', i + 543);

}

function writeYearBirthEngOption()
{
  if(isNaN(arguments[0]) && dtSetCurrentDate)
    arguments[0] = dtCurrentYear;
  if(!isNaN(arguments[0]) && (arguments[0] < dtCurrentYear - dtYearMinOffset))    document.write('<option value="', arguments[0], '" selected>', arguments[0]);
  for(i = dtCurrentYear ; i >= dtCurrentYear - 70; i--)     document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', i);

}


function writeYearDescOption()
{
  if(isNaN(arguments[0]) && dtSetCurrentDate)
    arguments[0] = dtCurrentYear;
  if(!isNaN(arguments[0]) && (arguments[0] < dtCurrentYear - dtYearMinOffset))
    document.write('<option value="', arguments[0], '" selected>', arguments[0] + 543);
  for(i = dtCurrentYear + dtYearMaxOffset; i >= dtCurrentYear - dtYearMinOffset; i--)
    document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', i + 543);
  if(!isNaN(arguments[0]) && (arguments[0] > dtCurrentYear + dtYearMaxOffset))
    document.write('<option value="', arguments[0], '" selected>', arguments[0] + 543);
}

function writeYearADOption()
{
  if(isNaN(arguments[0]) && dtSetCurrentDate)
    arguments[0] = dtCurrentYear;
  if(!isNaN(arguments[0]) && (arguments[0] < dtCurrentYear - dtYearMinOffset))
    document.write('<option value="', arguments[0], '" selected>', arguments[0] );
  for(i = dtCurrentYear - dtYearMinOffset; i <= dtCurrentYear + dtYearMaxOffset; i++)
    document.write('<option value="', i, (i == arguments[0]) ? '" selected>' : '">', i );
  if(!isNaN(arguments[0]) && (arguments[0] > dtCurrentYear + dtYearMaxOffset))
    document.write('<option value="', arguments[0], '" selected>', arguments[0] );
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function isValidDate(y,m,d) {
	if ((y == "")||(m == "")||(d == ""))
		return false;
	switch (d) {
		case "29" :  {if ((m=="2") &&(!LeapYear(y))) return false;
								break;
							}
		case "30" :  { if (m=="2") return false;
								break;
							}
		case "31" :  if ((m=="2")||(m=="4")||(m=="6")||(m=="9")||(m=="11")) return false;
	}
	return true;
}

function isValidNormalDate(date)
{
  datearr = date.split('/');
  if(datearr.length == 3)
  {
    d = datearr[0];
    m = datearr[1];
    y = datearr[2];
    if(isNaN(d) || isNaN(m) || isNaN(y))
    {
      return false;
    }
    else
    {
      return isValidDate(y - 543, m, d);
    }
  }
  else
  {
    return false;
  }
}

function integrateDate(y, m, d)
{
  return parseInt(y) * 10000 + parseInt(m) * 100 + parseInt(d);
}



