/* ----------------------------------- Desactivation de la touche ENTER ----------------------------------- */
/*	Desactiver la touche ENTER
	A placer au niveau d'un contrôle ou dans la balise <body> pour désactiver completement la touche ENTER
	Pour activer la désactivation, bEnable = false
*/

var enterKeyCheck = true;

function EnterKeyCheck(bEnable)
{
    enterKeyCheck = bEnable;
}

function EnterKeyPress(bEnable,event)
{
	if (navigator.appName=="Microsoft Internet Explorer")
	{
		key=window.event.keyCode;
	}
	if (navigator.appName=="Netscape")
	{
		key=window.event.which
	}

    if(enterKeyCheck)
    {
	    if(!bEnable && key==13)
	    {
		    // Touche ENTER
		    // Desactivation
		    event.keyCode=0;
	    }
	}
}

/* --- Rappel l'utilisateur de sauvegarder la page aprés une modification au cas où il essairait de la quitter  --- */
/*
Chaque champ de la page doit comporté un attribut "onfocus" avec "modify()" comme argument ex :
	<asp:TextBox ID="MaTextBox" onfocus="modify();"  Runat="server">
	<input type="text" id="maTextBox" onfocus="modify();">

Chaque lien permettant de quitter la page doit être fait de la façon suivante :
	<a href="javascript:saveMe('http://www.monurl.com')">Mon lien</a>


"textErrorCheckSave" contient le texte affiché dans la fénètre de confirmation,
il doit être initialisé en haut de chaque page et non pas dans ce fichier javascript

"dataSavedLabel" est un tableau contenant l'ID de tout les objets à cacher lorsqu'une modification est faite sur la page (appel de modify() )
*/

/*
/!\ CORRECTION /!\ 
La modification d'un champs correspond à l'evenement OnChange (et non OnFocus)
Donc, chaque champ de la page doit comporté un attribut "OnChange" avec "modify()" comme argument.
*/

var checkSaveIsActif = true;
var modified = false;
var textErrorCheckSave = "";
var dataSavedLabel = null;
var strSaveBefore = "";
	

function saveMe (url){
	var ok = true;
	if (checkSaveIsActif && modified) {
		ok = confirm(textErrorCheckSave);
		if (ok){
			if (url){
				document.location = url;
			}
		}
	}
	else {
		if (url){
			document.location = url;
		}
	}
}

function saveBefore(){
	if (modified) {
		return confirm(strSaveBefore);
	}
	
}

function modify(){

	var tmpArray = null;
	var tmpObjDataSaved = null;
	
	modified = true;
	if (dataSavedLabel != null){
		tmpArray = dataSavedLabel.split(";");
		for (var i = 0; i < tmpArray.length; i++) {
			tmpObjDataSaved = getElemStyle(tmpArray[i]);
			if (tmpObjDataSaved){
				tmpObjDataSaved.display = "none";
			}
		}
	}
	
	if (typeof(document.forms[0]["IH_MustSave"]) != 'undefined'){
		document.forms[0]["IH_MustSave"].value = "1";
	}
}

function ModifRbl(sender, args)
{
	modify();
	args.isValid = true;
}

/* ----------------------------------------- Ouverture de popup  ----------------------------------------------- */
// Si x et y ne sont pas mentionnés, la popup est automatiquement centrée
function openPopup (url,nomPopup,myScroll,l,h,x,y,myResizable){
	var win;
	if (x == null) {
		x = screen.width/2 - l/2;
	}
	if (y == null){
		y = screen.height/2 - h/2;
	}
	if (myScroll != 'no'){
		myScroll = 'yes';
	}
	if (myResizable != 'yes'){
		myResizable = 'no';
	}
	win=open(url,nomPopup,'width=' + l + ',height=' + h + ',status=no,toolbar=no,menubar=no,location=no,resizable=' + myResizable + ',titlebar=no,scrollbars=' + myScroll + ',fullscreen=no,left=' + x + ',top=' + y);
	win.focus();
	return win;
}


// Si x et y ne sont pas mentionnés, la popup est automatiquement centrée
function openModalPopup (url,args,myScroll,l,h,x,y,myResizable){

	if (x == null) {
		x = screen.width/2 - l/2;
	}
	if (y == null){
		y = screen.height/2 - h/2;
	}
	if (myScroll != 'no'){
		myScroll = 'yes';
	}
	if (myResizable != 'yes'){
		myResizable = 'no';
	}
	var sSettings = "help:no;border:thin;status:no;toolbar:no;menubar:no;location:no;scrollbars:" + myScroll + ";resizable:" + myResizable + ";dialogHeight:" + h + "px;dialogWidth:" + l + "px;dialogTop:" + y + "px;dialogLeft:" + x + "px";
	return window.showModalDialog(url,args,sSettings);
}

/*Pour retourner une valeur : 
	window.returnValue="toto";
Pour la récuperer : 
	maValeur = window.showModalDialog(url,args,sSettings);
	*/



/* ----------------------------------------- Détection du navigateur  ----------------------------------------------- */
isNS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 4 && parseInt(navigator.appVersion) < 5) ? true : false;
isNS6 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 5) ? true : false;
isIE4 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) >= 4 &&  parseInt(navigator.appVersion) < 6) ? true : false;
isIE6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE')+5,3)) >= 6) ? true : false;

// utiliser cette fonction pour afficher ou cacher une partie dans la page
function ShowDiv(obj, isVisible)
{
	var oDiv = getElem(obj);
	if (oDiv)
	{
		oDiv.style.display = isVisible?"":"none";
	}
}


