function ValidateForm2() { //v2.0 Adapted
  var i,objStr,field,theCheck,atPos,theNum,colonPos,min,max,errors='',fieldname,objForm;
  objForm = eval(ValidateForm.arguments[(navigator.appName == 'Netscape')?0:1]);
  for (i=2; i<(ValidateForm.arguments.length-3); i+=4) {
    objStr = ValidateForm.arguments[(navigator.appName == 'Netscape')?i:i+1];
    fieldname = ValidateForm.arguments[i+3];
    if ((objStr.indexOf('document.layers[')==0 && document.layers==null) ||
        (objStr.indexOf('document.all[')   ==0 && document.all   ==null))
      objStr = 'document'+objStr.substring(objStr.substring(0,objStr.lastIndexOf('.')).
                 lastIndexOf('.'),objStr.length);  //fix layer ref if not supp
    field = eval(objStr);
    theCheck = ValidateForm.arguments[i+2];
    if (field.value) { //IF NOT EMPTY FIELD
      if (theCheck.indexOf('isEmail') != -1) { //CHECK EMAIL
        atPos = field.value.indexOf('@');
        if (atPos < 1 || atPos == (field.value.length - 1))
          errors += '- '+fieldname+' ต้องระบุที่อยู่ e-mail\n';
      } else if (theCheck != 'R') { //START NUM CHECKS
        theNum = parseFloat(field.value);
        if (field.value != ''+theNum) erbors += '- '+fieldname+' ต้องเป็นตัวเลข\n';
        if (theCheck.indexOf('inRange') != -1) { //CHECK RANGE
          colonPos = theCheck.indexOf(':');
          min = theCheck.substring(8,colonPos);
          max = theCheck.substring(colonPos+1,theCheck.length);
          if (theNum < min || max < theNum) //bad range
            errors += '- '+fieldname+' ต้องเป็นค่าที่อยู่ระหว่าง '+min+' and '+max+'.\n';
    } } }
    else if (theCheck.charAt(0) == 'R') errors += '- '+fieldname+' ยังไม่ได้กรอก\n';
  }
  if (errors) alert('พบข้อผิดพลาด:\n'+
                    errors);
  else objForm.submit();
}

function ValidateForm() {
   var args, strValue, strLabel, blnNotEmpty, strType, i, strMessage, intPos;
   args = ValidateForm.arguments;

   objForm = args[0];
   strMessage = '';
    for (i = 1; i < args.length - 3; i+=4){
        strValue = args[i];
        strLabel = args[i+1];
        blnNotEmpty = args[i+2];
        strType = args[i+3];      

        //Check for not empty
        if (blnNotEmpty && (strValue == '' || strType == 'Radio'))
			if (strType == 'Radio' || strType == 'Combo')
				strMessage = strMessage + strLabel + ' - ยังไม่ได้เลือก\n';
			else
				strMessage = strMessage + strLabel + ' - ยังไม่ได้พิมพ์\n';
        else if (strValue != ''){

                //Check for Email.
                if (strType == 'Email' && (strValue.indexOf('@') == -1  || strValue.indexOf('@') == strValue.length - 1))
                    strMessage = strMessage + strLabel + ' - ต้องเป็นที่อยู่อีเมล์\n';
                
                //Check for Number (Unsigned Integer).
                intPos = strType.indexOf('Number');
                if (intPos != -1){   
                   if (!isFinite(parseFloat(strValue)) || (parseInt(strValue) != parseFloat(strValue)) || parseFloat(strValue) < 0)
                      strMessage = strMessage + strLabel + ' - ต้องเป็นตัวเลขจำนวนเต็มบวก\n';
                   //Check for number with parameters.
                   else if(strType != 'Number')
                      strMessage = strMessage + CheckNumber(strLabel, strValue, strType);                                 
                }

                //Check for Float.
                intPos = strType.indexOf('Float');
                if (intPos != -1){   
                   if (!isFinite(parseFloat(strValue)))
                      strMessage = strMessage + strLabel + ' - ต้องเป็นตัวเลข\n';
                   //Check for number with parameters.
                   else if(strType != 'Float')
                      strMessage = strMessage + CheckNumber(strLabel, strValue, strType);                                 
                }

                //Check for Text.
                intPos = strType.indexOf('Text');
                if (intPos != -1){
                   //Check for text with parameters.
                   if(strType != 'Text')
                      strMessage = strMessage + CheckText(strLabel, strValue, strType);
                }

               //Check for Date.
               intPos = strType.indexOf('Date');
               if (intPos != -1){
                  strMessage = strMessage + CheckDate(strLabel, strValue, strType);
               }

               //Check for Year.
               if (strType == 'Year'){
                  if(!isFinite(parseFloat(strValue)) || (Number(strValue) != parseFloat(strValue)) || strValue.length != 4 || parseFloat(strValue) < 0)
                  strMessage = strMessage + strLabel + ' - ต้องเป็นเลขจำนวนเต็มบวก 4 หลัก\n'
               }

        }
   } 
if (strMessage == '')
   objForm.submit();
else
   alert(strMessage);
} 



