var msg = function mensaje( pos, msg ){
	$('.formulario .text_input').eq( pos ).val( msg );
	
	$('.formulario .text_input').eq( pos ).css('borderColor', 'red');
	
	var ancho = $('.formulario .text_input').eq( pos ).outerWidth();
	
	var position = $('.formulario .text_input').eq( pos ).position();
	
	$('.alerta').css('top', position.top);
	$('.alerta').css('left', ( position.left + ancho + 5) );
	
	$('.alerta').fadeIn();
	
	setTimeout(function(){
		$('.alerta').fadeOut();
	}, 5000);
	
};

var msg_comm = function mensaje_comm( pos, msg ){
	
	$('#commentform .text_input').eq( pos ).val( msg );

	$('#commentform .text_input').eq( pos ).css('borderColor', 'red');
	
};



$(document).ready(function(){

	$('.formulario .text_input').val('');
	
	var msg_nombre = 'Ingrese su nombre';
	var msg_apellido = 'Ingrese su apellido';
	var msg_email = 'Ingrese su email';
	var msg_email_valido = 'Ingrese un correo válido';
	var msg_ciudad = 'Ingrese su ciudad';
	var msg_region = 'Ingrese su region';
	var msg_telefono = 'Ingrese su telefono';
	var msg_msg = 'Ingrese su mensaje';
	
	$('.formulario .text_input').focus(function(){
		
		if(
			$(this).val() == msg_nombre || 
			$(this).val() == msg_apellido || 
			$(this).val() == msg_email || 
			$(this).val() == msg_email_valido || 
			$(this).val() == msg_ciudad || 
			$(this).val() == msg_region || 
			$(this).val() == msg_telefono || 
			$(this).val() == msg_msg
		)
			
		$(this).val('');
		$('.alerta').hide();
		$(this).css('borderColor', '#7F9DB9');
		
	});
	
	
	//FORMULARIO DE CONTACTO
	if($('.contactForm')[0]){
		$('.contactForm').click(function(){
			
			var nombre = $('#nombres').val();
			var apellido = $('#apellidos').val();
			var email = $('#email').val();
			var ciudad = $('#ciudad').val();
			var region = $('#region').val();
			var telefono = $('#telefono').val();
			var mensaje = $('#comentarios').val();
			
			if( nombre == "" || nombre == " " ||  nombre == null || nombre == msg_nombre ) {
				msg(0, msg_nombre);
				return false;
			}

			if( apellido == "" || apellido == " " || apellido == null ||  apellido == msg_apellido ) {
				msg(1, msg_apellido);
				return false;
			}
			
			if(email == " " || email == null  || email == msg_email ) {
				msg(2, msg_email );
				return false;
			}

			if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#email').val())){
				msg( 2, msg_email_valido );
				return false;
			}
			
			if(ciudad == "" || ciudad == null || ciudad == msg_ciudad ) {
				msg( 3, msg_ciudad );
				return false;
			}
			

			if(region == "" || region == null) {
				msg( 4, msg_region );
				return false;
			}
			
			if(telefono == "" || telefono == null || isNaN(telefono) ) {
				msg(5, msg_telefono);
				return false;
			}

			if(mensaje == "" || mensaje == null ) {
				msg(6, msg_msg);
				return false;
			}

			var dataForm = $('.formulario').serialize();
			var urlDest = $('.formulario').attr('action');
			$.ajax({
					type: "POST",
					url: urlDest,
					data: dataForm,
					success: function(html){
						$('.box').html(html);
						$('.msg_contacto').fadeIn();
						setTimeout(function(){
							$('.msg_contacto').fadeOut();
						}, 3000);
					}
			 });
		});
	}
	
	//ENVIO COMENTARIOS
	$('#commentform .text_input').focus(function(){
		
		if(
			$(this).val() == msg_nombre || 
			$(this).val() == msg_apellido || 
			$(this).val() == msg_email || 
			$(this).val() == msg_email_valido || 
			$(this).val() == msg_ciudad || 
			$(this).val() == msg_region || 
			$(this).val() == msg_telefono || 
			$(this).val() == msg_msg
		)
			
		$(this).val('');
		$('.alerta').hide();
		$(this).css('borderColor', '#7F9DB9');
		
	});

	if($('.btn_submit')[0]){
		
			$('.btn_submit').click(function(){
			var nombre = $('#author').val();
			var email = $('#email').val();
			var url = $('#url').val();
			var comentario = $('#comment').val();
			
			if( nombre == "" || nombre == " " ||  nombre == null || nombre == msg_nombre ) {
				msg_comm(0, msg_nombre);
				return false;
			}
			
			if(email == " " || email == null  || email == msg_email ) {
				msg_comm(1, msg_email );
				return false;
			}

			if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#email').val())){
				msg_comm( 1, msg_email_valido );
				return false;
			}
			
			if(comentario == "" || comentario == null ) {
				msg_comm(3, msg_msg);
				return false;
			}
				
			 document.forms.commentform.submit();

		 });
	}

	//Suscripcion feedburner
	$('.suscripcion .email').focus(function(){
		$(this).val('');
		$(this).css('borderColor', ' #7F9DB9');
	});
	$('.btn_suscripcion').click(function(){
		if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('.suscripcion .email').val())){
			$('.suscripcion .email').val('Ingrese un email válido');
			$('.suscripcion .email').css('borderColor', 'red');
		}else{
			$('.suscripcion').eq(0).submit();
		}
	});
});

