// JavaScript Document
function MM_openBrWindow(theURL,winName,features) 
	// pour le pop up
	{ //v2.0
	  window.open(theURL,winName,features);
	  return false;
	}		

function guide_taille()
	{
		$('#fiche-guide-taille:hidden').show().animate({left:'+=240'},700);
		return false;
	}
	
	
function load_ajax()
	//liens pour slide panier
	{
		$('#nav-haut a#continuer').click(function(){
			$('#conteneur').slideUp('slow');
		});
	}
	
function load_compte()
	//telecharger le compte
	{
		$('#conteneur').load('commun/panier/log_compte.php',function (responseText, textStatus, XMLHttpRequest) 
		{
			if( textStatus=="success" )
			{
				$('#nav-haut').attr('class','change');
				load_ajax();
				$('#conteneur').attr('class','compte').slideDown('slow');
				
				$('a#inscrip').click(function(){
					$('#conteneur').load('commun/panier/'+langue+'/inscrip.php');
					return false;
				});
				
				$('input#button.etiquette.bouton').click(function(){
					var dataString = 'com=loger&email='+$('input#email').val()+'&password='+$('input#passe.input').val();
					$.ajax({
						type: "POST", 
						url: "commun/panier/front_loger.php", 
						data: dataString, 
						success: function() {
							load_compte();
							deloger();
						} 
					});//fin de AJAX
				});
				deloger();
			}
		});
	}

function deloger()
	{
		$('a#deloger').click(function(){
			var dataDeloger = 'com=deloger';
			$.ajax({
				type: "POST", 
				url: "commun/panier/front_loger.php", 
				data: dataDeloger, 
				success: function() {
					load_compte();
				}
			});//fin de AJAX
		});
	}

function nouveler_panier(etape)
	//retelecharger le panier
	{
		var_etp = ( etape == null ) ? '' :'?etp='+etape;
		$('#conteneur').load('commun/panier/load_panier.php'+var_etp,function (responseText, textStatus, XMLHttpRequest) 
			{
				if( textStatus == "success" )
				{
					$('#conteneur').html(responseText);
					$('#conteneur').attr('class','panier').slideDown('slow');
					$('#nav-haut').attr('class','normal');
					load_ajax();
					$('.suppanier').click(function(){
						suppanier( $(this) );
						return false;
					});
					if( etape == 2 )
					//si c'est le panier,ajouter le 'href' pour le boutton commander
					{
						$('div#precedent_etape a').click(function(){
							nouveler_panier(1);
						});
					}
				}
			});
		return false;
	}
	
function aupanier()
	//ajouter un produit dans le panier
	{
		nom    = $('input#nom').val();
		
		if( $("select#taille option:selected").val() == 'choisir' || $("select#taille option:selected").val() == 'choose' )
		{
			if( langue == 'francais' ) alert('vous devez choisir une taille.');
			else if( langue == 'english' ) alert('you must select a size.');
			return false;
		}
		else
		{
			qte    = $('input#qte').val();
			data = 'nom='+nom+'&taille='+$("select#taille option:selected").val()+'&qte='+qte;	
			$.ajax({
				type: "POST", 
				url: "commun/panier/setpanier.php", 
				data: data, 
				success: function() 
					{
						if( $('#conteneur:visible').length == 0 )
						{
							$('#a1 a').click();
						}
						else
						{
							nouveler_panier( null );
						}
					} 
			});
		}
	}
	
function suppanier( cible )
	//suppremer un produit dans le panier
	{
		supproduit = cible.attr('id');
		taille = cible.attr('rev');
		$.ajax({
				//recuperer la session
				type: "POST", 
				url: "commun/panier/suppanier.php", 
				data: supproduit = 'nom='+supproduit+'&taille='+cible.attr('rev'), 
				//recuperer la session
				
				success: function()
					{
						//recuperer le HTML
						if( $('#global2 tr.pro_pa').length > 1 )
						{
							cible.parent('td').parent('tr').remove();
						}
						else if( $('#global2 tr.pro_pa').length == 1 )
						{
							nouveler_panier( null );
						}
						//recuperer le HTML
					} 
		})
		return false;
	}
	
