// JavaScript Document
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' doit contenir une adresse email\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' doit contenir un nombre.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' doit contenir un nombre entre '+min+' et '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' est obligatoire.\n'; }
  } if (errors) alert('Les champs suivants sont manquants ou incorrects :\n'+errors);
  document.MM_returnValue = (errors == '');
}

function PopupImage(img)
{
    w=open("",'image','toolbar=no,scrollbars=no,resizable=no');
    w.document.write("<html><head><title></title></head>");
    w.document.write("<SCRIPT language=javascript>function ejs_nodroit() {return(false);}document.oncontextmenu = ejs_nodroit;function checksize()  { if (document.images[0].complete) {  window.resizeTo(document.images[0].width +10,document.images[0].height+60); window.focus();} else { setTimeout('check()',250) } }</"+"SCRIPT>");
    w.document.write("<BODY onload='checksize()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0 scrolling=no><IMG src='"+img+"' border=0 onclick='window.close();' alt='Fermer' title='fermer'><br>");
   // w.document.write("<table align='center' width='100%'><tr><td width='50%' align='center'>Previous</td><td align='center'>Next</td></tr></table>");
	w.document.write("");
    w.document.write("</body></html>");
	//alert("largeur image = "+ document.images[0].width+" et hauteur image = "+document.images[0].height);

    w.document.close();
}


function run_flash(url,width,height, wmode)
{
	/*
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="home" align="middle">\n');
	document.write('<param name="movie" value="'+url+'">\n');
	document.write('<embed src="'+url+'" width="'+width+'" height="'+height+'" type="application/x-shockwave-flash">\n');
	document.write('</embed>\n');
	document.write('</object>\n');
*/
	document.write('<object type=\"application/x-shockwave-flash\" data=\"'+url+'" width="'+width+'" height="'+height+'">');
	document.write('<param name="movie" value="'+url+'" />');
	document.write('<param name="wmode" value="'+wmode+'" />');
	document.write('</object>');

}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function ferme_panier_box(){

		document.getElementById('fond_grise_panier').style.display='none';
		if(document.getElementById('print_gauche'))
		{
			document.getElementById('print_gauche').style.visibility='visible';
		}
		
			if(document.choix_panier)
		{
		document.choix_panier.style.visibility='visible';
		}
		
			if(document.getElementById('slogan_club'))
		{
			document.getElementById('slogan_club').style.visibility='';
		}
			if(document.getElementById('cadeau_bienvenue'))
		{
			document.getElementById('cadeau_bienvenue').style.visibility='';
		}

}

function ferme_login_box(){

		document.getElementById('fond_grise_login').style.display='none';
		if(document.getElementById('print_gauche'))
		{
			document.getElementById('print_gauche').style.visibility='visible';
		}
		
			if(document.choix_panier)
		{
		document.choix_panier.style.visibility='visible';
		}
		
			if(document.getElementById('slogan_club'))
		{
			document.getElementById('slogan_club').style.visibility='';
		}
			if(document.getElementById('cadeau_bienvenue'))
		{
			document.getElementById('cadeau_bienvenue').style.visibility='';
		}

}

function ferme_box(){
	if(document.getElementById('print_gauche'))
	{
		document.getElementById('print_gauche').style.visibility='visible';
	}
	
	if(document.choix_panier)
	{
	document.choix_panier.style.visibility='visible';
	}
	
		if(document.getElementById('fin-commande'))
	{
		document.getElementById('fin-commande').style.visibility='visible';
	}
	

document.getElementById('fond_grise_action').style.display='none';
}

