/*
  $Id: validator.js 28080 2007-06-27 15:36:45Z ageers $
  generic form validator
  
*/
 

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.onsubmit=form_submit_handler;this.addValidation=add_validation;this.setListLength=set_listlen;}
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;}}
return true;}
function add_validation(itemname,descriptor,errstr){var itemobj=this.formobj[itemname];if(!itemobj.validationset){itemobj.validationset=new ValidationSet(itemobj);}
itemobj.validationset.add(descriptor,errstr);}
function set_listlen(itemname,listCount){var itemobj=this.formobj[itemname];if(!itemobj.validationset){itemobj.validationset=new ValidationSet(itemobj);}
itemobj.validationset.setListLength(listCount);}
function htmlalert(itemobj,errstr){var messgobj=document.createElement("div");messgobj.setAttribute("name","errstr_"+itemobj.name);messgobj.setAttribute("id","errstr_"+itemobj.name);messgobj.setAttribute("class","errstr");messgobj.setAttribute("className","errstr");messgobj.innerHTML=errstr;try{var el=document.getElementsByTagName("div");var re=new RegExp('errstr');for(var i=0;i<el.length;i++){var getID=el[i].getAttribute("id");if(re.exec(getID)!=null){var el=document.getElementById(getID);el.parentNode.removeChild(el);}}}
catch(e){}
itemobj.parentNode.appendChild(messgobj,itemobj);}
function ValidationDesc(inputitem,desc,error)
{this.desc=desc;this.error=error;this.itemobj=inputitem;this.validate=vdesc_validate;}
function vdesc_validate(listCount)
{if(!V2validateData(this.desc,this.itemobj,this.error,listCount))
{this.itemobj.focus();return false;}
return true;}
function ValidationSet(inputitem)
{this.vSet=new Array();this.add=add_validationdesc;this.setListLength=set_validationlist_len;this.validate=vset_validate;this.itemobj=inputitem;}
function add_validationdesc(desc,error)
{this.vSet[this.vSet.length]=new ValidationDesc(this.itemobj,desc,error);}
function set_validationlist_len(listCount)
{this.listCount=listCount;}
function vset_validate()
{for(var itr=0;itr<this.vSet.length;itr++)
{if(!this.vSet[itr].validate(this.listCount))
{return false;}}
return true;}
function validateEmailv2(email)
{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;}
return true;}
return false;}
function trim(val){return val.replace(/^[\s\t]*/,"").replace(/[\s\t]*$/,"");}
function V2validateData(strValidateStr,objValue,strError,listCount)
{var epos=strValidateStr.search("=");var command="";var cmdvalue="";if(epos>=0)
{command=strValidateStr.substring(0,epos);cmdvalue=strValidateStr.substr(epos+1);}
else
{command=strValidateStr;}
if(command!="dontselect"){var valueList;if(listCount){var itemList=objValue.value.replace(/;/g,',');valueList=itemList.split(',');if(valueList.length>listCount){htmlalert(objValue,"Please supply no more than "+listCount+"\n[Currently "+valueList.length+" ]");return false;}
for(var i=0;i<valueList.length;i++){valueList[i]=trim(valueList[i]);}}else{valueList=[objValue.value];}
for(var i=0;i<valueList.length;i++)
{var val=valueList[i];switch(command)
{case"req":case"required":{if(eval(val.length)==0)
{if(!strError||strError.length==0)
{strError=objValue.name+" : Required Field";}
htmlalert(objValue,strError);return false;}
break;}
case"maxlength":case"maxlen":{if(eval(val.length)>eval(cmdvalue))
{if(!strError||strError.length==0)
{strError=objValue.name+" : "+cmdvalue+" characters maximum ";}
htmlalert(objValue,strError+"\n[Current length = "+val.length+" ]");return false;}
break;}
case"minlength":case"minlen":{if(eval(val.length)<eval(cmdvalue))
{if(!strError||strError.length==0)
{strError=objValue.name+" : "+cmdvalue+" characters minimum  ";}
htmlalert(objValue,strError+"\n[Current length = "+val.length+" ]");return false;}
break;}
case"alphanumeric":{var charpos=val.search("[^A-Za-z0-9]");if(val.length>0&&charpos>=0)
{if(!strError||strError.length==0)
{strError=objValue.name+": Only alpha-numeric characters allowed ";}
htmlalert(objValue,strError+"\n [Error character position "+eval(charpos+1)+"]");return false;}
break;}
case"numeric":{var charpos=val.search("[^0-9]");if(val.length>0&&charpos>=0)
{if(!strError||strError.length==0)
{strError=objValue.name+": Only digits allowed ";}
htmlalert(objValue,strError+"\n [Error character position "+eval(charpos+1)+"]");return false;}
break;}
case"alphabetic":case"alpha":{var charpos=val.search("[^A-Za-z]");if(val.length>0&&charpos>=0)
{if(!strError||strError.length==0)
{strError=objValue.name+": Only alphabetic characters allowed ";}
htmlalert(objValue,strError+"\n [Error character position "+eval(charpos+1)+"]");return false;}
break;}
case"email":{if(!validateEmailv2(val))
{if(!strError||strError.length==0)
{strError=objValue.name+": Enter a valid Email address ";}
htmlalert(objValue,strError);return false;}
break;}
case"regexp":{if(val.length>0)
{if(!val.match(cmdvalue))
{if(!strError||strError.length==0)
{strError=objValue.name+": Invalid characters found ";}
htmlalert(objValue,strError);return false;}}
break;}}}}else{if(objValue.selectedIndex==eval(cmdvalue))
{if(!strError||strError.length==0)
{strError=objValue.name+": Please Select one option ";}
htmlalert(objValue,strError);return false;}}
return true;}