

/* --- 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() )
*/

var modified = false;
var textErrorCheckSave = "";
var dataSavedLabel = null;
var strSaveBefore = "";
var originalValue = '';


function saveMe (url){
	var ok = true;
	if (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";
	}
	
	if (isIE4 || isIE6) {
	var obj = window.event.srcElement;
	if (obj && obj.tagName == "INPUT" && obj.type == "text")
	{
		originalValue = obj.value;
	}
	}
}

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 supprimer les espaces dans le debut et la fin d'un string
function KillSpace(x)
{
	while((x.length>0) && (x.charAt(0)==' '))
		x = x.substring(1,x.length);
	while((x.length>0) && (x.charAt(x.length-1)==' '))
		x = x.substring(0,x.length-1);
	
	return x;
}

// 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;
}

//utiliser cette fonction pour recupérer le texte de l'item selectionné dans une DDL
function getDdlSelectedText(nom) 
{
	return getElem(nom).options[getElem(nom).selectedIndex].text;
}

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();
  }
}
