function checkForEnter(event) {
      if (event.keyCode == 13) {
         currentTextboxNumber = textboxes.index(this);

         if (textboxes[currentTextboxNumber + 1] != null) {
           nextTextbox = textboxes[currentTextboxNumber + 1];
           nextTextbox.select();
      }

         event.preventDefault();
         return false;
      }
}

$(document).ready(function(){

	textboxes = $("input:text");

	 if ($.browser.mozilla) {
	      $(textboxes).keypress(checkForEnter);
	  	} else {
	      $(textboxes).keydown(checkForEnter);
	 }
	
	if ($("#cdcateg").val() != ""){
		if ($("#cdcateg").val() != "PREMIUM" || $("#cdcateg").val() != "BUSINESS" ){
			$(".rpro").hide();
		} else {
			$(".rpro").show();
		}
	} else {
		$(".rpro").hide();
	}
	
	$(".paxResaLinkGrp").click(function(){
		var dater = $("#dateresa").val();
		var heure = $("#heure").val();
		var pax = $(this).attr("rel");
		var alink = $(this).attr("href");
		location.href = alink +"?dater="+dater+"&heure="+heure+"&pax="+pax;
		return false;
	});
	
	$(".returnInfos").click(function(){
		$("#loginResa").hide();
		$("#passResa").hide();
		$("#resaInfoButton").show();
		handleInfos($("#temp_pax").val(), 'choixPax');
	});
	
	$(".forpassresa").click(function(){
		$("#loginResa").hide();
		$("#passresa_email").val($("#loginresa_email").val());
		$("#passResa").show();
	});
	
	$(".returnStep").click(function(){
		if ($(this).attr("rel") == "step1"){
			$("#calResa").datepicker('destroy');
			$("#choixDate").hide();
			$("#choixMenu").show();
			$("#outMenuResa").html();
			$(".trMenuResa").hide();
			$("#menuresa").val('');
			$("#temp_menu").val('');
			$(this).hide();
		}
		
		if ($(this).attr("rel") == "step2"){
			$("#choixHeure").hide();
			$("#choixDate").show();
			$("#outDateResa").html('');
			$("#dateresa").val('');
			$("#temp_dater").val('');
			$(".trDateResa").hide();
			$(this).attr("rel","step1");
		}
		
		if ($(this).attr("rel") == "step3"){
			$("#choixPax").hide();
			$("#choixPartPro").hide();
			$("#choixHeure").show();
			$("#outHeureResa").html('');
			$("#heure").val('');
			$("#temp_heure").val('');
			$(".trHeureResa").hide();
			$(this).attr("rel","step2");
		}
		
		if ($(this).attr("rel") == "step4"){
			$("#resaInfoButton").hide();
			$("#loginResa").hide();
			$("#infosResa").hide();
			$("#choixPax").show();
			$("#outPaxResa").html('');
			$("#pax").val('');
			$("#morepeo").val('');
			$("#temp_pax").val('');
			$(".trPaxResa").hide();
			$(this).attr("rel","step3");
		}
	});
	
	$(".selectrPromo").click(function(){
		$("#choixMenu").hide();
		$("#choixDate").show();
		$(".returnStep").show();
		var id_prom = $(this).attr("rel");
		var alink = $(this);
		$("#temp_promo").val(id_prom);
		$.ajax({
			   type: "GET",
			   async: false,
			   cache:false,
			   url: "control.php?action=getPromoDate&id_prom="+id_prom,
			   success: function(msg){
				   if (msg != ""){
					   
					   $("#outMenuResa").html($(alink).parent("td").next("td").find(".promoTitre").html());
					   $("#outMenuResa").css("color","#BD009C");
					   $(".trMenuResa").show();
					   $("#promoresa").val($(alink).attr("rel"));
					   
					   $("#opened").val(msg);
					   
					   $("#labclose").html("Offre indisponible");
					   $("#labopen").html("Offre disponible");
					   
					   $("#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, 'choixDate');
							},
							beforeShowDay: getProm
						});
				   }
			   }
		});
	});
	
	$(".selectrSimple").click(function(){
		$("#choixMenu").hide();
		$(".group").show();
		$(".rprof").hide();
		$("#choixDate").show();
		$(".returnStep").show();
		
		var alink = $(this);
		$("#outMenuResa").html("Simple à la carte");
		$("#outMenuResa").css("color","#8A7F78");
		$(".trMenuResa").show();
		$("#menuresa").val($(alink).attr("rel"));
		$("#temp_menu").val($(alink).attr("rel"));
		
		$("#labclose").html("Restaurant fermé");
		$("#labopen").html("Restaurant ouvert");
		
		$("#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, 'choixDate');
			},
			beforeShowDay: getOpen
		});
	
		$(".rsochic").hide();
		$(".rinvitation").hide();
		$(".rcode").show();
	});
	
	$(".selectrMenu").click(function(){
		$("#choixMenu").hide();
		$(".group").show();
		$(".rprof").hide();
		$("#choixDate").show();
		$(".returnStep").show();
		
		var alink = $(this);
		$("#outMenuResa").html($(alink).attr("title"));
		$("#outMenuResa").css("color","#8A7F78");
		$(".trMenuResa").show();
		$("#menuresa").val($(alink).attr("rel"));
		
		var predate = $(alink).attr("alt");
		if (predate != ""){
			handleDateResa(predate);
			return;
		}
		
		$("#labclose").html("Restaurant fermé");
		$("#labopen").html("Restaurant ouvert");
		
		$("#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, 'choixDate');
			},
			beforeShowDay: getOpen
		});
		$(".rsochic").hide();
		$(".rinvitation").hide();
		$(".rcode").hide();
	});

	$(".selectrInvit").click(function(){
		$("#choixMenu").hide();
		$("#choixDate").show();
		$(".group").hide();
		$(".rprof").hide();
		$(".returnStep").show();
		
		var alink = $(this);
		$("#outMenuResa").html("Invitation Restaurant");
		$("#outMenuResa").css("color","#c90074");
		$(".trMenuResa").show();
		$("#menuresa").val("Invitation");
		
		$("#labclose").html("Restaurant fermé");
		$("#labopen").html("Restaurant ouvert");
		
		$("#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, 'choixDate');
			},
			beforeShowDay: getOpen
		});
		$(".rsochic").hide();
		$(".rinvitation").show();
		$(".rcode").hide();
	});
	
	$(".selectrSoChic").click(function(){
		$("#choixMenu").hide();
		$(".rprof").hide();
		$(".group").hide();
		$("#choixDate").show();
		$(".returnStep").show();
		
		var alink = $(this);
		$("#outMenuResa").html("Chèque cadeau SO CHIC");
		$("#outMenuResa").css("color","#c90074");
		$(".trMenuResa").show();
		$("#menuresa").val("sochic");
		
		$("#labclose").html("Restaurant fermé");
		$("#labopen").html("Restaurant ouvert");
		
		$("#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);
			},
			beforeShowDay: getOpen
		});
		
		$(".rinvitation").hide();
		$(".rsochic").show();
		$(".rcode").hide();
	});
	
	$(".proResaLink").click(function(){
		if ($(this).attr("rel") == "pro"){
			$(".rprof").show();
		} else {
			$(".rprof").hide();
		}
		handleInfos($("#temp_pax").val(), 'choixPax');
	});
	
});


