//
// Shopping cart functions, using product object instead of
//	parallel arrays.
//
// (c)2001 Electronic Media Group - www.emgnet.co.uk
//

// Array to hold all objects currently in the cart

var cart = new Array();		// Main cart, holds all current items.

// General Stuff

var viewf = parent.mainFrame;	// Main Item display frame
// var totf = parent.FOOTFRAME;	// Place where count so far goes

// Vars to hold info from orderform, optional

var cname = "";
var caddr1 = "";
var caddr2 = "";
var caddr3 = "";
var postcode = "";
var cardtype = "";
var cardexpire = "";
var cardnum = "";

// Optional Config stuff

// var shopback='images/corner.jpg';	// Background image for shopping list
var shoptcol="#000000";		// Colour for shoppinglist text
var shopalink="#ff0000";	// Active link colour for s/l
var shopvlink="#135888";	// visited link colour for s/l
var shoplink="#ff0000";		// link colour for s/l
var shopbcol="#FFFFFF";		// s/l back colour, to prevent text unreadable
				// while back image loads.
var lastUrl="";			// Stores url of last item page visited.
				// Needed as viewing shoplist.html screws up the
				// Browser history.

function set_cname() { cname=top.mainFrame.document.shopform.CustomerName.value; }
function set_caddr1() { caddr1=top.mainFrame.document.shopform.Address1.value; }
function set_caddr2() { caddr2=top.mainFrame.document.shopform.Address2.value; }
function set_caddr3() { caddr3=top.mainFrame.document.shopform.Address3.value; }
function set_post() { postcode=top.mainFrame.document.shopform.PostCode.value; }
function set_type() { cardtype=top.mainFrame.document.shopform.CardType.value; }
function set_expire() { cardexpire=top.mainFrame.document.shopform.CardExpiryDate.value; }
function set_num() { cardnum=top.mainFrame.document.shopform.CardNumber.value; }

function get_cname() { return cname; }
function get_caddr1() { return caddr1; }
function get_caddr2() { return caddr2; }
function get_caddr3() { return caddr3; }
function get_postcode() { return postcode; }
function get_cardtype() { return cardtype; }
function get_cardexpire() { return cardexpire; }
function get_cardnum() { return cardnum; }

// Function to convert a value to 2 dec places.

function tdv(invalue) {
	var tempstring=new String(parseFloat(invalue));  
	var tempstring2=new String(tempstring.substr(0,tempstring.length-2));
	tempstring=tempstring.substr(tempstring.length-2,2);
	if (tempstring2.length == 0)
	{
		tempstring2='0';
	}
	tempstring2=tempstring2+'.'+tempstring;
	return tempstring2;
}

// functions for url storing for Shoppinglist Back button

function storeUrl(urlname) { lastUrl=urlname; }
function getUrl() { return lastUrl; }

// Function to display all the params in a product for testing.
// Debug function, can be removed in final on-line versions.

function disp()
{
	for (var pname in this)
	{
		alert(pname+" = "+this[pname]);
	}
}

// Function to reload Product page to show any updates etc.

function reloadPage()
{
	viewf.location.reload();	// Main Page
//	totf.location.reload();		// Footer ( Counter )
}

// Function to return quantity of a given product
// Params:
//		ic:	code of product to get quantity for

function getQuantity(ic)
{
	// Check to see if actually exists so far

	if (cart[ic]) { return cart[ic].quan; }
	else

	// If not, return 0 for error cases

	{ return 0; }
}

// function to return a size, or null

function getSize(ic)
{
	if( (cart[ic]) && (cart[ic].size) )
	{
		return cart[ic].size;
	}
	else
	{
		return null;
	}
}

// function to return a Colour, or null

function getColour(ic)
{
	if( (cart[ic]) && (cart[ic].colour) )
	{
		return cart[ic].colour;
	}
	else
	{
		return null;
	}
}

// Function to return multiplier

function getmpr(ic) { return cart[ic].mpr; }

// Bedding functions coded specifically for fantasy-international, but give an
// example as to how the final field in dispitem can be used for extra information.

// Function to return whether bedding ordered or not

function hasBedding()
{
	var hasBed=0;
	for (co in top.leftFrame.cart)
	{
		if (top.leftFrame.cart[co].img=="bed")
		{
			hasBed+=top.leftFrame.cart[co].quan;
		}
	}
	return hasBed;
}

// Function to return bedding postage

function bedPost()
{
	var beddingPost=0;
	for (co in top.leftFrame.cart)
	{
		if (top.leftFrame.cart[co].img=="bed")
		{
			beddingPost+=6*(top.leftFrame.cart[co].quan);
		}
	}
	return beddingPost;
}

// Function to change a given parameter to a new value
// Params:
//		pname:	parameter to change
//		newval: value to change it to