function afficherResultats(obj) {
	
	if(document.choix_panier)
	{
	document.choix_panier.style.visibility='hidden';
	}
	if(document.getElementById('slogan_club'))
	{
		document.getElementById('slogan_club').style.visibility='hidden';
	}
		if(document.getElementById('cadeau_bienvenue'))
	{
		document.getElementById('cadeau_bienvenue').style.visibility='hidden';
	}

	document.getElementById('fond_grise_panier').style.display='';
	
	if(document.getElementById('print_gauche'))
	{
	document.getElementById('print_gauche').style.visibility='hidden';
	}
	
	if(document.getElementById('tableau_pass-oublie'))
	{
	document.getElementById('tableau_pass-oublie').style.display='none';
	}
	
	// Construction des noeuds
	tabResulttext = obj.responseText
	
	//alert(tabResulttext);

		
    //tabResult = obj.responseXML.getElementsByTagName('resultat_panier');	//var
	//alert(tabResulttext)
	 if(tabResulttext != "")
	 {
		 document.getElementById('zoneResultats_panier').innerHTML = tabResulttext;
	 } else {
          document.getElementById('zoneResultats_panier').style.visibility = 'hidden';
     }

}


var check_login = null
var deja_passe = null
var valeur_stock = null
// fontion qui ajoute au panier
function ajout_panier(ref_aticle,qte,taille,colori ) {
	
	if(qte == "" || taille == "null" || colori =="null")
	{
			action('ajout_non_renseigne')
			//alert(texte);
			return
	}
	
	if(valeur_stock==0)
	{
			action('article_rupture')
			//alert(texte);
			return
	}

	if(ref_aticle == "carte")
	{
			check_login = 1
			ferme_login_box()
	}
	if(check_login == 1)
	{
		ferme_login_box()
		//return
	}
	else
	
	//(check_login == null)
	{
			
			if(document.choix_panier)
			{
			document.choix_panier.style.visibility='hidden';
			}
			if(document.getElementById('slogan_club'))
			{
				document.getElementById('slogan_club').style.visibility='hidden';
			}
				if(document.getElementById('cadeau_bienvenue'))
			{
				document.getElementById('cadeau_bienvenue').style.visibility='hidden';
			}
		
			document.getElementById('fond_grise_login').style.display='';
			
			if(document.getElementById('print_gauche'))
			{
			document.getElementById('print_gauche').style.visibility='hidden';
			}
			
			if(document.getElementById('tableau_pass-oublie'))
			{
			document.getElementById('tableau_pass-oublie').style.display='none';
			}
			
			return
	}
     // Création de l'objet
    XHR = new XHRConnection(ref_aticle);
	XHR.appendData("d_reference", ref_aticle);
	XHR.appendData("d_qte", qte);
	XHR.appendData("d_num_taille", taille);
	XHR.appendData("d_coloris", colori);
	XHR.appendData("deja_passe", deja_passe);
   //  XHR.appendData("foo", document.getElementById('foo').value);
     // On soumet la requête
     // Signification des paramètres:               
     //      + On indique à l'objet qu'il faut appeler le fichier search.php
     //      + On utilise la méthode POST, adaptée l'envoi d'information
     //      + On indique quelle fonction appeler lorsque l'opération a été effectuée
     XHR.sendAndLoad("../shop/add_panier.php", "POST", afficherResultats);
}


function supprimer_panier(id_panier) {


     // Création de l'objet
    XHR = new XHRConnection(id_panier);
	XHR.appendData("id_panier", id_panier);
     XHR.sendAndLoad("../shop/suppr_panier.php", "POST", afficherResultats);
}

function affiche_panier(panier) {


     // Création de l'objet
    XHR = new XHRConnection("panier");
	XHR.appendData("panier", panier);
     XHR.sendAndLoad("../shop/affiche_panier.php", "POST", afficherResultats);
}

/*
	fonction qui permet de fait comme ci-dessus, mais qui ferme l'ancien bloc
*/
var old_open=null;
function show_hide(obj_) {
	obj = document.getElementById(obj_);
	
	//on ferme l'ancien bloc
	if(old_open) {
		old_open.style.display='none';
	}

	//on sauvegarde l'objet courant et on affiche et on cache l'image centrale par défaut dans le détail du produit
	if(document.getElementById('general_big'))
	{
	document.getElementById('general_big').style.display = 'none';
	}

	obj.style.display = '';
	old_open=obj;
}
var old_open3=null;
function show_hide3(obj_) {
	obj = document.getElementById(obj_);
	
	//on ferme l'ancien bloc
	if(old_open3) {

		if(old_open3 == "")
		{
			//return false
		}
		else
		{
			old_open3.style.display='none';
		}
		old_open3.style.display='none';
	}


	obj.style.display = '';
	old_open3=obj;
}

