// 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 valorradiobutton(){ 
    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 sex = getRadioButtonSelectedValue(document.form2.sexo);

function crea_query_string() {
//var sex = document.getElementById("sexo");
//var sex = getRadioButtonSelectedValue(document.form2.sexo);

var sex = valorradiobutton();
var nom = document.getElementById("nombre");
var apelli = document.getElementById("apellidos");
var fecha_nacimiento = document.getElementById("fecha_nac");
var docu = document.getElementById("documento");
var em = document.getElementById("empresa");
var carg = document.getElementById("cargo");
var dir = document.getElementById("direccion");
var bar = document.getElementById("barrio");
var ciu = document.getElementById("ciudad");
var pai = document.getElementById("pais");
var tel_par = document.getElementById("tel_particular");
var obs_en = document.getElementById("obs_entrega");
var usu = document.getElementById("usuario2");
var contra = document.getElementById("contrasenha");
var conf_contra = document.getElementById("confirmar_contrasenha");
var emai = document.getElementById("email");

return "sexo=" + sex + "&nombre=" + nom.value +
"&apellidos=" + apelli.value + "&fecha_nac=" + fecha_nacimiento.value + "&documento=" + docu.value + 
"&empresa=" + em.value + "&cargo=" + carg.value + "&direccion=" + dir.value + 
"&barrio=" + bar.value + "&ciudad=" + ciu.value + "&pais=" + pai.value + 
"&tel_particular=" + tel_par.value + "&obs_entrega=" + obs_en.value + 
"&usuario=" + usu.value + "&contrasenha=" + contra.value + 
"&confirmar_contrasenha=" + conf_contra.value + "&email=" + emai.value + "&nocache=" + Math.random();
}
function valida() {
peticion_http2 = inicializa_xhr2();
if(peticion_http2) {
peticion_http2.onreadystatechange = procesaRespuesta;
peticion_http2.open("POST", "validaDatos.php", true);
var query_string = crea_query_string();
peticion_http2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
peticion_http2.send(query_string);
}
}

function registrar() {
peticion_http2 = inicializa_xhr2();
if(peticion_http2) {
peticion_http2.onreadystatechange = procesaRespuesta2;
peticion_http2.open("POST", "mailer-registro.php", true);
var query_string = crea_query_string();
peticion_http2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
peticion_http2.send(query_string);
}
}

function procesaRespuesta() {
if(peticion_http2.readyState == READY_STATE_COMPLETE) {
if (peticion_http2.status == 200) {
	var param = peticion_http2.responseText;
	if(param=="exito")
	{
		document.getElementById("respuesta").innerHTML = "Procesando su envio ...";
		registrar();
		//alert("Se ha registrado con exito. </br>En breve le llegará un email");
		//document.location.href = "carrito.php";
		//document.getElementById("respuesta").innerHTML = "Se ha registrado con exito. <br>";
		//document.getElementById("form2").reset();
	}
	else
	{
		document.getElementById("respuesta").innerHTML = peticion_http2.responseText;
	}
}
}
}


function procesaRespuesta2() {
if(peticion_http2.readyState == READY_STATE_COMPLETE) {
if (peticion_http2.status == 200) {
	var param2 = peticion_http2.responseText;
	if(param2=="enviado")
	{
		//alert("Se ha registrado con exito.");
		//document.location.href = "contacto-rta.php";
		//document.getElementById("respuestas").innerHTML = "Procesando su envio ...";
		//enviar();
		//alert("Se ha registrado con exito. </br>En breve le llegará un email");
		document.location.href = "registro-rta.php?e=e";
		//window.close();
		//document.form1.submit();
		//document.getElementById("respuesta").innerHTML = "Se ha registrado con exito. <br>";
		//document.getElementById("form2").reset();
	}
	else
	{
		//alert("Se ha registrado con exito. </br>En breve le llegará un email");
		document.location.href = "registro-rta.php?e=e";
		//document.getElementById("respuesta").innerHTML = peticion_http.responseText;
		//document.getElementById("respuestas").innerHTML = peticion_http.responseText;
	}
}
}
}