//<script language='JavaScript' type="text/javascript">
<!--
//alert('validatorscript up')
/*
*  The Validator
*   The class that handles all validation related issues
*
*   pass the name of the form while constructing.
*   methods:
*    addValidation(input_item_name,validation_descriptor,error_string)
		or
*    addValidation(input_item_name,validation_descriptor,error_string,field_to_check_also)
*       call this method for each input item. Single input item can have
*       many validations
*
*    setAddnlValidationFunction(function_name)
*             call this function to set a custom validat function, which
will
*       be called after other validations are over.
*          The function should return 'true' or 'false'
*/
//alert('validator2.js is running');

function Validator(frmname)
  {
this.formobj=document.forms[frmname];
//preserve the form's name in a seperate property in case one of the input fields is also named "name"
this.formobj.formName=frmname;
 if(!this.formobj)
 {
   alert("BUG: could not get Form object "+frmname);
  return;
 }
 if(this.formobj.onsubmit)
 {
  this.formobj.old_onsubmit = this.formobj.onsubmit;
  this.formobj.onsubmit=null;
 }
 else
 {
  this.formobj.old_onsubmit = null;
 }
 this.formobj.onsubmit=form_submit_handler;
 //special handler to invoke validator
 this.formobj.validateNow=form_submit_handler;
 //end special handler
 this.addValidation = add_validation;
 this.setAddnlValidationFunction=set_addnl_vfunction;
 this.clearAllValidations = clear_all_validations;
}

function set_addnl_vfunction(functionname, params)
{
  //alert('additional params='+params);
  this.formobj.addnlvalidation = functionname;
  this.formobj.addnlvalparams = params;
}
function clear_all_validations()
{
 for(var itr=0;itr < this.formobj.elements.length;itr++)
 {
  this.formobj.elements[itr].validationset = null;
 }
}
function form_submit_handler()
{
 for(var itr=0;itr < this.elements.length;itr++)
 {
  if(this.elements[itr].validationset &&
    !this.elements[itr].validationset.validate())
  {
    return false;
  }
 }
 if(this.addnlvalidation)
 {
   //alert("this.addnlvalparams="+this.addnlvalparams);
   str =" var ret = "+this.addnlvalidation+"('"+this.addnlvalparams+"')";
   eval(str);
    if(!ret) return ret;
 }
 return true;
}

function add_validation(itemname,descriptor,errstr,fieldname){
	if(!this.formobj){
		alert("BUG: the form object is not set properly");
  		return;
 	}//if
	if(descriptor=="radioGroup"){
	//alert('found radioGroup named '+ itemname)
	//alert('this.formobj.formName= '+ this.formobj.formName)
	//alert('radioGroup button 1 name= '+ this.formobj[itemname][0].name)
 		//treat the formObj differently if we are validating a range of radio buttons
		//use the first radio button in the array of radio buttons to hold the validation info for the entire set
	 	this.formobj[itemname][0].formName=this.formobj.formName;
	 	var itemobj = this.formobj[itemname][0];
 	}else{
	 	var itemobj = this.formobj[itemname];
	 	var fieldobj = this.formobj[fieldname];
 	}
 	if(!itemobj){
   		alert("BUG: Could not get the input object named: "+itemname);
  		return;
 	}
	 /* NOTE- dont use this because there will not always be a fieldobj
	  if(!fieldobj){
   		alert("BUG: Couldnot get the input field object named: "+fieldname);
  		return;
 	  }
    */
	 if(!itemobj.validationset){
		itemobj.validationset = new ValidationSet(itemobj,fieldobj);
	 }
	 itemobj.validationset.add(descriptor,errstr);
}

function ValidationDesc(inputitem,desc,error,fielditem)
{
  this.desc=desc;
 this.error=error;
 this.itemobj = inputitem;
 this.validate=vdesc_validate;
 this.fieldobj=fielditem;
}

