$(document).ready(function(){

// ********************************************************************
// ********************************************************************
// ****	Intégraption Ajax pour superadmin (4/11/2009 Thomas Devos)	***
// ********************************************************************
// ********************************************************************

$(".bouton_superadmin_paypal").click(function(){
	$.get("fct_ajax.php",{fct : "confirm_paypal" , panier : $(this).attr("panier")},
	function(data){
		alert("data = "+data)
	}
	)
});

// ********************************************************************
// ********************************************************************

$("#cache").hide();

if($("input:radio:checked").val()=="yes")
{
	$(".info_perso").hide();
	$(".add_fact_js").hide();
	$(".add_livrai_js").hide();
	$(".connection").show();
	$(".infos_connection").hide();
	$(".check_livrais").hide();
	$("#continuer").hide();
}

if($("input:radio:checked").val()=="no")
{
	$(".info_perso").show();
	$(".add_fact_js").show();
	$(".add_livrai_js").show();
	$(".connection").hide();
	$(".infos_connection").show();
	$(".check_livrais").show();
	$("#continuer").show();
}

if($("input:radio:checked").val()=="dont")
{
	$(".info_perso").show();
	$(".add_fact_js").show();
	$(".add_livrai_js").show();
	$(".connection").hide();
	$(".infos_connection").hide();
	$(".check_livrais").show();
	$("#continuer").show();
}

$(".question_enreg").click(function(){

if($(this).val()=="yes")
{
	$(".info_perso").hide();
	$(".add_fact_js").hide();
	$(".add_livrai_js").hide();
	$(".connection").show();
	$(".infos_connection").hide();
	$(".check_livrais").hide();
	$("#continuer").hide();
}

if($(this).val()=="no")
{
	$(".info_perso").show();
	$(".add_fact_js").show();
	$(".add_livrai_js").show();
	$(".connection").hide();
	$(".infos_connection").show();
	$(".check_livrais").show();
	$("#continuer").show();
}

if($(this).val()=="dont")
{
	$(".info_perso").show();
	$(".add_fact_js").show();
	$(".add_livrai_js").show();
	$(".connection").hide();
	$(".infos_connection").hide();
	$(".check_livrais").show();
	$("#continuer").show();
}

});

$(".check_livrais_cmd").click(function(){
	if($(this).attr('checked'))
	{
		$(".add_livrai_js").hide();
		$(".div_check_fact").hide();
	}
	else
	{
		$(".add_livrai_js").show();
		$(".div_check_fact").show();
	}
});

$(".checkbox_magasin").click(function(){
	if($(this).attr('checked'))
	{
		nb=parseInt($("#nb_checked").val())+1;
		$("#nb_checked").val(nb);
		$(this).parents("tr").css('background-color','#E22');
	}
	else
	{
		nb=$("#nb_checked").val()-1;
		$("#nb_checked").val(nb);
		$(this).parents("tr").css('background-color','#E6E6E6');
	}
	
	if($("#nb_checked").val()==$("#nb_check").val())
	{
		$("#boutton_prepa_terminee").show();
	}
	else
	{
		$("#boutton_prepa_terminee").hide();
		
		
	}
});
	


$(".nombre").keyup(function(e){
	if(e.keyCode == 13) {

tr=$(this).parent();
nombre=$(this).parent().children(".nombre").val();
id_prod=tr.parent().children("li").children(".type_produit").val();
panier = $("#id_panier_session").val();



$.post("fct_ajax.php?fct=envoi_vers_panier", { nombre: nombre, id_prod: id_prod, panier:panier },
  function test(data){
  	basket($("#id_panier_session").val());
	  }
  );

if($(this).parent().children(":button").hasClass("boutton_false"))
{}
else
{
$(this).parent().children("#nombre").val(0);
$(this).parent().children(":button").addClass("boutton_sended");
$(this).parent().children(":button").val("Envoyé au panier");
}


}
});

$(".boutton").click(function(){
	
tr=$(this).parent();
nombre=$(this).parent().children(".nombre").val();
id_prod=tr.parent().children("li").children(".type_produit").val();
panier = $("#id_panier_session").val();

$.post("fct_ajax.php?fct=envoi_vers_panier", { nombre: nombre, id_prod: id_prod, panier:panier },
  function test(data){
  	basket($("#id_panier_session").val());
	  }
  );

if($(this).parent().children(":button").hasClass("boutton_false"))
{}
else
{
if(nombre==0)
{}
else
{
$(this).parent().children("#nombre").val(0);
$(this).parent().children(":button").addClass("boutton_sended");
$(this).parent().children(":button").val("Envoyé au panier");
}}

});


$(".nombre").keyup(function(){

tr=$(this).parent();
nombre=$(this).val();
id_prod=tr.parent().children("li").children(".type_produit").val();

panier = $("#id_panier_session").val();


$.post("fct_ajax.php?fct=calcul_ajax", { posted: nombre, id_prod: id_prod},
  function(data){

tr.children("#resultat_calcul").text(data);


  });
if($(this).parent().children(":button").hasClass("boutton_false"))
{}
else
{
if(nombre ==0)
{
	$(this).parent().children(":button").val("Insérer quantité");
}
else
{
	$(this).parent().children(":button").val("Ajouter au panier");
}
}
}
);

$(".type_produit").change(function(){

tr=$(this).parent().parent();
nombre=tr.children(".sec_li").children(".nombre").val();
id_prod=tr.children("li").children(".type_produit").val();
panier = $("#id_panier_session").val();



$.post("fct_ajax.php?fct=calcul_ajax", { posted: nombre, id_prod: id_prod },
  function(data){
tr.children(".sec_li").children("#resultat_calcul").text(data);
  });

$.post("fct_ajax.php?fct=affiche_prix", { posted: id_prod },
  function(data){  	
tr.children(".sec_li").children("#prix").text(data);
  });
  
$.post("fct_ajax.php?fct=verif_doublons", { id_type: id_prod , panier: panier},
  function(data){  	
if(data!=1)
{
tr.children(".sec_li").children(":button").removeClass("boutton_sended");
tr.children(".sec_li").children(":button").addClass("boutton_false");
tr.children(".sec_li").children(":button").val("Déjà commandé");
}
else
{
tr.children(".sec_li").children(":button").removeClass("boutton_sended");
tr.children(".sec_li").children(":button").removeClass("boutton_false");
	if(nombre ==0)
	{
		tr.children(".sec_li").children(":button").val("Insérer quantité");
	}
	else
	{
		tr.children(".sec_li").children(":button").val("Ajouter au panier");
	}
}


});



})

});
//	******************************************************
//	Affiche panier mini
//	******************************************************

