function getHTTPObject(dest)
{
  var xmlhttp = false;

  /* Compilation conditionnelle d'IE */
  /*@cc_on
  @if (@_jscript_version >= 5)
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end @*/

  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }

  if (xmlhttp)
  {
     /* on définit ce qui doit se passer quand la page répondra */
     xmlhttp.onreadystatechange=function()
     {
        if (xmlhttp.readyState == 4) /* 4 : état "complete" */
        {
           if (xmlhttp.status == 200) /* 200 : code HTTP pour OK */
           {
              /*
              Traitement de la réponse.
              Ici on affiche la réponse dans une boîte de dialogue.
              */
              document.getElementById(dest).innerHTML=xmlhttp.responseText;
		runScripts(document.getElementById('cont'));
           }
           else alert ('ajax error : '+xmlhttp.responseText);
        }
     }
  }
  return xmlhttp;
}

function recupmenu(myid,mymsg) {
    
if (!mymsg) mymsg='';    
                /* Création de l'objet : */
var xmlhttp = getHTTPObject('cont'); 
/* Préparation d'une requête asynchrone de type GET : */
xmlhttp.open("GET", "menu.php?item="+myid+'&ajax=true&mymsg='+mymsg,true); 
/* Effectue la requête : */
xmlhttp.send(null);

    return true;
}


function mylogin(user, pass){
	var myparam='username='+user+'&password='+pass+'&ajax=true';
	var xmlhttp = getHTTPObject('cont'); 
	xmlhttp.open("POST", "paniers.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", myparam.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(myparam);
}

function mylogout(){	
	var xmlhttp = getHTTPObject('cont'); 
	xmlhttp.open("GET", 'paniers.php?logout=true&ajax=true',true); 
	xmlhttp.send(null);	
}

function mypasswd(mymsg){
	if (!mymsg) mymsg='';
	 var xmlhttp = getHTTPObject('cont');
         xmlhttp.open("GET", 'conf/passwd.php?ajax=true&mymsg='+mymsg,true);
	 xmlhttp.send(null);
}


function mypasswdchg(oldpass,newpass,newpass1){
	if (!oldpass) oldpass='';
	if (!newpass) newpass='';
	if (!newpass1) newpass1='';
	var myparam='oldpass='+oldpass+'&newpass='+newpass+'&newpass1='+newpass1+'&ajax=true';
        var xmlhttp = getHTTPObject('cont');
        xmlhttp.open("POST", "/conf/passwd.php", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", myparam.length);
        xmlhttp.setRequestHeader("Connection", "close");
        xmlhttp.send(myparam);
}



function myshowsemaine(semaine){
	//alert(semaine);
	var myparam='semaine='+semaine;
	var xmlhttp = getHTTPObject('cont'); 
	xmlhttp.open("POST", "paniers/p_listpanier.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", myparam.length);
	xmlhttp.setRequestHeader("Connection", "close");	
	xmlhttp.send(myparam);
}

function addorder()
{
	var myparam='';
	myselects = document.getElementsByTagName("select");
	var confstr='Récapitulatif de votre commande :\n';
	var total=0;
	for (var i = 0; i < myselects.length; i++)
	{
		//if (myselects[i].value !=0)
		{
			var prixuni=0;
			if (myselects[i].name.substr(0,5) == 'ordep') 
			{ prixuni= parseFloat(document.getElementById('pricep'+myselects[i].name.substr(5)).value);}
			else { prixuni= parseFloat(document.getElementById('price'+myselects[i].name.substr(5)).value);}
			var quantite= myselects[i].value;
			if (myselects[i].value !=0) {confstr+= quantite+' x ' + myselects[i].id + '(' + prixuni +' €)\n'};
			total= total + prixuni * quantite;

			if (myparam =='') myparam+=myselects[i].name+'='+myselects[i].value;
			else myparam+='&'+myselects[i].name+'='+myselects[i].value;
		}
	}
	totcredit=parseFloat(document.getElementById('credit').value);
	totprevcredit=parseFloat(document.getElementById('prevcredit').value);
	totcredit=totcredit+totprevcredit;
	if (total > totcredit) {alert('Vous n\'avez pas assez de crédit\n'+total+' < '+totcredit);return}
	confstr+='\nSoit un total de '+ total +' €\nVeuillez svp confirmer';
	if (confirm(confstr))
	{
	var xmlhttp = getHTTPObject('cont'); 
	xmlhttp.open("POST", "orders/p_order.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", myparam.length);
	xmlhttp.setRequestHeader("Connection", "close");	
	xmlhttp.send(myparam);
	}
}

function runScripts(e) {
	if (e.nodeType != 1) return; //if it's not an element node, return
 
	if (e.tagName.toLowerCase() == 'script') {
		eval(e.text); //run the script
	}
	else {
		var n = e.firstChild;
		while ( n ) {
			if ( n.nodeType == 1 ) runScripts( n ); //if it's an element node, recurse
			n = n.nextSibling;
		}
	}
}

