// ===================================================
// 	Bibliothèque de fonctions JS de portée générale
// ===================================================
//
// function Text2Html(sText)
// function LoadLogonData ()
// function SaveLogonData()
// function LTrim (theText) // Suppression des espaces de gauche
// function RTrim (theText) // Suppression des espaces de droite
// function Trim (theText) // Suppression des espaces de gauche et droite
// function Encrypt(theText) // Fonction de cryptage
// function unEncrypt(theText) // Fonction de decryptage
// function isSpecial(CAR) // est un caractère spécial ?
// function isA(CAR) // est un caractère [a..z] | [A..Z] 
// function isC(CAR) // Est un caractère Ascii 128 - (special && sp) 
// function isD(CAR) // est un Chiffre [0..9]
// function isAD(CAR) // est un alphanumérique
// function isAlphaNum(STR)
// function isNum(STR)
// function isString(STR)
// function isDotString(STR)
// function isLocalPart(STR)
// function isLetDigHyp(CAR)
// function isName(STR)
// function isDomaine(STR)
// function testEmail(chaine)
// function getCookieVal (offset)
// function GetCookie (name)
// function SetCookie (name, value)
//
// ===================================================

function Text2Html(sText)
{
   var sResult = new String;
   
   iLen = sText.length;
   
   for (var i = 0; i < iLen; i++)
   {
      switch (sText.charCodeAt(i))
      {
         case 13:
            if ((i != iLen) && (sText.charCodeAt(i + 1) == 10))
               sResult += "<br>";
         case 10:
            break;
         default:
            sResult += sText.charAt(i);
      }
   }
   
   return sResult;
}

function LoadLogonData ()
{
	var sLogin;
  
   if(!(sLogin = GetCookie("Login"))) 
      sLogin = "";
       
   with (document.forms[0]) 
      clientlogin.value = Trim(sLogin);
   
	return null;
}

function SaveLogonData()
{
	var expdate = new Date();
	
	with (document.forms[0])  
	{
	   expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 31)); 
	   SetCookie ("Login", Trim(clientlogin.value), expdate);
	}
	
	return null;
}

// Suppression des espaces de gauche
function LTrim (theText)
{
   var sResult = new String;
   var iLen = theText.length;
   var iNbSpaces = 0;
   
   for (var i = 0; i < iLen; i++)
      if (theText.charAt(i) == " ")
         iNbSpaces++;
      else
      	break;
      	
   sResult = theText.substr (iNbSpaces, iLen - iNbSpaces);
   
   return sResult;
}

// Suppression des espaces de droite
function RTrim (theText)
{
   var sResult = new String;
   var iLen = theText.length;
   var iNbSpaces = 0;
   
   for (var i = iLen - 1; i >= 0; i--)
      if (theText.charAt(i) == " ")
         iNbSpaces++;
      else
      	break;
      	
   sResult = theText.substr (0, iLen - iNbSpaces);
   
   return sResult;
}

// Suppression des espaces de gauche et droite
function Trim (theText)
{
   return RTrim(LTrim(theText));
}


// Fonction de cryptage
function Encrypt(theText)
{
   var output = new String;
   var Temp = new Array();
   var Temp2 = new Array();
   var TextSize = theText.length;
   
   for (var i = 0; i < TextSize; i++)
   {
		rnd = Math.round(Math.random() * 122) + 68;
		Temp[i] = theText.charCodeAt(i) + rnd;
		Temp2[i] = rnd;
	}
	
	for (var i = 0; i < TextSize; i++)
		output += String.fromCharCode(Temp[i], Temp2[i]);

	return output;
}

 // Fonction de decryptage
function unEncrypt(theText)
{
	var output = new String;
	var Temp = new Array();
	var Temp2 = new Array();
	var TextSize = theText.length;
	
	for (i = 0; i < TextSize; i++)
	{
		Temp[i] = theText.charCodeAt(i);
		Temp2[i] = theText.charCodeAt(i + 1);
	}
	
	for (i = 0; i < TextSize; i = i+2) 
		output += String.fromCharCode(Temp[i] - Temp2[i]);
	
	return output;
}


