function inicializa_xhr() {
var navega_version=["MSXML2.XHTMLHttp.5.0","MSXML2.XHTMLHttp.4.0","MSXML2.XHTMLHttp.3.0","MSXML2.XHTMLHttp","Microsoft.XHTMLHttp",];
  if(window.XMLHttpRequest) {
    var peticion_http = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
  for(a=0;$a<navega_version.length;$a++){  
		  try { 
			var peticion_http = new ActiveXObject(navega_version[a]);
		 } 
		 catch (e) { 
		 alert('El navegador utilizado no está soportado'); 
		 } 
  }
  } 
  return peticion_http;
}

function mandatos_suma(numeroid,operacion) {
   if(operacion=="suma"){
     var cuenta="mas";
   }else{
    var cuenta="menos";
   }
  return "cuenta=" + encodeURIComponent(cuenta) +
         "&numero="+numeroid+
         "&nocache=" + Math.random();
}

function suma(operacion,bbdd){
  //1.Instanciar objeto peticion_http
  peticion_http=inicializa_xhr();
 
 
  //3-  Realizar peticion HTTP al servidor
    peticion_http.open("POST", "http:include/ajax/cargador.php", true);
    peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	
  //2.- Preparar la funcion de respuesta
  peticion_http.onreadystatechange = function() {
    if(peticion_http.readyState == 4) {
      if(peticion_http.status == 200){
	    document.getElementById("cargador_"+bbdd).style.display="none";
	      var dondesuma=document.getElementById("votos_"+bbdd);
		  var trocito=dondesuma.innerHTML.split("&nbsp;");
		  var trocea=parseInt(trocito[0]);
		  if(operacion=="suma"){
		    dondesuma.innerHTML=(trocea+1)+"&nbsp;Votos";
		  }else{
		    dondesuma.innerHTML=(trocea-1)+"&nbsp;Votos";
		  }
		  quitasuma=document.getElementById("suma_"+bbdd);
		  quitaresta=document.getElementById("resta_"+bbdd);
		  quitasuma.parentNode.removeChild( quitasuma );
		  quitaresta.parentNode.removeChild( quitaresta );
      }
    }else{
	    document.getElementById("cargador_"+bbdd).style.display="block";
	}
   }

    var query_string = mandatos_suma(bbdd,operacion);
    peticion_http.send(query_string);   
}


function olvido_con(){
  //1.Instanciar objeto peticion_http
  peticion_http=inicializa_xhr();
 
 
  //3-  Realizar peticion HTTP al servidor
    email=document.getElementById("email_olvi").value;
	emaildos=email.replace("@","a09a0");
	emailfin=emaildos.replace(".","a15a1");
    peticion_http.open("POST", "http:include/ajax/olvido.php?cual="+ emailfin +"", true);
    peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

  //2.- Preparar la funcion de respuesta
  peticion_http.onreadystatechange = function() {
    
		if(peticion_http.readyState==1){
            document.getElementById('cargador').style.display = "block"; 
		}
		else if(peticion_http.readyState == 4){ 
			if(peticion_http.status==200){	
				document.getElementById('cargador').style.display = "none"; 		
				respuesta = peticion_http.responseXML;
				var existe=respuesta.getElementsByTagName("p").item(0).firstChild.data;
					if (existe=="true"){
					document.getElementById("olvido_hecho").style.display = "block";
					}else{
					document.getElementById("olvido_error").style.display = "block";
					}
			}	
		}
   }

    peticion_http.send(null);   
}

function dainfo(club,equipo){

//1.Instanciar objeto peticion_http
  peticion_http=inicializa_xhr();
  //3-  Realizar peticion HTTP al servidor
    peticion_http.open("POST", "http:include/ajax/prueba_equipo.php", true);
    peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 
 
  //2.- Preparar la funcion de respuesta
  peticion_http.onreadystatechange=function(){
    if(peticion_http.readyState == 4) {
      if(peticion_http.status == 200){
	  //desactiva para debugear xml alert(peticion_http.responseText);
	 //recibir pagina XML
    ejeXML=peticion_http.responseXML;
	if(ejeXML){
	alert(ejeXML.getElementsByTagName("equipo").length);
	var equipo=ejeXML.getElementsByTagName("equipo")[0].firstChild.data;
	var club=ejeXML.getElementsByTagName("club")[0].firstChild.data;
	var web=ejeXML.getElementsByTagName("web")[0].firstChild.data;
	var escudo=ejeXML.getElementsByTagName("escudo")[0].firstChild.data;
	var comentario=ejeXML.getElementsByTagName("comentario")[0].firstChild.data;
	}else{ alert("Has vuelto a fallar");}
	  	
	 
	
	//  HACER EL ALERT 

	// creamos Container_Grande div como hijo del  BODY 
	mObj = document.getElementsByTagName("body")[0].appendChild(document.createElement("div"));
	mObj.id = "Container_grande";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";
	mObj.style.width = document.documentElement.scrollWidth + "px";

	
	
	
	// creamos DIV que hara de alert 
	alertObj = mObj.appendChild(document.createElement("div"));
	alertObj.id = "alertCaja";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(document.all && !window.opera){ alertObj.style.top = document.documentElement.scrollTop + "px";}
	// center the alert box
	alertObj.style.left = (document.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";
	
	// create an H2 element as the title bar
	h2 = alertObj.appendChild(document.createElement("h2"));
	h2.appendChild(document.createTextNode(equipo));

	// texto
	divdatos = alertObj.appendChild(document.createElement("div"));
	divdatos.id="datos_jug";
	msg = divdatos.appendChild(document.createElement("img"));
	if(escudo!="no"){ msg.setAttribute("src","imagenes/"+equipo+"_equipo/"+escudo);}
	else{ msg.setAttribute("src","imagenes/webimages/sinimagen.jpg");}
	msg.setAttribute("alt","escudo "+ equipo);
	af = divdatos.appendChild(document.createElement("a"));
	af.setAttribute("href",web);
	af.appendChild(document.createTextNode("Ir a su web"));
	da = divdatos.appendChild(document.createElement("p"));
	da.appendChild(document.createTextNode(comentario));
	
	// create an anchor element to use as the confirmation button.
	divenvio = divdatos.appendChild(document.createElement("p"));
	divenvio.setAttribute("class","quitajug");
	btn = divenvio.appendChild(document.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(document.createTextNode("cerrar"));
	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { quitalerta();return false; }
	

}else{
  alert(peticion_http.status);
}
}else{
}
	
}

	var mandalo=mandatos_jug(club,equipo);
    peticion_http.send(mandalo);
}
// removes the custom alert from the DOM
function quitalerta() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("Container_grande"));
}

function mandatos_jug(nom,equi) {
  return "club="+nom+"&equipo="+equi+"&nocache=" + Math.random();
}
