/*

  General scripts to animate and manage the pages

*/

// Says if the current web browser is Internet Explorer
var isIE = (document.attachEvent ? true : false);

// Shortcut to get an element from the page
function $(id) {
  return document.getElementById(id);
}

// Changes the style of a menu item when mouse enters it
function menu_over(item) {
  item.className = 'menu_over';
}

// Changes back the style of a menu item when the mouse leaves it
function menu_out(item) {
  item.className = 'menu_item';
}

// Launches every action that has to be taken upon page load
function body_load() {
  select_menuItem();
  /*
  if (!isIE) {
    // traitement des menus
    $('home').style.fontSize = 'small';
    $('about').style.fontSize = 'small';
    $('services').style.fontSize = 'small';
    $('contact').style.fontSize = 'small';
    $('feedback').style.fontSize = 'small';
    // traitement du texte principal
    $('column_left').style.fontSize = 'small';
    if ($('column_right') != undefined) {
      $('column_right').style.fontSize = 'small';
    }
    // traitement du formulaire d'envoi d'email
    if ($('pageName').innerHTML == 'contact' || $('pageName').innerHTML == 'feedback') {
      $('submit').style.fontSize = 'small';
      $('reset').style.fontSize = 'small';
      var fields = document.getElementsByTagName('input');
      for (i = 0; i < fields.length; i++) {
        if (fields[i].className == 'formField')
          fields[i].style.fontSize = 'small';
      }
      fields = document.getElementsByTagName('textarea');
      for (i = 0; i < fields.length; i++) {
        if (fields[i].className == 'formField')
          fields[i].style.fontSize = 'small';
      }
      var labels = $('tableForm').getElementsByTagName('td');
      for (i = 0; i < labels.length; i++) {
        if (labels[i].className == 'formLabel')
          labels[i].style.fontSize = 'small';
      }
    }
  }
*/
}

// Sets the style for the menu item corresponding to current page displayed
function select_menuItem() {
  var currentPage = $('pageName').innerHTML;
  if ($(currentPage) != undefined)
  {
    $(currentPage).className = 'menu_selected';
    if (isIE) {
      $(currentPage).onclick = '';
      $(currentPage).onmouseover = '';
      $(currentPage).onmouseout = '';
    } else {
      $(currentPage).setAttribute('onclick', '');
      $(currentPage).setAttribute('onmouseover', '');
      $(currentPage).setAttribute('onmouseout', '');
    }
  }
}

// Sends to another location
function goto(elt) {
  var id = elt.getAttribute('id');
  document.location.replace(id);
}

/* Removes borders on images */
function clean_borders() {
  var arrImg = document.getElementsByTagName('IMG');
  for (i = 0; i < arrImg.length; i++) {
    arrImg[i].style.border = '0';
  }
}

/* Finds the coordonninates of an element on screen */
function findPos(obj) {
  var curleft = 0;
  var curtop = 0;
  if(obj.x) {
    curleft += obj.x;
    curtop += obj.y;
  }
  else if(obj.offsetParent) {
    while(1)
    {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
  }
	return [curleft, curtop];
}

/* Checks if a form's mandatory fields are filled */
function checkform() {
  var msg1 = '';
  var msg2 = '';
  for (i = 0; i < lstFields.length; i++) {
    if ($(lstFields[i]).value == '') {
      msg1 += '    - ' + lstFieldsNames[i] + '\n';
    }
  }
  if (msg1 != '') {
    msg1 = '> the following mandatory fields where not filled:\n' + msg1;
  }
  if ($('email').value != '') {
//    var reg = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b", "i");
    var reg = new RegExp("^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$", "i");
    var test = reg.exec($('email').value);
    if (test == null) {
      msg2 += "> the email address was not properly formatted\n"
    }
  }
  if (msg1 != '' || msg2 != '') {
    alert('The form could not be sent because:\n' + msg1 + msg2);
    return false;
  } else {
    return true;
  }
}

var imgIdx = -1;
// fonction pour changer l'image d'une banniere a intervalle regulier
function rotate_banner()
{
  if (imgIdx >= 0 && banners.length > 1)
  {
    elt = banners[imgIdx];
		elt.style.display = 'none';
  }
  imgIdx++;
  if (imgIdx == banners.length) imgIdx = 0;
  elt = banners[imgIdx];
  elt.style.display = 'block';
}

// fonction pour retrouver les elements d'une certaine classe
document.getElementsByClassName = function(cl)
{
  var retnode = [];
  var myclass = new RegExp('\\b'+cl+'\\b');
  var elem = this.getElementsByTagName('*');
  for (var i = 0; i < elem.length; i++)
  {
    var classes = elem[i].className;
    if (myclass.test(classes)) retnode.push(elem[i]);
  }
  return retnode;
}

