function tri(url, value)
{
	url = url+'&s='+value;
	location.href = url;
	
}

/*************** INSCRIPTION *********************/

function bindInscription()
{
	$('#type_client').change(function() { societe('societe', $(this).val()); });
	$('#type_client_livraison').change(function() { societe('livraison_societe', $(this).val()); });
	$('#type_client_facturation').change(function() { societe('facturation_societe', $(this).val()); });
	
	$('#chk_information_livraison').change(function() { adresse('information_livraison', $(this).is(':checked')); });
	$('#chk_information_facturation').change(function() { adresse('information_facturation', $(this).is(':checked')); });
	
	$('.verif_int', '#inscription-form ').blur(function() { verif($(this).attr('id'), $(this).val()); });
	
	$('#password_bis').blur(function() { verif_pass($(this).attr('id'), $(this).val(), $('#password').val()); });
	
}

function societe(champ, valeur)
{
	if(valeur == 0)
	{
		$('#'+champ+'_div').hide();
	}
	else
	{
		$('#'+champ+'_div').show();
	}
}


function adresse(adresse, checked)
{
	if(checked)
	{
		$('#'+adresse).slideDown('slow');
	}
	else
	{
		$('#'+adresse).slideUp('slow');
	}
}

function verif(champ, valeur)
{
	email_old = $('#email_old').val();
	$.getJSON('/json/compte.json.php?champ='+champ+'&valeur='+valeur+'&email_old='+email_old, function(json)
	{
		if(json.ok = 1)
		{
			if(json.verif == '')	
			{
				$('#'+champ+'_div').addClass('success').removeClass('error');
				$('#'+champ+'_em').hide();
			}
			else
			{
				$('#'+champ+'_div').addClass('error').removeClass('success');
				$('#'+champ+'_em').text(json.verif).show();
			}
		}
		else err(json.err);
	});
}


function verif_pass(champ, valeur_1, valeur_2)
{
	if(valeur_1 == valeur_2)	
	{
		$('#'+champ+'_div').addClass('success').removeClass('error');
		$('#'+champ+'_em').hide();
	}
	else
	{
		$('#'+champ+'_div').addClass('error').removeClass('success');
		$('#'+champ+'_em').text('Les mots de passe saisis sont différents').show();
	}
}

/*************** PANIER *********************/


function update_qte(art)
{
	var q = $('#spinner-'+art).val();
	if (q != '')
	{
		$.getJSON('/json/page_panier.json.php?art='+art+'&q='+q, function(json)
		{
			if(json.ok == 1) 
			{	
				update_panier(art, json);
				$('#panier_ligne-'+art).stop().effect('pulsate',{ 'times': '1' },500);	
			}
			else err(json.err);
		})
	}
	timeout = false;
}

var timeout = false;

function bindPanier()
{
	$('.quantite_art', '#liste-articles').each(function(){ 
        var art = $(this).parents('li').attr('id').replace('panier_ligne-','');
		$(this).spinner({max: 1000, min: $('#qte-min-'+art).val()});	
	}).bind('change keyup',function() { 
		if(!timeout)
		{
			var art = $(this).parents('li').attr('id').replace('panier_ligne-','');
			timeout = true;
			setTimeout('update_qte(\''+art+'\')', 500);
		}
    });

	
    // supprimer ligne du panier
    $('.supprimer-quantite', '#liste-articles').click(function() {
    	var art = $(this).parents('li').attr('id').replace('panier_ligne-','');
		$.getJSON('/json/page_panier.json.php?art='+art+'&q=del', function(json)
		{
			if(json.ok == 1) 
			{
				$('#panier_ligne-'+art).effect('pulsate',{ 'times': '1' },500).slideUp(function() 
				{ 
					$('#panier_ligne-'+art).remove();
				});
				update_panier(art, json);

				
			}
			else err(json.err);
		})	
    });  
    
    // check de l'emballage cadeau
	$('.chk_cadeau', '#liste-articles').click(function() 
	{ 
		var art = $(this).parents('li').attr('id').replace('panier_ligne-','');
		var cadeau = ($(this).is(':checked')) ? 1 : 0;
		$.getJSON('/json/page_panier.json.php?art='+art+'&cadeau='+cadeau, function(json)
		{
			if(json.ok == 1) update_panier(art, json);
			else err(json.err);
		})
	});
	
	
	$('#btn_codepromo').click(function() 
	{ 
		var codepromo = $('#codepromo').val();
		$.getJSON('/json/codepromo.json.php?codepromo='+codepromo, function(json)
		{
			if(json.ok == 1)
			{
				if(json.valide == 1)
				{
					$('#page_panier-montantremise').html('Montant: -'+json.reduction+' €').effect('pulsate',{ 'times': '2'  },800);	
					$('#montant_total').html(json.montant_total);
					$('#montant').val(json.montant_total);	
				}
				else
					$('#page_panier-montantremise').html('Code promo invalide');
			}
			else err(json.err);
		})
	});	
	
	$('#page_panier-popupcalc').click(function() { popupcalc(10); });
}

