
function $(elem)
{
  return document.getElementById(elem);
}

function getXhr() 
{
  var xhr = null; 
  if(window.XMLHttpRequest) // Firefox et autres
    xhr = new XMLHttpRequest(); 
  else if(window.ActiveXObject){ // Internet Explorer 
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  else { // XMLHttpRequest non supporté par le navigateur 
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
    xhr = false; 
  } 
  return xhr
}


/* Compétitions */

function showMeeting(num) 
{
  if(navigator.appName == "Microsoft Internet Explorer")
    document.getElementById("infomeeting-"+num).style.display = "block";
  else
    document.getElementById("infomeeting-"+num).style.display = "table-row";
  document.getElementById("icon-meeting-"+num).src = "http://www.lbfa.be/cal/img/close.gif";
  document.getElementById("url-icon-meeting-"+num).href = "javascript:hideMeeting("+num+");";
  document.getElementById("url-text-meeting-"+num).href = "javascript:hideMeeting("+num+");";
  document.getElementById("nom2-"+num).style.display = "block";
  document.getElementById("meeting-"+num).style.fontWeight = "bold";
  //document.getElementById("meeting-"+num).style.background = "#E46C47";
  //document.getElementById("meeting-"+num).style.color = "white";
}

function hideMeeting(num) 
{
  document.getElementById("infomeeting-"+num).style.display = "none";
  document.getElementById("icon-meeting-"+num).src = "http://www.lbfa.be/cal/img/open.gif";
  document.getElementById("url-icon-meeting-"+num).href = "javascript:showMeeting("+num+");";
  document.getElementById("url-text-meeting-"+num).href = "javascript:showMeeting("+num+");";
  document.getElementById("nom2-"+num).style.display = "none";
  document.getElementById("meeting-"+num).style.fontWeight = "normal";
  //document.getElementById("meeting-"+num).style.background = "";
  //document.getElementById("meeting-"+num).style.color = "black";
}

/* Opérations */

function showAll()
{ 
  var parent = document.getElementById("compets"); 
  var link = document.getElementById("oper_showhideall");
  
  for(i = 0 ; i < parent.rows.length ; i = i + 2)
  {
    if(parent.rows[i].id != "")
      showMeeting(parent.rows[i].id.substring(8));
  }
  if(link)
  {
    link.href = "javascript:hideAll();"
    link.title = "Tout réduire";
    link.innerHTML = '<img src="http://www.lbfa.be/cal/img/closeall16.gif" />';
  }
}

function hideAll()
{ 
  var parent = document.getElementById("compets"); 
  var link = document.getElementById("oper_showhideall");
  
  if(parent)
  {
    for(i = 0 ; i < parent.rows.length ; i = i + 2)
    {
      if(parent.rows[i].id != "")
        hideMeeting(parent.rows[i].id.substring(8));
    }
    link.href = "javascript:showAll();";
    link.title = "Tout déployer";
    link.innerHTML = '<img src="http://www.lbfa.be/cal/img/openall16.gif" />';
  }
}

function printSelection() 
{
  var id = $("id").value; 
  var cal = $("cal").value; 
  var org = $("org").value; 
  var prov = $("prov").value;
  var type = $("type").value;
  var mois = $("mois").value;
  var cate = $("listcate").value;
  var epr = $("listepr").value;
  
  if(id && id == null)
    id = "";
  if(cal && cal == null)
    cal = "";
  if(org && org == null)
    org = "";
  if(prov && prov == null)
    prov = "";
  if(type && type == null)
    type = "";
  if(mois && mois == null)
    mois = "";
  if(cate && cate == null)
    cate = "";
  if(epr && epr == null)
    epr = "";
    
  window.open("print.php?id="+id+"&cal="+cal+"&org="+org+"&prov="+prov+"&type="+type+"&mois="+mois+"&cate="+cate+"&epr="+epr, "lbfacal");
}

function exportpdf() 
{
  var id = $("id").value; 
  var cal = $("cal").value; 
  var org = $("org").value; 
  var prov = $("prov").value;
  var type = $("type").value;
  var mois = $("mois").value;
  var cate = $("listcate").value;
  var epr = $("listepr").value;
  
  if(id && id == null)
    id = "";
  if(cal && cal == null)
    cal = "";
  if(org && org == null)
    org = "";
  if(prov && prov == null)
    prov = "";
  if(type && type == null)
    type = "";
  if(mois && mois == null)
    mois = "";
  if(cate && cate == null)
    cate = "";
  if(epr && epr == null)
    epr = "";
    
  window.open("exportpdf.php?id="+id+"&cal="+cal+"&org="+org+"&prov="+prov+"&type="+type+"&mois="+mois+"&cate="+cate+"&epr="+epr, "lbfacal");
}

function iCalSelection() 
{
  var id = $("id").value; 
  var cal = $("cal").value; 
  var org = $("org").value; 
  var prov = $("prov").value;
  var type = $("type").value;
  var mois = $("mois").value;
  var cate = $("listcate").value;
  var epr = $("listepr").value;
  
  if(id && id == null)
    id = "";
  if(cal && cal == null)
    cal = "";
  if(org && org == null)
    org = "";
  if(prov && prov == null)
    prov = "";
  if(type && type == null)
    type = "";
  if(mois && mois == null)
    mois = "";
  if(cate && cate == null)
    cate = "";
  if(epr && epr == null)
    epr = "";
    
  window.open("iCal.php?id="+id+"&cal="+cal+"&org="+org+"&prov="+prov+"&type="+type+"&mois="+mois+"&cate="+cate+"&epr="+epr, "lbfacal");
}

function csvSelection() 
{
  var id = $("id").value; 
  var cal = $("cal").value; 
  var org = $("org").value; 
  var prov = $("prov").value;
  var type = $("type").value;
  var mois = $("mois").value;
  var cate = $("listcate").value;
  var epr = $("listepr").value;
  
  if(id && id == null)
    id = "";
  if(cal && cal == null)
    cal = "";
  if(org && org == null)
    org = "";
  if(prov && prov == null)
    prov = "";
  if(type && type == null)
    type = "";
  if(mois && mois == null)
    mois = "";
  if(cate && cate == null)
    cate = "";
  if(epr && epr == null)
    epr = "";
    
  window.open("csv.php?id="+id+"&cal="+cal+"&org="+org+"&prov="+prov+"&type="+type+"&mois="+mois+"&cate="+cate+"&epr="+epr, "lbfacal");
}

function vCalSelection() 
{
  var id = $("id").value; 
  var cal = $("cal").value; 
  var org = $("org").value; 
  var prov = $("prov").value;
  var type = $("type").value;
  var mois = $("mois").value;
  var cate = $("listcate").value;
  var epr = $("listepr").value;
  
  if(id && id == null)
    id = "";
  if(cal && cal == null)
    cal = "";
  if(org && org == null)
    org = "";
  if(prov && prov == null)
    prov = "";
  if(type && type == null)
    type = "";
  if(mois && mois == null)
    mois = "";
  if(cate && cate == null)
    cate = "";
  if(epr && epr == null)
    epr = "";
    
  window.open("vCal.php?id="+id+"&cal="+cal+"&org="+org+"&prov="+prov+"&type="+type+"&mois="+mois+"&cate="+cate+"&epr="+epr, "lbfacal");
}

/*** Filtres ***/

function listCompets()
{
  var xhr = getXhr();
  
  var id = $("id").value; 
  var cal = $("cal").value;
  var saison = $("saison").value;
  var org = $("org").value; 
  var prov = $("prov").value;
  var type = $("type").value;
  var mois = $("mois").value;
  var cate = $("listcate").value;
  var epr = $("listepr").value;
  var past = $("past").value;
  
  if(id && id == null)
    id = "";
  if(cal && cal == null)
    cal = "";
  if(saison && saison == null)
    saison = "";
  if(org && org == null)
    org = "";
  if(prov && prov == null)
    prov = "";
  if(type && type == null)
    type = "";
  if(mois && mois == null)
    mois = "";
  if(cate && cate == null)
    cate = "";
  if(epr && epr == null)
    epr = "";
  if(past && past == null)
    past = "";
    
  xhr.onreadystatechange = function (){;
    $("divCompets").innerHTML = "<center><br /><br /><br /><img src='http://www.lbfa.be/cal/img/wait.gif'/><br /><br /><br />Chargement des données...</center>";
    if(xhr.readyState == 4 && xhr.status == 200){
      $("divCompets").innerHTML = xhr.responseText;
      // Nombre de compétitions + Bouton "Tout déployer"
      if($("compets"))
      {
        $("resumeCompets").style.display = "block";
        if($("compets").summary == 1)
        {
          $("nbCompets").innerHTML = $("compets").summary + " compétition";
          showAll();
        }
        else
        {
          $("nbCompets").innerHTML = $("compets").summary + " compétitions";
          hideAll();
        }
      }
      else
        $("resumeCompets").style.display = "none";
    }
  }
  
  xhr.open("POST","ajaxlistcompets.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send("id="+id+"&cal="+cal+"&saison="+saison+"&org="+org+"&prov="+prov+"&type="+type+"&mois="+mois+"&cate="+cate+"&epr="+epr+"&past="+past);
}


/* ID */


function showOne(id) 
{
  document.filterForm.id.value = id;
  listCompets();
}


/* Organisation */

function changeOrg()
{
  document.filterForm.id.value = "";
  document.filterForm.prov.value = "";
  listCompets();
}

function changeProv()
{
  document.filterForm.id.value = "";
  document.filterForm.org.value = "";
  listCompets();
}

function changeType()
{
  document.filterForm.id.value = "";
  listCompets();
}

/* Période */

function changeMonth()
{
  document.filterForm.id.value = "";
  listCompets();
}

function changePast()
{
  if($("past").value == "O")
  {
    $("past").value = "N";
    $("past").checked = "";
  }
  else
  {
    $("past").value = "O";
    $("past").checked = "checked";
  }
  listCompets();
}

/* Programme */

function selectCate(cal)
{ 
  //if(document.filterForm.cate.value)
    //$("listcate").value = $("listcate").value+document.filterForm.cate.value+",";
  selectEpr(cal,document.filterForm.cate.value);
}

function selectEpr(cal,cate)
{
  var xhr = getXhr();
  
  xhr.onreadystatechange = function (){;
    if(xhr.readyState == 4 && xhr.status == 200){ 
      $("divepr").innerHTML = xhr.responseText;
    }
  }
  
  xhr.open("POST","ajaxselectepr.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send("cal="+cal+"&cate="+cate);
}

function addEpr()
{
  var row;
  document.filterForm.id.value = "";
  
  //if($("listcate").value != "")
    //$("listcate").value = $("listcate").value + ",";
  $("listcate").value = $("listcate").value + document.filterForm.cate.value+ ",";
  
  //if($("listepr").value != "")
    //$("listepr").value = $("listepr").value + ",";
  $("listepr").value = $("listepr").value + document.filterForm.epr.value+ ",";
  
  var listcate = new String($("listcate").value);
  var cate = listcate.split(','); 
  var listepr = new String($("listepr").value);
  var epr = listepr.split(','); 
  
  id = cate.length-1; // vu virgule finale
  rowtext = "<tr id='row"+id+"'>";
  rowtext = rowtext + "<td>"+document.filterForm.cate[document.filterForm.cate.selectedIndex].text+"</td>";
  rowtext = rowtext + "<td>"+document.filterForm.epr[document.filterForm.epr.selectedIndex].text+"</td>";
  rowtext = rowtext + "<td><a href='javascript:delEpr("+id+");'><img src='../img/close_dashboard.png'/></a></td>";
  rowtext = rowtext + "</tr>";  
  //$("listOfCatEpr").innerHTML = $("listOfCatEpr").innerHTML + rowtext;
  
  var lastRow = $("listOfCatEpr").rows.length;
  var row = $("listOfCatEpr").insertRow(lastRow);
  row.id = 'row'+id;
  
  var cell1 = row.insertCell(0);
  var textNode = document.createTextNode(document.filterForm.cate[document.filterForm.cate.selectedIndex].text);
  cell1.appendChild(textNode);  
  
  var cell2 = row.insertCell(1); 
  var textNode = document.createTextNode(document.filterForm.epr[document.filterForm.epr.selectedIndex].text);
  cell2.appendChild(textNode);
  
  var cell3 = row.insertCell(2); 
  var textNode = document.createTextNode("<a href='javascript:delEpr("+id+");'><img src='../img/close_dashboard.png' /></a>");
  var link = document.createElement("a");
  link.setAttribute('href', "javascript:delEpr("+id+");");
  var img = document.createElement('img');
  img.setAttribute('src', "../img/close_dashboard.png");
  link.appendChild(img);
  cell3.appendChild(link);
  
  listCompets();
}

function delEpr(id)
{

  var listcate = new String($("listcate").value);
  var cate = listcate.split(','); 
  var listepr = new String($("listepr").value);
  var epr = listepr.split(','); 
  
  //cate.splice(id-1,1); // 0 au lieu de 1
  //epr.splice(id-1,1);
  cate[id-1] = "";
  epr[id-1] = "";
  
  $("listcate").value = "";
  for(i = 0 ; i < cate.length ; i++)
  {
    //if(cate[i] != null)
      $("listcate").value = $("listcate").value + cate[i] + ",";
  }
  $("listepr").value = "";
  for(i = 0 ; i < epr.length ; i++)
  {
    //if(epr[i] != null)
      $("listepr").value = $("listepr").value + epr[i] + ",";
  }
  
  $("row"+id).style.display = "none";
  listCompets();
}

/*** Opérations ***/

function showHisto(id)
{
  alert("Fonction en cours de développement");
}

function follow(id)
{
  alert("Fonction en cours de développement");
}

function savePageAsPDF()
{
 var authorId = "1CF832F0-3EE7-48AA-953F-AA2BCEAD19FA";
 var pageOrientation = "0";
 var topMargin = "0.5";
 var bottomMargin = "0.5";
 var leftMargin = "0.5";
 var rightMargin = "0.5";
 var sUriRequest = "";
 sUriRequest = "author_id=" + authorId;
 sUriRequest += "&page=" + pageOrientation;
 sUriRequest += "&top=" + topMargin;
 sUriRequest += "&bottom=" + bottomMargin;
 sUriRequest += "&left=" + leftMargin;
 sUriRequest += "&right=" + rightMargin;
 // savepageaspdf.pdfonline.com
 var pURL = "http://savepageaspdf.pdfonline.com/pdfonline/pdfonline.asp?cURL=" + "http://www.lbfa.be/cal/public/print.php&" + sUriRequest;
  window.open(pURL, "PDFOnline", "scrollbars=yes,resizable=yes,width=640,height=480,menubar,toolbar,location");
}

function calOver(link,cal)
{
  if(cal == "O")
    src = "http://www.lbfa.be/images/calendrieroutdoor.png";
  if(cal == "I")
    src = "http://www.lbfa.be/images/calendrierindoor.png";
  if(cal == "C")
    src = "http://www.lbfa.be/images/calendriercross.png";
  
  link.childNodes[0].src = src;  
}

function calOut(link,cal)
{
  if(cal == "O")
    src = "http://www.lbfa.be/images/calendrieroutdoorNB.png";
  if(cal == "I")
    src = "http://www.lbfa.be/images/calendrierindoorNB.png";
  if(cal == "C")
    src = "http://www.lbfa.be/images/calendriercrossNB.png";
  
  link.childNodes[0].src = src;  
}

function miniCalOver(link,typecal)
{
  if(typecal == "iCal"){
    src = "http://www.lbfa.be/cal/img/ical.png";
    width = "19";
    };
  if(typecal == "vCal"){
    src = "http://www.lbfa.be/cal/img/vcal.png";
    width = "19";
    };
  if(typecal == "csv"){
    src = "http://www.lbfa.be/cal/img/csv.png";
    width = "19";
    };
  link.childNodes[0].src = src;  
  link.childNodes[0].width = width; 
}

function miniCalOut(link,typecal)
{
  if(typecal == "iCal"){
    src = "http://www.lbfa.be/cal/img/icon_export_ical.png";
    width = "16";
    };
  if(typecal == "vCal"){
    src = "http://www.lbfa.be/cal/img/icon_export_vcal.png";
    width = "16";
    };
  if(typecal == "csv"){
    src = "http://www.lbfa.be/cal/img/miniCal.png";
    width = "19";
    };
  link.childNodes[0].src = src;  
  link.childNodes[0].width = width; 
}
