
jQuery(document).ready(function() {
    
    //setConfirmUnload(true);  // Prevent accidental navigation away

	jQuery("#search-zips-submit").click(function(){
	
		if(jQuery("#currentZip").val() != '-1'){
		
			if(jQuery("#currentZip").val() != jQuery("#zip-search-box").val()){
				verify = confirm("If you change your current zip code, your cart contents will be reset.\nAre you sure you want to continue?");
				
				if(!verify){
					jQuery("#chooseZipCodeForm").attr("onsubmit","return false();");
					jQuery("#chooseZipCodeForm").attr("action","");
				}
				else{
					jQuery("#chooseZipCodeForm").removeAttr("onsubmit");
					jQuery("#chooseZipCodeForm").attr("action","restaurants");
				}
			}
		}
	
	});
	
	jQuery(".menu-cat").click(function(){
		//remove selected from all other categories
		jQuery(this).parent().children().removeClass("selected");
	
		jQuery(this).addClass("selected");
		getCategory(this.id);

	});
	
	add_triggers = jQuery("button[rel]").overlay({
		
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		
		},
		closeOnClick: false
	});
	
	
	edit_triggers = jQuery("a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		
		},
		closeOnClick: false
	});

	

	jQuery("#clear-order").click(function(){
		
		jQuery('#order-items-container').html("<img id=\"menu-loader\" src=\"/images/ajax-loader-bar.gif\" />");
		
		jQuery.ajax({
			type: "GET",
			url: "/restaurants/clear-order",		
			dataType: "html",
			success: function(html){
				jQuery('#shopping-cart').html(html);	
			},
			error: function(){
				//alert("There was an error in your request");
			}
		});					
	});
	

	Date.format = 'mm/dd/yyyy';
	jQuery("#datepicker").datePicker({
		clickInput: true
	});
	
	if(document.getElementById('checkoutPayment')){
		if(jQuery("input[name=paymentChoice]:checked").val() == "cc"){
			jQuery("#credit-info").show();
		}
	}
	
	if(document.getElementById('order-time')){
		if(jQuery("input[name=timeChoice]:checked").val() == "later"){
			jQuery("#date-form").show();
		}	
	
	}
	
	loadButtonOverlay();
	
	if(document.getElementById('deliverylocation')){
	
		loc = jQuery("#deliverylocation").val();
	
		if( loc.match("home") ){
			jQuery("#loc-label").hide();	
			jQuery("#locationerrors").hide();
			jQuery("#locationname").hide();
		}
	}

});


function loadButtonOverlay(){

	jQuery("#checkout-next").click(function(){
		jQuery("#lock-pane").removeClass('LockOff');
		jQuery("#lock-pane").addClass('LockOn');
		
		scroll(0,0);
	
	});
	
	jQuery("#back-link").click(function(){
		jQuery("#lock-pane").removeClass('LockOff');
		jQuery("#lock-pane").addClass('LockOn');
	
		scroll(0,0);
	});		
	
	jQuery("#submit-order-button").click(function(){
		jQuery("#lock-pane").removeClass('LockOff');
		jQuery("#lock-pane").addClass('LockOn');
	
		scroll(0,0);
	});	

	
	
	jQuery("#checkout-steps li a").click(function(){
		jQuery("#lock-pane").removeClass('LockOff');
		jQuery("#lock-pane").addClass('LockOn');
	
		scroll(0,0);
	});	
}

function showDateForm(){

	if(jQuery("input[name=timeChoice]:checked").val() == "later"){
		jQuery("#date-form").show();
	}	
	else{
		jQuery("#date-form").hide();	
	}


	//there are only two choices on the form
	//jQuery("#date-form").toggle();

}


function setConfirmUnload(on) {
   
    //save any form data to session
	//remember current page
	//
	
	window.onbeforeunload = (on) ? unloadMessage : null;

}

function unloadMessage() {
   
     return 'If you leave this page without first saving your data, the changes can be lost.';

}



/**************************************/
/* Content updater functions
/**************************************/

//MENU
function handleResponse(html)
{
    //jQuery('#menu-info').html(html);
	document.getElementById("menu-info").innerHTML = html;
	
	// re-initialize events after content change
	jQuery("#clear-order").click(function(){
		
		jQuery('#order-items-container').html("<img id=\"menu-loader\" src=\"/images/ajax-loader-bar.gif\" />");
		
		jQuery.ajax({
			type: "GET",
			url: "/restaurants/clear-order",		
			dataType: "html",
			success: function(html){
				jQuery('#shopping-cart').html(html);	
			},
			error: function(){
				alert("There was an error in your request");
			}
		});						
	});	
	
	add_triggers = jQuery("button[rel]").overlay({
		
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		
		},
		closeOnClick: false
	});	
}

