//* FILE **********************************************************************
//*                                                                          **
//* NAME: htmlToolBox.js                                                     **
//*                                                                          **
//* GOAL: Fonctions JS utiles à la gestion des formulaires                   **
//*                                                                          **
//*****************************************************************************
//*                                                                          **
//* MODIFICATIONS:                                                           **
//*                                                                          **
//* Version  Date         Auteur  Comment                                    **
//*                                                                          **
//* 1.0      15 Oct 2005  MV      Creation de GetInputValue, IsInputEmpty    **
//*                               setInputFocus                              **
//* 1.1      25 Oct 2005  MV      setInputValue                              **
//* 1.2      14 Nov 2005  MV      GetForm, GetInputRef                       **
//* 1.3      19 Dec 2005  MV      IsInputSetEmpty                            **
//* 1.4      09 Fev 2006  MV      Ajout de TrimExtraSpaces                   **
//* 1.5      22 Fev 2006  MV      Ajout de setAllInputsValue                 **
//*                                                                          **
//* FILE **********************************************************************

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: GetForm                                                            **
//* GOAL: Renvoie l'objet Forumalire identifié par son nom                   **
//*                                                                          **
//***************************************************************** FUNCTION **
function GetForm(FormName)
{
  var theForm;
  
  if (FormName == undefined || FormName == '')
  {
    alert('GetForm: Param FormName indéfini.');
  }
  else
  {
    theForm = document.forms[FormName];
    if (theForm == undefined)
    {
      alert('GetForm: Nom de Formulaire invalide :'+ FormName);
    }
  }

  return theForm;
  
} // GetForm

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: GetInputRef                                                        **
//* GOAL: Renvoie la référence du champ de formulaire spécifié               **
//*                                                                          **
//***************************************************************** FUNCTION **
function GetInputRef(theForm, fieldName, noMsg)
{
  if (theForm == undefined || theForm.elements == undefined)
  {
    alert('htmlToolBox: GetInputValue: theForm est invalide');
    return false;
  }

  var input = theForm.elements[fieldName];
  if (input == undefined)
  {
    if (!noMsg)
      alert('htmlToolBox: GetInputValue: Le champ '+ fieldName + ' du formulaire '+ theForm.name + ' est invalide.');
    return false;
  }
  
  return input;
  
} // GetInputRef

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: ExisteInput                                                        **
//* GOAL: Renvoie true si l'input Existe dans le formulaire spécifié         **
//*                                                                          **
//***************************************************************** FUNCTION **
function ExisteInput(theForm, fieldName)
{
  if (theForm == undefined || theForm.elements == undefined)
  {
    alert('htmlToolBox: GetInputValue: theForm est invalide');
    return false;
  }

  var input = theForm.elements[fieldName];
  if (input == undefined)
  {
    return false;
  }

  return true;

} // ExisteInput



