var ECART = "ecart";
var ECART2 = "ecart2";
var ECART_MISC = "ecart_misc";
var ECART_GC = "ecart_gc";
var ECART_GCPYMT = "ecart_pymt";

//-----------------------------------------------------------------------------------
function gjsf_add_item_to_cart (p_prodcode, p_quanity)
{
	Set_XCookie (ECART, p_prodcode, p_quanity);
}
//-----------------------------------------------------------------------------------
function gjsf_add_misc_item_to_cart (p_desc, p_unitprice, p_qty)
{
	Set_XCookie (ECART_MISC, p_desc, p_unitprice+","+p_qty);
}
//-----------------------------------------------------------------------------------
function gjsf_add_promotion_code_to_cart (p_promo)
{
	Set_XCookie (ECART2, "promo", p_promo);
}

//-----------------------------------------------------------------------------------
function gjsf_add_note_to_cart (p_note)
{
	Set_XCookie(ECART2, "note", p_note);
}

//-----------------------------------------------------------------------------------
function gjsf_remove_item_from_cart (p_prodcode)
{
	Delete_XCookie(ECART, p_prodcode);
}
//-----------------------------------------------------------------------------------
function gjsf_remove_misc_item_from_cart (p_miscitem)
{
	Delete_XCookie(ECART_MISC, p_miscitem);
}

//-----------------------------------------------------------------------------------
function jsf_recalculate_order_subtotal()
{ 
	var xtag = document.getElementsByTagName("input");
	var k;
	ordertotal = 0.0;
	totalitems = 0;
	for (k=0; k<xtag.length; k++)
	{	if (xtag[k].id.substr(0,7)=="fd-qty-")
		{
			crow = xtag[k].parentNode.parentNode;
			unitprice = crow.cells[2].innerText * 1;
			qty = xtag[k].value * 1;
			itemtotal = unitprice.toFixed(2) * qty;
			totalitems += qty;
			ordertotal += itemtotal;
		}
//		itemtot = 0.0;
//		fname = xtag[k].id;
//		if (fname.substr(0,7)=="fd-upr-")
//		{	unitprice = xtag[k].value;
// 			prodcode = fname.substr(7); 
//			qty = document.getElementById("fd-qty-"+prodcode).value * 1;
//			totalitems += qty;
//			itemtot = unitprice * qty * 1;
//			ordertotal += itemtot;
//		}
	}
	if (document.getElementById('td-tot-qty'))
		document.getElementById('td-tot-qty').innerText = totalitems.toFixed(0);
		
	if (document.getElementById('td-order-total'))
		document.getElementById('td-order-total').innerText = ordertotal.toFixed(2);
}

//-----------------------------------------------------------------------------------
function jsf_change_item_quantity (p_fld, p_prodcode)
{
	var newqty = p_fld.value;
	if (!gjsf_isnumeric(newqty)||newqty<=0)
	{
		alert ('Invalid quantity');
		p_fld.value = 1;
		newqty = 1;
	}
	crow = (p_fld.parentNode.parentNode);

	var unitprice = crow.cells[2].innerText;
	
	newtotal = unitprice * newqty;
	crow.cells[4].innerText = newtotal.toFixed(2);

	if (p_prodcode!="")	// for item with product code
		gjsf_add_item_to_cart (p_prodcode, newqty);
	else				// for item without product code
	{	itemdesc = crow.cells[1].innerText;
		gjsf_add_misc_item_to_cart (itemdesc, unitprice, newqty);
	}
	jsf_recalculate_order_subtotal();
}

//-----------------------------------------------------------------------------------
function jsf_delete_item (p_pcode, p_obj)
{
	if (confirm("Are you sure to delete the selected item from cart?"))
	{
		p_pcode = p_pcode.toLowerCase();
		tbl = document.getElementById("tbl_order");
		if (tbl)
		{ 
			var rindx = p_obj.parentNode.parentNode.rowIndex;
			if (p_pcode!="")
				gjsf_remove_item_from_cart (p_pcode);
			else
			{	misc_item = tbl.rows[rindx].cells[1].innerText;
				gjsf_remove_misc_item_from_cart (misc_item);
			}
			tbl.deleteRow(rindx);
			jsf_recalculate_order_subtotal();
		}
	}
}
//-----------------------------------------------------------------------------------
function jsf_update_field_to_cookie (p_obj)
{
	var fieldname = p_obj.id;
	var fieldvalue = p_obj.value;
	Set_XCookie(ECART2, fieldname, fieldvalue);
}
//-----------------------------------------------------------------------------------
function gjsf_open_product_detail_window (p_cat, p_row, p_col)
{
	url = "shpwndw_details.php?cat="+p_cat+"&r="+p_row+"&c="+p_col;
	window.open (url, "_blank", "top=50,left=50,width=900,height=800,scrollbars=yes,resizable=yes");
}
