//######################################################### Xhr function
function getXhr(){
    var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et other
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	else { // XMLHttpRequest unsupported by the browser
	   alert("Error..."); 
	   xhr = false; 
	} 
    return xhr;
}



//######################################################### Update image
function Update_image(array,tool,tpl, pdf, police, color, etapeEnCours){
	var xhr = getXhr();
	// When we get the response
	xhr.onreadystatechange = function(){
		// do something if the serveur send response
		if(xhr.readyState == 4 && xhr.status == 200){
			content = xhr.responseText;
			// Add content to the left part
			if(etapeEnCours == 2){
				if(pdf ==''){
					document.getElementById('photos_image').innerHTML = '<img src="tmpl/'+tool+'/'+tpl+'/im2.php?tool='+tool+'&tpl='+tpl+'&'+content+'&pdf=" width="450">';
				}else{
			
					document.getElementById('photos_image').innerHTML = '<img src="tmpl/'+tool+'/'+tpl+'/im2.php?tool='+tool+'&tpl='+tpl+'&'+content+'&pdf=ok" width="450">';
				}
			}else{
				if(pdf ==''){
					document.getElementById('photos_image').innerHTML = '<img src="tmpl/'+tool+'/'+tpl+'/im.php?tool='+tool+'&tpl='+tpl+'&'+content+'&pdf=" width="450">';
				}else{
			
					document.getElementById('photos_image').innerHTML = '<img src="tmpl/'+tool+'/'+tpl+'/im.php?tool='+tool+'&tpl='+tpl+'&'+content+'&pdf=ok" width="450">';
				}
			}
			//<img src="script/font.php" width="' + width + '">
		}
		else if (xhr.readyState < 4) {
			document.getElementById("photos_image").innerHTML = "<div id=\"loader\"></div>";
			
		}
	}
	
	// Post information to the view
	xhr.open("POST","script/session_update.php",true);
	// Header for the post
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	//Value to post
	var str="";
	var z=1;
	
	for(var i=0; i<array.length; i++) {		
		
		if(array.elements[i].name == "text" + z)
		{
		 if(z > 1) { str+='&'; }
		 str+= array.elements[i].name +'='+ array.elements[i].value + '';
		 z++;
		}
	}
	
	//xhr.send(str + '&tool='+tool+'&tpl='+tpl+'');

	xhr.send(str + '&tool='+tool+'&tpl='+tpl+'&police='+police+'&color='+color);
}
