$(document).ready(function() {
	$(".output-messages").hide();
	contactFormInit();
});

function contactFormInit() {
    $('#contact-name').focusin(function() {
		if ( $(this).val() == 'Nombre *' ) {
			$(this).val('');
		}
    });
    $('#contact-name').focusout(function() {
		if ( $(this).val().trim() == '' ) {
			$(this).val('Nombre *');
		}
    });
    $('#contact-email').focusin(function() {
		if ( $(this).val() == 'E-mail *' ) {
			$(this).val('');
		}
    });
    $('#contact-email').focusout(function() {
		if ( $(this).val().trim() == '' ) {
			$(this).val('E-mail *');
		}
    });
    $('#contact-message').focusin(function() {
		if ( $(this).val() == 'Mensaje *' ) {
			$(this).val('');
		}
    });
    $('#contact-message').focusout(function() {
		if ( $(this).val().trim() == '' ) {
			$(this).val('Mensaje *');
		}
    });

    $('#send-email-button').click(function() {
		var name = $('#contact-name').val();
		if (name == "Nombre *") {
			$(".output-messages").show(100, function() {
				$(this).html("Debe ingresar su nombre...");
				$('#contact-name').focus();
			});
			return false;
		} else {
			$(".output-messages").hide();
		}

		var email = $('#contact-email').val();
		if ( ! validateEmail(email)) {
			$(".output-messages").show(100, function() {
				$(this).html("Debe ingresar una dirección de correo válida...");
				$('#contact-email').focus();
			});
			return false;
		} else {
			$(".output-messages").hide();
		}

		var message = $('#contact-message').val();
		if (message == "Mensaje *") {
			$(".output-messages").show(100, function() {
				$(this).html("Ingrese su mensaje...");
				$('#contact-message').focus();
			});
			return false;
		} else {
			$(".output-messages").hide();
		}

		$.post('email.php',
		{
			name: $('#contact-name').val(),
			email: $('#contact-email').val(),
			message: $('#contact-message').val()
		}, function(data) {
			alert(data);
			$('#contact-name').val('Nombre *'),
			$('#contact-email').val('E-mail *'),
			$('#contact-message').val('Mensaje *')
		})
		return false;
    });

	function validateEmail(email){
		if (email == "") {
			return false;
		} else {
			if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
				return true;
			} else {
				return false;
			}
		}
	}

}
