$(document).ready(function(){
    $("ul.sf-menu").supersubs({
        minWidth:    12.5,
        maxWidth:    35,
        extraWidth:  1
    }).superfish({
        delay:       1000,                            // one second delay on mouseout 
        animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
        speed:       'normal',                          // faster animation speed 
        autoArrows:  false,                           // disable generation of arrow mark-up 
        dropShadows: false                            // disable drop shadows 
    });
});

function AddBasket(PN_sid, PN_item_id, PN_item_type,LA_id,sRub,TO_id)
{
	$(document).ready(function(){
		var PN_item_qte = $("#Qte").val();
		var PN_jour_semaine = $("#PN_jour_semaine").val();
		$.ajax({
			type: "POST",
			cache: false,
			url: "/ajax.req.php?id=add_panier&rf="+Math.random(),
			data: {"PN_sid": PN_sid, "PN_item_id": PN_item_id, "PN_item_qte": PN_item_qte, "PN_item_type": PN_item_type, "PN_jour_semaine": PN_jour_semaine},
			beforeSend: function() {
				$('#form-loading').show();
			},
			success: function(dataReturn) {
				$('#form-loading').hide();
				$("#cart"+PN_item_id).html('<a href="javascript:void(0);" onclick="javascript:DelBasket(\''+PN_sid+'\',\''+PN_item_id+'\',\''+PN_item_type+'\',\''+LA_id+'\',\''+sRub+'\',\''+TO_id+'\');" title="Retirer ce produit du panier"><img src="/images/icones/cart_delete.png" style="margin-left:5px;vertical-align:middle;" /></a>');
				
				$("#Ft"+PN_item_id).removeClass('FooterPR');
				$("#Ft"+PN_item_id).addClass('FooterPR2');
				
				getPanier(PN_sid,LA_id,sRub,TO_id);
				$('#FormPanier').html("<h2 style='text-align:center;'>"+text[81]+"</h2>");
				window.setTimeout(function() { $('#dialogBox').jqmHide(); }, 1000);				
			}
		});
	});
}

function DelBasket(PN_sid, PN_item_id, PN_item_type,LA_id,sRub,TO_id)
{
	$(document).ready(function(){
		$.ajax({
			type: "POST",
			cache: false,
			url: "/ajax.req.php?id=del_panier&rf="+Math.random(),
			data: {"PN_sid": PN_sid, "PN_item_id": PN_item_id, "PN_item_type": PN_item_type, "sRub": sRub},
			success: function(dataReturn) {
				$("#Ft"+PN_item_id).removeClass('FooterPR2');
				$("#Ft"+PN_item_id).addClass('FooterPR');
				$("#cart"+PN_item_id).html(dataReturn);
				getPanier(PN_sid,LA_id,sRub,TO_id);
			}
		});
	});
}

function openDiag(page)
{
	$('#dialogBox').jqm({ajax:page, cache:false, overlay:80, toTop:'true', ajaxText: '<p style="text-align:center"><img src="/images/loader-big.gif" /></p>'});
	$('#dialogBox').jqmShow();
}

function closeDiag()
{
	$('#dialogBox').jqmHide();
}

function getPanier(Sid, LA_id, sRub, TO_id)
{
	$(document).ready(function(){
		if(document.getElementById('panier')) {
			$.ajax({
				type: "POST",
				cache: false,
				url: "/ajax.req.php?id=get_panier&rf="+Math.random(),
				data: {"Sid": Sid, "LA_id": LA_id, "sRub": sRub, "TO_id": TO_id},
				beforeSend: function() {
					$('#panier-loading').show();
				},
				success: function(dataReturn) {
					$('#panier-loading').hide();
					if(dataReturn.match('false'))
					{
						$("#ContentPanier").html('<p style="padding:5px;text-align:center;">'+text[82]+'</p>');
					}
					else
					{
						$("#ContentPanier").html(dataReturn);
					}
				}
			});
		}
	});
}

function backupPanier(Sid, ST_id, VL_id, DT_id, LA_identifier, TO_id)
{
	$(document).ready(function(){
		$.ajax({
			type: "POST",
			cache: false,
			url: "/ajax.req.php?id=backup-panier&rf="+Math.random(),
			data: {"Sid": Sid, "ST_id": ST_id, "VL_id": VL_id, "DT_id": DT_id, "LA_identifier": LA_identifier,"TO_id": TO_id, "CL_email": $("#CL_email").val()},
			beforeSend: function() {
				$('#bpanier').hide();
				$('#backup-loading').show();
			},
			success: function(dataReturn) {
				$('#backup-loading').hide();
				$('#bpanier').show();
				if(dataReturn.match('false'))
				{
					$("#bpanier").html('<p style="padding:5px;text-align:center;">Erreur lors de l\'envoi du mail</p><p style="padding:5px;text-align:center;"><img src=\'/images/icones/cross.png\' class=\'icones\' /><a href=\'javascript:closeDiag();\'>'+text[28]+'</a></p>');
				}
				else
				{
					$("#bpanier").html("<p style='padding:5px;text-align:center;'>"+text[83]+"</p><p style='padding:5px;text-align:center;'><img src='/images/icones/cross.png' class='icones' /><a href='javascript:closeDiag();'>"+text[28]+"</a></p>");
				}
			}
		});
	});
}

