Form.reset=function(form){if(isDefined($(form+'_errors')))Element.hide(form+'_errors')
if(isDefined($(form+'_model_errors')))Element.hide(form+'_model_errors');$(form).reset();var inputs=Form.getInputs(form);for(var i=0;i<inputs.length;i++){setInputState(inputs[i],'')}}
var f_submited='f_main';function submitForm(f_obj,f){if(!isDefined(f_obj))f_obj=$('f_main');if(isDefined($(f_obj.id+'_fv_obj'))&&!eval($F(f_obj.id+'_fv_obj')+".validate()"))return false;f_submited=f_obj.id;toggleSubmit(f_submited);return isDefined(f)?eval("submitForm"+f+"()"):true;}
function submitFormError(request){toggleSubmit(f_submited);}
function submitFormSuccess(request){toggleSubmit(f_submited);}
function toggleSubmit(f_submited)
{if(!isDefined(f_submited)){f_submited='f_main';}
if(isDefined($(f_submited+'_submit'))&&isDefined($(f_submited+'_submit_wait'))){Element.toggle(f_submited+'_submit',f_submited+'_submit_wait');}}
function submitFormResponse(request){var r=request.getResponseHeader('BB-Response');if('success'==r)submitFormSuccess(request);else submitFormError(request);}
function focusInput(ev){if('blur'==getInputState(getTarget(ev)))setInputState(getTarget(ev),'focus');};function blurInput(ev){if('focus'==getInputState(getTarget(ev)))setInputState(getTarget(ev),'blur');};function setInputState(el,s,focus){el=$(el);el.className=(''==s)?el.classNameDefault:((el.classNameDefault)?el.classNameDefault+' ':'')+s;el.setAttribute('state',s);if(focus)el.focus();};function getInputState(el){return el.getAttribute('state');};function getTarget(e){var t;if(!e)var e=window.event;if(e.target)t=e.target;else if(e.srcElement)t=e.srcElement;if(t.nodeType==3)
t=t.parentNode;return t;};if(Form==undefined)var Form={};Form.Validator=function(form){if(!form){throw('A valid form was not passed in.');}
this.form=form;this.list=[];this.stash={};};Form.Validator.VERSION='0.32';Form.Validator.prototype={form:null,list:null,report:null,stash:null,individual:null,set:function(field,constraint,option,condition){if(!this.form[field]){throw(field+" is not a valid form input");}
var c={};if(typeof(option)=='string'){c.onFailure=option;}else{c=option;}
if(typeof(constraint)=='string'){c.constraint=this[constraint];}else{c.constraint=constraint;}
c.field=field;c.condition=condition?condition:null;this.list.push(c);},get:function(field){var constraints=new Array();for(i in this.list){var c=this.list[i];if(c.field==field||field==null){constraints.push(c);}}
return constraints;},reporter:function(){if(arguments.length==0){if(this.report==null){this.report=new Form.Validator.Report.AlertAll(this);}}else{if(typeof(arguments[0])=='string'){var proto='Form.Validator.Report.'+arguments[0];var code='new '+proto+'(this);';this.report=eval(code);}else{this.report=arguments[0];}}
return this.report;},validate:function(){var i,j,c;var errors=0;var reporter=this.reporter();var constraints;var valids=[];if(arguments.length==0){constraints=this.list;this.individual=false;}else{return true;constraints=[];this.individual=true;for(i=0;i<arguments.length;i++){var field=arguments[i];var clist=this.get(field);if(!clist.length){valids.push(field);}
for(j=0;j<clist.length;j++){constraints.push(clist[j]);}}}
reporter.start(constraints,valids);try{var ok;for(i=0;i<constraints.length;i++){c=constraints[i];if(null==c.condition||eval(c.condition)){ok=c.constraint(this,c.field)
if(!ok){errors++}
reporter.run(ok,c);}}}
catch(e){}
reporter.finish();if(errors>0){return false;}else{return true;}},validateAndAlert:function(){var save=this.reporter();this.reporter('AlertAll');var result=this.validate(arguments);this.reporter(save);return result;},notBlank:function(self,field){var blankness=new RegExp(/^\s*$/);var f=self.form[field];if(f.value==''||blankness.test(f.value)){return false;}else{return true;}},notZero:function(self,field){if(self.form[field].value=="0"){return false;}else{return true;}},makeOptional:function(f){return function(fv,field){if(fv.notBlank(fv,field)){return f(fv,field);}else{return true;}}},makeLengthMin:function(n){return function(fv,field){if(fv.form[field].value.length<n){return false;}else{return true;}}},makeLengthMax:function(n){return function(fv,field){if(fv.form[field].value.length>n){return false;}else{return true;}}},makeValueMin:function(n){return function(fv,field){if(fv.form[field].value<n){return false;}else{return true;}}},makeValueMax:function(n){return function(fv,field){if(fv.form[field].value>n){return false;}else{return true;}}},makeMatchRegex:function(regex){return function(fv,field){return regex.test(fv.form[field].value);}},makeNotMatchRegex:function(regex){return function(fv,field){return!regex.test(fv.form[field].value);}},isEqual:function(other){return function(fv,field){return(fv.form[field].value==fv.form[other].value);}}};if(Form==undefined)var Form={};if(Form.Validator==undefined)Form.Validator={};if(Form.Validator.Report==undefined)Form.Validator.Report={};Form.Validator.Report.OnTheFly=function(fv){this.fv=fv;this.messages={};};Form.Validator.Report.OnTheFly.prototype={fv:null,messages:null,valids:null,prefix:"",suffix:"",separator:", ",onSuccess:"OK",start:function(constraints,valids){this.valids=valids;var i;for(i=0;i<constraints.length;i++){var c=constraints[i];var id=this.prefix+c.field+this.suffix;var test=null==c.condition||eval(c.condition);this.messages[id]={list:[],test:test,onSuccess:c.onSuccess||this.onSuccess};}},run:function(ok,c){if(!ok){var id=this.prefix+c.field+this.suffix;this.messages[id].list.push(c.onFailure);}},finish:function(){for(i=0;i<this.valids.length;i++){setInputState(this.valids[i],'valid');}
var key;var i=0;var model_errors_objid=this.fv.form.id+'_model_errors';var errors_objid=this.fv.form.id+'_errors';if(!this.fv.individual&&isDefined($(errors_objid))){Element.update(errors_objid,'');}
for(id in this.messages){var list=this.messages[id].list;var test=this.messages[id].test;if(!test)
{setInputState(id,'blur');}
else if(list.length>0)
{if(!this.fv.individual&&isDefined($(errors_objid))){error=null;errors=new Array;n_errors=this.messages[id].list.length
i=0;this.messages[id].list.each(function(item,index){pieces=item.split("|");if(1==pieces.length){errors[i]='<dt>'+pieces[0]+'</dt>';i++;}else{if(null==error){error='<dt><strong>'+pieces[0]+':</strong> '+pieces[1];}else{error+=(3==pieces.length?pieces[2]:', ')+pieces[1];}}
if(error!=null){error+='</dt>';errors[i]=error;error=null;i++;}});new Insertion.Bottom(errors_objid,errors.join(''));}
setInputState(id,'invalid',i==0);i++;}
else
{setInputState(id,'valid');}}
if(i!=0&&!this.fv.individual){if(isDefined($(errors_objid)))Element.show(errors_objid);if(isDefined($(model_errors_objid)))Element.hide(model_errors_objid);if(isDefined($(errors_objid)))Element.scrollTo(errors_objid);else if(isDefined($('top')))Element.scrollTo('top');}else if(i==0){if(isDefined($(errors_objid)))Element.hide(errors_objid);}
this.messages={};}};function form_end(){gInputState=new Array();ic=document.getElementsByTagName('INPUT');for(var i=0;i<ic.length;i++){e=ic[i];if("text"==e.type){setInputState(e,'blur');e.classNameDefault=e.className;AttachEvent(e,'focus',focusInput);AttachEvent(e,'blur',blurInput);}}}
AttachEvent(window,'load',form_end);function fv_isCPF(fv,field){return isCpf($F(field));};function fv_isCNPJ(fv,field){return isCnpj($F(field));};Form.Validator.prototype.isCPF=fv_isCPF;Form.Validator.prototype.isCNPJ=fv_isCNPJ;NUM_DIGITOS_CPF=11;NUM_DIGITOS_CNPJ=14;NUM_DGT_CNPJ_BASE=8;String.prototype.lpad=function(pSize,pCharPad)
{var str=this;var dif=pSize-str.length;var ch=String(pCharPad).charAt(0);for(;dif>0;dif--)str=ch+str;return(str);}
String.prototype.trim=function()
{return this.replace(/^\s*/,"").replace(/\s*$/,"");}
function unformatNumber(pNum)
{return String(pNum).replace(/\D/g,"").replace(/^0+/,"");}
function formatCpfCnpj(pCpfCnpj,pUseSepar,pIsCnpj)
{if(pIsCnpj==null)pIsCnpj=false;if(pUseSepar==null)pUseSepar=true;var maxDigitos=pIsCnpj?NUM_DIGITOS_CNPJ:NUM_DIGITOS_CPF;var numero=unformatNumber(pCpfCnpj);numero=numero.lpad(maxDigitos,'0');if(!pUseSepar)return numero;if(pIsCnpj)
{reCnpj=/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;numero=numero.replace(reCnpj,"$1.$2.$3/$4-$5");}
else
{reCpf=/(\d{3})(\d{3})(\d{3})(\d{2})$/;numero=numero.replace(reCpf,"$1.$2.$3-$4");}
return numero;}
function dvCpfCnpj(pEfetivo,pIsCnpj)
{if(pIsCnpj==null)pIsCnpj=false;var i,j,k,soma,dv;var cicloPeso=pIsCnpj?NUM_DGT_CNPJ_BASE:NUM_DIGITOS_CPF;var maxDigitos=pIsCnpj?NUM_DIGITOS_CNPJ:NUM_DIGITOS_CPF;var calculado=formatCpfCnpj(pEfetivo,false,pIsCnpj);calculado=calculado.substring(2,maxDigitos);var result="";for(j=1;j<=2;j++)
{k=2;soma=0;for(i=calculado.length-1;i>=0;i--)
{soma+=(calculado.charAt(i)-'0')*k;k=(k-1)%cicloPeso+2;}
dv=11-soma%11;if(dv>9)dv=0;calculado+=dv;result+=dv}
return result;}
function isCpf(pCpf)
{var numero=formatCpfCnpj(pCpf,false,false);var base=numero.substring(0,numero.length-2);var digitos=dvCpfCnpj(base,false);var algUnico,i;if(numero!=base+digitos)return false;algUnico=true;for(i=1;i<NUM_DIGITOS_CPF;i++)
{algUnico=algUnico&&(numero.charAt(i-1)==numero.charAt(i));}
return(!algUnico);}
function isCnpj(pCnpj)
{var numero=formatCpfCnpj(pCnpj,false,true);var base=numero.substring(0,NUM_DGT_CNPJ_BASE);var ordem=numero.substring(NUM_DGT_CNPJ_BASE,12);var digitos=dvCpfCnpj(base+ordem,true);var algUnico;if(numero!=base+ordem+digitos)return false;algUnico=numero.charAt(0)!='0';for(i=1;i<NUM_DGT_CNPJ_BASE;i++)
{algUnico=algUnico&&(numero.charAt(i-1)==numero.charAt(i));}
if(algUnico)return false;if(ordem=="0000")return false;return(base=="00000000"||parseInt(ordem,10)<=300||base.substring(0,3)!="000");}
function isCpfCnpj(pCpfCnpj)
{var numero=pCpfCnpj.replace(/\D/g,"");if(numero.length>NUM_DIGITOS_CPF)
return isCnpj(pCpfCnpj)
else
return isCpf(pCpfCnpj);}