/*--------------------------------------------------
FORM_VALIDATION
Version:   0.3.1
Generated: 31/3/2010
--------------------------------------------------*/
var UKISA=UKISA||{};UKISA.widget=UKISA.widget||{};UKISA.widget.FormValidation=function(h,m){var e,k,g,j;this.config={showAllErrors:false,quickValidation:false,debug:false,display:null,callback:null,scrollTo:true};this.context={form:null,log:[],rules:{},errors:0,builder:null,failure:false,errored:[],submit:null};this.validateFieldUpdate=function(o){var n;o=o||window.event;n=(o.keyCode)?o.keyCode:o.which;k.log("KeyUp event: "+n);if(n!==9&&n!==16){k.log("Validate from data change event: "+String.fromCharCode(n));k.validate(el)}};el=document.getElementById(h);if(!el){this.log("Cannot find form: "+h);return}k=this;j=YAHOO.util.Event;this.context.form=el;j.addListener(this.context.form,"submit",function(o){var n=k.validate();if(n){k.log("The form is valid");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.preventDefault(o);j.stopEvent(o);return false}});this.log("Found form: "+el.getAttribute("id"));if(typeof UKISA.locale!=="undefined"){g=UKISA.locale.get("widget.FormValidation");this.log("Found locale settings.");if(g&&g.messages){this.config.messages=g.messages}else{this.config.messages=UKISA.widget.FormValidation.messages}if(g&&g.display){this.config.display=g.display}}else{this.log("No locale settings.");this.config.messages=UKISA.widget.FormValidation.messages}for(var e in m){if(typeof this.config[e]!=="undefined"){this.config[e]=m[e]}}if(this.config.debug){window.onbeforeunload=function(){return false}}var c,d,b,f,l,a;c=document.createElement("div");d=c.cloneNode(false);b=c.cloneNode(false);f=c.cloneNode(false);c.className="validation-error";c.style.position="absolute";c.style.left="-999em";d.className="validation-error-header";b.className="validation-error-content";f.className="validation-error-footer";l=document.createElement("p");l.className="close";a=document.createElement("a");l.appendChild(a);c.appendChild(d);b.appendChild(l);c.appendChild(b);c.appendChild(f);document.body.appendChild(c);c.style.display="none";document.body.removeChild(c)};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}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,this.validateFieldUpdate)}}}}else{d=c.getAttribute("type");switch(c.nodeName.toLowerCase()){case"select":c.onchange=this.validateFieldUpdate;break;case"input":if(d==="checkbox"||d==="radio"){g=this.context.form[b];if(typeof g.length!=="undefined"&&this.config.quickValidation){for(e=0,ix=g.length;e<ix;e++){h.addListener(g[e],"click",this.validateFieldUpdate)}}else{if(this.config.quickValidation){h.addListener(g,"click",this.validateFieldUpdate)}}}else{if(d==="password"){if(this.config.quickValidation){h.addListener(c,"blur",this.validateFieldUpdate)}}else{if(this.config.quickValidation){h.addListener(c,"blur",this.validateFieldUpdate)}}}break;case"textarea":if(this.config.quickValidation){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(a){var k,d,e,j;if(!this.context.form){return true}if(this.context.builder&&this.context.builder.el){this.rule(this.context.builder.el,this.context.builder.options)}k=this;d=UKISA.widget.FormValidation.validators;e=this.config.messages;this.context.errors=0;this.context.errored=[];j=function(l){var p,t,q,r,m,o,s,n;t=k.context.rules[l];if(typeof t.active!=="undefined"&&t.active==false){k.log("This element has been disabled: "+l);n=document.getElementById("error-"+k.context.form.id+"-"+l);YAHOO.util.Event.purgeElement(n,true);if(n){n.parentNode.removeChild(n)}return}q=k.context.form[l];r=[];m=0;for(p in t){if(p!=="messages"&&p!=="errors"&&p!=="events"&&p!=="display"&&p!=="active"&&p!=="onError"&&p!=="onSuccess"){k.context.failure=false;if(typeof d[p]!=="undefined"){o=t[p];if(d[p].call(k,q,q.value,o)){k.log("Validating: "+l+" with rule: "+p+" and status: passed");if(t.onSuccess){t.onSuccess.call(k,q)}}else{k.log("Validating: "+l+" with rule: "+p+" and status: failed");k.context.errored.push(l);k.context.failure=true;if(typeof t.messages!=="undefined"&&typeof t.messages[p]!=="undefined"){s=t.messages[p];k.log("Using custom message: "+s)}else{if(typeof e[p]!=="undefined"){s=e[p]}else{s="This is not valid.";k.log("Default message not found")}}r.push(s.replace(/\{[0-9]\}/g,function(u){var v=parseInt(u.charAt(1));if(typeof o==="object"){if(typeof o[v]!=="undefined"){return o[v]}else{return""}}else{return o}}));m++}}else{k.log("Cannot find validator: "+p+" for: "+l)}if(p==="compare"){k.log("Exception for 'compare': add onkeychange");q.onkeyup=q.onblur}}}k.context.rules[l].errors={length:m,messages:r};k.context.errors+=m;k.render(l)};if(a){j(a);return false}else{for(a in this.context.rules){j(a);if(!this.config.showAllErrors&&this.context.failure){break}}this.isValid=(this.context.errors===0);if(!this.isValid&&this.config.scrollTo){var c=document.getElementById("form-validation-dummy");if(!c){this.context.submit=YAHOO.util.Selector.query("input.submit",this.context.form,true);c=document.createElement("div");c.id="form-validation-dummy";c.style.position="absolute";c.style.left="-999em";document.body.appendChild(c)}var f=YAHOO.util.Dom.getDocumentScrollTop();c.style.top=f;var b=YAHOO.util.Dom.getRegion(this.context.submit);var h=YAHOO.util.Dom.getRegion(document.getElementById("error-"+k.context.form.id+"-"+this.context.errored[0]));var g=new YAHOO.util.Anim("form-validation-dummy",{top:{from:f,to:h.top-20}},0.6,YAHOO.util.Easing.easeOut);g.onTween.subscribe(function(l,m){window.scrollTo(0,Math.ceil(this.getAttribute("top")))});g.animate()}return this.isValid}},render:function(a){var u,b,n,p,m,c,t,q,z,h,l,o,x,e,j;n=this.context.rules[a].errors;p=this.context.form[a];if(typeof p.nodeName==="undefined"){p=p[0]}m=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);z=c.cloneNode(false);q=c.cloneNode(false);c.className="validation-error";c.id=b;t.className="validation-error-header";z.className="validation-error-content";q.className="validation-error-footer";var r=document.createElement("p");r.className="close";var y=document.createElement("a");y.innerHTML="Close";y.title="Close";r.appendChild(y);YAHOO.util.Event.addListener(y,"mousedown",function(C){var B=document.getElementById(b);B.parentNode.removeChild(B);YAHOO.util.Event.preventDefault(C);YAHOO.util.Event.stopEvent(C)});h=document.createElement("ul");l=document.createElement("li");if(n.length){for(o in n.messages){if(n.messages.hasOwnProperty(o)){this.log("Form element: "+a+" has the error message: "+n.messages[o]);x=l.cloneNode(true);x.appendChild(document.createTextNode(n.messages[o]));h.appendChild(x)}}z.appendChild(h);c.appendChild(t);c.appendChild(z);c.appendChild(q);c.appendChild(r);var w=this.context.rules[a].display;if(false&&typeof w!=="undefined"&&(w.insertBefore||w.insertAfter)){var v=YAHOO.util.Selector.query||document.getElementById;if(w.insertBefore){m=v(w.insertBefore);if(m){YAHOO.util.Dom.insertBefore(c,m)}}else{if(w.insertBefore){m=v(w.insertBefore);if(m){YAHOO.util.Dom.insertBefore(c,m)}}}this.log("Custom error message DOM insertion.")}else{m.insertBefore(c,p)}m=YAHOO.util.Dom.getAncestorByTagName(p,"dd");m.appendChild(c);j=YAHOO.util.Dom.getRegion(c);var e=YAHOO.util.Dom.getRegion(p);var g=e.left;var f=e.top-j.height;if(YAHOO.util.Dom.getStyle(m,"position")==="relative"){this.log("Container has a relative position.");g=0;f=j.height*-1}if(this.config.display||typeof w!=="undefined"){this.log("Custom display for: "+a);var d="left";var s="top";if(this.config.display&&this.config.display.xPosition){d=this.config.display.xPosition}if(this.config.display&&this.config.display.yPosition){s=this.config.display.yPosition}if(w&&w.xPosition){s=w.xPosition}if(w&&w.yPosition){s=w.yPosition}var A=0;var k=0;if(this.config.display&&this.config.display.xOffset){A=this.config.display.xOffset}if(this.config.display&&this.config.display.yOffset){k=this.config.display.yOffset}if(w&&w.xOffset){A=w.xOffset}if(w&&w.yOffset){k=w.yOffset}if(d==="right"){g=e.left+e.width}if(A){g+=A}if(k){f+=j.height+k}}if(this.context.rules[a]["onError"]){this.context.rules[a]["onError"].call(this,this.context.form[a])}}},log:function(a){if(this.debug&&a){if(window.console){console.log(a)}else{if(this.config.debug){var b=document.createElement("p");b.innerHTML=a;document.body.appendChild(b)}}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={not:function(e,c,a){var d,b;for(d=0,b=a.length;d<b;d++){if(c===a[d]){return false}}return true},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){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 is not a valid 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}",not:"Please enter the required information"};