function update_panier(art, json)
{	
	$('#prix_ht-'+art).html(json.prix_ht);
	//$('#options-'+art).html(json.options);
	$('#prix_total_ht-'+art).html(json.prix_total_ht);
	//$('#tva-'+art).html(json.tva);
	//$('#eco_part-'+art).html(json.eco_part);
	/*if (json.cadeau == 1)
		html = 'Emballage cadeau: '+json.montant_cadeau_ht+' €<br />';
	else
		html = '<br />';
	$('#cadeau-'+art).html(html);
	$('#sous_total-'+art).html(json.sous_total);*/

	$('#total_panier_ht').html(json.total_panier_ht);
	
	$('#montant_panier').html(json.total_panier);
	
	$('#frais_livraison').html(json.frais_livraison);
	
	if (json.frais_livraison == 'offerts')
	{
		$('#frais_livraison').stop().effect('pulsate',{ 'times': '2'  },800);	
	}	

	if (json.reduction == 0) 
		var html = '<br />';
	else
		var html = 'Réduction: -'+json.reduction+' €';
	
	$('#page_panier-montantremise').html(html);
	
	$('#montant_total').html(json.montant_total);
	$('#montant').val(json.montant_total);
	
	$('#total_tva').html(json.total_tva);
	
	if(json.nb_article > 0)
	{
		$('#statut-panier').html('Vous avez actuellement <span class="couleur">'+json.nb_article+' produit'+(json.nb_article > 1 ? 's' : '')+'</span> dans votre panier');
	}
	else
	{
		$('#statut-panier').html('Votre panier est vide');
	}
}

/*************** LIVRAISON *********************/

function bindLivraison()
{
	$('.modelivraison-choix', '#page_livraison').click(function() { 
		var id = $(this).attr('id').replace('modelivraison-choix-','');	
		$.getJSON('/json/livraison.json.php?mode_livraison='+id, function(json)
		{
			if(json.ok = 1)
			{
				$('.modelivraison-choix', '#page_livraison').removeClass('active');
				$(this).addClass('active');
				$('#mode-livraison-'+id).attr('checked', 'checked'); 
			}
			else err(json.err);
		})
	})
    
	$('#modif_livr').click(function() { $('#info_livr').toggle('slow'); $('#form_livr').toggle('slow'); });
	$('#modif_fact').click(function() { $('#info_fact').toggle('slow'); $('#form_fact').toggle('slow'); });	
}

/*************** PAIEMENT *********************/

function bindPaiement()
{	
	$('.modepaiement-choix', '#page_choix_paiement').click(function() { 
		var id = $(this).attr('id').replace('modepaiement-choix-','');	
		$.getJSON('/json/paiement.json.php?mode_paiement='+id, function(json)
		{
			if(json.ok = 1)
			{
				$('.modepaiement-choix', '#page_choix_paiement').removeClass('active');
				$(this).addClass('active');
				$('#mode-paiement-'+id).attr('checked', 'checked'); 
				$('#totalapayer_wrapper').show(); 
				$('#btn_paiement_wrapper').show(); 
				$('#total_a_payer').html(json.total_a_payer); 
			}
			else err(json.err);
		})
	});
}
