//The code in this file is copyright © Creative Technology 2000.  All rights are reserved.
//The ordering system uses the hard-coded cookie called ct_basket, which has the format
// number_of_items | item_ordercode ^ quantity | item_ordercode ^ quantity (etc)
//JS Design file
//This file contains routines for formatting & display of shopping cart data.

//------THESE ROUTINES DEAL WITH CATALOGUE-SPECIFIC DATA

//----------This section is shopping-cart display and handling - Order Form URL at the end

function ct_show_pricelist(cat_page){
//Displays product list with Buy button
//MUST be called inside bare Table tag
	var startrow = '<tr>';
	var endrow = '</tr>';
	var endcell = '</font></td>';
	var starttitlecell = '<td align="center" rowspan="2" valign="top"><font face="Geneva, Verdana, Helvetica, Arial, Helvetica, Arial" size="1"><b>';
	var startleftcell = '<td align="left"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var startcentercell = '<td align="center"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var startrightcell = '<td align="right"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var starteurocell = '<td align="right" bgcolor="#AAAAFF"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var startdollarcell = '<td align="right" bgcolor="#FFAAAA"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var startpoundcell = '<td align="right" bgcolor="#FFFFFF"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';

	var i = 0;
	//Write the table header
		document.write(startrow);
//		document.write(starttitlecell + 'Code' + endcell);
		document.write(starttitlecell + '&nbsp;' + endcell);
		document.write('<td colspan="3" align="center"><font face="Geneva, Verdana, Helvetica, Arial" size="1"><b>Price per Clock' + endcell);
//		document.write(starttitlecell + 'Quantity' + endcell);
		document.write(starttitlecell+'&nbsp;' + endcell)
		document.write(endrow);

		document.write(startrow);
		document.write(startpoundcell + '&pound;' + endcell)
		document.write(startdollarcell + 'US$'  + endcell);
		document.write(starteurocell + 'Euro'  + endcell);
		document.write(endrow);

//		document.write('<form name="quantities" action="javascript:;">
	//now each product as a row in the list
	for (i=1;i<ProductList.length; i++) {
		if (ProductList[i].CatPage == cat_page)//cat_page hard-coded in header of each page which shows catalogue info.
		{
		document.write(startrow);
		document.write(startleftcell + ProductList[i].Description + endcell);
		document.write(startpoundcell + currencystring(ProductList[i].Price) + endcell);
		document.write(startdollarcell + currencystring((ProductList[i].Price) * dollar_rate) + endcell);
		document.write(starteurocell + currencystring((ProductList[i].Price)* euro_rate) + endcell);
//Wdith of button cell is 44 - padding set in page is 6
		document.write('<td width="44"><a href="javascript:ct_buy_button(\'' + ProductList[i].OrderCode + '\',\'1\')">');
		document.write('<img src="but_buy.gif" width="32" height="25" border="0" alt="Click here to add this item to your Shopping Basket"></a>' + endcell);
		document.write(endrow);
		}
	}

}

