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

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

// Is Internet Explorer ?
var MSIE=(document.all)?true:false;
//alert (DOM + " " + MSIE);
// 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;
}


// -------------------------------------------------------------------
// Ajax load utility - requires AJAX.JS
// -------------------------------------------------------------------

// Loads the given HTML part file in the DIV which id is given
// @param srcURI URI to the HTML file
// @param srcForm source form to post.
// @param elementId element id to find
// @return the element, false if not found
function ajax_load(srcURI, srcForm, elementId) {

  // Sanity checks
  if(!srcURI) {
     alert('HTML file name not supplied');
     return;
  }

  if(!elementId) {
     alert('Target DIV id not supplied');
     return;
  }

  // we create a return function to handle the response text
  var ajaxReturnFunction = function(responseText) {
      // we get the target popup-up DIV target
      var targetElement = elementFinder(elementId);

      if(targetElement) {
		targetElement.innerHTML = responseText;
      }
      else {
        alert('target not found !');
      }
  }

  // AJAX get on the URI with our return function
  if(!srcForm){
	ajax_GET(srcURI, null, ajaxReturnFunction, lang);
  }else{
	ajax_POST(srcURI, srcForm, ajaxReturnFunction, lang);
  }
}

// -------------------------------------------------------------------
// Ajax load utility - requires AJAX.JS
// -------------------------------------------------------------------

// Loads the given HTML part file in the DIV which id is given
// @param srcURI URI to the HTML file
// @param httpVars params for the GET request.
// @param elementId element id to find
// @return the element, false if not found
function ajax_load_GET(srcURI, httpVars, elementId) {

  // Sanity checks
  if(!srcURI) {
     alert('HTML file name not supplied');
     return;
  }

  if(!elementId) {
     alert('Target DIV id not supplied');
     return;
  }

  // we create a return function to handle the response text
  var ajaxReturnFunction = function(responseText) {
      // we get the target popup-up DIV target
      var targetElement = elementFinder(elementId);

      if(targetElement) {
		targetElement.innerHTML = responseText;
      }
      else {
        alert('target not found !');
      }
  }

	ajax_GET(srcURI, httpVars, ajaxReturnFunction, lang);
  
}
// -------------------------------------------------------------------
// 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]();
	}
}


// -------------------------------------------------------------------
// SAFE BODY ON RESIZE
// -------------------------------------------------------------------

// Body Onresize utility (supports multiple Onresize functions)
var gSafeOnresize = new Array();

// Safe Onresize() add...
// @param f function to add to the list
function safeAddOnresize(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.onresize
        window.onresize = safeOnresize;
        gSafeOnresize[gSafeOnresize.length] = f;
    } else {

        if(!window.onresize) {
           window.onresize = safeOnresize;
        }
        else if (window.onresize != safeOnresize) {
            gSafeOnresize[0] = window.onresize;
            window.onresize = safeOnresize;
        }

        gSafeOnresize[gSafeOnresize.length] = f;
    }
}

// Method to put in your <body> onLoad method. This way the method
// is called to execute all the Onresize functions.
function safeOnresize(){
	for (var i=0;i<gSafeOnresize.length;i++)
		gSafeOnresize[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);
     
     // 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 a mouse cursor
// @param elementId element identifier
// @param event event
function showLayerNearMouse(elementId, event) {
  showLayerAtPosition(elementId, findMouseLeftPos(event), findMouseTopPos(event));
}

// SHOW a hidden LAYER near a mouse cursor with an offset
// @param elementId element identifier
// @param event event
// @param offsetLeft offset x to add to the nearElement left position
// @param offsetTop offset y to add to the nearElement top position
function showLayerNearMouseWithOffset(elementId, event, offsetLeft, offsetTop) {
  showLayerAtPosition(elementId, findMouseLeftPos(event)+offsetLeft, findMouseTopPos(event)+offsetTop);
}

// Move a LAYER near a mouse cursor
// @param elementId element identifier
// @param event event
function moveLayerNearMouse(elementId, event) {
  moveLayerAtPosition(elementId, findMouseLeftPos(event), findMouseTopPos(event));
}
// Move a LAYER near a mouse cursor with an offset
// @param elementId element identifier
// @param event event
// @param offsetLeft offset x to add to the nearElement left position
// @param offsetTop offset y to add to the nearElement top position
function moveLayerNearMouseWithOffset(elementId, event, offsetLeft, offsetTop) {
  moveLayerAtPosition(elementId, findMouseLeftPos(event)+offsetLeft, findMouseTopPos(event)+offsetTop);
}

// 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 + "px";
      element.style.top = topPos + "px";
      element.style.visibility = 'visible';
      element.style.display = 'block';
      element.style.overflow = 'hidden';
   }
}


