/**
 * JavaScript librairy
 * To enable cross-browser functionality on HostFlow
 * 
 * Should be compatible with : Internet Explorer (5+), Opera (7.1+), Netscape (7+), Mozilla (1.3+) and Konqueror 3.0+
 * Compatibility with previous versions has not been tested but might work with : Opera 7, Netscape 6 and Mozilla 1.0
 * 
 * Author : Cyril MICHAUD
 * Date : 08/05/2003
 */

var browser = 0;

var NETSCAPE = 1;
var IE = 2;
var KONQUEROR = 3;
var OPERA = 4;


//from here
DOM = (document.getElementById) ? 1 : 0;
NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;
Opera5 = (navigator.userAgent.indexOf("Opera 5") > -1 || navigator.userAgent.indexOf("Opera/5") > -1) ? 1 : 0;
//to here : to be deleted
//only here for compatibility with old templates


/**
 * Identify the browser
 * Return :
 *    - NETSCAPE : for Netscape Navigator and compatibles (ex: Mozilla)
 *    - IE : for Internet Explorer and compatibles (ex: Opera)
 *    - KONQUEROR : for Konqueror browser
 */
function getBrowser() {
    if(browser == 0) {
        var bwr = navigator.appName;
        if(bwr == "Netscape") {
            browser = NETSCAPE;
        }
        else if (bwr == "Microsoft Internet Explorer") {
            browser = IE;
        }
        else if (bwr == "Konqueror") {
            browser = KONQUEROR;
        }
        else if (bwr == "Opera") {
            browser = OPERA;
        }
        else {
            //unknown browser... we suppose it is a JavaScript compliant (not JScript) like Netscape or Mozilla
            browser = NETSCAPE;
        }
    }
    return browser;
}

/**
 * Get the specified layer
 * Parameters :
 *   - layerName : the short name of the layer to show (example : <DIV id="toto">  => layerName="Toto")
 *                 note : a layer must be a <DIV> object to be compatible with both Netscape and IE
 *   - myDocument (optional) : default value = window.document
 *           Allows to specify a document object on another window
 * Returns :
 *   the layer object
 */
function getLayer(layerName, myDocument) {
    var browser = getBrowser();
    if(myDocument == null) myDocument = window.document;

    var object;
    var style;
    switch (browser) {
    case NETSCAPE :
        object = myDocument.getElementById(layerName);
        break;
    case KONQUEROR :
    case IE :
    case OPERA :
        object = eval("myDocument.all."+layerName);
        break;
    }
    return object;
}


/**
 * Show the specified layer
 * Parameters :
 *   - layerName : the short name of the layer to show (example : <DIV id="toto">  => layerName="Toto")
 *                 note : a layer must be a <DIV> object to be compatible with both Netscape and IE
 *   - myDocument (optional) : default value = window.document
 *           Allows to specify a document object on another window
 * Returns :
 *   void
 */
function showLayer(layerName, myDocument)
{
    var browser = getBrowser();
    if(myDocument == null) myDocument = window.document;

    var object;
    var style;
    switch (browser) {
    case NETSCAPE :
        object = myDocument.getElementById(layerName);
        style = object.style;
        break;
    case KONQUEROR :
    case OPERA :
    case IE :
        object = eval("myDocument.all."+layerName);
        style = object.style;
        break;
    }
    if(style.display != '') {
        style.display = '';
    }
}

/**
 * Hide the specified layer on the current html document
 * Parameters :
 *   - layerName : the short name of the layer to show (example : <DIV id="toto">  => layerName="Toto")
 *   - myDocument (optional) : default value = window.document
 *           Allows to specify a document object on another window
 * Returns :
 *   void
 */
function hideLayer(layerName, myDocument)
{
    var browser = getBrowser();
    if(myDocument == null) myDocument = window.document;

    var object;
    var style;
    switch (browser) {
    case NETSCAPE :
        object = myDocument.getElementById(layerName);
        style = object.style;
        break;
    case KONQUEROR :
    case OPERA :
    case IE :
        object = eval("myDocument.all."+layerName);
        style = object.style;
        break;
    }
    if(style.display != 'none') {
        style.display = 'none';
    }
}


/**
 * Get if the layer is currently displayed or not
 * Parameters :
 *   - layerName : the short name of the layer (example : <LAYER id="toto">  => layerName="Toto")
 *   - myDocument (optional) : default value = window.document
 *           Allows to specify a document object on another window
 * Returns :
 *   - true : the layer is displayed
 *   - false : the layer is not displayed
 */