//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: GetInputValue                                                      **
//* GOAL: Renvoie la valeur du champ de formulaire spécifié                  **
//*                                                                          **
//***************************************************************** FUNCTION **
function GetInputValue(theForm, fieldName)
{
  if (theForm == undefined || theForm.elements == undefined)
  {
    alert('htmlToolBox: GetInputValue: theForm est invalide: ('+theForm.name+')');
    return false;
  }

  var input = GetInputRef(theForm, fieldName);
  if (input == undefined)
  {
    alert('htmlToolBox: GetInputValue: Le champ '+ fieldName + ' du formulaire '+ theForm.name + ' est invalide.');
    return false;
  }
  
  if (input.type == 'select-one')
  {
    return input.options[input.selectedIndex].value;
  }
  
  if (input.length != undefined)
  {
    // Radio Input array: return the first selection value
    for (var i=0; i<input.length; i++)
    {
      if (input[i].checked)
      {
        return input[i].value;
      }
    }
    return '';
  }
  else
  {
    if (input.type == 'checkbox')
    {
      if (input.checked)
      {
        if (input.value)
          return input.value;
        else
          return true;
      }
      else
      {
        return false;
      }
    }
    else
    {
      return input.value;
    }
  } 
} // GetInputValue

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: GetSelectText                                                      **
//* GOAL: Renvoie le texte sélectionné du select donné                       **
//*                                                                          **
//***************************************************************** FUNCTION **
function GetSelectText(theSelect)
{
  if (theSelect == undefined)
  {
    alert('htmlToolBox: GetSelectText, champ select indéfini');
    return false;
  }

  if (theSelect.type == 'select-one')
  {
    return theSelect.options[theSelect.selectedIndex].text;
  }
  else
  {
    alert('htmlToolBox: GetSelectText, champ select invalide');
    return false;
  }
} // GetSelectText

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: IsInputEmpty                                                       **
//* GOAL: Renvoie true si le champ specifié n'est pas renseigné              **
//*                                                                          **
//***************************************************************** FUNCTION **
function IsInputEmpty(theForm, fieldName)
{
  var input = GetInputRef(theForm, fieldName);
  
  if (input.type == 'select-one')
  {
    if (input.options.length == 1)
      return false;
    else
      return input.selectedIndex == 0;
  }
  
  if (input.length != undefined)
  {
    return false;
  }
  
  if (input.type == 'checkbox')
  {
    return !input.checked;
  }
  
  return input.value == '';
  
} // IsInputEmpty

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: IsInputSetEmpty                                                    **
//* GOAL: true si tout les Champs ayant le préfixe spécifié sont vides       **
//*                                                                          **
//***************************************************************** FUNCTION **
function IsInputSetEmpty(theForm, fieldNamePrefixe)
{
  var i;
  var nbElem = theForm.elements.length;
  for (i=0; i<nbElem; i++)
  {
    var iname = theForm.elements[i].name;
    if (fIsPrefixe(fieldNamePrefixe, iname))
    {
      if (! IsInputEmpty(theForm, iname))
        return false;
    }
  }
  return true;

} // IsInputSetEmpty

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: _setInputValue                                                     **
//* GOAL: Set la valeur de l'input donné                                     **
//*       Donner true/false pour les Checkbox                                **
//*                                                                          **
//***************************************************************** FUNCTION **
function _setInputValue(input, val)
{
  if (input.type == 'select-one')
  {
    if (input.options.length > 1)
    {
      var nbElem = input.options.length;
      for (index=0; index < nbElem; index++)
      {
        if (input.options[index].value == val)
        {
          input.options[index].selected = true;
          return;
        }
      }
    }
    return;
  }

  if (input.length != undefined)
  {
    alert('htmlToolBox: setInputValue('+ input.name +', '+ val +'): cette méthode ne marche pas encore ...');
    return;
  }
  
  if (input.type == 'radio')
  {
    input.checked = (input.value == val);
    return;
  }

  if (input.type == 'checkbox')
  {
    input.checked = val;
    return;
  }

  input.value = val;
  return;

} // _setInputValue

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: setInputValue                                                      **
//* GOAL: Set la valeur de l'input spécifié par son nom                      **
//*       Donner true/false pour les Checkbox                                **
//*                                                                          **
//***************************************************************** FUNCTION **
function setInputValue(theForm, fieldName, val)
{
  var input = GetInputRef(theForm, fieldName);
  return _setInputValue(input, val);

} // setInputValue


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: setInputFocus                                                      **
//* GOAL: Focus sur l'input spécifié                                         **
//*                                                                          **
//***************************************************************** FUNCTION **
function setInputFocus(theForm, fieldName)
{
  var input = GetInputRef(theForm, fieldName, true);
  if (input)
    input.focus();
  
} // setInputFocus

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: setInputDisabled                                                   **
//* GOAL: Disabled ou Enable l'input spécifié                                **
//*                                                                          **
//***************************************************************** FUNCTION **
function setInputDisabled(theForm, fieldName, isDisabled)
{
  var input = GetInputRef(theForm, fieldName);
  input.disabled = isDisabled;

} // setInputValue


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: setAllInputsValue                                                  **
//* GOAL: Assigne la valeur donnée a tout les inputs enabled dont le nom     **
//*       correspond à l'exp régulière donné                                 **
//*       Renvoie le nombre de champs mis à jour                             **
//*       Exemples de Regexp pour fieldNamePrefixe:                          **
//*       /^tetiere_selection_/        =>  commencant par tetiere_selection_ **
//*       /^tetiere_selection_.+_FAVOR$/   => like tetiere_selection_*_FAVOR **
//*       Si val = 'INVERSION', alors inverse la sélection de la checkbox    **
//*                                                                          **
//***************************************************************** FUNCTION **
function setAllInputsValue(theForm, fieldNameRegexp, val)
{
  var i;
  var found = 0;
  var arrayInputs = theForm.elements;
  var nbElem = arrayInputs.length;
  
  for (i=0; i<nbElem; i++)
  {
    var elt = arrayInputs[i];
    if (!elt.disabled)
    {
      var iname = elt.name;
      if (fieldNameRegexp.test(iname))
      {
        found++;
				if (val == 'INVERSION')
				{
          if (elt.type == 'checkbox')
				  {
				    elt.checked = !elt.checked;
				  }
				}
				else
				{
        	_setInputValue(elt, val);
				}
      }
    }
  }
  return found;

} // setAllInputsValue

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: setMajuscule                                                       **
//* GOAL: Met en majuscule le champ texte donné                              **
//*                                                                          **
//***************************************************************** FUNCTION **
function setMajuscule(field)
{
  var str = field.value;
  field.value = str.toUpperCase();
} // setMajuscule

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: setMinuscule                                                       **
//* GOAL: Met en minuscule le champ texte donné                              **
//*                                                                          **
//***************************************************************** FUNCTION **
function setMinuscule(field)
{
  var str = field.value;
  field.value = str.toLowerCase();
} // setMinuscule

