function getHTTPObject() {
	var xmlhttp;
	if (navigator.appName=="Microsoft Internet Explorer")
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		//il faut remplacer "Microsoft.XMLHTTP" par "Msxml2.XMLHTTP.5.0" pour eviter l'erreur system : 1072896748 
		//pour les actentuées : test réussi est validé		
	else 	
		var xmlhttp = new XMLHttpRequest();  

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
      } catch (e) {
      	xmlhttp = false;
      }
    }
  return xmlhttp;
}

var http = getHTTPObject();

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

    // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0)
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    // 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;
            marked_row[theRowNum] = true;
            // Garvin: deactivated onclick marking of the checkbox because it's also executed
            // when an action (like edit/delete) on a single item is performed. Then the checkbox
            // would get deactived, even though we need it activated. Maybe there is a way
            // to detect if the row was clicked, and not an item therein...
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 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;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = 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;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = false;
        }
    } // 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;
} // end of the 'setPointer()' function



function setCheckboxColumn(theCheckbox){
    if (document.getElementById(theCheckbox)) {
        document.getElementById(theCheckbox).checked = (document.getElementById(theCheckbox).checked ? false : true);
        if (document.getElementById(theCheckbox + 'r')) {
            document.getElementById(theCheckbox + 'r').checked = document.getElementById(theCheckbox).checked;
        }
    } else {
        if (document.getElementById(theCheckbox + 'r')) {
            document.getElementById(theCheckbox + 'r').checked = (document.getElementById(theCheckbox +'r').checked ? false : true);
            if (document.getElementById(theCheckbox)) {
                document.getElementById(theCheckbox).checked = document.getElementById(theCheckbox + 'r').checked;
            }
        }
    }
}


function isExtsAllowed(path) {
	var lastDot = path.lastIndexOf("."); //index of last dot, the extension will follow this dot
	if(lastDot != 1) { //if we found a dot, that means we have an extension
		var fileExts = path.substring(lastDot + 1, path.length).toLowerCase();
		//loop through array of allowed extensions and check if this one is allowed
		for(var i = 0; i < allowedExts.length; i++) {
			//alert (fileExts);
			if(fileExts == allowedExts[i]) //if the extension is in the list of allowed
				return true;
		}
	}
	
	return false;
}


function messages(champs,libelle){
	if (document.getElementById(champs).value==""){
		document.getElementById('id_validation').style.visibility='visible'
		document.getElementById('id_validation').innerHTML="Le champs "+libelle+" ne doit pas être une chaine vide !"
		document.getElementById(champs).focus()
		return false
	}else{
		document.getElementById('id_validation').style.visibility='hidden'
		return true
	}
}

function adad(champs,libelle){
	if (isNaN(document.getElementById(champs).value)){
		document.getElementById('id_validation').style.visibility='visible'
		document.getElementById('id_validation').innerHTML="Le champs "+libelle+" n'est pas une valuer numérique !"
		document.getElementById(champs).focus()
		return false
	}else{
		document.getElementById('id_validation').style.visibility='hidden'
		return true
	}
}

function checkdate(champs,libelle){
	if(!isDate(document.getElementById(champs).value)  ) {
		document.getElementById('id_validation').style.visibility='visible'
		document.getElementById('id_validation').innerHTML="Le format de la date du champs "+libelle+" n'est pas valide (ex: 31-02-2007) !"
		document.getElementById(champs).focus()
		return false
	}else{
		document.getElementById('id_validation').style.visibility='hidden'
		return true
	}
}

