var baseURL = "/ajax/sumoshop/";

function init() {
	$('#jqFooterView').hide();
}

function checkLogin(username,password,url)
{
	$('.loginindicator').css("display","inline");
	
	$.post(baseURL+"login.cfm",
		{uid:username,pwd:password},
		function(data)
		{
			if(data==1)
			{
				if(arguments.length != 3)
					window.location.reload();
				else
					window.location.href = url;	
			}
			else
			{
				$('.loginindicator').css("display","none");
				$('.loginmsg').html('Failed login, please try again.');
				$('.loginmsg').css("display","block");
			}	
		
		});
}

function logout()
{
	$.post(baseURL+"logout.cfm",
		function(data)
		{
			alert('Logout successful');
			window.location.reload();
			
		});
}

function updateCartTotal(state)
{
	$.getScript(baseURL+"updateCheckoutTotal.cfm?state="+state,
		function(data)
		{
			
		});
}

function removeFromCart(itemid,remove,bReload)
{
	if(arguments.length < 3)
		bReload = 1;
	
	if($("." + remove).is(":checked"))
	{
	
		if(confirm("Remove this item from your cart?"))
		{
			$.getScript(baseURL+"removeFromCart.cfm?itemid="+itemid,
			function(data)
			{
				$('#'+itemid).remove();
				$('.cartindicator').css('display','none');
				$('#addtocart').css("display","block");
				$('#removefromcart_container').css("display","none");
			});
		}
	}		
}

function buildAttributePairs(aids)
{
	var l = "";
	var a = aids.split(',');
	for(var i = 0;i < a.length;i++)
	{
		l += a[i] + "=" + $('#'+a[i]).val();
		if(i!=a.length)
			l+=":";
	}
	return(l);
}

function updateViewCartQuantity(id, quantity, price)
{	
	//$('#itemtotal_'+id).load(baseURL + "updateCartTotal.cfm", 
	$.get(baseURL + "updateCartTotal.cfm",
	{quantity:quantity, itemid:id, price:price},
	
	function(data)
	{
		var sData = data.split("|");
		$('#jqFooterQuantity').html(sData[1]);
		$('.jqFooterSubTotal').html(sData[2]);
		$(sData[4]).html(sData[3]);
	});
}

function requestSample(pid,catid)
{
	$('.requestindicator').css('display','block');
	$.get(baseURL+"samplerequest.cfm",
	{productid:pid,categoryid:catid},
	
	function(data)
	{
		//hide add to cart, show remove from cart
		$('.requestindicator').css('display','none');
		$('#request_container').text("Request received");
		//$('#addtocart').css("display","none");
		//$('#removefromcart_container').css("display","block");
		
	});

}

/*
function addToCart(quantity, itemid, itemPrice, size, print)
{
	//var aIdValuePairs = buildAttributePairs(aids);
	
	var price = itemPrice;
	
	//var quantity = $('#quantity_'+itemid).val();
	//var quantity = 1; //yes this is hardcoded for now
	
	$('.cartindicator').css('display','block');
	
	$.get(baseURL + "addToCart.cfm",
	
	{itemid:itemid,quantity:quantity,price:price,size:size,print:print},
	
	function(data)
	{
		var sData = data.split("|");
		
		$('.addtocart').css('display','none');
		$('#removefromcart_container').css("display","block");
		$('#cartcount').text(data);
		
		$('#jqFooterView').show('slow');
		$('#jqFooterQuantity').html(sData[0]);
		$('.jqFooterSubTotal').html(sData[1]);
		
	});
}
*/

function addToCart(quantity, itemid, itemPrice, bOnSale, previousPrice, size, print)
{
	//var aIdValuePairs = buildAttributePairs(aids);
	
	var price = itemPrice;
	
	//var quantity = $('#quantity_'+itemid).val();
	//var quantity = 1; //yes this is hardcoded for now
	
	$('.cartindicator').css('display','block');
	
	$.get(baseURL + "addToCart.cfm",
	
	{itemid:itemid,quantity:quantity,price:price,bOnSale:bOnSale,previousPrice:previousPrice,size:size,print:print},
	
	function(data)
	{
		var sData = data.split("|");
		
		$('.addtocart').css('display','none');
		$('#removefromcart_container').css("display","block");
		$('#cartcount').text(data);
		
		$('#jqFooterView').show('slow');
		$('#jqFooterQuantity').html(sData[0]);
		$('.jqFooterSubTotal').html(sData[1]);
		
	});
}

function updateGiftWrap(giftValue)
{
	
	if (!giftValue) {
		var giftValue = 0;
	}
	
	$.get(
		baseURL + "updateGiftVoucher.cfm?giftValue=" + giftValue,
		
		function(data) {
			//var sData = data.split(",");
			var total = "";
			$.each(data, function(key, val)
			{
				total += val;
			});
			$(".jqFooterSubTotal").html(total);
		}
		
	);
}

function checkPromoCode(voucherCode, purchaseTotal, alertStr)
{
	$.get(
			baseURL + "calcPromoCode.cfm?voucherCode=" + voucherCode + "&purchaseTotal=" + purchaseTotal,
			
			function(vdata) 
			{
				var svdata = vdata.split(",");
				
				if ($.trim(svdata[0]) == 1) 
				{
					$("#vouchRemaining").css("display", "none");
					$("#cfVouchRemaining").html("");
					$("#vouchRemaining").html("Your promotional voucher has been applied");
					$("#vouchRemaining").fadeIn("slow");
					$("#totalamount").html(svdata[1]);
					$("#promotionalDiscount").val($.trim(svdata[2]));
					$("#nab").append($("#promotionalDiscount"));
					$("#postage").val($.trim(svdata[3]));
				}
				else
				{
					$("#vouchRemaining").hide("");
					alert(alertStr);
				}
			}
		);
}

function updateVoucher(voucherCode, purchaseTotal, paymentPage)
{
	
	var paymentPage = paymentPage || 0;
	
	$.get(
		baseURL + "calcGiftVoucher.cfm?voucherCode=" + voucherCode + "&purchaseTotal=" + purchaseTotal + "&paymentPage=" + paymentPage,
		
		function(data) {
			var sData = data.split(",");
			
			if (sData[0] == 0) 
			{
				checkPromoCode(voucherCode, purchaseTotal, sData[1]);
			} 
			else 
			{
				$("#vouchRemaining").css("display", "none");
				$("#giftVoucherField").fadeOut("slow");
				$("#cfVouchRemaining").html("");
				$("#vouchRemaining").html("You have " + sData[2] + " remaining on your gift voucher.<br />After this order is verified you will have " + sData[3] + " left." );
				$("#vouchRemaining").fadeIn("slow");
				
				// final payment page actions
				if (paymentPage == 1) {
					$("#totalamount").html(sData[4]);
					$("#jqVoucherGST").html("<input type='hidden' name='Gift Voucher Discount' value='" + sData[6] + "'>");
				}
				
				// if we are paying by the entire cost by the gift voucher
				if (sData[7] == 1) {
					$("#jqNonVoucher").css("display", "none");
					$("#jqVoucherFullAmount").html("<div class='typefloat base'><input type='radio' name='paymentMethod' id='paymentMethod5' class='radio' value='Voucher - Full Amount' checked /> Voucher - Full Amount</div>");
				}
				
			}
		}
		
	);
}

