var xmlhttp;
var idGral;
var pathGral;

function loadTestimonials(url,params,id,path){
  //alert("Entro 2");
  idGral = id;
  pathGral = path;
  xmlhttp = null;
  if (window.XMLHttpRequest)// code for Firefox, Opera, IE7, etc.
    xmlhttp = new XMLHttpRequest();
  else if (window.ActiveXObject){// code for IE6, IE5
    try {
      xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
      }
    catch(e) {
      try {
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
      catch(e) {
        xmlhttp = false;
        }
      }
    }
  if (xmlhttp != null){
    if(id == "primero")
      xmlhttp.onreadystatechange = stateChanged2;
    else if(id == "listado")
      xmlhttp.onreadystatechange = stateChanged3;
    else
      xmlhttp.onreadystatechange = stateChanged4;
    xmlhttp.open('GET',url,true);
    xmlhttp.send();
    }
  else{
    newDiv = document.createElement('div');
    newDiv.setAttribute('id', 'error');
    newDiv.innerHTML = '<!-- Your browser does not support XMLHTTP. -->';
    document.getElementById('listado').appendChild(newDiv);
    }
}

/***********************************
* @method RemoveWhitespace
* @param {Object} xml
************************************/
function RemoveWhitespace(xml){
  var loopIndex;
  for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++){
    var currentNode = xml.childNodes[loopIndex];
    if (currentNode.nodeType == 1){RemoveWhitespace(currentNode);}
    if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)){
      xml.removeChild(xml.childNodes[loopIndex--]);
    }
  }
  return xml;
}

function stateChanged2(){
  if (xmlhttp.readyState == 4){// 4 = "loaded"
    if (xmlhttp.status == 200 || xmlhttp.status == 304){  // 200 = "OK"
      xmlDoc = RemoveWhitespace(xmlhttp.responseXML);
      var elements = xmlDoc.childNodes[0].childNodes;
      var div_testimonios = document.getElementById('testimonio');
      id = elements[0].getAttribute('id');
      nombre = elements[0].getAttribute('nombre');
      if(elements[0].childNodes[0].textContent != undefined)
        titulo = elements[0].childNodes[0].textContent;
      if(elements[0].childNodes[0].text != undefined)
        titulo = elements[0].childNodes[0].text;
      if(elements[0].childNodes[1].textContent != undefined)
        descripcion = elements[0].childNodes[1].textContent;
      if(elements[0].childNodes[1].text != undefined)
        descripcion = elements[0].childNodes[1].text;
      div_testimonios.innerHTML = "<p><a href='testimonios.php'><img src='"+pathGral+"images/testimonios/small/"+id+".jpg' alt='"+nombre+"' width='50' height='58' border='0' title='"+nombre+"' /><b>"+nombre+":</b><br /><br />"+titulo+"</a></p>";
      //div_testimonios.innerHTML = "Entro";
      }
    else{
      newDiv = document.createElement('div');
      newDiv.setAttribute('id', 'error');
      newDiv.innerHTML = '<!-- Problem retrieving data:'+xmlhttp.statusText+' -->';
      document.getElementById('div_testimonios').appendChild(newDiv);
      }
    }
}

