// functions.js

var cur_breadcrumb = 1;
var cur_food_item_search_text = '';
var cur_food_item_restaurant_id = '';
var isSearchResult = false;
var isIntSearchResult = false;
var restaurant_total_prices = new Array();
var price_for_additional_shipping = 30;

var current_coupon = '';

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function _gen_rand_number() {
	return Math.floor(Math.random()*100000000000000001);
}

function mouseOverEffect() {

	var type = $(this).attr('id').split("-")[0];
				
	$(this).mouseover(function(){
		
		$(this).addClass(type + '-hover');
	});
	
	$(this).mouseout(function() {
		
		$(this).removeClass(type + '-hover');
		
	});

}

function typeToText(type) {
	if (type == "restaurant")
		return "Restaurant";
	else if(type == "cuisine")
		return "Cuisine";
	else if(type == "food_type")
		return "Food Type"
		
	return type;
}

function addEmailToForm() {
	var email = $('span#span_contact_email').text();
	$('input#contact_email').attr("value", email);
	
	return;
}

function choiceMouseClick() {
	var options = $(this).attr('id').split('-');
	
	var type = options[0];
	var search_param = options[1];
	var randomNum = _gen_rand_number();
	
	isSearchResult = false;

	$('#filter-div').animate({
			width:"619px",
			opacity: "1.0"
		}, 1000);
	
	// reset the menu div
	$('#food-item-div').animate({
		width:"403px",
		marginLeft: "216px"
	}, 1000);
		
	$('#filter-text').fadeOut(1000);
	
	$("#filter-sub-1-top").fadeOut(1000);
	$("#filter-sub-1").fadeOut(1000);
	$("#filter-sub-2").fadeOut(1000);
	$("#filter-sub-3").fadeOut(1000);
	
	$("#detail-info").fadeOut(1000);
	$("#detail-extra").fadeOut(1000);
	$("#detail-initial").fadeOut(1000);
	$("#detail-div").removeClass("highlight-div");
	
	$("#extra_markup").empty();
	

	$("#filter-sub-1").load("http://saintdelivery.com/index.php/ajax/get_list/" + type + "/" + "x" + "/" + randomNum, {}, function() {$(this).fadeIn(1000);});
	
	$("#breadcrumbs-1").empty();
	$("#breadcrumbs-2").empty();
	$("#breadcrumbs-3").empty();
	$("#breadcrumbs-4").empty();
	
	cur_breadcrumb = 2;
}

function goBackMouseClick() {
	$("#detail-info").fadeOut(1000);
	$("#detail-extra").fadeOut(1000);
	$("#detail-initial").fadeOut(1000);
	$("#detail-div").removeClass("highlight-div");


	if(isSearchResult == true) {
		$('#filter-div').animate({
				width:"619px",
				opacity: "1.0"
			}, 1000);
		
		// reset the menu div
			$('#food-item-div').animate({
				width:"0px",
				marginLeft: "0px"
			}, 1000);
		
		$("#filter-text").fadeIn(1000);
		
		$("#breadcrumbs-3").empty();
		cur_breadcrumb = 2;
	}
	
	else {
	
		// reset all menus
		$("#filter-sub-1-top").fadeOut(1000);
		
		// reset the menu div
			$('#food-item-div').animate({
				width:"403px",
				marginLeft: "216px"
			}, 1000);
		
		// view only 2 columns to menu div
		if(isIntSearchResult == true) {
			
			$('#filter-div').animate({
				width:"186px",
				opacity: "1.0"
			}, 1000);
		
			
			$("#food-item-div").html($("#extra_markup").html());
		
		}
		
		else { 
		
			$('#filter-div').animate({
				width:"619px",
				opacity: "1.0"
			}, 1000);
			
			var randomNum = _gen_rand_number();
			$("#food-item-div").load("http://saintdelivery.com/index.php/ajax/get_food_items/" + cur_food_item_search_text  + "/" + cur_food_item_restaurant_id + "/2" + "/x/" + randomNum, function() {scrollToSelectedFoodItem();});
		}
		
		//If back button is from search result, show the correct div
		if(isSearchResult == true) {$("filter-text").fadeIn(1000);}
		
		else {$("#filter-sub-1").fadeIn(1000);}
		
		var type = $("#breadcrumbs-2").text().split(':')[0];
		
		
		if(type == "Cuisine") {
			$("#filter-sub-2").fadeIn(1000);
			$("#filter-sub-3").fadeIn(1000);
		}
		
		else if (type == "Food Type") {$("#filter-sub-2").fadeIn(1000);}
	
	}
	
} 