function getLayerDisplayed(layerName, myDocument)
{
    var browser = getBrowser();
    if(myDocument == null) myDocument = window.document;

    var object;
    var style;
    switch (browser) {
    case NETSCAPE :
        object = myDocument.getElementById(layerName);
        style = object.style;
        break;
    case KONQUEROR :
    case OPERA :
    case IE :
        object = eval("myDocument.all."+layerName);
        style = object.style;
        break;
    }
    return style.display == '';
}



/**
 * Resize a layer
 * Parameters :
 *   - layerName : the short name of the layer (example : <LAYER id="toto">  => layerName="Toto")
 *   - width : the new width for the layer
 *   - height : the new height for the layer
 *   - myDocument (optional) : default value = window.document
 *           Allows to specify a document object on another window
 * Returns :
 *   - true : the layer is displayed
 *   - false : the layer is not displayed
 */
function resizeLayer(layerName, width, height, myDocument) {
    var browser = getBrowser();
    if(myDocument == null) myDocument = window.document;

    switch (browser) {
    case NETSCAPE :
        var objLayer = myDocument.getElementById(layerName);
        objLayer.style.width = width;
        objLayer.style.height = height;
        break;
    case KONQUEROR :
    case OPERA :
    case IE :
        var objLayer = eval("myDocument.all."+layerName);
        objLayer.style.width = width;
        objLayer.style.height = height;
        break;
    }
}


/**
 * Get a layer size
 * Parameters :
 *   - layerName : the short name of the layer (example : <LAYER id="toto">  => layerName="Toto")
 *   - myDocument (optional) : default value = window.document
 *           Allows to specify a document object on another window
 * Returns :
 *   an array containing the size :
 *      ['height']
 *      ['width']
 */
function getLayerSize(layerName, myDocument) {
    var browser = getBrowser();
    if(myDocument == null) myDocument = window.document;

    var res = new Array();
    switch (browser) {
    case NETSCAPE :
        var objLayer = myDocument.getElementById(layerName);
        res['width'] = objLayer.style.width;
        res['height'] = objLayer.style.height;
        break;
    case KONQUEROR :
    case OPERA :
    case IE :
        var objLayer = eval("myDocument.all."+layerName);
        res['width'] = objLayer.style.width;
        res['height'] = objLayer.style.height;
        break;
    }
    return res;
}


/**
 * Move a layer
 * Parameters :
 *   - layerName : the short name of the layer (example : <LAYER id="toto">  => layerName="Toto")
 *   - x : the new horizontal position
 *   - y : the new vertical position
 *   - myDocument (optional) : default value = window.document
 *           Allows to specify a document object on another window
 * Returns :
 */
function moveLayer(layerName, x, y, myDocument) {
    var browser = getBrowser();
    if(myDocument == null) myDocument = window.document;

    switch (browser) {
    case NETSCAPE :
        var objLayer = myDocument.getElementById(layerName);
        objLayer.style.top = y;
        objLayer.style.left = x;
        break;
    case KONQUEROR :
    case OPERA :
    case IE :
        var objLayer = eval("myDocument.all."+layerName);
        objLayer.style.pixelTop = y;
        objLayer.style.pixelLeft = x;
        break;
    }
}


/**
 * Get a layer position
 * Parameters :
 *   - layerName : the short name of the layer (example : <LAYER id="toto">  => layerName="Toto")
 *   - myDocument (optional) : default value = window.document
 *           Allows to specify a document object on another window
 * Returns :
 *   an array containing the positions :
 *      ['top'] : the vertical postion
 *      ['left'] : the horizontal position
 */
function getLayerPosition(layerName, myDocument) {
    var browser = getBrowser();
    if(myDocument == null) myDocument = window.document;

    var res = new Array();
    switch (browser) {
    case NETSCAPE :
        var objLayer = myDocument.getElementById(layerName);
        res['top'] = objLayer.style.top;
        res['left'] = objLayer.style.left;
        break;
    case KONQUEROR :
    case OPERA :
    case IE :
        var objLayer = eval("myDocument.all."+layerName);
        res['top'] = objLayer.style.pixelTop;
        res['left'] = objLayer.style.pixelLeft;
        break;
    }
    return res;
}