function stateChanged3(){
  if (xmlhttp.readyState == 4){// 4 = "loaded"
    if (xmlhttp.status == 200 || xmlhttp.status == 304){  // 200 = "OK"
      //return xmlhttp.responseText;
      //return xmlhttp.responseXML;
      //alert("Entro");
      xmlDoc = RemoveWhitespace(xmlhttp.responseXML);
      var elements = xmlDoc.childNodes[0].childNodes;
      var listado_principal = document.getElementById('listado');
      var listado = document.createElement('tbody');
      
      //alert("Primero="+xmlDoc.childNodes[1].nodeName);
      for(i = 0 ; i < elements.length ; i++){
        //alert("i="+i+" Lenght="+elements.length+" ID="+elements[i].getAttribute('id')+" Nombre="+elements[i].getAttribute('nombre'));
        if(i == 0)
          newTR = document.createElement('tr');
        else if(i%3 == 0){
          listado.appendChild(newTR);
          newTR = document.createElement('tr');
          newTD = document.createElement('td');
          newTD.setAttribute('colspan', '3');
          newTD.setAttribute('class', 'center');
          newTD.setAttribute('align', 'center');
          newTD.innerHTML = '&nbsp;';
          newTR.appendChild(newTD);
          listado.appendChild(newTR);
          newTR = document.createElement('tr');
          }
        id = elements[i].getAttribute('id');
        nombre = elements[i].getAttribute('nombre');
        fecha = elements[i].getAttribute('fecha');
        if(elements[i].childNodes[0].textContent != undefined)
          titulo = elements[i].childNodes[0].textContent;
        if(elements[i].childNodes[0].text != undefined)
          titulo = elements[i].childNodes[0].text;
        newTD = document.createElement('td');
        newTD.setAttribute('valign', 'top');
        newTD.setAttribute('class', 'center');
        newTD.setAttribute('align', 'center');
        newTD.setAttribute('width', '33%');
        newTD.innerHTML = '<a href="testimonios.php?s=descripcion&id='+id+'"><img src="'+pathGral+'images/testimonios/small/'+id+'.jpg" alt="'+nombre+'" width="50" height="58" border="0" title="'+nombre+'" /><br /><b>'+nombre+':</b><br />'+titulo+'</a>';
        newTR.appendChild(newTD);
        if(i+1 >= elements.length){
          listado.appendChild(newTR);
          newTR = document.createElement('tr');
          newTD = document.createElement('td');
          newTD.setAttribute('colspan', '3');
          newTD.setAttribute('calss', 'center');
          newTD.setAttribute('align', 'center');
          newTD.innerHTML = '&nbsp;';
          newTR.appendChild(newTD);
          listado.appendChild(newTR);
          }
        }
      
      listado_principal.appendChild(listado);
      
      }
    else{
      newDiv = document.createElement('div');
      newDiv.setAttribute('id', 'error');
      newDiv.innerHTML = '<!-- Problem retrieving data:'+xmlhttp.statusText+' -->';
      document.getElementById('listado').appendChild(newDiv);
      }
    }
}

function stateChanged4(){
  if (xmlhttp.readyState == 4){// 4 = "loaded"
    if (xmlhttp.status == 200 || xmlhttp.status == 304){  // 200 = "OK"
      //alert("idGral="+idGral);
      xmlDoc = RemoveWhitespace(xmlhttp.responseXML);
      var elements = xmlDoc.childNodes[0].childNodes;
      var desc = document.getElementById('desc');
      for(i = 0 ; i < elements.length ; i++){
        id = elements[i].getAttribute('id');
        if(id == idGral || i == 0){
          nombre = elements[i].getAttribute('nombre');
          if(elements[i].childNodes[0].textContent != undefined)
            titulo = elements[i].childNodes[0].textContent;
          if(elements[i].childNodes[0].text != undefined)
            titulo = elements[i].childNodes[0].text;
          if(elements[i].childNodes[1].textContent != undefined)
            descripcion = elements[i].childNodes[1].textContent;
          if(elements[i].childNodes[1].text != undefined)
            descripcion = elements[i].childNodes[1].text;
          if(id == idGral){
            desc.innerHTML = "<h3>"+titulo+"</h3><p><b>"+nombre+"</b></p><img src='"+pathGral+"images/testimonios/big/"+id+".jpg' alt='"+nombre+"' width='120' height='138' border='0' title='"+nombre+"' class='float_left' />"+descripcion+"<br /><br />";
            break;
            }
          }
        }
      }
    else{
      newDiv = document.createElement('div');
      newDiv.setAttribute('id', 'error');
      newDiv.innerHTML = '<!-- Problem retrieving data:'+xmlhttp.statusText+' -->';
      document.getElementById('desc').appendChild(newDiv);
      }
    }
}
