/*
 * Web Site IBGC - 2006-2007
 * Grégory Thiell <gregory.thiell@ibgc.cnrs.fr>
 *
 * JavaScript functions
 */

/*
 * Call a PHP page and return its output
 */

function ajaxcallpage(page)
    {
    if (window.XMLHttpRequest)          //  Objet de la fenêtre courant
        xhr = new XMLHttpRequest();     //  Firefox, Safari, ...
    else if (window.ActiveXObject)      //  Version Active
        xhr = new ActiveXObject("Microsoft.XMLHTTP");   // Internet Explorer 
    
    xhr.open("GET", page, false);
    xhr.send(null);
    
    if(xhr.readyState == 4) return xhr.responseText;
    }

/*
 * Send an e-mail (anti-spam security)
 */

function SendMail(user, host)
  {
  huerel="mailto:"+user+"@"+host;
  self.location=huerel;
  }

/*
 * Make live search in database
 * this function needs the declaration of the array 'table_ids'
 * containing all table's rows ids
 */

function updateSearch(what, s)
    {
    var header = false;
    var noresults = "<h2>Aucune entrée ne correspond à votre recherche.</h2>";
    
    // Call the updateSearch PHP page to search in database
    var dbvalues = ajaxcallpage('updateSearch.php?what='+what+'&s='+escape(s));
//    alert(dbvalues);   // debug
    
    if (what == "annu1")
        {
        var card = dbvalues.split("=");
        if (card[1]) // we only got one value: "=id"
            {
            // Call the displayCard PHP page to display -the- card
            var displaycard = ajaxcallpage('displayCard.php?id='+card[1]);
            document.getElementById("freediv").innerHTML = displaycard;
            // ... and hide the usual table
            hide("searchtable");
            return;
            }
        }
    
    // Parse the results
    var i;
    for(i in table_ids)
        hide("id"+table_ids[i]);
    
    var dbsplit = dbvalues.split(";");
    for(i in dbsplit)
        {
        var showid = dbsplit[i];
        if(showid == "")
            continue;
        showrow("id"+showid);
        header = true;
        }
    
    // Show or hide the entire table
    if (header == false)
        {
        hide("searchtable");
        document.getElementById("freediv").innerHTML = noresults;
        }
    else
        {
        showtable("searchtable");
        document.getElementById("freediv").innerHTML = "";
        }
    }

/*
 * Hide table or table rows
 * N.B. show() is declared in index_head.php
 */

function hide(id)
    {
    document.getElementById(id).style.display = "none";
    }

/*
 * Focus on the search field
 */

function sfocus(id)
    {
    var s = document.getElementById(id);
    if (s.value == "recherche...")
        s.value = "";
    s.style.color = "black";
    }
