function isValidEmail(address) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(address);
}

$(document).ready(function(){
	$('div#slideshow').cycle({
		timeout: 10000
	});

	$('#contact_form').submit(function(e){
		var form = $(this);
		var error = false;

		form.find('.msg').remove();
		$(form).find('.required').each(function(){
			var element = $(this);
			if ((element.val() == '')
				|| (element.hasClass('email')
				&& !isValidEmail(element.val()))) {
				form.prepend('<div class="msg error">Por favor completa todos los campos y asegúrate de ingresar un email valido.</div>');
				error = true;
				return false;
			} else {
			}
		});

		if (!error) {
			form.prepend('<div class="msg notice">Enviando mensaje</div>');
			form.find('.ajaxed').val('true');
			$.ajax({
				url: form.attr('action'),
				type: 'post',
				data: $(this).serialize(),
				dataType: 'json',
				success: function(data) {
					if (data.status == 'success') {
						form.find('.msg').remove();
						form.prepend('<div class="msg success">Tu comentario fue enviado. Será respondido a la brevedad.</div>');
						$(':input', form).not(':button, :submit, :reset, :hidden')
							.val('')
							.removeAttr('checked')
							.removeAttr('selected');
					} else {
						form.find('.msg').remove();
						form.prepend('<div class="msg error">Tu comentario no pudo ser enviado. Por favor, intenta de nuevo.</div>');
					}
				}
			});
		}
		e.preventDefault();
	});
});