// MOVE a 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 moveLayerAtPosition(elementId, leftPos, topPos){

   var element = elementFinder(elementId);

   if(element) {
      element.style.left = leftPos + "px";
      element.style.top = topPos + "px";
   }
}


// 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;
}

// FINDS the width of an HTML object
// @param obj HTML object
function findWidth(obj)
{
	if (obj.offsetParent) {
			return obj.offsetWidth;
	}
	else if (obj.clientWidth) {
		return obj.clientWidth;
    }

	return 0;
}

// FINDS the height of an HTML object
// @param obj HTML object
function findHeight(obj)
{
	if (obj.offsetParent) {
		return obj.offsetHeight;
	}
	else if (obj.clientHeight) {
		return obj.clientHeight;
    }

	return 0;
}


// FINDS the left position of a mouse cursor when an event is caught
// @param event event
function findMouseLeftPos(e)
{
	if (!e) var e = window.event;
	
	if (MSIE) { 
        return(e.clientX + document.body.scrollLeft);
    } 
    else if (DOM) {
        return(e.pageX);
    }
    else
        return false;
}

// FINDS the top position of a mouse cursor when an event is caught
// @param event event
function findMouseTopPos(e)
{
	if (!e) var e = window.event;
	
	if (MSIE) { 
        return(e.clientY + document.body.scrollTop);
    } 
    else if (DOM) {
        return(e.pageY);
    }
    else
        return false;
}



// -------------------------------------------------------------------
// 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;


		
// Loads the given HTML part file in the DIV which id is given
// @param srcURI URI to the HTML file
// @param srcForm source form to post.
// @param elementId element id to find
// @return the element, false if not found
function ajax_loadWithWait(srcURI, srcForm, elementId) {

  var targetElement = elementFinder(elementId);
  if(targetElement) {
	if(lang=='FR') {
	   targetElement.innerHTML = "<div class=\"waitText\">Chargement en cours <img src=\"/images/wait-dots.gif\" border=\"0\"/></div>";
	}
	else {
	   targetElement.innerHTML = "<div class=\"waitText\">Loading <img src=\"/images/wait-dots.gif\" border=\"0\"/></div>";
	}
  }
  
  ajax_load(srcURI, srcForm, elementId);
}

// Loads the given HTML part file in the DIV which id is given
// @param srcURI URI to the HTML file
// @param httpVars params for the get request.
// @param elementId element id to find
// @return the element, false if not found
function ajax_loadWithWait_GET(srcURI, httpVars, elementId) {

  var targetElement = elementFinder(elementId);
  if(targetElement) {
	if(lang=='FR') {
	   targetElement.innerHTML = "<div class=\"waitText\">Chargement en cours <img src=\"/images/wait-dots.gif\" border=\"0\"/></div>";
	}
	else {
	   targetElement.innerHTML = "<div class=\"waitText\">Loading <img src=\"/images/wait-dots.gif\" border=\"0\"/></div>";
	}
  }
  
  ajax_load_GET(srcURI, httpVars, elementId);
}

