$.fn.copyTo = function(to) {
    var to = $(to);
    for ( var i = 1; i < arguments.length; i++ )
        to.set( arguments[i], this.get(0)[ arguments[i] ] );
    return this;
};

new function() {
       // $.fn.validate = validate() {};
    $.fn.validate = {
        init: function(o) {
          if(o.name == 'usuario') { this.usuario(o) };
          if(o.name == 'password') { this.password(o) };
          if(o.name == 'email') { this.email(o) };
          if(o.name == 'nombre') { this.completo(o) };
          if(o.name == 'apellido') { this.completo(o) };
          if(o.name == 'direccion') { this.completo(o) };
          if(o.name == 'localidad') { this.completo(o) };
          if(o.name == 'telefono') { this.telefono(o) };
        },
        usuario: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(user)) {
             doValidate(o);
            } else {
             doError(o,'No se permiten caracteres especiales.');
            };
        },
        password: function(o) {
          var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(pass)) {
             doValidate(o);
            } else {
             doError(o,'No se permiten caracteres especiales.');
            };
        },
        email: function(o) {
          var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
           if (o.value.match(email)) {
			  doSuccess(o);
            } else {
              doError(o,'No es un email v&aacute;lido.');
            };
        },
        completo: function(o) {
          var complet = /[(\[\]\+\,\/\?\:\;\'\"\`\~\$\%\^\&\<\>)+]/;
           if (!o.value.match(complet)) {
             doValidate(o);
            } else {
             doError(o,'No se permiten caracteres especiales.');
            };
        },
        telefono: function(o) {
          var tel = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>a-zA-Z)+]/;
           if (!o.value.match(tel)) {
             doValidate(o);
            } else {
             doError(o,'No se permiten caracteres especiales ni alfab&eacute;ticos.');
            };
        }
     };

     function doSuccess(o) {
              $('#' + o.id + '_img').html('<img src="../img_new/accept.gif" border="0" style="float:left;" alt="" >');
              $('#' + o.id + '_li').removeClass("error");
              $('#' + o.id + '_msg').html("");
              $('#' + o.id + '_li').addClass("success");
     }

     function doError(o,m) {
              $('#' + o.id + '_img').html('<img src="../img_new/exclamation.gif" border="0" style="float:left;" alt="" >');
              $('#' + o.id + '_li').addClass("error");
              $('#' + o.id + '_msg').html(m);
              $('#' + o.id + '_li').removeClass("success");
     }
     //private helper, validates each type after check
     function doValidate(o) {
		$('#' + o.id + '_img').html('<img src="../img_new/loading.gif" border="0" style="float:left;" alt="" >');
		$.post('../lib/validator/ajax.php', { id: o.id, value: o.value }, function(json){
		eval("var args = " + json);
			if (args.success == true){
			  doSuccess(args);
			} else {
			  doError(args,args.msg);
			}
		});
    };

};
$.fn.match = function(m) {
	$('#' + this.get(0).id + '_img').html('<img src="../img_new/loading.gif" border="0" style="float:left;" alt="" >');
	if ($(this).get(0).val() == $(m.match).val()) {
          $('#' + this.get(0).id + '_img').html('<img src="../img_new/accept.gif" border="0" style="float:left;"alt=""  >');
          $(m.error).removeClass("error");
          $(m.error).addClass("success");
          $('#' + this.get(0).id + '_msg').html("");
        } else {
          $('#' + this.get(0).id + '_img').html('<img src="../img_new/exclamation.gif" border="0" style="float:left;" alt="" >');
          $(m.error).addClass("error");
          $(m.error).removeClass("success");
          $('#' + this.get(0).id + '_msg').html("El password no concuerda, intente nuevamente.");
        };
};

$(document).ready(function(){

  $("//[@class=validated]/input").blur(function() {
          $(this).validate.init(this);
  });
  

  $("#confirmpass").blur(function() {
          $(this).match({match: '#password', error: '#confirmpass_li'});
  });


  $("#password").keyup(function() {
          $(this).copyTo("#password_copy","value");
  });

  $(".form li").mouseover(function() {
          $(this).addClass("selected");
  });
  $(".form li").mouseout(function() {
          $(this).removeClass("selected");
  });
});
