// JavaScript Document
var CartUI = {
	shippingCharge: 12,
	serviceUrl: '',
	
	createOrder: function()
	{
		$.post(CartUI.serviceUrl + '/createOrder', 
			   	$('#frmItems').fastSerialize(),
				function(response)
				{
					if(response.succeed)
					{
						$('#frmPaypal')[0].submit();
					}
					else
					{
						util.unblock('#catalogue');
						alert(response.message);
					}
				},'json');
	},
	
	
	calculateUnitPrice: function(qty, price)
	{
		var groups = Math.floor( qty / 3);
		
		var reminder = qty - groups;
		
		var total = (reminder * price) + (groups * (price / 2));
		
		return total / qty;
	},
	
	submitToPaypal: function()
	{
		$('#items-container').html('');
		var html = '';
		var index = 1;
		var total = 0;
		
		var totalQuantity = 0;
		
		$('.qty').each(function()
								{
									totalQuantity += parseInt($(this).val(),10);
								});
		
		/*
		$('.product-id').each(function()
	    {
			var id = parseInt($(this).val(),10);
			totalQuantity += parseInt($('#ddlQuantity_' + id).val(),10);
		});
		*/
		
		if(totalQuantity == 0)
		{
			alert('Please select a quantity for at least one item');
			return false;
		}
		
		var auto_id = 0;
		
		var shipping = CartUI.shippingCharge;
		
		var total_qt = 0;
		
		$('.product-id').each(function()
	    {
			var id = parseInt($(this).val(),10);
			
			//alert($('#product_'+id+' .qty').length);
			
			$('#product_'+id+' .qty').each(function(){//each item
														var qt = parseInt($(this).val(),10);
														var p = parseFloat( $('#hdnPrice_' + id).val());
														var n = $('#hdnName_' + id).val();
														
														var t = $(this).attr('id');
														t = t.replace("ddlQuantity_"+id+"_","");
														t = t.replace("-"," ");
														
														var unitPrice = CartUI.calculateUnitPrice(qt, p);
														
														var amount = parseFloat(qt * unitPrice);														
														
														if(qt > 0){
															auto_id++;
															
															html += '<input type="hidden" name="item_name_' + auto_id + '" value="' + n + ' (' + t + ')"/>';
															html += '<input type="hidden" name="amount_' + auto_id + '" value="' + unitPrice.toFixed(2) + '"/>';
															html += '<input type="hidden" name="quantity_' + auto_id + '" value="' + qt + '"/>';
															
															total_qt += qt;
															
															if(id == 1)
															{
																if(total_qt > 10)
																{
																	shipping = (CartUI.shippingCharge * 2);
																}
																else
																{
																	shipping = CartUI.shippingCharge;
																}
															}
															else
															{
																if(total_qt > 19)
																{
																	shipping = (CartUI.shippingCharge * 2);
																}
																else
																{
																	shipping = CartUI.shippingCharge;
																}
															}
															
															//html += '<input type="hidden" name="shipping_' + auto_id + '" value="' + shipping.toFixed(2) + '"/>';
															//Fix shipping charge two issue: **20Sep
															
														
															total += amount;														
														}
													});
			
				
		
		/*
			var name = $('#hdnName_' + id).val();
			var quantity = parseInt( $('#ddlQuantity_' + id).val(), 10);
			
			var taste = $('#ddlTaste_' + id).val();
			var price = parseFloat( $('#hdnPrice_' + id).val());
			
			var unitPrice = CartUI.calculateUnitPrice(quantity, price);
			
			var amount = parseFloat(quantity * unitPrice);
			
			if(quantity > 0)
			{
				auto_id++;
				
				html += '<input type="hidden" name="item_name_' + auto_id + '" value="' + name + ' (' + taste + ')"/>';
				html += '<input type="hidden" name="amount_' + auto_id + '" value="' + unitPrice.toFixed(2) + '"/>';
				html += '<input type="hidden" name="quantity_' + auto_id + '" value="' + quantity + '"/>';
				
				var shipping = CartUI.shippingCharge;
				if(id == 1)
				{
					if(quantity > 10)
					{
						shipping = (CartUI.shippingCharge * 2);
					}
					else
					{
						shipping = CartUI.shippingCharge;
					}
				}
				else
				{
					if(quantity > 19)
					{
						shipping = (CartUI.shippingCharge * 2);
					}
					else
					{
						shipping = CartUI.shippingCharge;
					}
				}
				
				html += '<input type="hidden" name="shipping_' + auto_id + '" value="' + shipping.toFixed(2) + '"/>';
			
				total += amount;
			}
			*/
			
			index++;
	    });
		
		html += '<input type="hidden" name="shipping_1" value="' + shipping.toFixed(2) + '"/>';	
		
		//alert(html);
		//return;
		
		html += '<input type="hidden" name="amount" value="' + total.toFixed(2) + '"/>';
		$('#items-container').append(html);
		
		util.block('#catalogue');
		
		CartUI.createOrder();
		
		return false;
	},
	
	init: function(serviceUrl, shippingCharge)
	{
		CartUI.shippingCharge = shippingCharge;
		CartUI.serviceUrl = serviceUrl;
		$('#payment').bind('click', CartUI.submitToPaypal);
	}
};
