/** XHConn - Simple XMLHTTP Interface - 2005-04-08        **/
function XHConn(){
	var xmlhttp, bComplete = false;
	try { 
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	}catch (e) {
		try { 
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		}catch (e) {
			try { 
				xmlhttp = new XMLHttpRequest(); 
			}catch (e) { 
				xmlhttp = false; 
			}
		}
	}
	if (!xmlhttp) return null;
	this.connect = function(sURL, sMethod, sVars, fnDone){
		if (!xmlhttp) return false;
		bComplete = false;
		sMethod = sMethod.toUpperCase();
	
		try {
			if (sMethod == "GET"){
				xmlhttp.open(sMethod, sURL+"?"+sVars, true);
				sVars = "";
			}else{
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			}
			xmlhttp.onreadystatechange = function(){
		        if (xmlhttp.readyState == 4 && !bComplete){
		          bComplete = true;
		          fnDone(xmlhttp);
		
				}
			};
			xmlhttp.send(sVars);
		}catch(z) {
			return false;
		}
	return true;
	};
	return this;
}
function openPopUp(url,titulo,sizex,sizey){
	cont=1;     //esta variable es por si hay que abrir mas de 1 popup
	popUp=window.open(url,titulo + " [" + cont + "]","width="+sizex+", height="+sizey+",menubar=no,scrollbars=no,status=no,titlebar=no");
	popUp.moveTo((screen.width-sizex)/2,(screen.height-sizey)/2);
	cont++;
}

function cargar_contenido_div(target,url){
	//cambiamos el contenido del div que tiene id=target por Cargando....
	document.getElementById(target).innerHTML = '<div class=\"load\"><img src=\"images/ajaxloading.gif\" alt=\"+\" class=\"floatleft\" /> Cargando...</div>';
	//creamos la conexion ajax entre servidor y cliente, usando XHConn
	var myConn = new XHConn();
	if (!myConn) alert("XMLHTTP no esta disponible. Intentalo con un navegador mas reciente.");
	//cuando terminemos de hacer las cosas, el servidor respondera, y cargara su respuesta
	// en el div que tenga como id=target
	var finalizado = function (oXML) {  
		document.getElementById(target).innerHTML = oXML.responseText; 
	};
	//Nos conectamos al servidor mediante Ajax, enviando a la url 
	//(pagina de respuesta del servidor, que ya lleva los parametros via get en la url)
	//Los parametros GET van en el campo URL, por lo que el tercer parametro se deja en blanco
	//Una vez que nos responda el servidor nos devolvera los resultados en el campo finalizado
	myConn.connect(url, "GET", "", finalizado);
}
function cargar_contenido_div_post(target,url,div,campoid){
	//cambiamos el contenido del div que tiene id=target por Cargando....
	document.getElementById(target).innerHTML = '<div class=\"load\"><img src=\"images/ajaxloading.gif\" alt=\"+\" class=\"floatleft\" /> Cargando...</div>';
	//creamos la conexion ajax entre servidor y cliente, usando XHConn
	var myConn = new XHConn();
	if (!myConn) alert("XMLHTTP no esta disponible. Intentalo con un navegador mas reciente.");
	//cogemos el valor del campo del formulario que tenga el id=campoid
	var valorcampo = document.getElementById(campoid).value;
	//cuando terminemos de hacer las cosas, el servidor respondera, y cargara su respuesta
	// en el div que tenga como id=target
	var finalizado = function (oXML) { 
		document.getElementById(target).innerHTML = oXML.responseText; 
	};
	//nos conectamos al servidor mediante Ajax, enviando a la url (pagina de respuesta del servidor)
	//mediante POST los parametros con sus correspondientes valores, y una vez que nos responda el servidor
	//nos devolvera los resultados en el campo finalizado
	myConn.connect(url, "POST", "divID=" + div + "&codeID=" + valorcampo, finalizado);
}
function cargar_contenido_div_get(target,url,campoidget){
	//cambiamos el contenido del div que tiene id=target por Cargando....
	document.getElementById(target).innerHTML = '<div class=\"load\"><img src=\"images/ajaxloading.gif\" alt=\"+\" class=\"floatleft\" /> Cargando...</div>';
	//creamos la conexion ajax entre servidor y cliente, usando XHConn
	var myConn = new XHConn();
	if (!myConn) alert("XMLHTTP no esta disponible. Intentalo con un navegador mas reciente.");
	var valorcampo = document.getElementById(campoidget).value;
	//cuando terminemos de hacer las cosas, el servidor respondera, y cargara su respuesta
	// en el div que tenga como id=target
	var finalizado = function (oXML) {  
		document.getElementById(target).innerHTML = oXML.responseText; 
	};
	//Nos conectamos al servidor mediante Ajax, enviando a la url 
	//(pagina de respuesta del servidor, que ya lleva los parametros via get en la url)
	//Los parametros GET van en el campo URL, por lo que el tercer parametro se deja en blanco
	//Una vez que nos responda el servidor nos devolvera los resultados en el campo finalizado
	myConn.connect(url, "GET", campoidget+"="+$valorcampo, finalizado);
}
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
}

function circuitofdm_checkdnipassport(target,url){
	var dni_numero = document.getElementById('cifnif_SI_dni').value;
	var dni_letra = document.getElementById('cifnif_SI_letra').value;
	var passport = document.getElementById('cifnif_NO').value;
	var chip=getSelectedRadioValue(document.getElementById('chip'));

	//document.getElementById(target).innerHTML = '<div class=\"load\"><img src=\"images/ajaxloading.gif\" alt=\"+\" class=\"floatleft\" /> Cargando...</div>';
	//conexion ajax
	var myConn = new XHConn();
	if (!myConn) alert("XMLHTTP no esta disponible. Intentalo con un navegador mas reciente.");
	var finalizado = function (oXML) {  
		document.getElementById(target).innerHTML = oXML.responseText; 
	};
	myConn.connect(url, "GET", "&dni="+dni_numero+dni_letra+"&passport="+passport+"&chip="+chip, finalizado);

}
function showcontent(id){document.getElementById(id).style.display = "block";}
function hidecontent(id){document.getElementById(id).style.display = "none";}
