// JavaScript Document
var xhr = null;

//Fonction de creation de l'objet XMLHttpRequest qui resservira pour chaques fonctions AJAX
function getXhr()
{
if(window.XMLHttpRequest) xhr = new XMLHttpRequest(); 
else if(window.ActiveXObject)
{  
 try
  {
   xhr = new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
   xhr = new ActiveXObject("Microsoft.XMLHTTP");
  }
}
else 
{ 
 alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest, veuillez le mettre à jour"); 
 xhr = false; 
} 
}

// Fonction utilisée pour les formulaires des différentes pages d'administration
function ajax_admin_default(panel, action, id, value, page)
{
  getXhr();
  xhr.onreadystatechange = function()
   {
    if(xhr.readyState == 4 && xhr.status == 200)
     {
	  document.getElementById('ajax_'+panel).innerHTML = xhr.responseText;
	  
	  // on met en service les nouveaux sélecteurs de couleurs
	  jscolor.bind();
     }
   }
	envoi("ajax/"+panel+".php","id="+id+"&action="+action+"&value="+value+"&page="+page );
}

function ajax(fichier, variables, div)
{
	loadingBox(true);
  getXhr();
  xhr.onreadystatechange = function()
   {
    if(xhr.readyState == 4 && xhr.status == 200)
     {
    	if (div) {
    		document.getElementById(div).innerHTML = xhr.responseText;
    	}
		loadingBox(false);
     }
   }
	envoi("inc.php","incUrl="+fichier+"&"+variables);
}

function ajax_simple(fichier, div, var1_name, var1, var2_name, var2)
{
	loadingBox(true);
  getXhr();
  xhr.onreadystatechange = function()
   {
    if(xhr.readyState == 4 && xhr.status == 200)
     {
	  document.getElementById(div).innerHTML = xhr.responseText;
		loadingBox(false);
     }
   }
	envoi("inc.php","incUrl="+fichier+"&ajax=1&"+var1_name+"="+var1+"&"+var2_name+"="+var2 );
}

function ajax_send_form(fichier, div, action, id)
{
loadingBox(true);
  getXhr();
  xhr.onreadystatechange = function()
   {
    if(xhr.readyState == 4 && xhr.status == 200)
     {
    	var oldHeight = $('#'+div).height();
		document.getElementById(div).innerHTML = xhr.responseText;
    	var newHeight = $('#'+div).height();
    	
    	if (newHeight>oldHeight) {
    		//$.scrollTo( '+='+(newHeight - oldHeight)+'px', 1 );
    	}
    	else {
    		//$.scrollTo( '-='+(oldHeight - newHeight)+'px', 1 );
    	}
    	
		// on met en service les nouveaux contrôles
		ajax_reload_script('#'+div+' ');
		
		runScripts(document.getElementById(div)); //run all scripts now contained in the target div element
		loadingBox(false);
     }
   }
   var groupvar = get_form();
	envoi("inc.php","ajax=1&incUrl="+fichier+"&id="+id+"&action="+action+get_form() );
}


function ajax_send_form_dual(fichier, div, action, id, fichier2, div2, action2, id2)
{
	loadingBox(true);
  getXhr();
  xhr.onreadystatechange = function()
   {
    if(xhr.readyState == 4 && xhr.status == 200)
     {
	  document.getElementById(div).innerHTML = xhr.responseText;
	  ajax_send_form(fichier2, div2, action2, id2);
		loadingBox(false);
	  
		
		runScripts(document.getElementById(div)); //run all scripts now contained in the target div element
     }
   }
   var groupvar = get_form(); 
	envoi("inc.php","ajax=1&incUrl="+fichier+"&id="+id+"&action="+action+get_form() );
}


function ajax_send_form_triple(fichier, div, action, id, fichier2, div2, action2, id2, fichier3, div3, action3, id3)
{
	loadingBox(true);
  getXhr();
  xhr.onreadystatechange = function()
   {
    if(xhr.readyState == 4 && xhr.status == 200)
     {
	  document.getElementById(div).innerHTML = xhr.responseText;
	  ajax_send_form_dual(fichier2, div2, action2, id2, fichier3, div3, action3, id3);
		loadingBox(false);
	  
		runScripts(document.getElementById(div)); //run all scripts now contained in the target div element
     }
   }
   var groupvar = get_form();
	envoi("inc.php","ajax=1&incUrl="+fichier+"&id="+id+"&action="+action+get_form() );
}

function ajax_send_form_quad(fichier, div, action, id, fichier2, div2, action2, id2, fichier3, div3, action3, id3, fichier4, div4, action4, id4)
{
	loadingBox(true);
  getXhr();
  xhr.onreadystatechange = function()
   {
    if(xhr.readyState == 4 && xhr.status == 200)
     {
	  document.getElementById(div).innerHTML = xhr.responseText;
	  ajax_send_form_triple(fichier2, div2, action2, id2, fichier3, div3, action3, id3, fichier4, div4, action4, id4);
		loadingBox(false);
	  
		runScripts(document.getElementById(div)); //run all scripts now contained in the target div element
     }
   }
   var groupvar = get_form();
	envoi("inc.php","ajax=1&incUrl="+fichier+"&id="+id+"&action="+action+get_form() );
}
 
 function envoi(nom_fich,chaine)
{
	xhr.open("POST",nom_fich,true);
  	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=ISO-8859-1');
	
	 try
     {
      xhr.overrideMimeType('text/html; charset=ISO-8859-1'); 
     }
    catch (e)
     {
	 	 try
		 {
		 xhr.setRequestHeader("Content-Type", 'text/html; charset=ISO-8859-1');
		 }
		catch (e)
		 {
		 var error = '';
		 }
     }
	
  	xhr.send(chaine);
}
