
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;
}

function set_addnl_vfunction(functionname,passInValue){
  this.formobj.addnlvalidation = functionname;
  this.formobj.passInValue = passInValue;
}

function clear_all_validations(){
	for(var itr=0;itr < this.formobj.elements.length;itr++)
	{
		this.formobj.elements[itr].validationset = null;
	}
}
/*
	MODIFIED BY SUFIYAN TO ALLOW ALL ERROR MESSAGES IN ONE ALERT
*/
function form_submit_handler(){
	var allErrorMessage = "";

	for(var itr=0;itr < this.elements.length;itr++)
	{
		if(this.elements[itr].validationset) {
			allErrorMessage = this.elements[itr].validationset.validate(allErrorMessage);	
		}		  
	}
	
	if(allErrorMessage.length >0){
		alert (" The required information is incomplete or contains errors:\t\t\t\t\t\n\n" + allErrorMessage);	
		return false;
	}
	
	if(this.addnlvalidation){
	  str =" var ret = "+this.addnlvalidation+"("+this.passInValue+")";
	  
	  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];
  if(!itemobj)
	{
	  alert("BUG: Couldnot get the input object named: "+itemname);
		return;
	}
	if(!itemobj.validationset)
	{
	  itemobj.validationset = new ValidationSet(itemobj);
	}
  itemobj.validationset.add(descriptor,errstr);
}
function ValidationDesc(inputitem,desc,error)
{
  this.desc=desc;
	this.error=error;
	this.itemobj = inputitem;
	this.validate=vdesc_validate;
}
function vdesc_validate()
{
 if(!V2validateData(this.desc,this.itemobj,this.error))
 {
    this.itemobj.focus();
		return false;
 }
 return true;
}
function ValidationSet(inputitem)
{
    this.vSet=new Array();
	this.add= add_validationdesc;
	this.validate= vset_validate;
	this.itemobj = inputitem;
}
function add_validationdesc(desc,error)
{
  this.vSet[this.vSet.length]= 
	  new ValidationDesc(this.itemobj,desc,error);
}
/*
	MODIFIED BY SUFIYAN TO ALLOW ALL ERROR MESSAGES IN ONE ALERT
*/
function vset_validate(allError)
{
   for(var itr=0;itr<this.vSet.length;itr++)
	 {
	   if(!this.vSet[itr].validate())
		 {
		   allError += "*"+ this.vSet[itr].error + "\n";
		   //return false;
		 }
	 }
	// return true;
	return allError;
}
function validateEmailv2(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return true;
	}
    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,strError) 
{ 
    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 "notreq": 
        case "notrequired": 
         { 
           if(eval(objValue.value.length) > 0) 
           { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : Required Field"; 
              }//if 
              //alert(strError); 
              return false; 
           }//if 
           break;             
         }//
        case "maxlength": 
        case "maxlen": 
          { 
             if(eval(objValue.value.length) >  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : "+cmdvalue+" characters maximum "; 
               }//if 
               //alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
               return false; 
             }//if 
             break; 
          }//case maxlen 
        case "comp": 
        case "compare": 
          { 
             if((objValue.value <  cmdvalue) ||(objValue.value >  cmdvalue))
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : "+cmdvalue+" characters maximum "; 
               }//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 + " characters minimum  "; 
               }//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 characters allowed "; 
                }//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 "int": 
        case "integer": 
           { 
              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 "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 "alphabeticName": 
        case "alphaName": 
           { 
              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 "http": 
          { 
			  if(objValue.value != "" && objValue.value != "NA"){
              	 if(!validateHttpProtocolWOMsg(objValue.value)) 
               	{ 
                 	if(!strError || strError.length ==0) 
                 	{ 
                   	 strError = objValue.name+": Enter a valid Http address "; 
                	 }//if                                               
                 //alert(strError); 
                	 return false; 
               	}//if 
			  }
           break; 
          }//case email
        
        case "httpNotReq": 
          { 
             if (objValue.value != "" && objValue.value != "NA"){
                   if(!validateHttpProtocolWOMsg(objValue.value)) 
                   { 
                     if(!strError || strError.length ==0) 
                     { 
                        strError = objValue.name+": Enter a valid Http address "; 
                     }//if                                               
                     //alert(strError); 
                     return false; 
                   }//if 
               
               }
           break; 
          }//case email
          
        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 "phone": 
          { 
               if(!validatePhoneNo(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 
		    objValue.value=objValue.value*1;			
            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 "lte": 
        case "lessthanequal": 
         { 
		 	
            if(isNaN(objValue.value)) 
            { 
              //alert(objValue.name+": Should be a number "); 
              return false; 
            }//if 
			objValue.value=objValue.value*1;			
            if(eval(objValue.value) > eval(cmdvalue))
            { 
			  if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : value should be less than or equal to "+ 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 
			 objValue.value=objValue.value*1;			
             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 "gte": 
        case "greaterthanequal": 
         { 
            if(isNaN(objValue.value)) 
            { 
              //alert(objValue.name+": Should be a number "); 
              return false; 
            }//if 
			 objValue.value=objValue.value*1;
             if(eval(objValue.value) < eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : value should be greater than or equal to "+ cmdvalue; 
               }//if               
               //alert(strError); 
               return false;                 
             }//if             
            break; 
         }//case greaterthan
		 
		case "limit":
         { 
		 	if(isNaN(objValue.value)) 
            { 
              return false; 
            }//if 
			
			var limitRange = cmdvalue.split(',');
			
			if(!limitRange[1] || limitRange[0].length == 0) return false; 
			
			var limitRangeMin = limitRange[0];
			var limitRangeMax = limitRange[1];
						
            if( parseFloat(objValue.value) < parseFloat(limitRangeMin) || parseFloat(objValue.value) > parseFloat(limitRangeMax ))
            { 
			  if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : value should be less than or equal to "+ cmdvalue; 
              }//if               
              //alert(strError); 
              return false;                 
             }//if     
			 
            break; 
         }//case limit
		 
		case "limitInt":
         { 
		 
		 	if(isNaN(objValue.value)) 
            { 
              return false; 
            }//if 
			
			var limitRange = cmdvalue.split(',');
			
			if(!limitRange[1] || limitRange[0].length == 0) return false; 
			
			var limitRangeMin = limitRange[0];
			var limitRangeMax = limitRange[1];
			
			var charpos = objValue.value.search("[^0-9]"); //check int type
            if(charpos >= 0)
			{
				return false; 
			}
						
            if( parseFloat(objValue.value) < parseFloat(limitRangeMin) || parseFloat(objValue.value) > parseFloat(limitRangeMax ))
            { 
			  if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : value should be less than or equal to "+ cmdvalue; 
              }//if               
              //alert(strError); 
              return false;                 
             }//if     
			 
            break; 
         }//case limitInt    
		 
        case "regexp": 
         { 
		 	if(objValue.value.length > 0)
			{
	            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 "ip": 
         { 
		 	if(objValue.value != "" && objValue.value != "NA")
			{
			IPvalue = objValue.value;
		    errorString = "";
			theName = "IPaddress";
			
			var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
			var ipArray = IPvalue.match(ipPattern); 
			
			if (IPvalue == "0.0.0.0")
			errorString = 'invalid';

			if (ipArray == null)
			errorString = 'invalid';
			else {
			for (i = 0; i < 4; i++) {
			thisSegment = ipArray[i];
			if (thisSegment > 255) {
			errorString = 'invalid';
			i = 4;
			}
			if ((i == 0) && (thisSegment > 255)) {
			errorString = 'invalid';
			i = 4;
				  }
			   }
			}
			extensionLength = 3;
			if (errorString == "invalid"){ return false;	}
			}
             break; 
         }//case ip 
		 case "iplist": 
         { 
		 	if(objValue.value != "" && objValue.value != "NA")
			{
			IPvalue = objValue.value;
		    errorString = "";
			theName = "IPaddress";

			tmpIPvalue = IPvalue.split(",")
			for(var x=0; x<tmpIPvalue.length; x++)
			{					
				var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
				var ipArray = tmpIPvalue[x].match(ipPattern); 
				
				if (IPvalue == "0.0.0.0")
				errorString = 'invalid';
	
				if (ipArray == null)
				errorString = 'invalid';
				else {
				for (i = 0; i < 4; i++) {
				thisSegment = ipArray[i];
				if (thisSegment > 255) {
				errorString = 'invalid';
				i = 4;
				}
				if ((i == 0) && (thisSegment > 255)) {
				errorString = 'invalid';
				i = 4;
					  }
				   }
				}
			}
			extensionLength = 3;
			if (errorString == "invalid"){ return false;	}
			}
             break; 
         }//case iplist 		 
		 
    }//switch 
    return true; 
}
/*
	Applied by sufiyan
*/
function validateHttpProtocol(value){
	var pattern = /^https?:\/\/.+.*/i;
	
	var result = pattern.exec(value,"i");	

	if( result != null){
		return true;
	}else{		
		alert("Invalid http protocol. Please apply the following format \n http://<url address>\n example : http://www.google.com or https://www.google.com.");
		return false;
		
	}
}

/*
	Applied by sufiyan
*/
function validateHttpProtocolWOMsg(value){
	var pattern = /^https?:\/\/.+.*/i;
	
	var result = pattern.exec(value,"i");	

	if( result != null){
		return true;
	}else{				
		return false;
		
	}
}

/*
	Applied by sufiyan
*/  
function compareWthMsg(str1, str2,errormsg){
	
	if (compare(str1, str2) ==false)  {
		
        return false;
    }else {
    	
       return true;
    }
}

/*
	Applied by sufiyan
*/  
function compare(str1, str2){
	
	if (( str1 < str2 ) ||( str1 > str2 ))  {
		
        return false;
    }else {    	
       return true;
    }
}

/*
	Applied by sufiyan
*/
function validatePhoneNo(str1){	
	
	var pattern = /[^0-9\-\+]/i;
	
	var result = pattern.exec(str1,"i");	
	
	if( result != null){
		
		return false;
	}else{				
		return true;		
	}
}


/*
	Applied by sufiyan
*/
function validateAlpha(str1){	
	
	var pattern = /[^A-Za-z]/i;
	
	var result = pattern.exec(str1,"i");	
	
	if( result != null){		
		return false;
	}else{				
		return true;		
	}
}

/*
	Applied by sufiyan
*/
function validateAlphaNumeric(str1){	
	
	var pattern = /[^A-Za-z0-9]/i;
	
	var result = pattern.exec(str1,"i");	
	
	if( result != null){		
		return false;
	}else{				
		return true;		
	}
}

/*
	Applied by sufiyan
*/
function validateNumeric(str1){	
	
	var pattern = /[^0-9\.]/i;
	
	var result = pattern.exec(str1,"i");	
	
	if( result != null){		
		return false;
	}else{				
		return true;		
	}
}

/*
	Applied by sufiyan
*/  
function isLessThan(value1, value2){
	
	if ( value1 <= value2 )   {		
        return true;
    }else {    	
       return false;
    }
}

/*
	Applied by sufiyan
*/  
function isLessThanNumeric(value1, value2){
	return isLessThan(parseFloat(value1) , parseFloat(value2));	
}

/*
	Applied by sufiyan
*/  
function isPercentage(value1){
	
	if ((isLessThanNumeric(value1,100)== true) && (isLessThanNumeric(-1,value1)==true)){
	   return true;
    }else {    	
       return false;
    }
}

/*
	Applied by hernae
*/
function validateTime(str1){	
	
	var pattern = /^[0-9]+:[0-9]+ [aApP]+[mM]+$/i;
	
	var result = pattern.exec(str1,"i");	
	
	if( result != null){		
		return false;
	}else{				
		return true;		
	}
}

/*
	Copyright 2003 JavaScript-coder.com. All rights reserved.
*/
