 $(document).ready(function(){
	$("#sendmail").click(function(){
		var valid = '';
		var isr = ' es necesario.';
		var name = $("#txt_nameape").val();
		var edad = $("#txt_edad").val();
		var direccion = $("#txt_direccion").val();
		var distrito = $("#txt_distrito").val();
		var subject = $("#txt_telefono").val();
		var mail = $("#txt_email").val();
		var lugestudio = $("#txt_lugestudio").val();
		var lugtrabajo = $("#txt_lugtrabajo").val();
		var text = $("#txt_comentario").val();
		if (name.length<1) {
			$("#txt_nameape").addClass("textinput_error");
			valid += '<br />Nombres y Apellidos '+isr;
		}else{
			$("#txt_nameape").removeClass("textinput_error");
		}
		
		if (edad.length<1) {
			$("#txt_edad").addClass("textinput_error");
			valid += '<br />La edad no es válida, solo números '+isr;
		}else{
			$("#txt_edad").removeClass("textinput_error");
		}
		
		
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			$("#txt_email").addClass("textinput_error");
			valid += '<br />Ingrese un Correo Electronico válido ';
		}else{
			$("#txt_email").removeClass("textinput_error");
		}
		
		if (subject.length<1) {
			$("#txt_telefono").addClass("textinput_error");
			valid += '<br />Teléfono '+isr;
		}else{
		    $("#txt_telefono").removeClass("textinput_error");
		}
		
		if (text.length<1) {
			$("#txt_comentario").addClass("textinput_error");
			valid += '<br />Comentario, Preguntas o Sugerencia'+isr;
		}else{
			$("#txt_comentario").removeClass("textinput_error");
		}
		
		if (valid!='') {
			$("#response").fadeIn("slow");
			$("#response").html("<strong>Verifique los siguientes errores:</strong> "+valid);
		}
		else {
			var datastr ='txt_nameape=' + name + '&txt_edad=' + edad + '&txt_direccion=' + direccion + '&txt_distrito=' + distrito  + '&txt_telefono=' + subject + '&txt_email=' + mail + '&txt_lugestudio=' + lugestudio + '&txt_lugtrabajo=' + lugtrabajo + '&txt_comentario=' + text;
			$("#response").css("display", "block");
			$("#response").html("Enviando Mensaje .... ");
			$("#response").fadeIn("slow");
			setTimeout("send('"+datastr+"')",2000);
		}
		return false;
	});
});
function send(datastr){
	$.ajax({	
		type: "POST",
		url: "sendmail.php",
		data: datastr,
		cache: false,
		success: function(html){
		$("#response").fadeIn("slow");
		if(html==1){
		$("#response").html("Gracias, Su información ha sido enviado con éxito.");
		setTimeout('$("#response").fadeOut("slow")',2000);
		$("#txt_nameape").val('');
		$("#txt_email").val('');
		$("#txt_telefono").val('');
		$("#txt_comentario").val('');
		setTimeout("returnpage()",3000);
		}else{
		$("#response").html(html);
		setTimeout('$("#response").fadeOut("slow")',2000);
		}
	}
	});
}

function returnpage (){
location.href = 'index.html';	
}