//*****************************
// OLD FUNCTIONS DECLARATIONS *
//*****************************

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fGetInputTextValue                                                 **
//* GOAL: Return value of specified Input Text                               **
//*       If optional piFrameName isn't specified, use contentFrameName      **
//*                                                                          **
//***************************************************************** FUNCTION **
function fGetInputTextValue(piInputName, piFrameName)
{
  var input = top.frames[piFrameName?piFrameName:contentFrameName].document.forms[0].elements[piInputName];
  if (input)
    return input.value;
  else
    alert('PCS Javascript Error: fGetInputTextValue: '+ piInputName + ' element is undefined in the Form.');
} // fGetInputTextValue

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: selectIndex                                                        **
//* GOAL: return index of selected option of given selection list,           **
//*                                                                          **
//***************************************************************** FUNCTION **
function selectIndex(piSelectName, piFrameName)
{
  var sel;
  if (piFrameName)
    sel = top.frames[piFrameName].document.forms[0].elements[piSelectName];
  else
  {
    if (top.frames.length)
      sel = top.frames[top.contentFrameName].document.forms[0].elements[piSelectName];
    else
      sel = document.forms[0].elements[piSelectName];
  }

  return sel.selectedIndex;

} // selectIndex

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: selectValue                                                        **
//* GOAL: return value of selected option of given selection list,           **
//*                                                                          **
//***************************************************************** FUNCTION **
function selectValue(piSelectName, piFrameName)
{
  var sel;
  if (piFrameName)
    sel = top.frames[piFrameName].document.forms[0].elements[piSelectName];
  else
  {
    if (top.frames.length)
      sel = top.frames[top.contentFrameName].document.forms[0].elements[piSelectName];
    else
      sel = document.forms[0].elements[piSelectName];
  }

  return sel.options[sel.selectedIndex].value;
} // selectValue

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: selectText                                                         **
//* GOAL: return Text of selected option of given selection list,            **
//*                                                                          **
//***************************************************************** FUNCTION **
function selectText(piSelectName, piFrameName)
{
  var sel;

  if (piFrameName)
    sel = top.frames[piFrameName].document.forms[0].elements[piSelectName];
  else
  {
    if (top.frames.length)
      sel = top.frames[top.contentFrameName].document.forms[0].elements[piSelectName];
    else
      sel = document.forms[0].elements[piSelectName];
  }
  
  return sel.options[sel.selectedIndex].text;
} // selectText

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fSelectOptionFromText                                              **
//* GOAL: select corresponding item  in the list from the label              **
//*                                                                          **
//***************************************************************** FUNCTION **
function fSelectOptionFromText(piSelectName, piFrameName, piText)
{
  var index;
  
  var select = top.frames[piFrameName].document.forms[0].elements[piSelectName];

  if (select)
  {
    for (index=0; index < select.options.length; index++)
    {

      if (select.options[index].text == piText)
      {
        select.options[index].selected = true;
        return true;
      }
    }
    
  }

  fDebug('unable to find ' + piSelectName + ' in frame ' + piFrameName + '!');
  return false;

} // fSelectOptionFromText

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fSelectOptionFromValue                                             **
//* GOAL: select corresponding item  in the list from the value              **
//*                                                                          **
//***************************************************************** FUNCTION **
function fSelectOptionFromValue(piSelectName, piFrameName, piValue)
{
  var index;
  
  var select = top.frames[piFrameName].document.forms[0].elements[piSelectName];

  if (select)
  {
    for (index=0; index < select.options.length; index++)
    {

      if (select.options[index].value == piValue)
      {
        select.options[index].selected = true;
        return true;
      }
    }
    
  }

  fDebug('unable to find ' + piSelectName + ' in frame ' + piFrameName + '!');
  return false;

} // fSelectOptionFromValue

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fGetIndexOptionFromText                                            **
//* GOAL: get index of the corresponding item in the list from the label     **
//*                                                                          **
//***************************************************************** FUNCTION **
function fGetIndexOptionFromText(piSelectName, piFrameName, piText)
{
  var index;
  
  var select = top.frames[piFrameName].document.forms[0].elements[piSelectName];

  if (select)
  {
    for (index=0; index < select.options.length; index++)
    {

      if (select.options[index].text == piText)
      {
        return index;
      }
    }
    
  }

  fDebug('unable to find ' + piSelectName + ' in frame ' + piFrameName + '!');
  return -1;

} // fGetIndexOptionFromText

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: isChecked                                                          **
//* GOAL: return True if given CheckBox is checked                           **
//*       Looks in first Form of given frame                                 **
//*                                                                          **
//***************************************************************** FUNCTION **
function isChecked(piCheckboxName, piFrameName)
{
  var checkBox;
  
  if (piFrameName)
    checkBox = top.frames[piFrameName].document.forms[0].elements[piCheckboxName];
  else
    checkBox = document.forms[0].elements[piCheckboxName];

  return checkBox && checkBox.checked;
} // isChecked


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fSetHiddenValue                                                    **
//* GOAL: Set specified hidden input value to given value.                   **
//*       piFrameName is optional, by default it uses contentFrameName       **/
//*                                                                          **
//***************************************************************** FUNCTION **
function fSetHiddenValue(piHiddenName, piValue, piFrameName)
{
  top.frames[piFrameName?piFrameName:contentFrameName].document.forms[0].elements[piHiddenName].value = piValue;

} // fSetHiddenValue

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintInputHidden                                                  **
//* GOAL: Print INPUT Hidden                                                 **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintInputHidden(piName, piValue)
{
  var ret='';

  if (typeof(piValue) !='undefined')
    ret+= '<INPUT TYPE="HIDDEN" NAME="'+ piName +'" VALUE="'+ piValue +'">';
  else
    ret+= '<INPUT TYPE="HIDDEN" NAME="'+ piName + '">';

  if (DEBUG>10)
  {
    ret+= fPrintText('INPUT '+ piName + ':' + piValue + '.', 0, 'red');
  }

  return ret;
} // fPrintInputHidden

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fGetInput                                                          **
//* GOAL: Return the form input object from given name [and frame]           **
//*                                                                          **
//***************************************************************** FUNCTION **
function fGetInput(piInputName, piFrameName)
{
  var theform;
  if (top.frames.length)
  {
    theform = top.frames[piFrameName?piFrameName:contentFrameName].document.forms[0];
  }
  else
  {
    theform = top.document.forms[0];
  }

  return theform.elements[piInputName];

} // fGetInput


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fGetInputValue                                                     **
//* GOAL: Return value of given Input                                        **
//*       Also works with Radio Inputs (return '' if no selection)           **
//*       Also works with checkbox (return true if checked, false otherwise) **
//*                                                                          **
//***************************************************************** FUNCTION **
function fGetInputValue(piInputName, piFrameName)
{
  var input = fGetInput(piInputName, piFrameName);
  if (input)
  {
    if (input.length != undefined)
    {
      // Radio Input array: return the first selection value
      for (var i=0; i<input.length; i++)
      {
        if (input[i].checked)
        {
          return input[i].value;
        }
      }
      return '';
    }
    else
    {
      if (input.type == 'checkbox')
      {
        return input.checked;
      }
      else
      {
        return input.value;
      }
    }
  }
  else
    return input;

} // fGetInputValue

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintInputRadio                                                   **
//* GOAL: Print a Input Radio object, with as many Options as wanted         **
//*       - piName         : (MANDATORY) name of all INPUT options           **
//*       - piOptionArray  : (MANDATORY) Array of [value, label, checked]    **
//*       - piSeparatorText: any HTM code. Ex: '<BR>'                        **
//*       - piOnClickJsCode: any JS code. Ex: 'alert(\'Are you sure ?\');'   **
//*       - piCheckedValue : if defined, corresponding item will be checked  **
//*   EX; fPrintInputRadio('COSTINGPERIOD', [['M', 'Month', 1], ['Q', 'Quarter', 0], ['Y', 'Year', 0]], '<BR>', 'alert(\'Period clicked\');', 'Q');
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintInputRadio(piName, piOptionArray, piSeparatorText, piOnClickJsCode, piCheckedValue)
{
  var ret='';
  var i;
  var optionItem;     // an Array containing 3 items: [value, label, checked]

  if (piOnClickJsCode.length == 0)
  {
    piOnClickJsCode = 'top.setScreenModif();';
  }

  for (i=0; i<piOptionArray.length; i++)
  {
    optionItem = piOptionArray[i];
    var isChecked = optionItem[2] || piCheckedValue == optionItem[0];

    ret+= '<INPUT TYPE="radio" NAME="'+ piName + '" VALUE="'+ optionItem[0] + '" '+ (piOnClickJsCode.length?' ONCLICK="javascript:'+ piOnClickJsCode +'"':'') + (isChecked?' CHECKED':'') +'> ' + optionItem[1];
    if (i < piOptionArray.length-1)
      ret+= piSeparatorText;
  }

  return ret;
} // fPrintInputRadio

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintInputTextarea                                                **
//* GOAL: Print a Textarea object, with as many Options as wanted            **
//*       - piName         : (MANDATORY) name of INPUT                       **
//*       - piCols         : number of columns                               **
//*       - piRows         : number of rows                                  **
//*       - piOnChange     : any JS code. Ex: 'alert(\'Are you sure ?\');'   **
//*       - piOnBlur       : any JS code. Ex: 'alert(\'Really sure ?\');'    **
//*       - piOnFocus      : any JS code. Ex: 'alert(\'Absolutly sure ?\');' **
//*       - piOnSelect     : any JS code. Ex: 'alert(\'Positively sure?\');' **
//*       - piWrap         : "OFF"|"HARD"|"SOFT"                             **
//*       - piText         : content                                         **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintInputTextarea(piName, piCols, piRows, piOnChange, piOnBlur, piOnFocus, piOnSelect, piWrap, piText)
{
  var ret='';

  ret += '<TEXTAREA ';
  if (piName != undefined && piName.length)
    ret += ' NAME="'+ piName +'"';

  if (piCols != undefined)
    ret += ' COLS="'+ piCols +'"';
  if (piRows != undefined)
    ret += ' ROWS="'+ piRows +'"';

  ret += 'ONCHANGE="top.setScreenModif(true);';

  if (piOnChange != undefined && piOnChange.length)
    ret += piOnChange;

  ret += '"';

  if (piOnBlur != undefined && piOnBlur.length)
    ret += ' ONBLUR="'+ piOnBlur + '"';
  if (piOnFocus != undefined && piOnFocus.length)
    ret += ' ONFOCUS="'+ piOnFocus + '"';
  if (piOnSelect != undefined && piOnSelect.length)
    ret += ' ONSELECT="'+ piOnSelect + '"';

  if (piWrap != undefined)
    ret += ' WRAP="'+ piWrap +'"';

  ret+= '>';

  if (piText != undefined && piText.length)
    ret += piText;

  ret += '</TEXTAREA>';
  return ret;
} // fPrintInputTextarea

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintCheckBox                                                     **
//* GOAL: Print a Input Check Box with as many Options as wanted             **
//*       - piName         : name of the Input                               **
//*       - piChecked      : Initially checked or not (default is false)     **
//*       - piLabel        : Label displayed rightside of the check box      **
//*       - piOnClickJsCode: any JS code.                                    **
//*       - piValue        : Value sent to the server instead of 'ON' default**
//*   EX; fPrintCheckBox('DELETE', true, 'Delete', 'alert(\'Sure ?\');', 1); **/
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintCheckBox(piName, piChecked, piLabel, piOnClickJsCode, piValue)
{
  var ret='';

  ret+= '<INPUT TYPE="checkbox"';
  if (piName != undefined)
    ret+= ' NAME="'+ piName +'"';
  if (piChecked)
    ret+= ' CHECKED';
  if (piValue != undefined)
    ret+= ' VALUE="'+ piValue +'"';
  if (piOnClickJsCode != undefined && piOnClickJsCode.length)
    ret+= ' ONCLICK="'+ piOnClickJsCode + '"';
  ret+= '>';
  if (piLabel != undefined && piLabel.length)
    ret+= piLabel;

  return ret;
} // fPrintCheckBox

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintText                                                         **
//* GOAL: print a text with given optional FONT attributes                   **
//*       piSize = 'default', -2, -1, 0, +1, +2 ...                                     **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintText(piText, piSize, piColor)
{
  var ret = '';

  if (typeof(piSize) !='undefined')
  {
    ret+= '<FONT';
    if (piSize != 'default')
    {
      ret+= ' SIZE=' + piSize;
    }
    if (piColor)
    {
      ret+= ' COLOR=' + piColor;
    }
    ret+= '>' + piText + '</FONT>';

  }
  else
    ret = piText;

  return ret;

} // fPrintText


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintTextHelp                                                     **
//* GOAL: print a text with HREF linked on a help page                       **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintTextHelp(piText, piHelpPage, piTip, piSize)
{ 
  if (piHelpPage || piTip)
  {
    var ret='';
    var tip = (piTip?piTip:'');

    if (piHelpPage)
    {
      if (top.ApHlp)
        ret+= '<A HREF="javascript:top.ApHlp(\'' + top.gHtmPath + '/' + piHelpPage + '\')"';
      else
        ret+= '<A HREF="' + top.gHtmPath + '/' + piHelpPage + '"';
    }
    else
    {
      ret+= '<A HREF="javascript:alert(\''+piTip+'\')"';
    }
    ret+= ' onMouseOver=\"window.status=\''+ top.fFilterStringJS(tip) +'\'; return true;\" onMouseOut=\"window.status=\'\'; return true;\"';
    ret+= '>'+ (piSize?fPrintText(piText, piSize):piText) +'</A>';

    return ret;
  }
  else
    return piText;

} // fPrintTextHelp

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintTextWithJS                                                   **
//* GOAL: Print Text which launch JS code when clicked                       **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintTextWithJS(piText, piJsCode, piTip, piSize)
{
  if (piJsCode)
  {
    var ret='';
    var tip = (piTip?piTip:'');

    ret+= '<A HREF="javascript:'+ piJsCode + '"';
    ret+= ' onMouseOver=\"window.status=\''+ top.fFilterStringJS(tip) +'\'; return true;\" onMouseOut=\"window.status=\'\'; return true;\"';
    ret+= '>'+ (piSize?fPrintText(piText, piSize):piText) +'</A>';

    return ret;
  }
  else
    return piText;

} // fPrintTextWithJS


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintTextError                                                    **
//* GOAL: Format and Print given Error message                               **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintTextError(piMessage)
{
  return fPrintText('PCS SCRIPT ERROR: ' + piMessage, 2, 'red')
} // fPrintTextError

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintIconHelp                                                     **
//* GOAL: print an icon with HREF linked on a help page                      **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintIconHelp(piImageSource, piHelpPage, piTip)
{ 
  if (piHelpPage)
  {
    var ret='';

    ret+= '<A HREF="javascript:top.ApHlp(\'' + top.gHtmPath + '/' + piHelpPage + '\')">';
    ret+= fDisplayImg(piImageSource, '', piTip) +'</A>';
    return ret;
  }
  else
    return fDisplayImg(piImageSource, '', piTip);

} // fPrintIconHelp

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintInputHyperlink                                               **
//* GOAL: Print a Http or ftp hyperlink, which target may be modified        **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintInputHyperlink(piLabel, piTarget, piInputName)
{
  var ret='';
  var isModif = (piInputName?true:false);

  if (isModif)
  {
    ret+= fPrintInputHidden(piInputName, piTarget);
    ret+= _fDisplayIcon('', 'fUpdateHyperlink(\''+ piInputName + '\');', 'UpdateLink', 'cone1.gif', '', '', 'Update the HyperLink URL');
  }

  if (piLabel || piTarget)
  {
    if (!piLabel)
      piLabel = piTarget;

    if (piTarget && piTarget.length)
    {
      ret+= '<A HREF="'+ piTarget +'" TARGET="'+ piLabel +'"><U>'+ fFilterString(piLabel) +'</U></A>';
    }
    else if (!isModif)
    {
      ret+= piLabel;
    }
  }

  return ret;

} // fPrintInputHyperlink

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fUpdateHyperlink                                                   **
//* GOAL: Open a dialog box to change the hyperlink target                   **
//*                                                                          **
//***************************************************************** FUNCTION **
function fUpdateHyperlink(piInputName)
{
  var initialValue = fGetInputValue(piInputName);
  if (!initialValue)
  {
    initialValue = 'http://';
  }

  var res = prompt('Enter the Hyperlink target URL.\nNB: You need to Save the screen to take your modification into account.', initialValue);
  if (res != null && res != fGetInputValue(piInputName))
  {
    var reHttpOrFtp = new RegExp('^((http:)|(ftp:))(//)[\\d\\D]+(\\.st\\.com)[\\d\\D]*$', 'i');
    var reFile = new RegExp('^(file:///)[\\d\\D]+', 'i');

    if (!res || reHttpOrFtp.test(res) || reFile.test(res))
	  {
      fSetHiddenValue(piInputName, res);
      setScreenModif();
    }
    else
    {
      alert('Invalid URL format : "' + res + '".\nPlease enter a valid Http, Ftp or File Url. For example:\n\nhttp://ccsweb.sgp.st.com/\nftp://pcstest.gnb.st.com/pcs_mv_download/lpcs.mdb\nfile:///M|/Language Ref/Javascript/JavaScript 1.3 Reference/INDEX.HTM\n\nNB: only "st.com" Http or Ftp Url are allowed.');
    }
  }
} // fUpdateHyperlink


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintArray                                                        **
//* GOAL: format in HTML the content of a JS array                           **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintArray(piArray, piTitle)
{
  var ret = '';
  var i;

  ret+= '<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2>';
  ret+= '<TR><TH CLASS=TABLEHEADER>'+ piTitle +'</TH></TR>';
  for (i=0; i<piArray.length; i++)
  {
    ret+= '<TR><TD>'+ piArray[i] +'</TR></TD>';
  }
  ret+= '</TABLE>';

  return ret;

} // fPrintArray

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintPcsDate                                                      **
//* GOAL: convert given Date object into the PCS date format: MM/DD/YYYY     **
//*       Attach this method to the instance of Date object you create:      **
//*   EX: var myDate = new Date(); myDate.toPCSString = fPrintPcsDate;       **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintPcsDate()
{
  return fLeftJustify(this.getMonth()+1, 2) + '/' + fLeftJustify(this.getDate(), 2) + '/' + this.getFullYear();

} // fPrintPcsDate


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fJSIsEmpty                                                         **
//* GOAL: return true if this string is null or empty                        **
//*                                                                          **
//***************************************************************** FUNCTION **
function fJSIsEmpty(piString)
{
  return (piString == null || piString == '');

} // fJSIsEmpty