/**
 * Get a form object.
 * Parameters :
 *   - inputName : the short name of the object to get (example : <INPUT NAME="toto">  => Name="Toto")
 *   - formName (optional) : the name of the form which contains the input object
 *                 If the input object is not in a form, put formName = ''
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 *   - enforce (optional) : SHOULD NOT BE USED.
 *                 When an input object is not inside a form object, the input object must be named with the ID tag
 *                 instead of the NAME tag. If not, the object will not be accessible by Netscape browsers.
 *                 Setting enforce value to TRUE removes the Internet Explorer warning message.
 * Returns :
 *   . the object if found
 */
function getFormObject(inputName, formName, myDocument, enforce)
{
    var browser = getBrowser();
    if(myDocument == null || myDocument == '') myDocument = window.document;
    var object;
    switch(browser) {
    case NETSCAPE :
        if(formName == null || formName == '') {
            object = myDocument.getElementById(inputName);
        }
        else {
            object = eval("myDocument." + formName + "." + inputName);
        }
        if(!object)
            return null;
        break;
    case KONQUEROR :
    case OPERA :
    case IE :
        if(formName == null || formName == '') {
            formName = "all";
        }
        //object = eval("myDocument." + formName + "." + inputName);
        object = eval("myDocument." + formName + "['" + inputName + "']");
        if(!object)
            return null;
        
        if((object.id == null || object.id == "") && formName == 'all' && !enforce)
            window.alert("Warning : your Javascript will only work on Internet Explorer browsers.\nYou have to provide a value for the 'formName' argument.");
        
        break;
    }

    return object;
}



/**
 * Get the value of a form object.
 * Parameters :
 *   - inputName : the short name of the layer (example : <INPUT NAME="toto">  => Name="Toto")
 *   - formName (optional) : the name of the form which contains the input object
 *                 If the input object is not in a form, put formName = ''
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 *   - enforce (optional) : SHOULD NOT BE USED.
 *                 When an input object is not inside a form object, the input object must be named with the ID tag
 *                 instead of the NAME tag. If not, the object will not be accessible by Netscape browsers.
 *                 Setting enforce value to TRUE removes the Internet Explorer warning message.
 * Returns :
 *   . object is unique textfield/textarea : the value of the textfield
 *   . object is unique checkbox or radio : TRUE is checked, FALSE otherwise
 *   . object is multiple radio : the value of the radio checked
 *   . object is unique select-one : the value of the selected option
 *   . object is unique select-multiple : array of the values representing the selected options
 *   . other : unsupported
 */
function getInputValue(inputName, formName, myDocument, enforce)
{
    var object = getFormObject(inputName, formName, myDocument, enforce);
    
    if(object.length != null) {
        var nb = object.length;
        if(object.type == "select-one") {
            var i;
            for(i=0; i<nb; i++) {
                if(object.options[i].selected)
                    return object.options[i].value;
            }
            return null;
        }
        else if(object.type == "select-multiple") {
            var res = new Array();
            var i, j=0;
            for(i=0; i<nb; i++) {
                if(object[i].selected) {
                    res[j++] = object[i].value;
                }
            }
            return res;
        }
        else if(object[0].type == "radio") {
            var i;
            for(i=0; i<nb; i++) {
                if(object[i].checked)
                    return object[i].value;
            }
            return null;
        }
    }
    else if(object.type == "checkbox") {
        return object.checked;
    }
    else if(object.type == "radio") {
        if(object.checked)
            return object.value;
        else
            return null;
    }
    else {
        return object.value;
    }
}




/**
 * Set the value of a form object.
 * Parameters :
 *   - inputName : the short name of the layer (example : <INPUT NAME="toto">  => Name="Toto")
 *   - value : the new value
 *      . object is unique textfield/textarea : the new value of the textfield
 *      . object is unique checkbox or radio : TRUE to check, FALSE to uncheck
 *      . object is multiple radio : the value of the radio to check
 *      . object is unique select-one : the value of the option to select
 *      . object is unique select-multiple : array of values representing the options to select
 *      . other : not supported
 *   - formName (optional) : the name of the form which contains the input object
 *                 If the input object is not in a form, put formName = ''
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 *   - enforce (optional) : SHOULD NOT BE USED.
 *                 When an input object is not inside a form object, the input object must be named with the ID tag
 *                 instead of the NAME tag. If not, the object will not be accessible by Netscape browsers.
 *                 Setting enforce value to TRUE removes the Internet Explorer warning message.
 * Returns :
 */