function change(pname,newval)
{
	// Check to make sure parameter given actually exists

	if (this[pname]==-1)
	{
		// Debug info really, just need to be careful on calls from main page

		alert("Invalid parameter name:\n\n\t"+pname+"\n\ngiven in:\n\n\t"+this.name);
	}
	else
	{
		// If ok, make the change

		this[pname]=newval;
	}
	if (pname=="size")
	{
		var tmp=viewf.querySizes(this.code);
		this.sizeval=tmp[this.size];
	}
	if (pname=="colour")
	{
		var tmp=viewf.queryColours(this.code);
		this.colval=tmp[this.colour];
	}
}

// Actual product object definition ( constructor )
// Params:
//		code:	itemcode to add
//		name:	item name
//		price:	price per item
//		quan:	number to add ( might default to 1 )
//		size:	size of item ( where necessary )
//		colour: colour of item ( where necessary )
//		url:	url of page containing product
//		mpr:	multiplier. eg if quan 1 = 12 bottles, mul = 12
//		img:	image to display by product.

function product(code,name,price,quan,size,colour,url,mpr,img)
{
	// Variable initializations

	this.code=code;		// Item code, also used as index to cart array
	this.name=name;		// Full name for the product
	this.price=price;	// Price per item, in pence e.g. £1.39 = 139
	this.quan=quan;		// Current quantity.
	this.size=size;		// Size if required
	this.colour=colour;	// Colour if required
	this.url=url;		// Url of page containing product details
	this.mpr=mpr;
	this.img=img;

	this.headerimg=top.leftFrame.lastimg;
	this.colval='';
	this.sizeval='';

	// Method initialisations

	this.disp=disp;
	this.change=change;
	this.reloadPage=reloadPage;

	// General init stuff would go here if needed

	if(size > -1)
	{
		var tmp=viewf.querySizes(code);
		this.sizeval=tmp[this.size];
	}	
	if(colour > -1)
	{
		var tmp=viewf.queryColours(code);
		this.colval=tmp[this.colour];
	}	
}

// Function to add an item, called from product page
// Params:
//		ic:	itemcode to add
//		name:	item name
//		price:	price per item
//		quan:	number to add ( might default to 1 )
//		size:	size of item ( where necessary )
//		colour: colour of item ( where necessary )
//		url:	url of page containing product
//		mpr:	product multiplier.
//		img:	Image to display by item

function additem(ic,name,price,quan,size,colour,url,mpr,img)
{
	// Check incase product has already been added

	if ( cart[ic] )
	{
		alert(cart[ic].name+" is already in your cart.");
	}
	else
	{
		// New item, so create new product and store in cart array
		
		if(!size)
		{
			size=-1;
		}
		if(!colour)
		{
			colour=-1;
		}

		cart[ic]=new product(ic,name,parseFloat(price),parseInt(quan),size,colour,url,mpr,img);
		alert(cart[ic].name+" added to your cart.\n(Page will now update)");
		// Reload so displayed values are updated

		cart[ic].reloadPage();
	}
}

// Function to remove a product, called from relevant product page.
// Params:
//		ic:	item code to remove

function removeItem(ic)
{
	var tempprod;
	// Make sure product is already in cart

	if (!cart[ic])
	{
		alert("This item is not currently in your cart anyway.");
	}
	else
	{
		// Definitely in cart, so do removal

	   tempprod=cart[ic];
	
		// Check really want to remove item

	   if ( confirm("Remove "+tempprod.name+" from your cart?"))
	   {
		delete cart[ic];

		tempprod.reloadPage();
	   }
	}
}

// Function to update a product value, called from product page.
// Params:
//		ic: 	product code to change
//		pname:	product parameter to change
//		newval: value to change pname to

function changeVal(ic,pname,newval)
{
	// Check already in cart

	if (!cart[ic])
	{
		alert("Item doesnt exist");
	}
	else
	{
		// Change value

		cart[ic].change(pname,newval);
		if (pname=='quan')
		{
//			cart[ic].reloadPage();
		}
	}
}

// Quick Function to add up all the quantities so far in the list,
//	and return the total number.

function returnTotal()
{
	var temptot=0.00;
	for (co in cart)
	{
		temptot+=eval(cart[co].quan*cart[co].mpr);
	}
	return(temptot);
}

// Function to return how many multipack items are in the cart

function returnmTotal()
{
	var temptot=0.00;
	for (co in cart)
	{
	   // If a multipack

	   if (cart[co].mpr>1)
	   {
		temptot+=eval(cart[co].quan);
	   }
	}
	return(temptot);
}

// function to do cart checking
// waxman needed multiple of 12 bottles.
// This function is optional.

function checkcart()
{
	var temptot=0.00;
	
	// No point going further if no items selected

	if (returnTotal() == 0)
	{
		return -1;
	}

	// Step through all items selected 

	for (co in cart)
	{

		// Ignore multipacks.

		if ( cart[co].mpr == 1)
		{
			temptot+=eval(cart[co].quan);
		}
	}

	// Return total modulo 12 ( ie. no. of unmatched bottles )

	return (temptot % 12);
}
