
// -------------------------------------------------------------------
// ELEMENT FINDER
// -------------------------------------------------------------------

// Is DOM compatible ?
var DOM=(document.getElementById)?true:false;

// Is Internet Explorer ?
var MSIE=(document.all)?true:false;

// Element Finder
// @param elementId element id to find
// @return the element, false if not found
function elementFinder(elementId) {
    if (DOM) { 
        return(document.getElementById(elementId));
    } 
    else if (MSIE) {
        return(document.all[elementId]);
    }
    else {
        return false;
    }
}

// -------------------------------------------------------------------
// Style changer
// -------------------------------------------------------------------

/** Changes the style name of the element given in parameter
 * @param elementId element id
 * @param newClassName new CSS class name
 */
function changeStyle(elementId, newClassName) {
   var myElement = elementFinder(elementId);
   
   if(myElement) {
      myElement.className = newClassName;
   }
   
}

// -------------------------------------------------------------------
// SAFE BODY ON LOAD
// -------------------------------------------------------------------

// Body OnLoad utility (supports multiple OnLoad functions)
var gSafeOnLoad = new Array();

// Safe OnLoad() add...
// @param f function to add to the list
function safeAddOnLoad(f) {
   isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
   IEmac = ((document.all)&&(isMac)) ? true : false;
   IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;

    if (IEmac && IE4) { // IE 4.5 blows out on testing window.onload
        window.onload = safeOnLoad;
        gSafeOnLoad[gSafeOnLoad.length] = f;
    } else {

        if(!window.onload) {
           window.onload = safeOnLoad;
        }
        else if (window.onload != safeOnLoad) {
            gSafeOnLoad[0] = window.onload;
            window.onload = safeOnLoad;
        }

        gSafeOnLoad[gSafeOnLoad.length] = f;
    }
}

// Method to put in your <body> onLoad method. This way the method
// is called to execute all the OnLoad functions.
function safeOnLoad(){
  for (var i=0;i<gSafeOnLoad.length;i++) {
    gSafeOnLoad[i]();
  }
}


// -------------------------------------------------------------------
// DYNAMIC SCRIPTING UTILITIES
// -------------------------------------------------------------------

// Gets the <scripts> part of an HTML text and evaluates them.
// This is especially useful for HTML blocks returned via Ajax as their
// scripts are not run. Note that we expect simple scripting. No function
// definition is allowed. The script will end if script tags are badly formatted.
// @param text HTML text to parse for <script> tags.
function executeScriptFromHTML(htmlText){

  var curIndex = 0;
  
  while( (curIndex=htmlText.indexOf('<script', curIndex))>=0 ) {
     var scriptStart = htmlText.indexOf('>', curIndex+7)+1;
     
     if(scriptStart==0) {
        return; // invalid script
     }

     var scriptEnd = htmlText.indexOf('</script', curIndex+1);

     if(scriptEnd==-1) {
        return; // invalid script
     }
     
     var textToEval = htmlText.substring(scriptStart,scriptEnd);
     
     if(textToEval.indexOf('<')>=0 || textToEval.indexOf('>')>=0) {
        return; // text contains invalid chars
     }

     // EVAL
     eval(textToEval);

     curIndex = scriptEnd +8;
  }
}


// -------------------------------------------------------------------
// LAYER UTILITIES
// -------------------------------------------------------------------

// HIDE LAYER
// @param elementId element identifier
function hideLayer(elementId){

   var element = elementFinder(elementId);

   if(element) {
      element.style.visibility = 'hidden';
      element.style.display = 'none';
      element.style.overflow = 'hidden';
   }
}

// SHOW a hidden LAYER
// @param elementId element identifier
function showLayer(elementId){

   var element = elementFinder(elementId);

   if(element) {
      element.style.visibility = 'visible';
      element.style.display = 'block';
      element.style.overflow = 'hidden';
   }
}

// SHOW a hidden LAYER near another element
// @param elementId element identifier
// @param nearElement HTML element where we want to display this layer
function showLayerNearElement(elementId, nearElement) {
  showLayerAtPosition(elementId, findLeftPos(nearElement), findTopPos(nearElement));
}

// SHOW a hidden LAYER near another element with an offset
// @param elementId element identifier
// @param nearElement HTML element where we want to display this layer
// @param offsetLeft offset x to add to the nearElement left position
// @param offsetTop offset y to add to the nearElement top position
function showLayerWithOffset(elementId, nearElement, offsetLeft, offsetTop) {
  showLayerAtPosition(elementId, findLeftPos(nearElement)+offsetLeft, findTopPos(nearElement)+offsetTop);
}

// SHOW a hidden LAYER at a specified position
// @param elementId element identifier
// @param leftPos left position to set for the layer
// @param topPos top position to set for the layer
function showLayerAtPosition(elementId, leftPos, topPos) {

   var element = elementFinder(elementId);

   if(element) {
      element.style.left = leftPos;
      element.style.top = topPos;
      element.style.visibility = 'visible';
      element.style.display = 'block';
      element.style.overflow = 'hidden';
   }
}

// FINDS the left position of an HTML object
// @param obj HTML object
function findLeftPos(obj) {

  var curleft = 0;

  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curleft += obj.offsetLeft
      obj = obj.offsetParent;
    }
  }
  else if (obj.x) {
    curleft += obj.x;
  }

  return curleft;
}

// FINDS the top position of an HTML object
// @param obj HTML object
function findTopPos(obj) {

  var curtop = 0;

  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y) {
    curtop += obj.y;
  }

  return curtop;
}

// -------------------------------------------------------------------
// PRINTING UTILITIES
// -------------------------------------------------------------------

// Remove a specific section of the page to print.
// The section is identified by an id set to 'nonPrintable'
// This method is called by printing EVENTS.
function removeNonPrintableElements(){

  var nonPrintableElement = elementFinder('nonPrintable');

  if(nonPrintableElement) {
     nonPrintableElement.style.display='none';
  }
} 

// Adds a specific section of the page to print.
// The section is identified by an id set to 'nonPrintable'
// This method is called by printing EVENTS.
function addNonPrintableElements(){

  var nonPrintableElement = elementFinder('nonPrintable');

  if(nonPrintableElement) {
     nonPrintableElement.style.display='inline';
  }
}

// Displays the printing popup
function printWindow() {
   window.print();
}

// We register our both method to remove printing elements that are non printable
window.onbeforeprint=removeNonPrintableElements;
window.onafterprint=addNonPrintableElements;