function afficherResultatsAction(obj) {
	 
	document.getElementById('fond_grise_action').style.display='';
	
	if(document.getElementById('tableau_pass-oublie'))
	{
		document.getElementById('tableau_pass-oublie').style.display='none';
	}
	
		if(document.getElementById('fond_grise_login'))
	{
		document.getElementById('fond_grise_login').style.display='none';
	}
	
	if(document.getElementById('print_gauche'))
	{
			document.getElementById('print_gauche').style.visibility='hidden';
	}
	//alert("Le produit "+d_id_produit+" a bien été ajouté en "+d_qte+" quantité.")

	if(document.getElementById('fin-commande'))
	{
		document.getElementById('fin-commande').style.visibility='hidden';
	}
	
	if(document.choix_panier)
	{
	document.choix_panier.style.visibility='hidden';
	}
    // Construction des noeuds
    tabResult = obj.responseXML.getElementsByTagName('resultat');	//var
	
	tabResulttext = obj.responseText
	//alert(tabResulttext);
	//alert(tabResult.length);
	 
	 document.getElementById('zoneResultats').innerHTML = '';
     if (tabResult.length > 0) {
          // On définit la hauteur de la liste en fonction du nombre de rsultats et de la hauteur de ligne
          var hauteur = tabResult.length * 22; 
          with(document.getElementById('zoneResultats').style) {
               visibility = 'visible';
               height = hauteur + 'px';
			   
          }; 
          for (var i = 0; i < tabResult.length; i++) {
               resultat = tabResult.item(i); 
               var titre_h1 = document.createElement('h1')
			   var egt = document.createElement('li');
               var lnk = document.createElement('a');
               var texte_titre = document.createTextNode(resultat.getAttribute('titre'));
			   var texte = document.createTextNode(resultat.getAttribute('texte'));
               lnk.appendChild(texte);
               lnk.setAttribute('href', resultat.getAttribute('url'));
               lnk.setAttribute('title', resultat.getAttribute('titre'));
               titre_h1.appendChild(texte_titre);
			   egt.appendChild(texte);
               document.getElementById('zoneResultats').appendChild(titre_h1);
			   document.getElementById('zoneResultats').appendChild(egt);
          }
     }
     else {
          document.getElementById('zoneResultats').style.visibility = 'hidden';
     }
	 
}

function action(nom_action,argument) {
     // Création de l'objet
    XHR = new XHRConnection(nom_action);	//var
    
	XHR.appendData("nom_action", nom_action);
	XHR.appendData("email", argument);
   //  XHR.appendData("foo", document.getElementById('foo').value);
     // On soumet la requête
     // Signification des paramètres:               
     //      + On indique à l'objet qu'il faut appeler le fichier search.php
     //      + On utilise la méthode POST, adaptée l'envoi d'information
     //      + On indique quelle fonction appeler lorsque l'opération a été effectuée
     
	 XHR.sendAndLoad("../inc/action.php", "POST", afficherResultatsAction);
}

var ecarte = null
function verif_form_ecarte() {
	if(document.form.id_img.value != null)
	{
		MM_validateForm('nom','','R','prenom','','R','email','','RisEmail','adresses_mail','','R','message','','R');return document.MM_returnValue
		
	}
	else
	{
		action('no_ecarte')
		return false

	}
}

function affiche_carte(carte) {
	show_hide('img_'+carte);
	ecarte=1;
	document.form.id_img.value = carte;
	
}