function isDate(d) {
	// Cette fonction permet de vérifier la validité d'une date au format jj/mm/aa ou jj/mm/aaaa
	// Par Romuald
	
	if (d == "") // si la variable est vide on retourne faux
		return false;
	
	e = new RegExp("^[0-9]{1,2}\-[0-9]{1,2}\-([0-9]{2}|[0-9]{4})$");
	
	if (!e.test(d)) // On teste l'expression régulière pour valider la forme de la date
		return false; // Si pas bon, retourne faux

	// On sépare la date en 3 variables pour vérification, parseInt() converti du texte en entier
	j = parseInt(d.split("-")[0], 10); // jour
	m = parseInt(d.split("-")[1], 10); // mois
	a = parseInt(d.split("-")[2], 10); // année

	// Si l'année n'est composée que de 2 chiffres on complète automatiquement
	if (a < 1000) {
		if (a < 89)	a+=2000; // Si a < 89 alors on ajoute 2000 sinon on ajoute 1900
		else a+=1900;
	}

	// Définition du dernier jour de février
	// Année bissextile si annnée divisible par 4 et que ce n'est pas un siècle, ou bien si divisible par 400
	if (a%4 == 0 && a%100 !=0 || a%400 == 0) fev = 29;
	else fev = 28;

	// Nombre de jours pour chaque mois
	nbJours = new Array(31,fev,31,30,31,30,31,31,30,31,30,31);

	// Enfin, retourne vrai si le jour est bien entre 1 et le bon nombre de jours, idem pour les mois, sinon retourn faux
	return ( m >= 1 && m <=12 && j >= 1 && j <= nbJours[m-1] );
}


function changeImages(name,image) {
	document.images[name].src=image
}

function mailtoo(email){    
	chaine_mail = "mailto:"+email+"?subject=" + document.title;      
	chaine_mail += "&body="; 
	chaine_mail += ""; location.href = chaine_mail;     
} 

function DonnerFocus(nom) {
   document.forms[0].elements[nom].focus();
}

function datecomp(date1,date2){
   //vérification du format -  si la date est saisie je commence le travail, ce si se termine à la fin
   datedebut=document.getElementById(date1).value
   datefin=document.getElementById(date2).value
   if (datedebut!="") {

	//création d'un tableau ou je range la date début dans trois cases, si il y a des zero je les supprimme
	deb = new Array(3); 
	if (datedebut.substring(0,1)=="0"){
		deb[1]=parseInt(datedebut.substring(1,2));
	}else {
		deb[1]=parseInt(datedebut.substring(0,2));
	}
	if (datedebut.substring(3,4)=="0"){
		deb[2]=parseInt(datedebut.substring(4,5));
	}else {
		deb[2]=parseInt(datedebut.substring(3,5));
	}
	deb[3]=parseInt(datedebut.substring(6,10));
   
   //si la datefin contient 1 on vient du formulaire dateresadebut donc on compare avec la date du jour
   //dans ce cas on récupère un format de date javascript, on le transforme et on le range dans un tableau
   //je ne transforme pas la variable datfin que je réutilise plus loin
	fin = new Array(3); 
	if (datefin.substring(0,1)=="0"){
		fin[1]=parseInt(datefin.substring(1,2));
	}else {
		fin[1]=parseInt(datefin.substring(0,2));
	}
	if (datefin.substring(3,4)=="0"){
		fin[2]=parseInt(datefin.substring(4,5));
	} else {
		fin[2]=parseInt(datefin.substring(3,5));
	}
	fin[3]=parseInt(datefin.substring(6,10));
	var message="La date de fin restitution ne peut être infèrieure à la date de prise en charge";

   //comparaison des dates 
	if (deb[3]>fin[3]) {
		document.getElementById('id_validation').style.visibility='visible'
		document.getElementById('id_validation').innerHTML=message
		document.getElementById(date2).focus()
		return false;
	}else{
		if ((deb[3]== fin[3]) && (deb[2]>fin[2])){
			document.getElementById('id_validation').style.visibility='visible'
			document.getElementById('id_validation').innerHTML=message
			document.getElementById(date2).focus()
			return false;
		}else{
			if ((deb[3]== fin[3]) && (deb[2]==fin[2]) && (deb[1]>fin[1])){
				document.getElementById('id_validation').style.visibility='visible'
				document.getElementById('id_validation').innerHTML=message
				document.getElementById(date2).focus()
				return false;
			}
		}
	}
	document.getElementById('id_validation').style.visibility='hidden'
	return true;
   }
}

var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

function checkmail(champs,libelle){
	if ( !filter.test(document.getElementById(champs).value) ){
		document.getElementById('id_validation').style.visibility='visible'
		document.getElementById('id_validation').innerHTML="L'adresse email du champ "+libelle+" n'est pas valide "
		document.getElementById(champs).focus()
		return false
	}else{
		document.getElementById('id_validation').style.visibility='hidden'
		return true
	}
}
