/* ************************************************************************************* *\
* The MIT License Copyright (c) 2007 Fabio Zendhi Nagao - http://zend.lojcomm.com.br
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
\* ************************************************************************************* */
var fValidator=new Class({options:{msgContainerTag:"div",msgClass:"fValidator-msg",styleNeutral:{"background-color":"#ffc","border-color":"#cc0"},styleInvalid:{"background-color":"#fcc","border-color":"#c00"},styleValid:{"background-color":"#cfc","border-color":"#CFEBCF"},required:{type:"required",re:/[^.*]/,msg:"This field is required."},alpha:{type:"alpha",re:/^[a-z ._-]+$/i,msg:"This field accepts alphabetic characters only."},alphanum:{type:"alphanum",re:/^[a-z0-9 ._-]+$/i,msg:"This field accepts alphanumeric characters only."},integer:{type:"integer",re:/^[-+]?\d+$/,msg:"Please enter a valid integer."},real:{type:"real",re:/^[-+]?\d*\.?\d+$/,msg:"Please enter a valid number."},date:{type:"date",re:/^((((0[13578])|([13578])|(1[02]))[\/](([1-9])|([0-2][0-9])|(3[01])))|(((0[469])|([469])|(11))[\/](([1-9])|([0-2][0-9])|(30)))|((2|02)[\/](([1-9])|([0-2][0-9]))))[\/]\d{4}$|^\d{4}$/,msg:"Please enter a valid date (mm/dd/yyyy)."},email:{type:"email",re:/^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i,msg:"Please enter a valid email."},phone:{type:"phone",re:/^[\d\s ().-]+$/,msg:"Please enter a valid phone."},url:{type:"url",re:/^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i,msg:"Please enter a valid url."},confirm:{type:"confirm",msg:"Confirm Password does not match original Password."},onValid:Class.empty,onInvalid:Class.empty},initialize:function(form,options){this.form=$(form);this.setOptions(options);this.fields=this.form.getElements("*[class^=fValidate]");this.validations=[];this.fields.each(function(element){if(!this._isChildType(element)){element.setStyles(this.options.styleNeutral)}element.cbErr=0;var classes=element.getProperty("class").split(" ");classes.each(function(klass){if(klass.match(/^fValidate(\[.+\])$/)){var aFilters=eval(klass.match(/^fValidate(\[.+\])$/)[1]);for(var i=0;i<aFilters.length;i++){if(this.options[aFilters[i]]){this.register(element,this.options[aFilters[i]])}if(aFilters[i].charAt(0)=="="){this.register(element,$extend(this.options.confirm,{idField:aFilters[i].substr(1)}))}}}}.bind(this))}.bind(this));this.form.addEvents({submit:this._onSubmit.bind(this),reset:this._onReset.bind(this)})},register:function(B,A){B=$(B);this.validations.push([B,A]);B.addEvent("blur",function(){this._validate(B,A)}.bind(this))},_isChildType:function(B){var A=B.type.toLowerCase();if((A=="radio")||(A=="checkbox")){return true}return false},_validate:function(B,A){switch(A.type){case"confirm":if($(A.idField).getValue()==B.getValue()){this._msgRemove(B,A)}else{this._msgInject(B,A)}break;default:if(A.re.test(B.getValue())){this._msgRemove(B,A)}else{this._msgInject(B,A)}}},_validateChild:function(F,B){var A=this.form[F.getProperty("name")];var D=0;var E=true;for(var C=0;C<A.length;C++){if(A[C].checked){D++;if(!B.re.test(A[C].getValue())){E=false;break}}}if(D==0&&B.type=="required"){E=false}if(E){this._msgRemove(F,B)}else{this._msgInject(F,B)}},_msgInject:function(A,B){if(!$(A.getProperty("id")+B.type+"_msg")){var C=new Element(this.options.msgContainerTag,{id:A.getProperty("id")+B.type+"_msg","class":this.options.msgClass}).setHTML(B.msg).setStyle("opacity",0).injectAfter(A).effect("opacity",{duration:500,transition:Fx.Transitions.linear}).start(0,1);A.cbErr++;this._chkStatus(A,B)}},_msgRemove:function(A,B,D){D=D||false;if($(A.getProperty("id")+B.type+"_msg")){var C=$(A.getProperty("id")+B.type+"_msg");C.effect("opacity",{duration:500,transition:Fx.Transitions.linear,onComplete:function(){C.remove()}}).start(1,0);if(!D){A.cbErr--;this._chkStatus(A,B)}}},_chkStatus:function(B,A){if(B.cbErr==0){B.effects({duration:500,transition:Fx.Transitions.linear}).start(this.options.styleValid);this.fireEvent("onValid",[B,A],50)}else{B.effects({duration:500,transition:Fx.Transitions.linear}).start(this.options.styleInvalid);this.fireEvent("onInvalid",[B,A],50)}},_onSubmit:function(A){A=new Event(A);var B=true;this.validations.each(function(C){if(this._isChildType(C[0])){this._validateChild(C[0],C[1])}else{this._validate(C[0],C[1])}if(C[0].cbErr>0){B=false}}.bind(this));if(!B){A.stop()}return B},_onReset:function(){this.validations.each(function(A){if(!this._isChildType(A[0])){A[0].setStyles(this.options.styleNeutral)}A[0].cbErr=0;this._msgRemove(A[0],A[1],true)}.bind(this))}});fValidator.implement(new Events);fValidator.implement(new Options);
