function replaceSubstring(inputString, fromString, toString) 
{
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") 
   {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) 
   { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else 
   { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") 
      {
         for (var i=0; i < midStrings.length; i++) 
         {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) 
            {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) 
      {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) 
      {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

function checkLength(objField, objFieldStatus, intSize)
{
	var asBeenCut = false;
	
	if (objField.value.length > intSize)
	{
		objField.value = objField.value.substring(0, intSize)
		alert("Le message ne peut contenir plus de " + intSize + " caractères.")
		objField.focus()
		asBeenCut = true;
	}
	objFieldStatus.value = objField.value.length
	
	return asBeenCut
}

/*
Function qui valide qu'un champ n'est pas vide en trimant la valeur avant de verifier si il est vide.
*/
function isEmpty(inString)
{
	var trimedString = trim(inString)
	
	if (trimedString == "")
		return true
	
	return false
}

/* 
Function qui accepte un pattern et ensuite que la chaine soit dans ce pattern la.

Pour ce faire, 

-> A - charactere.
-> 9 - chiffre
-> Tous les autres charactere doivent etre compris dans la chaine pour que ca soit valide.
*/
function validString(inString, pattern)
{
	var trimedStr = trim(inString)
	if (trimedStr.length != pattern.length)
	{
		return false
	}
	
	for (i = 0; i < pattern.length; i++)
	{		
		switch (pattern.charAt(i).toLowerCase())
		{
			
			case "@" :
			{
				if ((trimedStr.charAt(i).toLowerCase() < "a" || trimedStr.charAt(i).toLowerCase > "z") && (isNaN(trimedStr.charAt(i))))
				{
					return false
				}
				break;
			}
			
			case "a" : 
			{	
				if (trimedStr.charAt(i).toLowerCase() < "a" || trimedStr.charAt(i).toLowerCase > "z")
				{
					return false
				}
				break;
			}
			case "9" : 
			{
				if (isNaN(trimedStr.charAt(i)))
				{
					return false
				}
				break;				
			}
			default : 
			{
				if (trimedStr.charAt(i) != pattern.charAt(i))
				{
					return false				
				}
				break;
			}
		}
	}
	return true
}

function trim(str)
{
	// Enlève les espaces au début
	while(str.charAt(0)==" ")
	{
		str = str.substring(1,str.length);
	}
	// Enlève les espaces à la fin
	while(str.charAt(str.length-1)==" ")
	{
		str = str.substring(0,str.length - 1);
	}
	return str;
}

function validPostalCode(pc)
{
	//If not 7 character with the space
	if (pc.length != 7)
		return false;
		
	if (!isNaN(pc.charAt(0)))
		return false;
	
	if (isNaN(pc.charAt(1)))
		return false;
	
	if (!isNaN(pc.charAt(2)))
		return false;
	
	if (pc.charAt(3) != ' ')
		return false;

	if (isNaN(pc.charAt(4)))
		return false;
	
	if (!isNaN(pc.charAt(5)))
		return false;
	
	if (isNaN(pc.charAt(6)))
		return false;
		
	return true;
}

function shortTelephoneIsValid(telephone)
{	
	msg = "Entrer un numéro de téléphone valide (999-9999).";

	if (telephone.value.length != 8)
	{
		alert(msg);
		telephone.focus();
		telephone.select();
		return false;
	}
	
	for (i = 0; i < 3; i++)
	{
		if (isNaN(telephone.value.charAt(i)))
		{
			telephone.focus();
			telephone.select();
			alert(msg);
			return false;
		}
	}
	
	if (telephone.value.charAt(3) != "-")
	{
		telephone.focus();
		telephone.select();
		alert(msg);
		return false;
	}
	
	for (i = 4; i < 8; i++)
	{
		if (isNaN(telephone.value.charAt(i)))
		{
			telephone.focus();
			telephone.select();
			alert(msg);
			return false;
		}
	}
	return true;
}
function telephoneIsValid(telephone,texte)
{	
	msg = "Entrer un numéro de "+texte+" valide (999-9999 ou 999-999-9999).";

	// Vérifier les deux formats possibles soient 999-9999 ou 999-999-9999

	if (telephone.value.length != 8)
	{
		if (telephone.value.length != 12)
		{
			alert(msg);
			telephone.focus();
			telephone.select();
			return false;
		}
		// Code régionnal
		for (i = 0; i < 3; i++)
		{
			if (isNaN(telephone.value.charAt(i)))
			{
				telephone.focus();
				telephone.select();
				alert(msg);
				return false;
			}
		}
	
		// Séparateurs
		if ((telephone.value.charAt(3) != "-") ||
			(telephone.value.charAt(7) != "-"))
		{
			telephone.focus();
			telephone.select();
			alert(msg);
			return false;
		}

		// 1ere partie
		for (i = 4; i < 7; i++)
		{
			if (isNaN(telephone.value.charAt(i)))
			{
				telephone.focus();
				telephone.select();
				alert(msg);
				return false;
			}
		}
	
		// 2eme partie
		for (i = 8; i < 12; i++)
		{
			if (isNaN(telephone.value.charAt(i)))
			{
				telephone.focus();
				telephone.select();
				alert(msg);
				return false;
			}
		}
		// Numéro avec code régionnal valide
		return true;
	}
	
	for (i = 0; i < 3; i++)
	{
		if (isNaN(telephone.value.charAt(i)))
		{
			telephone.focus();
			telephone.select();
			alert(msg);
			return false;
		}
	}
	
	if (telephone.value.charAt(3) != "-")
	{
		telephone.focus();
		telephone.select();
		alert(msg);
		return false;
	}
	
	for (i = 4; i < 8; i++)
	{
		if (isNaN(telephone.value.charAt(i)))
		{
			telephone.focus();
			telephone.select();
			alert(msg);
			return false;
		}
	}
	// Numéro sans code régionnal valide
	return true;
}

function isEmail(field) 
{
	
  var str = field.value;
  str = trim(str);
  field.value = str;
  if (window.RegExp) 
  {
    var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
    var reg1 = new RegExp(reg1str);
    var reg2 = new RegExp(reg2str);
    if (!reg1.test(str) && reg2.test(str)) 
    {
      return true;
	}
    field.focus();
    field.select();
    alert("Vous devez entrer une adresse de courriel valide.");
    return false;
  } 
  else 
  {
    if ((str.indexOf("@") >= 0) && (str.indexOf(" ")))
      return true;
    field.focus();
    field.select();
    alert("Vous devez entrer une adresse de courriel valide.");
    return false;
  }
}

function isEmailEn(field) 
{
	
  var str = field.value;
  str = trim(str);
  field.value = str;
  if (window.RegExp) 
  {
    var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
    var reg1 = new RegExp(reg1str);
    var reg2 = new RegExp(reg2str);
    if (!reg1.test(str) && reg2.test(str)) 
    {
      return true;
	}
    field.focus();
    field.select();
    alert("Please enter a valid email adress.");
    return false;
  } 
  else 
  {
    if ((str.indexOf("@") >= 0) && (str.indexOf(" ")))
      return true;
    field.focus();
    field.select();
    alert("Please enter a valid email adress.");
    return false;
  }
}

function verifyIP (IPvalue) 
{
	errorString = "";
	theName = "L'adresse IP";


	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipPattern); 

	if (IPvalue == "0.0.0.0")
		errorString = errorString + theName + ": "+IPvalue+" est une adresse IP réservée.";
	else if (IPvalue == "255.255.255.255")
		errorString = errorString + theName + ": "+IPvalue+" est une adresse IP réservée.";
	if (ipArray == null)
		errorString = errorString + theName + ": "+IPvalue+" n'est pas une adresse IP valide.";
	else 
	{
		for (i = 1; i <= 4; i++) 
		{
			thisSegment = ipArray[i];
			//alert(thisSegment);
			if (thisSegment > 255) 
			{
				errorString = errorString + theName + ": "+IPvalue+" n'est pas une adresse IP valide.";
				i = 4;
			}
			if ((i == 0) && (thisSegment > 255)) 
			{
				errorString = errorString + theName + ": "+IPvalue+" est une adresse IP réservée.";
				i = 4;
			}
		}
	}
	extensionLength = 3;
	if (errorString != "")
	{
		alert(errorString);
		return false;
	}	
	
	return true;
		
}

function validDate(thedate)
{
	//If not 10 character with the space
	if (thedate.length != 10)
	{
		alert("Le format de la date est incorrect (AAAA-MM-JJ).");
		return false;
	}

   var parsedDate = thedate.split ("-");
   if (parsedDate.length != 3) 
   {	
		alert("Le format de la date est incorrect (AAAA-MM-JJ).");
		return false;
   }
   var day, month, year;
   year = parsedDate[0];
   month = parsedDate[1];
   day = parsedDate[2];


	if(date_validate(year,month,day)==false)
   {
		alert("La date est invalide.");
		return false;
   }
   	
   	return true
}

function date_validate(yyyy,mm,dd)
{
   var ok    = true;
   var maxDay = 0;

   // calling function to get maximum day for this month
   maxDay = max_day(mm, yyyy);  

   if((dd <= 0) || (dd > maxDay))
	{ ok = false;}
	else if((mm <= 0) || (mm > 12))
	{ ok = false;}
	else if((yyyy <= 0))
	{ ok = false;}

	
	return ok;
}
// function for calculating maximum day 
function max_day(mn, yr)
{
   var mDay;
   if((mn == 4) || (mn == 6) || (mn == 9) || (mn == 11))
   { 
     mDay = 30;
   }
	else if(mn == 2)
	{
	//calling leap year function 
	mDay = isLeapYear(yr) ? 29 : 28;    
	}
	else
	{
	mDay = 31;
	}
	return mDay; 
	}

    // function to check leap year
function isLeapYear(yr)
{
if ((yr % 4 == 0) &&((yr % 100 != 0) ||(yr % 400 == 0)    ))
   return true;
   return false;
}

function radioButtonIsChecked(radioObj) {
var i = 0;
var foundOneChecked = false
while (i < radioObj.length && !foundOneChecked) {
	if (radioObj[i].checked) {
		foundOneChecked= true
	}
	else {
		i++;
	}
}

return foundOneChecked;
}

function allRadioButtonsAreChecked(formObj) {
var myRadioButtonsNames = new Array();
var k = 0;
for (j = 0; j < formObj.length; j++) {
	if (formObj[j].type == "radio") {
		var foundRadioButtonName = false;
		var m = 0;
		while (!foundRadioButtonName && m < myRadioButtonsNames.length) {
			if (myRadioButtonsNames[m] == formObj[j].name) {
				foundRadioButtonName = true;
			}
			else {
				m++;
			}
		}

		if (!foundRadioButtonName) { //Ajoute seulement s'il n'a pas été trouvé.
			myRadioButtonsNames[k] = formObj[j].name;
			k++;
		}
	}
}

for (l = 0; l < myRadioButtonsNames.length; l++) {
	var i = 0;
	var foundOneChecked = false
	while (i < eval("document." + formObj.name +"." + myRadioButtonsNames[l] + ".length") && !foundOneChecked) {
		if (eval("document." + formObj.name + "." + myRadioButtonsNames[l] + "[i].checked")) {
			foundOneChecked= true
		}
		else {
			i++;
		}
	}

	if (!foundOneChecked) {
		alert("Vous devez choisir une réponse pour toutes les questions.");
		return false;
	}
}

return true;
}

function checkEnter(e, theForm){ 
	var characterCode 
		
	if(e && e.which){ 
		e = e
		characterCode = e.which 
	} else {
		e = event
		characterCode = e.keyCode 
	}
		
	if(characterCode == 13){				//if generated character code is equal to ascii 13 (if enter key)
		theForm.submit()							//submit the form
		return false 
	} else {
		return true 
	}
}
