// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
var MySQLDomServer = "http://www.myavdf.org/inssa/";//solo se necesita se hay conexion con mysql

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}//createXmlHttpRequestObject

// read a file from the server
function process()
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate the asynchronous HTTP request
      xmlHttp.open("GET", "index.php?imagenes=indice", true);
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}//process

// function called when the state of the HTTP request changes
function handleRequestStateChange() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponse();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}//handleRequestStateChange

// handles the response received from the server
function handleServerResponse()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;  
  // obtain arrays with book titles and ISBNs 
  imgArray = xmlRoot.getElementsByTagName("imagen");
  linkArray = xmlRoot.getElementsByTagName("link");
  // generate HTML output
  var html = "";  
  // iterate through the arrays and create an HTML structure
  for (var i=0; i<imgArray.length; i++)
    html += imgArray.item(i).firstChild.data + 
            ", " + linkArray.item(i).firstChild.data + "<br/>";
  // obtain a reference to the <div> element on the page
  // display the HTML output  
  myImg = document.getElementById("logojd");
  myImg.src = imgArray.item(0).firstChild.data;
  myImg = document.getElementById("logoemail");
  myImg.src = imgArray.item(1).firstChild.data;
  myImg.style.cursor = "hand";
  myImg.style.cursor = "pointer";
  myImg.onclick = function(){
	  //abrirSeccion();
	  mostrarEmail("dispensadoras");
   
	};  
  
  myImg = document.getElementById("logoinssa");
  myImg.src = imgArray.item(7).firstChild.data;
  
//inclusiones de scriptaculous!!
  incluir("prototype.js");
  incluir("scriptaculous.js");
  
//hilo que genera la repeticion de las imagenes en la pagina!! 
  //setTimeout("cargarImgIndice();", 6 * 1000);
}//handleServerResponse

//funcion encargada de abrir al seccion correspondiente al icono
function abrirSeccion(url)
{
	var name = "Maquinas dispensadoras automaticas :: JD Ossa SAS - Colombia";
	window.open(url,'_self', name);
	//alert(url);
	}//abrirSeccion

//Funcion encargad de incluir las clases de striptaculous.js y prototype.js
function incluir(clase)
{
	// Create the Script Object
	var script = document.createElement('script');
	script.src = clase;
	script.type = 'text/javascript';
	script.defer = true;
	script.id = 'scriptID'; // This will help us in referencing the object later for removal
	// Insert the created object to the html head element
	//var head = document.getElementsByTagName('head').item(0);
	//head.appendChild(script);
}//incluir

//Funcion encargada de gargar de manera aleatoria los indices o imagenes principales
function cargarImgIndice()
{
	var miArray = new Array(3);
	
	miArray[0] = "img/paginas/indice_vending.png";
	miArray[1] = "img/paginas/indice_sispago.png"; 
	miArray[2] = "img/paginas/indice_bicicletas.png";
	miArray[3] = "img/paginas/indice_horeca.png";
	var randomnumber=Math.floor(Math.random()*4);
	myImg = document.getElementById("indice");
	myImg.innerHTML = "<img src='"+miArray[randomnumber]+"'>";
	//myImg.src =  miArray[randomnumber];

	$(myImg).fade({ duration: 5.0, from: 0, to: 1 });

	//alert(randomnumber);
	setTimeout("cargarImgIndice();", 10 * 1000);
}//cargarImgIndice

//Funcion utilizada para animar los thumbs
function animarThumb(thumb)
{
	$(thumb).fade({ duration: 3.0, from: 0.5, to: 1 });
}//animarThumb

//Funcion utilizada mostrar el formulario de email
function mostrarEmail(thumb)
{
	window.open('email/Email.swf', this.target, 'location=no, width=550,height=400');
}//mostrarEmail

