$(document).ready(function() {
	//////////////////////
	// general features //
	//////////////////////
	$('.delSubmit').click(function() {
		var del = confirm('Are you sure you want to delete these records?'+"\n\n"+'This action may be permanent - check the note on the page.');
		return del;
	});
	$('.date_input').date_input({
		stringToDate: function(string) {
			var matches;
			if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
				return new Date(matches[1],matches[2]-1,matches[3]);
			} else {
				return null;
			}
		},
		dateToString: function(date) {
			var month = (date.getMonth()+1).toString();
			var dom = date.getDate().toString();
			if (month.length==1) month = "0"+month;
			if (dom.length==1) dom = "0"+dom;
			return date.getFullYear()+"-"+month+"-"+dom;
		}
	});
	$('.privateQM').click(function( e ) {
		e.preventDefault();
		alert('This menu has been created by Kitchen Kapers staff specifically for your account.');
	});
	/////////////////
	// signup page //
	/////////////////
	// hide/show info when they select company/individual
	$('.signupType').click(function() {
		if ($(this).val()=='company') {
			$('.custonly').slideUp();
			$('.componly').slideDown();
		} else {
			$('.componly').slideUp();
			$('.custonly').slideDown();
		}
	});
	$('.signupType:checked').trigger('click');
	///////////
	// menus //
	///////////
	// hide/show item info on menus
	$('.menuInfoLink').click(function( e ) {
		e.preventDefault();
		var id = $(this).attr('href').substr(1);
		$('.itemInfo'+id).toggle();
	});
	$('.submitbtn').click(function() {
		var node = $(this);
		var tn = node.get(0).nodeName.toLowerCase();
		while (tn!='form') {
			node = node.parent();
			tn = node.get(0).nodeName.toLowerCase();
		}
		node.submit();
	});
	///////////////////////
	// create order page //
	///////////////////////
	// hide/show hidden sections on order confirmation
	$('.orderConfirm-menu').click(function( e ) {
		e.preventDefault();
		$('.orderConfirm-d-menu').slideToggle();
		if ($(this).text()=='Click to Expand') {
			$(this).text('Click to Hide');
		} else {
			$(this).text('Click to Expand');
		}
	});
	$('.orderConfirm-priceOverride').click(function( e ) {
		e.preventDefault();
		$('.orderConfirm-p-override').slideToggle();
	});
	$('.orderConfirm-disc').click(function( e ) {
		e.preventDefault();
		$('.orderConfirm-d-disc').slideToggle();
		if ($(this).text()=='Click to Expand') {
			$(this).text('Click to Hide');
		} else {
			$(this).text('Click to Expand');
		}
	});
	// set up and submit remove disc form
	$('.removeDisc').click(function( e ) {
		e.preventDefault();
		var code = $(this).attr('rel');
		$('.removediscinput').val(code);
		$('.removediscform').submit();
	});
	// show/hide spread delivery options
	$('.buffetDateSpread').click(function( e ) {
		if ($(this).val()=='y') {
			$('.spreadOne').hide();
			$('.spreadTwo').show();
		} else {
			$('.spreadOne').show();
			$('.spreadTwo').hide();
		}
	});
	///////////////////////
	// order detail page //
	///////////////////////
	$('.cancelorder-click').click(function( e ) {
		e.preventDefault();
		$('.cancelorder-div').slideDown('slow');
	});
	$('.cancelorder-btn').click(function( e ) {
		if (!confirm('Are you sure? You cannot re-instate a cancelled order')) {
			e.preventDefault();
		} else {
			if ($('.cancel-code').val().toUpperCase()!=$('.cancel-auth').val()) {
				alert('You have entered an incorrect code, please try again');
				alert($('.cancel-code').val().toUpperCase());
				e.preventDefault();
			}
		}
	});
});