function basket(id_panier)
{

var xhr = getXhr();

// On défini ce qu'on va faire quand on aura la réponse
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
	//Action(s)

document.getElementById("basket").innerHTML = xhr.responseText;

}}

xhr.open("POST", "fct_ajax.php?fct=update_basket",  true); 
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
var data="panier="+id_panier;
xhr.send(data);

}

//	******************************************************
//	Supprime ligne panier
//	******************************************************


function supprim_ligne_panier(id_type_panier)
{
	id_panier=$("#id_panier_session").val();
	$.post("fct_ajax.php?fct=supprim_ligne", { id_type_panier: id_type_panier, id_panier : id_panier },
  function(data){
  	
$("#content").html(data);
basket(id_panier);

  });
}

//	******************************************************
//	Remplace ligne panier par formulaire pour modif
//	******************************************************

function modif_ligne_panier(id_type_panier)
{

var xhr = getXhr();

	// On défini ce qu'on va faire quand on aura la réponse
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
	//Action(s)

var rempl=xhr.responseText;

$(".promo").hide("fast");
$("#"+id_type_panier).html(rempl);

}}

xhr.open("POST", "fct_ajax.php?fct=modif_panier",  true); 
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
var data="posted="+id_type_panier;
xhr.send(data);

}

//	******************************************************
//	Affiche panier dyn
//	******************************************************


function affiche_panier(id,id_panier)
{
var xhr = getXhr();

// On défini ce qu'on va faire quand on aura la réponse
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
	//Action(s)
basket(id_panier);
document.getElementById("content").innerHTML = xhr.responseText;

}

}

xhr.open("POST", "fct_ajax.php?fct=afficher_panier_apres_modif_ligne",  true); 
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
var data="posted="+id+"&nombre="+parseInt(document.getElementById("nombre").value)+"&id_panier="+id_panier;
xhr.send(data);
}

//	******************************************************
//	affiche un message d'erreur (INUTILE)
//	******************************************************

function affiche_message(what,texte)
{
	if(what=="n")
	document.getElementById("block_message").style.display = "none";
	else
	{
	document.getElementById("block_message").style.display = "block";
	document.getElementById("block_message").innerHTML = texte;
	}
}


//	******************************************************
//	Montre detail de promotions
//	******************************************************

function montrer(who) {
$(".cache").hide("slow");
$("#hide"+who).parent("td").children(".cache").show("fast");
};
