Event.observe(document, 'keypress', variable_session);

nombre_vh_compare = 0;

function nouvelle_fenetre(url,scroll,largeur,hauteur)
{
    window.open(url,"aide","menubar=no, status=no, scrollbars="+scroll+", menubar=no, width="+largeur+", height="+hauteur);
}

function kill_session(){
	var url = "ajax/kill_session.php";
	var myAjax = new Ajax.Request(
						url,
						{
							method: 'get',
							onComplete: function(){
											window.location='index.php';
										}
						});
}

function supprime_compare(indice)
{
    var url = 'ajax/supprime_compare.php';
    pars = "indice="+indice;
	  var myAjax = new Ajax.Request(
					                      url,
                      					{
                      						method: 'get',
                      						parameters: pars,
                      						onComplete: function(reponse)
                                              {
                                                  window.location="index.php?nav="+reponse.responseText;
                                              }
                      					});
}

function propose_enchere(enchere_demande,enchere_en_cours,pas_enchere)
{
		var dividende = Math.ceil((parseInt(enchere_demande) - parseInt(enchere_en_cours))/pas_enchere);
		return (parseInt(enchere_en_cours) + (dividende * pas_enchere));
}

function controle_statut_vente()
{
	var url = 'ajax/controle_statut_vente.php';
	var myAjax = new Ajax.PeriodicalUpdater(
					'heure_actuelle',
					url,
					{
						method: 'get',
						evalScripts: true,
						frequency: 5
					});
}

function variable_session(event)
{
	if (event.charCode == 19 || event.keyCode == 19)
    {
    	kill_session();
	} 
	if(event.charCode == 178 || event.keyCode == 222)
	{
        var url = "ajax/variable_session.php";
	      var myAjax = new Ajax.Request(
              						url,
              						{
              							method: 'get',
              							onComplete: function(requestObject){
              											alert(requestObject.responseText);
              										}
              						});
     }
     
}

function link_accueil(complete){
	var url = "ajax/link_accueil.php";
	var pars = "complete="+complete;
	var myAjax = new Ajax.Request(
						url,
						{
							method: 'get',
							parameters: pars,
							onComplete: function(requestObject){
											window.location = requestObject.responseText;
											//alert(requestObject.responseText);
										}
						});
}

function comparaison()
{
	if(parseInt(nombre_vh_compare,10) < 2){
		alert('Il faut sélectionner au minimun 2 véhicules');
	} else {
		window.location = 'index.php?nav=compare';
	}
}

function assistant(){
    var all_input = document.getElementsByTagName('input');
    var nb_input = all_input.length;
    var pars = '';
    var compteur = 0;
    for(var i=0;i<nb_input;i=i+1){
        if((all_input[i].id).substring(0,9) == 'assistant' && all_input[i].checked==true){
            compteur = compteur + 1;
            pars = pars + '&VH' + compteur + '=' + (all_input[i].id).substring(9);
        }
    }
    var url = 'ajax/nouveau_assistant.php';
    var pars = 'VHtotal='+compteur+pars;
    if (compteur > 1){
        var myAjax = new Ajax.Request(
						                 url,
						                 {
							                   method: 'post',
							                   parameters: pars,
							                   onComplete: function(){
                        window.location="index.php?nav=details_robot&rob_id=0&action=nouveau";
                                                       }
						                 });
    } else {
        alert('Vous devez sélectionner au moins 2 véhicules');
    }
}

function actualise_nb_vh_liste(titre){
		
	$('nb_vh_liste_hidden').value = $('nb_vh_liste_hidden').value - 1;	
	if ($('nb_vh_liste_hidden').value == 0){
		$('nb_vh_liste').innerHTML = '';
		window.location ='index.php?nav=liste_vh';
	}else{
		$('nb_vh_liste').innerHTML = '<a class=\"titre\" href=index.php?nav=details_mesencheres>'+titre+' ('+$('nb_vh_liste_hidden').value+')</A>';
	}
		
}

