function load_shopping_cart($cart_template)
{
	if(undefined == $cart_template){
		$.ajax({
			type: "POST",
			url: '/cart/check_checkout_style',
			success: function(result) {
				load_cart_style(result);
	        }
	    });
	} else {
		load_cart_style($cart_template);
	}
}

function load_cart_style($cart_template){
	if($cart_template == 'simple')
	{
		window.location = '/cart/';
	} else {
		$("#remove_coupon").hide();
		$('#shopping_cart').jqm({
			overlay: 80,
			modal: true,
			ajax: '/cart',
			trigger: true,
			onLoad:check_window_size
		});
	
		$('#shopping_cart').jqmShow();
	}
}

var check_window_size=function(hash){
	hash.w.show();

	var window_width = 0, window_height = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		window_width 	= window.innerWidth;
		window_height 	= window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		window_width 	= document.documentElement.clientWidth;
		window_height 	= document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		window_width 	= document.body.clientWidth;
		window_height 	= document.body.clientHeight;
	}

	if(window_height < 600){
		$('.jqmWindow').css("top", '0');
		$('.jqmWindow').css("height", '400px');
		$('#items').css("height", '155px');
	}
	if(window_width < 800){
		$('.jqmWindow').css("width", '700px');
		$('#checkout_bottom').css("width", '700px');
		$('#shopping_cart').css("width", '700px');
		$('#items').css("width", '700px');
	}
};

function close_checkout()
{
	window.location = '/products/illustrations';
}

function add_item($product_id)
{
	$.ajax({
		type: "GET",
		url: "/cart/add_item/"+$product_id,
		dataType: "xml",
		success: function(xml) {
			$(xml).find('shopping_cart').each(function(){
				var qty = $(this).find('quantity').text();
				$("#cart_quantity_display").html(qty);

				var subtotal = $(this).find('subtotal').text();
				$("#cart_subtotal").html(subtotal);

				var tax = $(this).find('tax').text();
				$("#tax").html(tax);

				var total = $(this).find('total').text();
				$("#cart_total").html(total);

				var item_qty = $(this).find('item').text();
				$("#"+$product_id+"_quantity").html(item_qty);

				$("#checkout_message_area").html("Item Added");
			})
		}
	})
}

function remove_item($product_id,$quantity)
{
	$.ajax({
		type: "GET",
		url: "/cart/remove_item/"+$product_id+"/"+$quantity,
		dataType: "xml",
		success: function(xml) {
			$(xml).find('shopping_cart').each(function(){
				var qty = $(this).find('quantity').text();
				$("#cart_quantity_display").html(qty);

				var subtotal = $(this).find('subtotal').text();
				$("#cart_subtotal").html(subtotal);

				var tax = $(this).find('tax').text();
				$("#tax").html(tax);

				var total = $(this).find('total').text();
				$("#cart_total").html(total);

				var item_qty = $(this).find('item').text();
				$("#"+$product_id+"_quantity").html(item_qty);
				if(item_qty == 0)
				{
					$("#cart_item_"+$product_id).empty();
					$("#cart_item_"+$product_id).hide("slow");
				}
				$("#checkout_message_area").html("Item Removed");
			})
		}
	});
}

function empty_cart($user_id)
{
	$.ajax({
		type: "GET",
		url: "/cart/empty_cart/"+$user_id,
		success: function(data) {
			$(data).find('shopping_cart').each(function(){
				var qty = $(this).find('quantity').text();
				$("#cart_quantity_display").html(qty);
				$("#items").empty();
				$("#cart_subtotal").html('$0.00');
				$("#cart_total").html('$0.00');
				$("#checkout_message_area").html("Your Cart Is Now Empty");
			})
		}
	})
}

function apply_discount()
{
	$discount = $("#discount_code").val();
	$.ajax({
		type: "GET",
		url: "/cart/apply_discount/"+$discount,
		dataType: "xml",
		success: function(xml)
		{
			$(xml).find('shopping_cart').each(function(){
				var status = $(this).find('status').text();
				if(status == 1)
				{
					var total = $(this).find('total').text();
					$("#cart_total").html(total);

					$("#discount_amount").html($(this).find('discount_amount').text());

					$("#coupon_code").html($discount);

					$("#enter_coupon").hide();
					$("#remove_coupon").fadeIn("slow");
					$("#discount_amount").fadeIn("slow");
				}
				var message = $(this).find('message').text();
				$("#checkout_message_area").html(message);
			})
		}
	})
}

/**
* Removes the discount form a shopping cart
* param $user_id integer
 **/
