var	CKShipCountry	= 0;
var	CKShipMethod	= 0;
var	CKCouponCode	= "";
var	CCC				= "Super5";
var	CCD				= 5;

function ParseGet()
{
	for	(i=0; i<itemNum; i++)
		ProdArray[idxItem[i]].count = GetSCItems(CK_Item[idxItem[i]]);

	CKShipCountry = GetSCItems(CK_ShipCountry);
	CKShipMethod = GetSCItems(CK_ShipMethod);
	CKCouponCode = GetSCItems(CK_CouponCode);

	parseData = location.search.substring(1,location.search.length);

	//Check "sku" for SKU
	if	(parseData.indexOf('sku=') != -1)
	{
		idx = parseData.indexOf('sku=');
		sku = parseData.substring(idx+4, idx+7);
		ProdArray[sku].count++;
		if	(ProdArray[sku].count >= 10)
		{
			alert("You are limited to order less than 10 per item.");
			ProdArray[sku].count--;
		}
		if	(ProdArray[sku].avail == 0)
		{
			alert(ProdArray[sku].name+" is temporary SOLD OUT.\nIt should be available soon.");
			ProdArray[sku].count = 0;
		}
		SetSCItems(CK_Item[sku],ProdArray[sku].count);
	}

	//Check "sc" for Shipping Country
	if	(parseData.indexOf('sc=') != -1)
	{
		idx = parseData.indexOf('sc=');
		CKShipCountry = parseData.substring(idx+3, idx+4);
		if	(parseData.substring(idx+4, idx+5) != '&')
			CKShipCountry = parseData.substring(idx+3, idx+5);
		SetSCItems(CK_ShipCountry,CKShipCountry);
	}

	//Check "sm" for Shipping Method
	if	(parseData.indexOf('sm=') != -1)
	{
		idx = parseData.indexOf('sm=');
		CKShipMethod = parseData.substring(idx+3, idx+4);
		SetSCItems(CK_ShipMethod,CKShipMethod);
	}

	//Check "d_" for item to be deleted
	//Check "q_" for Quantity update
	var	jd;
	var	jq;
	for	(j=0; j<itemNum; j++)
	{
		jd = "d" + j + "=";	//Converted the number i to string
		jq = "q" + j + "=";	//Converted the number i to string
		if	(parseData.indexOf(jd) != -1)
		{
			ProdArray[idxItem[j]].count = 0;
			SetSCItems(CK_Item[idxItem[j]],0);
		}
		else
			if	(parseData.indexOf(jq) != -1)
			{
				idx = parseData.indexOf(jq);
				if	(j <10)
					pq = parseData.substring(idx+3, idx+5);
				else
					pq = parseData.substring(idx+4, idx+6);
				if	(Number(pq) >= 10)
					alert("You are limited to order less than 10 per item.");
				else
					if	(Number(pq) < 0)
					{
						ProdArray[idxItem[j]].count = 0;
						SetSCItems(CK_Item[idxItem[j]],0);
					}
					else
					{
						if	(j <10)
							pq = parseData.substring(idx+3, idx+4);
						else
							pq = parseData.substring(idx+4, idx+5);
						ProdArray[idxItem[j]].count = pq;
						SetSCItems(CK_Item[idxItem[j]],pq);
					}
			}
	}

	//Check "sc" for Shipping Country
	if	(parseData.indexOf('cc=') != -1)
	{
		idx = parseData.indexOf('cc=');
		CKCouponCode = parseData.substring(idx+3, idx+9);
		if	((CKCouponCode != CCC) && (CKCouponCode != ""))
		{
			if	(CKCouponCode.indexOf('&updat') == -1)
				alert("Your Coupon Code is NOT valid.\nPlease try again.");
			CKCouponCode = "";
		}
		SetSCItems(CK_CouponCode,CKCouponCode);
	}
}