function passe_oublie()
	//envoyer un e-mail pour le cas "mot de passe oublie"
	{
		if( langue == 'francais' )
		{
			remplir = 'rempliez le champs email,SVP.';
			envoye  = 'Un mail va vous etre envoye';
			pas_enrgistre = 'Cette adresse e-mail n\'est pas enregistr&eacute;, vous pouvez vous inscrire maintenant';
			reussi_mot_pass = 'Votre mot de passe &agrave; &eacute;t&eacute; envoy&eacute; vers votre e-mail';
			probleme = 'il y a une probl&egrave;me avec la fonction d\'envoyer le mail.'
		}
		else if( langue == 'english' )
		{
			remplir ='fill in the fields email, please.';
			envoye  = 'The mail is currently being sent...';
			pas_enrgistre = 'Cette adresse e-mail n\'est pas enregistr&eacute;, vous pouvez vous inscrire maintenant';
			reussi_mot_pass = 'your new password is already sent to your e-mail.';
			probleme = 'there is a problem with the function of sending the mail.'
		}
		if( $('input#email').val() == '' ) $('p#notice, p#notice_panier').html(remplir);
		else
		{
			dataStr = 'com=oublie&email='+$('input#email').val()+'&langue='+langue;
			$.ajax({
				type:'POST',
				url:'commun/panier/inscrip.foc.php',
				data:dataStr,
				dataType:'json',
				beforeSend:function()
				{
					$('p#notice, p#notice_panier').html(envoye);
				},
				success:function(reponse)
				{
					if( reponse == 'false' ) $('p#notice, p#notice_panier').html(pas_enrgistre);
					else if( reponse == 'true' ) $('p#notice, p#notice_panier').html(reussi_mot_pass);
					else if( reponse == 'null' ) $('p#notice, p#notice_panier').html(probleme);
				}
			});
		}
		return false;
	}
	
function modif_qte_panier( cible )
	//changer les quantites.
	{
		maxQte = parseInt(cible.attr('title'));
		qte = parseInt(cible.val());
		
		notice = '';
		notice = ( langue == 'francais' ) ? "Quantite maximum en stock: "+maxQte+"\nVeuillez corriger s'il vous plait." : notice;
		notice = ( langue == 'english' ) ? "Maximum quantity in stock: "+maxQte+"\nPlease correct." : notice;
		
		if( maxQte < qte )
		{
			alert( notice );
			qte = maxQte;
		}
		dataStr = 'com=m_qte&nom='+cible.parent('td').parent('tr').find('td a.suppanier').attr('rel')+'&taille='+cible.parent('td').parent('tr').find('span.rien').html()+'&qte='+qte;
		$.ajax({
			type:"POST",
			url:"commun/panier/suppanier.php",
			data:dataStr,
			success:function(){
				nouveler_panier();
			}
		});
	}
	
function modif_taille_panier( cible )
	//modifier la taille pour les produits
	{
		cible.addClass('choisir');
		dataStr = 'com=m_taille&nom='+cible.parent('td').parent('tr').find('td a.suppanier').attr('rel')+'&taille='+cible.parent('td').parent('tr').find('span.rien').html()+'&nouv_taille='+cible.find("option:selected").val();
		$.ajax({
			type:"POST",
			url:"commun/panier/suppanier.php",
			data:dataStr,
			success:function()
			{
				nouveler_panier();
			}
		});
	}
	
function code_promo( cible )
	//utiliser un code promotion
	{
		erreur = ( langue == 'francais' ) ? 'Votre code n\'est pas correct.' : '';
		erreur = ( langue == 'english' ) ? 'Your code isn\'t correct.' : erreur;
		
		if( cible.val().length < 6 ) return false;
		else if( cible.val().length == 6 )
		{
			dataStr = 'com=promo&code='+cible.val();
			$.ajax({
				type:"POST",
				url:"commun/panier/suppanier.php",
				dataType:'json',
				data:dataStr,
				success:function(reponse){
					if( reponse == 'true' ) nouveler_panier();
					else if( reponse == 'false' )
					{
						alert(erreur);
						clble.val('');
					}
				}
			});
			return false;
		}
	}
	
