//Permet d'ecrire dans un DIV (localisé par son ID) la valeur de "texte"
function writeDiv(texte, iddiv)  {
	document.getElementById(iddiv).innerHTML = texte;
}

function writeException(lib) {
	document.getElementById('warning').innerHTML = document.getElementById('warning').innerHTML + lib;
}

function ouvrir(page) {
	document.location = page;
}

function change_page(i){
	document.formulaire.numpage.value=i;
	document.formulaire.submit();
}

function imprimer(){
	window.print();
}

function setSessionVar(nom, cpt, id){ 
	var xhr = getXhr();
	
	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var res = '';

			if(reponse.getElementsByTagName("res")[0].firstChild != null) {
				res = reponse.getElementsByTagName("res")[0].firstChild.nodeValue;
			}
			if(document.getElementsByName(nom)[cpt+1].value == '') {
				document.getElementsByName(nom)[cpt+1].value = res;
			}
		}
	}

	// Ici on va voir comment faire du post
	xhr.open("POST","ajax_get_session_var.php",true);
	// ne pas oublier ça pour le post
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	// ne pas oublier de poster les arguments
	xhr.send("id="+id);
}

function getXhr(){
    var xhr = null; 
	if(window.XMLHttpRequest) {// Firefox et autres
		xhr = new XMLHttpRequest();
	} 
	else if(window.ActiveXObject){ // Internet Explorer 
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else { // XMLHttpRequest non supporté par le navigateur 
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
    return xhr;
}

//Node cleaner
function go(c){
	if(!c.data.replace(/\s/g,''))
		c.parentNode.removeChild(c);
}

function clean(d){
	var bal=d.getElementsByTagName('*');

	for(i=0;i<bal.length;i++){
		a=bal[i].previousSibling;
		if(a && a.nodeType==3)
			go(a);
		b=bal[i].nextSibling;
		if(b && b.nodeType==3)
			go(b);
	}
	return d;
} 

function get_hover(id){
	document.getElementById(id).style.backgroundColor = "#598fbc";
	document.getElementById(id).style.cursor = "pointer";
}
function get_nohover(id){
	document.getElementById(id).style.backgroundColor = "#7eadd7";
}

function get_bold(id){
	document.getElementById(id).style.fontWeight = "bold";
	document.getElementById(id).style.color = "black";
	document.getElementById(id).style.cursor = "pointer";
}
function get_nobold(id){
	document.getElementById(id).style.fontWeight = "normal";
	document.getElementById(id).style.color = "#5e5e5e";
}

function change_orderby(id){
	document.formulaire._orderby.value=id;
	document.formulaire.submit();
}
function change_orderby(id, modif){
	document.formulaire._orderby.value=id;
	document.formulaire.modif.value=modif;
	document.formulaire.submit();
}
function switchVisible(id){
	var styleValue = document.getElementById(id).className;
	var index = styleValue.indexOf("visible");
	var before = styleValue.substring(0, index);
	var mode = styleValue.substring((index+7), styleValue.length);
	if(mode == "on") {
		styleValue = "visibleoff";
	}
	else {
		styleValue = "visibleon";
	}
	document.getElementById(id).className = before + " " + styleValue;
	
	var bal=document.getElementsByName(id);

	for(i=0;i<bal.length;i++){
		bal[i].className = before + " " + styleValue;
	}
}


function date_valide(datevalue, msg) {
	datval = true;

	if(datevalue.length!=10) 
		datval=false;

	for(j=0;j<10;j++){
		b=Number(datevalue.charAt(j));
		if((j==2)||(j==5)){
			if(datevalue.charAt(j)!='/'){
				datval=false;
			}
		}
		if(((j!=2)&&(j!=5))&&(String(Number(b))=='NaN')){
			datval=false;
		}
	}

	jour=Number(datevalue.charAt(0)+datevalue.charAt(1));
	mois=Number(datevalue.charAt(3)+datevalue.charAt(4));
	annee=Number(datevalue.charAt(6)+datevalue.charAt(7)+datevalue.charAt(8)+datevalue.charAt(9));

	if((annee<1990)||(annee>2099))
		datval=false;
	if((mois>12)||(mois==0))
		datval=false;
	if(((((((mois==1)||(mois==3))||(mois==5))||(mois==7))||(mois==8))||(mois==10))||(mois==12)){
		if((jour<1)||(jour>31)){
			datval=false;
		}
	}

	if((((mois==4)||(mois==6))||(mois==9))||(mois==11)){
		if((jour<1)||(jour>30)){
			datval=false;
		}
	}

	if(mois==02){
		if(annee%4==0){
			if((jour<1)||(jour>29)){
				datval=false;
			}
		}
		else{
			if((jour<1)||(jour>28)){
				datval=false;
			}
		}
	}

	if(!datval){
		ok=false;
		alert(msg);

		return false;
	}
	return datval;
}

function select_innerHTML(objeto,innerHTML){
	/******
	 * select_innerHTML - corrige o bug do InnerHTML em selects no IE
	 * Veja o problema em: http://support.microsoft.com/default.aspx?scid=kb;en-us;276228
	 * Versão: 2.1 - 04/09/2007
	 * Autor: Micox - Náiron José C. Guimarães - micoxjcg@yahoo.com.br
	 * @objeto(tipo HTMLobject): o select a ser alterado
	 * @innerHTML(tipo string): o novo valor do innerHTML
	 *******/
	objeto.innerHTML = ""
		var selTemp = document.createElement("micoxselect")
		var opt;
	selTemp.id="micoxselect1"
		document.body.appendChild(selTemp)
		selTemp = document.getElementById("micoxselect1")
		selTemp.style.display="none"
			if(innerHTML.toLowerCase().indexOf("<option")<0){//se não é option eu converto
				innerHTML = "<option>" + innerHTML + "</option>"
			}
	innerHTML = innerHTML.toLowerCase().replace(/<option/g,"<span").replace(/<\/option/g,"</span")
	selTemp.innerHTML = innerHTML


	for(var i=0;i<selTemp.childNodes.length;i++){
		var spantemp = selTemp.childNodes[i];

		if(spantemp.tagName){     
			opt = document.createElement("OPTION")

			if(document.all){ //IE
				objeto.add(opt)
			}else{
				objeto.appendChild(opt)
			}       

			//getting attributes
			for(var j=0; j<spantemp.attributes.length ; j++){
				var attrName = spantemp.attributes[j].nodeName;
				var attrVal = spantemp.attributes[j].nodeValue;
				if(attrVal){
					try{
						opt.setAttribute(attrName,attrVal);
						opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
					}catch(e){}
				}
			}
			//getting styles
			if(spantemp.style){
				for(var y in spantemp.style){
					try{opt.style[y] = spantemp.style[y];}catch(e){}
				}
			}
			//value and text
			opt.value = spantemp.getAttribute("value")
			opt.text = spantemp.innerHTML
			//IE
			opt.selected = spantemp.getAttribute('selected');
			opt.className = spantemp.className;
		} 
	}    
	document.body.removeChild(selTemp)
	selTemp = null
}

function format_number(pnumber,decimals){
    if (isNaN(pnumber)) { return 0};
    if (pnumber=='') { return 0};
    var snum = new String(pnumber);
    var sec = snum.split('.');
    var whole = parseFloat(sec[0]);
    var result = '';
    if(sec.length > 1){
        var dec = new String(sec[1]);
        dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
        dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
        var dot = dec.indexOf('.');
        if(dot == -1){
            dec += '.';
            dot = dec.indexOf('.');
        }
        while(dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    } else{
        var dot;
        var dec = new String(whole);
        dec += '.';
        dot = dec.indexOf('.');
        while(dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    }
    return result.replace('.', ',');
}

