<!--

//
// Copyright Notice
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2, or (at your option)
//    any later version.
//
//    library.js is distributed in the hope that it will be
//    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    General Public License for more details.
//
//


// Expressions régulières de test de longueur
var regExp8Chars = /^[0-9a-zA-Z]{8,}$/g;	// Accepte une chaine d'au moins 8 carctères alphanumeriques (pour un mot de passe par exemple).

// Expressions régulières de test de type de caractère
var regExpAlphanumeric = /^[0-9a-zA-Z]+$/g;					// Accepte une chaine alphanumérique
var regExpAlphanumericWithWhitespace = /^[0-9a-zA-Z ]+$/g;	// Accepte une chaine alphanumérique + ' '
var regExpAlphabetic = /^[a-zA-Z]+$/g;						// Accepte une chaine alphabétique
var regExpAlphabeticWithWhitespace = /^[a-zA-Z ]+$/g;		// Accepte une chaine alphabétique
var regExpNumeric = /^[0-9]+$/g;							// Accepte une chaine numérique

// Expressions régulières de test de type
var regExpInt = /^[0-9]+$/g;																			// Accepte une chaine de type 'int'
var regExpDouble = /^[-+]?[0-9]+(\.[0-9]+)?$/g;															// Accepte une chaine de type 'double'
var regExpFloat = /^[-+]?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?$/g;										// Accepte une chaine de type 'float'
var regExpTime = /^([01][0-9]|2[0123])\:([012345][0-9])(\:([012345][0-9])(.([0-9]{3})+)?)?$/g;			// Accepte une chaine de type 'time'. Ex : 12:51 ou 21:45:35.654
var regExpFrenchDate = /^(0[1-9]|[12][0-9]|3[01])[\- \/\.](0[1-9]|1[012])[\- \/\.](19|20)\d\d$/g;  		// date au format jj/mm/aaaa ou jj-mm-aaaa ou jj mm aaaa ou jj.mm.aaaa avec aaaa compris entre 1900 et 2099.
var regExpEnglishDate = /^(19|20)\d\d[\- \/\.](0[1-9]|1[012])[\- \/\.](0[1-9]|[12][0-9]|3[01])$/g; 		// idem ci-dessus mais format anglais (Ex : aaaa/mm/jj)
var regExpBoolean = /^true|false$/g;																	// Accepte une chaine de type 'boolean'

// Expressions régulières de test de types administratifs français
var regExpCodePostal = /^([A-Z]+[A-Z]?\-)?[0-9]{1,2} ?[0-9]{3}$/g;																	// Accepte une chaine de type 'code postal'. Ex : F-33370 ou 33 370 ou 33370 ou F-1 370
var regExpTelephoneFixe = /^(01|02|03|04|05)[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}$/g;					// Accepte un numero de téléphone de type 'fixe'. Ex : 01.34.12.52.30 ou 0134125230
var regExpTelephonePortable = /^(06)[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}$/g;							// Accepte un numero de téléphone de type 'portable'.
var regExpTelephoneNational = /^(0[1234568])[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}$/g;					// Accepte un numero de téléphone de type 'national' y compris numéros en '08'.
var regExpTelephoneInternational = /^(\+[0-9]{2})[ \.\-]?[0-9][ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}$/g;	// Accepte un numero de téléphone de type 'international'. Ex : (+33) 1 34 12 52 30
var regExpNumeroSecuriteSociale = /^[12][ \.\-]?[0-9]{2}[ \.\-]?(0[1-9]|[1][0-2])[ \.\-]?([0-9]{2}|2A|2B)[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{2}$/g; // Accepte un numero de sécurité sociale français. Ex : 1 85 34 33 354 450 45
var regExpTVAIntracommunautaire = /^[A-Z]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}$/g;					// Accepte un numero de TVA Intra-communautaire. Ex : FR 02 254 254 254
var regExpNumeroSiren = /^[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}$/g;																// Accepte un numero SIREN. Ex : 254 254 254
var regExpNumeroSiret = /^[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{5}$/g;												// Accepte un numero SIRET. Ex : 254 254 254 12345
var regExpCodeApe = /^[0-9]{2}[ \.\-]?[0-9]{1} ?[a-zA-Z]$/g;																		// Accepte un code APE. Ex : 25.4Z

// Expressions régulières de test de types liés à internet
var regExpEmailAdress = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,4})$/g;	// Accepte une adresse email. Ex : toto@toto.com
var regExpIpAdress = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/g; // Accepte une adresse ip. Ex : 192.168.0.1
var regExpDomainName = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/g;									// Accepte un nom de domaine. Ex : toto.com
var regExpUrl = /^(((ht|f)tp(s?))\:\/\/)?(([a-zA-Z0-9]+([@\-\.]?[a-zA-Z0-9]+)*)(\:[a-zA-Z0-9\-\.]+)?@)?(www.|ftp.|[a-zA-Z]+.)?[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,})(\:[0-9]+)?\/?/g; // Accepte une url ftp, http ou https, avec ou sans login/mot de passe, avec ou sans numero de port. Ex : http://www.toto.com, ftp://toto:toto@ftp.toto.com:21/
var regExpHexColor = /^#[0-9A-Fa-f]{6}$/g; 																					// Accepte une couleur hexadécimale