function scrollToSelectedFoodItem() {

		if($('a.highlight-item').length) {
			var divOffset = $('#food-item-div').offset().top;
			var pOffset = $('a.highlight-item').offset().top;
			var pScroll = pOffset - divOffset;
			$('#food-item-div').animate({scrollTop: '+=' + pScroll + 'px'}, 1000);
		}
		else {
			var divOffset = $('#food-item-div').offset().top;
			$('#food-item-div').animate({scrollTop: '=' + divOffset + 'px'}, 1000);
		}
}


function foodItemMouseClick() {
		var options = $(this).attr("id").split("-");
		var entry_id = options[1];
		var searchText = options[2].replace(/_/g, "%20");
		searchText = searchText.replace(/\u2019/g, "'");
		var breadCrumbText = options[2].replace(/_/g, " ");
		var restaurant_entry_id = options[3];
		
		// add more viewable columns to menu
		var randomNum = _gen_rand_number();
		$("#detail-initial").fadeOut(500);
		
		// if the food item area is already full sized, don't bother sending another request
		if( (!isSearchResult) && ($('#food-item-div').css('width') != '617px') ) {$("#food-item-div").load("http://saintdelivery.com/index.php/ajax/get_food_items/" + cur_food_item_search_text  + "/" + cur_food_item_restaurant_id + "/3" + "/" + entry_id + "/" + randomNum, {}, function() {
				if ( $('#closed-restaurant-' + cur_food_item_restaurant_id).length ) {
				
					var text =  $('#closed-restaurant-' + cur_food_item_restaurant_id).text();
					$('h3#menu-title').append(' ' + text);
				}
				
				scrollToSelectedFoodItem();

			});
		
			// collapse the restaurant column
			$('#filter-div').animate({
				width:"0",
				opacity: 0.0
			}, 1000);
			
			// expand the menu div
			$('#food-item-div').animate({
				width:"617px",
				marginLeft: 0
			}, 1000);
		
		}
				
		
		$("#food-item-div a.highlight-item").removeClass("highlight-item");
	
		$(this).addClass("highlight-item");

		$("#detail-info").fadeOut(500);
		$("#detail-extra").fadeOut(500);
		$("#detail-div").removeClass("highlight-div");
		

		$("#detail-info").load("http://saintdelivery.com/index.php/ajax/get_details/" + entry_id + "/" + randomNum, {}, function(responseText, textStatus, XMLHttpRequest){
			$(this).fadeIn(1000); 
			refreshLinks("detail-info");
			$("#detail-div").addClass("highlight-div");
		});
		
		$("#detail-extra").load("http://saintdelivery.com/index.php/ajax/get_sides/" + searchText + "/" + restaurant_entry_id + "/" + entry_id + "/" + randomNum, {}, function(responseText, textStatus, XMLHttpRequest){
			$(this).fadeIn(1000); 
			refreshLinks("detail-extra");
			$("#detail-div").addClass("highlight-div");
			
			checkIfSidesCanBeAdded();
		});
		
		//$("#breadcrumbs-" + cur_breadcrumb).text(" -> Food Item: " + breadCrumbText);
}

function replaceBadChars(str, disp) {

	var newStr = str.replace(/\u2019/g, "'");
	newStr = newStr.replace(/\*\*/g, "-"); 
	
	if(disp == true) {
		newStr = newStr.replace(/_/g, " ");
	}
	
	else {
	    newStr = newStr.replace(/_/g, "%20");
	}


	return newStr;
}

