$(document).ready(function(){


	/* ----- Reservations Form Changer ----- */
	
	$('#accommodation_reservations').hide(); // Hide the accommodation form by default.
	checkFormState(); // Check current form state.

	function loadAccommodation(){
			$('#restaurant_reservations').hide();
			$('#accommodation_reservations').fadeIn();
			$("#res_form_fields").attr("action","https://reservations.synxis.com/gc/rez.aspx");
			$("#res_form_fields").attr("method","get");
			$("#res_form_fields").attr("target","_blank");
	}
	
	function loadReservation(){
			$('#accommodation_reservations').hide();
			$('#restaurant_reservations').fadeIn();
			$("#res_form_fields").attr("action","/forms/reservations/");
			$("#res_form_fields").attr("method","post");
	}
	
	function checkFormState(){
			if ( $("#type_of_reservation").val() == 'Accommodation' ) {
				loadAccommodation();
			}
			else {
				loadReservation();
			}
	}
	
	$("#type_of_reservation").change(function() {
			checkFormState();
	});
		

	/* ----- Reservations Slider ----- */
		
	$(".reservation").click(function(){
		if ( $("#make_reservation_btn").hasClass('closed') ) {
			$("#reservations_form_container").animate({"top": "0px"}, 300);
			$("#make_reservation_btn").removeClass("closed");
			$("#make_reservation_btn").addClass("open");
			}
		else {
			$("#reservations_form_container").animate({"top": "-135px"}, 300);
			$("#make_reservation_btn").removeClass("open");
			$("#make_reservation_btn").addClass("closed");
		}
	});	
	
	
	/* ----- Form Validation ----- */
		
	function IsValidEmail(email) {
		var reg = new RegExp("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 
		return (reg.test(email))
	}
	
	$('#mailing_subForm').submit(function() {
	
		// Validate Name
		if ( ($("#cm-name").val() == '') || ($("#cm-name").val() == 'Name') ) {
			alert('Please enter your name.');
			$("#cm-name").focus();
			return false;
		}
		// Validate Email
		if (!IsValidEmail($("#mmktr-mmktr").val())) {
			alert("Please enter a valid email address.");
			$("#mmktr-mmktr").focus();
			return false;
		}
	});
				
	$('#res_form_fields').submit(function() {
	
		if ( $("#type_of_reservation").val() != 'Accommodation' ) {
		
			// Validate Reservation Type
			if ( $("#type_of_reservation").val() == '') {
				alert("Please select type of reservation.");
				$("#type_of_reservation").focus();
				return false;
			}
			// Validate Name
			if ( $("#name").val() == '') {
				alert('Please enter your name.');
				$("#name").focus();
				return false;
			}
			// Validate Email
			if (!IsValidEmail($("#email").val())) {
				alert("Please enter a valid email address.");
				$("#email").focus();
				return false;
			}
			// Validate Number in Party
			if ( $("#no_in_party").val() == '') {
				alert("Please select number in party.");
				$("#no_in_party").focus();
				return false;
			}
			// Validate Preferred Time
			if ( $("#pref_time").val() == '') {
				alert("Please enter a preferred time.");
				$("#pref_time").focus();
				return false;
			}			
			// Validate Preferred Date
			if ( $("#pref_date").val() == '') {
				alert("Please enter a preferred date.");
				$("#pref_date").focus();
				return false;
			}
			
		var type = $("#type_of_reservation").val();
		var name = $("#name").val();
		var email = $("#email").val();
		var telephone = $("#telephone").val();
		var no_in_party = $("#no_in_party").val();
		var pref_time = $("#pref_time").val();
		var pref_date = $("#pref_date").val();
		var form_sent = 'PKKH87';		
			
		var dataString = "&type_of_reservation=" + type + "&name=" + name + "&email=" + email + "&telephone=" + telephone + "&no_in_party=" + no_in_party + "&pref_time=" + pref_time + "&pref_date=" + pref_date + "&form_sent=" + form_sent;
		
		$.ajax({
			type: "POST",
			url: "/forms/reservations/",
			data: dataString,
			success: function(html) {
				$('#reservation').html("<div id='message'></div>");
				$('#message').append(html)
				.hide()
				.fadeIn(1500, function() {
					$('#message');
				});
			}
		});
		return false;
		}

	});
	
	
	/* ----- Collapsible Menus ----- */	
	
	$('.dishes_container').hide();
	$('.toggle').append("<span>View Dishes</span>");
	$('.toggle').wrapInner('<a />');
	$('.toggle').click(function() {
		$(this).toggleClass('open');
		var text = $(this).find('span').text();
		$(this).find('span').text(text == "View Dishes" ? "Close" : "View Dishes");				
		$(this).parents().next('.dishes_container').slideToggle('normal');
	});
	
	
	/* ----- Make Promos Clickable ----- */	
		
	$(".promo").click(function(){
     window.location=$(this).find("a").attr("href");
     return false;
});

});