function setInputValue(inputName, value, formName, myDocument, enforce)
{
    var object = getFormObject(inputName, formName, myDocument, enforce);
    
    if(object.length != null) {
        var nb = object.length;
        if(object.type == "select-one") {
            var i;
            for(i=0; i<nb; i++) {
                if(object[i].value == value) {
                    object.options[i].selected = true;
                }
            }
        }
        else if(object.type == "select-multiple") {
            var i, j;
            for(i=0; i<nb; i++) {
                object[i].selected = false;
                for (j=0; j<value.length; j++) {
                    if(object[i].value == value[j])
                        object[i].selected = true;
                }
            }
        }
        else if(object[0].type == "radio") {
            var i;
            for(i=0; i<nb; i++) {
                object[i].checked = false;
                if(object[i].value == value)
                    object[i].checked = true;
            }
        }

    }
    else if(object.type == "checkbox") {
        object.checked = value;
    }
    else if(object.type == "radio") {
        object.checked = value;
    }
    else {
        object.value = value;
    }
}


/**
 * Set an input object disabled.
 * Parameters :
 *   - inputName : the short name of the layer (example : <INPUT NAME="toto">  => Name="Toto")
 *   - formName (optional) : the name of the form which contains the input object
 *                 If the input object is not in a form, put formName = ''
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 *   - enforce (optional) : SHOULD NOT BE USED.
 *                 When an input object is not inside a form object, the input object must be named with the ID tag
 *                 instead of the NAME tag. If not, the object will not be accessible by Netscape browsers.
 *                 Setting enforce value to TRUE removes the Internet Explorer warning message.
 * Returns :
 */
function disableInput(inputName, formName, myDocument, enforce) {
    var object = getFormObject(inputName, formName, myDocument, enforce);
    object.disable = true;
}

/**
 * Enable an input object.
 * Parameters :
 *   - inputName : the short name of the layer (example : <INPUT NAME="toto">  => Name="Toto")
 *   - formName (optional) : the name of the form which contains the input object
 *                 If the input object is not in a form, put formName = ''
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 *   - enforce (optional) : SHOULD NOT BE USED.
 *                 When an input object is not inside a form object, the input object must be named with the ID tag
 *                 instead of the NAME tag. If not, the object will not be accessible by Netscape browsers.
 *                 Setting enforce value to TRUE removes the Internet Explorer warning message.
 * Returns :
 */
function enableInput(inputName, formName, myDocument, enforce) {
    var object = getFormObject(inputName, formName, myDocument, enforce);
    object.disable = false;
}

/**
 * Get if an input object is disabled or enabled
 * Parameters :
 *   - inputName : the short name of the layer (example : <INPUT NAME="toto">  => Name="Toto")
 *   - formName (optional) : the name of the form which contains the input object
 *                 If the input object is not in a form, put formName = ''
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 *   - enforce (optional) : SHOULD NOT BE USED.
 *                 When an input object is not inside a form object, the input object must be named with the ID tag
 *                 instead of the NAME tag. If not, the object will not be accessible by Netscape browsers.
 *                 Setting enforce value to TRUE removes the Internet Explorer warning message.
 * Returns :
 *    - true : object is enabled
 *    - false : object is disabled
 */
function getInputEnabled(inputName, formName, myDocument, enforce) {
    var object = getFormObject(inputName, formName, myDocument, enforce);
    return !object.disable;
}


/**
 * Submit a form
 * Parameters :
 *   - formName : the name of the form to submit
 *   - action (optional) : to submit the form to a specific URL
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 * Returns : false
 */
function submitForm(formName, action, myDocument) {
    var browser = getBrowser();
    if(myDocument == null || myDocument == '') myDocument = window.document;

    var object;
    switch(browser) {
    case NETSCAPE :
    case KONQUEROR :
    case OPERA :
    case IE :
        object = eval("myDocument." + formName);
        break;
    }

    if (action != null && action != '') {
        object.action = action;
    }
    object.submit();
}



/**
 * Reset a form
 * Parameters :
 *   - formName : the name of the form to reset
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 * Returns :
 */
function resetForm(formName, action, myDocument) {
    var browser = getBrowser();
    if(myDocument == null || myDocument == '') myDocument = window.document;

    var object;
    switch(browser) {
    case KONQUEROR :
    case OPERA :
    case NETSCAPE :
    case IE :
        object = eval("myDocument." + formName);
        break;
    }

    object.reset();
    //return object;
}