function vdesc_validate()
{
 if(!V2validateData(this.desc,this.itemobj,this.error,this.fieldobj))
 {
 if(this.fieldobj) {
      this.fieldobj.focus();
 }else{
      this.itemobj.focus();
 }
  return false;
 }
 return true;
}


function ValidationSet(inputitem,fielditem)
{
  this.vSet=new Array();
 this.add= add_validationdesc;
 this.validate= vset_validate;
 this.itemobj = inputitem;
 this.fieldobj = fielditem;
}
function add_validationdesc(desc,error)
{
  this.vSet[this.vSet.length]=
   new ValidationDesc(this.itemobj,desc,error,this.fieldobj);
}
function vset_validate()
{
   for(var itr=0;itr<this.vSet.length;itr++)
  {
    if(!this.vSet[itr].validate())
   {
     return false;
   }
  }
  return true;
}

//---------------------------------EMailCheck ------------------------------------ 

/*  checks the validity of an email address entered
*   returns true or false
*
*/

function validateEmailv2(email)
{
// a very simple email validation checking.
// you can add more complex email checking if it helps
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null)
      {
     var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
     if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

/* function V2validateData
*  Checks each field in a form

*/
function V2validateData(strValidateStr,objValue,strError,fieldValue)
{
    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":
         {
           if(eval(objValue.value.length) == 0)
           {
              if(!strError || strError.length ==0)
              {
                strError = objValue.name + " : Required Field";
              }//if
              alert(strError);
              return false;
           }//if
           break;
         }//case required
        case "maxlength":
        case "maxlen":
          {
             if(eval(objValue.value.length) >  eval(cmdvalue))
             {
               if(!strError || strError.length ==0)
               {
                 strError = objValue.name + " : "+cmdvalue+" charactersmaximum ";
               }//if
               alert(strError + "\n[Current length = " + objValue.value.length + " ]");
               return false;
             }//if
             break;
          }//case maxlen
        case "minlength":
        case "minlen":
           {
             if(eval(objValue.value.length) <  eval(cmdvalue))
             {
               if(!strError || strError.length ==0)
               {
                 strError = objValue.name + " : " + cmdvalue + " charactersminimum  ";
               }//if
               alert(strError + "\n[Current length = " + objValue.value.length + " ]");
               return false;
             }//if
             break;
            }//case minlen
        case "alnum":
        case "alphanumeric":
           {
              var charpos = objValue.value.search("[^A-Za-z0-9]");
              if(objValue.value.length > 0 &&  charpos >= 0)
              {
               if(!strError || strError.length ==0)
                {
                  strError = objValue.name+": Only alpha-numeric charactersallowed ";
                }//if
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
                return false;
              }//if
              break;
           }//case alphanumeric
        case "num":
        case "numeric":
           {
              var charpos = objValue.value.search("[^0-9]");
              if(objValue.value.length > 0 &&  charpos >= 0)
              {
                if(!strError || strError.length ==0)
                {
                  strError = objValue.name+": Only digits allowed ";
                }//if
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
                return false;
              }//if
              break;
           }//numeric
        case "float":
           {
              var charpos = objValue.value.search("[^0-9\.]");
              if(objValue.value.length > 0 &&  charpos >= 0)
              {
                if(!strError || strError.length ==0)
                {
                  strError = objValue.name+": Only digits and periods allowed ";
                }//if
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
                return false;
              }//if
              break;
           }//float
        case "alphabetic":
        case "alpha":
           {
              var charpos = objValue.value.search("[^A-Za-z]");
              if(objValue.value.length > 0 &&  charpos >= 0)
              {
                  if(!strError || strError.length ==0)
                {
                  strError = objValue.name+": Only alphabetic characters allowed ";
                }//if
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
                return false;
              }//if
              break;
           }//alpha
  case "alnumhyphen":
   {
              var charpos = objValue.value.search("[^A-Za-z0-9\-_]");
              if(objValue.value.length > 0 &&  charpos >= 0)
              {
                  if(!strError || strError.length ==0)
                {
                  strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _";
                }//if
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
                return false;
              }//if
   break;
   }
        case "email":
          {
               if(!validateEmailv2(objValue.value))
               {
                 if(!strError || strError.length ==0)
                 {
                    strError = objValue.name+": Enter a valid Email address ";
                 }//if
                 alert(strError);
                 return false;
               }//if
           break;
          }//case email
        case "lt":
        case "lessthan":
         {
            if(isNaN(objValue.value))
            {
              alert(objValue.name+": Should be a number ");
              return false;
            }//if
            if(eval(objValue.value) >=  eval(cmdvalue))
            {
              if(!strError || strError.length ==0)
              {
                strError = objValue.name + " : value should be less than "+ cmdvalue;
              }//if
              alert(strError);
              return false;
             }//if
            break;
         }//case lessthan
        case "gt":
        case "greaterthan":
         {
            if(isNaN(objValue.value))
            {
              alert(objValue.name+": Should be a number ");
              return false;
            }//if
             if(eval(objValue.value) <=  eval(cmdvalue))
             {
               if(!strError || strError.length ==0)
               {
                 strError = objValue.name + " : value should be greater than "+ cmdvalue;
               }//if
               alert(strError);
               return false;
             }//if
            break;
         }//case greaterthan
        case "regexp":
         {
            if(!objValue.value.match(cmdvalue))
            {
              if(!strError || strError.length ==0)
              {
                strError = objValue.name+": Invalid characters found ";
              }//if
              alert(strError);
              return false;
            }//if
           break;
         }//case regexp
        case "dontselect":
         {
            if(objValue.selectedIndex == null)
            {
              alert("BUG: dontselect command for non-select Item");
              return false;
            }
            if(objValue.selectedIndex == eval(cmdvalue))
            {
             if(!strError || strError.length ==0)
              {
              strError = objValue.name+": Please Select one option ";
              }//if
              alert(strError);
              return false;
             }
             break;
         }//case dontselect
    //====
  case "checked":
      {
          if (!objValue.checked)
        {
    if(!strError || strError.length ==0)
          {
           strError = objValue.name + " : You must check this box";
        }
          alert(strError);
          return false;
         }
         break;
      }//case checked

  case "checkedAndField":
  //alert('in checkedAndField')
  //alert('objValue= ' + objValue)
  //alert('fieldValue= ' + fieldValue)
    if (objValue.checked) {
    	if (!fieldValue.value) {
     		if(!strError || strError.length ==0) {
            	strError = objValue.name + " : You must specify a date for the class";
          	}
            alert(strError);
            return false;
        }
   	 }
     break;

  case "fieldAndChecked":
  //alert('in fieldAndCheck')
  //alert('objValue= ' + objValue)
  //alert('fieldValue= ' + fieldValue)
     if (fieldValue.value) {
     	if (!objValue.checked) {
     		if(!strError || strError.length ==0) {
             strError = objValue.name + " : You must check te box next to your chosen date";
          	}
            alert(strError);
            return false;
        }
   	  }
      break;

  case "radioGroup":
  		//alert('in radioGroup')
		//alert(document.forms[objValue.formName][objValue.name])
		//alert(document.forms[objValue.formName][objValue.name].length)
  		//reset the objValue to represent all of the buttons in the radio group, not just the first button which is storing the validation code
		checkFound=0
		radioSet=document.forms[objValue.formName][objValue.name]
		for(inc=0;inc<radioSet.length;inc++){
			if(radioSet[inc].checked){
				checkFound=1
			}
		}
		//now alert if no check was found in this radio group
		if(!checkFound){
			if(!strError||strError.length==0){
				strError=objValue.name+" : You must select a radio button"
			}
			alert(strError)
			return false
		}
      	break;
	  
 }//switch
    return true;
}

//-->