// utiliser cette fonction pour récupérer un objet en fonction du navigateur
function getElem (nom) {
	
	var tmp = null;
	
	if (nom != ""){
		if (isNS4) {
			tmp = nav4FindLayer(document, nom);
		}
		else if (isNS6 || isIE6) {
			tmp = eval('document.getElementById("' + nom + '")');
		}
		else {
			tmp = eval('document.all.' + nom);
		}
	}

	return ((tmp!=null) ? tmp : false);
}

// utiliser cette fonction pour récupérer le style d'un objet en fonction du navigateur
function getElemStyle (nom) {
	var tmpElem = getElem (nom);
	
	return (isNS4) ? tmpElem : (tmpElem) ? tmpElem.style : false;
}


function nav4FindLayer(doc, id)
{
  var i;
  var subdoc;
  var obj;
  
  for (i = 0; i < doc.layers.length; ++i)
  {
    if (doc.layers[i].id && id == doc.layers[i].id)
      return doc.layers[i];
      
    subdoc = doc.layers[i].document;
    obj    = nav4FindLayer(subdoc, id);
    if (obj != null)
      return obj;
  }
  return null;
}


/* ----------------------------------------- Roll over sur les images  ----------------------------------------------- */
// les images de boutons qui possèdent un rollover doivent finir par
// --    "_on.ext" pour l'image affichée quand le pointeur se trouve sur le bouton
// --    "_off.ext" pour l'image affichée quand le pointeur sort du bouton
function rollOn (img){
	var tmp;

	tmp = img.src ;
	if (tmp.indexOf("_off.") > -1)
		img.src = tmp.replace("_off.","_on.");
	else if (tmp.lastIndexOf(".") > -1)
	{
		img.src = tmp.substring(0,tmp.lastIndexOf(".")) + "_b" + tmp.substring(tmp.lastIndexOf("."),tmp.length);
	}
}

function rollOff (img){
	var tmp;

	tmp = img.src ;
	if (tmp.indexOf("_on.") > -1)
		img.src = tmp.replace("_on.","_off.");
	else if (tmp.indexOf("_b.") > -1)
		img.src = tmp.replace("_b.",".");
}

/* ------------------------------------------- Désactive un élément -------------------------------------------------*/
var secondClick = false;

function setMeDisable(){
	if (secondClick) {
		return true;
	}
	else
	{
		secondClick = true;
		return false;
	}
}

/* --------------------------------------------- Validation ----------------------------------------------------------- */


	
function isInt(value) 
{
	var reg = /^-{0,1}[0-9]*$/;

	return reg.test(value);
}

function isFloat(value) 
{
	var reg = /(^-{0,1}[0-9]*$)|(^-{0,1}[0-9]+(,|.)[0-9]+$)/;

	return reg.test(value);
}

function customValidatorInt(source, arguments)
{
	isValid = true;
	
	value = clearFormatValue(arguments.Value);
	
	if(!isInt(value))
	{
		isValid = false;
	}
	
	arguments.IsValid = isValid;
	
	return isValid;
}

function customValidatorFloat(source, arguments)
{
	isValid = true;
	
	value = formatValueOff(arguments.Value);
	
	if(!isFloat(value))
	{
		isValid = false;
	}
	
	arguments.IsValid = isValid;
	
	return isValid;
}

/* ---------------------------------------------- Formatage ----------------------------------------------------------- */
var isClickFocus = false;

function clearFormatValue(value)
{
	splitString = value.split(" ");
			
	value = "";
	for(i=0;i<splitString.length;i++)
	{
		value += splitString[i];			
	}
	
	return value;
}

function clearFormatObject(object)
{
	object.value = clearFormatValue(object.value);
	
	if(isClickFocus)
		setCaretAtEnd(object);
	else
		object.select();
		
	isClickFocus = false;
}

function formatIntValue(value)	
{
	if(!isInt(value))
	{
		return value;
	}
			
	valueLength = value.length;				
	intValue = valueLength / 3;
			
	newValue = "";
	for(i=0;i<intValue;i++)
	{
		if(newValue.length == 0)
		{
			newValue = value.substring(valueLength-((i*3)+3),valueLength-(i*3));
		}
		else
		{
			newValue = value.substring(valueLength-((i*3)+3),valueLength-(i*3)) + " " + newValue;
		}
	}
	
	return newValue;
}

function formatIntObject(object)
{
	object.value = formatIntValue(object.value);
}

function formatFloatValue(value)	
{
	if(!isFloat(value))
	{
		return value;
	}
	
	valueDecimal = "";
	value = value.replace(",",".");
	if(value.indexOf(".") > 0)
	{
		valueDecimal = value.substr(value.indexOf("."));
		value = value.substring(0,value.indexOf("."));
	}
	
			
	valueLength = value.length;				
	intValue = valueLength / 3;
			
	newValue = "";
	for(i=0;i<intValue;i++)
	{
		if(newValue.length == 0)
		{
			newValue = value.substring(valueLength-((i*3)+3),valueLength-(i*3));
		}
		else
		{
			newValue = value.substring(valueLength-((i*3)+3),valueLength-(i*3)) + " " + newValue;
		}
	}
	
	newValue += valueDecimal;
	
	return newValue;		
}

function formatFloatObject(object)
{
	object.value = formatFloatValue(object.value);
}

function setCaretAtEnd (field) {
  if (field.createTextRange) {
    var r = field.createTextRange();
    r.moveStart('character', field.value.length);
    r.collapse();
    r.select();
  }
}


/* --------------------- redimensionnement des images à l'affichage --------------------------------------------*/

function checkDim(img){
    temp = 1;
    largeur = img.width;
    hauteur = img.height;
    if (largeur >= hauteur){
        if (largeur >= 90){
            temp = 90 / largeur;
        }
    }else {        
        if(hauteur >= 90){
            temp = 90 / hauteur;
        }
    }
    temp2 = largeur * temp;
    img.width = temp2;
    }