/******************************************************************
        Ajout un certain nombre de 0 avant le nombre.
*******************************************************************/
function Ajout0avantNbr(valeur, long) 
{
	for (i = valeur.toString().length; i < long; i++)
		valeur = "0" + valeur;
	return valeur;
}

/******************************************************************
   AffDate()
   
        Retourne la date.
	
   Author: Steeve Bédard
   Date Created: 2002-02-11
*******************************************************************/
function AffDate()
{
	dateDuJour = new Date();
	anne = dateDuJour.getYear().toString();
	mois = Ajout0avantNbr(dateDuJour.getMonth() + 1, 2);
	jour = Ajout0avantNbr(dateDuJour.getDate(), 2);
	return jour + "/" + mois + "/" + anne;
}
/******************************************************************
   Arrondir(nombre, decimal)
   
        Arrondit un nombre d'après le nombre de décimal.
	
	nombre : valeur a arrondir
	decimal : nombre de décimal
   
   Author: Steeve Bédard
   Date Created: 2001-09-26
*******************************************************************/
function Arrondir(nombre, decimal) 
{
    var result1 = nombre * Math.pow(10, decimal);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimal);
    return AjoutZero(result3, decimal);
}

/******************************************************************
   AjoutZero()
   
   Ajoute des zéro d'après le nombre de décimal.
   
   Author: Steeve Bédard
   Date Created: 2001-09-26
*******************************************************************/
function AjoutZero(valeur, NbDecimal) 
{
    // Convert the number to a string
    var value_string = valeur.toString();
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".");

    // Is there a decimal point?
    if (decimal_location == -1) 
    {
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0;
        
        // If NbDecimal is greater than zero, tack on a decimal point
        value_string += NbDecimal > 0 ? "." : "";
    }
    else 
    {
        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1;
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = NbDecimal - decimal_part_length;
    
    if (pad_total > 0) 
    {
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0";
    }
    return value_string;
}

/**********************************************/
function ValideDate(j, m, a)
{
	if (j < 1 || m < 1 || m > 12|| a < 0)
		return false;
	else
		switch (m)
		{
			case 1  :
			case 3  :
			case 5  :
			case 7  :
			case 8  :
			case 10 :
			case 12 :	if (j > 31)
							return false;
						break;
			case 4  :
			case 6  :
			case 9  :
			case 11 : 	if (j > 30)
							return false;
						break;
			case 2  : 	if ((a % 100) == 0)
						{
							if ((a % 400) == 0)
							{
								if (j > 29)
									return false;
							}
							else
								if (j > 28)
									return false;
						}
						else
							if ((a % 4) == 0)
							{
								if (j > 29)
									return false;
							}
							else
								if (j > 28)
									return false;
						break;
		}
	return true;
}

/******************************************************************
   ignoreSpaces(string)
   
        Enlève les espaces dans une String.
		
	string : chaine de caractères
   
   Author: Steeve Bédard
   Date Created: 2001-09-26
*******************************************************************/
function ignoreSpaces(string) 
{
  var temp = "";
  string = '' + string;
  splitstring = string.split(" ");
  for(i = 0; i < splitstring.length; i++)
    temp += splitstring[i];
  return temp;
}

/**********************************************/
function VerifieValeur(champs, nom_champs)
{
  if (ignoreSpaces(champs.value) == "")
  {
      alert("Please enter value into field " + nom_champs);
	  try
	  {
	    champs.focus();
	  }
	  catch(ex)
	  {
	    //Do nothing
	  }
	  
      return false;
  }
  return true;
}

/******************************************************************
   VerifieNombre(champ, NbDecimal)
   
        Vérifie si c'est un nombre valide.
		
   champ : Objet du formulaire
   NbDecimal : Nombre de décimal après le point
   
   Author: Steeve Bédard
   Date Created: 2001-12-03
*******************************************************************/
function VerifieNombre(champs, nom_champs, NbDecimal) 
{	
	if (NbDecimal == 0)
	{
		if (isNaN(champs.value) || ignoreSpaces(champs.value) == "" || champs.value.indexOf(".") != -1 || champs.value <= "0")
		{
			alert("Please enter value into field " + nom_champs);
			champs.focus();
			return false;
		}
	}
	else
	{
		if (isNaN(champs.value) || ignoreSpaces(champs.value) == "" || champs.value <= "0.00")
		{
			alert("Please enter value into field " + nom_champs);
			champs.focus();
			return false;
		}
		else
			champs.value = Arrondir(champs.value, NbDecimal);
	}
	return true;
}