function handleDateResa(dateText){
	
	
	/*if (dateText==undefined){
		
		alert("Merci de sélectionner une date");
		
		$.ajax({
			   type: "GET",
			   async: false,
			   cache:false,
			   url: "control_ajax.php?action=errorDate&dater="+dateText,
			   success: function(msg){
			   
			   }
		});
		
		return false;
	}*/
	
	handleHours(dateText);
	
	/*$.ajax({
		   type: "GET",
		   async: false,
		   cache:false,
		   url: "control_ajax.php?action=sendDate&dater="+dateText,
		   success: function(msg){
		   
		   }
	});*/
	$("#presResa").hide();
}

function handleHours(dateText){
	
	var id_per = $("#id_per").val();
	var id_prom = $("#temp_promo").val();
	
	$.ajax({
		   type: "GET",
		   async: false,
		   cache:false,
		   url: "control.php?action=getHours&id_per="+id_per+"&dater="+dateText+"&id_prom="+id_prom,
		   success: function(msg){
			   
			   if (msg.length > 1){
				   $("#outDateResa").html(dateText);
				   $("#dateresa").val(dateText);
				   $("#temp_dater").val(dateText);
				   $(".trDateResa").show();
				   $("#choixDate").hide();
				   $("#heureResa").html(msg);
				   $("#titleHeure").show();
				   $("#pHeure").show();
				   $("#choixHeure").show();
				   $(".returnStep").attr("rel", "step2");
				   $(".hourResaLink").click(function(){
					  handlePax($(this).html(), 'choixHeure');
				   });
			   }
			   
			   if (msg.length == 0){
				   
				   $.ajax({
					   type: "GET",
					   async: true,
					   url: "control_ajax.php?action=trackTel&id_per="+id_per,
					   success: function(msg){}
				   });
				   
				   alert($("#numresto").val());
				   $(".returnStep").attr("rel", "step2");
				   return false;
			   }
		   }
	});
	
}