function remove_discount($user_id)
{
	$.ajax({
		type: "GET",
		url: "/cart/remove_discount/"+$user_id,
		dataType: "xml",
		success: function(xml)
		{
			$(xml).find('shopping_cart').each(function(){
				var subtotal = $(this).find('subtotal').text();
				$("#cart_subtotal").html(subtotal);

				var total = $(this).find('total').text();
				$("#cart_total").html(total);

				$("#remove_coupon").hide();
				$("#discount_amount").hide();
				$("#enter_coupon").fadeIn("slow");

				$("#checkout_message_area").html("Coupon Removed");
			})
		}
	})
}

function checkout()
{
	window.location = '/checkout';
}

/**
* This funciton can be deleted
**/
function load_checkout()
{
	$('#checkout').jqm({
		overlay: 80,
		modal: true,
		ajax: '/checkout/form',
		trigger: true
	});
	$('#checkout').jqmShow();
}

function apply_spice_credit()
{
	$.ajax({
		type: "GET",
		url: "/checkout/apply_credit/",
		dataType: "xml",
		success: function(xml)
		{
			$(xml).find('shopping_cart').each(function(){
				var total = $(this).find('total').text();
				$("#total").html(total);

				var total_plain = $(this).find('total_plain').text();
				$("#total_plain").html(total_plain);

				$("#apply_spice_credit").hide();
				$("#remove_spice_credit").fadeIn("slow");
				
				$("#checkout_message_area").show();
				$("#checkout_message_area").html("Spice Credit Has Been Applied");
			})
		}
	})
}

function remove_spice_credit()
{
	$.ajax({
		type: "GET",
		url: "/checkout/remove_credit/",
		dataType: "xml",
		success: function(xml)
		{
			$(xml).find('shopping_cart').each(function(){
				var total = $(this).find('total').text();
				$("#total").html(total);

				var total_plain = $(this).find('total_plain').text();
				$("#total_plain").html(total_plain);

				$("#remove_spice_credit").hide();
				$("#apply_spice_credit").fadeIn("slow");
				
				$("#checkout_message_area").show();
				$("#checkout_message_area").html("Spice Credit Has Been Removed");
			})
		}
	});
}

function add_address()
{
	window.location = '/profile/edit_contact';
}

function update_address($address_id)
{
	$("#loading_address_"+$address_id).show();
	var user_id = $("#user_id_"+$address_id).val();
	var address_id = $("#address_id_"+$address_id).val();
	var address = $("#address_"+$address_id).val();
	var city = $("#city_"+$address_id).val();
	var state = $("#state_"+$address_id).val();
	var zip = $("#zip_"+$address_id).val();
	var country = $("#country_"+$address_id).val();

	$.post("/user/update_address/"+$address_id,{user_id: user_id,id: address_id,address: address, city: city, state_id: state, zip: zip, country_id: country,address_type: 'billing'},
		function() {
			$("#checkout_message_area").show();
			$("#checkout_message_area").html("Address Has Been Saved");
			$("#loading_address_"+$address_id).hide();
		});
}

function finish($user_id)
{
	$("#checkout_message_area").hide();
	$("#loading_payment").show();
	var cc_name = $("#cc_name").val();
	var cc_number = $("#cc_number").val();
	var cc_cvv = $("#cc_cvv").val();
	var cc_month = $("#cc_expiration_month").val();
	var cc_year = $("#cc_expiration_year").val();
	var address_id = $("input[@name='address_use']:checked").val();
	var total_plain = $("#total_plain").html();

	var process = false;

	if(total_plain > 0)
	{
		if(!address_id) {
			set_message("Please Select An Address");
			return false;
		}
		if(!cc_name) {
			set_message("Please Enter The Name on Your Credit Card");
			return false;
		}
		if(!cc_number) {
			set_message("Please Enter Your Credit Card Number");
			return false;
		}
		var process = true;
	} else {
		var process = true;
	}

	if(process == true)
	{
		$.ajax({
			type: "POST",
			url: "/checkout/purchase/"+$user_id,
			data: "cc_name="+cc_name+"&cc_number="+cc_number+"&cc_cvv="+cc_cvv+"&cc_expiration[month]="+cc_month+"&cc_expiration[year]="+cc_year+"&address_id="+address_id,
			dataType: "xml",
			success: function(xml)
			{
				$(xml).find('shopping_cart').each(function(){
					var status = $(this).find('status').text();
					var order_id = $(this).find('order_id').text();
					var error_code = $(this).find('error_code').text();
					var message = $(this).find('message').text();
					if(status == "ACCEPT")
					{
						window.location = '/account/purchase/'+order_id;
					}
					if(status == "REJECT")
					{
						$("#checkout_message_area").show();
						$("#checkout_message_area").html(message);
					}
					$("#loading_payment").hide();
				})
			}
		});
	} else {
		set_message("There is a problem with the information you have entered");
		return false;
	}
}

function set_message($message)
{
	$("#checkout_message_area").show();
	$("#checkout_message_area").html($message);
	$("#loading_payment").hide();
}