// JavaScript Document
function returnFloatPrice(price)
{
	var priceString = String(price);
	if(priceString.indexOf('.')!= -1)
	{
		var priceAry = priceString.split('.');
		var intPrice = priceAry[0];
		var decPrice = priceAry[1];
		if(decPrice.length==1)
		{
			var str = decPrice.concat('0'); 
		}
		else
		{
			var str = decPrice.substr(0,2);
		}
		var retPrice = intPrice.concat('.',str);
	}
	else
	{
		var retPrice = priceString.concat('.00'); 
	}
	return retPrice; 
}
/////// FUNCTION TO DISPLAY SHIPPING CHARGES //////////
function getShippingCharge(val)
{
	document.getElementById('hdn_cur_ship').value = val;
	document.form_basket.submit();
}
////////// FUNCTION TO GET TOTAL PRICE ///////////////
function getTotalPrice()
{
	var subtotal = document.getElementById('hdn_sub_total_price').value;
	if(document.getElementById('txt_voucher').value != '')
	{
		subtotal = parseFloat(subtotal) - parseFloat(subtotal*.10);
		var discount = parseFloat(subtotal*.10); 
	}
	else
	{
		var discount = 0; 			
	}
	discount = returnFloatPrice(discount);
	var vat = ((subtotal)*(0.12));
	vat = returnFloatPrice(vat);
	var shipping = document.getElementById('hdn_shipping_price').value;
	var totalprice = (parseFloat(subtotal)+parseFloat(vat)+parseFloat(shipping));
	document.getElementById('hdn_discount').value = discount;
	document.getElementById('id_discount').innerHTML = '&pound;'+discount;
	document.getElementById('vat_id').innerHTML = '&pound;'+vat;
	document.getElementById('hdn_vat').value = vat;
	totalprice = returnFloatPrice(totalprice);
	document.getElementById('hdn_total_price').value = totalprice;
	document.getElementById('total_price').innerHTML = '&pound;'+totalprice;
}
//////////////
function getPrice(noItems,id,price)
{
	price = parseFloat(price);
	//////////////////////////////// FOR INDIVIDUAL ITEM TOTAL PRICE IN VIEW PAGE START /////////////////////////
	var itemsPrice = (noItems*parseFloat(price));
	itemsPrice = returnFloatPrice(itemsPrice);
	document.getElementById('itemsPrice_'+id).innerHTML = '&pound;'+itemsPrice;
	//////////////////////////////// FOR INDIVIDUAL ITEM TOTAL PRICE IN VIEW PAGE END /////////////////////////
	//////////////////////////////// FOR SUBTOTAL PRICE IN VIEW PAGE START /////////////////////////
	
		if(noItems>document.getElementById('hdn_previousItems_'+id).value)
			var subTotalPrice = parseFloat(document.getElementById('hdn_sub_total_price').value)+((parseInt(noItems)-parseInt(document.getElementById('hdn_previousItems_'+id).value))*parseFloat(price));
		else if(noItems<document.getElementById('hdn_previousItems_'+id).value)
			var subTotalPrice = parseFloat(document.getElementById('hdn_sub_total_price').value)-((parseInt(document.getElementById('hdn_previousItems_'+id).value)-1)*parseFloat(price));
		else
			var subTotalPrice = parseFloat(document.getElementById('hdn_sub_total_price').value);
	subTotalPrice = returnFloatPrice(subTotalPrice);
	//////////////////////////////// FOR SUBTOTAL PRICE IN VIEW PAGE END /////////////////////////
	//////////////////////////////// FOR TOTAL PRICE IN VIEW PAGE START /////////////////////////
	var vat = ((subTotalPrice)*(0.12));
	vat = returnFloatPrice(vat);
	var shipping = document.getElementById('hdn_shipping_price').value;
	var totalprice = (parseFloat(subTotalPrice)+parseFloat(vat)+parseFloat(shipping));
	totalprice = returnFloatPrice(totalprice);
	////////////// FOR DISCOUNT //////////////////////////
	/*if(document.getElementById('hdn_discount').value!=0)
		totalprice = totalprice - document.getElementById('hdn_discount').value */
	//////////////////////////////// FOR TOTAL PRICE IN VIEW PAGE END /////////////////////////
	document.getElementById('vat_id').innerHTML = '&pound;'+vat;
	document.getElementById('hdn_previousItems_'+id).value=noItems;
	totalprice = returnFloatPrice(totalprice);
	document.getElementById('sub_total_price').innerHTML = '&pound;'+subTotalPrice;
	document.getElementById('hdn_sub_total_price').value = subTotalPrice;
	document.getElementById('hdn_total_price').value = totalprice;
	document.getElementById('total_price').innerHTML = '&pound;'+totalprice;
}  
///////// FUNCTION TO GET ITEMS PRICE //////////////
function updateBasket(totItems)
{
	for (num = 0; num < totItems; num++) {
		if(document.getElementById('prod_qty_'+num).value=='')
		{
			alert('Please Eneter Quantity');	
			document.getElementById('prod_qty_'+num).focus();
			return false;
		}
		if(document.getElementById('prod_qty_'+num).value==0)
		{
			alert('Please Eneter Quantity greater than 0');	
			document.getElementById('prod_qty_'+num).focus();
			return false;
		}
		if(isNaN(document.getElementById('prod_qty_'+num).value))
		{
			alert('Please Eneter Only Numaric Data');	
			document.getElementById('prod_qty_'+num).focus();
			return false;
		}
		document.getElementById('hdn_updatebasket').value = 1;
		document.form_basket.submit();
	}
}
/////// FUNCTION TO GET DISCOUNT ON VOUCHE CODE ////////////////////
function getDiscount()
{
	if(document.getElementById('hdn_discount').value==0)
	{
		if(document.getElementById('txt_voucher').value == '')
		{
			alert('Please enter voucher code to get discount');	
			document.getElementById('txt_voucher').focus();
			return false;
		}
		else
		{
			document.form_basket.submit();
		}
	}
}
/////// FUNCTION TO DELETE ITEMS FROM THE SESSION ////////////////
function deleteBasketItems(itemId,isSample)
{
	if(confirm('Are you sure you want to delete this product from basket'))
	{
		document.getElementById('hdn_itemId').value = itemId;
		document.getElementById('hdn_isSample').value = isSample;
		document.form_basket.submit();
	}
}

/////// FUNCTION TO REDIRECT TO CHECKOUT PAGE ////////
function submitToCheckout() {
	document.location = site_url+'checkout';	
}