function handlePax(heureText,el){
	$("#outHeureResa").html(heureText);
	$("#heure").val(heureText);
	$("#temp_heure").val(heureText);
	$(".trHeureResa").show();
	$(".returnStep").attr("rel", "step3");
	$("#choixPax").show();
	$("#"+el).hide();
	
	$(".paxResaLink").click(function(){
		$(".rprof").hide();
		if ($(this).attr("rel") > 9 && $("#cdcateg").val() == ""){
			handleType($(this).attr("rel"), 'choixPax');
		} else {
			handleInfos($(this).attr("rel"), 'choixPax');
		}
	});
	
}

function handleType(paxText,el){
	$("#temp_pax").val(paxText);
	$("#"+el).hide();
	$("#choixPartPro").show();
}

function handleInfos(paxText, el){
	$("#choixPartPro").hide();
	$("#outPaxResa").html(paxText);
	$("#pax").val(paxText);
	$("#temp_pax").val(paxText);
	$(".trPaxResa").show();
	$(".returnStep").attr("rel", "step4");
	$("#infosResa").show();
	$("#resaInfoButton").show();
	$("#"+el).hide();
	if ($("#cdcateg").val() == "PREMIUM" || $("#cdcateg").val() == "BUSINESS" ){
		$(".rpro").show();
		$(".rprof").hide();
		handleCode();
		handleInviter(paxText);
	}
	if (paxText > 9){
		$(".rgroupe").show();
	} else {
		$(".rgroupe").hide();
	}
	
}

function handleCode(){
	var id_per = $("#per_id_per").val();
	if (id_per != ""){
		$.ajax({
			   type: "GET",
			   async: false,
			   cache:false,
			   url: "control_ajax.php?action=getImput&id_per="+id_per,
			   success: function(msg){
				   if (msg != ""){
					   $("#tdcode").html(msg);
					   $(".rprocode").show();
					   $(".codeclass").focus(function(){
						   $(".codeimp"+$(this).attr("id")).show();
					   });
					   
					   $(".codeclass").blur(function(){
						   $(".codeimp"+$(this).attr("id")).hide();
					   });
					   
					   $(".codelic").click(function(){
						   alert($(this).html());
						   alert($(this).text());
						   $("#"+$(this).attr("rel")).val($(this).html());
					   });
				   }
			   }
		});
	}
}

function handleInviter(paxText){

	if ( ($("#cdcateg").val() == "PREMIUM" || $("#cdcateg").val() == "BUSINESS") && paxText < 10 ){
	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").show();
		
		for(var i=0;i<nb;i++){
			inner += '<p class="paddingtop5 txtformresa"><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").hide();
		}
	} else {
		$("#trInviter").hide();
	}
}

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

function getProm(date){
	var id_per = $("#id_per").val();
	eval($("#opened").val());
	var noWeekend = $.datepicker.noWeekends(date);
	return displayDays(date, opDays);
}


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];
}