function ct_show_basket(){
//Displays shopping basket
//MUST be called inside bare Table tag
	var startrow = '<tr>';
	var endrow = '</tr>';
	var starttitlecell = '<td align="center" rowspan="2" valign="top"><font face="Geneva, Verdana, Helvetica, Arial" size="1"><b>';
	var startleftcell = '<td align="left"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var startcentercell = '<td align="center"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var startrightcell = '<td align="right"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var starteurocell = '<td align="right" bgcolor="#AAAAFF"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var startdollarcell = '<td align="right" bgcolor="#FFAAAA"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var startpoundcell = '<td align="right" bgcolor="#FFFFFF"><font face="Geneva, Verdana, Helvetica, Arial" size="1">';
	var endcell = '</font></td>';
	var emailconfirm='';
	var totalvalue=0;
	var isCookie = ct_readCookie();
//	alert(isCookie);
	if (isCookie != null)	{	
//		alert('doing it anyway');
		var myproducts = ct_fill_product_array();
		var myquantities = ct_fill_quantity_array();
		var numitems = ct_get_number_of_products()


		//Write the table header
		document.write(startrow);
		document.write(starttitlecell + 'Description' + endcell);
		document.write(starttitlecell + 'Price (&pound)' + endcell);
		document.write(starttitlecell + 'Quantity'  + endcell);
		document.write('<td colspan="3" align="center"><font face="Geneva, Verdana, Helvetica, Arial" size="1"><b>Item Total' + endcell)
		document.write(starttitlecell+'&nbsp;' + endcell)
		document.write(endrow);

		document.write(startrow);
		document.write(startpoundcell + '&pound;' + endcell)
		document.write(startdollarcell + 'US$'  + endcell);
		document.write(starteurocell + 'Euro'  + endcell);
		document.write(endrow);

		//now each basketitem as a row in the list
		for (var i=1;i<=numitems; i++) {
			myitem = ct_get_basket_item(myproducts[i]);
			totalvalue = totalvalue + (myquantities[i] * myitem.Price);
	//Leave a gap
			document.write(startrow + '<td colspan="7"><img src="trans.gif" height="3"></td>' + endrow);

			document.write(startrow);
	//		document.write(startleftcell + myitem.OrderCode + endcell);
	//		document.write(starttitlecell + '&nbsp;' + endcell);
			document.write(startleftcell + myitem.Description + endcell);
			document.write(startpoundcell + currencystring(myitem.Price) + endcell);
			document.write(startcentercell + myquantities[i] + endcell);
			document.write(startpoundcell + currencystring(parseInt(myquantities[i]) * myitem.Price) + endcell);
			document.write(startdollarcell + currencystring(parseInt(myquantities[i]) * myitem.Price * dollar_rate)  + endcell);
			document.write(starteurocell + currencystring(parseInt(myquantities[i]) * myitem.Price * euro_rate)  + endcell);
			document.write(startcentercell + '<a href="javascript:ct_delete_button(' + i + ')">');
			document.write('<img src="but_delete.gif" border="0"width="32" height="25" border="0" alt="Click here to delete this item from your Shopping Basket"></a>' + endcell);

			document.write(endrow);
			//build email confirm string
			emailconfirm += 'Code: ' + myitem.OrderCode + ' Item: ' + myquantities[i] + ' x ' + myitem.Description + ': &pound; ' + currencystring(parseInt(myquantities[i]) * myitem.Price) + '\n';

	//Leave a gap
			document.write(startrow + '<td colspan="8"><img src="trans.gif" height="3"></td>' + endrow);
			}
		
	//SHIPPING - do at checkout only.  
		if (ct_checkout)	{
				document.write(startrow);
		
	//First - work it out
			var shippingamount=get_shipping();
			totalvalue = totalvalue + shippingamount;
	//display it			
			if (shippingamount == 0) {
				document.write('<td colspan="3" align="left" bgcolor="#FFFFFF"><font face="Geneva, Verdana, Helvetica, Arial" size="1">');
				document.write('SHIPPING</td>');
				document.write('<td colspan="3"><font face="Geneva, Verdana, Helvetica, Arial" size="1">Please contact us for shipping prices to your country</td><td></td>')
				}
			else{
				document.write('<td colspan="3" align="right" bgcolor="#FFFFFF"><font face="Geneva, Verdana, Helvetica, Arial" size="1">');
				document.write('Shipping</td>');
				document.write(startpoundcell + currencystring(shippingamount) + endcell);
				document.write(startdollarcell + currencystring(shippingamount * dollar_rate) + endcell);
				document.write(starteurocell + currencystring(shippingamount * euro_rate) + endcell);
				document.write('<td></td>');
				}
			document.write(endrow)
		}//END OF SHIPPING

	//Leave a gap
			document.write(startrow + '<td colspan="8"><img src="trans.gif" height="3"></td>' + endrow);

	//now the total...
			document.write(startrow);
			document.write('<td colspan="3" align="right" bgcolor="#FFFFFF"><font face="Geneva, Verdana, Helvetica, Arial" size="1">');
			document.write('TOTAL ORDER VALUE<BR><font size="1">(Excluding taxes)</td>');
			document.write(startpoundcell + currencystring(totalvalue) + endcell);
			document.write(startdollarcell + currencystring(totalvalue * dollar_rate) + endcell);
			document.write(starteurocell + currencystring(totalvalue * euro_rate) + endcell);

	//if we're not at the checkout, put a checkout button in here
			if (ct_checkout){
				document.write('<td></td>' + endrow)}
			else{
				document.write(startrightcell + '<a href="javascript:checkout()"><img src="but_checkout.gif" border="0"width="32" height="25" border="0" alt="Click here to go to the Secure Checkout page"></a>' + endcell + endrow);
			}

			//this bit only if at secure server
			if (ct_checkout){
				//leave a gap
				document.write(startrow + '<td colspan="8"><img src="trans.gif" height="10"></td>' + endrow);
			//Now the VAT
				var VATamount;
				if (getVATstatus()) {VATamount=totalvalue*0.175} else {VATamount=0};
				//Leave a gap, the do VAT
				document.write(startrow + '<td colspan="8"><img src="trans.gif" height="10"></td>' + endrow);
				document.write('<td colspan="3" align="right" bgcolor="#FFFFFF"><font face="Geneva, Verdana, Helvetica, Arial" size="1">');
				document.write('For customers in the European Community, add VAT at 17.5%</td>');
				document.write(startpoundcell + currencystring(VATamount) + endcell);
				document.write(startdollarcell + currencystring(VATamount * dollar_rate) + endcell);
				document.write(starteurocell + currencystring(VATamount * euro_rate) + endcell);
				document.write('<td></td>'+endrow);
				//Leave a gap, then grand total
				document.write(startrow + '<td colspan="8"><img src="trans.gif" height="10"></td>' + endrow);
				document.write('<td colspan="3" align="right" bgcolor="#FFFFFF"><font face="Geneva, Verdana, Helvetica, Arial" size="1">');
				document.write('GRAND TOTAL</td>');
				document.write(startpoundcell + currencystring(totalvalue+VATamount) + endcell);
				document.write(startdollarcell + currencystring((totalvalue+VATamount) * dollar_rate) + endcell);
				document.write(starteurocell + currencystring((totalvalue+VATamount) * euro_rate) + endcell);
				document.write('<td></td>'+endrow);

			}
//customer email confirm string
	document.write ('<input type="hidden" name="emailconfirm" value="' + emailconfirm + '">');

	}
	else document.write(startrow + startleftcell + '<font size="2"><b>No Items in Basket</b></font>' + endcell + endrow);
}