function checkIfSidesCanBeAdded() {
	setTimeout(function() {
		var code = $("#detail-info form input[name='code']").val();
		
		var itemExists = false;
		jQuery.each(fc_json.products, function() {
			var this_product = this;
			var this_code = this_product.code;
			if(this_code == code) {
				itemExists = true;
			}
		});
		
		if(itemExists == true) {
			$("#detail-extra input.foxycart_link").removeAttr("disabled");
		}
		else {
			$("#detail-extra input.foxycart_link").attr("disabled", "disabled");
		}	
		
		}, 1000);	
}

function getListLinkMouseClick() {
	var options = $(this).attr("id").split('-');
	var section = options[0];
	var divNum = Number(options[1]);
	var plainSearchText = options[2].replace(/_/g, " ");
	plainSearchText = plainSearchText.replace(/\u2019/g, "'");
	var searchText = options[2].replace(/_/g, "%20");
	searchText = searchText.replace(/\u2019/g, "'");
	var breadCrumbText = options[2].replace(/_/g, " ");
	var searchText2 = options[3].replace(/_/g, "%20");
	searchText2 = searchText2.replace(/\u2019/g, "'");
	var randomNum = _gen_rand_number();
	
	isIntSearchResult = false;
	
	var cur_div = $(this).parent().parent().parent();
	$(".highlight-item", cur_div).removeClass("highlight-item");
	
	$("#detail-div").removeClass("highlight-div");
	$("#detail-info").fadeOut(500);
	$("#detail-extra").fadeOut(500);
	
	$(this).parent().addClass("highlight-item");
	
	if(section == "restaurant") {
		
		// fade out all boxes for menu
		$("#detail-initial").fadeOut(500);
		
		$("#filter-sub-1").fadeOut(1000);
		$("#filter-sub-2").fadeOut(1000);
		$("#filter-sub-3").fadeOut(1000);
		
		// move restaurant box into top box
		$("#filter-sub-1-top").html(cur_div.html());
		$("#filter-sub-1-top").fadeIn(1000);	

		$("#food-item-div").fadeOut(500);
		
		// load menu div
		cur_food_item_search_text = searchText;
		cur_food_item_restaurant_id = searchText2;
		
		$("#detail-initial").load("http://saintdelivery.com/index.php/ajax/get_restaurant_details/" + searchText2  + "/" + randomNum, {}, function() {$(this).fadeIn(500);});
		
		
		$("#food-item-div").load("http://saintdelivery.com/index.php/ajax/get_food_items/" + searchText  + "/" + searchText2 + "/2" + "/x/" + randomNum, {}, function(responseText, textStatus, XMLHttpRequest){
			//$("#food-item-div").html( $('#food-item-div').html() );
			
			$('#filter-div').animate({width:"188px"}, 1000);

			$(this).fadeIn(500); 
			
			if ( $('#closed-restaurant-' + cur_food_item_restaurant_id).length ) {
			
				var text =  $('#closed-restaurant-' + cur_food_item_restaurant_id).text();
				$('h3#menu-title').append(' ' + text);
			}
			
			scrollToSelectedFoodItem();
						
		});
	
	}
	
	else if (section == "cuisine") {
		
		// fade out other boxes
		$("#filter-sub-1-top").fadeOut(1000);
		$("#filter-sub-2").fadeOut(1000);
		$("#filter-sub-3").fadeOut(1000);
		
		//load food type and restaurant boxes
		$("#filter-sub-" + 3).load("http://saintdelivery.com/index.php/ajax/get_list/" + "cuisine_food_type" + "/" + searchText + "/" + searchText2 + "/" + randomNum, {}, function() {$(this).fadeIn(1000);});
		
		$("#filter-sub-" + 2).load("http://saintdelivery.com/index.php/ajax/get_list/" + "cuisine_restaurant" + "/" + searchText + "/" + searchText2 + "/" + randomNum, {}, function() {$(this).fadeIn(1000);});
		
	}
	
	else if (section == "food_type") {
		
		/*
		$("#filter-sub-1-top").fadeOut(1000);
		for(i = divNum; i <= 3; i++) {
			$("#filter-sub-" + i).fadeOut(1000);
		}
		*/
		
		isIntSearchResult = true;
				
		// fade out all boxes for menu
		$("#detail-initial").fadeOut(500);
		
		$("#filter-sub-1").fadeOut(1000);
		$("#filter-sub-2").fadeOut(1000);
		$("#filter-sub-3").fadeOut(1000);
		
		$("#food-item-div").fadeOut(1000);
		
		// move restaurant box into top box
		$("#filter-sub-1-top").html(cur_div.html());
		$("#filter-sub-1-top").fadeIn(1000);
		$("#filter-sub-1").html(cur_div.html());
		
		
		/*
		$("#filter-sub-" + divNum).load("http://saintdelivery.com/index.php/ajax/get_list/" + section + "/" + searchText + "/" + searchText2 + "/" + randomNum, {}, function() {$(this).fadeIn(1000);});
		*/
		
		
		// food item search by food type
		$("#food-item-div").load("http://saintdelivery.com/index.php/ajax/get_food_items_by_food_type/" + randomNum, {food_type: plainSearchText}, function(){
				
			$('#filter-div').animate({width:"188px"}, 1000);
			
			$("#extra_markup").html($(this).html());

			$(this).fadeIn(500); 	
		});

		
	}
	
	// Modify breadcrumbs
	
	$('span#breadcrumbs-initial').hide();
	first_breadcrumb = true;
	for(i=2; i<=4; i++) {
		
		if(divNum == i) {
			
			prefix = " -> ";
			if(first_breadcrumb) {
				prefix = "";
			}
			
			if ( (section == 'restaurant') && $('#closed-restaurant-' + cur_food_item_restaurant_id).length ) {
			
				var closedRestaurantText =  $('#closed-restaurant-' + cur_food_item_restaurant_id).text();
				breadCrumbText += ' ' + closedRestaurantText;
			}
			
			$("#breadcrumbs-" + i).text(prefix + typeToText(section) + ": " + breadCrumbText);
		}
		
		else if(i > divNum) {
			$("#breadcrumbs-" + i).empty();
		}
		
		if( $("#breadcrumbs-" + i).text() != "") {
			first_breadcrumb = false;
		}
		
	}	
}

