jQuery.cart = {
	buy_now: function(product_id,id) {
		var qty = ($('#qty_' + id).val());
		var price = ($('#price_' + id).html());
		var d = new Date();
		// alert('Product_id = ' + product_id + ' And qty = ' + qty + ' price = ' + price + ' id ' + id ) ;
	 	 jQuery.get("get_cart.xhtml&action=buy_now&prod_id="+product_id+"&qty=" + qty +"&price=" + price + "&rand="+d.getTime(), { },
				  function(data){
				//	alert(data);
				  var res =  data.replace(/^\s+|\s+$/g,"");				
				  if(res == 'LOGIN'){
					  alert('You are required to login in order to edit or buy Sugarless Co products');
					  window.location = 'login.htm';
				  }else
				     jQuery('#in_cart').html(data);					
			});			
	},
//nearly does the same job as buy_now
	add_cart: function(product_id,original_qty,el_id) {
		var qty;		
		var new_qty = parseInt((jQuery('#' + el_id).val()));
		var og_qty = parseInt(original_qty);
		//alert(og_qty + ' ' + new_qty);
		//User doesn't want to increase by 1
		if(og_qty < new_qty)
		{ 	qty = new_qty - og_qty;}				
		 else
		 {qty = 1;}
			//alert(qty);
		jQuery.get("get_cart.xhtml&action=add&prod_id=" + products_id + "&qty = " + qty, { },
				  function(data){
					 var res =  data.replace(/^\s+|\s+$/g,"");				
					  if(res == 'LOGIN'){
						  alert('You are required to login in order to edit or buy Sugarless Co products');
						  window.location = 'login.htm';
					  }else{
						  jQuery('#in_cart').html(data);
		              }	
		 });		       
	},
	 remove_item: function(product_id,original_qty,el_id) {
		var qty = 0;
		var new_qty = parseInt((jQuery('#' + el_id).val()));
		var og_qty = parseInt(original_qty);
		//alert(og_qty + ' ' + new_qty);
		//User doesn't want to increase by 1
		if(og_qty < new_qty)
		{ 	
			return false; //follow the rules - as decrease and + as increase
		}		
		else if(new_qty < og_qty)
		{
			qty =  new_qty - og_qty;			
		}
		else
		{
			 qty = -1;
		}		
		 if(og_qty + qty ==0)
		 {
			var goahed = confirm('Are you sure you want to delete this product from your cart?');
			if(!goahed)						
			return false;
		 }
		
		jQuery.get("get_cart.xhtml&action=update_product&prod_id=" + product_id + "&qty="+ quantity , { },
				  function(data){		
					   var res =  data.replace(/^\s+|\s+$/g,"");				
						  if(res == 'LOGIN'){
							  alert('You are required to login in order to edit or buy Sugarless Co products');
							  window.location = 'login.htm';
						  }else{
							jQuery('#in_cart').html(data);
						  }
								
		});			
	},
	delete_cart: function() {

		var goahed = confirm('Are you sure you want to empty your cart?');
                if(!goahed)   return false;

		jQuery.get("get_cart.xhtml&action=remove_item", { },
				  function(data){		
				 // alert(data);
 	  			 jQuery('#in_cart').html(data);	
			});			
	},
  	get_single_product: function(prod_id){
                jQuery('#products_list').html( 'loading....');
                jQuery.get("catalogue.xhtml&prod_id=" + prod_id, { },
                                  function(data){
                                  //alert(data);
                                        jQuery('#products_list').html( data);
                                  });
        },

        edit_cart: function(){
                jQuery('#products_list').html( 'loading....');
                jQuery.get("catalogue.xhtml&edit_cart=edit_cart", { },
                                  function(data){
                                  //alert(data);
                                        jQuery('#cart_edit').html( data);
                                  });
        }


};