/*
	Written by Bborie Park
	Copyright (c) 2008 California Vectorborne Disease Surveillance System
	Licensed under the GNU GPL

	Depends upon jquery.util.js for the following functions:
		objectLength
		objectSort
		strRight
*/
(function(f){f.extend({Validate:new (function(){function r(a,b){b=typeof b.alternate!="undefined"?b.alternate:null;var c=false;a+="";if(a.length<1)c=true;else if(typeof a=="undefined")c=true;else if(a==null)c=true;return c?b!=null?b:false:a}function n(a,b){if(isNaN(a))return false;return((typeof b.signed=="boolean"?b.signed:true)?/^-?\d+$/:/^\d+$/).test(a)?a:false}function o(a,b){if(isNaN(a))return false;b=(b.signed&&typeof b.signed=="boolean"?b.signed:true)?/^-?\d+\.?\d*$/:/^\d+\.?\d*$/;var c=f.trim(a.replace(/^(-\.){1}/,
"-0."));c=f.trim(c.replace(/^(\.){1}/,"0."));return b.test(c)?a:false}function p(a,b){a+="";var c=b.mask||"Y-M-D";b=typeof b.raw=="boolean"?b.raw:false;for(var d=f.Validate.MASK_DATE,h={m:null,d:null,y:null},g=c.split(""),j=g.length,e=0;e<j;e++){var i=g[e];if(d[i]&&h[i.toLowerCase()]==null){if(i.toLowerCase()=="d"){var l=false;if(e==0)l=true;else if(g[e-1]!="\\")l=true;if(l)g[e]=d[i][1]}else g[e]=d[i][1];h[i.toLowerCase()]=e}else d[i]||(g[e]="\\"+g[e]+"?")}a=f.trim(a);g=g.join("");g=a.match(new RegExp("^"+
g+"$"));if(g==null)return false;a=f.Util.objectSort(h);e=1;for(var k in a){if(g[e])a[k]=g[e];else switch(k){case "y":a[k]=(new Date).getFullYear();break;default:a[k]=1;break}e++}for(e in h){if(!n(a[e],{signed:false}))return false;if(e=="y"&&a[e].length!=4){k=(""+(new Date).getFullYear()).split("");switch(a[e].length){case 1:a[e]=k[2]+a[e];case 2:a[e]=k[1]+a[e];case 3:a[e]=k[0]+a[e];break}}}if(f.Util.checkDate(a.m,a.d,a.y)){for(var m in a)switch(m){case "y":if(c.indexOf("y")!=-1)a.y=(new String(a.y)).substr(2);
break;case "m":case "d":if(c.indexOf(m.toUpperCase())!=-1&&(a[m]+"").length==1)a[m]="0"+a[m];break}g=c.split("");for(e=0;e<g.length;e++){i=g[e];if(d[i]&&a[i.toLowerCase()]!=null)g[e]=a[i.toLowerCase()]}return b?{raw:a,clean:g.join("")}:g.join("")}else return false}function z(a,b){b=b||null;switch(f.trim(uDataType.toLowerCase())){case "int":case "integer":return n(a,b);case "float":case "double":case "real":return o(a,b);case "date":return p(a,b);default:return false}}function s(a,b){a+="";b={is:typeof b.is!=
"undefined"?b.is:null,min:typeof b.min!="undefined"?b.min:null,max:typeof b.max!="undefined"?b.max:null};a=typeof a.length!="undefined"?a.length:f.Util.objectLength(a);if(typeof a=="undefined")return false;var c=true;if(b.is!=null&&a!=b.is)c=false;else if(b.min!=null)if(a<b.min)c=false;else{if(b.max!=null&&a>b.max)c=false}else if(b.max!=null&&a>b.max)c=false;return c}function t(a,b){a*=1;b={min:typeof b.min!="undefined"?b.min:null,max:typeof b.max!="undefined"?b.max:null};var c=true;if(b.min!=null)if(a<
b.min)c=false;else{if(b.max!=null&&a>b.max)c=false}else if(b.max!=null&&a>b.max)c=false;return c}function u(a,b){a+="";if(typeof b.mask=="undefined")return false;var c=b.mask+"";b=c.length;c=c.split("");for(var d=[],h=f.Validate.MASK_FORMAT,g=[],j=0;j<b;j++){var e=c[j];if(h[e]){d.push(h[e]);g.push(j)}else d.push("\\"+e+"?")}a=f.trim(a);d=d.join("");a=a.match(new RegExp("^"+d+"$"));if(a==null)return false;d=[];h=1;for(j=0;j<b;j++){e=c[j];if(f.inArray(j,g)===-1)d.push(e);else{a[h]&&a[h].length&&d.push(a[h]);
h++}}return d.join("")}function v(a,b){var c=this,d=b.reverse&&typeof b.reverse=="boolean"?b.reverse:false,h=b.nocase&&typeof b.nocase=="boolean"?b.nocase:true,g=a+"",j;if(!b.reference)return false;reference=typeof b.reference=="function"?b.reference.call(c):b.reference;if(typeof reference=="string")j=true;else if(reference instanceof Array)j=false;else return false;if(j&&!f.Validate.VALIDATE_REMOTE)return true;var e={value:encodeURIComponent(a)};b.data&&typeof b.data=="object"&&f.each(b.data,function(i,
l){e[i]=typeof l=="function"?l.call(c):l});if(j)return f.ajax({url:reference,data:e,timeout:3E4,success:function(i){i=i.length?true:false;c.results.context=d?!i:i;typeof c.rules.callback!=null&&c.rules.callback.call(c)},error:function(){c.results.context=d?false:true;typeof c.rules.callback!=null&&c.rules.callback.call(c)}});else{b=false;b=[];b=reference.slice(0);if(b.length<1)return a;if(h){g=g.toLowerCase();for(a=0;a<b.length;a++)b[a]=(b[a]+"").toLowerCase()}b=f.Util.arrayIndexOf(b,g)!=-1;return d?
!b:b}}function w(a){var b=this.rules;this.results={};var c,d;uValueSet=!(a instanceof Array)?[a]:a.splice(0);for(a=0;a<uValueSet.length;a++){c=uValueSet[a];for(d in b)switch(f.trim(d.toLowerCase())){case "empty":this.results[d]=r(c,b[d]);break;case "datatype":this.results[d]=z(c,b[d]);break;case "int":this.results[d]=n(c,b[d]);break;case "float":this.results[d]=o(c,b[d]);break;case "date":this.results[d]=p(c,b[d]);break;case "length":this.results[d]=s(c,b[d]);break;case "minmax":this.results[d]=t(c,
b[d]);break;case "format":this.results[d]=u(c,b[d]);break;case "context":this.results[d]=v.call(this,c,b[d]);break;case "form":break;case "debug":case "callback":break;default:if(typeof b[d]=="function")this.results[d]=b[d].call(this);break}}return this.results}function x(){var a,b=this;switch(b.type){case "select-one":a=b.options[b.selectedIndex].value;break;case "select-multiple":a=[];for(var c=0;c<b.options.length;c++)b.options[c].selected&&a.push(b.options[c].value);a=!a.length?"":a;break;default:a=
f(b).val();break}return w.call(b,a)}function y(){var a,b=this;switch(b.type){case "checkbox":case "radio":a=[];if(typeof b.name!="string")b.checked&&a.push(b.value);else{var c=b.form[b.name];if("checked"in c)c.checked&&a.push(c.value);else for(var d=0;d<c.length;d++)c[d].checked&&a.push(c.value)}a=!a.length?"":a.join(",");case "image":case "button":return w.call(b,a);case "submit":b.results=[];f.Validate.VALIDATE_REMOTE=false;f.Validate.inSubmit=true;f(":input",b.form).not(":submit, input[type=hidden], :reset").each(function(h){if(typeof this.rules!=
"undefined"){switch(q(this.type)){case "change":x.call(this);break;case "click":y.call(this);break}typeof this.rules.callback=="function"&&this.rules.callback.call(this);b.results[h]=f.Validate.resultsSimplify(this)}});f.Validate.VALIDATE_REMOTE=true;f.Validate.inSubmit=false;for(d=0;d<b.results.length;d++){if(b.results[d]==null)return null;if(b.results[d]==false)return false}return true}}function q(a){switch(a){case "select-one":case "select-multiple":case "text":case "textarea":case "password":case "file":return"change";
case "checkbox":case "radio":case "submit":case "image":case "button":return"click";case "reset":default:return}}this.validateEmpty=r;this.validateInt=n;this.validateFloat=o;this.validateDate=p;this.validateLength=s;this.validateMinMax=t;this.validateFormat=u;this.validateContext=v;this.VALIDATE_REMOTE=true;this.MASK_DATE={d:[null,"([0-9]{0,2})"],D:[null,"([0-9]{0,2})"],m:["m+","([0-9]{0,2})"],M:["M+","([0-9]{0,2})"],y:["y+","([0-9]{0,4})"],Y:["Y+","([0-9]{0,4})"]};this.MASK_FORMAT={"9":"(\\d|\\+|\\-)?",
"#":"(\\d|\\+|\\-){1}","?":"([a-zA-Z])?",L:"([a-zA-Z]){1}",a:"(\\w)?",A:"(\\w){1}",_:"(.)?","&":"(.){1}"};this.formattedDate=function(a,b){var c=f.Validate.MASK_DATE;a=new Date(a);a={y:a.getUTCFullYear(),m:a.getUTCMonth()+1,d:a.getUTCDate()};for(var d in a)switch(d){case "y":if(b.indexOf("y")!=-1)a.y=(new String(a.y)).substr(2);break;case "m":case "d":if(b.indexOf(d.toUpperCase())!=-1&&(a[d]+"").length==1)a[d]="0"+a[d];break}b=b.split("");for(d=0;d<b.length;d++){var h=b[d];if(c[h]&&a[h.toLowerCase()]!=
null)b[d]=a[h.toLowerCase()]}return b.join("")};this.defaults={debug:false,callback:null};this.construct=function(a){return this.each(function(){if(!f(this).not(":input").length)if(a==="destroy"){this.rules=undefined;switch(q(this.type)){case "change":f(this).unbind("change.validate");break;case "click":f(this).unbind("click.validate");break}}else if(f.Util.objectLength(a)){var b;this.rules={};b=f.extend(this.rules,f.Validate.defaults,a);if(!this.results)this.results={};switch(q(this.type)){case "change":f(this).bind("change.validate",
function(){x.call(this);typeof b.callback=="function"&&b.callback.call(this)});break;case "click":f(this).bind("click.validate",function(){var c;c=y.call(this);if(typeof b.callback=="function")c=b.callback.call(this);return c});break}}})};this.resultsSimplify=function(a){var b=a.rules;a=a.results;var c=0;for(var d in b)f.Util.arrayIndexOf(["debug","callback"],d)==-1&&c++;if(c==0)for(d in a)if(typeof a[d]!="undefined")if(a[d]===false)return false;else{c=window.ActiveXObject?window.ActiveXObject:XMLHttpRequest;
try{if(a[d]instanceof c)return null}catch(h){try{if(c.prototype.isPrototypeOf(a[d]))return null}catch(g){return true}}}else return null;else for(d in b)if(f.Util.arrayIndexOf(["debug","callback"],d)==-1)if(typeof a[d]!="undefined")if(a[d]===false)return false;else{c=window.ActiveXObject?window.ActiveXObject:XMLHttpRequest;try{if(a[d]instanceof c)return null}catch(j){try{if(c.prototype.isPrototypeOf(a[d]))return null}catch(e){return true}}}else return null;return true}})});f.fn.extend({Validate:f.Validate.construct,
validate:f.Validate.construct})})(jQuery);

