function Formulario(c,h){var d={errores:[],form:null,init:{action:null,async:!0,cache:!1,data:{},dataType:"text",ok:null,type:"POST"},asInteger:function(a){a=parseInt(a);return isNaN(a)?0:a},enviar:function(){$.ajax({async:this.init.async,cache:this.init.cache,data:this.init.data,dataType:this.init.dataType,type:this.init.type,url:this.init.url,success:function(a){a.substr(0,4).toLowerCase()=="msg:"?alert(a.substr(4)):d.init.ok&&d.init.ok({respuesta:a})}});return this},formulario:function(){$("INPUT,TEXTAREA,SELECT,:checkbox",
this.form).each(function(){d.validar(this,this.name)});this.errores.length?(alert(this.errores[0].msg),this.errores[0].element.focus()):this.init.url?this.enviar():this.init.action?(this.form.attr("action",this.init.action),this.form.submit()):alert("Tiene que indicar alguna de las siguientes propiedades: action, url.")},validar:function(a,d){if(a.type!="button"&&!a.disabled){var f=a.getAttribute("mask"),e=a.getAttribute("msg"),b=a.getAttribute("req"),g=a.value,c=null;e||(e=d);f&&(f=f.toLowerCase());
b=b!=null?!0:!1;a.getAttribute("trim")!=null&&(g=$.trim(g));b&&(a.type=="checkbox"?a.checked||(c={element:a,msg:e}):$.trim(g)||(c={element:a,msg:"Tiene que indicar el siguiente valor: "+e}));c?this.errores.push(c):f=="email"?g=this.validarEmail(a,e):f=="fecha"?g=this.validarFecha(a,e):f=="float"?g=this.validarFloat(a,e):f=="int"&&(g=this.validarInt(a,e));this.init.data[d]=g}},validarEmail:function(a,c){a.value=$.trim(a.value);if(!a.value)return"";/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(a.value)||
d.errores.push({element:a,msg:"El dato "+c+" no es correcto, tiene que ser un email."});return a.value},validarFecha:function(a,c){var f=/[\/\.\-]/,e=!1;a.value=$.trim(a.value);if(!a.value)return"";if(/^([0-9]{1,2})+[\/\.\-]+([0-9]{1,2})+[\/\.\-]+([0-9]{1,4})$/.test(a.value)){var b=a.value.split(f);b[0]=d.asInteger(b[0].replace(/^0*/g,""));b[1]=d.asInteger(b[1].replace(/^0*/g,""));b[2]=d.asInteger(b[2].replace(/^0*/g,""));b[2]>0&&b[2]<100&&(b[2]+=2E3);b[0]>0&&b[1]>0&&b[1]<=12&&b[2]>0&&(b[1]==2?b[0]<=
28?e=!0:b[0]==29&&b[2]%4==0&&(e=!0):e=b[1]==4||b[1]==6||b[1]==9||b[1]==11?b[0]<31:b[0]<32)}if(e)return a.value=(b[0]<10?"0":"")+b[0]+"/"+(b[1]<10?"0":"")+b[1]+"/"+b[2],a.value;d.errores.push({element:a,msg:"El dato "+c+" no es correcto, tiene que ser una fecha (dd/mm/aaaa)."});return a.value},validarFloat:function(a,c){a.value=$.trim(a.value);if(!a.value)return"";if(/^-?[0-9]+[\.|,]?[0-9]*$/.test(a.value))return a.value=a.value.replace(",","."),parseFloat(a.value);d.errores.push({element:a,msg:"El dato "+
c+" no es correcto, tiene que ser un valor decimal."});return a.value},validarInt:function(a,c){a.value=$.trim(a.value);if(!a.value)return"";if(/^-?[0-9]+$/.test(a.value))return parseInt(a.value);d.errores.push({element:a,msg:"El dato "+c+" no es correcto, tiene que ser un valor entero."});return a.value}};typeof h=="undefined"&&(h={});if(c)return typeof c=="string"?c=$("#"+c):d.form=$(c),$.extend(d.init,h),d.formulario(),d;else alert("Tiene que indicar un formulario");return null};
