// Count the number of words, that is, excluding punctuation
function NOT_USED_countNbRealWords(str) {
  var nb = 0;

  str = str.replace(' +', ' ');
//  str = str.replace(/(^\s*)|(\s*$)/g, ' ');

  var bits = str.split(' ');

  for (var i = 0; i < bits.length; i++) {
    if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", bits[i])) {
      nb++;
    }
  }

  return(nb);
}

// Toggle the display of an element
function toggleElementDisplay(buttonId, textId, textButtonShow, textButtonHide) {
  var text = document.getElementById(textId);
  var button = document.getElementById(buttonId);
  if (text.style.display == 'none') {
    // Display the text
    text.style.display = 'block';
    button.innerHTML = textButtonHide;
  } else {
    // Hide the text
    text.style.display = 'none';
    button.innerHTML = textButtonShow;
  }
}

// Do a click on the adjacent radio or checkbox input element
// so as to be able to have the label clickable
// offering an easier navigation on smartphones
function clickAdjacentInputElement(label) {
  var inputElement = label.parentNode.getElementsByTagName('input')[0];
  inputElement.checked = !(inputElement.checked);
}

function getElementsByClass(nameOfClass) {
  var foundElements = new Array();
  var allHTMLTags = document.getElementsByTagName("*");
  var j = 0;
  for (i = 0; i < allHTMLTags.length; i++) {
    if (allHTMLTags[i].className == nameOfClass) {
      foundElements[j] = allHTMLTags[i];
      j++;
    }
  }
  return(foundElements);
}

function stripTags(str) {
  var re = /(<([^>]+)>)/gi;
  str = str.replace(re, "")
    return(str);
}

// Toggle a table body of rows visible and invisible
// An example usage:
// $strDisplayState = "<a href='javascript:void(null);' onClick=\"toggleTableBodyOfRows('question_$elearningQuestionId', 'button_question_$elearningQuestionId', '$gCommonImagesUrl/$gImageFolded', '$gCommonImagesUrl/$gImageCollapsed');\" style='font-weight:normal; text-decoration:none;'><img border='0' id='button_question_$elearningQuestionId' src='$gCommonImagesUrl/$gImageFolded'/>" . $question . '</a>';
function toggleTableBodyOfRows(tbodyDiv, imageDiv, imageUrlShow, imageUrlHide) {
  var tbody = document.getElementById(tbodyDiv);
  var image = document.getElementById(imageDiv);
  if (tbody.style.display == "") {
    tbody.style.display = "none";
    image.src = imageUrlShow;
  } else {
    tbody.style.display = "";
    image.src = imageUrlHide;
  }
}

// Check if a variable is set with a value
function isSet(variable) {
  return(typeof(variable) != 'undefined');
}

// Place the focus on the first form field of the page
var skipFormFocus = false;
function formFocus() {
  if (isSet(skipFormFocus) && skipFormFocus) {
    return;
  }
  if (document.forms != null && document.forms.length > 0) {
    var field = document.forms[0];
    if (field != null) {
      for (var i = 0; i < field.length; i++) {
        if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea")) {
          if (field.elements[i] && field.elements[i].focus) {
            field.elements[i].focus();
          }
          return(field.elements[i]);
          break;
        }
      }
    }
  }
}

// Timer count down
function updateCountdownTimer(minutes, minutesId, seconds, secondsId, timeOutFn) {
  if (seconds < 10) {
    seconds = '0' + seconds;
  }

  document.getElementById(minutesId).innerHTML = minutes;
  document.getElementById(secondsId).innerHTML = seconds;

  seconds = seconds - 1;

  if (seconds < 0) {
    seconds = 59;
    minutes = minutes - 1;
  }

  if (minutes < 0) {
    timeOutFn();
    exit;
  } else {
    setTimeout("updateCountdownTimer('"+minutes+"', '"+minutesId+"', '"+seconds+"', '"+secondsId+"', "+timeOutFn+")", 1000);
  }
}

// Add an onload function
function addLoadListener(func) { 
  if (window.addEventListener) { 
    window.addEventListener("load", func, false); 
  } else if (document.addEventListener) { 
    document.addEventListener("load", func, false); 
  } else if (window.attachEvent) { 
    window.attachEvent("onload", func); 
  } else if (typeof window.onload != "function") { 
    window.onload = func; 
  } else { 
    var oldonload = window.onload; 
    window.onload = function() { 
      oldonload(); 
      func(); 
    }; 
  } 
}

// Print a browser page
function printPage() {
  if (window.print) {
    setTimeout('window.print();', 200);
  }
}

// Close a browser window
function closeWindow(delay) {
  if (window) {
    setTimeout('window.close();', delay);
  }
}

// Returns a random number between 1 and number
function getRandom(number) {
  var today = new Date();
  var seed = today.getTime();
  seed = (seed*9301+49297) % 233280;
  seed = seed/(233280.0);
  return(Math.ceil(seed * number));
};

// Clear the window status
function clearStatus() {
  window.status=''; return true;
}

// Trim the blank spaces
function trim(str) {
  trimmed =  str.replace(/(^\s*)|(\s*$)/g,''); 

  return(trimmed);
}

// Prevent the IE browser from drawing a border around the Flash objects
function hideFlashBorders(document) {
  flashObjects = document.getElementsByTagName("object");
  for (var i = 0; i < flashObjects.length; i++) {
    flashObjects[i].outerHTML = flashObjects[i].outerHTML;
  }
}

