/*
  -------------------------------------------------------------------------
	                    JavaScript Form Validator
                                Version 2.0.2
	Copyright 2003 JavaScript-coder.com. All rights reserved.
	You use this script in your Web pages, provided these opening credit
    lines are kept intact.
	The Form validation script is distributed free from JavaScript-Coder.com

	You may please add a link to JavaScript-Coder.com,
	making it easy for others to find this script.
	Checkout the Give a link and Get a link page:
	http://www.javascript-coder.com/links/how-to-link.php

    You may not reprint or redistribute this code without permission from
    JavaScript-Coder.com.

	JavaScript Coder
	It precisely codes what you imagine!
	Grab your copy here:
		http://www.javascript-coder.com/
    -------------------------------------------------------------------------
*/
function Validator(frmname)
{
  this.formobj=document.forms[frmname];
	if(!this.formobj)
	{
	  alert("BUG: couldnot 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;
	this.addValidation = add_validation;
	this.setAddnlValidationFunction=set_addnl_vfunction;
	this.clearAllValidations = clear_all_validations;
        this.formobj.clearAllValidations = clear_all_validations;
}
function set_addnl_vfunction(functionname)
{
  this.formobj.addnlvalidation = functionname;
}
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++)	{
          var elm = this.elements[itr];
          //validate radio group
          if (/radio/i.test(elm.type)){
            if (this[elm.name].validationset && !this[elm.name].validationset.validate()) {
		  return false;
            }
          }
         if(elm.validationset && !elm.validationset.validate()) {
		  return false;
         }
	}
	if(this.addnlvalidation)
	{
	  str =" var ret = "+this.addnlvalidation+"()";
	  eval(str);
    if(!ret) return ret;
	}
	return true;
}
function add_validation(itemname,descriptor,errstr)
{
  if(!this.formobj)
	{
	  alert("BUG: the form object is not set properly");
		return;
	}//if
  var itemobj = this.formobj[itemname];
//bugfix  Stan
//  if (typeof(itemobj.length) == 'number' && itemobj.type!='select-one' ) {
//   itemobj=itemobj[0];
//  }
  if (!itemobj) {
    if(itemname.indexOf('[') != -1){
       var the_field_name = itemname.substring(0, itemname.indexOf('['));
       var the_field_index = itemname.substring((itemname.indexOf('[')+1), itemname.indexOf(']'));
       itemobj=this.formobj[the_field_name][the_field_index];
    }
  }
  if(!itemobj)
	{
	  alert("BUG: Couldnot get the input object named: "+itemname);
		return;
	}
	if(!itemobj.validationset)
	{
	  itemobj.validationset = new ValidationSet(itemobj,this);
	}
  itemobj.validationset.add(descriptor,errstr);
}
function ValidationDesc(inputitem,desc,error,validator)
{
  this.desc=desc;
	this.error=error;
	this.itemobj = inputitem;
	this.validate=vdesc_validate;
        this.topValidator=validator;
}
function vdesc_validate()
{
 if(!V2validateData(this.desc,this.itemobj,this.error))
 {
   this.topValidator.clearAllValidations();
   if (typeof(this.itemobj.focus) != "undefined"){
     this.itemobj.focus();
   } else {
     this.itemobj[0].focus();
   }
   return false;
 }
 return true;
}
function ValidationSet(inputitem, validator)
{
    this.vSet=new Array();
	this.add= add_validationdesc;
	this.validate= vset_validate;
	this.itemobj = inputitem;
        this.setValidator=validator;
}
function add_validationdesc(desc,error)
{

  this.vSet[this.vSet.length]=
	  new ValidationDesc(this.itemobj,desc,error,this.setValidator);
}
function vset_validate()
{
   for(var itr=0;itr<this.vSet.length;itr++)
	 {

	   if(!this.vSet[itr].validate())
		 {
		   return false;
		 }
	 }
	 return true;
}
function validateEmailv2(email)
{
// a very simple email validation checking.
// you can add more complex email checking if it helps
    if(email.length <= 0)
	{
	  return false;
	}
	
    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(strValidateStr,objValue,objName)
{
    var epos = strValidateStr.search("=");
    var  command  = "";
    var  cmdvalue = "";
    var theValue = getFormFieldValue(objValue); //getFieldValue(objValue.name, objValue.form.name);
    if(epos >= 0)
    {
     command  = strValidateStr.substring(0,epos);
     cmdvalue = strValidateStr.substr(epos+1);
    }
    else
    {
     command = strValidateStr;
    }
    switch(command)
    {
        case "req":
        case "required":
         {
           if(eval(theValue.length) == 0)
           {
           	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
              alert(objName + " : Required Field");
              }
              return false;
           }//if
           break;
         }//case required
        case "maxlength":
        case "maxlen":
          {
             if(eval(theValue.length) >  eval(cmdvalue))
             {
                        	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
               alert(objName + " : "+cmdvalue+" characters maximum " + "\n[Current length = " + theValue.length + " ]");
              }
               return false;
             }//if
             break;
          }//case maxlen
        case "minlength":
        case "minlen":
           {
             if(eval(theValue.length) <  eval(cmdvalue))
             {
                        	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
               alert(objName + " : " + cmdvalue + " characters minimum  " + "\n[Current length = " + theValue.length + " ]");
              }
               return false;
             }//if
             break;
            }//case minlen
        case "alnum":
        case "alphanumeric":
           {
              var charpos = theValue.search("[^A-Za-z0-9]");
              if(theValue.length > 0 &&  charpos >= 0)
              {
                          	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
                alert(objName+": Only alpha-numeric characters allowed " + "\n [Error character position " + eval(charpos+1)+"]");
                }
                return false;
              }//if
              break;
           }//case alphanumeric
        case "num":
        case "numeric":
           {
              var charpos = theValue.search("[^0-9]");
              if(theValue.length > 0 &&  charpos >= 0)
              {
                           	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
                alert(objName+": Only digits allowed " + "\n [Error character position " + eval(charpos+1)+"]");
               }
				return false;
              }//if
              break;
           } //numeric
        case "decimal99x00": //decimal less than 100 ending with .00
        case "dec99x00":
           {
   	  	if ( theValue.match("^[0-9]{1,2}\\.[0]{2}$")==null){
          	            	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
          	 alert(objName+": Decimal format of (X)X.00 expected. ");
          	 }
          	 return false;
          	}//if
                break;
        }  //decimal2x2
	case "decimalx99": //decimal less than 100 ending with .00
        case "decx99":
           {
   	  	if ( theValue.match("^[0-9]{1,9}\\.[0-9]{2}$")==null){
          	 alert(objName+": Decimal format of (X)X.XX expected. ");
          	 return false;
          	}//if
                break;
        }  //decimal2x2
        case "alphabetic":
        case "alpha":
           {
              var charpos = theValue.search("[^A-Za-z]");
              if(theValue.length > 0 &&  charpos >= 0)
              {
              
                         	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
                alert(objName+": Only alphabetic characters allowed " + "\n [Error character position " + eval(charpos+1)+"]");
               }
                return false;
              }//if
              break;
           }//alpha
		case "alnumhyphen":
			{
              var charpos = theValue.search("[^A-Za-z0-9\-_]");
              if(theValue.length > 0 &&  charpos >= 0)
              {
              
              if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
                alert(objName+": characters allowed are A-Z,a-z,0-9,- and _" + "\n [Error character position " + eval(charpos+1)+"]");
                }
                return false;
              }//if
			break;
			}
        case "email":
          {
               if(!validateEmailv2(theValue))
               {
                          	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
                 alert(objName+": Enter a valid Email address ");
                 }
                 return false;
               }//if
           break;
          }//case email
 	case "same":
          {
               if(theValue!=getFieldValue(cmdvalue))
               {
                          	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
                 alert(objName+": must be exactly the same. ");
                 }
                 return false;
               }//if
           break;
          }//case email
        case "lt":
        case "lessthan":
         {
            if(isNaN(theValue))
            {
                       	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
              alert(objName+": Should be a number ");
              }
              return false;
            }//if
            if(eval(theValue) >=  eval(cmdvalue))
            {
                       	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
            
              alert(objName + " : value should be less than "+ cmdvalue);
              }
              return false;
             }//if
            break;
         }//case lessthan
        case "gt":
        case "greaterthan":
         {
            if(isNaN(theValue))
            {
              alert(objName+": Should be a number ");
              return false;
            }//if
             if(eval(theValue) <=  eval(cmdvalue))
             {
               alert(objName + " : value should be greater than "+ cmdvalue);
               return false;
             }//if
            break;
         }//case greaterthan
        case "regexp":
         {
		 	if(theValue.length > 0)
			{
	            if(!theValue.match(cmdvalue))
	            {
	              alert(objName+": Invalid characters found ");
	              return false;
	            }//if
			}
           break;
         }//case regexp
        case "dontselect":
         {
            if(objValue.selectedIndex == null)
            {
				alert(objValue.name);
				alert("BUG: dontselect command for non-select Item");
              return false;
            }
            if(objValue.selectedIndex == eval(cmdvalue))
            {
                       	  if (objName.length > 0) {	
           	   alert(objName);
           	  } else {
              alert(objName+": Please Select one option ");
              }
              return false;
             }
             break;
         }//case dontselect
    }//switch
    return true;
}
/*
	Copyright 2003 JavaScript-coder.com. All rights reserved.
*/
