// FONCTIONS GENERALES

function ajax( page, variables, functRetour, method )
{

	var xhr; 
	
	try
	{
		xhr = new ActiveXObject("Microsoft.XMLHTTP");    // Essayer Internet Explorer 
	}
	catch(e)
	{
		  xhr = new XMLHttpRequest();  // Autres navigateurs
	}
	
	if( method == 'GET' )
	{
		var urlVars = variables +'&token='+ Math.random();
		xhr.open( 'GET', page + '?'+ urlVars,  true);
		xhr.send(null); 
	}
	else
	{
		xhr.open( "POST", page,  true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send( variables ); 
	}
	
    xhr.onreadystatechange  = function()
	{ 
		if( xhr.readyState == 4 && functRetour )
			functRetour( xhr.responseText );
	}
}




// met en couleur (color) un élément sur x (2 par défaut) identifié par tag, dans l'élément id
function colorAlternate(id, tag, color){
	var bloc = document.getElementById( id );
	var elements = bloc.getElementsByTagName( tag );
	
	var nb;
	if(isNaN(parseInt(arguments[3]))) nb = 2;
	else nb = parseInt(arguments[3]);
	for(var j=0; j<elements.length; j++){
		if(j%nb == 0) elements[j].style.backgroundColor = color; 
		else elements[j].style.backgroundColor = ''; 
	}
}


// affiche ou efface
function display(div,val){ 
	if( val ) document.getElementById(div).style.display='block';
	else document.getElementById(div).style.display='none';
}

// affiche ou masque
function visible(div,val){
	if( val ) document.getElementById(div).style.visibility='visible';
	else document.getElementById(div).style.visibility='hidden';
}


// met un input en readonly
function readonly(input,val){
	if( val ) document.getElementById(input).readOnly=true;
	else document.getElementById(input).readOnly=false;
}

// change la couleur de fond
function bgColor(id,val){
	document.getElementById(id).style.backgroundColor=val;
}





// ajoute un bouton calendrier
function calendrier(input){
	document.write('<input type="image" src="icones/calendrier.gif" onClick="displayDatePicker(\''+input+'\')" title="Affichez le calendrier" style="vertical-align:middle"/>');
}

// affiche le calendrier
function selectDate(input){
	displayDatePicker(input);
}



// parse un fichier Txt pour en faire un tableau (les lignes sont reconnues par le saut de ligne \n; les champs sont reconnus par le séparateur
function parseTxt( str, separateur )
{
	var tab = new Array();
	
	if( str.length > 0 )
	{
		tab = str.split('\n');
		for( var i=0 ; i < tab.length ; i++ )
		{
			tab[i] = tab[i].split( separateur );
		}
	}
	return tab;
}

// fonction qui vérifie la présence d'un élément dans un tableau
function inArray( value, array ){

	for( var i =0; i < array.length; i++ )
	{
		if( array[i] == value)
			return true;
	}

	return false;
}