// est un caractère spécial ?
function isSpecial(CAR)
{
   // La liste des caractères spéciaux
   var Special = new Array("<",">","(",")","[","]","\\",".",",",";",":","#","'","\"")
   
   for(var IndSpe=0;IndSpe < Special.length ; IndSpe++)
		if (CAR == Special[IndSpe])
      	return true;
   
   return false; 
}

function isAccent(CAR)
{
   return (CAR.charCodeAt(0) <= 254) && (CAR.charCodeAt(0) >= 224) 
}

// est un caractère [a..z] | [A..Z] 
function isA(CAR)
{   
   return ((CAR >= "a") && (CAR <= "z")) || ((CAR >= "A") && (CAR <= "Z"));
}


// Est un caractère Ascii 128 - (special && sp) ?
function isC(CAR)
{
   return (((CAR.charCodeAt(0) <= 154) && (CAR.charCodeAt(0) >= 32)) || isAccent(CAR) )  && !isSpecial(CAR); 
}


function isD(CAR)
{
   return (CAR >= "0") && (CAR <= "9");
}

// est un alphanumérique
function isAD(CAR)
{
   return isA(CAR) || isD(CAR);
}

function isAlphaNum(STR)
{
	// vérifie chaque caractère de la chaine STR
	for (var IndStr=0; IndStr < STR.length ; IndStr++)
		if (!isAD(STR.charAt(IndStr)) && !(STR.charAt(IndStr) == "-")) 
         return false;
	
   return true;
}	

function isNum(STR)
{
	// vérifie chaque caractère de la chaine STR
	for (var IndStr=0; IndStr < STR.length ; IndStr++)
		if (!(isD(STR.charAt(IndStr))) ) 
         return false;
	
   return true;
}	


// est une Chaine de caractères avec espace
function isString(STR)
{
   // vérifie chaque caractère de la chaine STR
	for (var IndStr=0; IndStr < STR.length ; IndStr++)
		if (!(isC(STR.charAt(IndStr))) ) 
         return false;
	
   return true;
}


function isDotString(STR)
{
   var POINT = STR.indexOf(".");
   
   if (POINT > 0)
   {
      var LeftPart = STR.slice(0,POINT);
   	var RightPart = STR.slice(POINT + 1,STR.length);
   	
      return (isString(LeftPart)) && (isDotString(RightPart));
  	}
  	else
   	return isString(STR);
}

// est LocalPart
function isLocalPart(STR)
{
  return isDotString(STR);
}

function isLetDigHyp(CAR)
{
   return isAD(CAR) || (CAR == "-");
}

function isName(STR)
{
   // Vérifie que le 1er caractère de la chaine sont des [A..Z] ou [a..z]
   //if (!isA( STR.charAt(0))) 
   //   return false;
      
   // Vérifie que les caractères suivants sont des [a..z] || [A..Z] || [0..9] || "-"
   for (var IndName=1 ; IndName < STR.length ; IndName++)
      if (!isLetDigHyp(STR.charAt(IndName)))
         return false;
  
  return true;            
}

// est Domaine
function isDomaine(STR)
{
  var POINT = STR.indexOf(".")

  if (POINT > 0)
  {
	  var LeftPart = STR.slice(0,POINT);
     var RightPart = STR.slice(POINT + 1 , STR.length); 
      
     return isName(LeftPart) && (isDomaine(RightPart) || isString(RightPart));
  }
  else 
     return false;
}

function testEmail(chaine)
{	
	var Arobace = chaine.indexOf("@");
	
	chaine = chaine.toLowerCase();
	
	// Commence les tests : Arobace présente et pas en premier
	if (Arobace > 0)
	{
   	var LocalPart = chaine.slice(0,Arobace);
   	var Domaine = chaine.slice(Arobace + 1, chaine.length);
   	
   	return isLocalPart(LocalPart) && isDomaine(Domaine);
   }
	else
	   return false;
}

function getCookieVal (offset)
{
   var endstr = document.cookie.indexOf (";", offset);
   
   if (endstr == -1)
      endstr = document.cookie.length;
   
   return unescape(document.cookie.substring(offset, endstr));
}


function GetCookie (name)
{
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   
   while (i < clen)
   {
      var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		
		i = document.cookie.indexOf(" ", i) + 1;
		
		if (i == 0)
			break; 
	}
	
	return null;
}

  
function SetCookie (name, value)
{	
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}



	
