/*---------------------------------------------------------------------------------------------------
* Liste des fonctions utiles
* @der.mod : 20/06/2006
*/

/*
* G?re la s?lection de l'?l?ment gr?ce ? son ID
*****************************************************************************************************/
function getElement(id){
	if(document.getElementById) {
		return document.getElementById(id);
	} else if(document.all) {
		return document.all[id];
	} else return;
}

/*
* Equivalent de trim en PHP -> enl?ve les espaces aux extr?mit?s d'une chaine
*****************************************************************************************************/
function trim(s) {
	s = s.replace(/[ ]*$/,'');
	s = s.replace(/^[ ]*/,'');
	return s;
}

/*
* S?l?ctionne une valeur dans un menu d?roulant ? partir d'une valeur
*****************************************************************************************************/
function selSelect(mSelect, valASelect){
	if(valASelect != ''){
		for (var y = 0; y < mSelect.length; y++){
			if(mSelect.options[y].value == valASelect){
				mSelect.options[y].selected	= true;
			}
		}
	}
}

/*
* V?rifie la validit? d'une date
*****************************************************************************************************/
function isDateValid(chaineDate) {
   var ladate	= (chaineDate).split("/")
   if ((ladate.length != 3) || isNaN(parseInt(ladate[0])) || isNaN(parseInt(ladate[1])) || isNaN(parseInt(ladate[2]))) return false
   var unedate	= new Date(eval(ladate[2]),eval(ladate[1])-1,eval(ladate[0]))
   var annee	= unedate.getYear()
   if ((Math.abs(annee)+"").length < 4) annee = annee + 1900
   return ((unedate.getDate() == eval(ladate[0])) && (unedate.getMonth() == eval(ladate[1])-1) && (annee == eval(ladate[2])))
}

/*
* Convertie un texte UTF-8 en ISO
*****************************************************************************************************/
function decode_utf8(utftext) {
	var plaintext		= ""; var i=0; var c=c1=c2=0;
	while(i<utftext.length){
		c = utftext.charCodeAt(i);
		if(c<128){
			plaintext	+= String.fromCharCode(c);
			i++;
		}else if((c>191) && (c<224)){
			c2 = utftext.charCodeAt(i+1);
			plaintext	+= String.fromCharCode(((c&31)<<6) | (c2&63));
			i+=2;
		}else{
			c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
			plaintext	+= String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
			i+=3;
		}
	}
	return plaintext;
}

/*
* Convertie un texte ISO en UTF-8
*****************************************************************************************************/
function encode_utf8(isotext) {
	isotext				= isotext.replace(/\r\n/g,"\n");
	var utftext			= "";
	for(var n=0; n<isotext.length; n++){
		var c=isotext.charCodeAt(n);
		if(c<128){
			utftext += String.fromCharCode(c);
		}else if((c>127) && (c<2048)){
			utftext += String.fromCharCode((c>>6)|192);
			utftext += String.fromCharCode((c&63)|128);
		}else{
			utftext += String.fromCharCode((c>>12)|224);
			utftext += String.fromCharCode(((c>>6)&63)|128);
			utftext += String.fromCharCode((c&63)|128);
		}
	}
	return utftext;
}

/*
* Permet la connexion asynchrone ? un fichier XML
*****************************************************************************************************/
function makeRequest(url){
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = getXml;
	http_request.open('GET', url, true);
	http_request.send(null);
}

/*
* Supprime les espaces vides au sein d'un fichier XML
*****************************************************************************************************/
function cleanNode(c){
	if(!c.data.replace(/\s/g,'')){
		c.parentNode.removeChild(c);
	}
}

function cleanXml(d){
	var bal	= d.getElementsByTagName('*');
	for(i=0;i<bal.length;i++){
		a	= bal[i].previousSibling;
		if(a && a.nodeType==3)
			cleanNode(a);
		b	= bal[i].nextSibling;
		if(b && b.nodeType==3)
			cleanNode(b);
	}
	return d;
} 

/*
* recherchje une valeur dans un tableau
*****************************************************************************************************/
function in_array(array, valeur) {
	for(var i in array) {
		if(array[i] == valeur) return true; }
	return false;
}

function getFinMois(annee, mois){
	var myDate	= new Date(annee,mois,01);
	var myMonth	= myDate.setMonth(myDate.getMonth()+1);
	var theDay	= myDate.setDate(0);
	var lastDay	= myDate.getDate();
	return lastDay;
}