﻿$(function() {


	// site url
	var site_url = 'http://' + document.location.hostname;
	if(site_url.indexOf('localhost') > -1){site_url = site_url + '/albm_projects/hfc_eelabs';}
	

	// open external links in a new window
	$("a").click(function(e){
		// the code that determines if the destination link is on the same domain
		if(this.href.indexOf(location.hostname) == -1) {
			$(this).attr('target', 'ext_lnk');
		}
	});

	// reserve item
	$(':submit[id^="reserve_"]').click(function(e) {
		e.preventDefault();
		var button = $(this);
		var button_id = $(this).attr('id').split("_");
		var product_id = button_id[1];
		var qty = $('#qty_'+product_id).val();
		var variation = $('#variation_'+product_id).val();
		var price = $('#price_'+product_id).val();
		button.attr('value', 'Adding item...');
		button.attr('disabled', 'disabled');
		if (isNaN(qty)) {
			alert("Oops! Looks like you entered something in the 'Quantity' field that isn't a number. Please try again.");
			button.attr('value', 'Reserve');
			button.removeAttr('disabled');
			$('#qty_'+product_id).attr('value', '1');
			exit;
		}
		var url = site_url+'/ajax/reservation_add/'+product_id+'/'+qty+'/'+price+'/'+variation;
		$.post(url, function(data) {
			$('#your_reservation').slideDown('fast');
			$('#reservation_summary').text(data);
			button.attr('value', 'Reserve');
			button.removeAttr('disabled');
			$('#confirmation_'+product_id).fadeIn('fast').delay(1000).fadeOut(3000);
		});
	});
	
	// update item
	$(':submit[id^="update_"]').click(function(e) {
		e.preventDefault();
		var button = $(this);
		var button_id = $(this).attr('id').split("_");
		var row_id = button_id[1];
		var qty = $('#qty_'+row_id).val();
		var no_items = $('#no_items').val();
		button.attr('value', 'Updating...');
		button.attr('disabled', 'disabled');
		if (isNaN(qty)) {
			alert("Oops! Looks like you entered something in the 'Quantity' field that isn't a number. Please try again.");
			button.attr('value', 'Update');
			button.removeAttr('disabled');
			$('#qty_'+row_id).attr('value', qty);
			exit;
		}
		var url = site_url+'/ajax/reservation_update/'+row_id+'/'+qty+'/';
		$.post(url, function(data) {
			$('.green_price').text(data);
			button.attr('value', 'Update');
			button.removeAttr('disabled');
			if (qty == 0) {
				$('tr#'+row_id).fadeOut('slow');
				no_items = no_items - 1;
				$('#no_items').val(no_items);
				if (no_items == 0) {
					var redirect = site_url+'/products/';
					window.location.replace(redirect);
				}
			}
		});
	});
	
	// continue browsing
	$('#continue_browsing').click(function(e) {
		e.preventDefault();
		var redirect = site_url+'/products/';
		window.location.replace(redirect);
	});
	
	// cancel reservation
	$('#cancel_reservation').click(function(e) {
		e.preventDefault();
		var cancel = confirm('Are you sure you want to cancel this reservation?');
		if (cancel) {
			$('tr').fadeOut('slow');
			$('.green_price').fadeOut('slow');
			$(this).fadeOut('slow');
			var redirect = site_url+'/reservation/cancel/';
			window.location.replace(redirect)
		}
	});
	
	// submit reservation (sanity checks)
	$(':submit[id="submit_reservation"]').click(function(e) {
		e.preventDefault();
		var name = $('#name').val();
		var email = $('#email').val();
		var message = $('#message').val();
		var validform = true;
		// validate name
		if (name == '') {
			validform = false;
			$('#lbl_name').css('color', 'red');
			$('#name').css('border', 'solid 1px red');
		}
		else {
			$('#lbl_name').css('color', '');
			$('#name').css('border', '');		
		}
		// validate email
		if (email == '') {
			validform = false;
			$('#lbl_email').css('color', 'red');
			$('#email').css('border', 'solid 1px red');
		}
		else if (!isValidEmailAddress(email)) {
			validform = false;
			$('#lbl_email').css('color', 'red');
			$('#email').css('border', 'solid 1px red');
		}
		else {
			$('#lbl_email').css('color', '');
			$('#email').css('border', '');
		}
		// if there are any errors, alert the user
		if(!validform) {
			$('#error_message').fadeIn('slow');
		}
		// otherwise, prepare to submit the form
		else {
			$('#error_message').hide();
			$(':input').attr('disabled', 'disabled');
			$(':submit').attr('disabled', 'disabled');
			$('#submit_reservation').replaceWith('<p align="center"><img src="'+site_url+'/images/loading.gif" alt="" style="display: inline; margin: 10px 0 0 10px;" /><br />Submitting your reservation.</p>');
			// ajax/post
			var url =  site_url+'/ajax/reservation_confirm/';
			var data = { 'name': name, 'email': email, 'message': message }
			$.post(url, data, function(response) {
				$('#reservation_wrapper').html('<p><b>Thank you.</b> Your reservation has been submitted, and an email confirmation sent to the address you provided.</p><p>When coming to collect your goods, you can find us in the main Headington shopping parade, on the south side of the London Road.</p><p><a href="../visit">Visit the Windmill Shop</a></p>');
			});
		}
	});
	
	// toggle hide/show extra product info
	$('.slide_trigger').click(function(e) { 
		e.preventDefault();
		var item_id = $(this).attr('id');
		$('#extra_'+item_id).slideToggle('fast');
		$(this).text($(this).text() == 'more info' ? 'less info' : 'more info');
	});

});

// additional functions
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
