function showSearchCriterias(){
	var divCriterias = document.getElementById('DIV_CRITERIAS');
	if(divCriterias.style.display == 'none' || divCriterias.style.display == ''){
		divCriterias.style.display = 'block';
	} else {
		divCriterias.style.display = 'none';
	}
}

function showSearchRules(){
	var divRules = document.getElementById('DIV_RULES');
	if(divRules.style.display == 'none' || divRules.style.display == ''){
		divRules.style.display = 'block';
	} else {
		divRules.style.display = 'none';
	}
}

function getX(r){
  return getOffset(r,"offsetLeft")
}

function getY(r){
  return getOffset(r,"offsetTop")
}

function getOffset(element,attr){
  var offset=0;
  while(element){
    offset+=element[attr];
    element=element.offsetParent
  }
  return offset
}

// change l'etat de tous les checkbox d'un formulaire dont l'identifiant est passe en parametre
function fillListField(id, etat) {

	var checkboxs = document.getElementById( id ).getElementsByTagName( "input" );

	for ( compteur=0; compteur<checkboxs.length; compteur++) {
		if ( checkboxs[compteur].nodeName == "INPUT" ) {
			if ( checkboxs[compteur].getAttribute( "type" ) == "checkbox" ) {
				checkboxs[compteur].checked = etat;
			}
		}
	}
}

// change l'etat de tous les checkbox d'un formulaire dont l'identifiant est passe en parametre
function fillListField(id, etat) {

	var checkboxs = document.getElementById( id ).getElementsByTagName( "input" );

	for ( compteur=0; compteur<checkboxs.length; compteur++) {
		if ( checkboxs[compteur].nodeName == "INPUT" ) {
			if ( checkboxs[compteur].getAttribute( "type" ) == "checkbox" ) {
				checkboxs[compteur].checked = etat;
			}
		}
	}
}

// maqsque pour les dates
function MasqueSaisieDate(obj, pos) {
	if ( obj.value != '' ) {
		DateOrHeure(obj,"/", pos);
	}
}

function DateOrHeure(obj,sep, pos){
	var ch;
	var ch_gauche, ch_droite, ch_initial;
	var first_separator, second_separator;
	var day, month, year;
	ch = obj.value;
	ch.toString();
	
	// initialise la chaine
	ch_initial = '';
	while ( ch_initial != ch ) {
		ch_initial = ch;
		ch = ch.replace("//","/");
		ch = ch.replace("--","-");
		ch = ch.replace("  "," ");
	}	// détermine la position du premier séparateur
	first_separator = ch.indexOf("/", 0);
	if ( ( ( first_separator > ch.indexOf("-", 0) ) && ( ch.indexOf("-", 0) != -1 ) ) || ( first_separator == -1 ) ) {
		first_separator = ch.indexOf("-", 0);
	}
	if ( ( ( first_separator > ch.indexOf(" ", 0) ) && ( ch.indexOf(" ", 0) != -1 ) ) || ( first_separator == -1 ) ) {
		first_separator = ch.indexOf(" ", 0);
	}
	
	// détermine la position du second séparateur
	if ( first_separator != -1 ) {
		second_separator = ch.indexOf("/", first_separator+1);
		if ( ( ( second_separator > ch.indexOf("-", first_separator+1) ) && ( ch.indexOf("-", first_separator+1) != -1 ) ) || ( second_separator == -1 ) ) {
			second_separator = ch.indexOf("-", first_separator+1);
		}
		if ( ( ( second_separator > ch.indexOf(" ", first_separator+1) ) && ( ch.indexOf(" ", first_separator+1) != -1 ) ) || ( second_separator == -1 ) ) {
			second_separator = ch.indexOf(" ", first_separator+1);
		}
	} else {
		second_separator = -1;
	}
	


	// s'il n'y a pas de séparateur on traite la donnée comme une année (yyyy) en ajoutant au début le mois et le jour (dd/mm/)
	if ( first_separator == -1 ) {
		year = ch;
		if ( year.length > 4 ) {
			year = year.slice(0,4);
		} else {
			for (i = year.length; i < 4; i++) {
				year = '0' + year;
			}
		}
		if ( pos == 'to' ) {
			ch = '31/12/' + year;
		} else {
			ch = '01/01/' + year;
		}
	} else {
		// s'il y a un seul séparateur on traite la donnée comme une année et un mois (mm/yyyy)  en ajoutant au début le jour (dd/)
		if ( second_separator == -1 ) {
			month = ch.slice(0, first_separator );
			year = ch.slice(first_separator+1, ch.length);
			
			// vérifie que le mois est bien entre 1 et 12
			if ( month > 12) {
				month = 12;
			}
			if ( month < 1 ) {
				month = '01';
			}
			
			// met l'année sur 4 caractères
			if ( year.length > 4 ) {
				year = year.slice(0,4);
			} else {
				for (i = year.length; i < 4; i++) {
					year = '0' + year;
				}
			}
			if ( pos == 'to' ) {
				if ( ( month == 1 ) || ( month == 3 ) || ( month == 5 ) || ( month == 7 ) || ( month == 8 ) || ( month == 10 ) || ( month == 12 ) ) {
					ch = '31/' + month + '/' + year;
				} else if ( ( month == 4 ) || ( month == 6 ) || ( month == 9 ) || ( month == 11 ) ) {
					ch = '30/' + month + '/' + year;
				} else if ( month == 2 ) {
					if ( year % 4 == 0 ) {
						ch = '29/' + month + '/' + year;
					} else {
						ch = '28/' + month + '/' + year;
					}
				}
			} else {
				ch = '01/' + month + '/' + year;
			}
		} else {
			// s'il y a deux séparateurs on traite la donnée comme une année, un mois et un jour(dd/mm/yyyy) 
			day = ch.slice(0, first_separator );
			month = ch.slice(first_separator+1, second_separator);
			year = ch.slice(second_separator+1, ch.length);
			
			// vérifie que le mois est bien entre 1 et 12
			if ( month > 12) {
				month = 12;
			}
			if ( month == 0) {
				month = '01';
			}
			
			// met l'année sur 4 caractères
			if ( year.length > 4 ) {
				year = year.slice(0,4);
			} else {
				for (i = year.length; i < 4; i++) {
					year = '0' + year;
				}
			}
			if ( ( month == 1 ) || ( month == 3 ) || ( month == 5 ) || ( month == 7 ) || ( month == 8 ) || ( month == 10 ) || ( month == 12 ) ) {
				if ( day > 31 ) {
					day = 31;
				}
			} else if ( ( month == 4 ) || ( month == 6 ) || ( month == 9 ) || ( month == 11 ) ) {
				if ( day > 30 ) {
					day = 30;
				}
			} else if ( month == 2 ) {
				if ( year % 4 == 0 ) {
					if ( day > 29 ) {
						day = 29;
					}
				} else {
					if ( day > 28 ) {
						day = 28;
					}
				}
			}
			if ( day == 0 ) {
				day = '01';
			}
			ch = day + '/' + month + '/' + year;
		}
	}
	obj.value = ch;

	return;
}