function retenirVH(veh_id,reference,etat,titre)
{
	if(etat=='true'){
		if($('nb_vh_liste').innerHTML == ''){		
			$('nb_vh_liste_hidden').value = 1;
		} else {
			$('nb_vh_liste_hidden').value = parseInt($('nb_vh_liste_hidden').value) + 1;	
		}
		$('nb_vh_liste').innerHTML = '<a class=\"titre\" href=index.php?nav=details_mesencheres>'+titre+' ('+$('nb_vh_liste_hidden').value+')</A>';
				
		$(veh_id).className = 'mdc';
		$(veh_id).innerHTML = '';
		//$(veh_id).onmouseover = function(){handLink(this);};
		//$(veh_id).onclick = function(){window.location="index.php?nav=details_vh&veh_id="+veh_id;};
	// etat = false => case decochee
	} else {		
		actualise_nb_vh_liste(titre);
		$(veh_id).className = 'nocolor';
	}
	
	var url = "ajax/session_vh.php";
	var pars = "veh_id="+veh_id+"&etat="+etat+"&reference="+reference;	
	var myAjax = new Ajax.Updater(
						'mesencheres',
						url,
						{
							method: 'get',
							parameters: pars,
							evalScripts: true
						});
}

function mail_detail_vh(veh_id,vte_id,ach_id,contenu)
{
	var url = 'ajax/mail_detail_vh.php';
	var pars = "veh_id="+veh_id+"&vte_id="+vte_id+"&ach_id="+ach_id+"&contenu="+contenu;	
	var myAjax = new Ajax.Request(						
						url,
						{
							method: 'get',
							parameters: pars,
							onComplete: function(RequestObject){
								$('contenu_mail').value="";
								$('detaille').style.display='none';
								alert(RequestObject.responseText);
							}
						});
	
}	


function compareVH(veh_id,etat){
	var url = "ajax/compare_vh.php";
	var pars = "veh_id="+veh_id+"&etat="+etat;
	
	var myAjax = new Ajax.Updater(
						'exec_javascript',
						url,
						{
							method: 'get',
							parameters: pars,
							onComplete: function(requestObject){
									if(parseInt(requestObject.responseText) > 4){
										$('compare'+veh_id).checked = false;
										alert('La comparaison se limite à 4 véhicules');
									} else {
										nombre_vh_compare = parseInt(requestObject.responseText);
									}
							}
						});
}

function convertir()
{
	window.location = 'index.php?nav=details_robot&action=nouveau';
}

function enchere_non_valable(det_enchere_max,pas_enchere)
{
  
	var mon_enchere_max = $('det_enchere_max_hidden').innerHTML;
	var ma_nouvelle_enchere = $('det_enchere_max').value;
	var enchere_en_cours = $('enchere_en_cours').innerHTML;
	ma_nouvelle_enchere = propose_enchere(det_enchere_max,enchere_en_cours,pas_enchere);
	if(parseInt(ma_nouvelle_enchere) - parseInt(pas_enchere,10) < parseInt(enchere_en_cours,10)){
		$('det_enchere_max').value = '';
		var montant_minimum = parseInt(enchere_en_cours,10) + parseInt(pas_enchere,10);
		return ('Vous devez saisir un montant minimum de '+montant_minimum);
	}
	return false;
}

function robot_unique(veh_id,reference,det_enchere_max,pas_enchere)
{
  if($('det_enchere_max').value == ''){
      alert('Vous devez entrer un montant');
      return(false);
  }
  if(det_enchere_max >= 1000000){
      alert('Montant maximum dépassé');
      return;
  }
	var message = enchere_non_valable(det_enchere_max,pas_enchere);
	if(!message){
	    var enchere_en_cours = $('enchere_en_cours').innerHTML;
	    ma_nouvelle_enchere = propose_enchere(det_enchere_max,enchere_en_cours,pas_enchere);
		  if(confirm('Vous êtes sur le point de poser une enchère\nmaximale de '+ma_nouvelle_enchere+' €\nContinuer ?')){
		      var url = "ajax/enchere_max.php";
				  var pars = "veh_id="+veh_id+"&reference="+reference+"&det_enchere_max="+ma_nouvelle_enchere;
				  var myAjax = new Ajax.Updater(
									'mesencheres',
									url,
									{
										  method: 'get',
										  parameters: pars,
										  evalScripts: true,  
                      onComplete: function(){
                                  change_class('encheremax');
                                  $('det_enchere_max_hidden').innerHTML = ma_nouvelle_enchere;
                                  $('det_enchere_max').value = '';
                                  $(veh_id).innerHTML = '';
                                  }
                      
									});
		  } else {
		    	$('det_enchere_max').value = '';
		  	alert('Opération annulée');
	  	}
	} else {
		alert(message);
	}
}

