/******************************************
 * Fonctions pour afficher/masquer un DIV *
 ******************************************/

function showDiv(divId)
{
	if (document.getElementById(divId) != null) document.getElementById(divId).style.display = "block";
}

function hideDiv(divId)
{
	if (document.getElementById(divId) != null) document.getElementById(divId).style.display = "none";
}


/**********************************
 * Fonctions pour les formulaires *
 **********************************/


//Verifie si un champs est vide
function isEmpty(field){
	if(field==null) return true;
	if(field.length<1) return true;
	for(i=0;i<field.length;i++) {
		if(field.charAt(i)!=' ') return false;
	}
	return true;
}

//Verifie le format d'un integer pour une valeur
function checkInt(theint) {
	return checkInteger(theint);
}
function checkInteger(theint) {
	if(theint==null) return true;
	if(theint.length<1) return true;
	for(i=0;i<theint.length;i++) {
	   c =  theint.charAt(i);
	   if(c<'0' || '9'<c ) return false;
	}
	return true;
}

//Verifie si un champs email est valide : s'il est different de nul et s'il contient un @
function isMail(field)
{
	if ( field.indexOf( ' ' ) >= 0 ) 	return false ;
	if(field.length < 5 ) 		 	return false ;
	if(field.indexOf('@') < 2 ) 	 	return false;

	var i = field.indexOf('@');
	if( i <= 0 ) return false;

	field =  field.substring( i + 1 , field.length );
	var j = field.indexOf('@');
	if( j >= 0 ) return false ;

	if( field.length < 1 )  return false;

	var j = field.indexOf( '.' );
	if ( j <= 0 ) return false;
	if ( field.substring( 0 ,j ) <= 0 ) return false;
	if ( field.substring( j+1 , field.length ) <= 0 ) return false;

	return true;
}

//rajouté par SLT peut nous servir
/**
 * Permet de limiter le nombre de caractères saisis pour un champ
 */
function limitLength(baliseId, taille) {

	var texte = document.getElementById(baliseId).value;
	if (texte.length > taille)
	{
		texte = texte.substring(0,(taille-1));
		document.getElementById(baliseId).value=texte;
	}
}


/**********************************************************************/
/*Function name :isDigit(theDigit) */
/*Usage of this function :test for an digit */
/*Input parameter required:thedata=string for test whether is digit */
/*Return value :if is digit,return true */
/* else return false */
/**********************************************************************/
function isDigit(theDigit)
{
    var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j;

    for (j = 0; j < digitArray.length; j++)
    {
        if (theDigit == digitArray[j])
            return true
    }
    return false
}
/*************************************************************************/
/*Function name :isPositiveInteger(theString) */
/*Usage of this function :test for an +ve integer */
/*Input parameter required:thedata=string for test whether is +ve integer*/
/*Return value :if is +ve integer,return true */
/* else return false */
/*function require :isDigit */
/*************************************************************************/
function isPositiveInteger(theString)
{
    var theData = new String(theString)

    if (!isDigit(theData.charAt(0)))
        if (!(theData.charAt(0)== '+'))
            return false

    for (var i = 1; i < theData.length; i++)
        if (!isDigit(theData.charAt(i)))
            return false
        return true
}
/**********************************************************************/
/*Function name :isDate(s,f) */
/*Usage of this function :To check s is a valid format */
/*Input parameter required:s=input string */
/* f=input string format */
/* =1,in mm/dd/yyyy format */
/* else in dd/mm/yyyy */
/*Function required :isPositiveInteger() */
/**********************************************************************/
function isDate(s,f)
{

	var a1=s.split("/");
	var a2=s.split("-");
	var e=true;
	if ((a1.length!=3) && (a2.length!=3))
	{
		e=false;
	}
	else
	{
		if (a1.length==3)
			var na=a1;
		if (a2.length==3)
			var na=a2;
		//alert("day: " + na[0] + "month: " + na[1] + "year: " + na[2]);
		if (na[0].length != 2 || na[1].length != 2 || na[2].length != 4){
			e = false;
		}
		else{
			if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2]))
			{
				if (f==1)
				{
					var d=na[1],m=na[0];
				}
				else
				{
					var d=na[0],m=na[1];
				}
				var y=na[2];
				if (((e) && (y<1000)||y.length>4))
					e=false
				if (e)
				{
					v=new Date(m+"/"+d+"/"+y);
					if (v.getMonth()!=m-1)
					e=false;
				}
			}
			else
			{
				e=false;
			}
		}
	}
	return e
}

