//
// Ascii (or Character) to Hexidecimal
//        string = hex(numeric)
//        string = hex(character)
//        (returns empty string on error or empty string)
//
// Character (or Hexidecimal )to Ascii
//        numeric = asc(character)
//        numeric = asc(string)
//        (returns -1 on error or empty string)
//
// Ascii (or Hexidecimal) to Character
//        character = chr(numeric)
//        character = chr(string)
//        (returns empty string on error or 0 or "%00")
//
// internal function
//        chrListInit()
//        (creates chrList and hexList, returns nothing)
//
// Ascii must be numeric from 0 through 255.999999
// Hexidecimal must be a string of length 3 and prefix '%'
// Character must be a string of at least length 1
//
// All variables used are local except for chrList and hexList
// which is are global string of lengths 255 and 22
//
// Note: a character of ascii 0 terminates a string so
// converting %00 or 0 to a charcter returns an empty string
//
// -->
function chr(i) {
   if (typeof i == "number") {                          // invalid if not numeric
      if ((i >= 1) && (i < 256)) {                      // invalid if out of range
         if (typeof chrList != "string") chrListInit()  // init chrList if undefined
         return chrList.charAt(i-1)                     // chrList.charAt(0) = ascii 1
      }
   }
   if (typeof i == "string") {                          // if a string, might be hex
      if (i.length == 3) {                              // hex must be length 3
         if (i.charAt(0) == "%") {                      // hex must have prefix %
            var k = "0123456789ABCDEFabcdef"            // legal hex digits
            var j = i.charAt(1)                         // first digit
            var l = i.charAt(2)                         // second digit
            if ((k.indexOf(j) >= 0) && (k.indexOf(l) >= 0))
               return unescape("%"+j+l)
         }
      }
   }
   return ""                                            // fail, invalid
}

function hex(i) {
   if (typeof i == "number") {                          // invalid if not numeric
      if ((i >= 0) && (i < 256)) {                      // invalid if out of range
         var k = "0123456789ABCDEF"                     // hex digits
         var j = parseInt(i/16)                         // upper eight bits
         var l = parseInt(i) % 16                       // lower eight bits
         return "%" + k.charAt(j) + k.charAt(l)         // return result
      }
   }
   if (typeof i == "string") {                          // invalid if not string
      if (i.length > 0) {                               // invalid if null string
         if (typeof chrList != "string") chrListInit()  // init chrList if undefined
         var l = chrList.indexOf(i.charAt(0))+1
         var j = parseInt(l/16)                         // upper eight bits
         l %= 16                                        // lower eight bits
         return "%" + hexList.charAt(j) + hexList.charAt(l)
      }
   }
   return ""                                            // fail, invalid
}

function asc(i) {
   if (typeof i == "string") {                          // invalid if not string
      if (i.length == 3) {                              // invalid if incorrect string length
         if (i.charAt(0) == "%")  {                     // invalid if not prefixed with %
            var k = "0123456789ABCDEFabcdef"
            var j = k.indexOf(i.charAt(1))
            var l = k.indexOf(i.charAt(2))
            if ((j >= 0) && (l >= 0)) {                 // invalid if non-hex characers found
               if (j > 15) j -= 6                       // adjust for lower case
               if (l > 15) l -= 6
               return j * 16 + l
            }
         }
      }
   }
   if (typeof i == "string") {                          // invalid if not string
      if (i.length > 0) {                               // invalid if null string
         if (typeof chrList != "string") chrListInit()  // init chrList if undefined
         return chrList.indexOf(i.charAt(0))+1          // chrList.charAt(0) = ascii 1
      }
   }
   return -1                                            // fail, invalid
}

function chrListInit() {
   chrList = ""
   hexList = "0123456789ABCDEFabcdef"
   for (var i=0; i<16; i++)
      for (var j=0; j<16; j++)
         chrList += unescape("%" + hexList.charAt(i) + hexList.charAt(j))
}