function GetId(id)
				{
				return document.getElementById(id);
				}
				var i=false; // La variable i nous dit si la bulle est visible ou non
				
				function move(e) {
				  if(i) {  // Si la bulle est visible, on calcul en temps reel sa position ideale
				    if (navigator.appName!="Microsoft Internet Explorer") { // Si on est pas sous IE
				    GetId("curseur").style.left=e.pageX + 5+"px";
				    GetId("curseur").style.top=e.pageY + 10+"px";
				    }
				    else { // Modif proposé par TeDeum, merci à lui
				    if(document.documentElement.clientWidth>0) {
				        GetId("curseur").style.left=20+event.x+document.documentElement.scrollLeft+"px";
				        GetId("curseur").style.top=10+event.y+document.documentElement.scrollTop+"px";
				    }
				    else {
				        GetId("curseur").style.left=20+event.x+document.body.scrollLeft+"px";
				        GetId("curseur").style.top=10+event.y+document.body.scrollTop+"px";
				    }
				    }
				  }
				}
				
				function montre(text) {
				  if(i==false) {
				  GetId("curseur").style.visibility="visible"; // Si il est cacher (la verif n'est qu'une securité) on le rend visible.
				  GetId("curseur").innerHTML = text; // Cette fonction est a améliorer, il parait qu'elle n'est pas valide (mais elle marche)
				  i=true;
				  }
				}
				function cache() {
				if(i==true) {
				GetId("curseur").style.visibility="hidden"; // Si la bulle etais visible on la cache
				i=false;
				}
}


function terminer_commande()
{
	ferme_panier_box()
	window.location.replace( "../shop/fin-commande.php");

}

function check_form_login(email,password)
{
	
	if(email == "" || password == "")
	{
		action('login_error')
		
	}
	if(email != "" && password != "")
	{
		document.form_login.submit();
	}
}

function paiement()
{
	if(document.paiement.cgv.checked  == true)
	{ 	document.paiement.submit(document.paiement.submit); }
	else
	{	msg = "Vous devez accepter les conditions générales de vente."; 
	//alert(msg);
	}
}

function connexion_espace_client (email,password)
{ 
	if(email == "" || password == "")
	{
		action('login_club_error')
	}
	if(email != "" && password != "")
	{
		// Création de l'objet
		XHR = new XHRConnection(email);	//var

		XHR.appendData("email", email);
		XHR.appendData("password", password);
		//  XHR.appendData("foo", document.getElementById('foo').value);
		// On soumet la requête
		// Signification des paramètres:               
		//      + On indique à l'objet qu'il faut appeler le fichier search.php
		//      + On utilise la méthode POST, adaptée l'envoi d'information
		//      + On indique quelle fonction appeler lorsque l'opération a été effectuée
			
		XHR.sendAndLoad("../inc/check_espace_client.php", "POST", resultat_login_espace);
	}

}

function connexion_club (email,password)
{ 
	
	if(email == "" || password == "")
	{
		action('login_club_error')
	}
	if(email != "" && password != "")
	{
		// Création de l'objet
		XHR = new XHRConnection(email);	//var

		XHR.appendData("email", email);
		XHR.appendData("password", password);
		//  XHR.appendData("foo", document.getElementById('foo').value);
		// On soumet la requête
		// Signification des paramètres:               
		//      + On indique à l'objet qu'il faut appeler le fichier search.php
		//      + On utilise la méthode POST, adaptée l'envoi d'information
		//      + On indique quelle fonction appeler lorsque l'opération a été effectuée
			
		XHR.sendAndLoad("../inc/check_club.php", "POST", resultat_login_club);
	}

}