function searchResultsFoodItemClick() {

		var options = $(this).attr("id").split('-');
		var food_item_name = replaceBadChars(options[1], false);
		var food_item_disp_name = replaceBadChars(options[1], true);
		var food_item_entry_id = options[2];
		var restaurant_name = replaceBadChars(options[3],false);
		var restaurant_entry_id = options[4];
		var cur_food_type = "";
		if(options.length > 5) {
			cur_food_type = replaceBadChars(options[5],false);
		}
		var randomNum = _gen_rand_number();
		
		$('#filter-text').fadeOut(1000);
		$("#filter-sub-1").fadeOut(1000);
		$("#filter-sub-2").fadeOut(1000);
		$("#filter-sub-3").fadeOut(1000);
		$("#filter-sub-1-top").fadeOut(1000);
		
		cur_food_item_restaurant_id = restaurant_entry_id;
		cur_food_item_search_text = restaurant_name;
		
		// collapse the restaurant column
		$('#filter-div').animate({
			width:"0",
			opacity: 0.0
		}, 1000);
		
		// expand the menu div
		$('#food-item-div').animate({
			width:"617px",
			marginLeft: 0
		}, 1000);

		$("#detail-info").load("http://saintdelivery.com/index.php/ajax/get_details/" + food_item_entry_id + "/" + randomNum, {}, function(responseText, textStatus, XMLHttpRequest){
			$(this).fadeIn(1000); 
			refreshLinks("detail-info");
			$("#detail-div").addClass("highlight-div");
		});
		
		$("#detail-extra").load("http://saintdelivery.com/index.php/ajax/get_sides/" + food_item_name + "/" + restaurant_entry_id + "/" + food_item_entry_id + "/" + randomNum, {}, function(responseText, textStatus, XMLHttpRequest){
			$(this).fadeIn(1000); 
			refreshLinks("detail-extra");
			$("#detail-div").addClass("highlight-div");
			
			checkIfSidesCanBeAdded();
		});
		
		$("#breadcrumbs-3").text(" -> Food Item: " + food_item_disp_name);
		cur_breadcrumb = 3;
	
		$("#food-item-div").empty();
		
		var add_param = "";
		if (isIntSearchResult == true) {
			add_param = "/" + cur_food_type;
		}
		
		$("#food-item-div").load("http://saintdelivery.com/index.php/ajax/get_food_items/" + restaurant_name  + "/" + restaurant_entry_id + "/3/" + food_item_entry_id + add_param + "/" + randomNum, {}, function(responseText, textStatus, XMLHttpRequest){

			$(this).fadeIn(500); 
						
			if ( $('#closed-restaurant-' + cur_food_item_restaurant_id).length ) {
			
				var text =  $('#closed-restaurant-' + cur_food_item_restaurant_id).text();
				$('h3#menu-title').append(' ' + text);
			}
			
			scrollToSelectedFoodItem();
						
		});

}