//
//Actual Shopping Cart showing all items to be purchased
//Allow Remove, Change Qty and S&H method
//
function DisplayShoppingCart()
{
	var	itemPrice		= 0;
	var	itemCountTotal	= 0;
	var	totalshCost		= 0;
	var	cDiscount		= 0;
	var	amountTotal		= 0;
	var	eligible		= 0;	//Eligible for Budget shipment?

	for	(i=0; i<itemNum; i++)
	{
		itemCountTotal += Number(ProdArray[idxItem[i]].count);
		if	(Number(ProdArray[idxItem[i]].count) != 0)
		{
			if	(ProdArray[idxItem[i]].economy == 1)	//If product is eligible for Budget shipment
			{
				eligible = 1;
				if	(Number(itemCountTotal) >= 3)	//But still depending on order quantity
					eligible = 0;
			}
			else
				eligible = 0;
		}
	}


	if	(itemCountTotal > 0)
		document.write('<FORM method="GET" action="ShoppingCart.html">');

	document.write('<table width="100%" cellpadding="3" cellspacing="1" border="1" align="center">');
	document.write('<tr bgcolor=#FFFFFF>');
	document.write('<td align="center" width="8%"><font face="Arial" size="2"><b>Remove</b></font></td>');
	document.write('<td align="center" width="8%"><font face="Arial" size="2"><b>Qty</b></font></td>');
	document.write('<td align="left" width="10%"><font face="Arial" size="2"><b>SKU</b></font></td>');
	document.write('<td align="left" width="59%"><font face="Arial" size="2"><b>Item</b></font></td>');
	document.write('<td align="right" width="15%"><font face="Arial" size="2"><b>Price&nbsp;(USD)</b></font></td>');

//List of all items
	for	(i=0; i<itemNum; i++)
	{
		if	(ProdArray[idxItem[i]].count > 0)
		{
			itemTotalPrice = Number(ProdArray[idxItem[i]].price) * Number(ProdArray[idxItem[i]].count);
			amountTotal += itemTotalPrice;
			document.write('</tr>');
			document.write('<tr bgcolor=#FFFFFF>');
			document.write('<td align="center" width="8%"><input type="checkbox" name="d'+i+'" value="1"></td>');
			document.write('<td align="center" width="8%"><input type="text" name="q'+i+'" value="'+ProdArray[idxItem[i]].count+'" size="1"></td>');
			document.write('<td align="left" width="10%"><font face="Arial" size="2">'+ProdArray[idxItem[i]].SKU+'</font></td>');
			document.write('<td align="left" width="59%"><font face="Arial" size="2">'+ProdArray[idxItem[i]].name+'&nbsp;&nbsp;@'+FormatMoney(ProdArray[idxItem[i]].price)+' each</font></td>');
			document.write('<td align="right" width="15%"><font face="Arial" size="2">'+FormatMoney(itemTotalPrice)+'</font></td>');
			document.write('</tr>');
		}
	}

	if	(itemCountTotal <= 0)	//If Shopping Cart is Empty
	{
		CKShipMethod = 0;	//Default to Budget 1st Class
		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=5 align="center"><font face="Arial" size="2">Cart Empty</font></td>');
		document.write('</tr>');
	}
//Shipping and Handling
	if	(Number(CKShipCountry) == 0)	//U.S.A.
	{
		if (Number(CKShipMethod) >= 5)	//Switched from International to U.S.A.
		{
			CKShipMethod = Number(CKShipMethod) - 5;
			SetSCItems(CK_ShipMethod,CKShipMethod);
		}
		if	(Number(CKShipMethod) == 2)	CKShipMethod = 3;	//From Budget Priority Mail to Priority Mail
	}
	else
	{
		if (Number(CKShipMethod) < 5)	//Switched from U.S.A. to International
		{
			CKShipMethod = Number(CKShipMethod) + 5;
			SetSCItems(CK_ShipMethod,CKShipMethod);
		}
	}
	if	(eligible == 0 && itemCountTotal > 0)	//Switched from Not eligible Budget shipment
	{
		if	(Number(CKShipMethod) == 0)	CKShipMethod = 1;	//From Budget 1st Class to 1st Class
		if	(Number(CKShipMethod) == 5)	CKShipMethod = 6;	//From Budget Airmail to Airmail
		if	(Number(CKShipMethod) == 2)	CKShipMethod = 3;	//From Budget Priority Mail to Priority Mail
		if	(Number(CKShipMethod) == 7)	CKShipMethod = 8;	//From Budget Global Priority Mail to Global Priority Mail
		SetSCItems(CK_ShipMethod,CKShipMethod);
	}
	if	((Number(CKShipCountry) == 10)			//Italy
			|| (Number(CKShipCountry) == 17)	//Turkey
			|| (Number(CKShipCountry) == 18))	//Other Countries
	{
		if ((Number(CKShipMethod) == 2)		//Selected Budget Priority before
			|| (Number(CKShipMethod) == 3)	//Selected Priority before
			|| (Number(CKShipMethod) == 7)	//Selected Budget Global Priority before
			|| (Number(CKShipMethod) == 8))	//Selected Global Priority before
		{
			CKShipMethod = 9;	//Set to Global Express
			SetSCItems(CK_ShipMethod,CKShipMethod);
		}
	}

	if	(itemCountTotal > 0)
	{
		//Calculate the total weight
		var	totalWeight = 0;
		for (w=0; w<itemNum; w++)
		{
			if	(ProdArray[idxItem[w]].count > 0)	//Has order
				totalWeight = totalWeight + Number(ProdArray[idxItem[w]].weight) * Number(ProdArray[idxItem[w]].count);
		}
		//Calculate shipping cost
		totalshCost = CalculateShippingCost(CKShipCountry,CKShipMethod);

		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=4 align="right">');
		document.write('<table width="100%" border="0" cellpadding="0" cellspacing="0">');
		//Country
		document.write('<tr>');
		document.write('<td colspan=9 align="center">');
		document.write('<font face="Arial" size="2" color="orange"><b>Please select the shipping method and destination below<b></font>');
		document.write('</td>');
		document.write('</tr>');
		//Space
		document.write('<tr>');
		document.write('<td colspan=9>');
		document.write('</td>');
		document.write('</tr>');
		document.write('<tr>');
		//Budget (1st Class / International Airmail)
		if	(eligible == 1)
		{
			document.write('<td >');
			document.write('<table border="0" cellpadding="0" cellspacing="0" class="boxTableFull">');
			document.write('<tr>');
			document.write('<td align="left" valign="top" class="textSmall">');
			document.write('&nbsp;<b>Bubble Envelope</b>');
			document.write('<br>');
			document.write('<img src="bmp/Economy.gif" border=0 width=90 height=90>');
			document.write('<br>');
			if	((Number(CKShipMethod) == 0) || (Number(CKShipMethod) == 5))
			{
				if	(Number(CKShipCountry) == 0)
					document.write('<input type="radio" name="sm" value="0" checked onClick="submit()">');
				else
					document.write('<input type="radio" name="sm" value="5" checked onClick="submit()">');
			}
			else
			{
				if	(Number(CKShipCountry) == 0)
					document.write('<input type="radio" name="sm" value="0" onClick="submit()">');
				else
					document.write('<input type="radio" name="sm" value="5" onClick="submit()">');
			}
			if	(Number(CKShipCountry) == 0)
				document.write('&nbsp;<b>1st Class Mail</b>');
			else
				document.write('&nbsp;<b>Airmail</b>');
			document.write('</td>');
			document.write('</tr>');
			document.write('</table>');
			document.write('</td>');
			document.write('<td>&nbsp;</td>');
		}
		//1st Class / International Airmail
		document.write('<td >');
		document.write('<table border="0" cellpadding="0" cellspacing="0" class="boxTableFull">');
		document.write('<tr>');
		document.write('<td align="left" valign="top" class="textSmall">');
		document.write('&nbsp;<b>Shipping Box</b>');
		document.write('<br>');
		document.write('<img src="bmp/US1stClass.gif" border=0 width=90 height=90>');
		document.write('<br>');
		if	((Number(CKShipMethod) == 1) || (Number(CKShipMethod) == 6))
		{
			if	(Number(CKShipCountry) == 0)
				document.write('<input type="radio" name="sm" value="1" checked onClick="submit()">');
			else
				document.write('<input type="radio" name="sm" value="6" checked onClick="submit()">');
		}
		else
		{
			if	(Number(CKShipCountry) == 0)
				document.write('<input type="radio" name="sm" value="1" onClick="submit()">');
			else
				document.write('<input type="radio" name="sm" value="6" onClick="submit()">');
		}
		if	(Number(CKShipCountry) == 0)
			document.write('&nbsp;<b>1st Class Mail</b>');
		else
			document.write('&nbsp;<b>Airmail</b>');
		document.write('</td>');
		document.write('</tr>');
		document.write('</table>');
		document.write('</td>');
		document.write('<td>&nbsp;</td>');
		//Budget Priority Mail
		if	(eligible == 1 && Number(CKShipCountry) != 0)	//Not applicable to U.S.A.
		{
			if	((Number(CKShipCountry) != 10)			//Italy
					&& (Number(CKShipCountry) != 17)	//Turkey
					&& (Number(CKShipCountry) != 18))	//Other Countries
			{
				document.write('<td>');
				document.write('<table border="0" cellpadding="0" cellspacing="0" class="boxTableFull">');
				document.write('<tr>');
				document.write('<td align="left" valign="top" class="textSmall">');
				document.write('&nbsp;<b>Bubble Envelope</b>');
				document.write('<br>');
				document.write('<img src="bmp/Economy.gif" border=0 width=90 height=90>');
				document.write('<br>');
				if	((Number(CKShipMethod) == 2) || (Number(CKShipMethod) == 7))
				{
					if	(Number(CKShipCountry) == 0)
						document.write('<input type="radio" name="sm" value="2" checked onClick="submit()">');
					else
						document.write('<input type="radio" name="sm" value="7" checked onClick="submit()">');
				}
				else
				{
					if	(Number(CKShipCountry) == 0)
						document.write('<input type="radio" name="sm" value="2" onClick="submit()">');
					else
						document.write('<input type="radio" name="sm" value="7" onClick="submit()">');
				}
				if	(Number(CKShipCountry) == 0)
					document.write('&nbsp;<b>Priority Mail</b>');
				else
					document.write('&nbsp;<b>Global Priority</b>');
				document.write('</td>');
				document.write('</tr>');
				document.write('</table>');
				document.write('</td>');
				document.write('<td>&nbsp;</td>');
			}
		}
		//Priority Mail
		if	((Number(CKShipCountry) != 10)			//Italy
				&& (Number(CKShipCountry) != 17)	//Turkey
				&& (Number(CKShipCountry) != 18))	//Other Countries
		{
			document.write('<td>');
			document.write('<table border="0" cellpadding="0" cellspacing="0" class="boxTableFull">');
			document.write('<tr>');
			document.write('<td align="left" valign="top" class="textSmall">');
			document.write('&nbsp;<b>Box / Envelope</b>');
			document.write('<br>');
			if	(Number(CKShipCountry) == 0)
				document.write('<img src="bmp/USPriority.gif" border=0 width=90 height=90>');
			else
				document.write('<img src="bmp/GlobalPriority.gif" border=0 width=90 height=90>');
			document.write('<br>');
			if	((Number(CKShipMethod) == 3) || (Number(CKShipMethod) == 8))
			{
				if	(Number(CKShipCountry) == 0)
					document.write('<input type="radio" name="sm" value="3" checked onClick="submit()">');
				else
					document.write('<input type="radio" name="sm" value="8" checked onClick="submit()">');
			}
			else
			{
				if	(Number(CKShipCountry) == 0)
					document.write('<input type="radio" name="sm" value="3" onClick="submit()">');
				else
					document.write('<input type="radio" name="sm" value="8" onClick="submit()">');
			}
			if	(Number(CKShipCountry) == 0)
				document.write('&nbsp;<b>Priority Mail</b>');
			else
				document.write('&nbsp;<b>Global Priority</b>');
			document.write('</td>');
			document.write('</tr>');
			document.write('</table>');
			document.write('</td>');
			document.write('<td>&nbsp;</td>');
		}
		//Express Mail
		document.write('<td>');
		document.write('<table border="0" cellpadding="0" cellspacing="0" class="boxTableFull">');
		document.write('<tr>');
		document.write('<td align="left" valign="top" class="textSmall">');
		document.write('&nbsp;<b>Postal Envelope</b>');
		document.write('<br>');
		if	(Number(CKShipCountry) == 0)
			document.write('<img src="bmp/USExpress.gif" border=0 width=90 height=90>');
		else
			document.write('<img src="bmp/GlobalExpress.gif" border=0 width=90 height=90>');
		document.write('<br>');
		if	((Number(CKShipMethod) == 4) || (Number(CKShipMethod) == 9))
		{
			if	(Number(CKShipCountry) == 0)
				document.write('<input type="radio" name="sm" value="4" checked onClick="submit()">');
			else
				document.write('<input type="radio" name="sm" value="9" checked onClick="submit()">');
		}
		else
		{
			if	(Number(CKShipCountry) == 0)
				document.write('<input type="radio" name="sm" value="4" onClick="submit()">');
			else
				document.write('<input type="radio" name="sm" value="9" onClick="submit()">');
		}
		if	(Number(CKShipCountry) == 0)
			document.write('&nbsp;<b>Express Mail</b>');
		else
			document.write('&nbsp;<b>Global Express</b>');
		document.write('</td>');
		document.write('</tr>');
		document.write('</table>');
		document.write('</td>');
		document.write('</tr>');
		//Space
		document.write('<tr>');
		document.write('<td colspan=9>');
		document.write('</td>');
		document.write('</tr>');
		document.write('<tr>');
		document.write('<td colspan=9>');
		document.write('</td>');
		document.write('</tr>');
		//Country
		document.write('<tr>');
		document.write('<td colspan=9 align="right">');
		document.write('<table width="100%" border="0" cellpadding="0" cellspacing="0">');
		document.write('<tr>');
		document.write('<td width="65%" align="right" valign="bottom" nowrap>');
		document.write('&nbsp;<b><font face="Arial" size="3" color="red"><span style="background-color: #FFFF00">The country to be shipped to</span>&nbsp;</font></b>');
		document.write('</td>');
		document.write('<td width="25%" align="right" valign="bottom" nowrap>');
		document.write('<b><marquee width="100%" direction=right scrolldelay=100 behavior=scroll><font face="Arial" size="3">>>></font><font face="Arial" size="3" color="red">select here</font><font face="Arial" size="3">>>>&nbsp;</font></marquee></b>');
		document.write('</td>');
		document.write('<td width="10%"align="right" valign="bottom">');
		document.write('<select type="hidden" name="sc" ONCHANGE="submit()">');
		for	(c=0; c<ctNum; c++)
		{
			if	(Number(CKShipCountry) == c)
				document.write('<option selected value="'+c+'">'+Country[c]);
			else
				document.write('<option value="'+c+'">'+Country[c]);
		}
		document.write('</select></td>');
		document.write('</tr>');
		document.write('</table>');
		document.write('</td>');
		document.write('</tr>');
		document.write('</table>');
		document.write('<td align="right" width="15%" bgcolor=#FFFFFF><font face="Arial" size="2">'+FormatMoney(totalshCost)+'</font></td>');
		document.write('</tr>');

		//Shipping Detail Information
		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=9 align="left">');
		document.write('<b><font face="Arial" size="2" color="green">');
		document.write(ShipOptionText[Number(CKShipMethod)]);
		document.write('</font></b>&nbsp;:&nbsp;&nbsp;&nbsp;<font face="Arial" size="2">');
		document.write(ShipDetailText[Number(CKShipMethod)]);
		document.write('&nbsp;&nbsp;<b>Destination:</b>&nbsp;</font><font face="Arial" size="2" color="red">');
		document.write(Country[Number(CKShipCountry)]);
		document.write('</font></td>');
		document.write('</tr>');
	}

	if	(itemCountTotal > 0)
	{
/*/Coupon/Discount
		if	((CKCouponCode == CCC) && (amountTotal+totalshCost >= 169))
		{
			if	(amountTotal+totalshCost >= 200)
				cDiscount = CCD * 2;
//			else
//				cDiscount = CCD;
		}
		else
		{
			cDiscount = 0;
			CKCouponCode = "";
			SetSCItems(CK_CouponCode,CKCouponCode);
		}
		amountTotal -= cDiscount;
		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=4 align="right"><font face="Arial" size="2"><b>Coupon Code:</b>&nbsp;<input type="text" name="cc" value="'+CKCouponCode+'" size="10" maxlength="6"></font></td>');
		if	(cDiscount == 0)
			document.write('<td align="right" width="15%"><font face="Arial" size="2">'+FormatMoney(cDiscount)+'</font></td>');
		else
			document.write('<td align="right" width="15%"><font face="Arial" size="2" color="red">-'+FormatMoney(cDiscount)+'</font></td>');
		document.write('</tr>');
*/
//Update button, Amount Total
		amountTotal += totalshCost;	//Include the S&H
		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=2 align="left"><input type="submit" name="update" value="Update Cart"></td>');
		document.write('<td colspan=2 align="right"><font face="Arial" size="2"><b>Total Amount:</b>&nbsp;</font></td>');
		document.write('<td align="right" width="15%" bgcolor=#FFFFFF><font face="Arial" size="2" color="blue"><b>'+FormatMoney(amountTotal)+'</b></font></td>');
		document.write('</tr>');
	}
	else
	{
		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=4 align="right"><font face="Arial" size="2"><b>Total Amount:</b>&nbsp;</font></td>');
		document.write('<td align="right" width="15%" bgcolor=#FFFFFF><font face="Arial" size="2" color="blue"><b>'+FormatMoney(amountTotal)+'</b></font></td>');
		document.write('</tr>');
	}
	document.write('</table>');
	if	(itemCountTotal > 0)
		document.write('</FORM>');
}