/**
 * Simulate a click on the specified button
 * WARNING : the click() function can only be called on a BUTTON or on a LINK
 *           DO NOT TRY to call it on a <input type="image" ...>, it does NOT work on Netscape/Mozilla/Konqueror
 * Parameters :
 *   - buttonName : the short name of the button (example : <BUTTON ID="toto">  => Name="Toto")
 *   - formName (optional) : the name of the form which contains the object
 *                 If the button is not in a form, put formName = ''
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 *   - enforce (optional) : SHOULD NOT BE USED.
 *                 When an input object is not inside a form object, the input object must be named with the ID tag
 *                 instead of the NAME tag. If not, the object will not be accessible by Netscape browsers.
 *                 Setting enforce value to TRUE removes the Internet Explorer warning message.
 * Returns :
 *    the value returned by [button].click()
 */
function clickButton(buttonName, myDocument) {
    var object = getFormObject(buttonName, formName, myDocument, enforce);
    return object.click();
}



/**
 * Get a form
 * Parameters :
 *   - formName : the name of the form which contains the object
 *                If the button is not in a form, put formName = ''
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 * Returns :
 *    the form object
 */
function getForm(formName, myDocument) {
    var browser = getBrowser();
    if(myDocument == null || myDocument == '') myDocument = window.document;

    var object;
    switch(browser) {
    case KONQUEROR :
    case NETSCAPE :
    case OPERA :
    case IE :
        object = eval( "myDocument." + formName);
        break;
    }

    return object;
}

/**
 * Get the action of a form
 * Parameters :
 *   - formName : the name of the form which contains the object
 *                If the button is not in a form, put formName = ''
 *   - myDocument (optional) : default value = window.document
 *                 Allows to specify a document object on another window
 * Returns :
 *    the form object
 */
function getFormAction(formName, myDocument) {
    var object = getForm(formName, myDocument);
    return object.action;
}




function printCurrentWindow() {
    if(window.print) {
        window.print();
    }
    else {
        window.alert("Your browser does not support this functionnality. To print this document, try to click with the right button on the page and select \"Print\".");
    }
}




//*******************************************************************
//*                    MENU FUNCTIONS                               *
//* Some browser specific functions or variable to display the menu *
//*******************************************************************
var thresholdY = 15;		// in pixels; threshold for vertical repositioning of a layer
var ordinata_margin = -10;	// to start the layer a bit above the mouse vertical coordinate
var currentY = -1;

if(document.all) {
   window.onresize = hf_resizeWindow;
}
function hf_resizeWindow() {
   if( document.all && document.body && document.body.clientHeight && document.all('hf_template_data') ) {
      document.all('hf_template_data').style.height = parseInt(document.body.clientHeight) - 138;
   }
   return true;
}

/**
 * Capture the mouse movements
 */
function captureMouseEvents() {
    var browser = getBrowser();
    switch(browser) {
    case NETSCAPE :
       document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE);
       break;
    }
}

/**
 * Used to get menu vertical position
 */
function grabMouse(e) {
   var browser = getBrowser();
   switch(browser) {
   case NETSCAPE :
      currentY = e.pageY;
      break;
   case OPERA :
   case IE :
   case KONQUEROR :
      currentY = event.y + document.body.scrollTop;
      break;
   }
}


/**
 * Move a menu layer to its location
 */
function moveLayerY(menuName, ordinata, obj) 
{
   var browser = getBrowser();
   if (loaded) { // to avoid stupid errors of Microsoft browsers
      if( obj ) {
         if( browser == NETSCAPE) {
            document.getElementById(menuName).style.left = getx(obj);
         }
         else {
            document.all[menuName].style.pixelLeft = getx(obj);
         }
      }
   }
}


function setOnClickFunction(fonction) {
    var browser = getBrowser();
    switch(browser) {
    case NETSCAPE:
    case OPERA :
    case KONQUEROR:
    case IE:
        document.onclick = fonction;
        break;
    }
}



function getElementsToHide() {
    var browser = getBrowser();
    if(browser == NETSCAPE || browser == OPERA)
        return new Array();

    if(browser == IE) {
        return document.all.tags("SELECT");
    }

    if(browser == KONQUEROR) {
        var res = new Array();
        var cpt = 0;
        var formsArray = document.forms;
        for (var i = 0; i < formsArray.length; i++) {
            var elementsArray = formsArray[i].elements;
            for(var j = 0; j < elementsArray.length; j++) {
                var elem = elementsArray[j];
                if(elem.type != 'hidden')
                    res[cpt++] = elem;
            }
        }
        return res;
    }

    return new Array();
}

function getElementsToShow() {
    return getElementsToHide();
}


function getx( obj )
{
  var x = 0;
  elt = document.getElementById(obj);
  while (elt.tagName != "BODY")
  {
      x += elt.offsetLeft;
      elt = elt.parentNode;
  }

  return x;
}