/**
*INICIO RUTINA AJAX ENCARGADA DE MOSTRAR LAS IMAGENES EN REJILLA DE CENTRO DE PAGINA!!!!!!!
**/
//lee un archivo php desde el servidor y retorna el dom para la pagina
function showSeccionesMaqDisp(imagenes)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
	  // initiate the asynchronous HTTP request
      xmlHttp.open("GET", "index.php?imagenes="+imagenes, true);
      xmlHttp.onreadystatechange = handleRequestStateChangeGrid;
      xmlHttp.send(null);//DATA SEND ONLY FOR "POST" MUST BE > 512 PREFFERED
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}//showSeccionesMaqDisp

// function called when the state of the HTTP request changes
function handleRequestStateChangeGrid() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponseGrid();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}//handleRequestStateChangeGrid

// handles the response received from the server
function handleServerResponseGrid()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;  
  // obtain arrays with book titles and ISBNs 
  imgArray = xmlRoot.getElementsByTagName("imagen");
  linkArray = xmlRoot.getElementsByTagName("link");
  //for loop variables
  var arrayLen = imgArray.length;
  var columnas = 4;
  var filas = Math.ceil(arrayLen/columnas);
  
  // generate HTML output
  var html = "";  
  var th = '<table width="100%" height="100%" border="0" cellspacing="2" cellpadding="0">';
  var hrows = '<tr>';
  var trows = '</tr>';
  var tf = '</table>';
  html += th;//header table label add
  var idr = 0;
  
  // iterate through the arrays and create an HTML structure
  for (var i=0; i<filas; i++)
  {
	html += hrows;
	for(var j=0; j<columnas; j++)
	{
		var tdid = "tst"+idr;
		html+='<td id="'+tdid+'"></td>';
		idr++;
	}
	html += trows;
  }
  html += tf;//footer table label add
  
  // obtain a reference to the <div> element on the page
  // display the HTML output  
  myImg = document.getElementById("indice");
  myImg.innerHTML = html;
  
  
  //after retrieve the grid we must print the images
  for (var i=0; i<imgArray.length; i++)
  {
	var tdid = "tst"+i;
	var mtst = document.getElementById(tdid);
	var lnk = linkArray.item(i).firstChild.data;

	mtst.innerHTML = "<img id='' src='"+imgArray.item(i).firstChild.data+"' onclick = location.href('"+lnk+"')>";
	mtst.style.cursor = "hand";
	mtst.style.cursor = "pointer";
	
	}

}//handleServerResponse

/*
*FIN RUTINA AJAX ENCARGADAS DE MOSTRAR LAS IMAGENES EN REJILLA DE CENTRO DE PAGINAĦĦĦ
*/

/*
*INICIO FUNCIONES ENCARGADAS DE GESTIONAR LOS MENUS
*/
	function ocultarMenu(origen)
	{
			if(origen=="maquinas")
			{
				setTimeout("ocultarMaquinas();", 15 * 1000);
			}
			else if(origen=="spago")
			{
				setTimeout("ocultarSispago();", 15 * 1000);
				//alert(origen);
			}
			else
			{
				setTimeout("ocultarBicis();", 15 * 1000);
			}
		}//ocultarMenu
		
	function ocultarMaquinas()
	{
		$('maquinas').setStyle({display: 'block'});
		new Effect.Opacity(
		   'maquinas', { 
			  from: 1.0, 
			  to: 0.0,
			  duration: 1.5
		   }
		);
		}//ocultar
		
	function ocultarSispago()
	{
		$('spago').setStyle({display: 'block'});
		new Effect.Opacity(
		   'spago', { 
			  from: 1.0, 
			  to: 0.0,
			  duration: 1.5
		   }
		);
		}//ocultar

	function mostrarMenu(origen)
	{
		$(origen).setStyle({display: 'block'});
		new Effect.Opacity(
		   origen, { 
			  from: 0.0, 
			  to: 1.0,
			  duration: 1.0
		   }
		);
		}//mostrarMenus
/*
*FIN FUNCIONES ENCARGADAS DE GESTIONAR LOS MENUS
*/