/**
 * Sets/unsets the pointer in browse mode
 * Usage: onMouseOver="lib_set_pointer(this,...)" onMouseOut="lib_set_pointer(this,...)"
 *
 * @param   object   the table row
 * @param   object   the color to use for this row
 *
 * @return  boolean  whether pointer is set or not
 */
 
 
 
function lib_set_row(theRow, theColor)
  {
  if (theColor == '' || typeof(theRow.style) == 'undefined')
    return false;
  if (typeof(document.getElementsByTagName) != 'undefined')
    var theCells = theRow.getElementsByTagName('td');
  else if (typeof(theRow.cells) != 'undefined')
    var theCells = theRow.cells;
  else
    return false;

  var rowCellsCnt  = theCells.length;
  for (var c = 0; c < rowCellsCnt; c++)
    theCells[c].style.backgroundColor = theColor;

  return true;
  }

/**
 * Compatibility
 */
function setPointer(theRow, thePointerColor)
  {
  return lib_set_row(theRow, theColor);
  }

/**
 * Find an object by ID
 * Usage: lib_find_obj(id)
 *
 * @param   string   the object name
 *
 * @return  object   the object found
 */
function lib_find_obj(n, d)
  {
  var p,i,x;
  if (!d)
    d = document;
  if ((p = n.indexOf("?")) > 0 && parent.frames.length)
    {
    d = parent.frames[n.substring(p+1)].document;
    n = n.substring(0, p);
    }
  if (!(x = d[n]) && d.all)
    x = d.all[n];
  for (i = 0; !x && i < d.forms.length; i++)
    x = d.forms[i][n];
  for (i = 0; !x && d.layers && i < d.layers.length; i++)
    x = lib_find_obj(n, d.layers[i].document);
  if (!x && d.getElementById)
    x = d.getElementById(n);
  return x;
  }

/**
 * Funzione ITERATIVA di tipo BREADTH-FIRST:
 * Tra tutti i figli del container "c", restituisce il "primo" oggetto denominato "n"  (cioè scendendo il meno possibile)
 */
function lib_getElementByName(c,n)
  {
  // inizializziamo l'array di lavoro
  // ricerca sui figli diretti
  if (c.hasChildNodes())
    {
    var i, j, k1, k2;
    var l = Array(c.childNodes);
    while(k1 = l.length)
      {
      var y = Array();
      for(j = 0; j < k1; j++)
        {
        var x = l[j];
        k2 = x.length;
        for(i = 0; i < k2; i++)
          {
          if (x[i].nodeType == 1)
            if (x[i].getAttribute("name") == n)
              return x[i];
            else
              if (x[i].hasChildNodes())
                {
                var z = x[i].childNodes;
                // carichiamo l'array con i figli da esplorare
                y = y.concat(z);
                }
          }
        }
      // buttiamo via i vecchi oggetti e passiamo sul nuovo array
      l = y;
      }
    }
  }

/**
 * Funzione ITERATIVA di tipo BREADTH-FIRST:
 * Tra tutti i figli del container "c", restituisce "tutti" gli oggetti denominati "n"
 */
function lib_getElementsByName(c,n)
  {
  var result = Array();
  // inizializziamo l'array di lavoro
  // ricerca sui figli diretti
  if (c.hasChildNodes())
    {
    var i, j, k1, k2;
    var l = Array(c.childNodes);
    while(k1 = l.length)
      {
      var y = Array();
      for(j = 0; j < k1; j++)
        {
        var x = l[j];
        k2 = x.length;
        for(i = 0; i < k2; i++)
          {
          if (x[i].nodeType == 1)
            if (x[i].getAttribute("name") == n)
              result = result.concat(x[i]);
            else
              if (x[i].hasChildNodes())
                {
                var z = x[i].childNodes;
                // carichiamo l'array con i figli da esplorare
                y = y.concat(z);
                }
          }
        }
      // buttiamo via i vecchi oggetti e passiamo sul nuovo array
      l = y;
      }
    }
  return result;
  }