function loger()
	//incrire un nouveau client
	{
		if( $('input#email').val().length < 6 || $('input#mot_de_passe').val().length < 6 )
		{
			alert('Merci de remplir E-mail ou Mot de passe!');
		}
		else
		{
			var dataString = 'com=loger&email='+$('input#email').val()+'&password='+$('input#mot_de_passe').val();
			$.ajax({
				type: "POST", 
				url: "commun/panier/front_loger.php", 
				data: dataString, 
				success: function() {
					nouveler_panier(2);
				} 
			});//fin de AJAX
		}
		return false;
	}
	
function load_form()
	//telecharger le formulaire anglais pour incrire
	{
		url = ( langue == 'francais' )? 'commun/panier/html/francais/inscrip.php':'';
		url = ( langue == 'english' )? 'commun/panier/html/english/inscrip.php':url;
		$('#conteneur').load(url);
		return false;
	}
function chkform_francais( id, cible )
	//verifier les informations dans la formulaire
	{
		var notice = null;
		
		switch( id )
		{	
			case 'email':
				reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
				if( cible.find('input#email').val() == '' )
				{
					notice = 'Attention : Ce champ doit &ecirc;tre rempli.';
				}
				else if( !reg.test(cible.find('input#email').val()) )
				{
					notice= 'Le mail ne peut pas contenir d\'espace, ni de lettres sp&eacute;ciales, et il doit contenir un @ .';
				}
				break;
				
			case 'password1':
				reg = /^[a-zA-Z]{1}\w{5,17}$/;
				if( !reg.test(cible.find('input#password1').val() ) )
				{
					notice = 'Attention : Ce champs doit avoir de 6 &agrave; 18 lettres ou chiffres et le premier doit &ecirc;tre 1 lettre.';
				}
				break;
				
			case 'password2':
				if( cible.find('input#password2').val() != cible.prev('div.champs').find('input#password1').val() )
				{
					notice = 'Attention : Les deux mots de passe doivent &ecirc;tre pareils.'
				}
				break;
				
			case 'nom':
				if( cible.find('input#nom').val() == '' )
				{
					notice = 'Attention : Ce champ doit &ecirc;tre rempli.';
				}
				break;
				
			case 'prenom':
				if( cible.find('input#prenom').val() == '' )
				{
					notice = 'Attention : Ce champ doit &ecirc;tre rempli.';
				}
				break;
				
			case 'sexe': 
				break;
				
			case 'birthday':
				reg = /^[0-3]{1}\d{1}\/{1}[0-1]{1}\d{1}\/{1}[1-2]{1}\d{3}$/;
				if( !reg.test( cible.find('input#birthday').val() ) )
				{
					notice = 'Attention : Le format de la date doit &ecirc;tre &eacute;crit comme ceci: "jj/mm/aaaa".';
				}
				break;
				
			case 'telephone': 
				reg = /^[0-9]+$/;
				if( !reg.test( cible.find('input#telephone').val() ) )
				{
					notice = 'Attention : Ce champ ne peut contenir que les chiffres.'
				}
				break;
				
			case 'adresse': 
				if( cible.find('input#adresse').val() == '' )
				{
					notice = 'Attention : Ce champ doit &ecirc;tre rempli.';
				}
				break;
				
			case 'complementaire': 
				break;
							
			case 'postal': 
				reg = /^[0-9]{1,}$/;
				if( !reg.test( cible.find('input#postal').val() ) )
				{
					notice = 'Attention : Ce champ ne peut contenir que les chiffres.'
				}
				break;
				
			case 'ville': 
				if( cible.find('input#ville').val() == '' )
				{
					notice = 'Attention : Ce champ doit &ecirc;tre rempli.';
				}
				break;
				
			case 'pays': 
				break;
				
			default:
				notice = 'Attention : Ce champ n\'existe pas.'
				
		}
		return notice;
	}
