$(document).ready(function(){

	$("#calResa").datepicker({ dateFormat: 'dd/mm/yy',
		 firstDay: 1,
		 minDate: new Date(),
		 changeMonth: true,
		 changeYear: true,
		 showButtonPanel: true,
		 showOn: 'button',
		 buttonImage: 'assets/calendar.gif',
		 buttonImageOnly: true,
		 onSelect: function(dateText, inst) {
			handleDateResa(dateText, 'flowDateResa');
		},
		beforeShowDay: getOpen
	});

	$(".menuGet").click(function(){
		$(".trMenuResa").show();
		$("#outMenuResa").html($(".menu"+$(this).attr("rel")).html());
		$("#menuresa").attr("value", $(this).attr("rel"));
			if($(this).hasClass("cpromo")){
				$("#temp_menu").attr("value", "");
				$("#temp_promo").attr("value", $(this).attr("rel"));
			} else {
				$("#temp_menu").attr("value", $(this).attr("rel"));
				$("#temp_promo").attr("value", "");
			}
		$("#flowMenuResa").hide();
		$("#flowInfoResa").show();
	});

	$("#changeDate").click(function(){
		
		$(".trDateResa").hide();
		$(".trHeureResa").hide();
		$(".trPaxResa").hide();
		
		$("#outDateResa").html("");
		$("#outPaxResa").html("");
		$("#dateresa").attr("value", "");
		$("#pax").attr("value", "");
		$("#heure").attr("value", "");
		
		$(".trMenuResa").hide();
		$("#outMenuResa").html("");
		$("#menuresa").attr("value", "");
		$("#flowMenuResa").hide();
		$("#flowHeureResa").hide();
		$("#flowPaxResa").hide();
		$("#flowInfoResa").hide();
		
		var el = $("#contextresa").attr("value");
		$("#"+el).show();
		
	});

	$("#changeHeure").click(function(){
		
		var dateText = $("#outDateResa").html();
		handleHours(dateText);
		
		$(".trHeureResa").hide();
		$(".trPaxResa").hide();
		
		$("#outHeureResa").html("");
		$("#outPaxResa").html("");
		
		$("#pax").attr("value", "");
		$("#heure").attr("value", "");
		
		$(".trMenuResa").hide();
		$("#outMenuResa").html("");
		$("#menuresa").attr("value", "");
		$("#flowMenuResa").hide();
		$("#flowPaxResa").hide();
		$("#flowHeureResa").show();
		$("#flowInfoResa").hide();
	});

	$("#changePax").click(function(){
		$(".trPaxResa").hide();
		$("#outPaxResa").html("");
		$("#pax").attr("value", "0");
		$("#inviterInner").html("");
		$(".trMenuResa").hide();
		$("#outMenuResa").html("");
		$("#menuresa").attr("value", "");
		$("#flowMenuResa").hide();
		$("#flowInfoResa").hide();
		$("#flowPaxResa").show();
	});

	$("#changeMenu").click(function(){
		$(".trMenuResa").hide();
		$("#outMenuResa").html("");
		$("#menuresa").attr("value", "");
		$("#msgemail").hide();
		$("#flowInfoResa").hide();
		handleMenu();
		handlePromo();
		$("#flowMenuResa").show();
		$("#tableInfoResa").show();
	});

	$(".paxResaLi").each(function(){
			var h = $(this).text();
			$(this).bind("click", {txt:h}, addPax);
		
	});
	
});

