// 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");
}
}

//Obtener valor de radio button
/*function getRadioButtonSelectedValue(ctrl)
{
    if(ctrl.length == undefined)
	{
		return ctrl.value;
	}
	else
	{
		for(i=0;i<ctrl.length;i++)
		{
        	if(ctrl[i].checked) 
			{
				return ctrl[i].value;
			}
		}
	}	
}
*/
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 tel_em = document.getElementById("tel_empresa");
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 + "&tel_empresa=" + tel_em.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_http = inicializa_xhr();
if(peticion_http) {
peticion_http.onreadystatechange = procesaRespuesta;
peticion_http.open("POST", "validaDatos.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-registro.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")
	{
		document.getElementById("respuesta").innerHTML = "Procesando su envio ...";
		enviar();
		//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_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();
		//alert("Se ha registrado con exito. </br>En breve le llegará un email");
		document.location.href = "registro.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.php?e=e";
		//document.getElementById("respuesta").innerHTML = peticion_http.responseText;
		//document.getElementById("respuestas").innerHTML = peticion_http.responseText;
	}
}
}
}