function delete_robot_unique(veh_id)
{
	if(confirm("Etes vous sûr de vouloir supprimer ce véhicule de vos préférences. \nContinuer ?")){
		    var url = "ajax/delete_robot_unique.php";
				var pars = "veh_id="+veh_id;
				var myAjax = new Ajax.Updater(
									'mesencheres',
									url,
									{
										method: 'get',
										parameters: pars,
										evalScripts: true,
										onComplete: temp_actualise_liste('non_tri')
									});			
	}
}

function supprimer_assistant(rob_id)
{
    if(confirm("Etes vous sûr de supprimer cet assistant d\'achat. \nContinuer ?")){
		    var url = "ajax/supprimer_assistant.php";
				var pars = "rob_id="+rob_id;

				var myAjax = new Ajax.Request(
									url,
									{
										method: 'get',
										parameters: pars,
										onComplete: function(){window.location = "index.php?nav=assistant";}
									});			
	}
}

function pousser_enchere(enchere_en_cours,pas_enchere)
{
    $('det_enchere_max').value = parseInt(enchere_en_cours) + parseInt(pas_enchere);
}

function creer_robot(tableau,rob_id,pas_enchere)
{
    if($F('rob_vehicule_max') > tableau.length){
        $('rob_vehicule_max').value='';
        alert("Le 'Nombre de véhicules max'\n n'est pas correctement renseigné");
        return;
    }
    if($F('rob_vehicule_max') <= 0){
        $('rob_vehicule_max').value='';
        alert("Le 'Nombre de véhicules max'\n n'est pas correctement renseigné");
        return;
    }
    if($F('rob_nom') == ''){
        $('rob_nom').select();
        alert("Il faut obligatoirement saisir un nom");
        return;
    }
    // ----------- calcul de la somme engagé par l'assistant d'achat -----------
    // ------------ ainsi que le budget minimum pouvant être saisi -------------
    var budget_avance = 0;
    var vh_gagne_virtuel = 0;
		var classe_en_cours = '';
		var budget_minimum = 1000000;
    for(var z=0; z<tableau.length; z++)
    {
        if(($('max'+tableau[z]).parentNode.className) == 'gagnant'){
            budget_avance = budget_avance + parseInt($F('max'+tableau[z]),10);
            vh_gagne_virtuel = vh_gagne_virtuel + 1;
        }
        if(parseInt($F('max'+tableau[z]),10) < budget_minimum){
            budget_minimum = parseInt($F('max'+tableau[z]),10);
        }
    }
    if(budget_avance > $F('rob_budget_max')){
        alert("Le budget maximal ne peut être inférieur à la somme engagée par l'assistant d'achat");
        $('rob_budget_max').value = $F('rob_budget_max_hidden');
        $('rob_budget_max').select();
        return;
    }
    if($F('rob_budget_max') != '' && $F('rob_budget_max') < budget_minimum){
        alert("Le budget minimum n'est pas atteint (égale à la plus petite enchère engagée)");
        $('rob_budget_max').value = $F('rob_budget_max_hidden');
        $('rob_budget_max').select();
        return;
    }
    
    if($F('rob_vehicule_max') < vh_gagne_virtuel){
        alert("Le nombre de véhicules max ne peut être inférieur au nombre de véhicules gagnés par l'assistant d'achat");
        $('rob_vehicule_max').value = $F('rob_vehicule_max_hidden');
        $('rob_vehicule_max').select();
        return;
    }

		
    
		var url = "ajax/creer_robot.php";
		var pars = 'VHtotal='+tableau.length+'&VHmax='+$F('rob_vehicule_max')+'&rob_nom='+$F('rob_nom')+'&rob_id='+rob_id;
		
    var dividende = 0;
    var enchere_arrondie = 0;
    for(var i=0; i<tableau.length; i++)
		{
        if($F('max'+tableau[i]) == ''){
             alert('le montant du véhicule '+(i+1)+' est vide');
            return;
        }
  		  if(parseInt($F('map'+tableau[i]),10)>parseInt($F('max'+tableau[i]),10)){
            alert('le montant du véhicule '+(i+1)+' est inférieur à la mise à prix');
            return;
        }
        dividende = Math.ceil(parseInt($F('max'+tableau[i]),10)/parseInt(pas_enchere,10));
  		  enchere_arrondie = dividende * pas_enchere;
  		  if(parseInt($F('max'+tableau[i]),10) != enchere_arrondie){
            if(confirm('L\'enchère de la ligne ' + (i+1) + ' sera arrondie à ' + enchere_arrondie)){
              $('max'+tableau[i]).value = enchere_arrondie;
            } else {
                $('max'+tableau[i]).select();
                return;
            }
        }
  			ec = i + 1;
  			pars = pars + '&VH'+ec+'='+tableau[i]+'&max'+ec+'='+$F('max'+tableau[i]);
  			pars = pars + '&hidden_max'+ec+'='+$F('hidden_max'+tableau[i])
		}
		
		// -----------------------  calcul du budget max  --------------------------
    if($F('rob_budget_max') == ''){
        var tab_budget_max = new Array();
        for(var z=0; z<tableau.length; z++)
    		{
    		    tab_budget_max[z] = parseInt($F('max'+tableau[z]),10);
    		}
        tab_budget_max.sort(nbrdesc);
        
        var budget_max = 0;
        for(var i=0; i<$F('rob_vehicule_max'); i++)
		    {
		        budget_max = budget_max + parseInt(tab_budget_max[i],10);
		    }
    } else {
        var budget_max = $F('rob_budget_max');
    }
		pars = pars + '&rob_budget_max=' + budget_max;
		
		// ---------------------  vérification des arrondis  ------------------------
    /*
    for(var z=0; z<tableau.length; z++)
    {
        dividende = Math.ceil(parseInt($F('max'+tableau[z]),10)/parseInt(pas_enchere,10));
		    enchere_arrondie = dividende * pas_enchere;
		    if(parseInt($F('max'+tableau[z]),10) != enchere_arrondie){
            if(confirm('L\'enchère de la ligne ' + (z+1) + ' sera arrondie à ' + enchere_arrondie)){
                $('max'+tableau[z]).value = enchere_arrondie;
            } else {
                $('max'+tableau[z]).select();
                return;
            }
        }
    }
    */
		var myAjax = new Ajax.Request(
						url,
						{
							method: 'get',
							parameters: pars,
							onComplete: function(requestObject){
							        var text_reponse = requestObject.responseText;
											alert(text_reponse);
										
											if(text_reponse.substr(0,4) != "Il n"){
											    window.location='index.php?nav=assistant';
											} else {
                          for(var j=0; j<tableau.length; j++)
                      		{
                            if(parseInt($F('hidden_max'+tableau[j]),10) > parseInt($F('max'+tableau[j]),10)){
                                 $('max'+tableau[j]).value = $F('hidden_max'+tableau[j]);
                            }
                          }
                      }
										}
						});
      
}