function handleMenu(){

	var heur = $("#heure").val();
	var dat = $("#dateresa").val();
	var id_per = $("#id_per").val();
	$.ajax({
		   type: "GET",
		   async: true,
		   url: "control.php?action=getMenus&id_per="+id_per+"&heure="+heur+"&dater="+dat,
		   success: function(msg){
				if (msg != ""){
					$("#orderResaMenu").html("");
					$("#orderResaMenu").html(msg);
					
					$(".menuGet").click(function(){
						
						var linkM = $(this).parent("td").next("td").find(".noMenuTitre").html();
						re = new RegExp("Menu Saint Valentin","i");
						
						if(re.test(linkM) == true){
							
							var today = new Date();
							var year = today.getFullYear();
							if ($("#outDateResa").html() == "13/02/2010" || $("#outDateResa").html() == "14/02/2010"){
								
							} else {
									alert("Ce menu est disponible uniquement pour le jour de la Saint Valentin le 14 Février.\nLa date de votre réservation va être mise à jour.");
									$("#outDateResa").html("14/02/"+year);
									$("#dateresa").attr("value","14/02/"+year);
									$("#temp_dater").attr("value","14/02/"+year);
							}
						}
						
						$(".trMenuResa").show();
						$("#outMenuResa").html($(".menu"+$(this).attr("rel")).html());
						$("#menuresa").attr("value", $(this).attr("rel"));
							if($(this).hasClass("cpromo")){
								$("#temp_menu").attr("value", "");
								$("#temp_promo").attr("value", $(this).attr("rel"));
							} else {
								$("#temp_menu").attr("value", $(this).attr("rel"));
								$("#temp_promo").attr("value", "");
							}
						$("#flowMenuResa").hide();
						$("#flowInfoResa").show();
					});
					
					$(".menuDetail").click(function(){
						var el = $(this).attr("rel");
						$("#"+el).toggle();
					});
					
				}
			}
	});
	
	
}

function handleDateResa(dateText, el){
	
	$(".trDateResa").show();
	$("#outDateResa").html(dateText);
	$("#dateresa").attr("value", dateText);
	$("#temp_dater").attr("value", dateText);
	$("#contextresa").attr("value", el);
	$("#"+el).hide();
	
	handleHours(dateText);
}

function handleHours(dateText){
	
	var id_per = $("#id_per").val();
	
	$.ajax({
		   type: "GET",
		   async: true,
		   url: "control.php?action=getHours&id_per="+id_per+"&date="+dateText,
		   success: function(msg){
				if (msg != ""){
					var ret = msg.split("|");
					var out = '';
					var out1 = '';
					
					var id_prom = $("#promoresa").attr("value");
					var ms_prom = $("#promoMS"+id_prom).attr("value");
					
		     		for(var i=0;i<parseInt(ret.length-1);i++){
		     			var hr = ret[i].split("#")[0];
		     			var ms = ret[i].split("#")[1];
		     			
		     			if (id_prom){
				     			if (id_prom != "0"){
							     			if (ms == "M" && (ms_prom == "M" || ms_prom == "MS")){
							     				out += '<li class="hourResaLi"><a href="javascript:void(0)" class="hourResaLink" rel="'+i+'">' + hr + '</a></li>';
							     			}
							     			
							     			if (ms == "S" && (ms_prom == "S" || ms_prom == "MS")){
							     				out1 += '<li class="hourResaLi"><a href="javascript:void(0)" class="hourResaLink" rel="'+i+'">' + hr + '</a></li>';
							     			}
							     			
					     			} else {
							     				if (ms == "M"){
								     				out += '<li class="hourResaLi"><a href="javascript:void(0)" class="hourResaLink" rel="'+i+'">' + hr + '</a></li>';
								     			}
								     			
								     			if (ms == "S"){
								     				out1 += '<li class="hourResaLi"><a href="javascript:void(0)" class="hourResaLink" rel="'+i+'">' + hr + '</a></li>';
								     			}
					     			}
				     			
				     	} else {
				     			
				     			if (ms == "M"){
							     			out += '<li class="hourResaLi"><a href="javascript:void(0)" class="hourResaLink" rel="'+i+'">' + hr + '</a></li>';
							     }	
							     
				     			if (ms == "S"){
							     			out1 += '<li class="hourResaLi"><a href="javascript:void(0)" class="hourResaLink" rel="'+i+'">' + hr + '</a></li>';
							     }
				     	}
		     		}
		     		
		     		if (out.length > 3){
		     			var output = '<p class="paddingtop5 txtform">D\351jeuner :</p><ul class="hourResaUl">' + out + '</ul>';
		     			
		     		}
		     		if (out1.length > 3){
		     			var output1 = '<p class="paddingtop5 txtform">Diner :</p><ul class="hourResaUl">' + out1 + '</ul>';
		     		}
		     		
		     		var genout = '';
		     		if (output){ genout += output;}
		     		if (output1){ genout += output1;}
		     		
		     		$("#heureResa").html(genout);
		     		$(".hourResaLi").each(function(){
		     			var h = $(this).text();
		     			$(this).bind("click", {txt:h}, addHours);
		     		
		     		});
		     	}
		   }
	  	});
	
	$("#flowHeureResa").show();
	
}

