function charValidoLetras(e)
{
	var key = (document.all) ? e.keyCode : e.which;
	if ((key < 65 || key > 90) && (key < 97 || key > 122) && key != 8 && key != 9 && key != 32)
	{
		return false;
	}
}

function charValidoLetrasMail(e)
{
	var key = (document.all) ? e.keyCode : e.which;
	if ((key < 48 || key > 57) && (key < 64 || key > 90) && (key < 97 || key > 122) && key != 8 && key != 9 && key != 46)
	{
		return false;
	}
}

function limitarMsg(obj)
{
	var msg = obj.value;
	if (msg.length > 200)
	{
		obj.value = msg.substring(0,200)
	}
}

window.valido = 0;

function validaCampo(obj, tipo, ret)
{
	switch (tipo)
	{
		case 'nombre':
			var nombre = obj.value;
			if (nombre == '' || nombre.length < 5)
			{
				if ($('#e_nombre').is(':hidden'))
					$('#e_nombre').html('El nombre debe tener mas de 5 caracteres.').fadeIn('slow');
				if (ret == 1)
					return 0;
			}
			if (nombre.length > 5)
			{
				$('#e_nombre').fadeOut('slow');
				window.valido++;
				if (ret == 1)
					return 1;
			}
		break;
		case 'telefono':
			var telefono = obj.value;
			if (telefono == '' || telefono < 6)
			{
				if ($('#e_telefono').is(':hidden'))
					$('#e_telefono').html('El tel&eacute;fono debe tener mas de 6 caracteres.').fadeIn('slow');
				if (ret == 1)
					return 0;
			}
			if (telefono > 5)
			{
				$('#e_telefono').fadeOut('slow');
				window.valido++;
				if (ret == 1)
					return 1;
			}
		break;
		case 'mail':
			var mail = obj.value;
			if (mail == '')
			{
				if ($('#e_mail').is(':hidden'))
					$('#e_mail').html('Debe ingresar su email.').fadeIn('slow');
				if (ret == 1)
					return 0;
			} else {
				if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(mail))
				{
					$('#e_mail').fadeOut('slow');
					window.valido++;
				if (ret == 1)
					return 1;
				} else {
					$('#e_mail').html('El email ingresado es incorrecto.').fadeIn('slow');
				if (ret == 1)
					return 0;
				}
			}
		break;
		case 'msg':
			var msg = obj.value;
			if (msg == '' || msg.length < 10)
			{
				if ($('#e_msg').is(':hidden'))
					$('#e_msg').html('El mensaje debe tener mas de 10 caracteres.').fadeIn('slow');
				if (ret == 1)
					return 0;
			} else {
				$('#e_msg').fadeOut('slow');
				window.valido++;
				if (ret == 1)
					return 1;
			}
		break;
		case 'codigo':
			var cod = obj.value;
			if (cod == '')
			{
				if ($('#e_cod').is(':hidden'))
					$('#e_cod').html('Debe ingresar el c&oacute;digo de seguridad.').fadeIn('slow');
				if (ret == 1)
					return 0;
			} else {
				$.get(window.cmsurl+"/validaCod.php?i="+cod, function(data) {
					if (data == 0)
					{
						$('#e_cod').html('El c&oacute;digo ingresado es inv&aacute;lido.').fadeIn('slow');
						var sid = Math.floor(Math.random()*999);
						$('#c_cod_img').attr('src', window.cmsurl+'/imgs/captcha.php?sid='+sid);
						if (ret == 1)
							return 0;
					} else {
						$('#e_cod').fadeOut('slow');
						window.valido++;
						if (ret == 1)
							return 1;
					}
				});
			}
	}
}

function reiniciarFormulario(t)
{
	if (t == 1)
	{
		$('#c_nombre').val('');
		$('#c_mail').val('');
		$('#c_fono').val('');
		$('#c_empresa').val('');
		$('#c_web').val('');
		$('#c_msg').val('');
		$('#c_cod').val('');
		var sid = Math.floor(Math.random()*999);
		$('#c_cod_img').attr('src', window.cmsurl+'/imgs/captcha.php?sid='+sid);
	} else {
		var sid = Math.floor(Math.random()*999);
		$('#c_cod_img').attr('src', window.cmsurl+'/imgs/captcha.php?sid='+sid);
	}

}

function enviarContacto()
{
	var x = 0;
	var msg = "Error, le faltan completar los siguientes campos:\n\n";
	if (validaCampo(document.getElementById('c_nombre'), 'nombre', 1) == 0)
	{
		msg = msg+"* Nombre\n";
		x++;
	}
	if (validaCampo(document.getElementById('c_mail'), 'mail', 1) == 0)
	{
		msg = msg+"* E-Mail\n";
		x++;
	}
	if (validaCampo(document.getElementById('c_fono'), 'telefono', 1) == 0)
	{
		msg = msg+"* Telefono\n";
		x++;
	}
	if (validaCampo(document.getElementById('c_msg'), 'msg', 1) == 0)
	{
		msg = msg+"* Mensaje\n";
		x++;
	}
	if (validaCampo(document.getElementById('c_cod'), 'codigo', 1) == 0)
	{
		msg = msg+"* Codigo de Seguridad\n";
		x++;
	}
	if (x > 0)
	{
		alert(msg);
	} else {
		// Enviar datos.
		$('#btnEnviar').attr("disabled", "disabled");
		$('#cargando').text("Enviando... espere un momento...").fadeIn('slow');
		$.post(window.cmsurl+"/enviarContacto.php", 
			{nombre: $('#c_nombre').val(), 
			mail: $('#c_mail').val(), 
			telefono: $('#c_fono').val(),
			empresa: $('#c_empresa').val(),
			web: $('#c_web').val(),
			msg: $('#c_msg').val()}, function(data) {
				if (data == 1)
				{
					reiniciarFormulario(1);
					alert('Contacto enviado, gracias.');
					$('#btnEnviar').attr("disabled", "");
					$('#cargando').fadeOut('slow');
				} else {
					reiniciarFormulario(2);
					alert('Ocurrio un problema al tratar de enviar la informacion, favor de intentarlo nuevamente.');
					$('#btnEnviar').attr("disabled", "");
					$('#cargando').fadeOut('slow');
				}
				});
	}
}