//------------------------------------------------------------------------------
// Fonctions de controle de saisie
//------------------------------------------------------------------------------
function matchRegularExpression(valeur, regularExpression) {
	var resultat = valeur.match(regularExpression);
	if (resultat !=null && resultat.length == 1) return true;
	else return false;
}

function doesntMatchRegularExpression(valeur, regularExpression) {
	if (matchRegularExpression(valeur, regularExpression)) return false;
	else return true;
}

function isNot8CharsLength(valeur) { return doesntMatchRegularExpression(valeur, regExp8Chars); }
function isNotAlphanumeric(valeur) { return doesntMatchRegularExpression(valeur, regExpAlphanumeric); }
function isNotAlphanumericWithWhitespace(valeur) { return doesntMatchRegularExpression(valeur, regExpAlphanumericWithWhitespace); }
function isNotAlphabetic(valeur) { return doesntMatchRegularExpression(valeur, regExpAlphabetic); }
function isNotAlphabeticWithWhitespace(valeur) { return doesntMatchRegularExpression(valeur, regExpAlphabeticWithWhitespace); }
function isNotNumeric(valeur) {	return doesntMatchRegularExpression(valeur, regExpNumeric); }
function isNotInt(valeur) {	return doesntMatchRegularExpression(valeur, regExpInt); }
function isNotDouble(valeur) { return doesntMatchRegularExpression(valeur, regExpDouble); }
function isNotFloat(valeur) { return doesntMatchRegularExpression(valeur, regExpInt); }
function isNotBoolean(valeur) {	return doesntMatchRegularExpression(valeur, regExpBoolean); }
function isNotTime(valeur) { return doesntMatchRegularExpression(valeur, regExpTime); }
function isNotCodePostal(valeur) { return doesntMatchRegularExpression(valeur, regExpCodePostal); }
function isNotNumeroSecuriteSociale(valeur) { return doesntMatchRegularExpression(valeur, regExpNumeroSecuriteSociale); }
function isNotTVAIntracommunautaire(valeur) { return doesntMatchRegularExpression(valeur, regExpTVAIntracommunautaire); }
function isNotNumeroSiren(valeur) { return doesntMatchRegularExpression(valeur, regExpNumeroSiren); }
function isNotNumeroSiret(valeur) { return doesntMatchRegularExpression(valeur, regExpNumeroSiret); }
function isNotCodeApe(valeur) { return doesntMatchRegularExpression(valeur, regExpCodeApe); }
function isNotEmailAdress(valeur) { return doesntMatchRegularExpression(valeur, regExpEmailAdress); }
function isNotIpAdress(valeur) { return doesntMatchRegularExpression(valeur, regExpIpAdress); }
function isNotDomainName(valeur) { return doesntMatchRegularExpression(valeur, regExpDomainName); }
function isNotUrl(valeur) { return doesntMatchRegularExpression(valeur, regExpUrl); }
function isNotHexColor(valeur) { return doesntMatchRegularExpression(valeur, regExpHexColor); }