function DisplayShoppingCartFinal()
{
	var	itemCountTotal	= 0;
	var	totalshCost		= 0;
	var	amountTotal		= 0;
	var	cDiscount		= 0;
	var	shipNextDay		= 0;
	var	eligible		= 0;	//Eligible for Budget shipment?

	for	(i=0; i<itemNum; i++)
	{
		itemCountTotal += Number(ProdArray[idxItem[i]].count);
		if	(Number(ProdArray[idxItem[i]].count) != 0)
		{
			if	(ProdArray[idxItem[i]].economy == 1)	//If product is eligible for Budget shipment
			{
				eligible = 1;
				if	(Number(itemCountTotal) >= 3)	//But still depending on order quantity
					eligible = 0;
			}
			else
				eligible = 0;
		}
	}

	if	(itemCountTotal > 0)
		document.write('<FORM method="GET" action="ShoppingCart.html">');

	document.write('<table width="100%" cellpadding="3" cellspacing="1" border="1" align="center">');
	document.write('<tr bgcolor=#FFFFFF>');
	document.write('<td align="center" width="8%"><font face="Arial" size="2"><b>Qty</b></font></td>');
	document.write('<td align="left" width="10%"><font face="Arial" size="2"><b>SKU</b></font></td>');
	document.write('<td align="left" width="67%"><font face="Arial" size="2"><b>Item</b></font></td>');
	document.write('<td align="right" width="15%"><font face="Arial" size="2"><b>Price (USD)</b></font></td>');

	//List of all items
	for	(i=0; i<itemNum; i++)
	{
		if	(ProdArray[idxItem[i]].count > 0)
		{
			itemTotalPrice = Number(ProdArray[idxItem[i]].price) * Number(ProdArray[idxItem[i]].count);
			amountTotal += itemTotalPrice;
			document.write('</tr>');
			document.write('<tr bgcolor=#FFFFFF>');
			document.write('<td align="center" width="8%"><input type="text" name="q'+i+'" value="'+ProdArray[idxItem[i]].count+'" size="1"></td>');
			if	((ProdArray[idxItem[i]].avail == 1) && Number(CKShipCountry) == 0)
			{//Next Day Shipment items
				shipNextDay += 1;
				document.write('<td align="left" width="10%"><font face="Arial" size="2">'+ProdArray[idxItem[i]].SKU+'</font>&nbsp;<font face="Arial" size="2" color="blue">*</font></td>');
			}
			else
				document.write('<td align="left" width="10%"><font face="Arial" size="2">'+ProdArray[idxItem[i]].SKU+'</font></td>');
			document.write('<td align="left" width="67%"><font face="Arial" size="2">'+ProdArray[idxItem[i]].name+'&nbsp;&nbsp;@'+FormatMoney(ProdArray[idxItem[i]].price)+' each</font></td>');
			document.write('<td align="right" width="15%"><font face="Arial" size="2">'+FormatMoney(itemTotalPrice)+'</font></td>');
			document.write('</tr>');
		}
	}

	if	(itemCountTotal <= 0)
	{
		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=5 align="center"><font face="Arial" size="2">Cart Empty</font></td>');
		document.write('</tr>');
	}

	//Calculate shipping cost
	totalshCost = CalculateShippingCost(CKShipCountry,CKShipMethod);
	//Shipping and Handling
	if	(itemCountTotal > 0)
	{
		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=3 align="right" class="textLarge"><font face="Arial" size="2" color="blue"><b>Shipping and Handling:</b>&nbsp;&nbsp;&nbsp;</font>');
		document.write(ShipOptionText[Number(CKShipMethod)]);
		if	(Number(CKShipCountry) == ctNum-1)
		{	//Other Countries
			document.write('&nbsp;to&nbsp;your Country');
		}
		else
			if	(Number(CKShipCountry) == 0)
			{	//U.S.A.
				document.write('&nbsp;within&nbsp;');
				document.write(Country[Number(CKShipCountry)]);
			}
			else
			{	//Known International Countries
				document.write('&nbsp;to&nbsp;');
				document.write(Country[Number(CKShipCountry)]);
			}
		document.write('&nbsp;</td>');
		document.write('<td align="right" width="15%" bgcolor=#FFFFFF><font face="Arial" size="2">'+FormatMoney(totalshCost)+'</font></td>');
		document.write('</tr>');
	}

	//Coupon/Discount
	if	(CKCouponCode != "")
	{
		if	(amountTotal+totalshCost >= 200)
			cDiscount = CCD * 2;
		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=3 align="right"><font face="Arial" size="2" color="red"><b>Discount:</b>&nbsp;</font></td>');
		document.write('<td align="right" width="15%" bgcolor=#FFFFFF><font face="Arial" size="2" color="red">-'+FormatMoney(cDiscount)+'</font></td>');
		document.write('</tr>');
		amountTotal += totalshCost - cDiscount;
	}
	else
		amountTotal += totalshCost;

	//Amount Total
	if	(itemCountTotal > 0)
	{
		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=3 align="right"><font face="Arial" size="2"><b>Total Amount:</b>&nbsp;</font></td>');
		document.write('<td align="right" width="15%" bgcolor=#FFFFFF><font face="Arial" size="2">'+FormatMoney(amountTotal)+'</font></td>');
		document.write('</tr>');
	}
	else
	{
		document.write('<tr height="20" bgcolor=#FFFFFF>');
		document.write('<td colspan=3 align="right"><font face="Arial" size="2"><b>Total Amount:</b>&nbsp;</font></td>');
		document.write('<td align="right" width="15%" bgcolor=#FFFFFF><font face="Arial" size="2">'+FormatMoney(amountTotal)+'</font></td>');
		document.write('</tr>');
	}

	document.write('</table>');
	if	(itemCountTotal > 0)
		document.write('</FORM>');

	//Order shipment timeframe and remarks
	if	(itemCountTotal > 0)
	{//Have order
		if	(Number(CKShipCountry) == 0)
		{//U.S.A.
			if	(shipNextDay > 0)
			{
				document.write('<font face="Arial" size="2" color="blue">');
				document.write('*Item(s) ordered will be shipped the next business day.');
				document.write('</font>');
			}
		}
		if	(eligible == 1)
		{
				document.write('<br>');
				document.write('<font face="Arial" size="2" color="green">');
				document.write('Budget shipment will not include CDs and boxes.');
				document.write('</font>');
		}
	}
}

