var ajaxdestination="";
var ajaxdestination2="";

var set = "";

var control = 0;

var pag = "";

var val = "";

var cont = 0;
// creamos la funcion getXMLHTTPRequest que identifica al navegador y crea el objeto para la coneccion con ajax
function getXMLHTTPRequest() {
	try {
	req = new XMLHttpRequest();
	} catch(err1) {
	  try {
	  req = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (err2) {
		try {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (err3) {
		  req = false;
		}
	  }
	}
	return req;
}

//almacenamos la funcion getXMLHTTPRequest en la variable http para usarla en todo el script ajax
var http = getXMLHTTPRequest();
	
//la funcion enviarHTTP envia los datos al servidor
function enviarHTTP (pagina, contenedor, valores) 
{
	/*if(pagina == "descargas_interior.php")
	{
		alert("control 0");
		control = 0;
	}*/
	control = 0;
	var rand = parseInt(Math.random()*99999999); //se usa para que la pagina no se cargue del cache del explorador, colocando un n&#250;mero aleatorio en la url nunca se usar&#225; la del cache
	
	//pag = pagina;
	//alert("tu padre");
	http.open("POST", pagina, true); // abrimos la coneccion indicando el m&#233;todo (GET), la url y si queremos una peticion asincr&#243;nica
	
	ajaxdestination2 = contenedor;
	//display(ajaxdestination2, 1, 25);
	/*var respuesta = "<script>display ('element1',0,1);</script>";
	document.getElementById(ajaxdestination2).innerHTML = respuesta;*/
	
	//fillProgress('element1',25);
	
	http.onreadystatechange = respuestaHTTP; //cuando la respuesta del servidor cambie de estado llamamos a la funcion que creamos abajo que devuelve los datos del servidor
	//Ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	if(valores!="")
	{
		http.send(valores); // se envia la petici&#243;n
	}
	else
	{
		http.send(null); // se envia la petici&#243;n
	}
}

//cuando la respuesta del servidor este completa esta funcion muestra los datos devueltosservidor
function respuestaHTTP () 
{
	//alert(http.readyState);
	if (http.readyState == 4) 
	{  // el numero 4 significa que la peticion ha sido completada
		
		if (http.status == 200) 
		{ // el 200 significa que la petici&#243;n fue completada correctamente
			var respuesta_php = http.responseText; //almacenamos la respuesta del servidor en una variable
			document.getElementById(ajaxdestination2).innerHTML = respuesta_php; //insertamos el contenido que nos devolvi&#243; el servidor en el <div> de HTML
		}
		else
		{
			//alert(http.status);
			//si hay algun error lo muestra y detiene la petici&#243;n
			alert("Ha habido un problema al acceder al servidor: " + http.statusText);
		}
	}
	if (http.readyState < 4) 
	{
		//document.getElementById(ajaxdestination).innerHTML = 'ajax.gif';
		//esto es para que muestre un mensaje mientras se realiza el proceso
	}
	
}

/* WebAppers Progress Bar, version 0.2
* (c) 2007 Ray Cheung
*
* WebAppers Progress Bar is freely distributable under the terms of an Creative Commons license.
* For details, see the WebAppers web site: http://wwww.Webappers.com/
*
/*--------------------------------------------------------------------------*/

var initial = -120;
var imageWidth=240;
var eachPercent = (imageWidth/2)/100;
/************************************************************\
*
\************************************************************/
function setText (id, percent)
{
    $(id+'Text').innerHTML = percent+"%";
}
/************************************************************\
*
\************************************************************/
function display ( id, percentage,color )
{
	//alert(id);
	if (typeof color == "undefined") {
    color = "1";
  	}
    var percentageWidth = eachPercent * percentage;
    var actualWidth = initial + percentageWidth ;
    document.write('<img id="'+id+'" src="images/percentImage.png" alt="'+percentage+'%" class="percentImage'+color+'" style="background-position: '+actualWidth+'px 0pt;"/> <span id="'+id+'Text">'+percentage+'%</span>');
}
/************************************************************\
*
\************************************************************/
function emptyProgress(id)
{
    var newProgress = initial+'px';
    $(id).style.backgroundPosition=newProgress+' 0';
    setText(id,'0');
}
/************************************************************\
*
\************************************************************/
function getProgress(id)
{
    var nowWidth = $(id).style.backgroundPosition.split("px");
    return (Math.floor(100+(nowWidth[0]/eachPercent))+'%');
	
}
/************************************************************\
*
\************************************************************/
function setProgress(id, percentage)
{
    var percentageWidth = eachPercent * percentage;
    var newProgress = eval(initial)+eval(percentageWidth)+'px';
    $(id).style.backgroundPosition=newProgress+' 0';
    setText(id,percentage);
}
/************************************************************\
*
\************************************************************/
function plus ( id, percentage )
{
    var nowWidth = $(id).style.backgroundPosition.split("px");
    var nowPercent = Math.floor(100+(nowWidth[0]/eachPercent))+eval(percentage);
    var percentageWidth = eachPercent * percentage;
    var actualWidth = eval(nowWidth[0]) + eval(percentageWidth);
    var newProgress = actualWidth+'px';
    if(actualWidth>=0 && percentage <100)
    {
        var newProgress = 1+'px';
        $(id).style.backgroundPosition=newProgress+' 0';
        setText(id,100);
        //alert('full');
    }
    else
    {
        $(id).style.backgroundPosition=newProgress+' 0';
        setText(id,nowPercent);
    }
}
/************************************************************\
*
\************************************************************/
function minus ( id, percentage )
{
    var nowWidth = $(id).style.backgroundPosition.split("px");
    var nowPercent = Math.floor(100+(nowWidth[0]/eachPercent))-eval(percentage);
    var percentageWidth = eachPercent * percentage;
    var actualWidth = eval(nowWidth[0]) - eval(percentageWidth);
    var newProgress = actualWidth+'px';
    if(actualWidth<=-120)
    {
        var newProgress = -120+'px';
        $(id).style.backgroundPosition=newProgress+' 0';
        setText(id,0);
        alert('empty');
    }
    else
    {
        $(id).style.backgroundPosition=newProgress+' 0';
        setText(id,nowPercent);
    }
}
/************************************************************\
*
\************************************************************/
function fillProgress(id, endPercent)
{
	//alert(id);
    var nowWidth = $(id).style.backgroundPosition.split("px");
    startPercent = Math.ceil(100+(nowWidth[0]/eachPercent))+1;
    var actualWidth = initial + (eachPercent * endPercent);
    if (startPercent <= endPercent && nowWidth[0] <= actualWidth)
    {
        plus(id,'1');
        setText(id,startPercent);
        setTimeout("fillProgress('"+id+"',"+endPercent+")",10);
    }
}