
$(function()
	{ 
		$('.pageHeader').corner("round 15px");
			$('.listMainDiv').corner("round 15px");
			$('.itemHeader').corner("top round 8px");
			$('.prodListItem').corner("round 8px");
			$('.listHeader').corner("round 15px");
		
		$('#btnProceed').click(function()
			{
				$('.validationSummary').html('');
				var gatId =$('#cmbmethod').val();
				if(gatId == '0')
					{
						$('.validationSummary').html('<ul class="validation-summary-errors"><li>You need to select a payment method before proceeding.</li></ul>');
						return false;
					}
				else
					{
						var gatewayName;
						$.getJSON('/Gateway/GatewayName',
									{gatewayId:gatId},
									function(jResult)
										{
											if(jResult.Success == 1)
											{gatewayName=jResult.Message;}
										}
								);
						if((gatewayName=='BankDeposit') && (($('#cmbcountry')==null) || ($('#cmbcountry').val() == '')))
							{
								$('.validationSummary').html('<ul class="validation-summary-errors"><li>You need to select a country where you intend to make your payment.</li></ul>');
								return false;
							}
							else
								return true;
					}
			}
		);
		
		$('.listAddToCart').unbind('click');//had to add unbind because click was being bound twice
		$('.listAddToCart').click(function()
			{
				var prodId =$(this).attr('productid');
				
				var quantityVal = $('#txtQuantity').val();
                if(quantityVal==null)
					quantityVal = $(this).attr('quantity');
				if(quantityVal==null)
					quantityVal = 1;
					
				var iType = $(this).attr('itemtype');
				if(iType==null)
					iType=1;
					
				$('.section_cartsummary').load('/Cart/AddToCart',
					{itemId:prodId,itemType:iType,quantity:quantityVal},function()
						{
							alert('Item has been added to cart!');
						}
					);
					
			}
			);	
			
		
		
		//Add subscription to cart
		//$('.defaultSubscriptionSubDetailsCostBuy').unbind('click');
		//$('.defaultSubscriptionSubDetailsCostBuy').click(function()
		//	{
		//		var prodId = $(this).attr('id');
				//$('.cartAreaSpacer').hide();
		//		$('section_cartsummary').load('/Cart/AddToCart',
		//			{itemId:prodId,itemType:2, quantity:1  },function()
		//				{
		//					alert('Item has been added to cart!');
		//				}
		//		);
		//	}
		//);
		
		$('.thumbNail').click(function()
			{
				//var obj =this;
				var id= $(this).attr('id');
				LoadResource(id);
			}
		);
			
		$('.updateItemQuantityImg').click(function()
			{
				//var obj =this;
				
				var pidString=$(this).attr('id');
				var itemId=pidString.substring( pidString.lastIndexOf('_') + 1);
				var newQuantity =$('#item_' + itemId).val();
				
				if(newQuantity == '')
					{alert('Quantity can not be empty.');}
					else
				{
					$.getJSON('/Cart/UpdateItemQuantity',{itemId:itemId,newQuantity:newQuantity},
								function(jResult)
									{
										if(jResult.Success==1)
											{
												window.location.reload();
											}
										else
											{
												alert(jResult.Message);
											}
									}
							);				
				}
			}
		);
		
		$('.removeCartItemImg').click(function()
			{
				//var obj =this;
				var delString=$(this).attr('id');
				var itemId=delString.substring( delString.lastIndexOf('_') + 1);
				$.getJSON('/Cart/RemoveItemFromCart',{itemId:itemId,itemType:1},
							function(jResult)
								{
									if(jResult.Success==1)
										{
											window.location.reload();
										}
									else
										{
											alert(jResult.Message);
										}
								}
							);				
			}
		);
		
		$('.removeVoucherFromCart').click(function()
			{
				var cId = $(this).attr('cartid');
				$.getJSON('/Voucher/AjaxRemove',{cartId:cId},
							function(jResult)
								{
									if(jResult.Success==1)
										{
											window.location.reload();
										}
									else
										{
											alert(jResult.Message);
										}
								}
						)
				
			}
		);
	}
)
function LoadResource(resourceId)
    {
		$.getJSON('/Products/LoadResource',
			{resId:resourceId },
			function(jResult)
				{
					$('#productMain').html(jResult.Message);
				}
			);
	}
	
function AjaxAddToCart(prodId)
    {
		var quantityVal = $('#quantity').val();
		AddToCart(prodId,quantityVal);
	}
function AddToCart(prodId,quantityVal)
{
	//$('.cartAreaSpacer').hide();
	$('.section_cartsummary').load('/Cart/AddToCart',
					{itemId:prodId,itemType:1, quantity:quantityVal  });
}


