// 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 ciu = document.getElementById("ciudad");
var pai = document.getElementById("pais");
var con = document.getElementById("contrasenha");
var rep_con = document.getElementById("rep_contrasenha");
var tel = document.getElementById("telefono");
//var movi = document.getElementById("movil");
//var situ = document.getElementById("sitioweb");
var emai = document.getElementById("email");
var reg = document.getElementById("registrar");
//var cod = document.getElementById("code");

return "nombre=" + encodeURIComponent(nom.value) +
"&ciudad=" + encodeURIComponent(ciu.value) + "&pais=" + encodeURIComponent(pai.value) +  
"&telefono=" + encodeURIComponent(tel.value) + "&contrasenha=" + encodeURIComponent(con.value) + "&rep_contrasenha=" + encodeURIComponent(rep_con.value) + 
"&email=" + encodeURIComponent(emai.value) + "&registrar=" + encodeURIComponent(reg.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", "validaregistro.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", "registrar.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 = "Ihre Registrierung wird bearbeitet...";
		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.substring(0,1)=="<")
	{
		//alert("Se ha registrado con exito.");
		//document.location.href = "contacto-rta.php";
		//document.getElementById("respuestas").innerHTML = "Procesando su envio ...";
		//enviar();
		//document.getElementById("respuestas").innerHTML = param2;
		//document.registro.reset();
		document.location.href = "pitogue_confirmacion.php?p=exito&c=" + param2;
		//window.close();
		//document.form1.submit();
		//document.getElementById("respuesta").innerHTML = "Se ha registrado con exito. <br>";
		//document.getElementById("form2").reset();
	}
	else
	{
		document.getElementById("respuestas").innerHTML = param2;
		//location.href = "contacto-rta.php";
		//document.getElementById("respuestas").innerHTML = peticion_http.responseText;
	}
}
}
}

