// LiveValidation 1.4 (standalone version)
// Copyright (c) 2007-2008 Alec Hill (www.livevalidation.com)
// LiveValidation is licensed under the terms of the MIT License
var LiveValidation=function(b,a){this.initialize(b,a)};LiveValidation.VERSION="1.4 standalone";LiveValidation.TEXTAREA=1;LiveValidation.TEXT=2;LiveValidation.PASSWORD=3;LiveValidation.CHECKBOX=4;LiveValidation.SELECT=5;LiveValidation.FILE=6;LiveValidation.massValidate=function(b){for(var a=true,d=0,c=b.length;d<c;++d){var e=b[d].validate();if(a)a=e}return a};
LiveValidation.prototype={validClass:"LV_valid",invalidClass:"LV_invalid",messageClass:"LV_validation_message",validFieldClass:"LV_valid_field",invalidFieldClass:"LV_invalid_field",initialize:function(b,a){var d=this;if(!b)throw Error("LiveValidation::initialize - No element reference or element id has been provided!");this.element=b.nodeName?b:document.getElementById(b);if(!this.element)throw Error("LiveValidation::initialize - No element with reference or id of '"+b+"' exists!");this.validations=
[];this.elementType=this.getElementType();this.form=this.element.form;var c=a||{};this.validMessage=c.validMessage||"Thankyou!";var e=c.insertAfterWhatNode||this.element;this.insertAfterWhatNode=e.nodeType?e:document.getElementById(e);this.onlyOnBlur=c.onlyOnBlur||false;this.wait=c.wait||0;this.onlyOnSubmit=c.onlyOnSubmit||false;this.beforeValidation=c.beforeValidation||function(){};this.beforeValid=c.beforeValid||function(){};this.onValid=c.onValid||function(){this.insertMessage(this.createMessageSpan());
this.addFieldClass()};this.afterValid=c.afterValid||function(){};this.beforeInvalid=c.beforeInvalid||function(){};this.onInvalid=c.onInvalid||function(){this.insertMessage(this.createMessageSpan());this.addFieldClass()};this.afterInvalid=c.afterInvalid||function(){};this.afterValidation=c.afterValidation||function(){};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(f){d.doOnFocus(f);return d.oldOnFocus.call(this,f)};if(!this.onlyOnSubmit)switch(this.elementType){case LiveValidation.CHECKBOX:this.element.onclick=function(f){d.validate();return d.oldOnClick.call(this,f)};case LiveValidation.SELECT:case LiveValidation.FILE:this.element.onchange=function(f){d.validate();
return d.oldOnChange.call(this,f)};break;default:if(!this.onlyOnBlur)this.element.onkeyup=function(f){d.deferValidation();return d.oldOnKeyup.call(this,f)};this.element.onblur=function(f){d.doOnBlur(f);return d.oldOnBlur.call(this,f)}}},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(b,a){this.validations.push({type:b,params:a||{}});return this},remove:function(b,a){for(var d=[],c=0,e=this.validations.length;c<e;c++){var f=this.validations[c];f.type!=b&&f.params!=a&&d.push(f)}this.validations=d;return this},deferValidation:function(){this.wait>=300&&this.removeMessageAndFieldClass();var b=
this;this.timeout&&clearTimeout(b.timeout);this.timeout=setTimeout(function(){b.validate()},b.wait)},doOnBlur:function(b){this.focused=false;this.validate(b)},doOnFocus:function(){this.focused=true;this.removeMessageAndFieldClass()},getElementType:function(){var b=this.element.nodeName.toUpperCase(),a=this.element.type.toUpperCase();switch(true){case b=="TEXTAREA":return LiveValidation.TEXTAREA;case b=="INPUT"&&a=="TEXT":return LiveValidation.TEXT;case b=="INPUT"&&a=="PASSWORD":return LiveValidation.PASSWORD;
case b=="INPUT"&&a=="CHECKBOX":return LiveValidation.CHECKBOX;case b=="INPUT"&&a=="FILE":return LiveValidation.FILE;case b=="SELECT":return LiveValidation.SELECT;case b=="INPUT":throw Error("LiveValidation::getElementType - Cannot use LiveValidation on an "+a.toLowerCase()+" input!");default:throw Error("LiveValidation::getElementType - Element must be an input, select, or textarea - "+b.toLowerCase()+" was given!");}},doValidations:function(){this.validationFailed=false;for(var b=0,a=this.validations.length;b<
a;++b)if(this.validationFailed=!this.validateElement(this.validations[b].type,this.validations[b].params))return false;this.message=this.validMessage;return true},validateElement:function(b,a){switch(b){case Validate.Presence:case Validate.Confirmation:case Validate.Acceptance:this.displayMessageWhenEmpty=true;break;case Validate.Custom:if(a.displayMessageWhenEmpty)this.displayMessageWhenEmpty=true}var d=this.elementType==LiveValidation.SELECT?this.element.options[this.element.selectedIndex].value:
this.element.value;if(b==Validate.Acceptance){if(this.elementType!=LiveValidation.CHECKBOX)throw Error("LiveValidation::validateElement - Element to validate acceptance must be a checkbox!");d=this.element.checked}var c=true;try{b(d,a)}catch(e){if(e instanceof Validate.Error){if(d!==""||d===""&&this.displayMessageWhenEmpty){this.validationFailed=true;this.message=e.message.split("\n")[0];c=false}}else throw e;}finally{return c}},validate:function(){if(this.element.disabled)return true;else{this.beforeValidation();
if(this.doValidations()){this.beforeValid();this.onValid();this.afterValid();this.afterValidation();return true}else{this.beforeInvalid();this.onInvalid();this.afterInvalid();this.afterValidation();return false}}},enable:function(){this.element.disabled=false;return this},disable:function(){this.element.disabled=true;this.removeMessageAndFieldClass();return this},createMessageSpan:function(){var b=document.createElement("span"),a=document.createTextNode(this.message);b.appendChild(a);return b},insertMessage:function(b){this.removeMessage();
if(this.validationFailed||this.validMessage)if(this.displayMessageWhenEmpty&&(this.elementType==LiveValidation.CHECKBOX||this.element.value=="")||this.element.value!=""){b.className+=" "+this.messageClass+" "+(this.validationFailed?this.invalidClass:this.validClass);var a=this.insertAfterWhatNode.parentNode;this.insertAfterWhatNode.nextSibling?a.insertBefore(b,this.insertAfterWhatNode.nextSibling):a.appendChild(b)}},addFieldClass:function(){this.removeFieldClass();if(this.validationFailed){if(this.element.className.indexOf(this.invalidFieldClass)==
-1)this.element.className+=" "+this.invalidFieldClass}else if(this.displayMessageWhenEmpty||this.element.value!="")if(this.element.className.indexOf(this.validFieldClass)==-1)this.element.className+=" "+this.validFieldClass},removeMessage:function(){for(var b,a=this.insertAfterWhatNode;a.nextSibling;){if(a.nextSibling.nodeType===1){b=a.nextSibling;break}a=a.nextSibling}b&&b.className.indexOf(this.messageClass)!=-1&&this.insertAfterWhatNode.parentNode.removeChild(b)},removeFieldClass:function(){var b=
this.element.className;if(b.indexOf(this.invalidFieldClass)!=-1)this.element.className=b.split(this.invalidFieldClass).join("");if(b.indexOf(this.validFieldClass)!=-1)this.element.className=b.split(this.validFieldClass).join(" ")},removeMessageAndFieldClass:function(){this.removeMessage();this.removeFieldClass()}};var LiveValidationForm=function(b){this.initialize(b)};LiveValidationForm.instances={};
LiveValidationForm.getInstance=function(b){if(!b)throw Error("LiveValidationForm::getInstance - No element reference or element id has been provided!");b=b.nodeName?b:document.getElementById(b);var a=Math.random()*Math.random();if(!b.id)b.id="formId_"+a.toString().replace(/\./,"")+(new Date).valueOf();LiveValidationForm.instances[b.id]||(LiveValidationForm.instances[b.id]=new LiveValidationForm(b));return LiveValidationForm.instances[b.id]};
LiveValidationForm.prototype={beforeValidation:function(){},onValid:function(){},onInvalid:function(){},afterValidation:function(){},initialize:function(b){this.name=b.id;this.element=b;this.fields=[];this.oldOnSubmit=this.element.onsubmit||function(){};var a=this;this.element.onsubmit=function(d){var c=false;a.beforeValidation();a.valid=LiveValidation.massValidate(a.fields);a.valid?a.onValid():a.onInvalid();a.afterValidation();if(a.valid)c=a.oldOnSubmit.call(this,d||window.event)!==false;if(!c)return c}},
addField:function(b){this.fields.push(b)},removeField:function(b){for(var a=[],d=0,c=this.fields.length;d<c;d++)this.fields[d]!==b&&a.push(this.fields[d]);this.fields=a},destroy:function(b){if(this.fields.length!=0&&!b)return false;this.element.onsubmit=this.oldOnSubmit;LiveValidationForm.instances[this.name]=null;return true}};
var Validate={Presence:function(b,a){a=a||{};var d=a.failureMessage||"Can't be empty!";if(b===""||b===null||b===undefined)Validate.fail(d);return true},Numericality:function(b,a){var d=b;b=Number(b);a=a||{};var c=a.minimum||a.minimum==0?a.minimum:null,e=a.maximum||a.maximum==0?a.maximum:null,f=a.is||a.is==0?a.is:null,g=a.notANumberMessage||"Must be a number!",h=a.notAnIntegerMessage||"Must be an integer!",k=a.wrongNumberMessage||"Must be "+f+"!",i=a.tooLowMessage||"Must not be less than "+c+"!",j=
a.tooHighMessage||"Must not be more than "+e+"!";isFinite(b)||Validate.fail(g);if(a.onlyInteger&&(/\.0+$|\.$/.test(String(d))||b!=parseInt(b)))Validate.fail(h);switch(true){case f!==null:b!=Number(f)&&Validate.fail(k);break;case c!==null&&e!==null:Validate.Numericality(b,{tooLowMessage:i,minimum:c});Validate.Numericality(b,{tooHighMessage:j,maximum:e});break;case c!==null:b<Number(c)&&Validate.fail(i);break;case e!==null:b>Number(e)&&Validate.fail(j)}return true},Format:function(b,a){b=String(b);
a=a||{};var d=a.failureMessage||"Not valid!",c=a.pattern||/./,e=a.negate||false;!e&&!c.test(b)&&Validate.fail(d);e&&c.test(b)&&Validate.fail(d);return true},Email:function(b,a){a=a||{};Validate.Format(b,{failureMessage:a.failureMessage||"Must be a valid email address!",pattern:/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i});return true},Length:function(b,a){b=String(b);a=a||{};var d=a.minimum||a.minimum==0?a.minimum:null,c=a.maximum||a.maximum==0?a.maximum:null,e=a.is||a.is==0?a.is:null,f=a.wrongLengthMessage||
"Must be "+e+" characters long!",g=a.tooShortMessage||"Must not be less than "+d+" characters long!",h=a.tooLongMessage||"Must not be more than "+c+" characters long!";switch(true){case e!==null:b.length!=Number(e)&&Validate.fail(f);break;case d!==null&&c!==null:Validate.Length(b,{tooShortMessage:g,minimum:d});Validate.Length(b,{tooLongMessage:h,maximum:c});break;case d!==null:b.length<Number(d)&&Validate.fail(g);break;case c!==null:b.length>Number(c)&&Validate.fail(h);break;default:throw Error("Validate::Length - Length(s) to validate against must be provided!");
}return true},Inclusion:function(b,a){a=a||{};var d=a.failureMessage||"Must be included in the list!",c=a.caseSensitive===false?false:true;if(a.allowNull&&b==null)return true;!a.allowNull&&b==null&&Validate.fail(d);var e=a.within||[];if(!c){var f=[],g=0;for(c=e.length;g<c;++g){var h=e[g];if(typeof h=="string")h=h.toLowerCase();f.push(h)}e=f;if(typeof b=="string")b=b.toLowerCase()}f=false;g=0;for(c=e.length;g<c;++g){if(e[g]==b)f=true;if(a.partialMatch)if(b.indexOf(e[g])!=-1)f=true}if(!a.negate&&!f||
a.negate&&f)Validate.fail(d);return true},Exclusion:function(b,a){a=a||{};a.failureMessage=a.failureMessage||"Must not be included in the list!";a.negate=true;Validate.Inclusion(b,a);return true},Confirmation:function(b,a){if(!a.match)throw Error("Validate::Confirmation - Error validating confirmation: Id of element to match must be provided!");a=a||{};var d=a.failureMessage||"Does not match!",c=a.match.nodeName?a.match:document.getElementById(a.match);if(!c)throw Error("Validate::Confirmation - There is no reference with name of, or element with id of '"+
a.match+"'!");b!=c.value&&Validate.fail(d);return true},Acceptance:function(b,a){a=a||{};var d=a.failureMessage||"Must be accepted!";b||Validate.fail(d);return true},Custom:function(b,a){a=a||{};var d=a.failureMessage||"Not valid!";(a.against||function(){return true})(b,a.args||{})||Validate.fail(d);return true},now:function(b,a,d){if(!b)throw Error("Validate::now - Validation function must be provided!");var c=true;try{b(a,d||{})}catch(e){if(e instanceof Validate.Error)c=false;else throw e;}finally{return c}},
fail:function(b){throw new Validate.Error(b);},Error:function(b){this.message=b;this.name="ValidationError"}};