/**
 * Funzione NON RICORSIVA che ricerca solamente sui figli diretti:
 * Tra tutti i figli del container "c", restituisce il primo di tipo indicato
 */
function lib_getElementByTagName(c,n)
  {
  // ricerca sui figli diretti
  if (c.hasChildNodes())
    {
    var i;
    var l = c.childNodes;
    var k = l.length;
    for(i = 0; i < k; i++)
      if (l[i].nodeName == n)
        return l[i];
    }
  }

/**
 * Opzioni di visualizzazione tra una serie di oggetti
 * Usage: lib_show_hide(id1, option1, id2, option2,...)
 * option = "show" | "hide" | "none"
 */
function lib_show_hide()
  {
  var i,p,v,vd,vv,obj;
  var args = lib_show_hide.arguments;
  for (i = 0; i < (args.length - 1); i += 2)
    if ((obj = lib_find_obj(args[i])) != null)
      {
      v = args[i+1];
      vd = ((v == 'none') ? 'none' : 'inline');
      vv = ((v == 'show') ? 'show' : 'hide');
      if (obj.style)
        {
        obj = obj.style;
        vv = ((vv == 'show') ? 'visible' : ((vv == 'hide') ? 'hidden' : vv));
        }
      obj.display = vd;
      obj.visibility = vv;
      }
  }

/**
 * Aggiornamento di un IFrame
 */
function lib_load_iframe(iframe_name, url)
  {
  var ifr = lib_find_obj(iframe_name);
  if (typeof(ifr.src) != "undefined")
    ifr.src = url;
  else if (typeof(ifr.location) != "undefined")
    ifr.location = url;
  }
function lib_set_iframe(iframe_name, html_text)
  {
  var ifr = document.getElementById(iframe_name);
  var ifrdoc;
  if (ifr.contentWindow)
    ifrdoc = ifr.contentWindow.document; //  MSIE
  else
    ifrdoc = ifr.contentDocument.defaultView.document;  //  Mozilla
  ifrdoc.body.innerHTML = html_text;
  }

/**
 * Aggiustamento dinamico dell'IFRAME
 */
function adjustIFrameSize(iframeWindow)
  {
  var iframeElement = iframeWindow.parent.document.getElementById(iframeWindow.name);
  var iframeDocument = iframeWindow.document;
  if (iframeDocument.documentElement)
    iframeDocument = iframeDocument.documentElement;
  // Cerchiamo di venire incontro al sistema un po' strano di fare i conti dei browser
  // per evitare inutili scroll-bar
  iframeElement.height = iframeDocument.scrollHeight + 'px';
  iframeElement.width = iframeDocument.scrollWidth + 'px';
  }

/**
  * Gestione Forms
  */
/* Seleziona tutti i checkbox della form, il secondo parametro indica se:
 * selezionarli tutti (true), deselezionarli (false), oppure effettuare il toggle (null)
 */
function lib_select_all(thisform, action)
  {
  var n = thisform.elements.length;
  var i;
  for(i = 0; i < n; i++)
    {
    var elem = thisform.elements[i];
    if (elem.type == "checkbox")
      if (action == null)
        elem.checked = !elem.checked;
      else
        elem.checked = action;
    }
  }

/**
* Copy To Clipboard
*/
function lib_copy_to_clipboard(txt)
  {
  if (window.clipboardData)
    {
    window.clipboardData.setData("Text", txt);
    }
  else if (window.netscape)
    {
    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
    var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
    if (!clip)
      return;
    var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
    if (!trans)
      return;
    trans.addDataFlavor('text/unicode');
    var str = new Object();
    var len = new Object();
    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
    var copytext=txt;
    str.data=copytext;
    trans.setTransferData("text/unicode",str,copytext.length*2);
    var clipid=Components.interfaces.nsIClipboard;
    if (!clip)
      return false;
    clip.setData(trans,null,clipid.kGlobalClipboard);
    }
  return false;
  }