function NextFocus(F, strName, nbrElemSuivant)
{
    try
    {
         for(i = 0; i < F.length; i++)
         {
              if(F[i].name == strName)
                {
                    if(i + nbrElemSuivant < F.length)
                    {
                        if(F[i + nbrElemSuivant])
                        {
	                        F[i + nbrElemSuivant].focus();
	                        break;
	                    }
	                }
	                else
	                    return false
                }
          }
     }
     catch(ex)
     {
        //Do nothing
     }
}

function isEntier(champ)
{
	if (!isNaN(parseInt(champ.value, 10)) || champ.value.indexOf('.') == -1)
    {
    	return true;
    }
    return false;
}

function EstEntier(champ)
{
	if (champ.value == '' || ((!isNaN(parseInt(champ.value, 10)) && champ.value.indexOf('.') == -1 && champ.value.indexOf(',') == -1)))
    {
    	return true;
    }
    return false;
}

function isEntier(champ, nomChamp, NbrMin) 
{	
	if (isNaN(parseInt(champ.value, 10)) || champ.value.indexOf(".") != -1 || parseInt(champ.value, 10) < NbrMin)
	{
		AlertFocus(champ, nomChamp);
		return false;
	}
	return true;
}

function isEntier(champ, nomChamp, NbrMin, NbrMax) 
{	
	if (isNaN(parseInt(champ.value, 10)) || champ.value.indexOf(".") != -1 || parseInt(champ.value, 10) < NbrMin || parseInt(champ.value, 10) > NbrMax)
	{
		AlertFocus(champ, nomChamp);
		return false;
	}
	return true;
}

function isReel(champ, nomChamp, NbrMin) 
{	
	if (isNaN(parseFloat(champ.value, 10)) || parseFloat(champ.value, 10) < NbrMin)
	{
		AlertFocus(champ, nomChamp);
		return false;
	}
	return true;
}

function isReel2(champ) 
{	
	if (isNaN(parseFloat(champ.value)))
	{
		return false;
	}
	return true;
}

function isReelValue(value) {
    if (isNaN(parseFloat(value, 10))) {
        return false;
    }
    return true;
}

function AlertFocus(element, nomChamps)
{
	alert("Please enter value into field " + nomChamps + ".");
	if(!element.disabled)
	    element.focus();
}

function ExisteArray(Vecteur, Objects, Message)
{	
    var Trouver = false;

    for (var i = 0; i < Vecteur.length; i++)
        if (Vecteur[i] == Objects.value)
        {
	        Trouver = true;
	        break;
        }
    	
    if (!Trouver)
    {
        alert("The " + Message + " is not listed.");
        Objects.focus();
        Objects.select();
    }
}

function isSelectionner(champ, nomChamp) 
{	
	if (parseInt(champ.value, 10) < 1)
	{
		AlertFocus(champ, nomChamp);
		return false;
	}
	return true;
}

function randomNumber(limit)
{
  return Math.floor(Math.random()*limit);
}


// Removes ending whitespaces
function RTrim( value ) {
  
  var re = /((\s*\S+)*)\s*/;
  return value.replace(re, "$1");
  
}

function LRTrim( value ) {
    return LTrim(RTrim(value));
}

// Removes leading and ending whitespaces
function trim( sString ) {
  while (sString.substring(0,1) == ' ')
  {
    sString = sString.substring(1, sString.length);
  }
  while (sString.substring(sString.length-1, sString.length) == ' ')
  {
    sString = sString.substring(0,sString.length-1);
  }
  return sString;  
}

//L'équivalent d'un trim Vb.net
function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
		
function FindByValue(ddl,value)
{
    for(c = 0; c < ddl.options.length; c++)
    {
        if(ddl.options[c].value == value)
            return ddl.options[c];
    }
    return null;
}

function SearchPartOfValue(ddl,part)
{
    var sCpt;
    for(sCpt = 0; sCpt < ddl.options.length; sCpt++)
    {
        if(ddl.options[sCpt].value.indexOf(part) > -1)
            return ddl.options[sCpt];
    }
    return null;
}

function SelectByValue(ddl,value)
{
    for(c = 0; c < ddl.options.length; c++)
    {
        if(ddl.options[c].value == value)
            ddl.selectedIndex = c;
    }
}