function CheckDate(strLabel, strValue, strType){
  var strArray, strDateArray, strFormat, intDay, intMonth, intYear;
  var intAdjust, intMaxDay, strCompare, strDateArray2;
  var strCmpDate1, strCmpDate2, strCmpLabel;
  strArray = strType.split(' ');

  //Check format.
  if (strArray.length == 1)
     strFormat = 'English';
  else
     strFormat = 'Thai';



  //Get day, month, and year. 
  strDateArray = strValue.split('_');
  intDay =  Number(strDateArray[0]);
  intMonth = Number(strDateArray[1]);

  if ( ! isFinite(strDateArray[2]) ||  strDateArray[2] == '')
     return strLabel + ' - ปีต้องเป็นเลขจำนวนเต็มบวก 4 หลัก\n';
  else{
     if((Number(strDateArray[2]) != parseFloat(strDateArray[2])) || strDateArray[2].length != 4 || parseFloat(strDateArray[2]) < 0)
        return strLabel + ' - ปีต้องเป็นเลขจำนวนเต็มบวก 4 หลัก\n'
     else
        intYear = Number(strDateArray[2]);
  }

  //Check for special year.
  

  //Check day in month.
  switch (intMonth){
       case 1:
       case 3:
       case 5:
       case 7:
       case 8:
       case 10:
       case 12:
                intMaxDay = 31;           
                break;
       case 2:
                if (strFormat == 'Thai')  
                     intAdjust = 1;
                else
                     intAdjust = 0;
                if ((intYear + intAdjust) % 4 == 0)
                    intMaxDay = 29;
                else
                    intMaxDay = 28;  
                break;
       case 4:
       case 6:
       case 9:
       case 11:
                intMaxDay = 30;
                break;
       default:
           return strLabel + ' - เดือนไม่ถูกต้อง\n';
  }
  if (! (intDay >= 1 && intDay <= intMaxDay))
     return strLabel + ' - วันที่ในเดือน ' + DecodeMonth(intMonth) + ' ต้องอยู่ระหว่าง 1-' + intMaxDay + '\n';

  if (strArray.length > 2 ){
     strCompare = strArray[2];
     strDateArray2 = strArray[3].split('_');
     if (strDateArray2.length < 3){
        return '';
     } 
     strCmpDate1 = MergeCompareDate(strDateArray[0],strDateArray[1],strDateArray[2]);
     strCmpDate2 = MergeCompareDate(strDateArray2[0],strDateArray2[1],strDateArray2[2]);
     strCmpLabel = strArray[4];
     switch (strCompare){
       case 'eq':
         if (strCmpDate1 != strCmpDate2) 
           return strLabel + ' - ต้องเท่ากับ ' + strCmpLabel + '\n';
         else
           return '';
         break;
       case 'nq':
         if (strCmpDate1 == strCmpDate2) 
           return strLabel + ' - ต้องไม่เท่ากับ ' + strCmpLabel + '\n';
         else
           return '';
         break;
       case 'gt':
         if (strCmpDate1 <= strCmpDate2) 
           return strLabel + ' - ต้องมาหลัง ' + strCmpLabel + '\n';
         else
           return '';
         break;
       case 'lt':
         if (strCmpDate1 >= strCmpDate2) 
           return strLabel + ' - ต้องมาก่อน ' + strCmpLabel + '\n';
         else
           return '';
         break;
       case 'le':
         if (strCmpDate1 > strCmpDate2) 
           return strLabel + ' - ต้องมาก่อนหรือเป็นวันเดียวกับ ' + strCmpLabel + '\n';
         else
           return '';
         break;
       case 'ge':
         if (strCmpDate1 < strCmpDate2) 
           return strLabel + ' - ต้องมาหลังหรือเป็นวันเดียวกับ ' + strCmpLabel + '\n';
         else
           return '';
         break;                      
     }
  }
  return '';   
}

function MergeCompareDate(strDay, strMonth, strYear){
   var strCmpDay, strCmpMonth, strCmpYear;
   if (strDay.length == 1)
      strCmpDay = '0' + strDay;
   else
      strCmpDay = strDay;
   if (strMonth.length == 1)
      strCmpMonth = '0' + strMonth;
   else
      strCmpMonth = strMonth;
   switch (strYear.length){
      case 1:
              strCmpYear = '000' + strYear;
              break;
      case 2:
              strCmpYear = '00' + strYear;
              break;
      case 3:
              strCmpYear = '0' + strYear;
              break;
      default:
              strCmpYear = strYear;
   }
   return strCmpYear + strCmpMonth + strCmpDay;
}

