
var debug = 1;


function getElementById(obj_id)
{
	var obj = document.getElementById(obj_id);
	
	if(obj)
		return obj;
	else if(debug)
		alert('The object with id '+obj_id+' does not exist');
	else
		return null;
}

//	Fonctions mathématiques
function numberFormat(number, decimal_numbers, thousands_separator, decimal_separator)
{

	if(thousands_separator == null)
		thousands_separator = ',';
	
	if(decimal_separator == null)
		decimal_separator = '.';
		
	if(decimal_numbers == null)
		decimal_numbers = 2;
		
	var puis = Math.pow(10, decimal_numbers);
	
	var num = Math.round(number * puis)/puis;
	
	var numstr = num + '';
	var negsign = '';
	
	if(numstr.charAt(0) == '-')
	{
		negsign = '-';
		numstr = numstr.substring(1, numstr.length);
	}
	
	var pos = numstr.indexOf('.', 0);
	
	var decstr;
	if(pos != -1)
		decstr = numstr.substring(pos+1, numstr.length);
	else
		decstr = '00';
	
	var intstr;
	if(pos != -1)
		intstr = numstr.substring(0, pos);
	else
		intstr = numstr;
	
	var numint = intstr.length;
	var intarray = new Array;
	var pos = 0;
	
	for(var i = 0; i < Math.ceil(numint / 3); i++)
	{
		var numa;
		if(i == 0)
			numa = numint%3 != 0 ? numint%3 : 3;
		else
			numa = 3;

		intarray[i] = intstr.substr(pos, numa);
		pos+=numa;
	}
	
	var finalstr = negsign;
	for(var i = 0; i < Math.ceil(numint / 3); i++)
	{
		if(i != 0)
			finalstr+=thousands_separator;
		
		finalstr+=intarray[i];
	}			
	
	finalstr+= decimal_separator+decstr;
	
	return finalstr;	
}

//	Fonctions DOM

function hideObjectById(obj_id)
{
	var obj = document.getElementById(obj_id);	
	obj.style.display = "none";
}

function showObjectById(obj_id)
{
	var obj = document.getElementById(obj_id);	
	obj.style.display = "";
}

function hideObject(obj)	{obj.style.display = "none";}

function ShowObject(obj)	{obj.style.display = "";}

function toggleObjectDisplayById(obj_id)
{
	var obj = document.getElementById(obj_id);
	
	if(obj.style.display == "none")
		obj.style.display = "";
	else
		obj.style.display = "none";
}

function toggleObjectDisplay(obj)
{
	if(obj.style.display == "none")
		obj.style.display = "";
	else
		obj.style.display = "none";
}

function toggleSubObjectDisplay(obj_id, sub_obj_tag, skip_num)
{
	var obj = document.getElementById(obj_id);
	
	var elems = obj.getElementsByTagName(sub_obj_tag);
	
	var i;
	
	for(i = skip_num; i < elems.length; i++)
	{
		toggleObjectDisplay(elems.item(i));
	}
}

function changeColorById(obj_id, background_color, font_color)
{
	var obj = document.getElementById(obj_id);
	if(background_color != null)
		obj.style.backgroundColor = background_color;
	
	if(font_color != null)
		obj.style.color = font_color;
	
}

function changeColor(obj, background_color, font_color)
{
	if(background_color != null)
		obj.style.backgroundColor = background_color;
	
	if(font_color != null)
		obj.style.color = font_color;
}



function loadContent(file)
{
	var scriptTag = document.getElementById('loadScript');
	var head = document.getElementsByTagName('head').item(0);
	if( scriptTag ) 
		head.removeChild(scriptTag);
	script = document.createElement('script');
	script.src = file;	
	script.language = 'javascript1.2';
	script.id = 'loadScript';
	script.name = 'loadScript';	
	head.appendChild(script);
}

function loadContentWithSpecificTag(file, tag)
{
	var scriptTag = document.getElementById(tag);
	var head = document.getElementsByTagName('head').item(0);
	if( scriptTag ) 
		head.removeChild(scriptTag);
	script = document.createElement('script');
	script.src = file;		
	script.id = tag;
	script.name = tag;	
	script.language="javascript1.2";	
	head.appendChild(script);	
}

