var UKISA=UKISA||{};UKISA.widget=UKISA.widget||{};UKISA.widget.FormValidation=function(e,b){var d,a,c;this.config={showAllErrors:false,debug:false,display:null};this.context={form:null,log:[],rules:{},errors:0,builder:null};e=document.getElementById(e);if(!e){this.log("Cannot find form: "+e);return}a=this;this.context.form=e;this.context.form.onsubmit=function(){return a.validate()};this.log("Found form: "+e.getAttribute("id"));for(var d in b){if(typeof this.config[d]!=="undefined"){this.config[d]=b[d]}}if(UKISA.locale){c=UKISA.locale.get("widgets.FormValidation.messages");if(c){this.config.messages=c}else{this.config.messages=UKISA.widget.FormValidation.messages}}else{this.config.messages=UKISA.widget.FormValidation.messages}if(this.config.debug){window.onbeforeunload=function(){return false}}};UKISA.widget.FormValidation.prototype={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(l){var i;l=l||window.event;i=(l.keyCode)?l.keyCode:l.which;j.log("KeyUp event: "+i);if(i!==9&&i!==16){j.log("Validate from data change event: "+String.fromCharCode(i));j.validate(b)}};if(this.context.form[b]){this.log("Found form input: "+b);this.context.rules[b]=k;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},valid:function(){},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(k){var m,h,f,n,j,g,l;h=a.context.rules[k];f=a.context.form[k];n=[];j=0;for(m in h){if(m!=="messages"&&m!=="errors"&&m!=="events"){if(typeof b[m]!=="undefined"){g=h[m];if(b[m].call(a,f,f.value,g)){a.log("Validating: "+k+" with rule: "+m+" and status: passed")}else{a.log("Validating: "+k+" with rule: "+m+" and status: failed");if(typeof h.messages!=="undefined"&&typeof h.messages[m]!=="undefined"){l=h.messages[m];a.log("Using custom message: "+l)}else{if(typeof d[m]!=="undefined"){l=d[m]}else{a.log("Default message not found")}}n.push(l.replace(/\{[0-9]\}/g,function(o){var p=parseInt(o.charAt(1));if(typeof g==="object"){if(typeof g[p]!=="undefined"){return g[p]}else{return""}}else{return g}}));j++}}else{a.log("Cannot find validator: "+m+" for: "+k)}if(m==="compare"){a.log("Exception for 'compare': add onkeychange");f.onkeyup=f.onblur}}}a.context.rules[k].errors={length:j,messages:n};a.context.errors+=j;a.render(k)};if(c){e(c);return false}else{for(c in this.context.rules){e(c)}return(this.context.errors===0)}},render:function(a){var t,b,l,o,k,c,s,p,z,f,i,n,v,e,g;l=this.context.rules[a].errors;o=this.context.form[a];if(typeof o.nodeName==="undefined"){o=o[0]}k=o.parentNode;b="error-"+this.context.form.id+"-"+a;t=document.getElementById(b);if(t){t.parentNode.removeChild(t)}c=document.createElement("div");s=c.cloneNode(false);z=c.cloneNode(false);p=c.cloneNode(false);c.className="validation-error";c.id=b;s.className="header";z.className="content";p.className="footer";var q=document.createElement("p");q.className="close";var w=document.createElement("a");w.innerHTML="Close";w.title="Close";w.onclick=function(){var x=document.getElementById(b);x.parentNode.removeChild(x)};q.appendChild(w);f=document.createElement("ul");i=document.createElement("li");if(l.length){for(n in l.messages){if(l.messages.hasOwnProperty(n)){this.log("Form element: "+a+" has the error message: "+l.messages[n]);v=i.cloneNode(true);v.appendChild(document.createTextNode(l.messages[n]));f.appendChild(v)}}z.appendChild(f);c.appendChild(s);c.appendChild(z);c.appendChild(p);c.appendChild(q);k.insertBefore(c,o);k.style.position="relative";var u=this.context.rules[a].display;g=YAHOO.util.Dom.getRegion(c);var j=(g.bottom-g.top)*-1;this.log("Default y position: "+j);if(this.config.display||typeof u!=="undefined"){this.log("Custom display for: "+a);u=u||{};var e=YAHOO.util.Dom.getRegion(o);var d=u.xPosition||this.config.display.xPosition;var A=u.xOffset||this.config.display.xOffset;var m=null;var r=u.yPosition||this.config.display.yPosition;var h=u.yOffset||this.config.display.yOffset;if(typeof d!=="undefined"){m=(e.left-e[d])*-1;this.log("xPosition: "+d+", x: "+m);if(typeof A!=="undefined"){m+=A;this.log("xOffset: "+A+", x: "+m)}c.style.left=m+"px"}if(typeof r!=="undefined"){j+=(e.top-e[r])*-1;this.log("yPosition: "+r+", y: "+j);if(typeof h!=="undefined"){j-=h;this.log("yOffset: "+h+", y: "+j)}}}c.style.top=j+"px"}},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)},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)}};UKISA.widget.FormValidation.messages={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",compare:"Please ensure that this value is correct",min:"Please enter more than {0} characters",max:"Please enter less than {0} characters"};
