$(document).ready(function() {
	
	$('input').keypress(function() {
		$(this).parents("form:first").find('.globoError').fadeOut();
		if($(this).get(0).disableError) $(this).get(0).disableError();
	});
	
	/*
	 * Validacion de formulario Newsletter
	 */
	$("form#bbva").each(function (){
		/*var numero = $(this).find("#numero");*/
		
		// Agrego globo para mensajes de error en el body
		var $globo = $('<div class="globoError mensaje"><div class="alerta_top mensaje"></div><div class="alerta_centro"><table><tr><td class="body">msg</td></tr></table></div><div class="alerta_bottom"></div></div>')
			.css({top: "-999px", left: "-999px", position: 'absolute' });
		$(this).find(".globoError").remove();
		$(this).append($globo);
		$(this).validate({
		
			onkeyup: false,
			onfocusout: false,
			rules: {
				name: {
					required: true
				},
				lastname: {
					required: true
				},
				id: {
					required: true,
					maxlength:16,
					minlength:8,
					rut:true
				},
				phonenumber: {
					required: true,
					minlength:7
				}
			},
			messages: {
				name:{
					required:"Por favor ingrese su nombre."
				},
				lastname:{
					required:"Por favor ingrese sus apellidos."
				},
				id:{
					required:"Por vafor ingrese su n&uacute;mero de RUT.",
					maxlength:"Por favor ingrese un n&uacute;mero de RUT v&aacute;lido.",
					minlength:"Por favor ingrese un n&uacute;mero de RUT v&aacute;lido.",
					rut:"Por favor ingrese un n&uacute;mero de RUT v&aacute;lido."
				},
				phonenumber:{
					required:"Por favor ingrese su n&uacute;mero de tel&eacute;fono.",
					minlength:"Ingrese un n&uacute;mero telef&oacute;nico v&aacute;lido."
				}
			},
			errorPlacement: function(error, element) {
				
				var $form = element.parents('form:first');
				var firstError = $form.validate().errorList[0].message;
				
				$form.find('.globoError:first').find('td:first').html(firstError);
				showGlobo($form.validate().errorList[0].element);
			}
		});
	});
	
	$(".inputBox").keypress(function(){
		$(this).parents("form:first").find(".globoError").fadeOut(200);
	});
	
	$(".inputBoxNeeded").keypress(function(){
		$(this).parents("form:first").find(".globoError").fadeOut(200);
	});
});


function showGlobo(el) {
	
	var $input = $(el).parents('.campo:first'); //.parents('.campo:first');
	var punto = $input.offset();
	var $globo = $(el).parents("form:first").find('.globoError');
	var top_position = parseInt($input.height())/2;
	
	punto.left += parseInt($input.width()) - 279;
	punto.top +=  top_position - 82;
		
	if ($globo.is(':hidden')) {
		$globo.fadeIn(200, function() {
			$(el).focus();							
		});
	}
	
	$globo.css({
		'top': punto.top,
		'left': punto.left
	});
	
	return false;
	
}


