var ob;
var arrIDp;
$(document).ready(function(){
	ob=new Cart();
	arrIDp=new Array();
	
	$('.pqty').blur(function(){
		ob.setProductValues(this);
	});
	$('#upd').click(function(){
		$('#hmode').val('Update');
		document.shopFrm.submit();
	});
	$('#cAll').click(function(){
		$('.chk').attr('checked',$(this).is(':checked'));
	});
	$('.chk').click(function(){
		if($('.chk').length==$('.chk:checked').length)
			$('#cAll').attr('checked',true);
		else
			$('#cAll').attr('checked',false);
	});
	
	$('#rem').click(function(){
		if($('.chk:checked').length==0)
			alert('- Please Check at least one checkbox.');
		else
		{			
			$('#hmode').val('Remove');
			$('#shopFrm').submit();
		}
	});
	
	$('#checkout').click(function(){
		window.location.href='billing.php';
	});
	$('#continue').click(function(){
		if($('#cid').val()=="" || $('#cid').val()=="0")
		{								  
		window.location.href='./';
		}
		else
		{
			window.location.href='products.php?cid='+$('#cid').val();
		}
	});
});
function Cart()
{
	
}
Cart.prototype.setProductValues=function(v)
{	
	var pqty=$(v).val();
	if(pqty.length==0 || isNaN(pqty) || pqty<=0)
	pqty=parseInt($(v).attr('title'));
	pqty=parseInt(pqty);
	$(v).val(pqty);
	var totCol=$(v).parent().next();
	var priceTab=$(v).parent().next().find('.black-text');
	var price=parseFloat($(priceTab[0]).text());
	var GrandTotal=price*pqty;
	$(v).parent().next().next().find('.black-text').text(num2round(GrandTotal,2));
	
	
}
function num2round(num,dec)
{
	var n=num.toString();
	var pos=n.indexOf(".");
	if(pos==-1)
	{
		var decval="";
		for(var i=0;i<dec;i++)
			decval+="0";
		return num+"."+decval;
	}
	else
	{
		var decval=n.substring(pos+1);
		for(var i=decval.length;i<dec;i++)
			decval+="0";
		return n.substring(0,pos)+"."+decval.substring(0,dec);
	}
}