// Nota Bene: Inserire questo controllo sia sull'onSubmit della FORM (x IE) che nell'onSubmit del pulsante di submit (x Mozilla)
function lib_check_mail(address, msg_txt)
  {
  if (msg_txt == null)
    msg_txt = "Attenzione, indirizzo di e-mail non corretto.";
  re = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
  if (!re.test(address))
    {
    if (msg_txt != "")
      alert(msg_txt);
    return false;
    }
  else
    return true;
  }

function ondaflex(th)
  {
  // struttura:
  // <table border="0" cellpadding="0" cellspacing="0">
  //   <tr><td colspan="2"></td></tr>
  //   [ <tr><td><img></td><td><a onMouseOver="ondaflex(this)">text</a></td></tr> ] x n volte
  //   <tr><td colspan="2"></td></tr>
  // </table>
  // La prima e l'ultima riga sono utilizzate come BUFFER vuoti per gestire l'effetto "BORDO"
  //
  // compatibilità con la modalità "evento"
  if (!th || !th.nodeName)
    th = this;
  // START SIZE & END SIZE
  var startsize = 12;
  var endsize = 4;
  var s = startsize;

  // Ottimizzazione
  try
    {
    window.screen.updateInterval = 1000;
    }
  catch(e)
    {
    }

  try
    {
    // il tag <tr> corrente
    var trobj = th.parentNode.parentNode;
    // il tag <a>
    var aobj = trobj.lastChild.firstChild;
    // il tag <img>
    var iobj = trobj.firstChild.firstChild;
    // per gestire l'effetto bordo
    var tot = 0;
    aobj.style.fontSize = ''+s+'px';
    iobj.height = (s / 2);

    // Adesso scendiamo
    for(s = startsize, trobj = trobj.nextSibling; trobj; trobj = trobj.nextSibling)
      {
      if (aobj = trobj.lastChild.firstChild)
        aobj.style.fontSize = ''+s+'px';
      else
        {
        //for(tot = 0; s > 2;s--)
        //  tot += s;
        // effetto bordo
        //trobj.lastChild.style.height = ''+(tot / 1)+'px';
        }
      if (iobj = trobj.firstChild.firstChild)
        iobj.height = (s / 2);

      if (s > endsize)
        s--;
      }
    // ripristiniamo il tag centrale
    trobj = th.parentNode.parentNode;
    // Adesso risaliamo la china
    for(s = startsize, trobj = trobj.previousSibling; trobj; trobj = trobj.previousSibling)
      {
      if (aobj = trobj.lastChild.firstChild)
        aobj.style.fontSize = ''+s+'px';
      else
        {
        //for(tot = 0; s > 2;s--)
        //  tot += s;
        // effetto bordo
        //trobj.lastChild.style.height = ''+(tot / 1)+'px';
        }
      if (iobj = trobj.firstChild.firstChild)
        iobj.height = (s / 2);

      if (s > endsize)
        s--;
      }
    }
  catch(e)
    {
    }

  try
    {
    window.screen.updateInterval = 0;
    }
  catch(e)
    {
    }
  }
  
  
// funzione comune per l'implementazione di  TINYMCE
// come parametro bisogna passare una stringa con i nome delle textarea
// d separate da virgola
function load_tinymce(str_ids)
  {
  tinyMCE.init({
    mode : "exact",
    elements : str_ids,
    theme : "advanced",
    width: "90%",
    height: "300px",
    plugins : "table,flash",
    inline_styles : true,
    content_css : "../css/classes.css",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_buttons1 : "removeformat, formatselect, fontselect, fontsizeselect, styleselect, |, justifyleft, justifycenter, justifyright, justifyfull, |, table",
    theme_advanced_buttons2 : "bold, italic, underline, |, forecolor, backcolor, forecolorpicker, backcolorpicker, |, bullist, numlist, |, cut, copy, paste, |, undo, redo, |, hr, link, unlink, image, flash, |, cleanup, code",
    theme_advanced_buttons3 : "",
    convert_urls : false,
    relative_urls : false,
    remove_script_host : false,
    extended_valid_elements : "iframe[src|width|height|name|align|frameborder]"
  });
  }

