var g_cart_option = "";
var g_addToCartCustomMessage_agreed = false;


function addToCartCustomMessage_updateSameSchool(asyncOption)
{
	var inputData = {};
	inputData["entryFieldValue01"] = g_entryFieldValue01;
	$.ajax({
		type: "POST",
		async: asyncOption,
		data: inputData,
	    url: "ajax_custom_action_01.jsp",
	    success: function(data)
	    {
	    	// do nothing
	    }
	});
}

function addToCartCustomMessage_closeCart(asyncOption)
{
	var inputData = {};
	inputData["value"] = "true";
	$.ajax({
		type: "POST",
		async: asyncOption,
		data: inputData,
	    url: "ajax_custom_action_03.jsp",
	    success: function(data)
	    {
	    	// do nothing
	    }
	});
}
function addToCartCustomMessage()
{
	
	var inputData = {};
	$.ajax({
		type: "POST",
		data: inputData,
		dataType: 'json',
		async: false,
	    url: "ajax_custom_action_02.jsp",
	    success: function(data)
	    {
	    	var mySchoolName = "";
	    	var myCartSize = 0;
	    	
	    	$.each(data, function(key, val) {
	    		 if (key=="SCHOOL_NAME")
				 {
	    			mySchoolName = val;
				 }
	    		 else if (key=="CART_SIZE")
				 {
	    			 myCartSize = parseInt(val);
				 }
	    	 });
	    	
	    	if (myCartSize>1)
	    	{
	    		if (mySchoolName =="-FALSE-")
	    		{
	    			g_cart_option = "MULTIPLE_SCHOOL";
	    		}
	    		else if (mySchoolName.length>0)
	    		{
	    			g_cart_option = "SINGLE_SCHOOL";
	    		}
	    		else
	    		{
	    			g_cart_option = "MULTIPLE_SCHOOL";
	    		}
	    	}
	    	else
	    	{
	    		g_cart_option = "UNDECIDED";
	    	}
	    }
	});
	
	var dialogTitle = "";
	var divMessage = "";
	
	var dialog_buttons = {};
	
	if (g_cart_option=="UNDECIDED")
	{
		dialogTitle = "Item added to cart";
		
		divMessage = "<div>" + "<ul>" +
				"<li>Continue shopping for the same school." + 
				"<li>Continue shopping for different schools, limited to one product." + 
				"<li>Proceed to checkout." + 
		   "</ul>" +
		   "<p><i>Please note: your order will use one shipping address.</i></p>"+ "</div>";
		
		dialog_buttons["Same School"] = function(){
			$(".js_customEntryField01").val(g_entryFieldValue01);
			$(".js_customEntryField01").attr("disabled", "disabled");
			
			addToCartCustomMessage_updateSameSchool(false);
			
			g_addToCartCustomMessage_agreed = true;
	        $(this).dialog("close");
		}
		
		dialog_buttons["Different School"] = function(){
			location.reload();
			g_addToCartCustomMessage_agreed = true;
	        $(this).dialog("close");
		}
		
		dialog_buttons["Proceed to Checkout"] = function(){
			addToCartCustomMessage_closeCart(false);
			$(location).attr('href',"checkout");
			g_addToCartCustomMessage_agreed = true;
	        $(this).dialog("close");
		}
	}
//	else if (g_cart_option == "SINGLE_SCHOOL")
//	{
//		dialogTitle = "(Single School Option) Item added to cart";
//		
//		divMessage = "<div>" + "<ul><li>Continue shopping for the same school." + 
//		   "<li>Clear Cart and Start Over" +
//		   "</ul>" +
//		   "<p><i>Please note: your order will use one shipping address.</i></p>"+ "</div>";
//		
//		dialog_buttons["Continue Shopping"] = function(){
//			g_addToCartCustomMessage_agreed = true;
//	        $(this).dialog("close");
//		}
//	}
//	else if (g_cart_option == "MULTIPLE_SCHOOL")
//	{
//		dialogTitle = "(Multiple Schools, One Product Option) Item added to cart";
//		divMessage = "<div>" + "<ul><li>Continue shopping for multiple schools, limited to one product." +
//		   "<li>Clear Cart and Start Over" +
//		   "</ul>" +
//		   "<p><i>Please note: your order will use one shipping address.</i></p>"+ "</div>";
//		
//		dialog_buttons["Continue Shopping"] = function(){
//			g_addToCartCustomMessage_agreed = true;
//	        $(this).dialog("close");
//		}
//	}
	
	
	if (g_cart_option == "SINGLE_SCHOOL" || g_cart_option == "MULTIPLE_SCHOOL")
	{
		dialog_buttons["Clear Cart and Start Over"] = function(){
			
			$.ajax({
				cache: false, 
			    url: "ajax_empty_cart.jsp",
			    success: function(data)
			    {
			    	updateCartLabel(0);
			    }
			});
			

			$(".js_customEntryField01").val("");
			$(".js_customEntryField02").val("");

			$(".js_customEntryField01").removeAttr("disabled").removeAttr("readonly");
			$(".js_customEntryField02").removeAttr("disabled").removeAttr("readonly");
			
			g_addToCartCustomMessage_agreed = true;
	        $(this).dialog("close");
		}
		
		dialog_buttons["Proceed to checkout"] = function(){
			
			var link = "checkout";
			
	        if (link.length>0)
	    	{
	        	if (parent.document)
	        	{
	        		$(parent.document.location).attr('href', link);
	        	}
	        	else
	        	{
	        		$(location).attr('href', link);
	        	}
	    	}
	        
	        g_addToCartCustomMessage_agreed = true;
	        $(this).dialog("close");
		}
	}
	
	
	
	if (g_cart_option=="UNDECIDED")
	{
		
		$(divMessage).dialog({
		    title : dialogTitle,
		    autoOpen : true,
		    bgiframe : true,
		    modal : true,
		    closeOnEscape :  false,
		    width : 500,
		    resizable : false,
		    buttons : dialog_buttons,
		    beforeclose : function() {
				return g_addToCartCustomMessage_agreed; 
			}
		});
	}
}

$(document).ready(function() {
	
	$('a').each(function() {
		   var a = new RegExp('/' + window.location.host + '/');
		   if(!a.test(this.href)) {
		       $(this).click(function(event) {
		           event.preventDefault();
		           event.stopPropagation();
		           window.open(this.href, '_blank');
		       });
		   }
		});
	
	
});

