
function initPage() {
  divs = document.body.getElementsByTagName('div');
  var m = new Array();
  
  // Tous les divs dont l'id se termine par _handle sont des poignées pour les
  // onglets. On redéfinit leur élément onclick pour ne pas surcharger le HTML. 
  // 
  // Un onglet est : 
  //  <div class="container">
  //    <div id="tab1_handle"></div>
  //    <div id="tab2_handle"></div>
  //    <div id="tab_tab1">Content Alpha</div>
  //    <div id="tab_tab2">Content Phi</div>
  // </div>
  //

  for (i=0 ; i<divs.length ; i++) {
    if (divs[i].id.match(/(.*)_handle/)) {
      divs[i].onclick=focusTab;
    }
    // Delete icon. Click on it and we fire a deleteItem call. See below.
    if (divs[i].className.match(/deleteicon/)) {
      if (divs[i].onclick == null) 
        divs[i].onclick=deleteItem;
    }
  }

  // Les champs de class "montant" contiennent des zeuros, on y raccroche un
  // callback pour avoir un comportement de saisie web2.2
  inputs = document.body.getElementsByTagName('input');
  for (i=0 ; i<inputs.length ; i++) {
    if (inputs[i].className.match(/montant/)) {
      inputs[i].onblur=blurMoneyField;
      inputs[i].onfocus=focusMoneyField;
    }
  }
}

function blurMoneyField(e) {
  if (e) {
    f = e.target;
  } else {  // IE
    f = window.event.srcElement;
  }

  if (f.value.match(/^[0-9,.]+$/)) {
    f.value.replace('.', ',');
  }

}
function focusMoneyField(e) {
  if (e) {
    f = e.target;
  } else {  // IE
    f = window.event.srcElement;
  }
  if (f.value == '-') f.value='';
}

function focusTab(e) {
  if (e) {
    handle = e.target;
  } else {  // IE
    handle = window.event.srcElement;
  }

  // On retrouve la structure dans la page de notre onglet (le container qui le contient)
  tabstrip = handle.parentNode;
  while ((tabstrip != document.body) && (! tabstrip.className.match('container')))
    tabstrip = tabstrip.parentNode

  // Récupère l'id du tabstrip 
  m = tabstrip.id.match(/(.*)_tabstrip/);
  tabStripName = m[1];

  // On cache tous les onglets du container
  divs = tabstrip.getElementsByTagName('div');
  for (i=0 ; i<divs.length ; i++) {
    if (m = divs[i].id.match(/(.*)_handle/)) {
      divs[i].className='tab';
      tohide = document.getElementById( 'tab_'+m[1] );
      if (tohide) 
        tohide.style.display='none';
    }
  }
  // On affiche celui sélectionné
  m = handle.id.match(/(.*)_handle/);
  toshow = document.getElementById( 'tab_'+m[1] );
  if (toshow) 
    toshow.style.display='block';
  handle.className='tabfocus';


  // On mémorise que cet onglet a le focus (pour que sur un reload il soit focussé par défaut)
  document.cookie = 'focusTab['+tabStripName+']='+m[1];
  
}

// Change order for expenses log in budget/index.php
function sortlog(crit) {
  document.cookie = 'sortlog='+crit;
  window.location = 'index.php';
}

// Change order for expenses log in budget/index.php
function sortplein(crit) {
  document.cookie = 'sortplein='+crit;
  window.location = 'index.php';
}

function preloadImg(url) {
  var i = new Image();
  i.src = url;
}

// Found on http://www.editeurjavascript.com/trucs/35,ajax_interrogez_votre_serveur_avec_javascript.php
function HttpGetFile(fichier)
{
  if(window.XMLHttpRequest) // FIREFOX
    xhr_object = new XMLHttpRequest();
  else if(window.ActiveXObject) // IE
    xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  else
    return(false);
  xhr_object.open("GET", fichier, false);
  xhr_object.send(null);
  if(xhr_object.readyState == 4) return(xhr_object.responseText);
  else return(false);
}

// "Signup sikuriti"  ;)
function checkCreaUser(f) {
  ok = true;
  errstr = '';
  // Longueur du login
  if (f.login_wish.value.length < 4) {
    errstr = 'Login must be 4 caracters at least !';
    ok=false;
  } else {
    // Check exists
    a = HttpGetFile('ajax.php?action=login_exists&uid='+f.login_wish.value);
    if (parseInt(a) > 0) {
      errstr = 'This login is invalid !';
      ok=false;
    } 
  }
  // Pass equals
  if (f.password1.value != f.password2.value) {
    errstr = 'Passwords do not match !';
    ok = false;
  } 

  d = document.getElementById('signup_err');
  if (!ok) {
    if (d) d.innerHTML = errstr;
  } else {
    if (d) d.innerHTML = '';
  }

  return ok;
}


function loadXML(url) {
  try //Internet Explorer
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.load(url);
  } catch(e) {
    try //Firefox, Mozilla, Opera, etc.
      {
      xmlDoc=document.implementation.createDocument("","",null);
      xmlDoc.async = false;
      xmlDoc.load(url);
      }
    catch(e) {
      try //Google Chrome
      {
       var xmlhttp = new window.XMLHttpRequest();
       xmlhttp.open("GET",url,false);
       xmlhttp.send(null);
       xmlDoc = xmlhttp.responseXML.documentElement;
      }
      catch(e)
      {
       error=e.message;
      }
      }
    }

  return xmlDoc;
}


// This one sets the active stylesheet, provided that tey are defined as alternate in HTML HEAD
// It stores the change in DB as pref for logued users 
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
   }

// This one sets the active language. Pass the info to ajax.php and reload page

// Asks confirmation before deleting an item. 
function deleteItem(e) {
  if (e) {
    handle = e.target;
  } else {  // IE
    handle = window.event.srcElement;
  }

  m= handle.id.match(/delete_([a-z]+)_([0-9]+)/);
  type = m[1];
  id = m[2];
  if (type=='poi') {
    if (confirm('Do you realy want to delete this point ?\n\nPress OK to confirm')) {
      html=HttpGetFile('/ajax.php?action=deletepoi&id='+id);
      if (html!='OK') alert(html);
      updateMap();
    }
  } else if (type=='track') {
    if (confirm('Do you realy want to delete this track ?\n\nPress OK to confirm')) {
      var res = HttpGetFile('/ajax.php?action=deletetrack&id='+id);
      if (res != 'OK') { alert(res); }
      hideTrack(id);
      updateMap();
    }
  }  else if (type=='vehicule') {
    if (confirm('Do you realy want to delete this vehicule ?\n\nBe warned : this will also delete all associated data (fills, expenses, ...)\n\nPress OK to confirm')) {
      window.location='deletevehicule.php?id='+id;
    }
  } else if (type=='comment') {
    if (confirm('Do you realy want to delete this comment ?\n\nPress OK to confirm')) {
    }
  } 
}