function delQte(chp) {
	if(document.getElementById(chp).value > 1) {
		document.getElementById(chp).value--;
	}
}

function addQte(chp) {
	document.getElementById(chp).value++;
}

function cStation(TO_id)
{
	$(document).ready(function () {
		$.ajax({
			type: "POST",
			cache: false,
			url: "/ajax.req.php?id=cStations",
			data: {"ST_id": $('#cStation').val(), "TO_id": TO_id},
			beforeSend: function() {
				$("#Error").hide();
				$('#loadingVL').show();
			},
			success: function(dataReturn) {
				$('#loadingVL').hide();
				if(dataReturn.match('failure'))
				{
					$("#Village").hide();
					$("#Error").show();
					$("#Error").html(text[84]);
				}
				else
				{
					$("#Village").show();
					$("#Village").html(dataReturn);
					cVillage();
				}
			}
		});
	});
}

function cVillage()
{
	$(document).ready(function () {
		$.ajax({
			type: "POST",
			cache: false,
			url: "/ajax.req.php?id=cVillages",
			data: {"VL_id": $('#cVillage').val()},
			beforeSend: function() {
				$("#Error").hide();
				$('#loadingDT').show();
			},
			success: function(dataReturn) {
				$('#loadingDT').hide();
				if(dataReturn.match('failure'))
				{
					$("#Dates").hide();
					$("#Error").show();
					$("#Error").html(text[85]);
				}
				else
				{
					$("#Dates").show();
					$("#Dates").html(dataReturn);
					ValidateLIV();
				}
			}
		});
	});
}

function ValidateLIV()
{
	$(document).ready(function () {
		$("#ValiderLIV").show().html(
			'<img src="/images/icones/tick.png" class="icones"/><a href="/index.php?toDo=configCMD&ST_id='+$('#cStation').val()+'&VL_id='+$('#cVillage').val()+'&DT_id='+$('#cDate').val()+'">'+text[86]+'</a>'
		);
	});
}

function cResidences()
{
	$(document).ready(function () {
		if($('#cRes').val()=='Autre')
		{
			$("#DemandeRes").slideDown('fast');
		}
		else
		{
			if($("#DemandeRes").is(':visible')) $("#DemandeRes").slideUp('fast');
		}
	});
}

function ValidDemandeRes(Station, Village)
{
	$(document).ready(function () {
		var Erreur = "";
		if($('#ResDemande').val()=='') Erreur += text[87]+"<br />";
		if($('#EmailDemande').val()=='') Erreur += text[88];
		if(Erreur == "")
		{
			$.ajax({
				type: "POST",
				cache: false,
				url: "/ajax.req.php?id=DemandeRes",
				data: {"Station": Station,"Village":Village, "ResDemande": $('#ResDemande').val(), "EmailDemande": $('#EmailDemande').val()},
				beforeSend: function() {
					$("#cDres").hide();
					$('#loadingDres').show();
				},
				success: function(dataReturn) {
					$('#loadingDres').hide();
					if(dataReturn.match('failure'))
					{
						$("#cDres").show();
						$("#DResError").show();
						$("#DResError").html("Erreur lors de l'envoi du mail");
					}
					else
					{
						$("#cDres").show();
						$("#cDres").html("<p style='text-align:center;'>"+text[89]+"</p>");
					}
				}
			});
		}
		else
		{
			$("#DResError").show();
			$("#DResError").html(Erreur);
		}
	});
}

$(document).ready(function () {
	if(document.getElementById('CP_code')) {
		if($('#CP_code').val()!='')
		{
			$.ajax({
				type: "POST",
				cache: false,
				url: "/ajax.req.php?id=get_remise",
				data: {"CP_code": $('#CP_code').val(), "MontantCMD": $('#MontantCMD').val(), "Sid": $('#Sid').val(), "FP": $('#FraisP').val() },
				beforeSend: function() {
					$("#remise-loading").show();
				},			
				success: function(dataReturn) {
					$("#remise-loading").hide();
					if(dataReturn.match('failure'))
					{
						$('#CP_code').val("");
						$("#montantRemise").html("<span style='color:#FF0000'>"+text[90]+"</span>");
					}
					else
					{
						$("#montantRemise").html(dataReturn);
					}
				}
			});
		}
		$('#CP_code').blur(function() {
			if($('#CP_code').val()!='')
			{
				$.ajax({
					type: "POST",
					cache: false,
					url: "/ajax.req.php?id=get_remise",
					data: {"CP_code": $('#CP_code').val(), "MontantCMD": $('#MontantCMD').val(), "Sid": $('#Sid').val(), "FP": $('#FraisP').val() },
					beforeSend: function() {
						$("#remise-loading").show();
					},			
					success: function(dataReturn) {
						$("#remise-loading").hide();
						if(dataReturn.match('failure'))
						{
							$('#CP_code').val("");
							$("#montantRemise").html("<span style='color:#FF0000'>"+text[90]+"</span>");
						}
						else
						{
							$("#montantRemise").html(dataReturn);
						}
					}
				});
			
		}
			
		});
	}
});


