﻿function Validator(a){this.formobj=document.forms[a];if(!this.formobj){alert("Error: couldnot get Form object "+a);return}if(this.formobj.onsubmit){this.formobj.old_onsubmit=this.formobj.onsubmit;this.formobj.onsubmit=null}else this.formobj.old_onsubmit=null;this.formobj._sfm_form_name=a;this.formobj.onsubmit=form_submit_handler;this.addValidation=add_validation;this.setAddnlValidationFunction=set_addnl_vfunction;this.clearAllValidations=clear_all_validations;this.disable_validations=false;document.error_disp_handler=new sfm_ErrorDisplayHandler;this.EnableOnPageErrorDisplay=validator_enable_OPED;this.EnableOnPageErrorDisplaySingleBox=validator_enable_OPED_SB;this.show_errors_together=true;this.EnableMsgsTogether=sfm_enable_show_msgs_together;document.set_focus_onerror=true;this.EnableFocusOnError=sfm_validator_enable_focus}function sfm_validator_enable_focus(a){document.set_focus_onerror=a}function set_addnl_vfunction(a){this.formobj.addnlvalidation=a}function sfm_set_focus(a){if(document.set_focus_onerror)a.focus()}function sfm_enable_show_msgs_together(){this.show_errors_together=true;this.formobj.show_errors_together=true}function clear_all_validations(){for(var a=0;a<this.formobj.elements.length;a++)this.formobj.elements[a].validationset=null}function form_submit_handler(){var bRet=true;document.error_disp_handler.clear_msgs();for(var itr=0;itr<this.elements.length;itr++){if(this.elements[itr].validationset&&!this.elements[itr].validationset.validate())bRet=false;if(!bRet&&!this.show_errors_together)break}if(this.addnlvalidation){str=" var ret = "+this.addnlvalidation+"()";eval(str);if(!ret)bRet=false}if(!bRet){document.error_disp_handler.FinalShowMsg();return false}return true}function add_validation(c,d,e){var b=null;if(arguments.length>3)b=arguments[3];if(!this.formobj){alert("Error: The form object is not set properly");return}var a=this.formobj[c];if(a.length&&isNaN(a.selectedIndex))a=a[0];if(!a){alert("Error: Couldnot get the input object named: "+c);return}if(!a.validationset)a.validationset=new ValidationSet(a,this.show_errors_together);a.validationset.add(d,e,b);a.validatorobj=this}function validator_enable_OPED(){document.error_disp_handler.EnableOnPageDisplay(false)}function validator_enable_OPED_SB(){document.error_disp_handler.EnableOnPageDisplay(true)}function sfm_ErrorDisplayHandler(){this.msgdisplay=new AlertMsgDisplayer;this.EnableOnPageDisplay=edh_EnableOnPageDisplay;this.ShowMsg=edh_ShowMsg;this.FinalShowMsg=edh_FinalShowMsg;this.all_msgs=[];this.clear_msgs=edh_clear_msgs}function edh_clear_msgs(){this.msgdisplay.clearmsg(this.all_msgs);this.all_msgs=[]}function edh_FinalShowMsg(){this.msgdisplay.showmsg(this.all_msgs)}function edh_EnableOnPageDisplay(a){if(true==a)this.msgdisplay=new SingleBoxErrorDisplay;else this.msgdisplay=new DivMsgDisplayer}function edh_ShowMsg(c,b){var a=[];a["input_element"]=b;a["msg"]=c;this.all_msgs.push(a)}function AlertMsgDisplayer(){this.showmsg=alert_showmsg;this.clearmsg=alert_clearmsg}function alert_clearmsg(){}function alert_showmsg(c){var d="",a=null;for(var b=0;b<c.length;b++){if(null==a)a=c[b]["input_element"];d+=c[b]["msg"]+"\n"}alert(d);if(null!=a)sfm_set_focus(a)}function sfm_show_error_msg(b,a){document.error_disp_handler.ShowMsg(b,a)}function SingleBoxErrorDisplay(){this.showmsg=sb_div_showmsg;this.clearmsg=sb_div_clearmsg}function sb_div_clearmsg(b){var a=form_error_div_name(b);show_div_msg(a,"")}function sb_div_showmsg(b){var a="<ul>\n";for(var c=0;c<b.length;c++)a+="<li>"+b[c]["msg"]+"</li>\n";a+="</ul>";var d=form_error_div_name(b);show_div_msg(d,a)}function form_error_div_name(c){var a=null;for(var d in c){a=c[d]["input_element"];if(a)break}var b="";if(a)b=a.form._sfm_form_name+"_errorloc";return b}function DivMsgDisplayer(){this.showmsg=div_showmsg;this.clearmsg=div_clearmsg}function div_clearmsg(a){for(var c in a){var b=element_div_name(a[c]["input_element"]);show_div_msg(b,"")}}function element_div_name(b){var a=b.form._sfm_form_name+"_"+b.name+"_errorloc";a=a.replace(/[\[\]]/gi,"");return a}function div_showmsg(b){var e,a=null;for(var c in b){if(null==a)a=b[c]["input_element"];var d=element_div_name(b[c]["input_element"]);show_div_msg(d,b[c]["msg"])}if(null!=a)sfm_set_focus(a)}function show_div_msg(a,b){if(a.length<=0)return false;if(document.layers){divlayer=document.layers[a];if(!divlayer)return;divlayer.document.open();divlayer.document.write(b);divlayer.document.close()}else if(document.all){divlayer=document.all[a];if(!divlayer)return;divlayer.innerHTML=b}else if(document.getElementById){divlayer=document.getElementById(a);if(!divlayer)return;divlayer.innerHTML=b}divlayer.style.visibility="visible"}function ValidationDesc(b,d,c,a){this.desc=d;this.error=c;this.itemobj=b;this.condition=a;this.validate=vdesc_validate}function vdesc_validate(){if(this.condition!=null)if(!eval(this.condition))return true;if(!validateInput(this.desc,this.itemobj,this.error)){this.itemobj.validatorobj.disable_validations=true;sfm_set_focus(this.itemobj);return false}return true}function ValidationSet(b,a){this.vSet=[];this.add=add_validationdesc;this.validate=vset_validate;this.itemobj=b;this.msgs_together=a}function add_validationdesc(c,b,a){this.vSet[this.vSet.length]=new ValidationDesc(this.itemobj,c,b,a)}function vset_validate(){var a=true;for(var b=0;b<this.vSet.length;b++){a=a&&this.vSet[b].validate();if(!a&&!this.msgs_together)break}return a}function validateEmail(e){var a=e.match("^(.+)@(.+)$");if(a==null)return false;if(a[1]!=null){var c=/^\"?[\w-_\.]*\"?$/;if(a[1].match(c)==null)return false}if(a[2]!=null){var b=/^[\w-\.]*\.[A-Za-z]{2,4}$/;if(a[2].match(b)==null){var d=/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;if(a[2].match(d)==null)return false}return true}return false}function IsCheckSelected(c,f){var d=false,a=c.form.elements[c.name];if(a.length){var e=-1;for(var b=0;b<a.length;b++)if(a[b].value==f){e=b;break}if(e>=0)if(a[e].checked=="1")d=true}else if(c.checked=="1")d=true;return d}function TestDontSelectChk(b,d,a){var c=true;c=IsCheckSelected(b,d)?false:true;if(c==false){if(!a||a.length==0)a="Can't Proceed as you selected "+b.name;sfm_show_error_msg(a,b)}return c}function TestShouldSelectChk(b,d,a){var c=true;c=IsCheckSelected(b,d)?true:false;if(c==false){if(!a||a.length==0)a="You should select "+b.name;sfm_show_error_msg(a,b)}return c}function TestRequiredInput(objValue,strError){var ret=true,val=objValue.value;val=val.replace(/^\s+|\s+$/g,"");if(eval(val.length)==0){if(!strError||strError.length==0)strError=objValue.name+" : Required Field";sfm_show_error_msg(strError,objValue);ret=false}return ret}function TestMaxLen(objValue,strMaxLen,strError){var ret=true;if(eval(objValue.value.length)>eval(strMaxLen)){if(!strError||strError.length==0)strError=objValue.name+" : "+strMaxLen+" characters maximum ";sfm_show_error_msg(strError,objValue);ret=false}return ret}function TestMinLen(objValue,strMinLen,strError){var ret=true;if(eval(objValue.value.length)<eval(strMinLen)){if(!strError||strError.length==0)strError=objValue.name+" : "+strMinLen+" characters minimum  ";sfm_show_error_msg(strError,objValue);ret=false}return ret}function TestInputType(b,e,a,d){var c=true,f=b.value.search(e);if(b.value.length>0&&f>=0){if(!a||a.length==0)a=d;sfm_show_error_msg(a,b);c=false}return c}function TestEmail(a,b){var c=true;if(a.value.length>0&&!validateEmail(a.value)){if(!b||b.length==0)b=a.name+": Enter a valid Email address ";sfm_show_error_msg(b,a);c=false}return c}function TestLessThan(objValue,strLessThan,strError){var ret=true;if(isNaN(objValue.value)){sfm_show_error_msg(objValue.name+": Should be a number ",objValue);ret=false}else if(eval(objValue.value)>=eval(strLessThan)){if(!strError||strError.length==0)strError=objValue.name+" : value should be less than "+strLessThan;sfm_show_error_msg(strError,objValue);ret=false}return ret}function TestGreaterThan(objValue,strGreaterThan,strError){var ret=true;if(isNaN(objValue.value)){sfm_show_error_msg(objValue.name+": Should be a number ",objValue);ret=false}else if(eval(objValue.value)<=eval(strGreaterThan)){if(!strError||strError.length==0)strError=objValue.name+" : value should be greater than "+strGreaterThan;sfm_show_error_msg(strError,objValue);ret=false}return ret}function TestRegExp(a,d,b){var c=true;if(a.value.length>0&&!a.value.match(d)){if(!b||b.length==0)b=a.name+": Invalid characters found ";sfm_show_error_msg(b,a);c=false}return c}function TestDontSelect(objValue,dont_sel_index,strError){var ret=true;if(objValue.selectedIndex==null){sfm_show_error_msg("ERROR: dontselect command for non-select Item");ret=false}if(objValue.selectedIndex==eval(dont_sel_index)){if(!strError||strError.length==0)strError=objValue.name+": Please Select one option ";sfm_show_error_msg(strError,objValue);ret=false}return ret}function TestSelectOneRadio(a,b){var e=a.form.elements[a.name],c=false;for(var d=0;d<e.length;d++)if(e[d].checked){c=true;break}if(false==c){if(!b||b.length==0)b="Please select one option from "+a.name;sfm_show_error_msg(b,a)}return c}function validateInput(e,a,c){var b=true,g=e.search("="),f="",d="";if(g>=0){f=e.substring(0,g);d=e.substr(g+1)}else f=e;switch(f){case "req":case "required":b=TestRequiredInput(a,c);break;case "maxlength":case "maxlen":b=TestMaxLen(a,d,c);break;case "minlength":case "minlen":b=TestMinLen(a,d,c);break;case "alnum":case "alphanumeric":b=TestInputType(a,"[^A-Za-z0-9]",c,a.name+": Only alpha-numeric characters allowed ");break;case "alnum_s":case "alphanumeric_space":b=TestInputType(a,"[^A-Za-z0-9\\s]",c,a.name+": Only alpha-numeric characters and space allowed ");break;case "num":case "numeric":b=TestInputType(a,"[^0-9]",c,a.name+": Only digits allowed ");break;case "dec":case "decimal":b=TestInputType(a,"[^0-9.]",c,a.name+": Only numbers allowed ");break;case "alphabetic":case "alpha":b=TestInputType(a,"[^A-Za-z]",c,a.name+": Only alphabetic characters allowed ");break;case "alphabetic_space":case "alpha_s":b=TestInputType(a,"[^A-Za-z\\s]",c,a.name+": Only alphabetic characters and space allowed ");break;case "email":b=TestEmail(a,c);break;case "lt":case "lessthan":b=TestLessThan(a,d,c);break;case "gt":case "greaterthan":b=TestGreaterThan(a,d,c);break;case "regexp":b=TestRegExp(a,d,c);break;case "dontselect":b=TestDontSelect(a,d,c);break;case "dontselectchk":b=TestDontSelectChk(a,d,c);break;case "shouldselchk":b=TestShouldSelectChk(a,d,c);break;case "selone_radio":b=TestSelectOneRadio(a,c)}return b}function VWZ_IsListItemSelected(b,c){for(var a=0;a<b.options.length;a++)if(b.options[a].selected==true&&b.options[a].value==c)return true;return false}function VWZ_IsChecked(a,c){if(a.length){for(var b=0;b<a.length;b++)if(a[b].checked=="1"&&a[b].value==c)return true}else if(a.checked=="1")return true;return false}
