function update_quantity(this_element){

				var product_id = this_element.parent().siblings().eq(1).find('input[name="products_id[]"]').eq(0).val();
				
				var this_element = this_element;
				
				var lbl_unit_price = this_element.parent().parent().find('td[class="td4"]').eq(0).find('label');
				
				var lbl_total_price = this_element.parent().parent().find('td[class="td5"]').eq(0).find('label');	
				
				var lbl_sub_total = $j('#lbl_sub_total');
				
				$j.ajax({
				
						url: 'update_shopping_cart.php?random=' + Math.random(),
						
						type: 'POST',
						
						dataType: 'json',
						
						data: ({products_id : product_id , input_qty : this_element.val() , action : 'update_quantity'}),
						
						beforeSend : function(){
												
							lbl_unit_price.empty();
							
							lbl_total_price.empty();
							
							lbl_sub_total.animate({"opacity": 0}, "fast");
							
							lbl_unit_price.html('<img src="images/indicators/loading-grey.gif">');
							
							lbl_total_price.html('<img src="images/indicators/loading-grey.gif">');
						
						},
						
						error: function(error,result){
						
							//alert(result);
						
						},
						
						success: function(result){
						
							//alert(result);
						
							//Set Unit Price HTML
						
							lbl_unit_price.empty();
							
							lbl_unit_price.html(result.unit_price);
					
							lbl_unit_price.animate({"opacity": 1}, "slow");
							
							//Set Total Price HTML
							
							lbl_total_price.html(result.subtotal);
					
							lbl_total_price.animate({"opacity": 1}, "slow");
							
							//Set Sub-Total
							
							
							lbl_sub_total.html(result.sub_total);
							
							lbl_sub_total.animate({"opacity": 1}, "slow");
						
						}
				
				});

}

function update_shipping_cost(){

				var shipping = $j('#shipping_method');
				
				var lbl_shipping_cost = $j('#lbl_shipping_cost');
				
				$j.ajax({
				
						url: 'update_shopping_cart.php?random=' + Math.random(),
						
						type: 'POST',
						
						dataType: 'json',
						
						data: ({shipping : shipping.val() , action : 'update_shipping'}),
						
						beforeSend : function() {
																		
							lbl_shipping_cost.animate({"opacity": 0}, "slow");
							
							lbl_shipping_cost.empty();
							
						},
						
						error: function(error,result) {
						
							//alert(result);
						
						},
						
						success: function(result) {
								
							//alert(result);
	
							//Set Unit Price HTML
						
							lbl_shipping_cost.empty();
							
							lbl_shipping_cost.html(result.shipping_cost);
					
							lbl_shipping_cost.animate({"opacity": 1}, "slow");
							
						}
				
				});				
				
}

function update_total() {

	var lbl_total = $j('#lbl_total');
	
	var shipping = $j('#shipping_method');
	
	var lbl_discount = $j('#lbl_discount');
	
				$j.ajax({
				
						url: 'update_shopping_cart.php?random=' + Math.random(),
						
						type: 'POST',
						
						dataType: 'json',
						
						data: ({shipping : shipping.val(), action : 'update_total_price'}),
						
						beforeSend : function() {
																			
							lbl_total.animate({"opacity": 0}, "slow");
							lbl_discount.animate({"opacity": 0}, "fast");
							lbl_total.empty();
							
						},
						
						error: function(error,result) {
						
							//alert(result);
						
						},
						
						success: function(result) {

							//alert(result);
	
							//Set Unit Price HTML
						
							lbl_total.empty();
							lbl_discount.html(result.discount);
							lbl_discount.animate({"opacity": 1}, "slow");
							lbl_total.html(result.total_price);
							lbl_total.animate({"opacity": 1}, "slow");
							
						}
				
				});	

}
function updateModule(){
	jQuery.ajax({
   type: "POST",
  url: 'update_shopping_cart.php?random=' + Math.random(),
   data:  ({action : 'update_module'}),
  dataType: 'json',
   success: function(msg){
		//  if(jQuery('#shipping_method option').length!=msg.length){  
			  var new_option="";
			  for(var i=0;i<msg.length;i++){
				 new_option=new_option+'<option value="'+msg[i]['id']+'">'+msg[i]['module']+'</option>'+"\n";
			  }
			  jQuery('#shipping_method').empty();
			  jQuery('#shipping_method').html(new_option);
		 // }
   }
 });

}
$j(document).ready(function(){		
	
		$j('input[name="input_qty[]"]').each(function(){
		
				$j(this).blur(function(){
				
						update_quantity($j(this));
				
						setTimeout('update_shipping_cost();', 1000);
						
						setTimeout('update_total();', 2000);
						jQuery('#shipping_method').empty();
						jQuery('#shipping_method').html('<option>Loading...</option>');
						setTimeout('updateModule();', 2000);
						
				});
				
		});
		
		$j('#btn_update_cart').click(function(){
		
				$j('input[name="input_qty[]"]').each(function(){

								update_quantity($j(this));
								
								setTimeout('update_shipping_cost();', 1000);
								
								setTimeout('update_total();', 2000);
								
				});				
				
				return false;
		
		});
		
		$j('#shipping_method').change(function(){
		
			update_shipping_cost();
			
			setTimeout('update_total();', 1000);
		
		});
		
		update_shipping_cost();
		
		setTimeout('update_total();', 1000);
		
		//Set Content Height if too short
		
		if ($j('.inner_content').height() < 552){
		
			space_height = $j('#spacer').height();
		
			$j('#spacer').height(space_height + (552-$j('.inner_content').height()));
		
		}

});
