CS 101 Assignment # 3
<html>
<head>
<script language="JavaScript" type="text/javascript">
function alert_showmsg(msgs)
{
    var whole_msg="";
    var first_elmnt=null;
    for(var m=0;m < msgs.length;m++)
    {
        if(null == first_elmnt)
        {
            first_elmnt = msgs[m]["input_element"];
        }
        whole_msg += msgs[m]["msg"] + "\n";
    }
    alert(whole_msg);
    if(null != first_elmnt)
    {
        sfm_set_focus(first_elmnt);
    }
}
function sfm_validator_enable_focus(enable)
{
    document.set_focus_onerror = enable;
}
function set_addnl_vfunction(functionname)
{
  this.formobj.addnlvalidation = functionname;
}
function sfm_set_focus(objInput)
{
    if(document.set_focus_onerror)
    {
        objInput.focus();
    }
}
function sfm_enable_show_msgs_together()
{
    this.show_errors_together=true;
    this.formobj.show_errors_together=true;
}
function clear_all_validations()
{
            for(var itr=0;itr < this.formobj.elements.length;itr++)
            {
                        this.formobj.elements[itr].validationset = null;                       
            }
}
function form_submit_handler()
{
   var bRet = true;
    document.error_disp_handler.clear_msgs();
            for(var itr=0;itr < this.elements.length;itr++)
            {
                        if(this.elements[itr].validationset &&
               !this.elements[itr].validationset.validate())
                        {
                          bRet = false;
                        }
        if(!bRet && !this.show_errors_together)
        {
          break;
        }
            }
            if(this.addnlvalidation)
            {
              str =" var ret = "+this.addnlvalidation+"()";
              eval(str);
     if(!ret) 
     {
       bRet=false; 
     }
            }
   if(!bRet)
    {
      document.error_disp_handler.FinalShowMsg();
      return false;
    }
            return true;
}
function Validator(frmname)
{
  this.formobj=document.forms[frmname];
            if(this.formobj.onsubmit)
            {
             this.formobj.old_onsubmit = this.formobj.onsubmit;
             this.formobj.onsubmit=null;
            }
            else
            {
             this.formobj.old_onsubmit = null;
            }
            this.formobj._sfm_form_name=frmname;
            this.formobj.onsubmit=form_submit_handler;
            this.ValAddition = add_validation;
            this.setAddnlValidationFunction=set_addnl_vfunction;
            this.clearAllValidations = clear_all_validations;
    this.disable_validations = false;//new
    document.error_disp_handler = new sfm_ErrorDisplayHandler();
    this.EnableOnPageErrorDisplay=validator_enable_OPED;
            this.EnableOnPageErrorDisplaySingleBox=validator_enable_OPED_SB;
    this.show_errors_together=true;
    this.EnableMsgsTogether=sfm_enable_show_msgs_together;
    document.set_focus_onerror=true;
    this.EnableFocusOnError=sfm_validator_enable_focus;
}
function add_validation(itemname,descriptor,errstr)
{
            var condition = null;
            if(arguments.length > 3)
            {
             condition = arguments[3]; 
            }
  if(!this.formobj)
            {
                        alert("Error: The form object is not set properly");
                        return;
            }//if
            var itemobj = this.formobj[itemname];
    if(itemobj.length && isNaN(itemobj.selectedIndex) )
    //for radio button; don't do for 'select' item
            {
                        itemobj = itemobj[0];
            }          
  if(!itemobj)
            {
                        alert("Error: Couldnot get the input object named: "+itemname);
                        return;
            }
            if(!itemobj.validationset)
            {
                        itemobj.validationset = new ValidationSet(itemobj,this.show_errors_together);
            }
            itemobj.validationset.add(descriptor,errstr,condition);
    itemobj.validatorobj=this;
}
function validator_enable_OPED()
{
    document.error_disp_handler.EnableOnPageDisplay(false);
}
function validator_enable_OPED_SB()
{
            document.error_disp_handler.EnableOnPageDisplay(true);
}
function edh_clear_msgs()
{
    this.msgdisplay.clearmsg(this.all_msgs);
    this.all_msgs = new Array();
}
function edh_FinalShowMsg()
{
    this.msgdisplay.showmsg(this.all_msgs);
}
function edh_EnableOnPageDisplay(single_box)
{
            if(true == single_box)
            {
                        this.msgdisplay = new SingleBoxErrorDisplay();
            }
            else
            {
                        this.msgdisplay = new DivMsgDisplayer();                
            }
}
function edh_ShowMsg(msg,input_element)
{
   var objmsg = new Array();
   objmsg["input_element"] = input_element;
   objmsg["msg"] =  msg;
   this.all_msgs.push(objmsg);
}
function AlertMsgDisplayer()
{
  this.showmsg = alert_showmsg;
  this.clearmsg=alert_clearmsg;
}
function sfm_ErrorDisplayHandler()
{
  this.msgdisplay = new AlertMsgDisplayer();
  this.EnableOnPageDisplay= edh_EnableOnPageDisplay;
  this.ShowMsg=edh_ShowMsg;
  this.FinalShowMsg=edh_FinalShowMsg;
  this.all_msgs=new Array();
  this.clear_msgs=edh_clear_msgs;
}
function alert_clearmsg(msgs)
{
}
function sfm_show_error_msg(msg,input_elmt)
{
    document.error_disp_handler.ShowMsg(msg,input_elmt);
}
function SingleBoxErrorDisplay()
{
 this.showmsg=sb_div_showmsg;
 this.clearmsg=sb_div_clearmsg;
}
function sb_div_clearmsg(msgs)
{
            var divname = form_error_div_name(msgs);
            show_div_msg(divname,"");
}
function ValidationDesc(inputitem,desc,error,condition)
{
  this.desc=desc;
            this.error=error;
            this.itemobj = inputitem;
            this.condition = condition;
            this.validate=vdesc_validate;
}
function vdesc_validate()
{
            if(this.condition != null )
            {
                        if(!eval(this.condition))
                        {
                                    return true;
                        }
            }
            if(!validateInput(this.desc,this.itemobj,this.error))
            {
                        this.itemobj.validatorobj.disable_validations=true;
                        sfm_set_focus(this.itemobj);
                        return false;
            }
            return true;
}
function ValidationSet(inputitem,msgs_together)
{
    this.vSet=new Array();
            this.add= add_validationdesc;
            this.validate= vset_validate;
            this.itemobj = inputitem;
    this.msgs_together = msgs_together;
}
function add_validationdesc(desc,error,condition)
{
  this.vSet[this.vSet.length]= 
  new ValidationDesc(this.itemobj,desc,error,condition);
}
function vset_validate()
{
    var bRet = true;
    for(var itr=0;itr<this.vSet.length;itr++)
    {
        bRet = bRet && this.vSet[itr].validate();
        if(!bRet && !this.msgs_together)
        {
            break;
        }
    }
    return bRet;
}
function TestRequiredInput(objValue,strError)
{
 var ret = true;
 var val = objValue.value;
 val = val.replace(/^\s+|\s+$/g,"");//trim
    if(eval(val.length) == 0) 
    { 
       if(!strError || strError.length ==0) 
       { 
         strError = objValue.name + " : Required Field"; 
       }//if 
       sfm_show_error_msg(strError,objValue); 
       ret=false; 
    }//if 
return ret;
}
function TestMaxLen(objValue,strMaxLen,strError)
{
 var ret = true;
    if(eval(objValue.value.length) > eval(strMaxLen)) 
    { 
      if(!strError || strError.length ==0) 
      { 
        strError = objValue.name + " : "+ strMaxLen +" characters maximum "; 
      }//if 
      sfm_show_error_msg(strError,objValue); 
      ret = false; 
    }//if 
return ret;
}
function TestMinLen(objValue,strMinLen,strError)
{
 var ret = true;
    if(eval(objValue.value.length) <  eval(strMinLen)) 
    { 
      if(!strError || strError.length ==0) 
      { 
        strError = objValue.name + " : " + strMinLen + " characters minimum  "; 
      }//if               
      sfm_show_error_msg(strError,objValue); 
      ret = false;   
    }//if 
return ret;
}
function TestInputType(objValue,strRegExp,strError,strDefaultError)
{
   var ret = true;
    var charpos = objValue.value.search(strRegExp); 
    if(objValue.value.length > 0 &&  charpos >= 0) 
    { 
              objValue.style.background="red";
     if(!strError || strError.length ==0) 
      { 
        strError = strDefaultError;
      }//if 
      sfm_show_error_msg(strError,objValue); 
      ret = false; 
    }//if 
 return ret;
}
function TestLessThan(objValue,strLessThan,strError)
{
var ret = true;
              if(isNaN(objValue.value)) 
              { 
                sfm_show_error_msg(objValue.name +": Should be a number ",objValue); 
                ret = false; 
              }//if 
              else
              if(eval(objValue.value) >=  eval(strLessThan)) 
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name + " : value should be less than "+ strLessThan; 
                }//if               
                objValue.style.background="lightblue";
                        sfm_show_error_msg(strError,objValue); 
                ret = false;                 
               }//if   
