function highlightfade()
{

  jQuery('#total_amount').parent().highlightFade({color:"#990000",speed:1000});	
	jQuery('#total').highlightFade({color:"#990000",speed:1000});	
	jQuery('#qty').highlightFade({color:"#990000",speed:1000});	
  /*jQuery('#coupon_total').parent().highlightFade({color:"#990000",speed:1000});
  jQuery('#grand_total').parent().highlightFade({color:"#990000",speed:1000});/**/
}
function check_remove(id)
{
	if(confirm('Are You Sure To Remove'))
	{
		var url='ajax.request.php';
		var params={"case":"delete_cart","id":id};
   jQuery.getJSON(url,params,deleted_shoppingcart_result);
	}
}

function check_login() 
{
//alert('hi');
document.frm.submit();
}

function deleted_shoppingcart_result(arr)
{
		
		if(arr['id']!=undefined)
		{
	
			jQuery('input[name="btn_remove['+arr['id']+']"]').parent().parent().parent().remove();
	
			recalc("onload");
			
			jQuery('#qty').html(arr['totalquantity']);
			jQuery('#total_cart').html(Number(arr['totalamount']).toFixed(2));
			jQuery('#pur_cbm').html(Number(arr['pur_cbm']).toFixed(2));		
		  jQuery('#rem_cbm').html(Number(arr['rem_cbm']).toFixed(2));	
						
			if(parseInt(document.getElementById("total_amount").innerHTML)==0)
			{
				document.getElementById("tbl_cart").style.display="none";
				document.getElementById("div_cartblank").style.display="";
			}
		}	
}


jQuery(document).ready(function (){
	
	// bind the recalc function to the quantity fields
	jQuery("input[name^=txt_quantity_]").bind("keyup", recalc);
	// run the calculation function now
	recalc("onload");

			
});


function recalc(flg)
{
	
	//alert("ce");
	var pattern=/^[0-9]+$/;
		
	var productid=jQuery(this).attr('productid');
	var attributeid=jQuery(this).attr('attributeid');
	var parentid=jQuery(this).attr('parentid');
	
	var attprice=jQuery(this).attr('attributeprice');
	
	var sum=0;	
	var quantitysum=0;
	
	if(parentid!=undefined)
	{	
		if(!(jQuery(this).val().match(pattern)))
		{
			jQuery(this).val(0);
		}
		
		if(parseInt(jQuery('input[name=txt_quantity_'+parentid+']').parseNumber())<parseInt(jQuery(this).parseNumber()))
		{
			jQuery(this).val(jQuery('input[name=txt_quantity_'+parentid+']').val());
		}
	}	
	else
	{		
		if(flg!="onload")
		{	
			if(!(jQuery(this).val().match(pattern)) || parseInt(jQuery(this).val())<=0)
			{
				alert('Invalid Quantity');
				jQuery(this).val(1);			
			}
		}
		
		var quantity=jQuery(this).val();
		jQuery('[parentid='+productid+']').each(function(){
			if(quantity<jQuery(this).val())
			{
				//jQuery(this).attr('attributeid');			
				var params={'case':"cartupdtqty",productid:productid,quantity:jQuery(this).val()};
				jQuery.getJSON('ajax.request.php',params);
			}		
		});	
	}
	
	jQuery("[id^=span_amount]").calc(
//jQuery("[id^=total_cart]").calc(
		// the equation to use for the calculation
		"qty * price",
		// define the variables used in the equation, these can be a jQuery object
		{
			
			qty: jQuery("input[name^=txt_quantity_]"),
			price: jQuery("[id^=span_price_]")
		},
		// define the formatting callback, the results of the calculation are passed to this function
		function (s){		
			
			// return the number as a dollar amount
			//return "$" + s.toFixed(2);
			return s.toFixed(2);
		},
		// define the finish callback, this runs after the calculation has been complete
		function ($this){
			// sum the total of the $("[id^=total_item]") selector  
			sum = $this.sum();
			
			jQuery("#total_amount").text(
		
		
		///////////////////////////////////////////this will shown on top when we calculate and delete/////////////////////////////
			//jQuery("#total_cart").text(
				// round the results to 2 digits
				//"$" + sum.toFixed(2)
				sum.toFixed(2)
			);
			
			jQuery('#frm_final_total').val(jQuery("#total_amount").html());
			
			if(jQuery('#coupon_total').text()!='')
			{
				var percent=jQuery('#coupon_percent').parseNumber();
				var coupon_amount=jQuery('#coupon_total').parseNumber();
				var total_amount=jQuery("#total_amount").parseNumber();
				
				if(percent>0)
				{
			
					//coupon_amount=(jQuery("[id^=span_amount]:not([isattribute=1])").sum()*percent/100).toFixed(2);
					//jQuery('#coupon_total').text(coupon_amount);
				}
							
				var grand_total=total_amount-coupon_amount;				
				jQuery("#grand_total").text(grand_total.toFixed(2));
				jQuery('#final_total').val(jQuery("#grand_total").html());
			}
		});

	if(jQuery('#prev_total_amount').val()!=sum && jQuery('#prev_total_amount').val()>0  || attprice==0)
	{
		//var attr=attributeid==undefined?0:attributeid;
				//alert("asdad");
		var params={'case':"cartupdtqty",productid:productid,quantity:jQuery(this).val()};
		jQuery.getJSON('ajax.request.php',params,function(arr){
			jQuery('#pur_cbm').html(arr['pur_cbm'].toFixed(2));		
		});
				
		highlightfade();			
		
		quantitysum=jQuery("[name^=txt_quantity_]:not([isattribute=1])").sum();
				
		jQuery('#qty').html(quantitysum);
		jQuery('#total').html(sum.toFixed(2));
		//jQuery('#pur_cbm').html(arr['pur_cbm'].toFixed(2));		
	}
			
	jQuery('#prev_total_amount').val(sum);
}