function FindByText(ddl,text)
{
    var o;
    for(o = 0; o < ddl.options.length; o++)
    {
        if(ddl.options[o].text == text)
        {
            return ddl.options[o];
        }
    }
    return null;
}

function FindByTextContain(ddl,text)
{
    var o;
    for(o = 0; o < ddl.options.length; o++)
    {
        if(ddl.options[o].text.indexOf(text) > -1)
        {
            return ddl.options[o];
        }
    }
    return null;
}

function FindPosByValue(ddl,value)
{
    var o;
    for(o = 0; o < ddl.options.length; o++)
    {
        if(ddl.options[o].value == value)
            return o;
    }
    return -1;
}

function dateDifference(strDate1,strDate2){
     var Array1;
     var Array2;
     var datDate1;
     var datDate2;
     Array1 = strDate1.split('/');
     Array2 = strDate2.split('/');
     datDate1= Date.parse(Array1[1] + '/' + Array1[0] + '/' + Array1[2]);
     datDate2= Date.parse(Array2[1] + '/' + Array2[0] + '/' + Array2[2]);
     return Math.abs(Math.round((datDate1-datDate2)/86400000));
}

function getText(obj) {
    if (document.all) { // IE;
        return obj.innerText;
    }
    else {
        return obj.textContent;
    }
}

function setText(obj, Texte) {
    if (document.all) { // IE;
        obj.innerText = Texte;
    }
    else {
        obj.textContent = Texte;
    }
}

function dateAdd(aDate, interval, intervalType) {
    /*/
    dateAdd() returns a date object "interval" in the future (or past if "interval" is a negative number
    "dateString" (required) - a date/time string or date object;
    "interval" (required, either string or number) - interval of time to add, use negative number to subtract
    "intervalType" (optional, case-INsensitive) - if omitted, assumes "interval" is in days
    valid "intervalType" values:
    "y" : interval represents years to add
    "m" : interval represents months to add
    "d" : interval represents days to add
    "hh": interval represents hours to add
    "mm": interval represents minutes to add
    "ss": interval represents seconds to add
    "ms": interval represents milliseconds to add
    /*/
    interval = +interval; // convert interval from string to number if necessary
    if (intervalType) intervalType = intervalType.toLowerCase();
    switch (intervalType) {
        case "y":
            aDate.setFullYear(aDate.getYear() + interval);
            break;
        case "m":
            aDate.setMonth(aDate.getMonth() + interval);
            break;
        case "hh":
            aDate.setHours(aDate.getHours() + interval);
            break;
        case "mm":
            aDate.setMinutes(aDate.getMinutes() + interval);
            break;
        case "ss":
            aDate.setSeconds(aDate.getSeconds() + interval);
            break;
        case "ms":
            aDate.setMilliseconds(aDate.getMilliseconds() + interval);
            break;
        case "d":
        default:
            aDate.setDate(aDate.getDate() + interval);
    }
    return aDate;
}

function addOption(selectbox, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}

function CheckLength(text, long) {
    var maxlength = new Number(long); // Change number to your max length.
    if (text.value.length > maxlength) {
        text.value = text.value.substring(0, maxlength);

        alert(" Only " + long + " chars");

    }
}

function checkTextAreaMaxLength(textBox, e, length) {

    var mLen = textBox["MaxLength"];
    if (null == mLen)
        mLen = length;

    var maxLength = parseInt(mLen);
    if (!checkSpecialKeys(e)) {
        if (textBox.value.length > maxLength - 1) {
            if (window.event)//IE
                e.returnValue = false;
            else//Firefox
                e.preventDefault();
        }
    }
}
function checkSpecialKeys(e) {
    if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
        return false;
    else
        return true;
}

function sortlist(lb) {
    arrTexts = new Array();

    for (i = 0; i < lb.length; i++) {
        arrTexts[i] = lb.options[i].text;
    }

    arrTexts.sort();

    for (i = 0; i < lb.length; i++) {
        lb.options[i].text = arrTexts[i];
        lb.options[i].value = arrTexts[i];
    }
}


String.prototype.startsWith = function(str) { return (this.match("^" + str) == str) }

String.prototype.endsWith = function(str) { return (this.match(str + "$") == str) }

String.prototype.trim = function() { return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "")) }

