
function trimDateString(str)
{
  if (str.length == 0) return str;
  while (str.charAt(0) == ' ') {
    str = str.substring(1);
  }
  while (str.charAt(str.length - 1) == ' ') {
    str = str.substring(0, str.length - 1);
  }
  return str;
}

// To use this method
function checkDateYYYYMMDD(theField, fieldName)
{
    theField.value = trimDateString(theField.value);
    if (doCheckDateYYYYMMDD(theField.value, -1, fieldName)) {
        return false;
    }
    else
    {
        theField.focus();
        theField.select();
        return true;
    }
}

// check date format like YYYY/MM
function checkDateYYYYMM(theField, fieldName)
{
    theField.value = trimDateString(theField.value);
    if (doCheckDateYYYYMM(theField.value, -1, fieldName)) {
        return false;
    }
    else
    {
        theField.focus();
        theField.select();
        return true;
    }
}

// To use this method
function checkDateYYYYMMDD2(theField, dateStr, yearConstricted)
{
    if (doCheckDateYYYYMMDD(dateStr, yearConstricted, theField.name)) {
        return true;
    }
    else {
        theField.focus();
        theField.select();
        return false;
    }
}


// ========================
function theDaysInMonth(month)
{
   var daysInMonth = new Array(12);
   daysInMonth[1] = 31;
   daysInMonth[2] = 29;   // must programmatically check this
   daysInMonth[3] = 31;
   daysInMonth[4] = 30;
   daysInMonth[5] = 31;
   daysInMonth[6] = 30;
   daysInMonth[7] = 31;
   daysInMonth[8] = 31;
   daysInMonth[9] = 30;
   daysInMonth[10] = 31;
   daysInMonth[11] = 30;
   daysInMonth[12] = 31;
   return daysInMonth[month];
}

function isDigit(c)
{
   return ((c >= "0") && (c <= "9"));
}

function isAllDigit(str)
{
   // check all of them are '0'~'9'
   var ch;
   for (var i=0; i<str.length; i++)  {
      ch = str.charAt(i);
      if (!isDigit(ch)) {
         //alert("Error: Date must be only included '0'~'9'.");
         return false;
      }
   }
   return true;
}

