jQuery.fn.ForceNumericOnly = function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 13 || key == 8 || 
                key == 9 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};
function unlockSite()
{
	var d = parseInt($('form#validador_form input:eq(0)').val(),10);
	var m = parseInt($('form#validador_form input:eq(1)').val(),10);
	var a = parseInt($('form#validador_form input:eq(2)').val(),10);
	
	if(d>=1&&d<=31&&m>=1&&m<=12&&a>=0&&a<=99)
	{
		a = (a <= 10) ? 2000 + a : 1900 + a ;
		$("#validador_message").stop().fadeOut("fast");
		jQuery.post("/servicios/validar?callback=?",{'dd':d,'mm':m,'aa':a},function(data)
		{
			if( data.status == "1" || data.status == 1 )
			{
				$("#validador").fadeOut("normal",function(){ $(this).remove() });		
			}
			else
			{
				$("#validador_message").html("Lo sentimos, no tienes la mayor&iacute;a de edad.");
				$("#validador_message").stop(true, true).fadeIn("fast");
			}
		}, "json");
	}
	else{
		$("#validador_message").html("Ingresa una fecha v&aacute;lida.");
		$("#validador_message").stop().fadeIn("fast");
	}
	return false;
}

$(document).ready(function()
{
	/** VALIDADOR **/
	if( $('#validador').length > 0 )
	{
		if(window.location.hash == "#internacional")
		{
			jQuery.post("/servicios/validar?callback=?",{'dd':1,'mm':1,'aa':1911},function(data)
			{
				$("#validador").fadeOut("normal",function(){ $(this).remove() });
			}, "json");
		}
		else
		{
			$('#validador_content').delay(100).fadeIn("slow", function()
			{		
				$('#validador_content > div:first-child').fadeIn("slow", function () 
				{
					if( $(this).next().length > 0 )
					{
						$(this).next().fadeIn("slow", arguments.callee);
					}
				});
			});
				
			$('#validador_submit').click( unlockSite );
			$('form#validador_form input').keypress(function(event) { if (event.which == '13') unlockSite() });
			$('form#validador_form input').ForceNumericOnly();
			$('form#validador_form input').autotab_magic();
			$('form#validador_form div.first input').focus();
		}
	}
});
