// JavaScript Document

var READY_STATE_COMPLETE=4;
var peticion_http2 = null;

function inicializa_xhr2() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}


/*
function valorcheckbox(){ 
    var i 	
    for (i=0;i<document.form2.sexo.length;i++){ 
       if (document.form2.sexo[i].checked)           		  
		  break;	  
    } 
    //document.bgColor = document.form2.sexo[i].value 
	return document.form2.sexo[i].value ;
} 
*/
/*
var checkboxes = document.getElementById("contacto").opcion;

var cont = 0;  
for (var x=0; x < checkboxes.length; x++) 
{ 
	if (checkboxes[x].checked) 
	{  
		//cont = cont + 1; 
		var op[x] = checkboxes[x].value ;
	}
}
*/

function crea_query_string2() {

//var checkboxes = document.getElementById("contacto").opcion;

/*var pro = document.getElementById("proxy");
var iplo = document.getElementById("iplocal");
var hosname1 = document.getElementById("hostname1");
var ippu = document.getElementById("ippublica");*/
var cant = document.getElementById("cantidad");
var nom = document.getElementById("nombre");
var cedu = document.getElementById("ci");
var ciu = document.getElementById("ciudad");
var pai = document.getElementById("pais");
var dir = document.getElementById("direccion");
var tel = document.getElementById("telefono");
var tel_movil = document.getElementById("telefono_movil");
var em = document.getElementById("empresa");
var car = document.getElementById("cargo");
var emai = document.getElementById("email");
var comen = document.getElementById("comentario");
var cod = document.getElementById("code");


return "nombre=" + encodeURIComponent(nom.value) + "&ci=" + encodeURIComponent(cedu.value) +
"&ciudad=" + encodeURIComponent(ciu.value) + "&pais=" + encodeURIComponent(pai.value) +
"&direccion=" + encodeURIComponent(dir.value) + "&telefono=" + encodeURIComponent(tel.value) +
"&telefono_movil=" + encodeURIComponent(tel_movil.value) + "&empresa=" + encodeURIComponent(em.value) +
"&cargo=" + encodeURIComponent(car.value) + "&cantidad=" + encodeURIComponent(cant.value) + 
"&email=" + encodeURIComponent(emai.value)  + 
"&comentario=" + encodeURIComponent(comen.value)  + 
"&code=" + encodeURIComponent(cod.value) + "&nocache=" + Math.random();
}

function valida2() {
peticion_http2 = inicializa_xhr2();
if(peticion_http2) {
peticion_http2.onreadystatechange = procesaRespuesta2;
peticion_http2.open("POST", "validapedido.php", true);
var query_string2 = crea_query_string2();
peticion_http2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
peticion_http2.send(query_string2);
}
}

function enviar2() {
peticion_http2 = inicializa_xhr2();
if(peticion_http2) {
peticion_http2.onreadystatechange = procesaRespuesta3;
peticion_http2.open("POST", "mailer-pedido.php", true);
var query_string2 = crea_query_string2();
peticion_http2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
peticion_http2.send(query_string2);
}
}

function procesaRespuesta2() {
if(peticion_http2.readyState == READY_STATE_COMPLETE) {
if (peticion_http2.status == 200) {
	var param3 = peticion_http2.responseText;
	if(param3=="exito")
	{
		//alert("Se ha registrado con exito.");
		//document.location.href = "contacto-rta.php";
		document.getElementById("respuestas").innerHTML = "Procesando su envio ...";
		//enviar2();
		document.form1.action = "agregar-pedido.php";
		document.form1.submit();
		//document.contacto.submit();
		//document.getElementById("respuesta").innerHTML = "Se ha registrado con exito. <br>";
		//document.getElementById("form2").reset();
	}
	else
	{
		document.getElementById("respuestas").innerHTML = peticion_http2.responseText;
	}
}
}
}

function procesaRespuesta3() {
if(peticion_http2.readyState == READY_STATE_COMPLETE) {
if (peticion_http2.status == 200) {
	var param4 = peticion_http2.responseText;
	if(param4=="enviado")
	{
		//alert("Se ha registrado con exito.");
		//document.location.href = "contacto-rta.php";
		//document.getElementById("respuestas").innerHTML = "Procesando su envio ...";
		//enviar();
		//location.href = "rta.php?resultado=exito";
		document.getElementById("respuestaboletin").innerHTML = "<img src='images/icono-ok.gif' border='0' align='middle'/><br> Su suscripci&oacute;n ha sido enviada.";
		document.boletin.reset();
		//window.parent.img.location.reload();
		//window.close();
		//document.form1.submit();
		//document.getElementById("respuesta").innerHTML = "Se ha registrado con exito. <br>";
		//document.getElementById("form2").reset();
	}
	else
	{
		//location.href = "rta.php?resultado=exito";
		document.getElementById("respuestaboletin").innerHTML = "<img src='images/icono-ok.gif' border='0' align='middle'/><br> Su suscripci&oacute;n ha sido enviada.";
		document.boletin.reset();
		//window.parent.img.location.reload();
		//document.getElementById("respuestas").innerHTML = peticion_http.responseText;
	}
}
}
}
