/*
 * validateForm.js - validates form inputs
 */

function eCheckFloat(sn){
    s= sn.value;
    for (var i = 0; i < s.length; i++) {
        ch = s.substring(i, i + 1)
        if (!((ch >= "0" && ch <= "9") || ch=="." || ch==","))
             return false;
        }
   return true;
}

function CheckFloat(theForm){
   for(var i=1; i<CheckFloat.arguments.length; i++)
         if (!eCheckFloat(theForm.elements[CheckFloat.arguments[i]])){
            alert("Invalid number");
            theForm.elements[CheckFloat.arguments[i]].focus();
            return false;
        }
   return true;
};
function eCheckNum(sn){
    s= sn.value;
    for (var i = 0; i < s.length; i++) {
        ch = s.substring(i, i + 1)
        if (!(ch >= "0" && ch <= "9"))
             return false;
        }
   return true;
}
function CheckNum(theForm){
   for(var i=1; i<CheckNum.arguments.length; i++)
         if (!eCheckNum(theForm.elements[CheckNum.arguments[i]])){
            alert("Invalid number");
            theForm.elements[CheckNum.arguments[i]].focus();
            return false;
        }
   return true;
}

function eCheckTime(sn) {
      s= sn.value;
      i=0;
      tm= new Array();
      tm[0]= '';tm[1]= '';tm[2]='';tm[3]= '';tm[4]='';
      md = 0 ;

      while (i< s.length) {
          ch= s.substring(i, i + 1);
          if( ch<='9' && ch>='0')
              tm[md] += ch;
          else if(ch==':') md++;
          else if (ch==' ') {
             if(md<2) return false;
             md++;
          }
          else if(md>=2 && md<=3){
            tmp= s.substring(i, i + 2);
            tmp = tmp.toUppercase();
            if ( tmp=='AM' || tmp=='PM')
            {
               tm[3]= tmp;
               ++i;
               md++;
            }
            else return false;
          }
          else return false;
          i++;
      }

      if(tm[4]!='') return false;
      if( tm[0].valueOf()<1 || tm[0].valueOf()>23) return false;
      if(tm[0].valueOf()<13 && tm[3]=='') //Specify am/pm
          return false;

      if(tm[0].valueOf()>12 && tm[3]!='') //don't Specify am/pm
          return false;

      if( tm[1].valueOf()<1 || tm[1].valueOf()>59) return false;

      if(tm[2]!='')
          if( tm[2].valueOf()<1 || tm[2].valueOf()>59) return false;

      return (tm[4]=='');
}

function CheckTime(theForm){
   for(var i=1; i<CheckTime.arguments.length; i++)
         if (!eCheckTime(theForm.elements[CheckTime.arguments[i]])){
            alert("Invalid Time");
            theForm.elements[CheckTime.arguments[i]].focus();
            return false;
        }
   return true;
}
function eCheckDate(sn) {
      s= sn.value;
      var i=0;
      tm= new Array();
      tm[0]= '';tm[1]= '';tm[2]='';tm[3]='';
      sep='';
      md = 0 ;

      while (i< s.length) {
          ch= s.substring(i, i + 1);
          if( ch<='9' && ch>='0')
              tm[md] += ch;
          else if (sep=='') {
              if(ch=='.' || ch=='/' || ch=='-') {
                   md++;
                   sep=ch;
              }
          }
          else if (ch==sep) {
             md++;
             if(md>2) return false;
          }
          else return false;

          i++;
      }

      if(tm[3]!='') return false;
      if(tm[0].valueOf()<1 || tm[0].valueOf()>12) return false; //Month
      if(tm[1].valueOf()<1 || tm[1].valueOf()>31) return false;
      if(tm[2].valueOf()<100) return false;

      if ((tm[0]==4 || tm[0]==6 || tm[0]==9 || tm[0]==11) && tm[1]==31)
          return false;
      if (tm[0]==2) {
           isleap = (tm[2] % 4 == 0 && (tm[2] % 100 != 0 || tm[2] % 400 == 0));
           if (tm[1]>29 || (tm[1]==29 && !isleap))
                return false;
      }

      return (tm[3]=='');
}

function CheckDate(theForm){
   for(var i=1; i<CheckDate.arguments.length; i++)
         if (!eCheckDate(theForm.elements[CheckDate.arguments[i]])){
            alert("Invalid Date");
            theForm.elements[CheckDate.arguments[i]].focus();
            return false;
        }
   return true;
}

function eCheckAlphaNum(sn){
    s= sn.value;
    for (var i = 0; i < s.length; i++) {
        ch = s.substring(i, i + 1)
        if (!((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
           || (ch >= "0" && ch <= "9")))
             return false;
        }
   return true;
}
function CheckAlphaNum(theForm){
   for(var i=1; i<CheckAlphaNum.arguments.length; i++)
         if (!eCheckAlphaNum(theForm.elements[CheckAlphaNum.arguments[i]])){
            alert("Field entry is not valid");
            theForm.elements[CheckAlphaNum.arguments[i]].focus();
            return false;
        }
   return true;
}

function eCheckAlpha(sn){
    s= sn.value;
    for (var i = 0; i < s.length; i++) {
        ch = s.substring(i, i + 1)
        if (!((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")))
             return false;
        }
   return true;
}

function CheckAlpha(theForm){
   for(var i=1; i<CheckAlpha.arguments.length; i++)
         if (!eCheckAlpha(theForm.elements[CheckAlpha.arguments[i]])){
            alert("Field entry is not valid");
            theForm.elements[CheckAlpha.arguments[i]].focus();
            return false;
        }
   return true;
}

function eCheckEMail(sn){
       s= sn.value;
       re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
       if (re.test(s)){
           return true
       }
    return false;
}

function CheckEMail(theForm){
   for(var i=1; i<CheckEMail.arguments.length; i++)
         if (!eCheckEMail(theForm.elements[CheckEMail.arguments[i]])){
            alert("Field entry is not valid");
            theForm.elements[CheckEMail.arguments[i]].focus();
            return false;
        }
   return true;
}


function CheckRequiredFields(theForm) {
   for(i=1; i<CheckRequiredFields.arguments.length; i++)
        if(theForm.elements[CheckRequiredFields.arguments[i]].value==""){
            alert("This field is required");
            theForm.elements[CheckRequiredFields.arguments[i]].focus();
            return false;
        }
   return true;
}



