//
// utils.js for
//
// Made by Leurent Sylvain
// sylvain.leurent@laposte.net
//
// Started on  Wed Mar  3 17:59:18 2010 Leurent Sylvain
// Last update Mon Jan 23 19:39:46 2012 Leurent Sylvain
//

//retourn la valeur selectioner de l'element radio HTML
//name --> name du l'ensemble des radios
function	get_radio_value(name)
{
  radio = document.getElementsByName(name);
  max = radio.length;
  for (i = 0; !radio[i].checked && i < max; ++i) ;
  if (i == max)
    alert('pb');
  else
    return (radio[i].value);
}

//retourne la valeur selectionner de l'element select HTML
//name --> id de l'element
function	get_select_value(name)
{
  index = document.getElementById(name).selectedIndex;
  return (document.getElementById(name)[index].value);
}

//retourn la valeur des autres elelement HTML
//name --> id de l'element
function	get_value(name)
{
  return (document.getElementById(name).value);
}

//This function returns a string with whitespace stripped
//from the beginning and end of str
//   * " " (ASCII 32 (0x20)), an ordinary space.
//   * "\t" (ASCII 9 (0x09)), a tab.
//   * "\n" (ASCII 10 (0x0A)), a new line (line feed).
//   * "\r" (ASCII 13 (0x0D)), a carriage return.
//   * "\0" (ASCII 0 (0x00)), the NUL-byte.
//   * "\x0B" (ASCII 11 (0x0B)), a vertical tab.
//source http://www.webtoolkit.info/javascript-trim.html
function	trim(str)
{
  if (typeof(str) == 'string')
    {
      str = str.replace(new RegExp('&nbsp;', 'g'), ' ');  //opera
      str = str.replace(new RegExp('^[\\s]+', 'g'), '');
      return str.replace(new RegExp('[\\s]+$', 'g'), '');
    }
  return ('');
}

function	isset(data)
{
  if (typeof(data) == 'undefined')
    return (false);
  else
    return (true);
}

function	isfunc(data)
{
  if (typeof(data) == 'function')
    return (true);
  else
    return (false);
}

function        convertToHtml(str)
{
  str = str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  return (str.replace(new RegExp('"', 'g'), '&quot;'));
}

//if one param new copie of obj in a new variable (not the same reference)
//if tow param copie obj in newObj (without changing the reference of those object)
function        clone(obj, newObj)
{
  if (!isset(newObj))
    newObj = new Object();
  for (i in obj)
    {
      if (typeof obj[i] == 'object')
        newObj[i] = clone(obj[i]);
      else if (typeof obj[i] == 'array')
        newObj[i] = obj[i].slice();
      else
        newObj[i] = obj[i];
    }
  return (newObj);
}

//remove all the data in obj WITHOUT
//changing the reference to the obj
function        remove(obj)
{
  for (i in obj)
    delete i;
}

function        get_class(name)
{
  return (this[name]);
}

