/*******
* Ajax *
********/
var oReq, divCont, blDec, timer;
var aParams = new Array();

function cParam(sNom, sVal) {
	this.sNom = sNom.toString();
	this.sVal = sVal.toString();
}

function cReq() {
	var oXml;
	if (window.XMLHttpRequest) oXml = new XMLHttpRequest();
	else if (window.ActiveXObject) oXml = new ActiveXObject("Microsoft.XMLHTTP");
	else return false;
	return oXml;
}

function chargementReq(divElt, sFichier) {
	if (!document.getElementById) return true;
	divCont = divElt;
	oReq = cReq();
	if (!oReq) return true;
	attendre(document.getElementById(divElt));
	var argts = chargementReq.arguments;
	for(i=2; i<argts.length; i+=2) { aParams[(i-2)/2] = new cParam(argts[i], argts[i+1]); }
	try { chargementXML(sFichier, divElt, aParams); }
	catch(e) {
		alert("Erreur" + (typeof e == "string") ? e:((e.message) ? ": ".e.message:" de chargement"));
		oReq = divCont = null;
		return true;
	}
	return false;
}

function chargementXML(sFichier) {
	var sParams = "ajax=true";
	oReq.open("POST", sFichier, true);
	oReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	for(i=0; i<aParams.length; i++) sParams += "&"+aParams[i].sNom+"="+aParams[i].sVal;
	oReq.send(sParams);
	oReq.onreadystatechange = testObj;
}

function testObj() {
	if (oReq.readyState == 4) {
		if (oReq.status == 200) {
			affichageDiv();
			aParams = new Array();
		}
	}
}

function affichageDiv() {
	var sHtml = oReq.responseText;
	var divElt = document.getElementById(divCont);
	if (sHtml == "") {
		alert("Pas de reponse du serveur !");
		divElt.innerHTML = "";
	} else {
		divElt.innerHTML = sHtml;
	}
	degrader(divElt);
	cacheMessages();
}

/*************************
* Effets chargement Ajax *
**************************/
function attendre(divElt) {
	var flashMargin = "30px auto 30px auto";
	divElt.style.textAlign = "center";
	var contenu = "<img src=\"img/chargement.gif\" width=\"32\" height=\"32\" alt=\"Chargement en cours\" style=\"margin:"+flashMargin+";\" />";
	divElt.innerHTML = contenu;
}

function degrader(divElt) {
	blDec = 149;// 105 : #FFFF99
	divElt.style.backgroundColor = "#FFFFC5";
	timer = setInterval("eclaircir()", 100);
}

function eclaircir() {
	var divElt = document.getElementById(divCont);
	blDec += 15;
	divElt.style.backgroundColor = "#FFFF"+Dec2Hex(blDec);
	if (blDec > 250) {
		divElt.style.backgroundColor = "#FFFFFF";
		clearInterval(timer);
	}
}

function Dec2Hex(dec) {
	var hexChars = "0123456789ABCDEF";
	var a = dec%16;
	var b = (dec-a)/16;
	hex = ""+hexChars.charAt(b)+hexChars.charAt(a);
	return hex;
}

// ModContact: modification du fichier téléchargé
function modiFichier(sIdChp) {
	var oChp = document.getElementById(sIdChp);
	oChp.disabled = false;
	oChp.focus();
	return false;
}

/*********
* Divers *
*********/

var posX = 0;
var posY = 0;
var divPhoto = null;
var popWin = null;

function popFenetre(loc, w, h) {
	lpos	= (screen.width && screen.width > 800) ? (screen.width-w)/2 : 0;
	tpos	= (screen.height && screen.height > 640) ? (screen.height-h)/2 : 0;
	sets	='height='+h+',width='+w+',top='+tpos+',left='+lpos+',menubar=0,location=0,scrollbars=1,resizable=1';
	popWin	= window.open(loc, "popwin", sets);
	if (popWin.window.focus) { popWin.window.focus(); }
	return false;
}

function photoTrick(sFichier, sTitle, sIdiv, nW, nH, nX, nY, sContStyle) {
	sHtml = "<div style=\""+sContStyle+"\">"
//	+"<div style=\"width:"+nW+"px;height:"+nH+"px;margin:auto;background:url('"+sFichier+"') "+nX+"px "+nY+"px no-repeat;\" onmouseover=\"affichPhoto('"+sIdiv+"');\" onmouseout=\"cachPhotos('"+sIdiv+"');\"></div></div>"
	+"<div style=\"width:"+nW+"px;height:"+nH+"px;margin:auto;background:url('"+sFichier+"') "+nX+"px "+nY+"px no-repeat;cursor:pointer;\" onmouseover=\"affichPhoto('"+sIdiv+"');\" onmouseout=\"cachPhotos('"+sIdiv+"');\""
	+" title=\""+sTitle+" [nouvelle fenetre]\" onclick=\"window.open('"+sFichier+"','_blank');\"></div></div>"
	+"<div id=\""+sIdiv+"\" class=\"cach\"><img src=\""+sFichier+"\" alt=\""+sTitle+"\" title=\""+sTitle+"\" /></div>";
//	+"<div id=\""+sIdiv+"\" class=\"cach\"><a href=\""+sFichier+"\" target=\"_blank\" title=\""+sTitle+" [nouvelle fenetre]\"><img src=\""+sFichier+"\" alt=\""+sTitle+"\" title=\""+sTitle+"\" border=\"0\" /></a></div>";
	return sHtml;
}

function affichPhoto(sIdiv) {
	divPhoto = document.getElementById(sIdiv);
	divPhoto.style.display = "block";
	posPhoto();
}

function cachPhotos(sIdiv) {
	divPhoto = null;
	document.getElementById(sIdiv).style.display = "none";
}

function posPhoto() {
	if (divPhoto != null) {
		divPhoto.style.top = (posY+25)+"px";
		divPhoto.style.left = (posX-(divPhoto.offsetWidth/2))+"px";
	}
}

function posXY(e) {
	if (!e) var e = window.event;
	if (e) {
		if (e.pageX || e.pageY) {
			posX = e.pageX;
			posY = e.pageY;
		} else if (e.clientX || e.clientY) { // IE
//			posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
//			posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
			posX = e.clientX + document.documentElement.scrollLeft;
			posY = e.clientY + document.documentElement.scrollTop;
		}
	}
	posPhoto();
}
document.onmousemove = posXY;

function dyndiv(idDiv, nbDiv, bTbody) {
	var i, divSel, bCache;
	var sDisplay = (bTbody) ? 'table-row-group' : 'block';
	if (divSel = document.getElementById("dyndiv"+idDiv)) {
		bCache = (divSel.style.display == sDisplay) ? true : false;
		for (i=0; i<nbDiv; i++) { document.getElementById("dyndiv"+i).style.display = "none"; }
		divSel.style.display = (bCache) ? "none" : sDisplay;
	}
	return false;
}