function autocompleteFormatItem(resultsrow, rowpost, numitems, searchterm) {

	var resultsstring = resultsrow + "";
	var resultsarray = resultsstring.split(",");
	var title = resultsarray[0];
	
	return title;	

}

function autocompleteFormatResults(data, pos, numitems) {
	
	var str = data + "";
				
	var resultsarray = str.split(" at ");
	var title = resultsarray[0];
	//var restaurant = resultsarray[1];
	
	return " ";
	
}

function submitEntryIDSearchForm(event, data, formatted) {
	//var entry_id = $("span#search_entry_id").text();
	
	var str = data + ""; 
	str = str.split("</span>")[0];
	str = str.split('<span class="search_entry_id">')[1];
	
	isSearchResult = true;
	
	$("input#entry_id").attr("value", str);
	
	$('#filter-text').fadeOut(1000);
	
	$("#filter-sub-1-top").fadeOut(1000);
	$("#filter-sub-1").fadeOut(1000);
	$("#filter-sub-2").fadeOut(1000);
	$("#filter-sub-3").fadeOut(1000);
	
	$("#detail-info").fadeOut(1000);
	$("#detail-extra").fadeOut(1000);
	$("#detail-initial").fadeOut(1000);
	$("#detail-div").removeClass("highlight-div");
		
		// expand the menu div
		$('#filter-div').animate({
			width:"617px",
			opacity: 1.0
		}, 1000);
	
	$("#filter-text").load("http://saintdelivery.com/index.php/site/search", {'keywords' : "", 'entry_id' : str}, function() {$(this).fadeIn(1000);} );
	
	$('span#breadcrumbs-initial').hide();
	$("#breadcrumbs-1").empty();
	$("#breadcrumbs-2").empty();
	$("#breadcrumbs-3").empty();
	$("#breadcrumbs-4").empty();
	
	
	$("#breadcrumbs-2").text("Search: Food Item #" + str);
	cur_breadcrumb = 3;
	
	$('html, body').animate({
		scrollTop: $("#shoppingStart").offset().top
	}, 2000);
	
	//document.search.submit();
}