function DateForbiddenCharacter(obj) {
	// initialisation de la chaine de caractere de la boite de saisie et initialisation des variables temporaires
	var chaine = obj.value;
	var positionCurseur = getSelectionStart(obj);
	var repositionCurseur = chaine.length - getSelectionStart(obj);
	var i, countSlash;
	
	newChaine = '';
	countSlash = 0;
	for (i=0; i < chaine.length; i++) {
		
		if ( ( ( chaine.charAt(i) >= 0 ) && ( chaine.charAt(i) <= 9 ) ) || ( chaine.charAt(i) == '/' ) || ( chaine.charAt(i) == ' ' ) || ( chaine.charAt(i) == '-' ) ) {
		
			if ( ( chaine.charAt(i) == '/' ) || ( chaine.charAt(i) == ' ' ) || ( chaine.charAt(i) == '-' ) ) {
				countSlash = countSlash + 1;
			}
			if ( countSlash < 3) {
				newChaine += chaine.charAt(i);
			}
		}
	}
	obj.value = newChaine;
	setSelectionStart(obj, newChaine.length - repositionCurseur);
}

function getSelectionStart(objet) {
	if ( typeof objet.selectionStart != 'undefined' ) {
		return objet.selectionStart;
	}
	
	// IE Support
	objet.focus();
	var range = objet.createTextRange();
	range.moveToBookmark(document.selection.createRange().getBookmark());
	range.moveEnd('character', objet.value.length);
	return objet.value.length - range.text.length;
}

function getSelectionEnd(objet) {
	if ( typeof objet.selectionEnd != 'undefined' ) {
		return objet.selectionEnd;
	}
 
	// IE Support
	objet.focus();
	var range = objet.createTextRange();
	range.moveToBookmark(document.selection.createRange().getBookmark());
	range.moveStart('character', - objet.value.length);
	return range.text.length;
}

function setSelectionStart(objet, position) {
	if ( typeof objet.selectionStart != 'undefined' ) {
		objet.selectionStart = position;
		objet.selectionEnd = position;
	}
	
	// IE Support
	if(objet.setSelectionRange) {
		objet.focus();
		objet.setSelectionRange(position,position);
	}
	else if (objet.createTextRange) {
		var range = objet.createTextRange();
		range.collapse(true);
		range.moveEnd('character', position);
		range.moveStart('character', position);
		range.select();
	}
}