function MergeDate(strDay, strMonth, strYear){
	var strTempDay,  strTempMonth
   if (strDay + strMonth + strYear ==  '') 
      return '';
   else{
      return strDay + '_' + strMonth + '_' + strYear;
   }
}

function DecodeMonth(intValue){
   switch (intValue){
      case 1:
              return 'มกราคม';
              break;
      case 2:
              return 'กุมภาพันธ์';
              break;
      case 3:
              return 'มีนาคม';
              break;
      case 4:
              return 'เมษายน';
              break;
      case 5:
              return 'พฤษภาคม';
              break;
      case 6:
              return 'มิถุนายน';
              break;
      case 7:
              return 'กรกฎาคม';
              break;
      case 8:
              return 'สิงหาคม';
              break;
      case 9:
              return 'กันยายน';
              break;
      case 10:
              return 'ตุลาคม';
              break;
      case 11:
              return 'พฤศจิกายน';
              break;
      case 12:
              return 'ธันวาคม';
              break;
      default:
              return '';
   }
}


function CheckText(strLabel, strValue, strType){
  var strArray, strCompare, strMatchValue, strMatchMessage, strTemp, intMatchValue;
  strArray = strType.split(' ');
  strCompare = strArray[1];


  switch (strCompare){
         
      case 'eq':
                  strMatchValue = strArray[2];
                  if (strArray.length > 3)
                     strMatchMessage = strArray[3];
                  else
                     strMatchMessage = strMatchValue;
                  if (strValue != strMatchValue)
                      return strLabel + ' - ต้องเท่ากับ ' + strMatchMessage + '\n';
                 else
                    return '';
                 break;

      case 'len':
                 intMatchValue = parseInt(strArray[2]);
                 if (strValue.length < intMatchValue)
                    return strLabel + ' - ต้องมีความยาวไม่ต่ำกว่า ' + intMatchValue + ' ตัวอักษร\n';
                 else
                    return '';
                 break;  

      case 'Thai':
                 strTemp = strValue;
                 if (CheckTextMatch(strTemp.toUpperCase(), 65, 90))
                    return strLabel + ' - ต้องเป็นภาษาไทย\n';
                 else
                    return '';
                 break;

      case 'English':  
                 if (CheckTextMatch(strValue, 3585, 3675))
                    return strLabel + ' - ต้องเป็นภาษาอังกฤษ\n';
                 else
                    return '';
                 break;
      default:
                 return '';
  }  
}

function CheckTextMatch (strText, intFirst, intLast){
  var i,j;
  for(i=0; i<strText.length; i++){
     for(j=intFirst; j<=intLast; j++){
        if(strText.charCodeAt(i) == j){
          return true;
        }
     }
  }
  return false;
}

function CheckNumber(strLabel, strValue, strType){
  var strArray, strCompare, fltLow, fltHigh;
  strArray = strType.split(' ');
  strCompare = strArray[1];   
  
  switch (strCompare){
       
      case 'gt':
                 fltLow = parseFloat(strArray[2]);
                 if (parseFloat(strValue) <= fltLow) 
                    return strLabel + ' - ต้องมากกว่า ' + fltLow + '\n';
                 else
                    return '';
                 break;   
                   
     case 'ge':
                fltLow = parseFloat(strArray[2]);
                if (parseFloat(strValue) < fltLow) 
                   return strLabel + ' - ต้องมากกว่าหรือเท่ากับ ' + fltLow + '\n';
                else
                   return '';
                break;
                    
     case 'lt':
                fltHigh = parseFloat(strArray[2]);
                if (parseFloat(strValue) >= fltHigh) 
                   return strLabel + ' - ต้องน้อยกว่า ' + fltLow + '\n';
                else
                   return '';
                break;  

     case 'le':
                fltHigh = parseFloat(strArray[2]);
                if (parseFloat(strValue) > fltHigh) 
                   return strLabel + ' - ต้องน้อยกว่าหรือเท่ากับ ' + fltHigh + '\n';
                else
                   return '';
                break;

     case 'bt':
                fltLow = parseFloat(strArray[2]);
                fltHigh = parseFloat(strArray[3]);
                if (parseFloat(strValue) < fltLow || parseFloat(strValue) > fltHigh) 
                   return strLabel + ' - ต้องมีค่าระหว่าง ' + fltLow + ' ถึง ' + fltHigh + '\n';
                else
                   return '';
                break;   


  }
  return '';         
}
 
function CompareValue(strValue,strSwap){
  if (isFinite(strValue))
     return strValue;
  else
     return strSwap;
}

function CheckEmpty(strText){
	if (strText == '')
		return true;
	else
		return false;
}