function nbrdesc(a,b)
{ 
    return b-a;
}

function acheter(veh_id)
{
    var url = "ajax/acheter.php";
    var pars = "veh_id=" + veh_id;
    var myAjax = new Ajax.Request(
						url,
						{
							method: 'post',
							parameters: pars,
							onComplete: function(requestObject)
                          {
                              reponseText = requestObject.responseText;
                              if (reponseText.substring(0,1) == "#"){
                                  alert('Félicitation vous venez d\'acquérir un véhicule de type \n'+reponseText.substring(1));
                                  window.location='index.php?nav=liste_vh';
                              } else {
											            alert(requestObject.responseText);
											        }
										      }
						});

}

function actualise_liste(nomColonne)
{
	var url = "ajax/actualise_liste.php";
	if (nomColonne == ''){
		var pars='';
	} else {
		var pars = 'ajax=oui&nomColonne='+nomColonne+'&'+(window.location.search).substring(1);
	}
	
	var myAjax = new Ajax.Updater(
						'principal',
						url,
						{
							method: 'get',
							parameters: pars,
							evalScripts: true
						});
}

function handLink(element)
{
	element.style.cursor = 'pointer';
}

function temp_actualise_liste(type)
{
	var tmp = "actualise_liste('"+type+"')"; 
	var temporisation = window.setTimeout(tmp,200);
}