//* FUNCTION ****************************************************************
//*                                                                        **
//* NAME: fZeroToNull                                                      **
//* GOAL: if the input string value is zero, returns ''                    **
//*                                                                        **
//*************************************************************** FUNCTION **
function fZeroToNull(piString)
{
  if (parseFloat(piString))
  {
    return piString;
  }
  else
  {
    return '';
  }
} // fZeroToNull

//* FUNCTION ****************************************************************
//*                                                                        **
//* NAME: fNullToNbsp                                                      **
//* GOAL: if the input string is zero length, returns '&nbsp;'             **
//*                                                                        **
//*************************************************************** FUNCTION **
function fNullToNbsp(piString)
{
  if (piString.length)
  {
    return piString;
  }
  else
  {
    return '&nbsp;';
  }
} // fNullToNbsp

//* FUNCTION ****************************************************************
//*                                                                        **
//* NAME: fZeroToNbsp                                                      **
//* GOAL: if the input string value is zero returns '&nbsp;'               **
//*                                                                        **
//*************************************************************** FUNCTION **
function fZeroToNbsp(piString)
{
  if (parseFloat(piString))
  {
    return piString;
  }
  else
  {
    return '&nbsp;';
  }
} // fZeroToNbsp

//* FUNCTION ****************************************************************
//*                                                                        **
//* NAME: fIsPrefixe                                                       **
//* GOAL: return TRUE is piStr1 is a Prefixe of piStr2                     **
//*                                                                        **
//*************************************************************** FUNCTION **
function fIsPrefixe(piStr1, piStr2)
{
  return (piStr2.substr(0, piStr1.length) == piStr1);
}  // fIsPrefixe

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintVerticalLine                                                 **
//* GOAL: Return HTML code for a Vertical black line                         **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintVerticalLine(piHeight)
{
  return '<IMG SRC="'+ top.gGifPath + '/black.gif" border=0 height='+ piHeight +' width=1>';

} // fPrintVerticalLine

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintPlaceHolder                                                  **
//* GOAL: Return Html code for a transparent image of fixed width and height **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintPlaceHolder(piWidth, piHeight)
{
  return '<IMG SRC=\"'+ top.gGifPath +'/transparentPixel.gif\" BORDER=0 WIDTH=' + piWidth + ' HEIGHT=' + piHeight + '>';

} // fPrintPlaceHolder

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: TrimExtraSpaces                                                    **
//* GOAL: Renvoie la chaine sans les Espace surnuméraires                    **
//*                                                                          **
//***************************************************************** FUNCTION **
function TrimExtraSpaces(str)
{
  str = str.replace(/\r/g, " ");

  //There are regex switchs for printing /nonprinting characters but they stripped some characters either needed or left some not wanted, thats why the following replacement exists.
  str = str.replace(/[^ A-Za-z0-9`~!@#\$%\^&\*\(\)-_=\+\\\|\]\[\}\{'";:\?\/\.>,<]/g, "");
  str = str.replace(/'/g, "");
  str = str.replace(/ +/g, " ");
  str = str.replace(/^\s/g, "");
  str = str.replace(/\s$/g, "");
  if (str == ' '){str = ''};
  return str;
}

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: GetUrl                                                             **
//* GOAL: Recharge le contenu des frames de gauche et du centre              **
//*                                                                          **
//***************************************************************** FUNCTION **
function GetUrl(link1, link2)
{
  if (link1 != '')
  {
    if (parent.leftmenu != undefined)
      parent.leftmenu.location.href = link1;
  }
  if (link2 != '')
  {
    if (parent.center != undefined)
   	  parent.center.location.href = link2;
    else
      document.location.href = link2;
  }
}

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: ouvrirFenetre                                                      **
//* GOAL: permet d'uvrir une nouvelle fenetre avec la taille, la position,   **
//*       la cible et la source passées en paramètre et de garder le focus   **
//***************************************************************** FUNCTION **
function ouvrirFenetre(source, cible, largeur, hauteur, gauche, haut)
{
  var fenetre = window.open(source, cible, 'toolbar=1,width='+largeur+',height='+hauteur+',scrollbars=1,left='+gauche+',top='+haut+',resizable=1');
  fenetre.focus();
}

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: GetUrlAide                                                         **
//* GOAL: Recharge le contenu des frames de gauche et du centre              **
//*                                                                          **
//***************************************************************** FUNCTION **
function GetUrlAide(link1, link2)
{
	parent.header_aide.location.href = link1;
 	parent.menu_aide.location.href = link2;
}

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: ActualisePage                                                      **
//* GOAL: Recharge le contenu de la page si possible, en changeant le mode   **
//*                                                                          **
//***************************************************************** FUNCTION **
function ActualisePage(frame, nouveauMode)
{
  if (frame == undefined)
    return false;
    
  var url = frame.location.href;
  
  // alert(url);
  
  if (url.match('mode='))
  {
    var mode1 = getURLParam(url, 'mode');
    var mode2;
    
    if (nouveauMode == undefined || nouveauMode == '')
    {
      // alert('nouveauMode non donné');
      if (url.match('mode_a='))
      {
        mode2 = getURLParam(url, 'mode_a');
      }
    }
    else
    {
      // alert('nouveauMode donné !');
      mode2 = nouveauMode;
    }
    
    // alert('mode1='+mode1+', mode2='+mode2);
    if (mode2 != '' && mode2 != undefined)
    {
      url = url.replace('mode='+mode1,'mode='+mode2);
      // alert(url);
      frame.location.href = url;
    }
  }
}

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: getURLParam                                                        **
//* GOAL: Renvoie la valeur du parametre donne dans la chaine d'url          **
//*                                                                          **
//***************************************************************** FUNCTION **
function getURLParam(url, strParamName)
{
  var strReturn = "";
  var strHref = url;
  if (strHref.indexOf("&") > -1)
  {
    var strQueryString = strHref.substr(strHref.indexOf("&")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for (var iParam = 0; iParam < aQueryString.length; iParam++)
    {
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 )
      {
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
      }
    }
  }
  return strReturn;
}

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: setFramesetCols                                                    **
//* GOAL: Resize les colonnes du Frameset Central                            **
//*                                                                          **
//***************************************************************** FUNCTION **
function setFramesetCols(cols)
{
  if (document.all || document.getElementById)
	{
		// DOM support
		doc = top.document.all("top_frameset").all("middle_frameset");
		if (doc)
		{
			if (doc.cols == '30%,70%')
			{
				doc.cols = cols;
			}
			else
			{
				doc.cols = '30%,70%';
			}
		}
  }
}

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: closeWindow                                                        **
//* GOAL: Ferme une fenetre pop-up (si safari donne le focus à son opener)   **
//*                                                                          **
//***************************************************************** FUNCTION **
function closeWindow()
{
  var detect = navigator.userAgent.toLowerCase();

  if(detect.indexOf('safari') + 1)
  {
    opener.focus();
  }
  else
  {
    window.close();
  }
}

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: calcTextAreaRows                                                   **
//* GOAL: Augmente le nombre de Rows d'un TEXTAREA.                          **
//*       Ex: <textarea ...  onkeyup="calcTextAreaRows(this)">               **
//*                                                                          **
//***************************************************************** FUNCTION **
function calcTextAreaRows(t)
{
	a = t.value.split('\n');
	b=1;
	for (x=0;x < a.length; x++)
	{
		if (a[x].length >= t.cols)
		{
			b+= Math.floor(a[x].length/t.cols);
		}
	}
	b+= a.length;
	if (b > t.rows)
	{
		t.rows = (b>50?50:b);
	}
}

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: addToFavorites                                                     **
//* GOAL: Ajoute le site au favoris                                          **
//*                                                                          **
//***************************************************************** FUNCTION **
function addToFavorites(url,titre)
{
	if (navigator.appName != 'Microsoft Internet Explorer')
	{
		window.sidebar.addPanel(titre,url,"");
	}
	else
	{
		window.external.AddFavorite(url,titre);
	}
}