function isNotDate(valeur, mode) {
	switch (mode) {
		case "fr":  
			return doesntMatchRegularExpression(valeur, regExpFrenchDate);
			break;
		case "en":
			return doesntMatchRegularExpression(valeur, regExpEnglishDate);
			break;
		default: 
			return doesntMatchRegularExpression(valeur, regExpFrenchDate);
			break;
	}
}

function isNotTelephone(valeur, mode) {
	switch (mode) {
		case "fixe": 
			return doesntMatchRegularExpression(valeur, regExpTelephoneFixe);
			break;
		case "port":
			return doesntMatchRegularExpression(valeur, regExpTelephonePortable);
			break;
		case "nati":
			return doesntMatchRegularExpression(valeur, regExpTelephoneNational);
			break;
		case "inte":
			return doesntMatchRegularExpression(valeur, regExpTelephoneInternational);
			break;
		default: 
			return doesntMatchRegularExpression(valeur, regExpTelephoneNational);
			break;
	}
}

function isValidDate(d) {
	// Verification du format jj/mm/aa ou jj/mm/aaaa
	if (d == '') return false;
	er = /^[0-9]{1,2}\/[0-9]{1,2}\/([0-9]{2}|[0-9]{4})/;
	if (!er.test(d)) return false;

	// Recuperation des valeurs numeriques
	jj   = parseInt(d.split('/')[0], 10);
	mm   = parseInt(d.split('/')[1], 10);
	aaaa = parseInt(d.split('/')[2], 10);

	// Si annee sur deux chiffres ajout du siecle avec fenetrage an 2000
	if (aaaa < 1000) {
		if (aaaa < 90)  aaaa += 2000; else aaaa += 1900;
	}

	// Gestion annee bixestile
	if ((((aaaa % 4) == 0) && ((aaaa % 100) != 0)) || ((aaaa % 400) == 0)) fev = 29; else fev = 28;
	nbJours = new Array(31, fev, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	// Verification de la validite du jour et du mois
    return ((mm >= 1) && (mm <=12) && (jj >= 1) && (jj <= nbJours[mm - 1]));
}


//------------------------------------------------------------------------------
// Fonctions de manipulation de chaine
//------------------------------------------------------------------------------
function leftTrim(sString) {
	while (sString.substring(0, 1) == ' ') sString = sString.substring(1, sString.length);
	return sString;
}

function rightTrim(sString) {
	while (sString.substring(sString.length - 1, sString.length) == ' ') sString = sString.substring(0, sString.length - 1);
	return sString;
}

function allTrim(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;
}


//------------------------------------------------------------------------------
// Fonctions de conversions
//------------------------------------------------------------------------------
function Dec2RGB(value) {
	var hex_string = "";
	
  	for (var hexpair = 0; hexpair < 3; hexpair++) {
    	var myByte = value & 0xFF;       		// get low byte
    	value >>= 8;                        	// drop low byte
    	var nybble2 = myByte & 0x0F;          	// get low nybble (4 bits)
    	var nybble1 = (myByte >> 4) & 0x0F;   	// get high nybble
    	hex_string += nybble1.toString(16); 	// convert nybble to hex
    	hex_string += nybble2.toString(16); 	// convert nybble to hex
  	}
	
  	return hex_string.toUpperCase();
}


//------------------------------------------------------------------------------
// Fonctions de manipulation d'objets HTML
//------------------------------------------------------------------------------
function getRadioValue(arrGroup) {
   var result = '';
   var g = arrGroup;
   
   for (var i = 0; i < g.length; i++) {
      if (g[i].checked) {
         result = g[i].value;
         break;
      }
   }
   
   return result;
}

var marked_row = new Array;
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor) {
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
}


//------------------------------------------------------------------------------
// Fonctions de manipulation d'objets HTTP
//------------------------------------------------------------------------------
function createHTTPObject() {
	var objHTTP = null;
	
	if (typeof XMLHttpRequest != 'undefined') {
		try {
			// Native XML support (Firefox, etc...)
			objHTTP = new XMLHttpRequest();
		}
		catch (e1) {}
	}
	else {
    	try {
			// Internet Explorer support (new library)
			objHTTP = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e2) {
			try {
				// Internet Explorer support (old library)
        		objHTTP = new ActiveXObject("Microsoft.XMLHTTP");
        	}
			catch (e3) {}
        }
	}

	return objHTTP;
}


//-->