function change_class(classe)
{
    $('boite_outils_haut').className = classe;
    $('boite_outils_bas').className = classe;
}

/*******************************************************************************
 *************                   LOCALISATION                     ************** 
 ******************************************************************************/ 
function coordonnees(element) {
	  var posX = 0;
    var posY = 0;
	  if (element.offsetParent) {
		    posX = element.offsetLeft
		    posY = element.offsetTop
		    while (element = element.offsetParent) {
			      posX += element.offsetLeft
			      posY += element.offsetTop
		    }
	  }
	  return [posX,posY];
}

function choisir_departement()
{
    var mybody = document.getElementsByTagName('body')[0];
    if($('carte_france') == null){
        posX = coordonnees($('bouton_localisation'))[0] + $('bouton_localisation').offsetWidth;
        posY = coordonnees($('bouton_localisation'))[1];
        var newdiv = document.createElement('div');
        newdiv.id = "carte_france";
        newdiv.style.left = posX + "px";
        newdiv.style.top = posY + "px";
        mybody.appendChild(newdiv);
        var url = "ajax/departement.php";
        
        var myAjax = new Ajax.Updater(
                            'carte_france',
                  					url,
                  					{
                  						method: 'get'
                  					});
        
    } else {
        mybody.removeChild($('carte_france'));
    }
}

function select_departement(departement)
{
    var url = "ajax/select_departement.php";
	  var pars = "departement=" + departement;  
    var myAjax = new Ajax.Updater(
                            'principal',
                  					url,
                  					{
                  						method: 'get',
                  						parameters: pars,
                  						evalScripts: true,
                  						onComplete: function()
                  						            {
                  						                
                  						                $('select_localisation').value = departement;
                                              $('loc_check').checked = 1;
                                              if($('carte_france') != null){  
                                                  var mybody = document.getElementsByTagName('body')[0];
                                                  mybody.removeChild($('carte_france'));
                                              }
                                          }
                  					});
}

function session_loc(etat)
{
    var url = "ajax/session_loc.php";
	  var pars = "etat="+etat	
	  var myAjax = new Ajax.Updater(
		                				'principal',
                						url,
                						{
                							method: 'get',
                							parameters: pars,
                							evalScripts: true
                						});
}
/******************************************************************************/

function actualise_recherche(nomChamp,valeurChamp)
{
	var url = "ajax/actualise_recherche.php";
	if(nomChamp){
		var pars = "nomChamp="+nomChamp+"&valeurChamp="+escape(valeurChamp);
		pars = pars + "&"+(window.location.search).substring(1);
	} else {
		var pars = "";
	}
	var myAjax = new Ajax.Updater(
						'recherche',
						url,
						{
							method: 'post',
							parameters: pars,
							evalScripts: true,
							onComplete: temp_actualise_liste('non_tri')
						});
}

function rch_libre()
{
	t = window.setTimeout("recherche()",200);
}

function recherche()
{
	var recherche = $('rch_libre').value;
	var url = "ajax/rch_libre.php";
	var pars = "ajax=oui&recherche="+recherche+'&'+(window.location.search).substring(1);
	var myAjax = new Ajax.Updater(
					'principal',
					url,
					{
						method: 'post',
						parameters: pars,
						evalScripts: true
					});
}

function actualise_enchere_details(veh_id)
{
	var url = "ajax/actualise_enchere_details.php";
	var pars = "veh_id="+veh_id;
	var myAjax = new Ajax.PeriodicalUpdater(
					'exec_javascript',
					url,
					{
						method: 'get',
						frequency: 3,
						parameters: pars,
						evalScripts: true
					});
}