function submitKeywordSearchForm() {
	//var entry_id = $("span#search_entry_id").text();
		
	var keywords = $("input#keywords").attr("value");
	
	isSearchResult = true;
	
	
	$('#filter-text').fadeOut(1000);
	
	$("#filter-sub-1-top").fadeOut(1000);
	$("#filter-sub-1").fadeOut(1000);
	$("#filter-sub-2").fadeOut(1000);
	$("#filter-sub-3").fadeOut(1000);
	
	$("#detail-info").fadeOut(1000);
	$("#detail-extra").fadeOut(1000);
	$("#detail-initial").fadeOut(1000);
	$("#detail-div").removeClass("highlight-div");
	

		// expand the menu div
		$('#filter-div').animate({
			width:"617px",
			opacity: 1.0
		}, 1000);
	
	$("#filter-text").load("http://saintdelivery.com/index.php/site/search", {'keywords' : keywords, 'entry_id' : ''}, function() {$(this).fadeIn(1000);} );
	
	$('span#breadcrumbs-initial').hide();
	$("#breadcrumbs-1").empty();
	$("#breadcrumbs-2").empty();
	$("#breadcrumbs-3").empty();
	$("#breadcrumbs-4").empty();
	
	
	$("#breadcrumbs-2").text('Search: "' + keywords + '"');
	cur_breadcrumb = 3;
	
	$('html, body').animate({
		scrollTop: $("#shoppingStart").offset().top
	}, 2000);
	//document.search.submit();
}

function refreshLinks(containerName)
{

	var FoxyDomain = "saintdelivery.foxycart.com"; 

	if  (location.href.substring(location.href.lastIndexOf("/")+1,location.href.lastIndexOf("/")+5)!="cart") {
	
		fc_UpdateCart(FoxyDomain);
		
		jQuery("#" + containerName + " form.foxycart").submit( function(){
			
			MyFoxyData=jQuery(this).serialize(false);
			MyFoxyID=jQuery(this).attr("id");
			
			if(typeof(fc_PreProcess)=="function"){
				if(!fc_PreProcess(MyFoxyData,MyFoxyID)) {
					return false;
				}
			}
			
			if(MyFoxyData.match("cart=checkout")||MyFoxyData.match("redirect=")) {location.href="https://"+FoxyDomain+"/cart.php?"+MyFoxyData+fc_AddSession();}
			else {
				fc_tb_show(null,"https://"+FoxyDomain+"/cart.php?"+MyFoxyData+fc_AddSession(),false);
				
				jQuery("a.fc_tb_closeWindowButton").click(function(){	
					fc_UpdateCart(FoxyDomain);return false;
				})
			}
			
			return false;
		})

	}
}


	
function removeSidesWithoutMainDish() {
	var food_item_array = new Array();
	var did_remove_sides = false;
	
	jQuery.each(fc_json.products, function() {
			this_product = this;
			var is_side = this_product.options["is side"];
			
			if(is_side == "N") {
				var food_item_entry_id = this_product.options["food item entry id"];
				
				food_item_array[food_item_entry_id] = "X";
			}
	});
			
	jQuery.each(fc_json.products, function() {
			this_product = this; 
			var is_side = this_product.options["is side"];
			if(is_side == "Y") {
				
				var food_item_entry_id = this_product.options["food item entry id"];
				
				if(food_item_array[food_item_entry_id] == undefined) {
					did_remove_sides = true;
					id = this_product['id'];
					var url = "https://" + FoxyDomain + "/cart.php?cart=update&output=json&callback=fc_FoxyJSON&id="+id+"&quantity=0"+fc_AddSession();
					jQuery.getScript(url);
				}
			}
	});
	
	setTimeout(function() {	
				if(did_remove_sides) {

					$("#cart-with-rouge-sides").show();
					
					setTimeout(function() {$("#cart-with-rouge-sides").fadeOut(1000);}, 5000);
				}
			}, 2500); 
				
}