//SHOPPING CART
function handleOrderResponse(html)
{
    //jQuery('#shopping-cart').html(html);
	document.getElementById("shopping-cart").innerHTML = html;
	
	jQuery("#clear-order").click(function(){
		
		jQuery('#order-items-container').html("<img id=\"menu-loader\" src=\"/images/ajax-loader-bar.gif\" />");
		
		jQuery.ajax({
			type: "GET",
			url: "/restaurants/clear-order",		
			dataType: "html",
			success: function(html){
				jQuery('#shopping-cart').html(html);	
			},
			error: function(){
				alert("There was an error in your request");
			}
		});				
		
	});	
	
	//jQuery(".order-item").mouseenter(function(){
	//	jQuery(this).parents("#shopping-cart").children(".item-options").hide();
	//	jQuery(this).children(".item-options").show();	
		//jQuery(this).css("background-color", "#ccc");
	//});
	
	
	//jQuery(".order-item").mouseleave(function(){
	//	jQuery(this).parents("#shopping-cart").children(".item-options").hide();
	//	jQuery(this).children(".item-options").hide();
		//jQuery(this).css("background-color", "#ccc");
	//});
		
	edit_triggers = jQuery("a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		
		},
		closeOnClick: false
	});
}

function handleCheckoutResponse(html){

	//jQuery("#checkout-workspace").html(html);
	document.getElementById("checkout-workspace").innerHTML = "";
	document.getElementById("checkout-workspace").innerHTML = html;
	
	edit_triggers = jQuery("a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		
		},
		closeOnClick: false
	});	
}

function handleRegistrationResponse(html){

	//jQuery('#registration-holder').html(html);
	document.getElementById("registration-holder").innerHTML = html;
}

/**************************************/
/* ajax function calls
/**************************************/

function getCategory(catID)
{

	jQuery('#menu-info').html("<img id=\"menu-loader\" src=\"/images/ajax-loader-bar.gif\" />");

	jQuery.ajax({
		type: "GET",
   		url: "/restaurants/get-data",		
	    data: ({s: "get_category_trigger", id: catID}),
		dataType: "html",
		success: function(html){
			handleResponse(html);	
		},
		error: function(){
			alert("There was an error in your request");
		}
	});
	
}

function updatePrice(formID, priceDestination){

	number = jQuery("#" + formID).children(".options").children("#item-quantity").val();

	// make sure quantity is numerical data or do nothing
	if( (!isNaN(number)) && (number > 0)){

		formData = jQuery("#" + formID).serialize();
	
		jQuery.ajax({
			type: "POST",
   			url: "/restaurants/get-price",		
	   	 	data: formData,
			dataType: "html",
			success: function(html){
				jQuery("#"+ priceDestination).html(html);		
			},
			error: function(){
				alert("There was an error in your request");
			}
		});
		}
	else{
		alert("You need to have at least 1 item.");
	}

}

function addItemToCart(formID){

	number = jQuery("#" + formID).children(".options").children("#item-quantity").val();
	
	// make sure quantity is numerical data or do nothing
	if( (!isNaN(number)) && (number > 0)){

	jQuery('#order-items-container').html("<img id=\"menu-loader\" src=\"/images/ajax-loader-bar.gif\" />");


		formData = jQuery("#" + formID).serialize();

		jQuery.ajax({
			type: "POST",
			url: "/restaurants/add-item",		
			data: formData,
			dataType: "html",
			success: function(html){
				jQuery("#" + formID).children(".options").children("#item-quantity").val("1");
				jQuery("#" + formID).children(".options").children("#specialInstructions").val("");
				
				handleOrderResponse(html);	
			},
			error: function(){
				alert("There was an error in your request");
			}
		});
		
		add_triggers.each(function(){
			jQuery(this).overlay().close();
		});
	}
	else{
		alert("You need to have at least 1 item to add it to the cart.");
	}
}



function deleteCartItem(itemNumber){
	
	jQuery('#order-items-container').html("<img id=\"menu-loader\" src=\"/images/ajax-loader-bar.gif\" />");
	
	jQuery.ajax({
		type: "GET",
   		url: "/restaurants/delete-cart-item",		
	    data: ({itemNumber: itemNumber}),
		dataType: "html",
		success: function(html){
			handleOrderResponse(html);	
		},
		error: function(){
			alert("There was an error in your request");
		}
	});
}


function editCartItem(form){

	formData = jQuery("#"+form).serialize();

	jQuery.ajax({
		type: "POST",
   		url: "/restaurants/edit-item",		
	    data: formData,
		dataType: "html",
		success: function(html){
			handleOrderResponse(html);	
		},
		error: function(){
			alert("There was an error in your request");
		}
	});
}



