/*
*******************************************************************************************
*      Script Name: checkForm.js                                                          *
*      Function: Form Checking, error checking.                                           *
*      Function Name:                                                                     *
*Line                                                                                     *
*----                                                                                     *
* 54   bool checkExt(obj,str file_extention); // Check File Extention                     *
* 67   bool myExt(obj,str file_extention); // Inner Function of checkExt()                *
* 92   bool checkNumeric(obj); //Check integer is that numeric                            *
* 108  bool checkChar(obj); // Check string is the valid character(s) or not              *
* 119  bool checkAmount(obj); // Check the integer is that valid amount or not.           *
* 135  bool checkZero(obj); // Check text box's value whether it is 0 or not, and not     *
*                              allowed blank.                                             *
* 145  bool checkBlank(obj); // Checking form object(s) is that empty                     *
* 154  bool checkEmail(obj); // Check email address is valid or invalid                   *
* 167  bool checkDate(obj,[str auto],[int 1||2]); // Check date is valid or not           *
* 288  bool checkTextBox(obj,str text_box_name); // Check array type of the text box is   *
*                                                     empty or not                        *
* 301  bool checkMenuBox(obj,str menu_box_name); // Check array type of menu box is       *
*                                                     empty or not                        *
* 313  bool checkArrayNumeric(form,str text_box_name,bool true||false); // Check array    *
*                                 type of the text box value is valid numeric or not      *
* 343  bool checkBox(form); // Check all form elements of check box is that empty         *
* 364  book optionBox(form,str object_name); // Check form elements of check box or option*
*                                            box (single or multi elements) is that empty *
* 387  bool checkTime(form[,str h||m||hm||s|| ]); // Check Time is that valid time or not *
* 476  bool checkStrLen(obj) ; // Check string can't less than 6 characters or not        *
*                                       allowed more than 15 characters.                  *
* 489  bool checkPwd(obj1,obj2); // Check whether first object match with object 2 or not *
* 505  bool checkFloat(obj); // Check whether the value is that floating point            *
*                                                                                         *
*                                                                                         *
*     Author:  Gordon Lim Hon Wei                                                         *
*     Create Date: 2002-03-14                                                             *
*                                                                                         *
*******************************************************************************************

*******************************************************************************************
* Last Updated By      Date(YYYY-MM-DD)        Function Name    Description               *
* ---------------      ----------------        -------------    -----------               *
* Gordon Lim Hon Wei   2002-09-11 (Wednesday)  checkAmount()    Fix bugs                  *
* Gordon Lim Hon Wei   2002-09-11 (Wednesday)  checkBlank()     Fix check blank's bug     *
* Gordon Lim Hon Wei   2002-09-11 (Wednesday)  checkDate()      Fix bugs                  *
* Gordon Lim Hon Wei   2002-09-13 (Friday)     checkTime()      Fix bug & add more feature*
* Gordon Lim Hon Wei   2002-09-13 (Friday)     optionBox()      Add new function          *
* Gordon Lim Hon Wei   2002-09-13 (Friday)     checkZero()      Add new function          *
* Gordon Lim Hon Wei   2002-09-13 (Friday)     checkStrLen()    Add new function          *
* Gordon Lim Hon Wei   2002-09-13 (Friday)     checkPwd()       Add new function          *
* Gordon Lim Hon Wei   2002-09-13 (Friday)     all functions    Change Functions type     *
* Gordon Lim Hon Wei   2002-09-27 (Friday)     checkFloat()     Add new functions         *
*******************************************************************************************

*/

  function checkExt(file,exname) {
  var myfile=file.value;
  var myname=exname.toLowerCase();

         if (trim(myfile)!="") {
            if (myExt(file,myname)==true) {
                 return true;
            }else{
                 return false;
            }
         }
  }
  
  function myExt(file,exname) {
     var ext;
     var exname;
     var myfile=file.value;
     exname=exname.split(",");

			
       if (trim(myfile)=="") {
       }else{
           //if (myfile.indexOf("\\") != -1) {
               ext = myfile.slice(myfile.indexOf(".")).toLowerCase();

           //}
            for (i=0;i<exname.length;i++) {
                for (j=0;j<exname.length;j++) {
                     var myname="."+exname[j];
                
                     while(ext==myname) {
                           return true;
                     }
               }
           }
       }
  }

  function checkNumeric(field) {
     var myfield=field.value;
     var numericPat = /^(\"*\"|[0-9]\w*)$/;
     var matchArray = myfield.match(numericPat);


            if (isNaN(myfield)) {
               return false;
            }

                  if (trim(myfield)!="" && matchArray == null) {
                     return false;
                  }
     return true;
  }

  function checkChar(charStr) {
     if (trim(charStr.value)=="") {
        return false;
     }

     if (!isNaN(charStr.value)) {
        return false;
     }
     return true;
  }

  function checkAmount(amtNum) {
     var num=amtNum.value;
     var ext = num.slice(num.indexOf("."));

       if (trim(num)!="") {
            if (ext.length>3 || ext==".") {
               return false;
            }

            if (isNaN(num)) {
               return false;
            }
       }
     return true;
  }

  function checkZero(str) {
  var mystr=str.value;
  
         if(trim(mystr)==0 || isNaN(mystr)) {
            return false;
         }else{
            return true;
         }
  }
  
  function checkBlank(field) {
     if (trim(field.value)=="") {
        return false;
     }else{
        return true;
     }
  }


  function checkEmail(emailStr) {
     var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
     var mymail=emailStr.value;
     var matchArray = mymail.match(emailPat);

            if (trim(mymail)!="" && matchArray==null) {
                 return false;
            }else{
                  return true;
            }

  }

  function checkDate(dateStr,dauto,status) {
  // Status: 1 - DD-MM-YY
  //         2 - YY-MM-DD
  // dauto usage: auto giving date string if month or day only 1 string, "5", it will auto
  //              return it to "05"
  // Parameter For dauto(optional): auto
  // Status: default empty, if you did not pass status into the function, it will auto check
  //         date to status 1.
  
  var mydate=dateStr.value;
  if (status==null) status="1";
  
     if (trim(mydate)=="") {
          return true;
       }else
          if (status==1) {
             var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/;
          }
             if (status==2) {
                var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
             }
             
           var matchArray = mydate.match(datePat); // DD-MM-YY

           if (trim(mydate)!="" && matchArray == null) {
              return false;
           }
           
           if (status==1) {
             			month = matchArray[3]; // parse date into variables
              		day = matchArray[1];
                  year = matchArray[4];
           }

                  if (status==2) {
                     month = matchArray[3]; // parse date into variables
                     day = matchArray[5];
                     year = matchArray[1];
                  }

          if (month==0) {
              return false;
          }
          
             if (day==0) {
                 return false;
             }
             
          if (dauto!=null && dauto=="auto") {
             var numericPat = /^(\"*\"|[0]\w*)$/;

               if (month<="9") {
                  if (month==0) {
                     return false;
                  }
               
                 var monthArray=month.match(numericPat);
                 
                    if (monthArray==null && month.length<2) {
                       month="0"+month;
                    }else{
                       month=month;
                    }
               }
               
               if (day<=9) {
                  if (day==0) {
                     return false;
                  }
                  
                  var dayArray=day.match(numericPat);
                      if (dayArray==null && day.length<2) {
                          day="0"+day;
                      }else{
                          day=day;
                      }
               }
     			  }
          
      if (trim(mydate)=="" || dauto==null) {
      }else{
            if (status==1) {
               dateStr.value=day+"/"+month+"/"+year;
               mydate=day+"/"+month+"/"+year;
            }
               if (status==2) {
                  dateStr.value=year+"-"+month+"-"+day;
                  mydate=year+"-"+month+"-"+day;
               }
      }

          if (month < 1 || month > 12) { // check month range
             return false;
          }
          
             if (day < 1 || day > 31) {
                return false;
             }

        if ((month==4 || month==6 || month==9 || month==11) && day==31) {
           return false;
				}

       if (month == 2) { // check for february 29th
          var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
          if (day>29 || (day==29 && !isleap)) {
             return false;
          }
       }

          if (mydate.length<10 || mydate.length>10) {
             if (dauto!=null) {
                return false;
             }else{
                return false;
             }
          }

          return true;  // date is valid
  }

  function checkTextBox(field,myname) {
  // How to use? field=document.form_name
  // myname=your_text_box_name;

     var text="-=You have the following error message=-\n";
       for (i = 0; i < field.elements.length; i++) {
         if (field.elements[i].type=="text" && field.elements[i].name==myname && trim(field.elements[i].value)=="") {
          return false;
         }
       }
     return true;
  }

  function checkMenuBox(field,myname) {
  // How to use? field=document.form_name
  // myname=your_text_box_name;

    for (i = 0; i < field.elements.length; i++) {
        if (field.elements[i].name==myname && trim(field.elements[i].value)=="") {
           return false;
        }
    }
    return true;
  }

  function checkArrayNumeric(field,myname,ctrl) {
  //Function:  Checking Textbox's Array is Blank field or not and checking it is numeric or not.
  // How to use? field=document.form_name
  // myname=your_text_box_name;
  // ctrl=true or false.
     var myform=field;

       for(i=0;i<myform.elements.length-1; i++) {
          var numericPat = /^(\"*\"|[0123456789]\w*)$/;
          var matchArray=myform.elements[i].value.match(numericPat);

          if (ctrl=="true") { //True, Allowed Textbox Empty but must be numeric.
             if (field.elements[i].type=="text" && field.elements[i].name==myname && isNaN(field.elements[i].value)) {
                return false;
             }
          }

          if (ctrl=="false") { //False, Textbox Empty Not Allowed, and Must be numeric.
             if (myform.elements[i].type=="text" && myform.elements[i].name==myname && trim(myform.elements[i].value=="")) {
                 return false;
             }

             if (myform.elements[i].name==myname && isNaN(myform.elements[i].value)) {
                 return false;
             }
          }
       }
    return true;
  }

  function checkBox(form) {
  // Check all check box in same form whether it is empty or not.
  var myform=form.elements;
  var flag=false;

      for (i=0;i<myform.length-1;i++) {

             if (myform[i].type=="checkbox" && myform[i].checked==true) {
                flag=true;
             }
             
      }

     if (flag) {
        return true;
     }else{
        return false;
     }
  }


  function optionBox(form,name) {
  // Features: check radio button or check box is that "checked", allowed check
  // more than 1 check box simultaneous with same check box name.
  
  var myform=form.elements;
  var flag=false;

      for (i=0;i<myform.length-1;i++) {

            if (myform[i].name==name && myform[i].checked==true) {
               flag=true;
            }
            
      }

     if (flag) {
        return true;
     }else{
        return false;
     }
  }


  function checkTime(timeStr,flag) {
  // Features: Allowed to check hour or minitues, sec,or Hr:min or Hr:min:sec
  // Flags : h - hour, m - minutes, s-second, hm - hour:minutes
  // Function allowed optional to pass flag into the function or not, if
  // leave empty, like this: checkTime(form.timebox) are checking whole time,
  // Hr:min:sec
  
  var mytime=timeStr.value;
  
  if (mytime==null) {
      mytime=timeStr;
  }

    if (trim(mytime)!="") {
       switch(flag) {
          case "h":
             if (isNaN(mytime)) {
                 return false;
             }
              
             if (mytime>23 || mytime<"00") {
                return false;
             }
          break;
          
          case "m":
              if (isNaN(mytime)) {
                 return false;
              }
              
              if (mytime>59 || mytime<"00") {
                 return false;
              }
          break;
          
          case "s":
              if (isNaN(mytime)) {
                 return false;
              }

              if (mytime>59 || mytime<"00") {
                 return false;
          }
          break;
          
          case "hm":
          var timePat = /^(\d{2})(\:)(\d{2})$/;
          var matchArray = mytime.match(timePat);
          var tstr=mytime.split(":");

               if (matchArray==null) {
                  return false;
               }

               if (tstr[0]>23 || tstr[0]<"00") {
                  return false;
               }

                  if (tstr[1]>59 || tstr[1]<"00") {
                     return false;
                  }

          break;
          
          case null:
          var timePat = /^(\d{2})(\:)(\d{2})\2(\d{2}|\d{2})$/;
          var matchArray = mytime.match(timePat);
          var tstr=mytime.split(":");

               if (matchArray==null) {
                  return false;
               }

               if (tstr[0]>23 || tstr[0]<"00") {
                  return false;
               }

                  if (tstr[1]>59 || tstr[1]<"00") {
                     return false;
                  }
                     if (tstr[2]>59 || tstr[2]<"00") {
                         return false;
                     }
          break;
       }
    }
    return true;
  }
  
  function checkStrLen(usr) {
  // Check string can't less than 6 characters or can't more than 15 characters.
  
  var myusr=usr.value.length;

     if (trim(usr.value)!="") {
       if (myusr<6 || myusr>15) {
          return false;
       }
     }
    return true;
  }
  
  function checkPwd(pwd1,pwd2) {
  // Check pwd1 match with pwd2 or not.
  
  var mypwd1=pwd1.value;
  var mypwd2=pwd2.value;

    if (trim(mypwd1)!="" && trim(mypwd2)!="") {
      if (mypwd1!=mypwd2) {
         return false;
      }
    }
   return true;
  }

  function checkFloat(str) {
      var mynum=str.value;
      
      if (!parseFloat(mynum)) {
          return false;
      }else{
          return true;
      }
  }
  

  function ltrim ( s )
  {
	  return s.replace( /^\s*/, "" )
  }

  function rtrim ( s )
  {
	  return s.replace( /\s*$/, "" );
  }


  function trim ( s )
  {
	  return rtrim(ltrim(s));
  }

