// JavaScript Document
MAX = 400;

function sendDateShipping(){
	
	var date_shipping = $('idDlivraison').value;
	
	new Ajax.Request('ajax/_verif.php', {
					 method: 'post',
					 parameters: $H({'date':date_shipping}).toQueryString(),
			 		
					onFailure: function(eRequester) {
						alert("Nous ne livrons pas les week-end et jours fériés.");
					},
					
					onComplete: function(eRequester) {
						
						vInputValue = eval('(' + eRequester.responseText + ')');
						
						if( vInputValue.etat == 1 ){
							
							date_shipping = vInputValue.date;
							
							window.location="choix-coffrets-gourmands.php";
						
							/*new Ajax.Updater('view', 'ajax/_afficheCoffrets.php', {
										 method: 'post',
										 evalScripts: true,
										 parameters: $H({'date':date_shipping}).toQueryString(),
										 
										 onFailure: function(eRequester) {
											alert("Merci de nous contacter par téléphone au 03 20 69 09 59");
										}
							});*/
						} else if( vInputValue.etat == 2 ) {
							
							if( confirm(vInputValue.message) ){
								
								date_shipping = vInputValue.date;
								
								window.location="choix-coffrets-gourmands.php";
								
								/*new Ajax.Updater('view', 'ajax/_afficheCoffrets.php', {
										 method: 'post',
										 evalScripts: true,
										 parameters: $H({'date':date_shipping}).toQueryString(),
										 
										 onFailure: function(eRequester) {
											alert("Merci de nous contacter par téléphone au 03 20 69 09 59");
										}
								});*/
								
							}
							
						} else {
							
							alert(vInputValue.message);
							
						}
						
					
					}
	});
	
}



function initDraggables(allFiches, oneFiche) {
		
	var aleat = $$(allFiches);
	
	for(var i=0; i < aleat.length; i++) {	
	
		new Draggable( aleat[i],{ revert:true } );
		
	}
	
	Droppables.add( 'panierhid', { 
				   					accept:oneFiche, onDrop:function(fiche) { 
														updatePanier('add',fiche);  
													}
								});	
}//initDraggable

function supprime(p) {
	var id = p.split('_')[2];
	var type = p.split('_')[1];
	new Ajax.Updater('content','ajax/_remove.php',{
		postBody:$H({'id':id, 'type':type}).toQueryString(),
		evalScripts:true,
		onLoading : toggleIndicator,
		onComplete: toggleIndicator
	});
}


function updatePanier(mode,fiche) {
	
	Element.scrollTo('panier');
	var id = fiche.id.split('_')[2];
	var type = fiche.id.split('_')[1];
	var qte= $('qte_'+id).value;
	qte = Math.round(qte);
	
	if(qte > 0) {
		new Ajax.Updater('content','ajax/_'+mode+'.php',{
			postBody:$H({'id':id, 'qte':qte, 'type':type}).toQueryString(),
			evalScripts:true,
			onLoading : toggleIndicator,
			onComplete: toggleIndicator
		});
	}
	else{
		alert("La quantité d'article à ajouter doit être supérieure à 0");
	}
}



function toggleIndicator(){
	
	Element.toggle('indicator');
	
}


/*function addListener(z, baseName, handler) {
	
	if(z.addEventListener)
		z.addEventListener(baseName, handler, false);
	else if(z.attachEvent)
		z.attachEvent('on'+baseName, handler);
}*/





function verifLivraison(){
	
	if( $('alivrer').checked ) {
		$('adresseLivraison').style.display = 'block';
		$('adresseEmport').style.display = 'none';
		$('btnValider').style.display = 'none';
	} else {
		$('adresseLivraison').style.display = 'none';
		$('adresseEmport').style.display = 'block';
		$('btnValider').style.display = 'block';
	}
	
}



/**********************************************************************/
function selectVille(vCodepostal){
	
	
	Element.update('ville','<option>Choisissez votre ville</option>');
	

	//Verif code postal
	if( !isNaN(vCodepostal) && vCodepostal.length == 5 ) {
		
		new Ajax.Request('ajax/_verifCodepostal.php', {
					 method: 'post',
					 parameters: $H({'codePostal': vCodepostal}).toQueryString(),
					
					onComplete: function(eRequester) {
						
						vInputValue = eval('(' + eRequester.responseText + ')');
						
						if( vInputValue.etat == 1 )	{	
							new Ajax.Updater('ville', 'ajax/_select-villes.php', {
								method: 'post',
								parameters: $H({'codePostal': vCodepostal}).toQueryString(),
								
								onComplete : function(eRequester){
									
									//Afiichage du bouton "Valider"
									
									if($('ville').options[$('ville').selectedIndex].value == "-1")
										$('btnValider').style.display='none';
									else
										$('btnValider').style.display='block';
									
								}
							});
							
							
						}
						
						else {
							
							alert(vInputValue.message);
							$('btnValider').style.display='none';
						}
					}
		});
			
		
	} else {
		
		alert("Un code postal se compose de 5 chiffres.");
		$('btnValider').style.display='none';
		
	}

}