/**********************************************************************/
/*Function name :isFloat(value) */
/*Usage of this function :To check value is a valid positive float */
/*Input parameter required:value=input string */
/**********************************************************************/
function isFloat(value){

	floatValue=parseFloat(value)

	if (isNaN(floatValue)) {
		return false;
	} else {
		return (floatValue >= 0);
	}

}

/**********************************************************************/
/*Function name :isInt(value) */
/*Usage of this function :To check value is a valid positive integer */
/*Input parameter required:value=input string */
/**********************************************************************/
function isInt(value){

	intValue = parseInt(value);

	if (isNaN(intValue)) {
		return false;
	} else {
		return (intValue >= 0);
	}

}

/**********************************************************************/
/****************************** DATE **********************************/

/*
* Retourne true si la date1 est inf?rieure ou egale a la date2
* Retourne false si la date1 est strictement superieure ? la date2
* ou si les dates ne respectent pas le format "jj/mm/aaaa"
*/
function compareDate(date1,date2)
{

    if(isDate(date1) && isDate(date2))
    {
        return parseDate(date1) <= parseDate(date2);
    }
    return false;
}


/**
* A partir d'une date sous le format "jj/mm/aaaa", retourne
* une chaine dans le format "aaaammjj" plus adapt? pour comparer
* des dates.
*/
function parseDate(date)
{
    return date.substr(6,4) + date.substr(3,2) + date.substr(0,2);
}

/**
* Retourne la date du jour dans le format "jj/mm/aaaa"
*/
function getAujourdhui() {
       date = new Date();
       var d = new String(date.getDate());
       var m = new String(date.getMonth() + 1);
       var y = new String(date.getFullYear());
       if (d.length == 1)
	       d = '0' + d;
	if (m.length == 1)
		m = '0' +m;
	//alert("ajourd'hui: " + d + "/" + m + "/" + y);

       return d + "/" + m + "/" + y;

}

/**
* Retourne vrai si la date pass?e en param?tre est aujourd'hui, hier ou avant,
* retourne faux si la date pass?e en param?tre est demain ou apr?s
* OU si la date n'est pas au format "jj/mm/aaaa"
* Nom de m?thode plus significatif que compareDate
*/
function isPasseOuAujourdhui(date1) {
    return compareDate(date1, getAujourdhui());
}

/**
* Retourne vrai si la date pass?e en param?tre est aujourd'hui, demain ou apr?s,
* retourne faux si la date pass?e en param?tre est hier ou avant.
* OU si la date n'est pas au format "jj/mm/aaaa"
* Nom de m?thode plus significatif que compareDate
*/
function isAujourdhuiOuFutur(date1) {
    return compareDate(getAujourdhui(), date1);
}


function checkDate( datevalue) {

	var sValue = datevalue;
	var dDate;

	if ( sValue.length == 0)
		return true;

	if ( sValue.length == 8)
		sValue = sValue.substr(0,2) + '/' + sValue.substr(2,2) + '/' + sValue.substr(4,4);

	if ( sValue.length != 10 || sValue.substr(2,1) !='/' || sValue.substr(5,1) !='/' )
	{
		return false;
	}

	dDate = new Date( parseInt( sValue.substr(6, 4), 10), parseInt( sValue.substr(3, 2) - 1, 10 ), parseInt( sValue.substr(0, 2), 10 ) );

	if ( dDate.getMonth() + 1 != parseInt( sValue.substr(3, 2), 10) | dDate.getDate() != parseInt( sValue.substr(0, 2), 10 ) )
	{
		return false;
	}
	return true;
}


function checkOnBlurDebut( dt_debut, dt_fin, msg ) {
	var dDateDebut, dDateFin;

	if ( !checkDate(dt_debut, null) ) return false;

	var sValueDebut = dt_debut.value;
	var sValueFin = dt_fin.value;

	if ( sValueDebut.length == 0 || sValueFin.length == 0 ) return true;

	dDateDebut = new Date( parseInt( sValueDebut.substr(6, 4), 10), parseInt( sValueDebut.substr(3, 2) - 1, 10 ), parseInt( sValueDebut.substr(0, 2), 10 ) );
	dDateFin = new Date( parseInt( sValueFin.substr(6, 4), 10), parseInt( sValueFin.substr(3, 2) - 1, 10 ), parseInt( sValueFin.substr(0, 2), 10 ) );

	if ( dDateDebut > dDateFin ) {

		if ( msg == null ) {
			msg = 'La date de d?but saisie "'+sValueDebut+'" est sup?rieure ? la date de fin "'+sValueFin+'".';
		}

		alert( msg );
		dt_debut.focus();
		dt_debut.value='';
		return false;
	}
	return true;
}