function CalculateShippingCost(country,shipMethod)
{
	var	totalshCost = 0;
	//Calculate the total weight
	var	totalWeight = 0;
	for (w=0; w<itemNum; w++)
	{
		if	(ProdArray[idxItem[w]].count > 0)	//Has order
			totalWeight = totalWeight + Number(ProdArray[idxItem[w]].weight) * Number(ProdArray[idxItem[w]].count);
	}
	//Calculate shipping cost
	switch (Number(country))
	{
		//For USA
		case 0:
			switch	(Number(shipMethod))
			{
				case 0:
					totalshCost = totalWeight * 0.3 - 1;	//Budget 1st Class
					break;
				case 1:
					totalshCost = totalWeight * 0.3 + 2;	//1st Class
					break;
				case 2:
					totalshCost = totalWeight * 0.3 + 3;	//Budget Priority
					break;
				case 3:
					totalshCost = totalWeight * 0.3 + 4;	//Priority
					break;
				case 4:
					totalshCost = totalWeight * 0.5 + 9;	//Express
					break;
				default:
					alert("Unknown Shipping Method selected!");
					break;
			}
			break;
		//For Canada
		case 3:
			switch	(Number(shipMethod))
			{
				case 5:
					totalshCost = totalWeight * 0.3;		//Budget 1st Class
					break;
				case 6:
					totalshCost = totalWeight * 0.3 + 3;	//Airmail
					break;
				case 7:
					totalshCost = totalWeight * 0.3 + 4;	//Budget Global Priority
					break;
				case 8:
					totalshCost = totalWeight * 0.3 + 5;	//Global Priority
					break;
				case 9:
					totalshCost = totalWeight * 0.3 + 15;	//Global Express
					break;
				default:
					alert("Unknown Shipping Method selected!");
					break;
			}
			break;
		//For Australia, Japan
		case 1:
		case 11:
			switch	(Number(shipMethod))
			{
				case 5:
					totalshCost = totalWeight * 0.3 + 2;	//Budget 1st Class
					break;
				case 6:
					totalshCost = totalWeight * 0.6 + 5;	//Airmail
					break;
				case 7:
					totalshCost = totalWeight * 0.6 + 7;	//Budget Global Priority
					break;
				case 8:
					totalshCost = totalWeight * 0.6 + 8;	//Global Priority
					break;
				case 9:
					totalshCost = totalWeight * 0.6 + 20;	//Global Express
					break;
				default:
					alert("Unknown Shipping Method selected!");
					break;
			}
			break;
		//For Others Unlisted Countries
		case 18:
			switch	(Number(shipMethod))
			{
				case 5:
					totalshCost = totalWeight * 0.3 + 1;	//Budget 1st Class
					break;
				case 6:
					totalshCost = totalWeight * 0.7 + 6;	//Airmail
					break;
				case 7:
					totalshCost = totalWeight * 0.7 + 9;	//Budget Global Priority
					break;
				case 8:
					totalshCost = totalWeight * 0.7 + 10;	//Global Priority
					break;
				case 9:
					totalshCost = totalWeight * 0.7 + 21;	//Global Express
					break;
				default:
					alert("Unknown Shipping Method selected!");
					break;
			}
			break;
		//All Other Listed Countries
		default:
			switch	(Number(shipMethod))
			{
				case 5:
					totalshCost = totalWeight * 0.3 + 2;	//Budget 1st Class
					break;
				case 6:
					totalshCost = totalWeight * 0.5 + 5;	//Airmail
					break;
				case 7:
					totalshCost = totalWeight * 0.6 + 7;	//Budget Global Priority
					break;
				case 8:
					totalshCost = totalWeight * 0.6 + 8;	//Global Priority
					break;
				case 9:
					totalshCost = totalWeight * 0.5 + 20;	//Global Express
					break;
				default:
					alert("Unknown Shipping Method selected!");
					break;
			}
			break;
	}

	return	(totalshCost);
}

