/*
*Esta libreria es una libreria AJAX creada por Javier Mellado
*y descargada del portal AJAX Hispano http://www.ajaxhispano.com
*contacto javiermellado@gmail.com
*
*Puede ser utilizada, pasada, modificada pero no olvides mantener
*el espiritu del software libre y respeta GNU-GPL
*/

function creaAjax(benctype){
	var objetoAjax=false;
	try {
		/*Para navegadores distintos a internet explorer*/
		objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) {
		try {
			/*Para explorer*/
			objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E) {
			objetoAjax = false;
		}
	}
	
	 if (!objetoAjax && typeof XMLHttpRequest!='undefined') {
	  objetoAjax = new XMLHttpRequest();
	 }
	 
	 return objetoAjax;
}


function FAjax (url,capa,valores,metodo)
{
  //alert (url+" "+capa+" "+valores+" "+metodo);
  var ajax=creaAjax();
  var capaContenedora = document.getElementById(capa);

	/*Creamos y ejecutamos la instancia si el metodo elegido es POST*/
	if(metodo.toUpperCase()=='POST'){
	         ajax.open ('POST', url, true);
	         ajax.onreadystatechange = function() {
	         if (ajax.readyState==1) {
	                          capaContenedora.innerHTML="";
	         }
	         else if (ajax.readyState==4){
	           if(ajax.status==200)
	           {
						 			//alert ("correcto:"+ajax.responseText);
									if (ajax.responseText!="")
	                	 document.getElementById(capa).innerHTML=ajax.responseText;
									else
										 {
										 	    var txtVacia="";										 
                          txtVacia+="		<table id='ajxFicha' cellpadding='0' cellspacing='0' border='0'>";
                          txtVacia+="		<tr>";
                          txtVacia+="			<td id='ajxFichaLatIzq'>&nbsp;</td>";
                          txtVacia+="			<td id='ajxFichaCen'>";
                          txtVacia+="				<table width='100%' border='0' cellpadding='2' cellspacing='0'>";
                          txtVacia+="				<tr>";
                          txtVacia+="					<td id='ajxFichaMargen' class='classTitulo'>&nbsp;</td>";
                          txtVacia+="					<td id='ajxFichaTitulo' class='classTitulo'>Detalles del título</td>";
                          txtVacia+="					<td id='ajxFichaMargen' class='classTitulo'>&nbsp;</td>";
                          txtVacia+="				</tr>";
                          txtVacia+="				<tr><td colspan='3' style='font-size: 5px;'>&nbsp;</td></tr>";
                          txtVacia+="				<tr>";
                          txtVacia+="					<td id='ajxFichaMargen'>&nbsp;</td>";
                          txtVacia+="					<td id='textoFicha'>";
                          txtVacia+="					<b><br>No hay información adicional para este título<br></b>";
                          txtVacia+="					</td>";
                          txtVacia+="					<td id='ajxFichaMargen'>&nbsp;</td>";
                          txtVacia+="				</tr>";
                          txtVacia+="				<tr>";
                          txtVacia+="					<td id='ajxFichaMargen' style='font-size: 1px;'>&nbsp;</td>";
                          txtVacia+="					<td class='subRayaVerde' style='font-size: 1px;'>&nbsp;</td>";
                          txtVacia+="					<td style='font-size: 1px;' id='ajxFichaMargen'>&nbsp;</td>";
                          txtVacia+="				</tr>";
                          txtVacia+="				<tr>";
                          txtVacia+="					<td id='ajxFichaMargen'>&nbsp;</td>";
                          txtVacia+="					<td id='fichaDireccion'>";
                          txtVacia+="					<br>En breve, esta información estará disponible.<br>Disculpen las molestias.";
                          txtVacia+="					</td>";
                          txtVacia+="					<td id='ajxFichaMargen'>&nbsp;</td>";
                          txtVacia+="				</tr>";
                          txtVacia+="				<tr>";
                          txtVacia+="					<td id='ajxFichaMargen'>&nbsp;</td>";
                          txtVacia+="					<td id='fichaDireccion'>";
                          txtVacia+="					&nbsp;";
                          txtVacia+="					</td>";
                          txtVacia+="					<td id='ajxFichaMargen'>&nbsp;</td>";
                          txtVacia+="				</tr>";
                          txtVacia+="				<tr>";
                          txtVacia+="					<td id='ajxFichaMargen'>&nbsp;</td>";
                          txtVacia+="							<td id='fichaCalificacion'>&nbsp;</td>";
                          txtVacia+="					</xsl:choose>";
                          txtVacia+="					<td id='ajxFichaMargen'>&nbsp;</td>";
                          txtVacia+="				</tr>";
                          txtVacia+="				</table>";
                          txtVacia+="			</td>";
                          txtVacia+="		</tr>";
                          txtVacia+="		</table>";	
										 		 document.getElementById(capa).innerHTML=txtVacia;
										 }

									
	           }
	           else if(ajax.status==404){
	              capaContenedora.innerHTML = "La direccion no existe";
	           } else {
					 			//alert ("incorrecto:"+ajax.status+"-"+ajax.responseText+"-"+ajax.statusText);
	            	capaContenedora.innerHTML = "Error: "+ajax.status+"<br>Descripcion: "+ajax.statusText;
	           }
          }
	}
	         ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	         ajax.send(valores);
	         return;
	}
	/*Creamos y ejecutamos la instancia si el metodo elegido es GET*/
	if (metodo.toUpperCase()=='GET'){
	
	         ajax.open ('GET', url, true);
	         ajax.onreadystatechange = function() {
	         if (ajax.readyState==1) {
	                                      capaContenedora.innerHTML="<font id='espera'></font>";
	         }
	         else if (ajax.readyState==4){
	                   if(ajax.status==200){
	                                             document.getElementById(capa).innerHTML=ajax.responseText;
	                   }
	                   else if(ajax.status==404)
	                                             {
	
	                            capaContenedora.innerHTML = "La direccion no existe";
	                                             }
	                                             else
	                                             {
	                            capaContenedora.innerHTML = "Error: "+ajax.status+"<br>Descripcion: "+ajax.statusText;
	                                             }
	                                    }
	                  }
	         ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	         ajax.send(null);
	         return
	}
} 

function URLEncode(p1)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = p1;//document.URLForm.F1.value;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	//document.URLForm.F2.value = encoded;
	
	//return false;
	return encoded;
}

/*
Codigo sacado de Netflix

function xmlRequest()
{
	if(window.XMLHttpRequest)
	{
		req=new XMLHttpRequest();
		req.isDOM=true;
		return req;
	}
	else
	{
		if(window.ActiveXObject)
		{
			req=new ActiveXObject("Microsoft.XMLHTTP");
			return req;
		}
		else
		{
			return null;
		}
	}
}

*/



