// JavaScript Document

var READY_STATE_COMPLETE=4;
var peticion_http = null;

function inicializa_xhr() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}

function crea_query_string() {

var nom = document.getElementById("nombre");
var dir = document.getElementById("direccion");
var ciu = document.getElementById("ciudad");
var ruc = document.getElementById("ruc");
//var em = document.getElementById("empresa");
var tel = document.getElementById("telefono");
var movi = document.getElementById("movil");
var emai = document.getElementById("email");
var mensa = document.getElementById("mensaje");
var cod = document.getElementById("code");

return "nombre=" + encodeURIComponent(nom.value) + "&direccion=" + encodeURIComponent(dir.value) + 
"&ciudad=" + encodeURIComponent(ciu.value) + "&ruc=" + encodeURIComponent(ruc.value) + 
"&telefono=" + encodeURIComponent(tel.value) + "&movil=" + encodeURIComponent(movi.value) + 
"&email=" + encodeURIComponent(emai.value) + "&mensaje=" + encodeURIComponent(mensa.value) + 
"&code=" + encodeURIComponent(cod.value) + "&nocache=" + Math.random();
}

function valida() {
peticion_http = inicializa_xhr();
if(peticion_http) {
peticion_http.onreadystatechange = procesaRespuesta;
peticion_http.open("POST", "validacontacto.php", true);
var query_string = crea_query_string();
peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
peticion_http.send(query_string);
}
}

function enviar() {
peticion_http = inicializa_xhr();
if(peticion_http) {
peticion_http.onreadystatechange = procesaRespuesta2;
peticion_http.open("POST", "mailer.php", true);
var query_string = crea_query_string();
peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
peticion_http.send(query_string);
}
}

function procesaRespuesta() {
if(peticion_http.readyState == READY_STATE_COMPLETE) {
if (peticion_http.status == 200) {
	var param = peticion_http.responseText;
	if(param=="exito")
	{
		//alert("Se ha registrado con exito.");
		//document.location.href = "contacto-rta.php";
		document.getElementById("respuestas").innerHTML = "Procesando su envio ...";
		enviar();
		//document.form1.submit();
		//document.getElementById("respuesta").innerHTML = "Se ha registrado con exito. <br>";
		//document.getElementById("form2").reset();
	}
	else
	{
		document.getElementById("respuestas").innerHTML = peticion_http.responseText;
	}
}
}
}

function procesaRespuesta2() {
if(peticion_http.readyState == READY_STATE_COMPLETE) {
if (peticion_http.status == 200) {
	var param2 = peticion_http.responseText;
	if(param2=="enviado")
	{
		//alert("Se ha registrado con exito.");
		//document.location.href = "contacto-rta.php";
		//document.getElementById("respuestas").innerHTML = "Procesando su envio ...";
		//enviar();
		location.href = "contacto-rta.php";
		//window.close();
		//document.form1.submit();
		//document.getElementById("respuesta").innerHTML = "Se ha registrado con exito. <br>";
		//document.getElementById("form2").reset();
	}
	else
	{
		location.href = "contacto-rta.php";
		//document.getElementById("respuestas").innerHTML = peticion_http.responseText;
	}
}
}
}