function actualise_enchere_liste(tableau)
{
	  var url = 'ajax/actualise_enchere_liste.php';
	  var pars = 'total='+tableau.length;
		for(var i=0; i<tableau.length; i++)
		{
		  	ec = i + 1;
			  pars = pars + '&VH'+ec+'='+tableau[i];
		}

	  myUpdaterAjax = new Ajax.PeriodicalUpdater(
					'exec_javascript',
					url,
					{
						  method: 'post',
						  parameters: pars,
						  frequency: 4,
						  evalScripts: true
					});
}

function actualise_mes_encheres(tableau)
{
    var url = 'ajax/actualise_mes_encheres.php';
	  var pars = 'total='+tableau.length;
    for(var i=0; i<tableau.length; i++)
		{
		  	ec = i + 1;
			  pars = pars + '&VH'+ec+'='+tableau[i];
		}
    myUpdaterAjax = new Ajax.PeriodicalUpdater(
					'exec_javascript',
					url,
					{
						method: 'get',
						parameters: pars,
						evalScripts: true
					});				
}

function actualise_robot_details(tableau)
{
    var url = 'ajax/actualise_robot_details.php';
	  var pars = 'total='+tableau.length;
    for(var i=0; i<tableau.length; i++)
		{
		  	ec = i + 1;
			  pars = pars + '&VH'+ec+'='+tableau[i];
		}
    myUpdaterAjax = new Ajax.PeriodicalUpdater(
					'exec_javascript',
					url,
					{
						method: 'get',
						parameters: pars,
						evalScripts: true
					});				
}

function invitation_vente(vte_id)
{
    //alert(vte_id);
    var	url= "ajax/invitation.php";
    var params="vte_id="+vte_id;
    o_options = new Object();
    o_options = {method:'post',parameters:params,evalScripts:true};
    var laRequete = new Ajax.Updater($('exec_js'),url,o_options);  
}

function inscription()
{
    /*alert ('Vous devez vous inscrire pour effectuer cette opération');
    if(confirm('Voulez vous vous inscrire?')){      
      window.location = "index.php?nav=inscription";
    }*/
	_P_.show('non_identifie');
}

function revalidation(){
	/********************/
	/*if(confirm("Votre validation bancaire n\'est plus active\nProcéder à la validation bancaire ?")){      
	    window.location = "index.php?nav=inscription";
	}*/
	/*******************/
	//fenetre.show("validation_bq");
	_P_.show("validation1");
	
	
}

function deconnexion()
{    
    var	url= "ajax/logout.php";    
    o_options = new Object();
    o_options = {method:'get',onComplete: function(){
											window.location='index.php';
										}};
    var laRequete = new Ajax.Request(url,o_options);  
}

function IsMail(valeur)
{
  var mail = /^[\w\-]+(\.[\w\-]+)*@[\w\-]+(\.[\w\-]+)*\.[\w\-]{2,}$/;
  var reponse=mail.test(valeur)
  if (reponse) {
    return true;
  } else {        
      return false;
  }
}

function IsNonVide(valeur)
{
  var reponse = false;    
  for (var i=0;i<=valeur.length;i++){
    if(valeur.charAt(i) != "" && valeur.charAt(i) != " "){
      reponse = true;
      break; 
    }    
  }    
  return reponse;    
}

function IsNumeric(valeur)
{    
  var anum=/(^\d+$)|(^\d+\.\d+$)/
  if (anum.test(valeur)){
      reponse = true;
  } else {        
      reponse = false
  }
  return reponse;
}

function IsNumCompte(valeur,nombre)
{
 var numtrouve = 0;
  var reponse = false;
  for (var i=0;i<=valeur.length;i++){
    if (IsNumeric(valeur.charAt(i))){
      numtrouve++;
    }
  }
  if(numtrouve==nombre){
    reponse = true;      
  }    
  return reponse;    
}

function IsNumTel(valeur){
  var numtrouve = 0;
  var reponse = false;
  for (var i=0;i<=valeur.length;i++){
    if (IsNumeric(valeur.charAt(i))){
      numtrouve++;
    }
  }
  if(numtrouve>=10){
    reponse = true;      
  }    
  return reponse;    
}

