$(document).ready(function() 
{ 
	// remove  img mouseover and mouseout
	$(".addcartthumpimgdiv").mouseover(function() {
	  var product_id = this.title; 	 
	  $('#removeproduct_'+product_id).css("visibility","visible");
	});
	
	$(".addcartthumpimgdiv").mouseout(function() {
	  var product_id = this.title; 	 
	  $('#removeproduct_'+product_id).css("visibility","hidden");
	});		
	
	// remove addtocart particular product 
	$(".remove").click(function() {		
	    var product_id = parseInt(this.lang); 
		var product_process=$("#product_process").val();		
	    if(product_process!=1)
		{
			$("#product_process").val(1);				
			parameter="&product_id="+product_id+"&action=deletecart";				
			urlpath=HTTP_SERVER+DIR_INCLUDES+DIR_MODULES+"cart.php";			
			ajaxcallcart(urlpath,parameter);			
		}
	});
	
	function ajaxcallcart(urlpath,parameter)
	{				
		var radomno=Math.random()*1000000; 		
		var parametri ="&random_no="+radomno+parameter;	
		var leftwidth = parseInt(85);
		var url=urlpath;
	    	$.ajax({
			url: url,
			type: "POST",
			dataType: 'html',
			data: parametri,
			timeout: 5000,
			cache: false,
			success: function(html)
			{
			   var product_id = parseInt(html); 
			   var h=parseInt($("#addcartthumpimgdiv_"+product_id).attr("lang"));				
			   var fixedwidth=leftwidth*h;			  
			   $("#addcartthumpimgdiv_"+product_id).animate({"left": fixedwidth+"px","top": "115px"}, 1000, function()
			   {
				    var prod_order=  parseInt($(".prod_order_"+product_id).val());					
					var leftwidth = parseInt(85);
					var h=0;
					var main_content = $(".shop-center");
					var toBeTOCced = $(".addcartthumpimgdiv", main_content);
					toBeTOCced.each(function(i) 
					{
						h++;
					});	
					
					$("#addcartthumpimgdiv_"+product_id).remove();  
					
					for(k=prod_order; k<h; k++)
					{
					   
						var postion=parseInt(k);						
						if(postion != prod_order)
						{							
							var perviouspostion=parseInt(k)-1;			
							var productid=parseInt($("#prod_order_"+postion).attr("lang"));		
							$("#prod_order_"+postion).val(perviouspostion);							
							if(perviouspostion>=8)
							{					 
								 $('#addcartthumpimgdiv_'+productid).hide(1000);   
							}
							else
							{					
								 $('#addcartthumpimgdiv_'+productid).show(1000);  
							}
							   
							$("#addcartthumpimgdiv_"+productid).attr({'lang':perviouspostion});	
							$(".prod_order_"+productid).attr({'id':'prod_order_'+perviouspostion});				
							if(perviouspostion==0){ var fixedwidth="0px"; } else { var fixedwidth=""+leftwidth*perviouspostion+"px"; }					
							
							$("#addcartthumpimgdiv_"+productid).animate({"left": fixedwidth,"top": "25px"}, 1000, function()
							{	
							   
							
							});	
						}
					}	
					
					 $("#proceed_checkout_"+product_id).hide(); 
					 $("#add-list-btn_"+product_id).show();
			   }); 			   	
			   $(".shop_cart_prod_"+product_id).hide();
			   countparameter="&action=countcart";
			   urlpath=HTTP_SERVER+DIR_INCLUDES+DIR_MODULES+"cart.php";
			   ajaxcountcart(urlpath,countparameter);
			   ajaxtotaldisplay(urlpath);
			   $("#product_process").val('');
			}		
		});		
	}
	
	// count product	
	function ajaxcountcart(urlpath,parameter)
	{				
		var radomno=Math.random()*1000000; 
		var re = /\s*((\S+\s*)*)/;
		var parametri ="&random_no="+radomno+parameter;
		var url=urlpath;
	    	$.ajax({
			url: url,
			type: "POST",
			dataType: 'html',
			data: parametri,
			timeout: 5000,
			cache: false,
			success: function(html)
			{
			  var data=html.replace(re, "$1");
			  if(data){  $(".shop-bag-count").html("("+data+")"); } else {  $(".shop-bag-count").html("(0)"); }
			   return true;
			}		
		});		
	}	
	
	// total amount display
	function ajaxtotaldisplay(urlpath)
	{				
		var radomno=Math.random()*1000000; 
		var re = /\s*((\S+\s*)*)/;
		var parametri ="&action=gettotalamt&random_no="+radomno;
		var url=urlpath;
	    	$.ajax({
			url: url,
			type: "POST",
			dataType: 'html',
			data: parametri,
			timeout: 5000,
			cache: false,
			success: function(html)
			{  
			   var data=html.replace(re, "$1");
			   var splitdata = data.split("~");
			   
			   var taxtotal=splitdata[1];
			   var discounttotal=splitdata[2];
			   var coupontotal=splitdata[3];
			   var subtotal=splitdata[0];
			   var fulltotal=splitdata[4];
			   
			   if(taxtotal) { $("#tax_tot").html(taxtotal); }else { $("#tax_row").hide(); }
			   if(discounttotal) { $("#discount_tot").html(discounttotal); }else { $("#discount_row").hide(); }
			   if(coupontotal) { $("#coupon_tot").html(coupontotal); }else { $("#coupon_row").hide(); }
			   
			   if(taxtotal!="" || discounttotal!="" || coupontotal!="") { $("#hr_row").show();  }else { $("#hr_row").hide();  }
				   
			   $("#sub_tot").html(subtotal);
			   $("#full_tot").html(fulltotal);
			   if(subtotal=="")
			   {
				   $(".couponform").hide();
				   //$(".scrollContent").html("<br><br>Your Shopping Cart is empty."); 
				   window.location.href="index.php?mainpage=shoppingcart";				   
			    }
			   return true;
			}		
		});		
	}
	
	$(".quantity").keypress(function (e)
	{		
	   if(e.which!=13)
	   {
			if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
			{    
				alert("Numberic value only allowed");
				return false;
			}
	   }
	});
	
	
});	

function updatecouponcode()
{   
    var coupon_code=document.getElementById("coupon_code").value; 
	if(coupon_code=="")
	{
	  alert('Please Enter the coupon code');
	  document.getElementById("coupon_code").focus();
	  return false;
	}	
}

function cart_delete()
{
  return confirm('Are you sure to delete this Product?');
}

