//validation class starts
var	arr_image=Array('jpg','JPG','gif','GIF','png','PNG');
var	arr_video=Array('mpeg','MPEG');
var	arr_flash=Array('SWF','swf');
var	arr_avi=Array('avi','avi');


function Validator(frmname)
{
	this.formobj=document.forms[frmname];
	this.arr_itemobj=Array();
	this.arr_itemobj=Array();
	this.arr_descriptor=Array();
	this.arr_errstr=Array();
	this.status=true;
	
	if(!this.formobj)
	{
	alert("BUG: couldnot get Form object "+frmname);
	return false;
	}
	this.addValidation = add_validation;
	this.validate = form_validate;
}

function add_validation(itemname,descriptor,errstr)
{
/*	var itemobj = this.formobj[itemname];
	if(!itemobj)
	{
		alert("BUG: Couldnot get the input object named: "+itemname);
		return false;
	}
*/
	var itemobj =document.getElementById(itemname);
	if(!itemobj)
	{
		alert("BUG: Couldnot get the input object having id: "+itemname);
		return false;
	}
	this.arr_itemobj.push(itemobj);
	this.arr_descriptor.push(descriptor);
	this.arr_errstr.push(errstr);
}

function form_validate()
{
	for(i=0;i<this.arr_itemobj.length;i++)
	{
		var result=V2validateData(this.arr_descriptor[i],this.arr_itemobj[i],this.arr_errstr[i]);
		if(result==false)
		{
			this.status=false;
			return false;
		}
	}
	return true;
}

 
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; 
    } 
	hide_error(objValue.name)
    switch(command) 
    { 
        case "req": 
        case "required": 
         { 
           if(eval(trim(objValue.value).length) == 0) 
           { 
              if(!strError || strError.length ==0) 
              {
                strError = objValue.name + " : Required Field"; 
              }//if 
			  show_error(objValue.id,strError);
              return false; 
           }//if 
           break;             
         }//case required 
        case "maxlength": 
        case "maxlen": 
          { 
             if(eval(trim(objValue.value).length) >  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : "+cmdvalue+" characters maximum "; 
               }//if 
               strError += "\n[Current length = " + objValue.value.length + " ]"; 
			   show_error(objValue.id,strError)
               return false; 
             }//if 
             break; 
          }//case maxlen 
        case "minlength": 
        case "minlen": 
           { 
             if(eval(trim(objValue.value).length) <  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : " + cmdvalue + " characters minimum  "; 
               }//if               
               strError += "\n[Current length = " + objValue.value.length + " ]"; 
			   show_error(objValue.id,strError)
               return false;                 
             }//if 
             break; 
            }//case minlen 
        case "alnum": 
        case "alphanumeric": 
           { 
              var charpos = trim(objValue.value).search("[^A-Za-z0-9]"); 
              if(trim(objValue.value).length > 0 &&  charpos >= 0) 
              { 
               if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alpha-numeric characters allowed "; 
                }//if 
//               strError += "\n [Error character position " + eval(charpos+1)+"]"; 
			   show_error(objValue.id,strError)
                return false; 
              }//if 
              break; 
           }//case alphanumeric 
        case "num": 
        case "numeric": 
           { 
              var charpos = trim(objValue.value).search("[^0-9]"); 
              if(trim(objValue.value).length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only digits allowed "; 
                }//if               
//               strError += "\n [Error character position " + eval(charpos+1)+"]"; 
			   show_error(objValue.id,strError)
                return false; 
              }//if 
              break;               
           }//numeric 
        case "alphabetic": 
        case "alpha": 
           { 
              var charpos = trim(objValue.value).search("[^A-Za-z]"); 
              if(trim(objValue.value).length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alphabetic characters allowed "; 
                }//if                             
               strError += "\n [Error character position " + eval(charpos+1)+"]"; 
			   show_error(objValue.id,strError)
                return false; 
              }//if 
              break; 
           }//alpha 
		case "alnumhyphen":
			{
              var charpos = trim(objValue.value).search("[^A-Za-z0-9\-_]"); 
              if(trim(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                             
               strError += "\n [Error character position " + eval(charpos+1)+"]"; 
			   show_error(objValue.id,strError)
                return false; 
              }//if 			
			break;
			}
        case "email": 
          { 
               if(!validateEmailv(trim(objValue.value))) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError = objValue.name+": Enter a valid Email address "; 
                 }//if                                               
			   show_error(objValue.id,strError)
                 return false; 
               }//if 
           break; 
          }//case email 
        case "lt": 
        case "lessthan": 
         { 
            if(isNaN(trim(objValue.value)))
            { 
              alert(objValue.name+": Should be a number "); 
              return false; 
            }//if 
            if(eval(trim(objValue.value)) >=  eval(cmdvalue)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : value should be less than "+ cmdvalue; 
              }//if               
			   show_error(objValue.id,strError)
              return false;                 
             }//if             
            break; 
         }//case lessthan 
        case "gt": 
        case "greaterthan": 
         { 
            if(isNaN(trim(objValue.value))) 
            { 
              strError=objValue.name+": Should be a number "; 
			   show_error(objValue.id,strError)
              return false; 
            }//if 
             if(eval(trim(objValue.value)) <=  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : value should be greater than "+ cmdvalue; 
               }//if               
			   show_error(objValue.id,strError)
               return false;                 
             }//if             
            break; 
         }//case greaterthan 
        case "regexp": 
         { 
		 	if(trim(objValue.value).length > 0)
			{
	            if(!objValue.value.match(cmdvalue)) 
	            { 
	              if(!strError || strError.length ==0) 
	              { 
	                strError = objValue.name+": Invalid characters found "; 
	              }//if                                                               
			   show_error(objValue.id,strError)
	              return false;                   
	            }//if 
			}
           break; 
         }//case regexp 
        case "dontselect": 
         { 
            if(objValue.selectedIndex == null) 
            { 
              strError="BUG: dontselect command for non-select Item"; 
			   show_error(objValue.id,strError)
              return false; 
            } 
            if(objValue.selectedIndex == eval(cmdvalue)) 
            { 
             if(!strError || strError.length ==0) 
              { 
              strError = objValue.name+": Please Select one option "; 
              }//if                                                               
			   show_error(objValue.id,strError)
              return false;                                   
             } 
             break; 
         }//case dontselect 
        case "img": 
        case "image": 
         { 
            if(trim(objValue.value)=='') 
            { 
              strError=objValue.name+": Invalid "; 
			   show_error(objValue.id,strError)
              return false; 
            }//if
			var image_type=arr_image;
			var image_valid=false;
			for(image_i=0;image_i<image_type.length;image_i++)
			{
				if(image_valid==false && trim(objValue.value).lastIndexOf('.'+image_type[image_i]) > 0)
				{
					image_valid=true;
				}
			}
             if(image_valid == false) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : Invalid Image Type ";
               }//if               
			   show_error(objValue.id,strError)
               return false;                 
             }//if  
            break; 
         }//case image 
        case "video": 
         { 
            if(trim(objValue.value)=='') 
            { 
              strError=objValue.name+": Invalid "; 
			   show_error(objValue.id,strError)
              return false; 
            }//if
			var video_type=arr_video;
			var video_valid=false;
			for(video_i=0;video_i<video_type.length;video_i++)
			{
				if(video_valid==false && trim(objValue.value).lastIndexOf('.'+arr_video[video_i]) > 0)
				{
					video_valid=true;
				}
			}
             if(video_valid == false) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : Invalid Video Type ";; 
               }//if               
			   show_error(objValue.id,strError)
               return false;                 
             }//if             
            break; 
         }//case video 
        case "flash": 
         { 
            if(trim(objValue.value)=='') 
            { 
              strError=objValue.name+": Invalid "; 
			   show_error(objValue.id,strError)
              return false; 
            }//if
			var flash_type=arr_flash;
			var flash_valid=false;
			
			for(i=0;i<flash_type.length;i++)
			{
				if(flash_valid==false && trim(objValue.value).lastIndexOf('.'+arr_flash[i]) > 0)
				{
					flash_valid=true;
				}
			}
             if(flash_valid == false) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : Invalid Flash Type ";; 
               }//if               
			   show_error(objValue.id,strError)
               return false;                 
             }//if             
            break; 
         }//case video 
		 
	  case "avi":
	  		{	 
            if(trim(objValue.value)=='') 
            { 
              strError=objValue.name+": Invalid "; 
			   show_error(objValue.id,strError)
              return false; 
            }//if
			var avi_type=arr_avi;
			var avi_valid=false;
			
			for(i=0;i<avi_type.length;i++)
			{
				if(flash_valid==false && trim(objValue.value).lastIndexOf('.'+arr_avi[i]) > 0)
				{
					avi_valid=true;
				}
			}
			
             if(avi_valid == false) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : Invalid AVI Type ";; 
               }//if               
			   show_error(objValue.id,strError)
               return false;                 
             }//if             
            break; 
			}// end of avi video
	  
    }//switch 
}

function trim(stringToTrim) {
	
	return stringToTrim.replace(/^\s+|\s+$/g,'');
}

function validateEmailv(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 show_error(field,error)
{ 
	if(document.getElementById("err_"+field))
	document.getElementById("err_"+field).innerHTML=error;
	else
	alert(error);
	document.getElementById(field).focus();
}

function hide_error(field)
{
	if(document.getElementById("err_"+field))
	document.getElementById("err_"+field).innerHTML="";
}

//validation class ends