function verif_form_auth(formulaire,champs)
{
  var email=document.getElementById(champs).value;
  if (IsMail(email)==true){    
    var	url= "ajax/verif_mail.php";
    var pars = 'login='+email;    
    o_options = new Object();
    o_options = { method:'post',
                  parameters:pars,                  
                  onComplete: function(requestObject)
                          {
                              reponseText = requestObject.responseText;
                              
                              if (reponseText == "ok"){ 
                                                             
                                    formulaire.submit();
                              } else {
                                    alert(reponseText);
                                    $('login').value=$('captcha_login').value;
                                    $('password').select();
                                    $('captcha_login').value="";
                                    $('captcha_code').value="";
                                    
                              }                
										      }
										};
    var laRequete = new Ajax.Request(url,o_options);
    return true;
  } else {            
      alert("E-mail non valide!\nVeuillez recommencer!");
      document.getElementById(champs).select();
      return false;
  }
}

function log_on(){
  var ach_login= document.getElementById('login').value;
  var ach_password= document.getElementById('password').value 
  var memo = 0;
  if (document.getElementById('memo').checked == true){
      memo = 1;
  }
  
  if (IsMail(ach_login)==true){
    //alert ("lancement_ajax");
    var	url= "ajax/trasfert_session_panier.php";
    var pars = 'ach_login='+ach_login+'&ach_password='+ach_password+'&memo='+memo; 
          
    o_options = new Object();
    o_options = { method:'post',
                  parameters:pars,                  
                  onComplete: function(requestObject)
                          {
                              reponseText = requestObject.responseText;
                              
                              switch(reponseText){
                                case "auth_classique":
                                  delCookie("cardan_e");
                                  delCookie("cardan_p");
                                  //alert('vous êtes authentifié !');
                                  window.location.reload();
                                break;
                                case "auth_classique1":                                 
                                  setCookie("cardan_e",$('login').value,365);
                                  setCookie("cardan_p",$('password').value,365);
                                  //alert('vous êtes authentifié !');
                                  window.location.reload();
                                break;
                                case "compte_existant":
                                  alert("vous êtes déjà authentifié !");
                                  document.getElementById('login').value="";
                                  document.getElementById('password').value="";
                                break;
                                case "transfert":
                                  //alert("Transfert de compte ! ");
                                  window.location.reload();                                                                   
                                break;
                                default:
                                  alert(requestObject.responseText);
                                break;
                              }                              
										      }
										};
    var laRequete = new Ajax.Request(url,o_options);
    return true;
    } else {            
      alert("E-mail non valide!\nVeuillez recommencer!");
      document.getElementById('login').select();
      return false;
    }
}

function setCookie(nom,valeur,jours) {
    var expDate = new Date()
    expDate.setTime(expDate.getTime() + (jours * 24 * 3600 * 1000))
    document.cookie = nom + "=" + escape(valeur)
    + ";expires=" + expDate.toGMTString()
}
function delCookie(nom) { setCookie(nom,"",-1) }

function valid_compte(reponse,ach_id){
    var	url= "../ajax/validation_bancaire.php";
    var pars = 'reponse='+reponse+'&ach_id='+ach_id; 
    
    o_options = new Object();
    o_options = { method:'post',
                  parameters:pars,                  
                  onComplete: function(requestObject)
                          {
                              reponseText = requestObject.responseText;
                              
                              switch(reponseText){
                                case "oui":                                  
                                  window.opener.document.getElementById('validation_bancaire').innerHTML = "le compte est validé";    
                                  window.close();                       
                                break;
                                case "non": 
                                   window.opener.document.getElementById('validation_bancaire').innerHTML = "le compte n'est pas validé";   
                                   window.close();                                
                                break;                                
                                default:
                                  alert(requestObject.responseText);
                                break;
                              }                              
										      }
										};
    var laRequete = new Ajax.Request(url,o_options);
}

function trace_presence(){
      var url = 'ajax/trace_presence.php';
	    var myAjax = new Ajax.PeriodicalUpdater(
      		'presence',			
					url,
					{
						method: 'get',
						frequency: 10,
						evalScripts:true,
						onComplete: function(requestObject){                          
                           trace_presence();                          
										    }						    
					
			});
}

function go_to_inscription(){
        window.location = "index.php?nav=inscription";		
}