//=========================================
//function qui permet de formater les prix
//=========================================
function FormatString (EntryNb, Dec)
{
var DecMask = "00000000";
EntryNb = (Math.round(100*EntryNb)/100);
EntryString = EntryNb.toString(10);
// Position du point dans la chaine
DecPos = EntryString.indexOf(".");
if (DecPos <0)
{
// Le prix est round (ex : 123) => on rajoute le nombre de z?ro qui manque
EntryString = EntryString + dec_symb+ DecMask.substring(0,dec_nb);
}
else
{
fin = EntryString.substring(DecPos + 1);
if (fin.length >= 0)
{
while (fin.length < Dec)
 fin = fin + '0';
debut = EntryString.substring(0, DecPos);
res = debut + ',' + fin;
return(res);
}

EntryString =  EntryString.substring(0,DecPos+1+Number(dec_nb));
// Recup de la longeur de la chaine
Nb = EntryString.length;
if (dec_symb != "." && EntryString != 0 )
{
dec_symb_js = dec_symb;
}
else
{
dec_symb_js = ".";
}
EntryString_Tmp = EntryString;
// Le prix est egale ? la partie enti?re + le symbole de la d?cimale + le nombre de d?cimale
EntryString = EntryString_Tmp.substring(0,DecPos)+dec_symb_js+EntryString_Tmp.substring(DecPos+1,Nb);
// Le prix est round (ex : 4,3) => on rajoute le nombre de z?ro qui manque
if (EntryString_Tmp.substring(DecPos+1,Nb).length - dec_nb < 0)
{
EntryString = EntryString_Tmp.substring(0,DecPos)+dec_symb+EntryString_Tmp.substring(DecPos+1,Nb)+DecMask.substring(0,dec_nb-EntryString_Tmp.substring(DecPos+1,Nb).length);
}
grp_symb_js = grp_symb;
if (!isNaN(grp_symb)) grp_symb_js = chr(Number(grp_symb));
// Separateur de millier
Second_Part = dec_symb+EntryString_Tmp.substring(DecPos+1,Nb)+DecMask.substring(0,dec_nb-EntryString_Tmp.substring(DecPos+1,Nb).length);
Chaine = EntryString.substring(0,DecPos);
taille = Chaine.length;
index_tab = 1;
if (taille > grp_nb)
{
// Decoupe la chaine en fonction du nombre de separateur
while (taille > grp_nb)
{
Tab_String[index_tab] = Chaine.substring(taille-grp_nb,taille);
Chaine_tmp = Chaine.substring(0,taille-grp_nb);
taille = Chaine_tmp.length;
Chaine = Chaine_tmp;
if (taille > grp_nb) index_tab++;
}
// Reconstitue la chaine
Final_Chaine = Chaine_tmp+(grp_symb_js);
while (index_tab != 0)
{
Final_Chaine = Final_Chaine+Tab_String[index_tab];
index_tab--;
}
EntryString = Final_Chaine+ Second_Part;
}
}
return (EntryString);
}

//====================================
// fonction qui permet de recuperer
// la valeur numeriqiue d'un string 
// et de la renvoyee
//====================================
function GetValeurNumerique(valeur)
{
myVal = new String(valeur);
if (myVal == '')
return(0);
	p = myVal.indexOf(',');
	if (p  >= 0)
	{
		debut = new String(valeur);
		fin = new String(valeur);
		res = debut.substring(0, p) + '.' + fin.substring(p + 1);
		return(res);
	}
	else
	{
		return(valeur);
	}
}

//====================================
// Permet de savoir si la valeur 
// passee est numerique
//====================================
function isNotNumeric(valeur)
{
valeur = GetValeurNumerique(valeur);
res = isNaN(valeur);
myVal = new String(valeur);
p = myVal.indexOf('-');
if (p >= 0) return(true);
return(isNaN(valeur));
}



//====================================
// Permet de saisir un montant
//====================================
function inputAmount(form,new_amount,bad_amount,bAllowZero)
{
 myValue = eval('window.document.'+form+'.amount.options[window.document.'+form+'.amount.selectedIndex].value');
 NewValue = window.prompt(new_amount,'');
if(typeof(bAllowZero)=='undefined')
	bAllowZero = false;

 if (NewValue != null)
 {
  // Formatage du prix JavaScript
  
  // Position du s?parateur de d?cimal dans la chaine
  DecPos = NewValue.indexOf(dec_symb);

  if (DecPos > 0 && dec_symb != ".")
  {
   NewValue = NewValue.substring(0,DecPos)+"."+NewValue.substring(DecPos+1,NewValue.length);
   
   }

grp_symb_js = grp_symb;
if (!isNaN(grp_symb))
   {
    grp_symb_js = chr(Number(grp_symb));
    }
   // Position du s?parateur de millier dans la chaine
   DecPos = NewValue.indexOf(grp_symb_js);

while (DecPos > 0)
   {
    NewValue = NewValue.substring(0,DecPos)+NewValue.substring(DecPos+1,NewValue.length);
    DecPos = NewValue.indexOf(grp_symb_js);
    }

   NewValue = parseFloat(NewValue);

if (isNaN(NewValue))
   {
    window.alert (bad_amount);
    }
else
   {
    if ((NewValue<=0 && !bAllowZero ) || (percent && NewValue>100))
    window.alert (bad_amount);
	      else {
                    eval('window.document.'+form+'.amount.options[window.document.'+form+'.amount.selectedIndex].selected = false');
                    NewObjectOption = new Option();
		    NewObjectOption.selected = true;
                    var text_legend = '';
                    if (percent) text_legend = NewValue.toString(10)+" %"+" "+add_legend;
                    else text_legend = NewValue.toString(10)+" "+legend+" "+add_legend;
		    NewObjectOption.text     = text_legend;
		    NewObjectOption.value    = NewValue;

		    if (myValue == "-1")
		    eval('window.document.'+form+'.amount.options[0] = NewObjectOption');
		else
		    eval('window.document.'+form+'.amount.options[window.document.'+form+'.amount.options.length] = NewObjectOption');
		    }
   } // end if
} // end if
}

//====================================
// Fonction qui permet de remplacer
// les vigules d'une chaine par des
// point
//====================================
function ReplaceCommaByPoint(valeur)
{
    myVal = new String(valeur);
    if (myVal == '') return(0);
    p = myVal.indexOf(',');
    if (p  >= 0)
        {
            debut = new String(valeur);
            fin = new String(valeur);
            res = debut.substring(0, p) + '.' + fin.substring(p + 1);
            return(res);
        }
    else
        {
            return(valeur);
        }
}