function addHours(event){
	var hour = event.data.txt;
	$(".trHeureResa").show();
	$("#outHeureResa").html(hour);
	$("#heure").attr("value", hour);
	$("#temp_heure").attr("value", hour);
	$("#flowHeureResa").hide();
	handleMenu();
	$("#flowPaxResa").show();
}

function addPax(event){
	var pax = event.data.txt;
	$(".trPaxResa").show();
	$("#outPaxResa").html(pax);
	$("#pax").attr("value", pax);
	$("#temp_pax").attr("value", pax);
	$("#flowPaxResa").hide();
	$(".added").remove();
	handlePromo();
	$("#flowMenuResa").show();
	if($("#inviterInner")){
		handleInviter();
	}
}

function addPromo(event){
	
	var rel = event.data.rel;
	$(".trMenuResa").show();
	$("#outMenuResa").html($(".menu"+rel).html());
		if($(".promo"+rel).hasClass("cpromo")){
			$("#temp_menu").attr("value", "");
			$("#temp_promo").attr("value", rel);
		} else {
			$("#temp_menu").attr("value", rel);
			$("#temp_promo").attr("value", "");
		}
	$("#flowMenuResa").hide();
	$("#flowInfoResa").show();

}

function handleInviter(){

	var inner = '<div class="innercontent inviterwarn">Si votre invité ne fait pas partie de votre société, cocher la case Externe et notez le nom de sa société.</div>';
	var nb = parseInt($("#pax").val())-parseInt(1);
	if (nb>0){
		
		$("#trInviter").removeClass("hide");
		$("#trInviter").addClass("show");
		
		for(var i=0;i<nb;i++){
			inner += '<p class="paddingtop5"><input type="text" name="resa_inv_prenom['+i+']" value="Prénom" class="resainviterprenom inputtextmedium bordersearch"/> <input type="text" name="resa_inv_nom['+i+']" value="Nom" class="resainviternom inputtextmedium bordersearch"/> <input type="text" name="resa_inv_societe['+i+']" value="Société" class="resainvitersociete inputtextmedium bordersearch"/> <input type="checkbox" name="resa_inv_externe['+i+']" class="resainviterexterne"/> Externe</p>';
		}
	
				if (inner.length > 3){
					$("#inviterInner").html(inner);
					
					$(".resainviterexterne").each(function(){
						$(this).click(function(){
							
							if ($(this).is(':checked') == true){
									$(this).prev(".resainvitersociete").val("");
									$(this).prev(".resainvitersociete").focus();
									
									$(this).prev(".resainvitersociete").bind("blur",function(){
										if ($(this).val() == ""){
											
											$(this).val("Société");
											$(this).next(".resainviterexterne").attr("checked", false);
											alert("Vous devez entrer un nom de société pour cet invité.");
										}
									});
							} else if($(this).is(':checked') == false) {
								$(this).prev(".resainvitersociete").val("Société");
							}
							
						});
					});
					
					$(".resainvitersociete").each(function(){
						
						$(this).focus(function(){
								$(this).val("");
						});
						
						$(this).blur(function(){
							if ($(this).val() != "Société" && $(this).val() != ""){
								$(this).next(".resainviterexterne").attr("checked", true);
							} else {
								$(this).val("Société");
								$(this).next(".resainviterexterne").attr("checked", false);
								alert("Vous devez entrer un nom de société pour cet invité.");
							}
						});
						
					});
					
					$(".resainviterprenom").each(function(){
						
						$(this).focus(function(){
								$(this).val("");
						});
						
						$(this).blur(function(){
							if ($(this).val() != "Prénom" && $(this).val() != ""){
								
							} else {
								alert("Vous devez entrer un prénom pour cet invité.");
								$(this).val("Prénom");
							}
						});
						
					});
					
					$(".resainviternom").each(function(){
						
						$(this).focus(function(){
								$(this).val("");
						});
						
						$(this).blur(function(){
							if ($(this).val() != "Nom" && $(this).val() != ""){
								
							} else {
								alert("Vous devez entrer un nom pour cet invité.");
								$(this).val("Nom");
							}
						});
						
					});
				}
		} else {
			$("#trInviter").removeClass("show");
			$("#trInviter").addClass("hide");
		}
}

