
var ST_HUBERT_SHORT = 2;
var ST_HUBERT = 3;
var LOCAL = 4;
var NORTH_AMERICA = 5;
var INTERNATIONAL = 1;
var ERROR = 0; 
function TestFormat(strTelephone)
{
	var strFormat1, strFormat2, strFormat3, strFormat4
	strFormat1 = /^\s*\({0,1}\s{0,1}\d{3}\s{0,1}\){0,1}\s{0,1}-{0,1}\.{0,1}\d{3}\s{0,1}-{0,1}\.{0,1}\s{0,1}\d{4}\s*$/;
	strFormat2 = /^\s*\d{3}\s{0,1}-{0,1}\.{0,1}\s{0,1}\d{4}\s*/;
	strFormat3 = /^\s*6\s{0,1}\.{0,1}-{0,1}\s{0,1}\d{4}\s*/;
	strFormat4 = /^\s*\d{4}\s*/;
	if (checkInternationalPhone(strTelephone))
		if (strTelephone.match(strFormat1) != null)
			return NORTH_AMERICA;
		else
			return INTERNATIONAL;
	if (strTelephone.match(strFormat2))
		return LOCAL;
	if (strTelephone.match(strFormat3))
		return ST_HUBERT_SHORT;
	if (strTelephone.match(strFormat4))
		return ST_HUBERT;
	return ERROR;
}
function isInteger(strTmp)
{   var i;
	   for (i = 0; i < strTmp.length; i++)
   {
        // Check that current character is number.
        var c = strTmp.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function isDouble(strTmpPar){ 
    var reg = new RegExp(",","gi");
    var strTmp = strTmpPar.replace(reg, ".");
    var strFormat;
    strFormat=/([0-9]{1,2})|([0-9]{1,2}\.[0-9]{1})|(\.[0-9]{1})/;
    if  (!strTmp.match(strFormat) ){return false;}
    for (var i = 0; i < strTmp.length; i++)
    {
         // Check that current character is number.
         var c = strTmp.charAt(i);
         if (c !=".")
            if (((c < "0" && c !=".") || (c > "9" && c !="."))){return false;}
    }
         var nombre;
         nombre =  strTmp.split(".");
         // nombre de . dans la chaine de caractère
         if (nombre.length > 2)
             {return false;}
         // deux digits seulement dans la partie entière
         var entier;
         entier = nombre[0];
         if (entier.length > 2)
             {return false;}
         // un digits seulement dans la partie fractionnaire
        var fractionnaire;
        fractionnaire = nombre[1];
        if (fractionnaire != null )
        	if (fractionnaire.length > 1 )
            		{return false;}

     return true;
}

function isAlpha(strTmp){
    var NotNum = false;
    var NotLowAlpha = false;
    var NotUppAlpha = false;
   for (var i = 0; i < strTmp.length; i++)
    {
         // Check that current character is number.
         NotNum = false;
         NotLowAlpha = false;
         NotUppAlpha = false;
         var c = strTmp.charCodeAt(i);
         if ((c < 48 ) || (c > 57 ) ){NotNum = true; }
         if ((c < 97 ) || (c > 122 )) {NotLowAlpha = true;  }
         if ((c < 65 ) || (c > 90 )) {NotUppAlpha = true;}
         if (NotNum && NotLowAlpha && NotUppAlpha && c != 32)  return false;
    }
     return true;
}

function stripCharsInBag(strTmp, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < strTmp.length; i++)
    {   
        // Check that current character isn't whitespace.
       var c = strTmp.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function checkInternationalPhone(strPhone)
{
	var strValidWorldPhoneChars = "()- .+";
	var intMinDigitsInIPhoneNumber = 10;
	var intMaxDigitsInIPhoneNumber = 20;
	var strTmp=stripCharsInBag(strPhone,strValidWorldPhoneChars);
	return (isInteger(strTmp) && strTmp.length >= intMinDigitsInIPhoneNumber && strTmp.length < intMaxDigitsInIPhoneNumber);
}
	

/************************************************************************************************
 Appel la fonction validTelephone
 La condition du Switch utilise le numéro de format que retourne la fontion validTelephone.
 Si la fonction validTelephone retourne un numéro de format, CheckTelephone enlève tous les
 caractères non numériques, concaténe les valeurs numériques avec ()- pour créer un format 
 (999) 999-9999 et retourne la valeur créée dans la boîte de texte du formulaire.
 Si la fonction validTelephone retourne "False", la fonction CheckTelephone retourne un message 
 d'alerte avec le focus sur la boîte de texte.
 ************************************************************************************************/

function CheckTelephone(objTelField, strAlertMessage,strAcceptFormatList)
{
	var  strNonValid, strPhone, Format1, Format2;
	var strTelephoneValue;	
	
	strTelephoneValue = objTelField.value;
	Format1 = /-|\s|\.|\(|\)/g;
	Format2 = /-|\s|\./g;
	var intTestFormat = TestFormat(strTelephoneValue)
	if (strAcceptFormatList.indexOf(intTestFormat) >= 0)
	{
		if(intTestFormat == NORTH_AMERICA)
		{
			strTelephoneValue = strTelephoneValue.replace(Format1, "");
			strPhone = "(" + strTelephoneValue.substring(0,3) + ") " + strTelephoneValue.substring(3,6) + "-" + strTelephoneValue.substring(6,10);
			objTelField.value = strPhone;
			return true;
		}
		else if (intTestFormat == LOCAL)
		{
			strTelephoneValue = strTelephoneValue.replace(Format2, "");
			strPhone = strTelephoneValue.substring(0,3) + "-" + strTelephoneValue.substring(3,7);
			objTelField.value = strPhone;
			return true;
		}
		else if (intTestFormat == ST_HUBERT)
		{
			strTelephoneValue = strTelephoneValue.replace(Format2, "");
			strPhone = "(450) 926-" + strTelephoneValue.substring(1,5);
			objTelField.value = strPhone;
			return true;
		}
		else if (intTestFormat == ST_HUBERT_SHORT)
		{
			strPhone = "(450) 926-" + strTelephoneValue ;
			objTelField.value = strPhone;
			return true;
		}
		else if (intTestFormat == INTERNATIONAL)
		{
			strPhone = stripCharsInBag(strTelephoneValue ,"()- .+");
			objTelField.value = strPhone;
			return true;
		}
		else
		{
			if (strAlertMessage != "")
			{
				alert(strAlertMessage);
				objTelField.focus();
			}
			return false;
		}
			
	}
	else
	{
		if (strAlertMessage != "")
		{
			alert(strAlertMessage);
			objTelField.focus();
		}
		return false;
	} // End If
	return true;
}