function checkOnBlurFin( dt_debut, dt_fin, msg ) {

	var dDateDebut, dDateFin;

	if ( !checkDate(dt_fin, null) ) return false;

	var sValueDebut = dt_debut.value;
	var sValueFin = dt_fin.value;

	if ( sValueDebut.length == 0 || sValueFin.length == 0 ) return true;

	dDateDebut = new Date( parseInt( sValueDebut.substr(6, 4), 10), parseInt( sValueDebut.substr(3, 2) - 1, 10 ), parseInt( sValueDebut.substr(0, 2), 10 ) );
	dDateFin = new Date( parseInt( sValueFin.substr(6, 4), 10), parseInt( sValueFin.substr(3, 2) - 1, 10 ), parseInt( sValueFin.substr(0, 2), 10 ) );

	if ( dDateDebut > dDateFin ) {

		if (msg == null){
			msg = 'La date de fin saisie "'+sValueFin+'" est inf?rieure ? la date de d?but "'+sValueDebut+'".';
		}

		alert( msg );
		dt_fin.focus();
		dt_fin.value='';
		return false;
	}
	return true;
}

/**
 * Permet de transformer ? la vol?e en majuscule
 */
function onKeyPress2Upper( field ){
	field.value=field.value.toUpperCase()+String.fromCharCode(window.event.keyCode).toUpperCase();
	window.event.keyCode = null;
}

/**********************************************************************/
/* Ouverture des pages de recherche */
/**********************************************************************/
function openWindow(path, titre) {
	window.open(path,titre , 'top=100,left=330,width=540,height=530,toolbar=no,scrollbars=yes,menu=no,resizable=yes,status=no');
}

function openWindowResType(path, titre) {
	window.open(path,titre , 'top=110,left=230,width=540,height=550,toolbar=no,scrollbars=yes,menu=no,resizable=yes,status=no');
}

function openWindowSite(path, titre) {
	window.open(path,titre , 'top=200,left=330,width=540,height=450,toolbar=no,scrollbars=yes,menu=no,resizable=yes,status=no');
}



/**
 *Permet de savoir si un champ est vide
 */
function checkEmptyField (field, msg){
	value = field.value;
	if(value.length == 0) {
		alert(msg);
		return false;
	}
	return true;
}


/*
Function echeck is used to verify if the given value is a possible valid email address.
This function thus simply makes sure the email address has one (@), atleast one (.).
It also makes sure that there are no spaces, extra '@'s or a (.) just before or after the @.
It also makes sure that there is atleast one (.) after the @.
*/
function eMailCheck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)

	if (str.indexOf(at)==-1){
	   alert("email invalide\n\n(l'adresse doit comporter le caractère @)");
	   return false;
	}

	if (str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("email invalide\n\n(le caractère @ est mal placé)");
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	   alert("email invalide\n\n(l'adresse doit comporter un point)");
	   return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	   alert("email invalide\n\n(un seul caractère @ autorisé)");
	   return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	   alert("email invalide\n\n(le point est mal placé)");
	   return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	   alert("email invalide\n\n(un seul point autorisé)");
	   return false;
	 }

	 if (str.indexOf(" ")!=-1){
	   alert("email invalide\n\n(l'adresse ne doit pas comporter d'espace)");
	   return false;
	 }

	 return true
}

function checkOnKeyPressValid(valide, event){
 	if(arguments.length == 1 || event == null){ // ancienne utilisation compatible uniquement IE
		if(window.event){ // on est bien dans IE
			var key = window.event.keyCode;
			if(! isKeyInString(key, valide)){
				window.event.keyCode = null; // pour compatibilite avec anciens appels (sans return)
				return false;
			}
		}
		return true;
	}else{ // utilisation compatible tout navigateur
		var key = window.event ? event.keyCode : event.charCode;
		return key == 0 // touche non alpha numerique, on laisse passe
			|| isKeyInString(key, valide);
	}
}

function isKeyInString(key, valide){
	var characters = String.fromCharCode(key);
	return valide.indexOf(characters) != -1;
}