function chkform_english( id, cible )
	//verifier les informations dans la formulaire
	{
		var notice = null;
		
		switch( id )
		{	
			case 'email':
				reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
				if( cible.find('input#email').val() == '' )
				{
					notice = 'Warning : This is an obligatory field.';
				}
				else if( !reg.test(cible.find('input#email').val()) )
				{
					notice= 'Warning : The mail can not contain the space, letters of special purpose, and it must contain \'@ \'.';
				}
				break;
				
			case 'password1':
				reg = /^[a-zA-Z]{1}\w{5,17}$/;
				if( !reg.test(cible.find('input#password1').val() ) )
				{
					notice = 'Warning : This field needs to contain 6 to 18 letters et the first must be a letter.';
				}
				break;
				
			case 'password2':
				if( cible.find('input#password2').val() != cible.prev('div.champs').find('input#password1').val() )
				{
					notice = 'Warning : Les deux mots de passe doivent &ecirc;tre pareils.'
				}
				break;
				
			case 'nom':
				if( cible.find('input#nom').val() == '' )
				{
					notice = 'Warning : This is an obligatory field.';
				}
				break;
				
			case 'prenom':
				if( cible.find('input#prenom').val() == '' )
				{
					notice = 'Warning : This is an obligatory field.';
				}
				break;
				
			case 'sexe': 
				break;
				
			case 'birthday':
				reg = /^[0-3]{1}\d{1}\/{1}[0-1]{1}\d{1}\/{1}[1-2]{1}\d{3}$/;
				if( !reg.test( cible.find('input#birthday').val() ) )
				{
					notice = 'Warning : Date must be written like : dd/mm/yyyy.';
				}
				break;
				
			case 'telephone': 
				reg = /^[0-9]+$/;
				if( !reg.test( cible.find('input#telephone').val() ) )
				{
					notice = 'Warning : This field can contain only numbers.'
				}
				break;
				
			case 'adresse': 
				if( cible.find('input#adresse').val() == '' )
				{
					notice = 'Warning : This is an obligatory field.';
				}
				break;
				
			case 'complementaire': 
				break;
							
			case 'postal': 
				reg = /^[0-9]{1,}$/;
				if( !reg.test( cible.find('input#postal').val() ) )
				{
					notice = 'Warning : This field can contain only numbers.'
				}
				break;
				
			case 'ville': 
				if( cible.find('input#ville').val() == '' )
				{
					notice = 'Warning : This is an obligatory field.';
				}
				break;
				
			case 'pays': 
				break;
				
			default:
				notice = 'Warning : This type of field doesn\'t exist.'
				
		}
		return notice;
	}
function vider_commande()
	//vider la commander et recommencer l'achat
	{
		$('#conteneur').load('commun/panier/load_panier.php?com=vide_commande&etp=1'); 
	}
function modif_compte_francais()
	{
		$('#conteneur').load('commun/panier/html/francais/inscrip.php?com=modif');
	}
function modif_compte_english()
	{
		$('#conteneur').load('commun/panier/html/english/inscrip.php?com=modif');
	}
function change_taille_pro( cible , id_pro )
	{
		if( cible.find("option:selected") != "choisir" && cible.find("option:selected") != "choose" )
		{
			dataString = "com=change_taille_pro&id="+id_pro+"&taille="+cible.find("option:selected").val();;
			$.ajax({
				type: "POST", 
				url: "commun/panier/panier.foc.php", 
				dataType:'json',
				data: dataString, 
				success: function(reponse) 
				{
					$("form#panier").find("input#qte").attr( "title" , reponse );
					verif_qte_pro( $("form#panier").find("input#qte") );
				} 
			});//fin de AJAX
		}
	}
function verif_qte_pro( cible )
	{
		notice_taille = "Vous devez choisir une taille d'abord.";
		notice_taille = ( langue == "english" ) ? "You must select a size first." : notice_taille;
		notice_qte = "Quantite maximum en stock: "+cible.attr("title")+"\nVeuillez corriger s'il vous plait.";
		notice_qte = ( langue == "english" ) ? "Maximum quantity in stock: "+cible.attr("title")+"\nPlease correct." : notice_qte;
		
		
		if( cible.attr("title") == "" )
		{
			alert(notice_taille);
			cible.val(1);
		}
		else if( parseInt(cible.val()) > parseInt(cible.attr("title")) )
		{
			alert(notice_qte);
			cible.val(cible.attr("title"));
		}
	}