return ret;          
}
function TestGreaterThan(objValue,strGreaterThan,strError)
{
var ret = true;
     if(isNaN(objValue.value)) 
     { 
       sfm_show_error_msg(objValue.name+": Should be a number ",objValue); 
       ret = false; 
     }//if 
             else
     if(eval(objValue.value) <=  eval(strGreaterThan)) 
      { 
        if(!strError || strError.length ==0) 
        { 
          strError = objValue.name + " : value should be greater than "+ strGreaterThan; 
        }//if               
        objValue.style.background="lightblue";
                        sfm_show_error_msg(strError,objValue);  
        ret = false;
      }//if  
return ret;           
}
function CheckBtwValues(objValue,strGrtVal,strError)
{
var ret = true;
     if(isNaN(objValue.value)) 
     { 
       sfm_show_error_msg(objValue.name+": Should be a number ",objValue); 
       ret = false; 
     }//if 
             else
     if(objValue.value == '') 
      { 
                        strError = "Please enter a GPA number only.";
                        fldColor = "yello";
      }//if  
             else
     if(eval(objValue.value) >=  1 && eval(objValue.value) < 2) 
      { 
        strError = "FAIR - YOU HAVE GOT D GRADE.";
                        fldColor = "green";
      }//if  
             else
     if(eval(objValue.value) >=  2 && eval(objValue.value) < 3) 
      { 
        strError = "GOOD - YOU HAVE GOT C GRADE.";
                        fldColor = "green";
      }//if  
             else
     if(eval(objValue.value) >=  3 && eval(objValue.value) < 4) 
      { 
        strError = "VERY GOOD - YOU HAVE GOT B GRADE.";
                        fldColor = "green";
      }//if  
     if(eval(objValue.value) == 4) 
      { 
        strError = "EXCELLENT - YOU HAVE GOT A GRADE.";
                        fldColor = "green";
      }//if  
        objValue.style.background=fldColor;
                        sfm_show_error_msg(strError,objValue);  
        ret = false;        
return ret;           
}
function validateInput(strValidateStr,objValue,strError) 
{ 
    var ret = true;
    var epos = strValidateStr.search("="); 
    var  command  = ""; 
    var  cmdvalue = ""; 
    if(epos >= 0) 
    { 
     command  = strValidateStr.substring(0,epos); 
     cmdvalue = strValidateStr.substr(epos+1); 
    } 
    else 
    { 
     command = strValidateStr; 
    } 
    switch(command) 
    { 
        case "req": 
        case "required": 
         { 
                           ret = TestRequiredInput(objValue,strError)
           break;             
         }//case required 
        case "maxlength": 
        case "maxlen": 
          { 
                                     ret = TestMaxLen(objValue,cmdvalue,strError)
             break; 
          }//case maxlen 
        case "minlength": 
        case "minlen": 
           { 
                                     ret = TestMinLen(objValue,cmdvalue,strError)
             break; 
            }//case minlen 
        case "dec": 
        case "decimal": 
           { 
                ret = TestInputType(objValue,"[^0-9\.]",strError, 
                                                                        "Enter numbers only");
                break;               
           }
        case "lt": 
        case "lessthan": 
         { 
                  ret = TestLessThan(objValue,cmdvalue,strError);
              break; 
         }
        case "gt": 
        case "greaterthan": 
         { 
                                    ret = TestGreaterThan(objValue,cmdvalue,strError);
            break; 
         }//case greaterthan 
                        case "CheckGPA":
                        {
                                    ret = CheckBtwValues(objValue,strError);
                            break;
                        }                       
    }//switch 
            return ret;
}
</script>
</head>
<body>
<h2 align="center">YOUR STUDENT ID :: Assignment NUMBER</h2>
<form action="http://www.vu.edu.pk" method="post" id="cs101frm">
  <table width="800" border="1" align="center">
    <tr>
      <td width="116" height="50" align="center" bgcolor="#CCCCCC">GPA</td>
      <td width="468" bgcolor="#CCCCCC"><input name="GPA" type="text" id="GPA" size="75" /></td>
    </tr>
    <tr>
      <td height="50" bgcolor="#CCCCCC"> </td>
      <td bgcolor="#CCCCCC"><input type="submit" name="button" id="button" value="Find Grade" /></td>
    </tr>
  </table>
</form>
<script language="javaScript" type="text/javascript">
 var ValidFrm = new Validator("cs101frm");
  ValidFrm.ValAddition("GPA","decimal");
ValidFrm.ValAddition("GPA","maxlen=4","Maximum length is 4");
 ValidFrm.ValAddition("GPA","gt=0","Please enter a number which is greater than 0");
 ValidFrm.ValAddition("GPA","lt=5","Please submit number between 1 to 4 only");
 ValidFrm.ValAddition("GPA","CheckGPA");
</script> 
</body>
</html>