function FormatMoney(amt)
{
	amt = amt.toString().replace(/\$|\,/g,'');
	if	(isNaN(amt))
		amt = "0";
	sign = (amt == (amt = Math.abs(amt)));
	amt = Math.floor(amt*100+0.50000000001);
	cents = amt%100;
	amt = Math.floor(amt/100).toString();
	if	(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((amt.length-(1+i))/3); i++)
		amt = amt.substring(0,amt.length-(4*i+3))+','+amt.substring(amt.length-(4*i+3));

	return (((sign)?'':'-') + '$' + amt + '.' + cents);
}

function RoundMoney(amt)
{
	amt = amt.toString().replace(/\$|\,/g,'');
	if	(isNaN(amt))
		amt = "0";
	sign = (amt == (amt = Math.abs(amt)));
	amt = Math.floor(amt*100+0.50000000001);
	cents = amt%100;
	amt = Math.floor(amt/100).toString();
	if	(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((amt.length-(1+i))/3); i++)
		amt = amt.substring(0,amt.length-(4*i+3))+amt.substring(amt.length-(4*i+3));

	return (amt + '.' + cents);
}

function CheckOut()
{
	var	itemCountTotal	= 0;
	for	(i=0; i<itemNum; i++)
		itemCountTotal = itemCountTotal + Number(ProdArray[idxItem[i]].count);
//	var	allcmmt = "";
	if	(itemCountTotal > 0)
	{
		document.write('<FORM action="Payment.html" method="GET">');
		document.write('<INPUT type="submit" name="checkout" value="Checkout">');
		document.write('</FORM>');
	}
}
/*
function PayNow()
{
	var	allcmmt = "";
	if	(Number(SCItemCnt2)+Number(SCItemCnt3)+Number(SCItemCnt4)+Number(SCItemCnt5) > 0)
	{
		document.write('<FORM action="https://www.linkpointcentral.com/lpc/servlet/lppay" method="post">');
//		document.write('<FORM action="https://staging.linkpt.net/lpc/servlet/lppay" method="post">');
		document.write('<INPUT type="hidden" name="storename" value="873206">');
		document.write('<INPUT type="hidden" name="mode" value="fullpay">');
		document.write('<INPUT type="hidden" name="txntype" value="sale">');
//		document.write('<table><tr><td>');
//		document.write('<select type="hidden" name="chargetotal">');
//		document.write('<option value="'+ProdPriceArray[GenList[1].subcat][GenList[1].itemnum].eSaleINTshipS+'">Airmail (5-7 days) $10');
//		document.write('<option selected value="'+ProdPriceArray[GenList[1].subcat][GenList[1].itemnum].eSaleINTshipSR+'">Register Airmail $18');
//		document.write('<option value="'+ProdPriceArray[GenList[1].subcat][GenList[1].itemnum].eSaleINTshipE+'">Express (3-5 days) $25');
//		document.write('</select></td></tr></table>');
//		document.write('<INPUT type="hidden" name="subtotal" value="125">');
//		document.write('<INPUT type="hidden" name="shipping" value="10">');
//		document.write('<INPUT type="hidden" name="taxexempt" value="1">');
		if	(Number(SCItemCnt2) > 0)	//F2A Ultra 256Mb
		{
			allcmmt += "***G"+SCList[2].subcat+"a"+SCList[2].itemnum+"("+Number(SCItemCnt2)+")";
			document.write('<INPUT type="hidden" name="sku2" value="G'+SCList[2].subcat+'a'+SCList[2].itemnum+'">');
			document.write('<INPUT type="hidden" name="itemd2" value="'+ProdNArray[SCList[2].subcat][SCList[2].itemnum].eng+'">');
			document.write('<INPUT type="hidden" name="qty2" value="'+Number(SCItemCnt2)+'">');
		}
		if	(Number(SCItemCnt3) > 0)	//F2A Ultra 512Mb
		{
			allcmmt += "***G"+SCList[3].subcat+"a"+SCList[3].itemnum+"("+Number(SCItemCnt3)+")";
			document.write('<INPUT type="hidden" name="sku3" value="G'+SCList[3].subcat+'a'+SCList[3].itemnum+'">');
			document.write('<INPUT type="hidden" name="itemd3" value="'+ProdNArray[SCList[3].subcat][SCList[3].itemnum].eng+'">');
			document.write('<INPUT type="hidden" name="qty3" value="'+Number(SCItemCnt3)+'">');
		}
		if	(Number(SCItemCnt4) > 0)	//F2A Ultra 1Gb
		{
			allcmmt += "***G"+SCList[4].subcat+"a"+SCList[4].itemnum+"("+Number(SCItemCnt4)+")";
			document.write('<INPUT type="hidden" name="sku4" value="G'+SCList[4].subcat+'a'+SCList[4].itemnum+'">');
			document.write('<INPUT type="hidden" name="itemd4" value="'+ProdNArray[SCList[4].subcat][SCList[4].itemnum].eng+'">');
			document.write('<INPUT type="hidden" name="qty4" value="'+Number(SCItemCnt4)+'">');
		}
		if	(Number(SCItemCnt5) > 0)	//GBA TV
		{
			allcmmt += "***G"+SCList[5].subcat+"a"+SCList[5].itemnum+"("+Number(SCItemCnt5)+")";
			document.write('<INPUT type="hidden" name="sku2" value="G'+SCList[5].subcat+'a'+SCList[5].itemnum+'">');
			document.write('<INPUT type="hidden" name="itemd2" value="'+ProdNArray[SCList[5].subcat][SCList[5].itemnum].eng+'">');
			document.write('<INPUT type="hidden" name="qty2" value="'+Number(SCItemCnt5)+'">');
		}
		document.write('<INPUT type="hidden" name="shipment" value="'+ShipOptionText[SCShipping]+'">');
		document.write('<INPUT type="hidden" name="comments" value="'+allcmmt+'***S&H='+SCShipping+'">');
		document.write('<INPUT type="hidden" name="chargetotal" value="'+RoundMoney(totalAmt)+'">');
		document.write('<INPUT type="submit" name="Payment" value="Pay Now">');
		document.write('</FORM>');
	}
}
*/
function PayNowPP()
{
	var	itemCountTotal	= 0;
	var	amountTotal		= 0;
	var	totalshCost		= 0;
	var	x				= 0;
	var	cDiscount		= 0;

	for	(i=0; i<itemNum; i++)
	{
		if	(ProdArray[idxItem[i]].count > 0)	//Only count for any item ordered. Total amount with Qty will be calculated thru Paypal
			itemCountTotal += 1;
	}

	if	(itemCountTotal > 0)
	{
		//Calculate shipping cost
		totalshCost = CalculateShippingCost(CKShipCountry,CKShipMethod);
//		document.write('<FORM target=_new action="https://www.paypal.com/cgi-bin/webscr" method="post">');
		document.write('<FORM target=_parent method="post" action="https://www.paypal.com/cgi-bin/webscr">');
		if	(Number(CKShipCountry) == 0)	//U.S.A. Customer
			document.write('<INPUT type="hidden" name="business" value="epayment@goodylands.com">');
		else								//International Customer
			document.write('<INPUT type="hidden" name="business" value="epayments@goodylands.com">');
		if	(itemCountTotal == 1)
		{//Only ordered one item with Qty=quantity
			document.write('<INPUT type="hidden" name="cmd" value="_xclick">');
			//Construct the only one item ordered
			for	(i=0; i<itemNum; i++)
			{
				if	(ProdArray[idxItem[i]].count > 0)
				{
//					itemName = ProdArray[idxItem[i]].name+" @$"+ProdArray[idxItem[i]].price+" ea";
					document.write('<INPUT type="hidden" name="item_name" value="'+ProdArray[idxItem[i]].name+'">');
					document.write('<INPUT type="hidden" name="quantity" value="'+Number(ProdArray[idxItem[i]].count)+'">');
					document.write('<INPUT type="hidden" name="item_number" value="'+ProdArray[idxItem[i]].SKU+'">');
					document.write('<INPUT type="hidden" name="amount" value="'+ProdArray[idxItem[i]].price+'">');
					document.write('<input type="hidden" name="on0" value="Each">');
					document.write('<INPUT type="hidden" name="os0" value="@ $'+ProdArray[idxItem[i]].price+'">');
					document.write('<input type="hidden" name="on1" value="S&H">');
					document.write('<INPUT type="hidden" name="os1" value="'+ShipOptionText[Number(CKShipMethod)]+'">');
					amountTotal = Number(ProdArray[idxItem[i]].price) * Number(ProdArray[idxItem[i]].count);
				}
			}
			if	(CKCouponCode != "")
			{
				if	(amountTotal+totalshCost >= 200)
					cDiscount = CCD * 2;
				document.write('<INPUT type="hidden" name="shipping" value="'+RoundMoney(totalshCost-cDiscount)+'">');
			}
			else
				document.write('<INPUT type="hidden" name="shipping" value="'+RoundMoney(totalshCost)+'">');
		}
		else
		{//Order more than one item with Qty_x=quantity
			document.write('<INPUT type="hidden" name="cmd" value="_cart">');
			document.write('<INPUT type="hidden" name="upload" value="1">');
			//Construct all items ordered
			for	(i=0; i<itemNum; i++)
			{
				if	(ProdArray[idxItem[i]].count > 0)
				{
					x += 1;
//					itemName = ProdArray[idxItem[i]].name+" @$"+ProdArray[idxItem[i]].price+" ea";
					document.write('<INPUT type="hidden" name="item_name_'+x+'" value="'+ProdArray[idxItem[i]].name+'">');
					document.write('<INPUT type="hidden" name="quantity_'+x+'" value="'+Number(ProdArray[idxItem[i]].count)+'">');
					document.write('<INPUT type="hidden" name="item_number_'+x+'" value="'+ProdArray[idxItem[i]].SKU+'">');
					document.write('<INPUT type="hidden" name="amount_'+x+'" value="'+ProdArray[idxItem[i]].price+'">');
					document.write('<input type="hidden" name="on0_'+x+'" value="Each">');
					document.write('<INPUT type="hidden" name="os0_'+x+'" value="@ $'+ProdArray[idxItem[i]].price+'">');
					document.write('<input type="hidden" name="on1_'+x+'" value="S&H">');
					document.write('<INPUT type="hidden" name="os1_'+x+'" value="'+ShipOptionText[Number(CKShipMethod)]+'">');
					amountTotal += Number(ProdArray[idxItem[i]].price) * Number(ProdArray[idxItem[i]].count);
				}
			}
			if	(CKCouponCode != "")
			{
				if	(amountTotal+totalshCost >= 200)
					cDiscount = CCD * 2;
				document.write('<INPUT type="hidden" name="shipping_'+x+'" value="'+RoundMoney(totalshCost-cDiscount)+'">');
			}
			else
				document.write('<INPUT type="hidden" name="shipping_'+x+'" value="'+RoundMoney(totalshCost)+'">');
		}
		document.write('<INPUT type="hidden" name="page_style" value="Goodylands">');
		document.write('<INPUT type="hidden" name="return" value="http://www.goodylands.com/ThankYou.html">');
		document.write('<INPUT type="hidden" name="cancel_return" value="http://www.goodylands.com">');
		document.write('<INPUT type="hidden" name="currency_code" value="USD">');
		document.write('<INPUT type="submit" name="Payment" value="Pay Now">');
		document.write('</FORM>');
	}

}