function autoFill(formID){
	
	if( jQuery('#billingcheck').is(':checked') ){

		jQuery('#billingaddress').attr("style", "color: #fff");
		jQuery('#billingaddress2').attr("style", "color: #fff");
		jQuery('#billingcity').attr("style", "color: #fff");
		jQuery('#billingzip').attr("style", "color: #fff");
		
		jQuery('#billingaddress').attr("disabled", true);
		jQuery('#billingaddress2').attr("disabled", true);	
		jQuery('#billingcity').attr("disabled", true);
		jQuery('#billingstate').attr("disabled", true);
		jQuery('#billingzip').attr("disabled", true);	
	}
	else{
		jQuery('#billingaddress').attr("style", "color: #000");
		jQuery('#billingaddress2').attr("style", "color: #000");
		jQuery('#billingcity').attr("style", "color: #000");
		jQuery('#billingzip').attr("style", "color: #000");
	
		jQuery('#billingaddress').attr("disabled", false);
		jQuery('#billingaddress2').attr("disabled", false);	
		jQuery('#billingcity').attr("disabled", false);
		jQuery('#billingstate').attr("disabled", false);
		jQuery('#billingzip').attr("disabled", false);		
		
	}
	
}


function updateRegForm(){

	test = jQuery("#deliverylocation").val();
	
	if( test.match("apartment") ){
		jQuery("#loc-label").show();
		jQuery("#locationname").val("");
		jQuery("#locationname").show();
		
		jQuery("#loc-label").html("Complex  Name");		
		jQuery("#add2-label").html("Apartment #");	
		
	}
	else if( test.match("business") ){
		jQuery("#loc-label").show();
		jQuery("#locationname").val("");
		jQuery("#locationname").show();	
		
		jQuery("#loc-label").html("Business Name");
		jQuery("#add2-label").html("Suite #");			
			
	}
	else if( test.match("hotel") ){
		jQuery("#loc-label").show();
		jQuery("#locationname").val("");
		jQuery("#locationname").show();	
		
		jQuery("#loc-label").html("Hotel Name");
		jQuery("#add2-label").html("Room #");			
	}	
	else{
		jQuery("#loc-label").html("Location Name");
		jQuery("#loc-label").hide();
		jQuery("#locationerrors").hide();
		jQuery("#locationname").hide();
		jQuery("#locationname").val("NA");
		jQuery("#add2-label").html("Address 2");
	}

}
		


function showCreditArea(){

	jQuery("#lock-pane").removeClass('LockOff');
	jQuery("#lock-pane").addClass('LockOn');
	
	formData = jQuery("#checkoutPayment").serialize();

	if(jQuery("input[name=paymentChoice]:checked").val() == "cc"){
		jQuery.ajax({
			type: "POST",
			url: "/checkout/enable-credit-card",		
			data: formData,
			dataType: "html",
			success: function(html){
				document.getElementById("checkout-workspace").innerHTML = html;
				jQuery("#credit-info").show();

				loadButtonOverlay();
				
				jQuery("#lock-pane").removeClass('LockOn');
				jQuery("#lock-pane").addClass('LockOff');		


			},
			error: function(){
				alert("There was an error in your request");
			}
		});
		
	}
	else if(jQuery("input[name=paymentChoice]:checked").val() == "house"){
		jQuery.ajax({
			type: "POST",
			url: "/checkout/house-payment",		
			data: formData,
			dataType: "html",
			success: function(html){
				document.getElementById("checkout-workspace").innerHTML = html;
				jQuery("#credit-info").hide();
				jQuery("#house-info").show();

				loadButtonOverlay();
				
				jQuery("#lock-pane").removeClass('LockOn');
				jQuery("#lock-pane").addClass('LockOff');				
			},
			error: function(){
				alert("There was an error in your request");
			}
		});	
	}
	else{
		jQuery.ajax({
			type: "POST",
			url: "/checkout/disable-credit-card",		
			data: formData,
			dataType: "html",
			success: function(html){
				document.getElementById("checkout-workspace").innerHTML = html;
				jQuery("#credit-info").hide();

				loadButtonOverlay();

				jQuery("#lock-pane").removeClass('LockOn');
				jQuery("#lock-pane").addClass('LockOff');				
			},
			error: function(){
				alert("There was an error in your request");
			}
		});	
		
	}


	
}


function updateTotal(){
	
	var tip = jQuery("#driver-tip #tip").val();
	var total = jQuery("#order-totals #total-price").val();
	
	jQuery("#order-totals #total-price").val( total + tip);
}