// Divers

function pausecomp(millis) 
{
	date = new Date();
	var curDate = null;
	
	do 
	{ 
		var curDate = new Date(); 
	} 
	while(curDate-date < millis);
} 

// Input Control
function input_accept_only_numbers(input)
{		
	input.value = stripNotNumbers(input.value);
}

function stripNotNumbers(str)
{
	var i;
	var fstr = '';
	
	for(i = 0 ; i < str.length; i++)
	{		
		
		if(str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57)
			fstr += str.charAt(i);
	}
	
	return fstr;
}

function confirmLink(link, message, confirmVariable, e)
{
	disableEventPropagation(e);
  	var IsOK = confirm(message);
    
  	if (IsOK) 
    {
        link.href += confirmVariable;
    }
    return IsOK;
}

function disableEventPropagation(e)
{
	if (!e) 
		var e = window.event;
	
		e.cancelBubble = true;
	
	if (e.stopPropagation) 
		e.stopPropagation();
}

function popupView(url, left, top, screenX, screenY, width, height, resizable, scrollbars)
{
	if(!resizable)
		resizable = 0;
		
	if(!scrollbars)
		scrollbars = 0;
	window.open(url, '', 'toolbar=0, resizable='+resizable+', scrollbars='+scrollbars+', status=0, top='+top+', left='+left+', screenX='+screenX+', screenY='+screenY+', width='+width+', height='+height);
}

function popupViewCentered(url, width, height, resizable, scrollbars)
{
	var left = (screen.width - width)/2;
	var top = (screen.height - height)/2;
	
	popupView(url, left, top, left, top, width, height, resizable, scrollbars);
}

function getXHR()
{
	var xhr_object = null; 
	 
	if(window.XMLHttpRequest) // Firefox 
	   xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 	 
	}
	
	return xhr_object;
 
}

function XHRGetLoad(object_id, file, arguments_array)
{
	var xhr = null; 
	 
	if(window.XMLHttpRequest) // Firefox 
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	   xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   return; 	 
	}
	
	var content_url  = 'launchJsScript.php?object_id='+object_id+'&file='+file;
	
	if(arguments_array)
	for(var i = 0; i < arguments_array.length; i++)
	{
		if(arguments_array[i] && arguments_array[i]['name'] && arguments_array[i]['value'])
			content_url += '&'+arguments_array[i]['name']+'='+arguments_array[i]['value'];
	}
	xhr.open('GET', content_url, true);
		
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4)
			eval(xhr.responseText);
	}
	
	xhr.send(null);
	
}

function XHREngineGetLoad(object_id, file, arguments_array)
{
	var xhr = null; 
	 
	if(window.XMLHttpRequest) // Firefox 
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	   xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   return; 	 
	}
	
	var content_url  = 'launchEngineJsScript.php?object_id='+object_id+'&file='+file;
	
	if(arguments_array)
	for(var i = 0; i < arguments_array.length; i++)
	{
		if(arguments_array[i] && arguments_array[i]['name'] && arguments_array[i]['value'])
			content_url += '&'+arguments_array[i]['name']+'='+arguments_array[i]['value'];
	}
	xhr.open('GET', content_url, true);
		
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4)
			eval(xhr.responseText);
	}
	
	xhr.send(null);
	
}

function XHRModuleGetLoad(object_id, module_nom, sous_dossier, file, arguments_array)
{
	var xhr = null; 
	 
	if(window.XMLHttpRequest) // Firefox 
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	   xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   return; 	 
	}
	
	var content_url  = 'launchModuleJsScript.php?module_nom='+module_nom+'&sous_dossier='+sous_dossier+'&file='+file;
	
	if(arguments_array)
	for(var i = 0; i < arguments_array.length; i++)
	{
		if(arguments_array[i] && arguments_array[i]['name'] && arguments_array[i]['value'])
			content_url += '&'+arguments_array[i]['name']+'='+arguments_array[i]['value'];
	}
	xhr.open('GET', content_url, true);
		
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4)
			eval(xhr.responseText);
	}
	
	xhr.send(null);
	
}

