// LiveValidation 1.3 (standalone version)
// Copyright (c) 2007-2008 Alec Hill (www.livevalidation.com)
// LiveValidation is licensed under the terms of the MIT License
var LiveValidation=function(_1,_2){
this.initialize(_1,_2);
};
LiveValidation.VERSION="1.3 standalone";
LiveValidation.TEXTAREA=1;
LiveValidation.TEXT=2;
LiveValidation.PASSWORD=3;
LiveValidation.CHECKBOX=4;
LiveValidation.SELECT=5;
LiveValidation.FILE=6;
LiveValidation.massValidate=function(_3){
var _4=true;
for(var i=0,len=_3.length;i<len;++i){
var _6=_3[i].validate();
if(_4){
_4=_6;
}
}
return _4;
};
LiveValidation.prototype={validClass:"LV_valid",invalidClass:"LV_invalid",messageClass:"LV_validation_message",validFieldClass:"LV_valid_field",invalidFieldClass:"LV_invalid_field",initialize:function(_7,_8){
var _9=this;
if(!_7){
throw new Error("LiveValidation::initialize - No element reference or element id has been provided!");
}
this.element=_7.nodeName?_7:document.getElementById(_7);
if(!this.element){
throw new Error("LiveValidation::initialize - No element with reference or id of '"+_7+"' exists!");
}
this.validations=[];
this.elementType=this.getElementType();
this.form=this.element.form;
var _a=_8||{};
this.validMessage=_a.validMessage||"Thankyou!";
var _b=_a.insertAfterWhatNode||this.element;
this.insertAfterWhatNode=_b.nodeType?_b:document.getElementById(_b);
this.onValid=_a.onValid||function(){
this.addFieldClass();
};
this.onInvalid=_a.onInvalid||function(){
this.insertMessage(this.createMessageSpan());
this.addFieldClass();
};
this.onlyOnBlur=_a.onlyOnBlur||true;
this.wait=_a.wait||0;
this.onlyOnSubmit=_a.onlyOnSubmit||false;
if(this.form){
this.formObj=LiveValidationForm.getInstance(this.form);
this.formObj.addField(this);
}
this.oldOnFocus=this.element.onfocus||function(){
};
this.oldOnBlur=this.element.onblur||function(){
};
this.oldOnClick=this.element.onclick||function(){
};
this.oldOnChange=this.element.onchange||function(){
};
this.oldOnKeyup=this.element.onkeyup||function(){
};
this.element.onfocus=function(e){
_9.doOnFocus(e);
return _9.oldOnFocus.call(this,e);
};
if(!this.onlyOnSubmit){
switch(this.elementType){
case LiveValidation.CHECKBOX:
this.element.onclick=function(e){
_9.validate();
return _9.oldOnClick.call(this,e);
};
case LiveValidation.SELECT:
case LiveValidation.FILE:
this.element.onchange=function(e){
_9.validate();
return _9.oldOnChange.call(this,e);
};
break;
default:
if(!this.onlyOnBlur){
this.element.onkeyup=function(e){
_9.deferValidation();
return _9.oldOnKeyup.call(this,e);
};
}
this.element.onblur=function(e){
_9.doOnBlur(e);
return _9.oldOnBlur.call(this,e);
};
}
}
},destroy:function(){
if(this.formObj){
this.formObj.removeField(this);
this.formObj.destroy();
}
this.element.onfocus=this.oldOnFocus;
if(!this.onlyOnSubmit){
switch(this.elementType){
case LiveValidation.CHECKBOX:
this.element.onclick=this.oldOnClick;
case LiveValidation.SELECT:
case LiveValidation.FILE:
this.element.onchange=this.oldOnChange;
break;
default:
if(!this.onlyOnBlur){
this.element.onkeyup=this.oldOnKeyup;
}
this.element.onblur=this.oldOnBlur;
}
}
this.validations=[];
this.removeMessageAndFieldClass();
},add:function(_11,_12){
this.validations.push({type:_11,params:_12||{}});
return this;
},remove:function(_13,_14){
var _15=false;
for(var i=0,len=this.validations.length;i<len;i++){
if(this.validations[i].type==_13){
if(this.validations[i].params==_14){
_15=true;
break;
}
}
}
if(_15){
this.validations.splice(i,1);
}
return this;
},deferValidation:function(e){
if(this.wait>=300){
this.removeMessageAndFieldClass();
}
var _18=this;
if(this.timeout){
clearTimeout(_18.timeout);
}
this.timeout=setTimeout(function(){
_18.validate();
},_18.wait);
},doOnBlur:function(e){
this.focused=false;
this.validate(e);
},doOnFocus:function(e){
this.focused=true;
this.removeMessageAndFieldClass();
},getElementType:function(){
switch(true){
case (this.element.nodeName.toUpperCase()=="TEXTAREA"):
return LiveValidation.TEXTAREA;
case (this.element.nodeName.toUpperCase()=="INPUT"&&this.element.type.toUpperCase()=="TEXT"):
return LiveValidation.TEXT;
case (this.element.nodeName.toUpperCase()=="INPUT"&&this.element.type.toUpperCase()=="PASSWORD"):
return LiveValidation.PASSWORD;
case (this.element.nodeName.toUpperCase()=="INPUT"&&this.element.type.toUpperCase()=="CHECKBOX"):
return LiveValidation.CHECKBOX;
case (this.element.nodeName.toUpperCase()=="INPUT"&&this.element.type.toUpperCase()=="FILE"):
return LiveValidation.FILE;
case (this.element.nodeName.toUpperCase()=="SELECT"):
return LiveValidation.SELECT;
case (this.element.nodeName.toUpperCase()=="INPUT"):
throw new Error("LiveValidation::getElementType - Cannot use LiveValidation on an "+this.element.type+" input!");
default:
throw new Error("LiveValidation::getElementType - Element must be an input, select, or textarea!");
}
},doValidations:function(){
this.validationFailed=false;
for(var i=0,len=this.validations.length;i<len;++i){
var _1c=this.validations[i];
switch(_1c.type){
case Validate.Presence:
case Validate.Confirmation:
case Validate.Acceptance:
this.displayMessageWhenEmpty=true;
this.validationFailed=!this.validateElement(_1c.type,_1c.params);
break;
default:
this.validationFailed=!this.validateElement(_1c.type,_1c.params);
break;
}
if(this.validationFailed){
return false;
}
}
this.message=this.validMessage;
return true;
},validateElement:function(_1d,_1e){
var _1f=(this.elementType==LiveValidation.SELECT)?this.element.options[this.element.selectedIndex].value:this.element.value;
if(_1d==Validate.Acceptance){
if(this.elementType!=LiveValidation.CHECKBOX){
throw new Error("LiveValidation::validateElement - Element to validate acceptance must be a checkbox!");
}
_1f=this.element.checked;
}
var _20=true;
try{
_1d(_1f,_1e);
}
catch(error){
if(error instanceof Validate.Error){
if(_1f!==""||(_1f===""&&this.displayMessageWhenEmpty)){
this.validationFailed=true;
this.message=error.message;
_20=false;
}
}else{
throw error;
}
}
finally{
return _20;
}
},validate:function(){
if(!this.element.disabled){
var _21=this.doValidations();
if(_21){
this.onValid();
return true;
}else{
this.onInvalid();
return false;
}
}else{
return true;
}
},enable:function(){
this.element.disabled=false;
return this;
},disable:function(){
this.element.disabled=true;
this.removeMessageAndFieldClass();
return this;
},createMessageSpan:function(){
var _22=document.createElement("span");
var _23=document.createTextNode(this.message);
_22.appendChild(_23);
return _22;
},insertMessage:function(_24){
this.removeMessage();
if((this.displayMessageWhenEmpty&&(this.elementType==LiveValidation.CHECKBOX||this.element.value==""))||this.element.value!=""){
var _25=this.validationFailed?this.invalidClass:this.validClass;
_24.className+=" "+this.messageClass+" "+_25;
if(this.insertAfterWhatNode.nextSibling){
this.insertAfterWhatNode.parentNode.insertBefore(_24,this.insertAfterWhatNode.nextSibling);
}else{
this.insertAfterWhatNode.parentNode.appendChild(_24);
}
}
},addFieldClass:function(){
this.removeFieldClass();
if(!this.validationFailed){
if(this.displayMessageWhenEmpty||this.element.value!=""){
if(this.element.className.indexOf(this.validFieldClass)==-1){
this.element.className+=" "+this.validFieldClass;
}
}
}else{
if(this.element.className.indexOf(this.invalidFieldClass)==-1){
this.element.className+=" "+this.invalidFieldClass;
}
}
},removeMessage:function(){
var _26;
var el=this.insertAfterWhatNode;
while(el.nextSibling){
if(el.nextSibling.nodeType===1){
_26=el.nextSibling;
break;
}
el=el.nextSibling;
}
if(_26&&_26.className.indexOf(this.messageClass)!=-1){
this.insertAfterWhatNode.parentNode.removeChild(_26);
}
},removeFieldClass:function(){
if(this.element.className.indexOf(this.invalidFieldClass)!=-1){
this.element.className=this.element.className.split(this.invalidFieldClass).join("");
}
if(this.element.className.indexOf(this.validFieldClass)!=-1){
this.element.className=this.element.className.split(this.validFieldClass).join(" ");
}
},removeMessageAndFieldClass:function(){
this.removeMessage();
this.removeFieldClass();
}};
var LiveValidationForm=function(_28){
this.initialize(_28);
};
LiveValidationForm.instances={};
LiveValidationForm.getInstance=function(_29){
var _2a=Math.random()*Math.random();
if(!_29.id){
_29.id="formId_"+_2a.toString().replace(/\./,"")+new Date().valueOf();
}
if(!LiveValidationForm.instances[_29.id]){
LiveValidationForm.instances[_29.id]=new LiveValidationForm(_29);
}
return LiveValidationForm.instances[_29.id];
};
LiveValidationForm.prototype={initialize:function(_2b){
this.name=_2b.id;
this.element=_2b;
this.fields=[];
this.oldOnSubmit=this.element.onsubmit||function(){
};
var _2c=this;
this.element.onsubmit=function(e){
return (LiveValidation.massValidate(_2c.fields))?_2c.oldOnSubmit.call(this,e||window.event)!==false:false;
};
},addField:function(_2e){
this.fields.push(_2e);
},removeField:function(_2f){
var _30=[];
for(var i=0,len=this.fields.length;i<len;i++){
if(this.fields[i]!==_2f){
_30.push(this.fields[i]);
}
}
this.fields=_30;
},destroy:function(_32){
if(this.fields.length!=0&&!_32){
return false;
}
this.element.onsubmit=this.oldOnSubmit;
LiveValidationForm.instances[this.name]=null;
return true;
}};
var Validate={Presence:function(_33,_34){
var _35=_35||{};
var _36=_35.failureMessage||" Required!";
if(_33===""||_33===null||_33===undefined){
Validate.fail(_36);
}
return true;
},Numericality:function(_37,_38){
var _39=_37;
var _3a=Number(_3a);
var _3b=_3b||{};
var _3c=((_3b.minimum)||(_3b.minimum==0))?_3b.minimum:null;
var _3d=((_3b.maximum)||(_3b.maximum==0))?_3b.maximum:null;
var is=((_3b.is)||(_3b.is==0))?_3b.is:null;
var _3f=_3b.notANumberMessage||"Must be a number!";
var _40=_3b.notAnIntegerMessage||"Must be an integer!";
var _41=_3b.wrongNumberMessage||"Must be "+is+"!";
var _42=_3b.tooLowMessage||"Must not be less than "+_3c+"!";
var _43=_3b.tooHighMessage||"Must not be more than "+_3d+"!";
if(!isFinite(_3a)){
Validate.fail(_3f);
}
if(_3b.onlyInteger&&(/\.0+$|\.$/.test(String(_39))||_3a!=parseInt(_3a))){
Validate.fail(_40);
}
switch(true){
case (is!==null):
if(_3a!=Number(is)){
Validate.fail(_41);
}
break;
case (_3c!==null&&_3d!==null):
Validate.Numericality(_3a,{tooLowMessage:_42,minimum:_3c});
Validate.Numericality(_3a,{tooHighMessage:_43,maximum:_3d});
break;
case (_3c!==null):
if(_3a<Number(_3c)){
Validate.fail(_42);
}
break;
case (_3d!==null):
if(_3a>Number(_3d)){
Validate.fail(_43);
}
break;
}
return true;
},Format:function(_44,_45){
var _46=String(_46);
var _47=_47||{};
var _48=_47.failureMessage||"Not valid!";
var _49=_47.pattern||/./;
var _4a=_47.negate||false;
if(!_4a&&!_49.test(_46)){
Validate.fail(_48);
}
if(_4a&&_49.test(_46)){
Validate.fail(_48);
}
return true;
},Email:function(_4b,_4c){
var _4d=_4d||{};
var _4e=_4d.failureMessage||"Must be a valid email address!";
Validate.Format(_4b,{failureMessage:_4e,pattern:/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i});
return true;
},Length:function(_4f,_50){
var _51=String(_51);
var _52=_52||{};
var _53=((_52.minimum)||(_52.minimum==0))?_52.minimum:null;
var _54=((_52.maximum)||(_52.maximum==0))?_52.maximum:null;
var is=((_52.is)||(_52.is==0))?_52.is:null;
var _56=_52.wrongLengthMessage||"Must be "+is+" characters long!";
var _57=_52.tooShortMessage||"Must not be less than "+_53+" characters long!";
var _58=_52.tooLongMessage||"Must not be more than "+_54+" characters long!";
switch(true){
case (is!==null):
if(_51.length!=Number(is)){
Validate.fail(_56);
}
break;
case (_53!==null&&_54!==null):
Validate.Length(_51,{tooShortMessage:_57,minimum:_53});
Validate.Length(_51,{tooLongMessage:_58,maximum:_54});
break;
case (_53!==null):
if(_51.length<Number(_53)){
Validate.fail(_57);
}
break;
case (_54!==null):
if(_51.length>Number(_54)){
Validate.fail(_58);
}
break;
default:
throw new Error("Validate::Length - Length(s) to validate against must be provided!");
}
return true;
},Inclusion:function(_59,_5a){
var _5b=_5b||{};
var _5c=_5b.failureMessage||"Must be included in the list!";
var _5d=(_5b.caseSensitive===false)?false:true;
if(_5b.allowNull&&_59==null){
return true;
}
if(!_5b.allowNull&&_59==null){
Validate.fail(_5c);
}
var _5e=_5b.within||[];
if(!_5d){
var _5f=[];
for(var j=0,length=_5e.length;j<length;++j){
var _61=_5e[j];
if(typeof _61=="string"){
_61=_61.toLowerCase();
}
_5f.push(_61);
}
_5e=_5f;
if(typeof _59=="string"){
_59=_59.toLowerCase();
}
}
var _62=false;
for(var i=0,length=_5e.length;i<length;++i){
if(_5e[i]==_59){
_62=true;
}
if(_5b.partialMatch){
if(_59.indexOf(_5e[i])!=-1){
_62=true;
}
}
}
if((!_5b.negate&&!_62)||(_5b.negate&&_62)){
Validate.fail(_5c);
}
return true;
},Exclusion:function(_64,_65){
var _66=_66||{};
_66.failureMessage=_66.failureMessage||"Must not be included in the list!";
_66.negate=true;
Validate.Inclusion(_64,_66);
return true;
},Confirmation:function(_67,_68){
if(!_68.match){
throw new Error("Validate::Confirmation - Error validating confirmation: Id of element to match must be provided!");
}
var _69=_69||{};
var _6a=_69.failureMessage||"Does not match!";
var _6b=_69.match.nodeName?_69.match:document.getElementById(_69.match);
if(!_6b){
throw new Error("Validate::Confirmation - There is no reference with name of, or element with id of '"+_69.match+"'!");
}
if(_67!=_6b.value){
Validate.fail(_6a);
}
return true;
},Acceptance:function(_6c,_6d){
var _6e=_6e||{};
var _6f=_6e.failureMessage||"Must be accepted!";
if(!_6c){
Validate.fail(_6f);
}
return true;
},Custom:function(_70,_71){
var _72=_72||{};
var _73=_72.against||function(){
return true;
};
var _74=_72.args||{};
var _75=_72.failureMessage||"Not valid!";
if(!_73(_70,_74)){
Validate.fail(_75);
}
return true;
},now:function(_76,_77,_78){
if(!_76){
throw new Error("Validate::now - Validation function must be provided!");
}
var _79=true;
try{
_76(_77,_78||{});
}
catch(error){
if(error instanceof Validate.Error){
_79=false;
}else{
throw error;
}
}
finally{
return _79;
}
},fail:function(_7a){
throw new Validate.Error(_7a);
},Error:function(_7b){
this.message=_7b;
this.name="ValidationError";
}};