function handlePromo(){

	var dateText = $("#temp_dater").val();
	var heureResa = $("#temp_heure").val();
	var paxResa = $("#temp_pax").val();
	var id_per = $("#id_per").val();
	
	$.ajax({
		   type: "GET",
		   async: true,
		   url: "control.php?action=getPromos&id_per="+id_per+"&date="+dateText+"&heure="+heureResa+"&pax="+paxResa,
		   success: function(msg){
				if (msg != ""){
					var ret = msg.split("|");
					var outp = '';
					
					for(var i=0;i<parseInt(ret.length-1);i++){
		     			var id_prom = ret[i].split("#")[0];
		     			var titre_prom = ret[i].split("#")[1];
		     			var cont_prom = ret[i].split("#")[2];
		     			var cond_prom = ret[i].split("#")[3];
		     			var contact_prom = ret[i].split("#")[4];
		     			
		     			outp += '<tr class=\"added\"><td width="240" valign="top" class="getPromoLink"><a href="javascript:void(0)" class="menuGet cpromo promo'+id_prom+'" rel="'+id_prom+'">Choisir cette offre</a></td>';
		     			outp += '<td valign="top"><p class="promoTitre menu'+id_prom+'">'+titre_prom+'</p><p class="paddingtop5">'+cont_prom+'</p>';
		     			if (cond_prom.lentgh > 1){
		     				outp += '<p class="paddingtop5">'+cond_prom+'</p>';
		     			}
		     			outp += '</td></tr>';
		     			 
		     			outp += '<tr class=\"added\"><td height="10" colspan="2"></td></tr>';
					}
					
					if (outp.length > 3){
						
						$(".toAdd:last-child").after(outp);
						$("#flowMenuResa").show();
					}
					
					$(".cpromo").each(function(){
		     			var idr = $(this).attr("rel");
		     			$(this).bind("click", {rel:idr}, addPromo);
		     		
		     		});
					
					$(".menuGet").click(function(){
						$(".trMenuResa").show();
						$("#outMenuResa").html($(".menu"+$(this).attr("rel")).html());
						$("#menuresa").attr("value", $(this).attr("rel"));
							if($(this).hasClass("cpromo")){
								$("#temp_menu").attr("value", "");
								$("#temp_promo").attr("value", $(this).attr("rel"));
							} else {
								$("#temp_menu").attr("value", $(this).attr("rel"));
								$("#temp_promo").attr("value", "");
							}
						$("#flowMenuResa").hide();
						$("#flowInfoResa").show();
		     		});
					
				}
			}
	  });
	
}

function getOpen(date){
	var id_per = $("#id_per").val();
	eval($("#closed").val());
	var noWeekend = $.datepicker.noWeekends(date);
	return closeDays(date, natDays);
}

function displayDays(date, natDays) {
	  var m = date.getMonth();
	  var d = date.getDate();
	  var y = date.getFullYear();
	  for (i = 0; i < natDays.length; i++) {
	    if ((m == natDays[i][0] - 1) && (d == natDays[i][1]) && (y == natDays[i][2]))
	    {
	      return [true,'_ondays'];
	    }
	  }
	  return [false];
}

function closeDays(date, natDays) {
	  var m = date.getMonth();
	  var d = date.getDate();
	  var y = date.getFullYear();
	  for (i = 0; i < natDays.length; i++) {
	    if ((m == natDays[i][0] - 1) && (d == natDays[i][1]) && (y == natDays[i][2]))
	    {
	      return [false];
	    }
	  }
	  return [true];
}