function daysInFebruary(year)
{
    return ( ((year % 4 == 0) && (!(year % 100 == 0) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function parseInt2(str) // remove first character which is '0'
{
    if (str.length == 0 || str.length == 1) return parseInt(str);

    //alert("OLD=[" + str + "]");
    while (str.charAt(0) == '0') {
        str = str.substring(1, str.length);
    }
    //alert("NEW=[" + str + "]");
    return parseInt(str);
}

// check format YYYY/MM
function doCheckDateYYYYMM(dateStr, yearConstricted, filedName)
{
     if (dateStr == null || dateStr.length == 0) {
         alert("Please Input "+ filedName +".  (YYYY/MM)");
         return false;
     }

     // check all of them are '0'~'9' or '/'
     var ch;
     for (var i=0; i<dateStr.length; i++)  {
         ch = dateStr.charAt(i);
         if (!isDigit(ch) && ch != '/') {
            alert(filedName +" must be only included '0'~'9' or '/ '. (YYYY/MM)");
            return false;
         }
     }

     // need two '/'
     var sArray = dateStr.split("/");

     if (sArray.length != 2) {
         alert(filedName +" must be separated by one '/ '. (YYYY/MM)");
         return false;
     }

     // check format: "YYYY/MM"
     //alert("sArray[0]=[" + sArray[0] + "]");
     //alert("sArray[1]=[" + sArray[1] + "]");
     if (sArray[0].length != 4) {
         alert("Please input "+ filedName +" year (4 digits). (YYYY/MM)");
         return false;
     }
     if (sArray[1] == "") {
         alert("Please input "+ filedName +" month. (YYYY/MM)");
         return false;
     }

     var iYear  = parseInt2(sArray[0]);
     var iMonth = parseInt2(sArray[1]);

     //alert("year=[" + iYear + "], month=[" + iMonth + "]");

     //if (iYear < 1970 || iYear > 2043) {
     if (yearConstricted != -1 && yearConstricted > iYear) {
         //if (!confirm("Are you sure the year is correct ? The year should be > " + yearConstricted)) {
         alert(filedName+" year should be > " + yearConstricted);
            return false;
         //}
     }
     else if (iYear < 1900 || iYear > 3000) {
         alert(filedName+" year must be between 1900 and 3000, error Year=[" + iYear + "]");
         return false;
     }

     if (iMonth < 1 || iMonth > 12) {
        alert(filedName+" month must be between 1 and 12, error Month=[" + iMonth + "]");
        return false;
     }

     return true;
}

function doCheckDateYYYYMMDD(dateStr, yearConstricted, filedName)
{
     if (dateStr == null || dateStr.length == 0) {
         alert("Please Input "+ filedName +".  (YYYY/MM/DD)");
         return false;
     }

     // check all of them are '0'~'9' or '/'
     var ch;
     for (var i=0; i<dateStr.length; i++)  {
         ch = dateStr.charAt(i);
         if (!isDigit(ch) && ch != '/') {
            alert(filedName +" must be only included '0'~'9' or '/ '. (YYYY/MM/DD)");
            return false;
         }
     }

     // need two '/'
     var sArray = dateStr.split("/");

     if (sArray.length != 3) {
         alert(filedName +" must be separated by two '/ '. (YYYY/MM/DD)");
         return false;
     }

     // check format: "YYYY/MM/DD"
     //alert("sArray[0]=[" + sArray[0] + "]");
     //alert("sArray[1]=[" + sArray[1] + "]");
     //alert("sArray[2]=[" + sArray[2] + "]");
     if (sArray[0].length != 4) {
         alert("Please input "+ filedName +" year (4 digits). (YYYY/MM/DD)");
         return false;
     }
     if (sArray[1] == "") {
         alert("Please input "+ filedName +" month. (YYYY/MM/DD)");
         return false;
     }
     if (sArray[2] == "") {
         alert("Please input "+ filedName +" day. (YYYY/MM/DD)");
         return false;
     }

     var iYear  = parseInt2(sArray[0]);
     var iMonth = parseInt2(sArray[1]);
     var iDay   = parseInt2(sArray[2]);

     //alert("year=[" + iYear + "], month=[" + iMonth + "], day=[" + iDay + "]");

     //if (iYear < 1970 || iYear > 2043) {
     if (yearConstricted != -1 && yearConstricted > iYear) {
         //if (!confirm("Are you sure the year is correct ? The year should be > " + yearConstricted)) {
         alert(filedName+" year should be > " + yearConstricted);
            return false;
         //}
     }
     else if (iYear < 1900 || iYear > 3000) {
         alert(filedName+" year must be between 1900 and 3000, error Year=[" + iYear + "]");
         return false;
     }

     if (iMonth < 1 || iMonth > 12) {
        alert(filedName+" month must be between 1 and 12, error Month=[" + iMonth + "]");
        return false;
     }
     if (iDay < 1 || iDay > 31) {
        alert(filedName+" day must be between 1 and 31, error Day=[" + iDay + "]");
        return false;
     }

     // more checking
     var days = theDaysInMonth(iMonth);
     if (iMonth !=2 && iDay > days) {
         alert(filedName+" last day of month "+ iMonth + " must be " + days);
         return false;
     }
     days = daysInFebruary(iYear);
     if (iMonth == 2 && iDay > days) {
         alert(filedName+" (February)last day of month " + iMonth + " must be " + days);
         return false;
     }

     return true;
}


function checkDateYYYYMMDD3(yyyyField, mmField, ddField)
{
    yyyyField.value = trimDateString(yyyyField.value);
    mmField.value = trimDateString(mmField.value);
    ddField.value = trimDateString(ddField.value);

     // check format: "YYYY/MM/DD"
     //alert("yyyyField.value.length="  + yyyyField.value.length +  ", yyyyField.value=[" + yyyyField.value + "]");

     if (yyyyField.value.length != 4 || !isAllDigit(yyyyField.value)) {
         alert("Error: Please input year (4 digits).\n (YYYY)");
         yyyyField.focus();
         yyyyField.select();
         return false;
     }
     var iYear  = parseInt2(yyyyField.value);
     var yearConstricted = -1;
     //if (iYear < 1970 || iYear > 2043) {
     if (yearConstricted != -1 && yearConstricted > iYear) {
         //if (!confirm("Are you sure the year is correct ? The year should be > " + yearConstricted)) {
         alert("The year should be > " + yearConstricted);
            yyyyField.focus();
            yyyyField.select();
            return false;
         //}
     }
     else if (iYear < 1900 || iYear > 3000) {
         alert("Error: Year must be between 1900 and 3000, error Year=[" + iYear + "]");
         yyyyField.focus();
         yyyyField.select();
         return false;
     }

     if (mmField.value.length <1 || mmField.value.length >2 || !isAllDigit(mmField.value)) {
         alert("Error: Please input month.\n (MM)");
         mmField.focus();
         mmField.select();
         return false;
     }
     var iMonth = parseInt2(mmField.value);
     if (iMonth < 1 || iMonth > 12) {
        alert("Error: Month must be between 1 and 12, error Month=[" + iMonth + "]");
        mmField.focus();
        mmField.select();
        return false;
     }

     if (ddField.value.length <1 || ddField.value.length >2 || !isAllDigit(ddField.value)) {
         alert("Error: Please input day.\n (DD)");
         ddField.focus();
         ddField.select();
         return false;
     }
     var iDay   = parseInt2(ddField.value);
     if (iDay < 1 || iDay > 31) {
        alert("Error: Day must be between 1 and 31, error Day=[" + iDay + "]");
        ddField.focus();
        ddField.select();
        return false;
     }

     // more checking
     var days = theDaysInMonth(iMonth);
     if (iMonth !=2 && iDay > days) {
         alert("Error: Last day of month "+ iMonth + " must be " + days);
         ddField.focus();
         ddField.select();
         return false;
     }
     days = daysInFebruary(iYear);
     if (iMonth == 2 && iDay > days) {
         alert("Error: (February)Last day of month " + iMonth + " must be " + days);
         ddField.focus();
         ddField.select();
         return false;
     }

     return true;
}