function onCheckout() {

	var restaurant_array = new Array();
	var isAnyStoreClosed = false;
	jQuery.each(fc_json.products, function() {
		var this_product = this;
		var restaurant_id = this_product.options["restaurant entry id"];
		
		restaurant_array[restaurant_id] = "X";
	});	
	
	var restaurant_str = "";
	for (key in restaurant_array) {
		restaurant_str += key + "|";
	}
	
	if (restaurant_str != "") {
		restaurant_str = restaurant_str.slice(0, -1)
	}
	var randomNum = _gen_rand_number();
	$.get("http://saintdelivery.com/index.php/ajax/is_restaurant_open/" + restaurant_str + "/" + randomNum, function(data){
		  
	    restaurant_total_prices = new Array();
	    var hasItems = false;

		jQuery.each(fc_json.products, function() {
			this_product = this;
			var restaurant_id = this_product.options["restaurant entry id"];
			
			var isOpen = $(data).find('restaurant#' + restaurant_id + " isOpen").text();
			if(isOpen == 'n') {
				isAnyStoreClosed = true;
				id = this_product['id'];
				var url = "https://" + FoxyDomain + "/cart.php?cart=update&output=json&callback=fc_FoxyJSON&id="+id+"&quantity=0"+fc_AddSession();
					jQuery.getScript(url); 
			}
			
			else {
				hasItems = true;
				var value = this_product["price"];
				var category = this_product["category"];
					
				if(restaurant_total_prices[category] != undefined)
					restaurant_total_prices[category] = restaurant_total_prices[category] + value;
				else
					restaurant_total_prices[category] = value;		
			}	
		});
		
		if(!isAnyStoreClosed) {
				
			for(var i in restaurant_total_prices) {
				this_total_price = restaurant_total_prices[i];
	
				if(this_total_price >= price_for_additional_shipping) {
					var data = 'block_fb=true&x%3Asku=58&code=00058&weight=0.00&name=Added+Shipping+Charges&price=1.00&category=settings&quantity=1';
					jQuery.getScript("https://" + FoxyDomain + "/cart.php?" + data + fc_AddSession());
				}
	
			}
			
			setTimeout(function() {		
				if(hasItems) {
					window.location = "https://" + FoxyDomain + "/cart.php?block_fb=true&cart=checkout" + fc_AddSession();
				}
			},1500);
		
		}
		
		else {
			setTimeout(function() {	
				if(hasItems) {$("#cart-with-old-items").show();}
				else {
					$("#empty-cart-with-old-items").show();
					
					setTimeout(function() {$("#empty-cart-with-old-items").fadeOut(1000);}, 5000);
				}
			}, 2500);
		}
  	 
	}, "xml");

}



function onContinueCheckoutYes() {
	$("#cart-with-old-items").hide();
	for(var i in restaurant_total_prices) {
		this_total_price = restaurant_total_prices[i];

		if(this_total_price >= price_for_additional_shipping) {
			var data = 'block_fb=true&x%3Asku=58&code=00058&weight=0.00&name=Added+Shipping+Charges&price=1.00&category=settings&quantity=1';
			jQuery.getScript("https://" + FoxyDomain + "/cart.php?" + data + fc_AddSession());
		}

	}
	
	setTimeout(function() {		
		window.location = "https://" + FoxyDomain + "/cart.php?block_fb=false&cart=checkout" + fc_AddSession();
	},1500);

}

function onContinueCheckoutNo() {$("#cart-with-old-items").hide();}

function changeCoupon() {


	$('span#change_coupon').hide();
	$('input#coupon_entry').show();
	$('span#coupon_delim').show();
	
	$('input#coupon_entry').click(function (e){
		if( $('input#coupon_entry').attr("value") == "Enter Coupon") {
			$('input#coupon_entry').attr("value", "");
		}
	});
	
	
	$('input#coupon_entry').keypress(function (e){
		
		if(e.which == 13) {
			var coupon = $(this).attr("value");
			
			
			if( $('input#coupon_value').length ){
				$('input#coupon_value').attr("value", coupon)
			}
			else {
				$('#detail-info form.foxycart').append("<input id='coupon_value' size='20' type='hidden' value='" + coupon + "' name='coupon' />");
			}
		
			
			var data = 'block_fb=true&coupon=' + coupon;
			$.get("https://" + FoxyDomain + "/cart.php?" + data + fc_AddSession());
			
			$('span#change_coupon').html('<br />Coupon "' + coupon + '" applied on checkout');
			$('span#change_coupon').show();
			$('input#coupon_entry').hide();
			$('span#coupon_delim').hide();
		}
	
	});


}

function dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }

    alert(out);
}

