/*
 * MTVlaUtils.FormValidator 0.1.0
 * leonardo.herman@gmail.com
 */
if(typeof MTVlaUtils==="undefined"){var MTVlaUtils={}}MTVlaUtils.FormValidator=function(D,G){var M,K;function F(P){return P.replace(/^\s\s*/,"").replace(/\s\s*$/,"")}function B(P){return P.length&&!P.type?P[0]:P}function I(Q){var P=Q.charAt(0).toUpperCase();return"is"+P+Q.substr(1,Q.length-1)}function L(S){var Q=B(S),P=0,R="";switch(Q.tagName.toLowerCase()){case"input":case"textarea":switch(Q.type){case"checkbox":case"radio":if(S.length){for(R=[];P<S.length;P++){if(S[P].checked){if(Q.type==="checkbox"){R.push(S[P].value)}else{R=S[P].value;break}}}}else{R=S.checked?S.value:""}break;default:R=F(S.value)}break;case"select":if(S.type==="select-multiple"){for(R=[];P<S.length;P++){if(S.options[P].selected){R.push(S.options[P].value)}}}else{R=S.options[S.selectedIndex].value}break}return R}function N(S){var R=[],Q,T;if(S.name&&S.name.length>0){for(var P in G){Q=G[P].fields.indexOf(S.name)>-1;T=G[P].fields==="*";if(Q||T){R.push(G[P])}}}return R}var H={isEmail:function(P){return P.indexOf(".")>2&&P.indexOf("@")>0},isNotEmpty:function(P){return P.length>0},isNumber:function(P){return !isNaN(P)},isString:function(P){return typeof P==="string"&&isNaN(P)}};function O(Q){var P=L(Q);this.apply=function(T){var S=H[I(T)];if(typeof P==="array"){for(var R=0;R<P.length;R++){if(!S(P[R])){return false}}}else{return S(P)}return true};this.getValue=function(){return P}}function C(P){this.getElement=function(){return P};this.isValid=function(Q){var U=true,T=0,R;var S=new O(P);switch(typeof Q){case"string":R=Q.split(",");while(T<R.length){U&=S.apply(F(R[T++]))}break;case"object":for(;T<Q.length;T++){if(typeof Q[T]==="function"){U&=Q[T](S.getValue())}}break;case"function":U=Q(S.getValue());break}return U}}var A={data:[],load:function(){var Q,Y,X,T,S;var P=M.getElementsByTagName("*");var W="";function V(c){var b=Q.length===1;var a=W.length>0;var Z=W.indexOf(c.name)>-1;if(b&&a&&Z){return true}else{W=c.name;return false}}function R(Z,a){Z.push({field:new C(a),message:Q[T].message,methods:Q[T].methods})}for(var U=0;U<P.length;U++){switch(P[U].type){case"submit":case"button":case"hidden":break;default:Q=N(P[U]);for(T=0;T<Q.length;T++){if(V(P[U])){continue}if(Q[T].fields==="*"){R(this.data,M[P[U].name]);continue}Y=Q[T].fields.split(",");for(S=0;S<Y.length;S++){X=F(Y[S]);if(X===W){R(this.data,M[X])}}}}}}};function J(U,R){var Q=B(U.getElement());var S=L(U.getElement());var P=R.replace("$value",S);if(typeof S==="array"){S="["+S.join(", ")+"]"}if(K){try{K({field:Q,message:P,value:S})}catch(T){alert(P)}}else{alert(P)}Q.focus()}function E(){var S,R,P;if(A.data.length===0){A.load()}for(var Q in A.data){S=A.data[Q].field;R=A.data[Q].message;P=A.data[Q].methods;if(!S.isValid(P)){J(S,R);return false}}return true}this.version="0.1.0";this.addMethod=function(Q,P){H[I(Q)]=P};this.setErrorCallback=function(P){if(typeof P==="function"){K=P}};M=document.getElementById(D)||document[D];if(M){M.onsubmit=function(){return E()}}};