// CART REVISIONS
/*	Cart version 1.04

	10/05/02	- popupConvert() altered to let user choose currency if they click the
				  "curreny convertor" link, not the "check todays rate" links

				- Using images instead of wingdings font
				  (there's no point keeping it - Kids, wingdings are bad, mmkay?)

				- VAT Floating point error sorted (again)

				 was using-
					 vatcost = (priceFormat(vatcost)*quant);
				 now using-
					 vatcost = (priceFormat(vatcost)*quant);
					 vatcost = priceFormat(vatcost);

	21/12/01	- popupConvert() altered to accomodate different currencies
				  now pass popupConvert([cur]), cur can be any valid 3 letter
				  currency code eg; 'USD' or 'EUR'

				- Currency conversion rates are now able to cope with non-decimal
				  values, I simply priceFormatted it!!

	29/11/01 	- Floating point error on calculating VAT resolved
						(VAT now calculated before quantity applied)
				UPDATE To Above: This didn't work, see 10/05/02 note above

				- Can no longer select a delivery location until cart is loaded

				- Rounding error on summing is corrected

	27/11/01 	- Define font colors for the cart to contrast with the stripes

*/

var exchRate = 1.44; //USD to pound
var euroRate = 1.62; //euro to pound
var prodvat = 0;
var bgcolorStripe1 = "#FFFFFF"; // these dictate the color of the cart
var fontColor1 = "#6666AA"; // these dictate the color of the cart
var bgcolorStripe2 = "#6666AA"; // these dictate the color of the cart
var fontColor2 = "#FFFFFF"; // these dictate the color of the cart
var numItems = 0;
var thisText = "";
var bgcolor = bgcolorStripe1;
var fontColor = fontColor1;
var requireColour = 0;
var requireSize = 1;
var safeFlag = false;

tempArray = new Array(20);

function setTempArray()
{
	for(w=0;w<20;w++)
	{
		tempArray[w] = 0;
	}
	return;
}

setTempArray();

function hRule()
{
	if(requireColour && requireSize)
	{
		document.write('<td colspan="9"><hr></td>');
	}
	else if(requireColour || requireSize)
	{
		document.write('<td colspan="8"><hr></td>');
	}
	else
	{
		document.write('<td colspan="7"><hr></td>');
	}
}

function makeSpaces()
{
	if(requireColour)
	{
		document.write('<td>&nbsp;</td>');
	}

	if(requireSize)
	{
		document.write('<td>&nbsp;</td>');
	}
}

