/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//loads contents of url to div header_right
function loadLogin(url)
{
var xmlhttp;
var txt,xx,xy,xz,x,i,ext;

    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else{// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("header_right").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}


function loadAgenda(url)
{
var xmlhttp;
var txt,xx,xy,xz,x,i;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    txt="<h2>Et portem a...</h2><ul>";
    x=xmlhttp.responseXML.documentElement.getElementsByTagName("event");
    for (i=0;i<x.length;i++)
      {
      //txt=txt + "<li>";
      xz=x[i].getElementsByTagName("dataini");
      xx=x[i].getElementsByTagName("titol");
      xy=x[i].getElementsByTagName("descripcio");
      {
        try
          {
          txt=txt + "<li title=\""+ xy[0].firstChild.nodeValue +"\">" + xz[0].firstChild.nodeValue + " " + xx[0].firstChild.nodeValue + "</li>";
          }
        catch (er)
          {
          txt=txt + "<li>&nbsp;</li>";
          }
        }
      txt=txt + "</li>";
      }
    txt=txt + "</ul>";
    document.getElementById('agenda').innerHTML=txt;
    }
  }
xmlhttp.open("GET",url,true);
xmlhttp.send();
}

function loadMenu(url)
{
var xmlhttp;
var txt,xx,xy,x,i;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    txt="<ul>";
    x=xmlhttp.responseXML.documentElement.getElementsByTagName("section");
    for (i=0;i<x.length;i++)
      {
      xx=x[i].getElementsByTagName("titol");
      xy=x[i].getElementsByTagName("link");
      {
        try
          {
              if( xy[0].firstChild.nodeValue=="services.xml"){
                txt=txt + "<li><a href=\"#\" onclick=\"loadServices('"+xy[0].firstChild.nodeValue+"')\">" + xx[0].firstChild.nodeValue + "</a></li>";
              }
              else{
                txt=txt + "<li><a href=\"#\" onclick=\"loadContent('"+xy[0].firstChild.nodeValue+"')\">" + xx[0].firstChild.nodeValue + "</a></li>";
              }
          }
        catch (er)
          {
          txt=txt + "<li>&nbsp;</li>";
          }
        }
      }
    txt=txt + "</ul>";
    document.getElementById('menu').innerHTML=txt;
    }
  }
xmlhttp.open("GET",url,true);
xmlhttp.send();
}

function stripExtension(url){
    var separator=".";
    var pos,max,ext;
    max=url.toString().length;
    if(max > 0){
        pos=url.toString().lastIndexOf(separator);
        //pos is de position of the character -> ext=".xml"
        //pos+1 is the next position -> ext="xml"
        ext=url.toString().substr(pos+1, max);
    }
    else{
        ext="-1";
    }
    return ext;
}

function loadContent(url)
{
var xmlhttp;
var txt,xx,xy,xz,x,i,ext;

ext=stripExtension(url);
if(ext=="-1"){
    exit;
}
else if(ext=="xml"){
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        txt="";
        x=xmlhttp.responseXML.documentElement.getElementsByTagName("section");
        for (i=0;i<x.length;i++)
          {
          xx=x[i].getElementsByTagName("idioma");
          xy=x[i].getElementsByTagName("titol");
          xz=x[i].getElementsByTagName("content");
          {
            try
              {
                  //comprovem idioma
                  if (xx[0].firstChild.nodeValue == "cat"){
                    txt=txt + "<h2 class=\"content\">Home > "+xy[0].firstChild.nodeValue+"</h2><br/><pre>" + xz[0].firstChild.nodeValue + "</pre>";
                  }
              }
            catch (er)
              {
              txt=txt + "<p>&nbsp;</p>";
              }
            }
          }
        document.getElementById("content").innerHTML=txt;
        }
      }
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
    }
    else if(ext=="html"){
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else{// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById("content").innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open("GET",url,true);
        xmlhttp.send();
        }
    
    else {
        alert("error extensio");
    }
}

function loadServices(url)
{
var xmlhttp;
var txt,xv,xx,xy,xz,x,i;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        txt="<h2 class=\"cat\">Home > Serveis</h2><br/>";
        txt=txt + "<h2 class=\"es\">Home > Servicios</h2>";
        txt=txt + "<h2 class=\"en\">Home > Services</h2>";
        txt=txt + "<h2 class=\"fr\">Home > Services</h2>";
        x=xmlhttp.responseXML.documentElement.getElementsByTagName("section");
        for (i=0;i<x.length;i++){
          //txt=txt + "<li>";
          xv=x[i].getElementsByTagName("idioma");
          xx=x[i].getElementsByTagName("titol");
          xy=x[i].getElementsByTagName("imatge");
          xz=x[i].getElementsByTagName("descripcio");

          try{
              //services layout
              txt=txt + "<img class=\"float\" src=\""+ xy[0].firstChild.nodeValue +"\" style=\"margin-right:10px;\"><h3>"+ xx[0].firstChild.nodeValue +"</h3><p>" + xz[0].firstChild.nodeValue +"</p>";
          }
          catch (er){
              txt=txt + "<br/>";
          }
          txt=txt + "<p class=\"clear\">&nbsp;</p>";
        }
        document.getElementById('content').innerHTML=txt;
    }
  }
xmlhttp.open("GET",url,true);
xmlhttp.send();
}