// Secure Server stuff - mostly getting basket data into hidden orderform objects

function setcountry(){
	document.forms[0].country.selectedIndex = parseInt(getcountryindex());
}

function get_shipping_factor(){
			var shipping_factor=0;
			//reads the countryindex cookie, which defaults to defaultcountryindex
			mycountry=getcountryindex();
			if (mycountry == 0){
				alert('Error - cannot calculate shipping factor - no country cookie');
				return 0
			}
			else {
				if (mycountry==195){shipping_factor = 1;}//UK
				else {
					if (getVATstatus()){shipping_factor=shipping_ec;}//Other EC countries - cheat by using VAT routine
					else {
						if (mycountry==198 || mycountry==37){shipping_factor=shipping_us}//USA or Canada
						else {
							if (mycountry==101 || mycountry==12 || mycountry==139){shipping_factor=shipping_aus} //Australia, NZ, Japan
							else {shipping_factor = shipping_other}
							 }
						  }
					 }
			    }
		return shipping_factor
}

function setformvars(){
//sets hidden pricing and date vars in form
	var isCookie = ct_readCookie();
	if (isCookie != null){
		var now= new Date();
		var mydate = now.toGMTString();
		var myproducts = ct_fill_product_array();
		var myquantities = ct_fill_quantity_array();
		var numitems = ct_get_number_of_products();
		var	ct_order_items = numitems + ' Item(s):';
			for (var i=1;i<=numitems; i++) {
				ct_order_items += myproducts[i] + ' x ' + myquantities[i] + ', ';
			}

		document.forms[0].Date.value = mydate;
		document.forms[0].item_total.value = currencystring(get_total());
		document.forms[0].shipping.value = currencystring(get_shipping());
		document.forms[0].vat.value = currencystring(get_VAT());
		document.forms[0].order_total.value = currencystring(get_grand_total());
		document.forms[0].order_items.value = ct_order_items;
	}
	else alert('Your shopping basket is empty!')
}

function show_form(){
	for (var i=0; i<document.forms[0].elements.length ;i++ ){
		var mystring = document.forms[0].elements[i].name + '=' + document.forms[0].elements[i].value;
		alert(mystring);	
	}
}


//shopping checkout secure site bit

//Default country - 0 is none, 195 is UK
var defaultcountryindex = 195 
var defaultcountryvalue = 'UK' //UK is default country for Malkin - used in getcountryindex() in cookies.js
var testing = false;
var order_form = 'http://192.168.1.4/secure/malkin/checkin.htm'; 

if (!testing){
	var order_form = 'https://secure.hotpotatoes.net/malkin/checkin.htm'; //MUST INCLUDE https:// if reqd - used by checkout routine in cookies.js
}
