$.fn.copyTo = function(to) {
    var to = $(to);
    for ( var i = 1; i < arguments.length; i++ )
        to.attr( arguments[i], this.get(0)[ arguments[i] ] );
    return this;
};

new function() {
       // $.fn.validate = validate() {};
    $.fn.validate = {
        init: function(o) {
          if(o.name == 'username') { this.username(o) };
          if(o.name == 'passw') { this.password(o) };
          if(o.name == 'email') { this.email(o) };
          if(o.name == 'dob') { this.dob(o) };
          if(o.name == 'inicio') { this.inicio(o) };
          if(o.name == 'nombre') { this.nombre(o) };
          if(o.name == 'apellido') { this.apellido(o) };

        },
        username: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(user)) {
             doValidate(o);
            } else {
             doError(o,'No esta permitido caracteres especiales');
            };
        },
        password: function(o) {
          var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(pass)) {
             doValidate(o);
            } else {
             doError(o,'No esta permitido 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,'Email no valido');
            };
        },
        dob: function(o) {
          var dob  = /(0[1-9]|[12][0-9]|3[01])+\/(0[1-9]|1[012])+\/(19|20)\d\d/;
            if (o.value.match(dob)) {
              doSuccess(o);
            } else {
              doError(o,'Fecha no Valida. Ej: dd/mm/aaaa');
            };
        },
		inicio: function(o) {
          var dob  = /(0[1-9]|[12][0-9]|3[01])+\/(0[1-9]|1[012])+\/(19|20)\d\d/;
            if (o.value.match(dob)) {
              doSuccess(o);
            } else {
              doError(o,'Fecha no Valida. Ej: dd/mm/aaaa');
            };
        },
        nombre: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(user)) {
             doValidate(o);
            } else {
             doError(o,'No esta permitido caracteres especiales');
            };
        },
        apellido: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(user)) {
             doValidate(o);
            } else {
             doError(o,'No esta permitido caracteres especiales');
            };
        }
     };

     function doSuccess(o) {
              $('#' + o.id + '_img').html('<img src="images/accept.gif" border="0" style="float:left;" />');
              $('#' + o.id + '_li').removeClass("error");
              $('#' + o.id + '_msg').html("");
              $('#' + o.id + '_li').addClass("success");
     }

     function doError(o,m) {
              $('#' + o.id + '_img').html('<img src="images/exclamation.gif" border="0" style="float:left;" />');
              $('#' + 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="images/loading.gif" border="0" style="float:left;" />');
			if(o.id!='terminox')
			{
				$.post('php/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);
						}
					});
			}
			else
			{
				$.post('php/ajax.php', { id: o.id, value: o.checked }, 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="images/loading.gif" border="0" style="float:left;" />');
	if ($('#' + this.get(0).id).val() == $(m.match).val()) {
          $('#' + this.get(0).id + '_img').html('<img src="images/accept.gif" border="0" style="float:left;" />');
          $(m.error).removeClass("error");
          $(m.error).addClass("success");
          $('#' + this.get(0).id + '_msg').html("");
        } else {
          $('#' + this.get(0).id + '_img').html('<img src="images/exclamation.gif" border="0" style="float:left;" />');
          $(m.error).addClass("error");
          $(m.error).removeClass("success");
          $('#' + this.get(0).id + '_msg').html("Las claves son distintas.");
        };
};
$(document).ready(function()
{

  $("input[class=validated]").blur(function() {
          $(this).validate.init(this);
  });
  

  $("#confirmpass").blur(function() {
          $(this).match({match: '#passw', error: '#confirmpass_li'});
  });


  $("#password").keyup(function() {
          $(this).copyTo("#password_copy","value");
  });

  // This Used To Be HOVER, But I.E. Didnt Like It
  //$(".form li").hover(function(){$(this).addClass("selected");},function(){$(this).removeClass("selected");});
  $(".form li").mouseover(function() {
          $(this).addClass("selected");
  });
  $(".form li").mouseout(function() {
          $(this).removeClass("selected");
  });
});

function validar_formulario()
{
	var cont=0;

	$("input[class=validated]").each(function(){
		if($(this).attr("value")=='')
		{
			$("#"+this.id+"_li").removeClass("success").addClass("error");	
			$("#"+this.id+"_img").html('<img src="images/exclamation.gif" border="0" style="float:left;" />');	
			$("#"+this.id+"_msg").html('Campo vacio');
			cont++;
		}
		else
		{
			$("#"+this.id+"_li").removeClass("error").addClass("success");	
			$("#"+this.id+"_img").html('<img src="images/accept.gif" border="0" style="float:left;" />');
			$("#"+this.id+"_msg").html('');
		}
	});

	if($('#dob').attr("value")=='')
	{
		$("#dob_li").removeClass("success").addClass("error");	
		$("#dob_img").html('<img src="images/exclamation.gif" border="0" style="float:left;" />');	
		$("#dob_msg").html('Campo vacio');
		cont++;
	}
	else
	{
		$("#dob_li").removeClass("error").addClass("success");	
		$("#dob_img").html('<img src="images/accept.gif" border="0" style="float:left;" />');
		$("#dob_msg").html('');
	}

	if($('#terminox').attr("checked")=='')
	{
		$("#terminox_li").removeClass("success").addClass("error");	
		$("#terminox_img").html('<img src="images/exclamation.gif" border="0" style="float:left;" />');	
		$("#terminox_msg").html('Para finalizar el registro debes aceptar el termino de uso!');
		cont++;
	}
	else
	{
		$("#terminox_li").removeClass("error").addClass("success");	
		$("#terminox_img").html('<img src="images/accept.gif" border="0" style="float:left;" />');
		$("#terminox_msg").html('');
	}
	//alert(cont);
	if(cont>0)
		return(false);
	else
		return(true);
}