function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// Ajax Engine
function createXMLHttpRequest() {
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	alert("XMLHttpRequest not supported");
	return null;
}
function AjaxRequestSetup()
{
	this.url = "";
	this.Method = "GET";
	this.returnType = "Text";
	this.ReturnFunction = function(XMLResponse) {alert(XMLResponse);};
	this.ErrorFunction = Global_CatchError;
	this.TimeOutFunction = Global_CatchTimeOut;
	this.ErrorGlobalText = "Une erreur est survenu : \nErreur {0}\n{1}";
	this.TimeOutGlobalText = "La ressource ne répond pas.\nDélai d'attente dépassé : {0} secondes ce sont écoulées";
	this.IsAsync = true;
	this.MAXIMUM_WAITING_TIME = 10000;
	this.Request = null;
}
var XmlRequestSetup = new AjaxRequestSetup();
var MAXIMUM_WAITING_TIME = XmlRequestSetup.MAXIMUM_WAITING_TIME;

function Global_CatchError(status, statusText)
{
	var txt = XmlRequestSetup.ErrorGlobalText.replace("{0}", status);
	txt = txt.replace("{1}", statusText);
	alert(txt);
}
function Global_CatchTimeOut(timeoutTime)
{
	var txt = XmlRequestSetup.TimeOutGlobalText.replace("{0}", parseInt(timeoutTime/60));
	alert(txt);
};
function XmlRequest() //url, Method, returnType, ReturnFunction, ErrorFunction, TimeOutFunction
{
	// initialisation via l'apelle de fonction (compatibility mode pour les vieilles procédure)
	if (arguments.length >= 1)
		XmlRequestSetup.url = arguments[0];
	if (arguments.length >= 2)
		XmlRequestSetup.Method = arguments[1];
	if (arguments.length >= 3)
		XmlRequestSetup.returnType = arguments[2];
	if (arguments.length >= 4)
		XmlRequestSetup.ReturnFunction = arguments[3];
	if (arguments.length >= 5)
		XmlRequestSetup.ErrorFunction = arguments[4];
	if (arguments.length >= 6)
		XmlRequestSetup.TimeOutFunction = arguments[5];
	
	// Début de la procédure
	var ajaxRequest = createXMLHttpRequest();
		ajaxRequest.open(XmlRequestSetup.Method, XmlRequestSetup.url, XmlRequestSetup.IsAsync);
		
		if (XmlRequestSetup.Request != null){ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8;");}
		
		var requestTimer = setTimeout(function() { // Handle timeout situation, e.g. Retry or inform user.
			ajaxRequest.abort();
			XmlRequestSetup.TimeOutFunction(XmlRequestSetup.MAXIMUM_WAITING_TIME);
		}, XmlRequestSetup.MAXIMUM_WAITING_TIME);
		
		ajaxRequest.onreadystatechange = function() 
		{
			if (ajaxRequest.readyState != 4) {return;}
			clearTimeout(requestTimer);
			if (ajaxRequest.status != 200) // Handle error, e.g. Display error message on page
			{
				XmlRequestSetup.ErrorFunction(ajaxRequest.status, ajaxRequest.statusText);
				return;
			}
			
			var XMLResponse;
			if (XmlRequestSetup.returnType == "Text") // Handle response type, e.g. Text Or Xml
			{
				XMLResponse = ajaxRequest.responseText;
			}
			else
			{
				XMLResponse = ajaxRequest.responseXML;
			}
			XmlRequestSetup.ReturnFunction(XMLResponse);
		};
		ajaxRequest.send(XmlRequestSetup.Request);
}

function XmlDom(xml, async) 
{
	var doc;
	// code for IE
	if (window.ActiveXObject)
	{
		doc = new ActiveXObject("Microsoft.XMLDOM");
		doc.async = async;
		doc.loadXML(xml);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else
	{
		var parser = new DOMParser();
		doc = parser.parseFromString(xml, "text/xml");
	}
	return doc;
}

function ObjWorker()
{
	var obj = document.getElementById(ObjWorker.arguments[0]);
	if (obj != null)
	{
		if (ObjWorker.arguments[1] == "write")
		{
			obj.innerHTML = ObjWorker.arguments[2];
		}
		else if (ObjWorker.arguments[1] == "append")
		{
			obj.innerHTML = obj.innerHTML + ObjWorker.arguments[2];
		}
		else if (ObjWorker.arguments[1] == "get")
		{
			return eval("obj." + ObjWorker.arguments[2]);
		}
		else if (ObjWorker.arguments[1] == "set")
		{
			eval("obj." + ObjWorker.arguments[2] + "=" + ObjWorker.arguments[3]);
		}
		else
		{
			obj.style.display = ObjWorker.arguments[1];
		}
	}
}

// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}

// Example:
// var b = new BrowserInfo();
// alert(b.version); 
var b = new BrowserInfo();
function BrowserInfo()
{
  this.name = navigator.appName;
  this.codename = navigator.appCodeName;
  this.version = navigator.appVersion.substring(0,4);
  this.platform = navigator.platform;
  this.javaEnabled = navigator.javaEnabled();
  this.screenWidth = screen.width;
  this.screenHeight = screen.height;
}

//	
//	Count Down dans le panel des boutton de Gestion Live
//	
var InactivityCounter_div, InactivityCounter
var GLOBAL_TIME = 1200;
var MaxSec = GLOBAL_TIME;

//
//<div id="InactivityCounter"></div>
//
function InactivityCounter_Load()
{
	setTimeout(function()
		{
			if (MaxSec == GLOBAL_TIME)
			{
				MaxSec = 1140;
				InactivityCounter_div = document.getElementById("InactivityCounter");
				InactivityCounter = setInterval(InactivityCounter_Update,1000)
			}
		}, 1000);
}

function InactivityCounter_Update()
{
	var Min = Math.floor(MaxSec/60);
	var Sec = (MaxSec % 60);
	if ((MaxSec % 60) == 0) {
		Sec = "00";
	} else if ((MaxSec % 60) < 10) {
		Sec = "0" + (MaxSec % 60);
	}
	
	MaxSec--
	if (MaxSec < 0)
	{
		clearInterval(InactivityCounter);
		InactivityCounter_div.innerHTML = "<span class=\"clock_ex\">Session Expirée !</span>";
	}
	else
	{
		InactivityCounter_div.innerHTML = Min + ":" + Sec;
	}
}
function IsIE()
{
	return ("Microsoft Internet Explorer" == b.name);
}
function IsNumeric(val)
{
	var myNumRegex = new RegExp("^[0-9]+$")
	return myNumRegex.test(val);
}


/*** Open Dialog ***/
function modelessDialogShow(url,width,height)
{
	if (IsIE())
	{
		window.showModelessDialog(url,window,"dialogWidth:"+width+"px;dialogHeight:"+height+"px;edge:Raised;center:1;help:0;resizable:1;");
	}
	else
	{
		left = (screen.width-width)/2
		top = (screen.height-height)/2
		window.open(url, "dependent=yes,width="+width+",height="+height+",left="+left+",top="+top);
	}
}
function modalDialogShow(url,width,height)
{
	if (IsIE())
	{
		window.showModalDialog(url,window,"dialogWidth:"+width+"px;dialogHeight:"+height+"px;edge:Raised;center:1;help:0;resizable:1;maximize:1");
	}
	else
	{
		left = (screen.width-width)/2
		top = (screen.height-height)/2
		window.open(url, "dependent=yes,width="+width+",height="+height+",left="+left+",top="+top);
	}
}
function OpenAspxUploaderPopUp(URL)
{
	var width = 425;
	var height = 150;
	var left = (screen.width-width)/2
	var top = (screen.height-height)/2
	window.open(URL, 'AspxUploaderPopUp', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'')
}


function FontSize()
{
	this.minSize = 8;
	this.maxSize = 20;
	this.incrementation = 2;
	this.defaultSize = 10;
	this.modifyChildNodes = false;
	this.Unit = "px";
	this.modifyChildNodesFunction = function(myID, local_fontSize)
	{
		var TheBox = document.getElementById(myID);
		LoopDirectChild(local_fontSize, TheBox);
	};
}
var FontSizeSetting = new FontSize();
function LoopDirectChild(local_fontSize, obj)
{
	var i = 0;
	for (i=0;i<obj.childNodes.length;i++)
	{
		try
		{
			obj.childNodes[i].style.fontSize = local_fontSize + FontSizeSetting.Unit;
		}
		catch (e)
		{
		}
		if (obj.childNodes[i].hasChildNodes())
		{
			LoopDirectChild(local_fontSize, obj.childNodes[i])
		}
	}
}
function setFaceSize(myID,local_fontSize) {
	obj = document.getElementById(myID);
	obj.style.fontSize = local_fontSize + FontSizeSetting.Unit;
	if (FontSizeSetting.modifyChildNodes)
	{
		FontSizeSetting.modifyChildNodesFunction(myID, local_fontSize);
	}
}

function FontLarger(myID) {
	if (myID == "") {
		myID = "txt";
	}
	obj = document.getElementById(myID);
	var local_fontSize = obj.style.fontSize;
	if (local_fontSize == "") {
		local_fontSize = FontSizeSetting.defaultSize;
	} else {
		local_fontSize = local_fontSize.replace(FontSizeSetting.Unit, "");
	}
	local_fontSize = Math.floor(local_fontSize);
	if (local_fontSize < FontSizeSetting.maxSize) {
		local_fontSize += FontSizeSetting.incrementation;
		setFaceSize(myID,local_fontSize);
	}
}
	
function FontSmaler(myID) {
	if (myID == "") {
		myID = "txt";
	}
	obj = document.getElementById(myID);
	var local_fontSize = obj.style.fontSize;
	if (local_fontSize == "") {
		local_fontSize = FontSizeSetting.defaultSize;
	} else {
		local_fontSize = local_fontSize.replace(FontSizeSetting.Unit,"");
	}
	local_fontSize = Math.floor(local_fontSize);
	if (local_fontSize > FontSizeSetting.minSize) {
		local_fontSize -= FontSizeSetting.incrementation
		setFaceSize(myID,local_fontSize);
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

var PagingPopUpWin = 0;
function PagingPopUpWindow(URLStr, left, top, width, height) {
	if (left == 'mid') {
		left = (screen.width-width)/2
	}
	if (top == 'mid') {
		top = (screen.height-height)/2
	}
	if(PagingPopUpWin) {
		if(!PagingPopUpWin.closed) PagingPopUpWin.close();
	}
	PagingPopUpWin = open(URLStr, 'PagingPopUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
function PrintMe(id) {
	if (id == "")
		id = "printid_"
	PagingPopUpWindow("/pages/print.asp?a=htmlgrab&id=" + id, 'mid', 'mid', 700, 550)
}

//####################################
//#####		   FORM FOCUS		 #####
//####################################
	var elem = 0;
	function formFocus() {
		try {
			if (document.forms[0].elements[elem].type == "button" || document.forms[0].elements[elem].type == "checkbox" || document.forms[0].elements[elem].type == "hidden" || (document.forms[0].elements[elem].type == "select-one" && document.forms[0].elements[elem].id == "inpList") ) {
				elem += 1;
				formFocus();
			} else {
				document.forms[0].elements[elem].focus();
			}
		} catch (e) {
			//alert(e.description)
		}
	}

	function OMconfirm(msg,url) {
		if ( confirm( msg ) ) {
			location = url;
		}
	}

	function countRest(obj,id) {
		try {
			if (obj.value.length > 255) {
				obj.value = Left(obj.value,255);
			}
			TheDiv = document.getElementById(id);
			TheDiv.innerHTML = 255 - obj.value.length;
		} catch (e) {
			alert(e.description)
		}
	}
	function Left(str, n){
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else
			return String(str).substring(0,n);
	}
	function Right(str, n){
		if (n <= 0)
		   return "";
		else if (n > String(str).length)
		   return str;
		else {
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
	}
	
	function ShowLayer(layerId) {
		var eventslayer = document.getElementById(layerId);
		if (IE) {
			eventslayer.style.display = "block";
			popUp(event, layerId);
		} else {
			if (eventslayer.style.display == "block") {
				eventslayer.style.display = "none";
				eventslayer.style.visibility = "hidden";
				eventslayer.style.top = "-1000px";
				eventslayer.style.left = "-1000px";
			} else {
				eventslayer.style.display = "block";
				eventslayer.style.visibility = "visible";
				if (global_X < 0) {global_X=0;}
				eventslayer.style.left = (global_X + 20) + "px"
				if (global_Y < 0) {global_Y=0;}
				eventslayer.style.top = global_Y + "px"
			}
		}
		return true
	}
	
	function ClassChanger(obj)
	{
		var className = String(obj.className);
	   
		if (className.substring(className.length - 4, className.length) == "Over")
			obj.className = className.substring(0,className.length - 4);
		else
			obj.className += "Over";
	}
	
	function FormatNumber(number, decimal)
	{
		var multiple = Math.pow(10, decimal);
		var String_Number = String(Math.round(number*multiple)/multiple);
		String_Number = FormatNumberEndingZero(String_Number, decimal);
		return String_Number;
	}
	function FormatNumberEndingZero(number, decimal)
	{
		var tmp = number.replace(",", ".");
		var index = tmp.indexOf(".");
		
		if (index == -1)
		{
			tmp += ".";
			for (var iZero = 0; iZero < decimal; iZero++)
			{
				tmp += "0";
			}
		}
		else
		{
			var current_decimal = tmp.substring(index+1);			
			while (current_decimal.length < decimal)
			{
				tmp += "0";
				current_decimal = tmp.substr(index, tmp.length-index);
			}
		}
		return tmp;
	}
	
	function MM_preloadImages() { //v3.0
		var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}
	
	function ValidateChar(sender, charLen)
	{
		charLen = charLen - 1;
		if (sender.value.length >= charLen)
		{
			sender.value = sender.value.substring(0, charLen);
		}
	}

/*
Ajax True / False Updater
*/
	var ActifUpdater = function()
	{
		this.TableName = null;
		this.ConnectionString = null;
		this.idField = null;
		this.idType = null;
		this.actifField = null;
		this.imageActif = null;
		this.imageInactif = null;
		this.imageAltActif = null;
		this.imageAltInactif = null;
	}
	var Updater = new ActifUpdater();
	
	function AfficherGalerie(imgId, id)
	{
		XmlRequestSetup.ReturnFunction = AfficherGalerieReturn;
		XmlRequestSetup.url = "/gestion/pages/ActifUpdater.asp?id=" + id + 
							  "&idField=" + Updater.idField + 
							  "&actifField=" + Updater.actifField + 
							  "&ConnectionString=" + Updater.ConnectionString + 
							  "&TableName=" + Updater.TableName + 
							  "&imgId=" + imgId;
		if (Updater.idType != null)
			XmlRequestSetup.url += "&idType=" + Updater.idType
		
		XmlRequest();
	}
	
	function AfficherGalerieReturn(XMLResponse)
	{
		var ReturnValues = XMLResponse.split("|");
		
		var SwitchValue = ReturnValues[0];
		
		switch (SwitchValue)
		{
			case "01" : 
				var imgId = ReturnValues[1];
				var img = document.getElementById(imgId);
				
				var inactifValue = document.getElementById("inactifValue");
				var count = null;
				if (inactifValue != null && inactifValue != undefined)
				{
					count = parseInt(inactifValue.innerHTML);
				}
				
				if (img.src.indexOf(Updater.imageActif) > -1)
				{
					img.src = Updater.imageInactif;
					img.alt = Updater.imageAltInactif;
					
					if (count != null)
						inactifValue.innerHTML = ++count;
				}
				else
				{
					img.src = Updater.imageActif;
					img.alt = Updater.imageAltActif;
					
					if (count != null)
						inactifValue.innerHTML = --count;
				}
				img.parentNode.title = img.alt;
				break;
			case "02" : 
				alert("Une erreur est survenu : \n" + ReturnValues[1]);
				break;
			case "03" : 
				alert("Vous n'avez pas accès à cette fonctionalité...");
				break;
			default : 
				alert("Une réponse de type inconu à été reçu...");
		}
	}

function omHsrc(el,hsrc){
	var src = el.src;
	el.src=hsrc;
	el.onmouseout = function(){this.src=src;}
}

function attachCss(sheet)
{
	var head = document.getElementsByTagName("HEAD")[0];
	var myLink = document.createElement("link");
	myLink.setAttribute("rel", "stylesheet");
	myLink.setAttribute("type", "text/css");
	myLink.setAttribute("href", sheet);
	head.appendChild(myLink);
}

function removeCss(sheet)
{
	var head = document.getElementsByTagName("HEAD")[0];
	var links = document.getElementsByTagName("link");
	
	for (var i = 0; i < links.length; i++)
		if (links[i].getAttribute("href") == sheet)
			links[i].parentNode.removeChild(links[i]);
}