function resultat_login_espace(obj) {
	//tabResult = obj.responseXML.getElementsByTagName('resultat');
	//var login = document.createTextNode(resultat.getAttribute('titre'));
	tabResulttext = obj.responseText
	//alert(tabResulttext);
	
	if(tabResulttext == "ok")
	{
		//action('login_ok')
		window.location.replace( "../espace_client/");
		check_login=1
	}
	if(tabResulttext == "inconnu")
	{
		action('login_inconnu')
	}
	if(tabResulttext == "pas_client")
	{
		//window.status="Vous n'êtes pas membre";
		action('pas_client')
	}
		
}
function resultat_login_club(obj) {
	//tabResult = obj.responseXML.getElementsByTagName('resultat');
	//var login = document.createTextNode(resultat.getAttribute('titre'));
	tabResulttext = obj.responseText
	//alert(tabResulttext);
	
	if(tabResulttext == "ok")
	{
		action('login_ok')
		check_login=1
	}
	if(tabResulttext == "inconnu")
	{
		action('login_inconnu')
	}
	if(tabResulttext == "pas_membre")
	{
		//window.status="Vous n'êtes pas membre";
		action('pas_membre')
	}
		
}


//verif_stock('{$produit.reference}',document.choix_panier.taille.value,document.choix_panier.colori.value,document.choix_panier.qte.value)

function verif_stock(reference,num_taille,coloris,qte) {
	if(reference != "null" && num_taille != "null" && coloris != "null" && qte != 0)
	{
		XHR = new XHRConnection(reference);
		XHR.appendData("reference", reference);
		XHR.appendData("num_taille", num_taille);
		XHR.appendData("coloris", coloris);
		XHR.appendData("qte", qte);
		
		XHR.sendAndLoad("../inc/check_stock.php", "POST", resultat_check_stock);
	}
}

function resultat_check_stock(obj) {
	//tabResult = obj.responseXML.getElementsByTagName('resultat');
	//var login = document.createTextNode(resultat.getAttribute('titre'));
	tabResulttext = obj.responseText
	tab_stock = tabResulttext.split("-")
	
	texte_stock = tab_stock[0]
	
	prix_stock = tab_stock[1]
	prix_stock = prix_stock.replace(".",",")
	prix_stock_club = tab_stock[2]
	prix_stock_club = prix_stock_club.replace(".",",")
	//alert(prix_stock);
	//alert(prix_stock_club);
	
	document.getElementById('zoneprix_stock_club').innerHTML = prix_stock_club+"&nbsp;&euro;";
	document.getElementById('zoneprix_stock').innerHTML = prix_stock+"&nbsp;&euro;";
	 
	if(texte_stock == "ok")
	{
		document.getElementById('delai_stock').style.display="none";
		valeur_stock=1;
	}
	if(texte_stock == "pas_ok")
	{
		document.getElementById('delai_stock').style.display="";
		valeur_stock=0;
	}

		
}

function verif_big_zoom(img_zoom_) {
	//fonction qui permet de checker en Ajax si le grand zoom est disponible, en liaison avec verf_big_zoom_resultat
		XHR = new XHRConnection(img_zoom_);
		XHR.appendData("img_zoom_", img_zoom_);

		XHR.sendAndLoad("../inc/check_img_zoom.php", "POST", verf_big_zoom_resultat);
		
}

function verf_big_zoom_resultat(obj) {
	tabResulttext = obj.responseText
//	alert(tabResulttext);
	tab_zoom = tabResulttext.split("-")
	ref_zoom = tab_zoom[0]
	zoom_ok = tab_zoom[1]
	
	if(zoom_ok == "ok")//on va afficher le lien "Agrandir" et valoriser le lien
 	{
		
		document.getElementById('loop_zoom').style.display=""
		//lien_zoom = "<a href=\"javascript:PopupImage(\'../img_article/big/"+ ref_zoom + ".jpg\')\"><img src=\"../img_fr/picto_voir.gif\" border=\"0\" />Agrandir</a>"
		lien_zoom = "<a href=\"javascript:PopupImage(\'../img_article/big/"+ ref_zoom + ".jpg\')\"><img src=\"../img_fr/picto_voir.gif\" border=\"0\" /></a>"
		document.getElementById('zone_loupe').innerHTML = lien_zoom
		
	}
	else
	{
		document.getElementById('loop_zoom').style.display="none"	
	}
}
