window.onload=function() {getNewBackground();getNewForme();};

function getNewBackground()
{	
	if (window.XMLHttpRequest)
	{
		var xhr = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		var xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Votre navigateur n'est pas compatible avec AJAX...");
	}
	
	xhr.onreadystatechange = function ()
	{
		if(xhr.readyState == 4)
		{
			if (window.opera) 
			{
				document.getElementById("body").style.backgroundImage = "url('imgs/tramervb/" + xhr.responseText + "')";
			}
			else if (document.all) 
			{
				document.getElementById("body").style.setAttribute("cssText","background-image:url(imgs/tramervb/" + xhr.responseText +");");
			}
			else
			{
				document.getElementById("body").setAttribute("style","background-image: url(imgs/tramervb/" + xhr.responseText +");");
			}
		}
	}

	xhr.open("POST","scripts/getBackgroundImage.php",true);
	xhr.send(null);
}

function traiteXmlFormes(pXml)
{
	var arrayFormes = pXml.getElementsByTagName("forme");
	var i=0;
	for(i;i<arrayFormes.length;i++)
	{
		var idDiv = "shape_"  + arrayFormes[i].getAttribute("id");
		var idImg= "img_" + arrayFormes[i].getAttribute("id");
		var arrayChild = arrayFormes[i].childNodes;
		var imgNode = document.createElement("img");
		imgNode.setAttribute("src",arrayChild[0].firstChild.nodeValue);
		imgNode.setAttribute("id",idImg);
		
		document.getElementById(idDiv).innerHTML = "";
		document.getElementById(idDiv).appendChild(imgNode);
		
		var top = arrayChild[1].firstChild.nodeValue;
		var left = arrayChild[2].firstChild.nodeValue;
		var height = arrayChild[3].firstChild.nodeValue;

		var styleDiv = "top:"+ top +";left:"+ left +";position:absolute;"
		var styleImg = "height:"+ height +";width:auto;"
		if (window.opera) 
		{
			document.getElementById(idDiv).setAttribute("style",styleDiv);
			document.getElementById(idImg).setAttribute("style",styleImg);
		}
		else if (document.all) 
		{
			document.getElementById(idDiv).style.setAttribute("cssText",styleDiv);
			document.getElementById(idImg).style.setAttribute("cssText",styleImg);
		}
		else
		{
			document.getElementById(idDiv).setAttribute("style",styleDiv);
			document.getElementById(idImg).setAttribute("style",styleImg);
		}
	}
}

function getNewForme()
{	
	if (window.XMLHttpRequest)
	{
		var xhr = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		var xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Votre navigateur n'est pas compatible avec AJAX...");
	}
	
	xhr.onreadystatechange = function ()
	{
		if(xhr.readyState == 4)
		{
			//alert(xhr.responseXML);
			traiteXmlFormes(xhr.responseXML);
		}
	}

	xhr.open("POST","scripts/getForme.php",true);
	xhr.send(null);
}