	$(document).ready(function() {
		var cookieName = "enquirylist";
		var enquiryList = new Array();
		var enquiryExists = false;
		
		//read the cookie and put in js array
		function readCookie() {
			if($.cookie(cookieName)){
				enquiryList = {};
				enquiryList = $.cookie(cookieName).split('|');
			}	
		}
		
		//process js array and place in cookie
		function writeCookie() {
			var cookieStr = "";
			if ((enquiryList != null) && (enquiryList.length > 0)) {
				for (var x=0; x < enquiryList.length; x++) {
					cookieStr += enquiryList[x] + '|';
				}

				$.cookie(cookieName, "", { path: '/' });
				if (enquiryList.length > 0) {
					cookieStr = cookieStr.substring(0, (cookieStr.length-1));
				}
			}
			$.cookie(cookieName, cookieStr, { path: '/' });
		}
		
		//destroy the cookie
		function deleteCookie() {
			$.cookie(cookieName, null);
		}
		
		// show stored 
		function showExistingSelected() {
			readCookie();
			if ((enquiryList != null) && (enquiryList.length > 0)) {
				for (var z=0; z < enquiryList.length; z++) {
					$("#" + enquiryList[z]).addClass("added");
					$("#" + enquiryList[z] + "_image").addClass("added");
					
					//calender stuff
					$("input#" + enquiryList[z]).attr('checked', true);
				}
				$(".enquiry_status").html('You have selected <span class="colour_num">' + enquiryList.length + "</span> cruises to enquire about.");
			}
		}
		
		// check enquiry
		function checkEnquiry(id) {
			enquiryExists = false;
			readCookie();

			for (var i=0; i< enquiryList.length; i++) {
				if (enquiryList[i] == id) {
					enquiryExists = true;
					var position = i;
				}
			}
						
			if (enquiryExists == false) {
				$("#" + id).addClass("added");
				$("#" + id + "_image").addClass("added");
				enquiryList.push(id);
			}
			else {
				$("#" + id).removeClass("added");
				$("#" + id + "_image").removeClass("added");
				enquiryList.splice(position, 1);
			}
			if (enquiryList.length >= 0) {
				$(".enquiry_status").html('You have selected <span class="colour_num">' + enquiryList.length + "</span> cruises to enquire about.");			
			}
			writeCookie();
		}
		
		function checkCalenderEnquiry(id) {
			enquiryExists = false;
			readCookie();

			for (var i=0; i< enquiryList.length; i++) {
				if (enquiryList[i] == id) {
					enquiryExists = true;
					var position = i;
				}
			}

			if (enquiryExists == false) {
				enquiryList.push(id);
			}
			else {
				enquiryList.splice(position, 1);
			}
			writeCookie();
		}
		
		$(".btn_enquiry").click(function() {
			var id = $(this).attr('id');
			checkEnquiry(id);
		});

		// add deal enquiry
		$(".cruises-request-quote a").click(function() {
			var id = $(this).attr('id');
			checkEnquiry(id);
		});
		
		//calender enquiry
		$(".month_select").click(function() {
			var id = $(this).attr('name');
			checkCalenderEnquiry(id);
		});
		
		$("#calender_enquiry").click(function(evt) {
			if (enquiryList.length == 0) {
				evt.preventDefault();
				alert("Enquiry List is empty - Please add an enquiry");
			}
		});

		$(".enquiry_submit").click(function(evt) {
			if (enquiryList.length == 0) {
				evt.preventDefault();
				alert("Enquiry List is empty - Please add an enquiry");
			}
		});
		
		

		//on load pre-select the enquiries based on stuff stored in cookie
		showExistingSelected();
	});