function popupConvert(cur)
{
	if(cur=="USD")
	{
		cur = "&To=USD";
	} else if(cur=="EUR")
	{
		cur = "&To=EUR";
	} else {
		cur = "";
	}

	priceToConvert = document.orderform.grandtotal.value;
    if(priceToConvert == 0)
    {
		priceToConvert = 1;
	}
	CurrencyWindow = window.open ('http://www.xe.net/ecc/input.cgi?Template=sw&Amount=' + priceToConvert + '&From=GBP'+cur, 'CurrencyWindow', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,height=170,width=600');
	CurrencyWindow.focus();
}

function listCheck(theForm,what,theString)
{
	setTempArray();

	if(theString) // no need to set theString in every cart
	{
		tempArray = theString.split('|');
	}

	theIndex = what.selectedIndex;
	addprice = tempArray[theIndex];
	theName = what.name + "price";
	theForm[theName].value = priceFormat(addprice);
	theTotal = ((theForm.basicprice.value*100) + (theForm.descprice.value*100) + (theForm.sizeprice.value*100) + (theForm.colourprice.value*100))/100;
	theForm.price.value = priceFormat(theTotal);
}

function checkout(what)
{
	mycookies=new Array(20);
	var i=0;
	subtotal=0;
	var startpoint=0;
	whole_len=document.cookie.length;

	while(whole_len!=startpoint&&i<20)
	{
		if((temlen=document.cookie.indexOf(";",startpoint))!=-1)
		{
			mycookies[i]=document.cookie.substring(startpoint,temlen);
			i++;
			startpoint=temlen+1
		}
		else if(startpoint!=whole_len)
		{
			mycookies[i]=document.cookie.substring(startpoint,whole_len);
			i++;
			startpoint=whole_len;
		}
	}

	for(j=0;j<i;j++)
	{
		if((mycookies[j].substring(0,3) == "FTS") || (mycookies[j].substring(0,4) == " FTS") || (mycookies[j].substring(0,6) == " FTS"))
		{
			subtotal=getCookie(mycookies[j],what);
		}
	}

	if(what=="hidden")
	{
		subtotal = priceFormat(subtotal);
		document.write("<input type=hidden name=total value="+subtotal+">");
		document.write("<input type=hidden name=numItems value="+ numItems +">");
		document.write("<input type='hidden' name='requireColour' value='" + requireColour + "'>");
		document.write("<input type='hidden' name='requireSize' value='" + requireSize + "'>");
	}
}

function getCookie(acookie,what)
{
	clen = acookie.length;
	nlen = acookie.indexOf("=",0);
	if(acookie.substring(0,1) == " ")
	{
		cname = acookie.substring(6,nlen);
	}
	else
	{
		cname = acookie.substring(4,nlen);
	}

	bname = acookie.substring(nlen+1,clen);
	sname_len = cname.indexOf("+",0);
	store = cname.substring(0,sname_len);
	dlen=cname.indexOf("+",sname_len+1);
	descrip = cname.substring(cname.indexOf("|D|",0),dlen);

	if(descrip.substring(0,1) == " ")
	{
		descrip = descrip.substring(1,descrip.length);
	}

	product = cname.substring(dlen+1,nlen);
	vatrate = cname.substring(sname_len+1,cname.indexOf(" ",sname_len+1));
	vatrate = parseFloat(vatrate);
	qlen = bname.indexOf("+",0);
	rlen = bname.indexOf("+",qlen+1);
	quant = bname.substring(0,qlen);

	if(quant==0)
	{
		document.cookie = cname+"=bb; expires=Saturday, 16-Sep-90 23:59:59 GMT; path=/"+"";
	}

	refer = bname.substring(qlen+1,rlen);
	price = bname.substring(rlen+1,clen-nlen);
	price = priceFormat(price);
	itemcost = quant*price;
	itemcost = priceFormat(itemcost);
	vatcost = price*vatrate/100;

	vatcost = (priceFormat(vatcost)*quant);
	vatcost = priceFormat(vatcost);

	if(quant!=0 && what=="hidden") // prevent double count of VAT
	{
		subtotal+=price*quant;
		prodvat+=vatcost;
	}

	printcontent(what);
	return subtotal;
}

function printcontent(what)
{
	if(quant!=0)
	{
		vatrate = parseFloat(vatrate);
		itemcost = quant*price;
		itemcost = priceFormat(itemcost);
		vatcost = price*vatrate/100;
		vatcost = (priceFormat(vatcost)*quant);
		vatcost = priceFormat(vatcost);

		// SPLIT the descrip field into bits!

		descPart = descrip.indexOf("|D|",0);
		colorPart = descrip.indexOf("|C|",0);
		sizePart = descrip.indexOf("|S|",0);

		showDesc = descrip.substring(descPart+3,colorPart);
		showColour = descrip.substring(colorPart+3,sizePart);
		showSize = descrip.substring(sizePart+3,descrip.length);

		if(what=="display")
		{
			if(bgcolor == bgcolorStripe1)
			{
				bgcolor = bgcolorStripe2;
				fontColor = fontColor2;
			}
			else // alternate row colors
			{
				bgcolor = bgcolorStripe1;
				fontColor = fontColor1;
			}

			numItems = j + 1;

			var upOne = parseInt(quant) + 1;
			var downOne = parseInt(quant) - 1;

			thisText = ("\n<form name='store" + j +"'>")
			+ ("\n\t<tr bgcolor='" + bgcolor + "'>")
			+ ("\n\t\t<td class=cart><font color='" + fontColor + "'>" + product + "</font></td>\n\t\t<td class=cart><font color='" + fontColor + "'>&nbsp;" + showDesc + "</font></td>");

			if(requireColour)
			{
				thisText = thisText + "\n\t\t<td class=cart><font color='" + fontColor + "'>&nbsp;" + showColour + "</font></td>";
			}

			if(requireSize)
			{
				thisText = thisText + "\n\t\t<td class=cart><font color='" + fontColor + "'>&nbsp;" + showSize + "</font></td>";
			}

			thisText = thisText + ("\n\t\t<td class=cart><font color='" + fontColor + "'>" + quant + "</font></td>\n\t\t<td class=cart><font color='" + fontColor + "'>&pound;" + price + "</font></td>\n\t\t<td class=cart><font color='" + fontColor + "'>&pound;" + itemcost + "</font></td>\n\t\t<td class=cart><font color='" + fontColor + "'>&pound;" + vatcost + "</font></td>")
			+ ("\n\t\t<td class=cart align='left'>")
			+ ("<b><A href='javascript:document.store" + j +".quantity.value = " + upOne + "; addto(document.store" + j +");self.location=self.location;' onmouseout='status=\"\"; return true;' onmouseover='status=\"Increase Quantity\"; return true;'><img src='./images/increase.gif' border='0' alt='increase quantity'></A>")
			+ ("&nbsp;&nbsp;&nbsp;<A href='javascript:document.store" + j +".quantity.value = " + downOne + "; addto(document.store" + j +");self.location=self.location;' onmouseout='status=\"\"; return true;' onmouseover='status=\"Decrease Quantity\"; return true;'><img src='./images/decrease.gif' border='0' alt='decrease quantity'></A></b>")
			+ ("&nbsp;&nbsp;&nbsp;<A href='javascript:document.store" + j +".quantity.value = 0; addto(document.store" + j +");self.location=self.location;' onmouseout='status=\"\"; return true;' onmouseover='status=\"Remove from Cart\"; return true;'><img src='./images/delete.gif' border='0' alt='delete'></A>&nbsp;&nbsp;&nbsp;</font></td>")
			+ ("\n\t<td class=cart bgcolor=white><input type='hidden' name='quantity' value='" + quant + "'>")
			+ ("<input type='hidden' name='FTShop' value='" + product + "'>")
			+ ("<input type='hidden' name='price' value='" + price + "'>")
			+ ("<input type='hidden' name='VATrate' value='" + vatrate + "'>")
			+ ("<input type='hidden' name='desc' value='" + showDesc + "'>")
			+ ("<input type='hidden' name='colour' value='" + showColour + "'>")
			+ ("<input type='hidden' name='size' value='" + showSize + "'>")
			+ ("<input type='hidden' name='store' value='FTShop'></td>")
			+ ("\n\t</tr>\n</form>");

			document.write(thisText);
		}
		else if(what=="hidden")
		{
			thisText = ("\n<INPUT type='hidden' name='product[" + j + "]' value='" + product + "'>")
			+ ("\n<INPUT type='hidden' name='descrip[" + j + "]' value='" + showDesc + "'>")
			+ ("\n<INPUT type='hidden' name='colour[" + j + "]' value='" + showColour + "'>")
			+ ("\n<INPUT type='hidden' name='size[" + j + "]' value='" + showSize + "'>")
			+ ("\n<INPUT type='hidden' name='quant[" + j + "]' value='" + quant + "'>")
			+ ("\n<INPUT type='hidden' name='price[" + j + "]' value='" + price + "'>")
			+ ("\n<INPUT type='hidden' name='itemcost[" + j + "]' value='" + itemcost + "'>")
			+ ("\n<INPUT type='hidden' name='vatrate[" + j + "]' value='" + vatrate + "'>")
			+ ("\n<INPUT type='hidden' name='vatcost[" + j + "]' value='" + vatcost + "'>");

			document.write(thisText);
		}
	}
}


function priceFormat(tmpprice)
{
	dot=tmpprice.toString().indexOf(".");
	if(dot!=-1)
	tmpprice=tmpprice.toString().substr(0,dot+4);
	tmpprice=Math.round(tmpprice*100)/100;
	dot=tmpprice.toString().indexOf(".");
	if(dot==-1)
	tmpprice=tmpprice.toString()+".00";
	else if((tmpprice.toString().length-dot)==2)
	tmpprice=tmpprice.toString()+"0";

	return tmpprice;
}


function addto(entry)
{
	var description;

	entry.quantity.value = stripNonNumbers(entry.quantity.value);

	if(entry.quantity.value)
	{
		if(entry.desc)
		{
			if(!entry.desc.value) // check to see if select used
			{
				thisDesc = entry.desc.options[entry.desc.selectedIndex].text;
			}
			else
			{
				thisDesc = entry.desc.value;
			}
		} else {
			thisDesc = " ";
		}

		if(entry.colour)
		{
			if(!entry.colour.value) // check to see if select used
			{
				thisColour = entry.colour.options[entry.colour.selectedIndex].text;
			}
			else
			{
				thisColour = entry.colour.value;
			}
		} else {
			thisColour = " ";
		}

		if(entry.finish)
		{
			if(!entry.finish.value) // check to see if select used
			{
				thisSize = entry.finish.options[entry.finish.selectedIndex].text;
			}
			else
			{
				thisSize = entry.finish.value;
			}
		} else {
			thisSize = " ";
		}


		description = entry.VATrate.value + " |D|" + thisDesc + "|C|" + thisColour + "|S|" + thisSize;

		store_name = entry.store.value;
		product_name = (entry.FTShop.value) ? entry.FTShop.value : entry.FTShop.options[entry.FTShop.selectedIndex].text;
		if(product_name=="")
		{
			alert("Please select a color");
			return;
		}
		qty = entry.quantity.value;
		price = entry.price.value;

		visDesc = thisDesc;

		if(visDesc != "" && visDesc != " ")
		{
			visProd = entry.FTShop.value + " (" + visDesc + ")";
		}
		else
		{
			visProd = entry.FTShop.value;
		}

		document.cookie ="FTS" + store_name + "+" + description + "+" + product_name + "=" + qty + "++" + price + "";

		if(qty==1)
		{
			alert(qty + ' x ' + visProd + '\nhas been placed in your shopping cart.\nThank you!');
		}
		else if(qty>1)
		{
			alert(qty + ' x ' + visProd + '\nhave been placed in your shopping cart.\nThank you!');
		}
		else if(qty<1)
		{
			alert('All ' + visProd + '\nare now removed from your shopping cart\nThank you!');
		}
	}
	else
	{
		entry.quantity.value = 0;
	}
}

function stripNonNumbers (InString)
{
	// DO NOT CHANGE ANY OF CODE BELOW!!
	OutString="";
	for (Count=0;Count<InString.length;Count++)
	{
		TempChar=InString.substring (Count,Count+1);
		Strip=false;
 		CharString="0123456789.";
 		// included . so we can have 1.25 metres, etc.
		for (Countx=0;Countx<CharString.length;Countx++)
		{
			StripThis=CharString.substring (Countx,Countx+1)
			if (TempChar==StripThis)
			{
				Strip=true;
				break;
			}
		}
		if (Strip)
		OutString=OutString+TempChar;
	}
	return (OutString);
	// DO NOT CHANGE ABOVE CODE
}

function insertValues()
{
	document.orderform.subtotal.value = subtotal;
	safeFlag = true;
	document.orderform.shipping.focus();
}

function ship(shipop)
{
	if(safeFlag)
	{
		switch(shipop)
		{
			case 2:
				document.orderform.shippingcost.value = priceFormat(0.00);
				break;

			case 3:
				document.orderform.shippingcost.value = priceFormat(0.00);
				break;

			case 4:
				document.orderform.shippingcost.value = priceFormat(0.00);
				break;

			// ADD MORE CASES AS REQUIRED

			default:
				document.orderform.netvalue.value = "";
				document.orderform.grandtotal.value = "";
				document.orderform.dollartotal.value = "";
				document.orderform.eurototal.value = "";
				document.orderform.shippingvat.value = "";
				document.orderform.totalvat.value = "";
				document.orderform.shippingcost.value = "";
				alert('Please select a valid location');
				document.orderform.shipping.focus();
				return;
		}

		if(shipop == 2 || shipop == 3)
		{
			//apply VAT ONLY if UK or EU

			document.orderform.shippingvat.value = priceFormat((parseInt(document.orderform.shippingcost.value * 17.5))/100);

			document.orderform.totalvat.value = priceFormat(parseFloat(prodvat) + parseFloat(document.orderform.shippingvat.value));
		}
		else
		{
			document.orderform.shippingvat.value = "0.00";
			document.orderform.totalvat.value = "0.00";
		}

		if(shipop > 1)
		{
			document.orderform.netvalue.value = priceFormat(parseFloat(document.orderform.shippingcost.value) + parseFloat(document.orderform.subtotal.value));

			document.orderform.grandtotal.value = priceFormat(parseFloat(document.orderform.netvalue.value) + parseFloat(document.orderform.totalvat.value));

			document.orderform.dollartotal.value = priceFormat(parseInt(document.orderform.grandtotal.value * 100) * exchRate /100);

			document.orderform.eurototal.value = priceFormat(parseInt(document.orderform.grandtotal.value * 100) * euroRate /100);
		}
	}
	else
	{
		document.orderform.shipping.selectedIndex = 0;
	}
}

function dollars()
{
	document.write('Approximate order total in $US ($' + exchRate + ':&pound;1.00):');
	document.orderform.exchrate.value = priceFormat(exchRate);
}

function euros()
{
	document.write('Approximate order total in Euros (&euro;' + euroRate + ':&pound;1.00):');
	document.orderform.eurorate.value = priceFormat(euroRate);
}

function preSubmit()
{
	//if() // have they ordered anything?
	//{
		if(document.orderform.shipping.selectedIndex > 1)
		{
			document.orderform.submit();
		}
		else
		{
			alert("Please select your shipping location.");
			document.orderform.shipping.focus();
		}
	//}
}
