/*--------------------------------------------------
UKISA.WIDGET.FORM-VALIDATION
Version:   0.0.26
Generated: 24/11/2009
--------------------------------------------------*/
var UKISA=UKISA||{};UKISA.widget=UKISA.widget||{};UKISA.widget.FormValidation=function(c,m){var f,k,h,j;this.config={showAllErrors:false,debug:false,display:null,callback:null};this.context={form:null,log:[],rules:{},errors:0,builder:null,failure:false};c=document.getElementById(c);if(!c){this.log("Cannot find form: "+c);return}k=this;j=YAHOO.util.Event;this.context.form=c;j.addListener(this.context.form,"submit",function(o){var n=k.validate();if(n){k.log("The form is valid");k.log("Callback check: "+typeof k.config.callback);if(typeof k.config.callback==="function"){k.log("Callback: "+k.config.callback);if(window.console){console.info(k.config.callback)}return k.config.callback.call(k,o)}return true}else{k.log("The form is NOT valid");j.stopEvent(o);return false}});this.log("Found form: "+c.getAttribute("id"));if(typeof UKISA.locale!=="undefined"){h=UKISA.locale.get("widget.FormValidation");this.log("Found locale settings.");if(h&&h.messages){this.config.messages=h.messages}else{this.config.messages=UKISA.widget.FormValidation.messages}if(h&&h.display){this.config.display=h.display}}else{this.log("No locale settings.");this.config.messages=UKISA.widget.FormValidation.messages}for(var f in m){if(typeof this.config[f]!=="undefined"){this.config[f]=m[f]}}if(this.config.debug){window.onbeforeunload=function(){return false}}var d,e,b,g,l,a;d=document.createElement("div");e=d.cloneNode(false);b=d.cloneNode(false);g=d.cloneNode(false);d.className="validation-error";d.style.position="absolute";d.style.left="-999em";e.className="validation-error-header";b.className="validation-error-content";g.className="validation-error-footer";l=document.createElement("p");l.className="close";a=document.createElement("a");l.appendChild(a);d.appendChild(e);b.appendChild(l);d.appendChild(b);d.appendChild(g);document.body.appendChild(d);d.style.display="none";document.body.removeChild(d)};UKISA.widget.FormValidation.prototype.isValid=false;UKISA.widget.FormValidation.prototype={disable:function(a){if(this.context.rules[a]){this.context.rules[a]["active"]=false;this.log("Disabled element: "+a)}else{this.log("Cannot disable element: "+a)}},enable:function(a){if(this.context.rules[a]){this.context.rules[a]["active"]=true;this.log("Enabled element: "+a)}else{this.log("Cannot enable element: "+a)}},add:function(c){var a,b;a=this;b={is:function(e,d){if(a.context.builder.el){a.context.builder.options[e]=d;a.context.builder.lastOption=e}a.log("Validation builder: "+a.context.builder.el+", is: "+e);return this},andIs:function(e,d){this.is.apply(this,arguments);return this},withMessage:function(d){if(typeof a.context.builder.options.message==="undefined"){a.context.builder.options.message={}}a.context.builder.options.message[a.context.builder.lastOption]=d;return this}};if(!this.context.builder){this.context.builder={el:null,options:{},lastOption:""}}if(this.context.builder.el){this.rule(this.context.builder.el,this.context.builder.options)}this.context.builder.el=c;this.context.builder.options={};this.context.builder.lastOption="";this.log("Validation builder: "+c);return b},rule:function(b,k){var j,h,f,c,d,g,e,a;j=this;h=YAHOO.util.Event;if(!this.context.form){return}f=function(m){var l;m=m||window.event;l=(m.keyCode)?m.keyCode:m.which;j.log("KeyUp event: "+l);if(l!==9&&l!==16){j.log("Validate from data change event: "+String.fromCharCode(l));j.validate(b)}};if(this.context.form[b]){this.log("Found form input: "+b);this.context.rules[b]=k;this.context.rules[b]["active"]=true;if(typeof this.context.form[b].nodeName!=="undefined"){c=this.context.form[b]}else{c=this.context.form[b][0]}if(typeof k.events!=="undefined"){for(a in k.events){if(k.events.hasOwnProperty(a)){if(a!=="submit"){h.addListener(c,a,f)}}}}else{d=c.getAttribute("type");switch(c.nodeName.toLowerCase()){case"select":c.onchange=f;break;case"input":if(d==="checkbox"||d==="radio"){g=this.context.form[b];if(typeof g.length!=="undefined"){for(e=0,ix=g.length;e<ix;e++){h.addListener(g[e],"click",f)}}else{h.addListener(g,"click",f)}}else{if(d==="password"){h.addListener(c,"blur",f)}else{h.addListener(c,"blur",f)}}break;case"textarea":h.addListener(c,"keyup",f);h.addListener(c,"blur",f);break;default:this.log("Unkown element node type.");break}}}else{this.log("Cannot find form input: "+b)}return this},validate:function(c){var a,b,d,e;if(!this.context.form){return true}if(this.context.builder&&this.context.builder.el){this.rule(this.context.builder.el,this.context.builder.options)}a=this;b=UKISA.widget.FormValidation.validators;d=this.config.messages;this.context.errors=0;e=function(f){var k,o,l,m,g,j,n,h;o=a.context.rules[f];if(typeof o.active!=="undefined"&&o.active==false){a.log("This element has been disabled: "+f);h=document.getElementById("error-"+a.context.form.id+"-"+f);if(h){h.parentNode.removeChild(h)}return}l=a.context.form[f];m=[];g=0;for(k in o){if(k!=="messages"&&k!=="errors"&&k!=="events"&&k!=="display"&&k!=="active"&&k!=="onError"&&k!=="onSuccess"){a.context.failure=false;if(typeof b[k]!=="undefined"){j=o[k];if(b[k].call(a,l,l.value,j)){a.log("Validating: "+f+" with rule: "+k+" and status: passed");if(o.onSuccess){o.onSuccess.call(a,l)}}else{a.log("Validating: "+f+" with rule: "+k+" and status: failed");a.context.failure=true;if(typeof o.messages!=="undefined"&&typeof o.messages[k]!=="undefined"){n=o.messages[k];a.log("Using custom message: "+n)}else{if(typeof d[k]!=="undefined"){n=d[k]}else{n="This is not valid.";a.log("Default message not found")}}m.push(n.replace(/\{[0-9]\}/g,function(p){var q=parseInt(p.charAt(1));if(typeof j==="object"){if(typeof j[q]!=="undefined"){return j[q]}else{return""}}else{return j}}));g++}}else{a.log("Cannot find validator: "+k+" for: "+f)}if(k==="compare"){a.log("Exception for 'compare': add onkeychange");l.onkeyup=l.onblur}}}a.context.rules[f].errors={length:g,messages:m};a.context.errors+=g;a.render(f)};if(c){e(c);return false}else{for(c in this.context.rules){e(c);if(!this.config.showAllErrors&&this.context.failure){break}}this.isValid=(this.context.errors===0);return this.isValid}},render:function(a){var u,b,m,p,l,c,t,q,B,f,j,o,z,e,g;m=this.context.rules[a].errors;p=this.context.form[a];if(typeof p.nodeName==="undefined"){p=p[0]}l=p.parentNode;b="error-"+this.context.form.id+"-"+a;u=document.getElementById(b);if(u){u.parentNode.removeChild(u)}c=document.createElement("div");t=c.cloneNode(false);B=c.cloneNode(false);q=c.cloneNode(false);c.className="validation-error";c.id=b;t.className="validation-error-header";B.className="validation-error-content";q.className="validation-error-footer";var r=document.createElement("p");r.className="close";var A=document.createElement("a");A.innerHTML="Close";A.title="Close";A.onclick=function(){var x=document.getElementById(b);x.parentNode.removeChild(x)};r.appendChild(A);f=document.createElement("ul");j=document.createElement("li");if(m.length){for(o in m.messages){if(m.messages.hasOwnProperty(o)){this.log("Form element: "+a+" has the error message: "+m.messages[o]);z=j.cloneNode(true);z.appendChild(document.createTextNode(m.messages[o]));f.appendChild(z)}}B.appendChild(f);c.appendChild(t);c.appendChild(B);c.appendChild(q);c.appendChild(r);var w=this.context.rules[a].display;if(typeof w!=="undefined"&&(w.insertBefore||w.insertAfter)){var v=YAHOO.util.Selector.query||document.getElementById;if(w.insertBefore){l=v(w.insertBefore);if(l){YAHOO.util.Dom.insertBefore(c,l)}}else{if(w.insertBefore){l=v(w.insertBefore);if(l){YAHOO.util.Dom.insertBefore(c,l)}}}this.log("Custom error message DOM insertion.")}else{l.insertBefore(c,p)}l.style.position="relative";g=YAHOO.util.Dom.getRegion(c);var k=(g.bottom-g.top)*-1;this.log("Default y position: "+k);if(this.config.display||typeof w!=="undefined"){this.log("Custom display for: "+a);w=w||{};var e=YAHOO.util.Dom.getRegion(p);var d=w.xPosition||this.config.display.xPosition;var C=w.xOffset||this.config.display.xOffset;var n=null;var s=w.yPosition||this.config.display.yPosition;var h=w.yOffset||this.config.display.yOffset;if(typeof d!=="undefined"){n=(e.left-e[d])*-1;this.log("xPosition: "+d+", x: "+n);if(typeof C!=="undefined"){n+=C;this.log("xOffset: "+C+", x: "+n)}c.style.left=n+"px"}if(typeof s!=="undefined"){k+=(e.top-e[s])*-1;this.log("yPosition: "+s+", y: "+k);if(typeof h!=="undefined"){k-=h;this.log("yOffset: "+h+", y: "+k)}}}c.style.top=k+"px";if(this.context.rules[a]["onError"]){this.context.rules[a]["onError"].call(this,this.context.form[a])}}},log:function(a){if(a){if(window.console){console.log(a)}this.context.log.push(a)}}};UKISA.widget.FormValidation.filters={trim:function(a){return a.replace(/^\s+|\s+$/g,"")},strip:function(a){return a.replace(/\s+/g,"")}};UKISA.widget.FormValidation.validators={required:function(c,b,a){if(typeof c.nodeName==="undefined"){return UKISA.widget.FormValidation.validators.checked(c,b,a)}else{if(c.getAttribute("type")==="checkbox"||c.getAttribute("type")==="radio"){return UKISA.widget.FormValidation.validators.checked(c,b,a)}else{return(b.length>0)?true:false}}},checked:function(e,c,a){if(typeof e.length!=="undefined"){for(var d=0,b=e.length;d<b;d++){if(e[d].checked){return true}}return false}else{return(e.checked)}},range:function(c,b,a){return(a[1])?(a[0]===0)?(b.length>0&&b.length<=a[1]):(b.length>=a[0]&&b.length<=a[1]):(b.length>=a[0])},email:function(c,b,a){b=b.toLowerCase();var d=new RegExp(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);return d.test(b)},luhn10:function(d,c,a){var f=c.replace(/\D/g,"");var b=f.length;var g=b%2;var e=0;for(i=0;i<b;i++){var h=f.charAt(i);if(i%2==g){h=h*2;if(h>9){h=h-9}}e=e+parseInt(h)}if(e%10==0){return true}else{return false}},nectar:function(d,c,a){if(c.length==0){return true}var f="98263000"+c.replace(/\D/g,"");var b=f.length;var g=b%2;var e=0;for(i=0;i<b;i++){var h=f.charAt(i);if(i%2==g){h=h*2;if(h>9){h=h-9}}e=e+parseInt(h)}if(e%10==0){return true}else{return false}},postcode:function(d,b,a){var e,c;if(b.length==0){return false}c=UKISA.widget.FormValidation.filters.strip(b.toUpperCase());if(!UKISA.widget.FormValidation.validators.alphaNumeric(c)){return false}e=new RegExp("^[A-Z]{1,2}([0-9]{1,2}|[0-9][A-Z])[0-9][ABD-HJLNP-UW-Z]{2}$");if(!e.test(c)){return false}return true},alphaNumeric:function(c,b,a){var d=new RegExp("[^A-Za-z0-9]");return !d.test(b)},numeric:function(d,b,a){if(b===""){return false}var c=parseFloat(b);return(!isNaN(c)||b==="")},phoneNumber:function(c,b,a){var d=new RegExp("^[0-9 ]*$");return d.test(b)},compare:function(c,b,a){var d;d=this.context.form[a];return(d&&d.value===b)},min:function(c,b,a){return(b.length>=a)},max:function(c,b,a){return(b.length<a)},maxWords:function(d,c,b){var a,e,f;a=b;e="";e=c.replace(/^\s+|\s+$/g,"");e=e.replace(/\s+/g," ");f=e.split(" ");return(f.length<=a)},integer:function(d,a,c){var b=parseInt(a);return(a.indexOf(".")<0&&(!isNaN(b)||a===""))},between:function(c,a,b){if(isNaN(a)){return false}return(a>=b[0]&&a<=b[1])}};UKISA.widget.FormValidation.messages={numeric:"This must be a number",required:"This is a required field",range:"Please enter between {0} and  {1} characters",email:"This email address does not appear to be correct",postcode:"Please enter a valid UK postcode",luhn10:"Please check you have entered your card number correctly",nectar:"Please check you have entered your Nectar card number correctly",compare:"Please ensure that this value is correct",min:"Please enter more than {0} characters",max:"Please enter less than {0} characters",maxWords:"You may only enter up to {0} words",integer:"Only whole numbers are accepted e.g. 14",between:"Please enter a number between {0} and  {1}"};
