
//
// Functions to include in objects for use with shopping trolley code
//
// (c)2001 Electronic Media Group - www.emgnet.co.uk
//
// ********************************************************
//  This file needs to be included in all pages containing
//	buyable items. The way each item is displayed can
//	be altered by editing the dispitem function.
// ********************************************************

// General definitions.

var cartf=parent.leftFrame; 	// Location of main cart functions.
var sizeArrays=new Array;	// Holding Array for item sizes 
var colourArrays=new Array;	// Holding Array for item colours
var priceArrays=new Array;	// Holding Array for item prices
function initArrays()
{
}
// Function to return given possible sizes

function querySizes(ic)
{
        initArrays();
        return sizeArrays[ic];
}

// Function to return given possible colours

function queryColours(ic)
{
        initArrays();
        return colourArrays[ic];
}

// Function to Display total number of items so far.
// 	probably not needed in here, only in frame needing total

function dispTotal()
{
        var tempres=cartf.returnTotal();
        document.writeln(tempres);
}

// Function to do a new size.

function changeSize(ic)
{
	var tempstring="SIZE"+ic;
	cartf.changeVal(ic,"size",document.ITEMFORM.elements[tempstring].selectedIndex);
	if (priceArrays[ic])
	{
		cartf.changeVal(ic,"price",priceArrays[ic][document.ITEMFORM.elements[tempstring].selectedIndex]);
	}
}

// Function to do a new colour.

function changeColour(ic)
{
	var tempstring="COLOUR"+ic;
	cartf.changeVal(ic,"colour",document.ITEMFORM.elements[tempstring].selectedIndex);

}

// Function to show sizes

function dispSize(size,ic)
{
        var possSizes=querySizes(ic);
        var ts1="SIZE"+ic;
        var tempString="changeSize('"+ic+"')";
        document.writeln('<SELECT NAME="'+ts1+'" onChange="'+tempString+'">');
        for (co in possSizes)
        {
                document.write("<OPTION VALUE='"+co+"'");
                if (co == size)
                {
                        document.write(" SELECTED>");
                }
                else
                {
                        document.write(">");
                }
                document.writeln(possSizes[co]);
        }
        document.writeln("</SELECT>&nbsp;&nbsp;");
}

// Function to show Colours

function dispColour(colour,ic)
{
        var possColours=queryColours(ic);
        var ts1="COLOUR"+ic;
        var tempString="changeColour('"+ic+"')";
        document.writeln("&nbsp;&nbsp;");
        document.writeln('<SELECT NAME="'+ts1+'" onChange="'+tempString+'">');
        for (co in possColours)
        {
                document.write("<OPTION VALUE='"+co+"'");
                if (co == colour)
                {
                        document.write(" SELECTED>");
                }
                else
                {
                        document.write(">");
                }
                document.writeln(possColours[co]);
        }
        document.writeln("</SELECT>");
}

// Function to show an item, will be changed depending on required layout etc.

function dispItem(ic,name,price,quantity,size,colour,url,mpr,img)
{
	var tempprice=price;
	initArrays();
	if (tempprice==-1)
	{	
		tempprice=priceArrays[ic][size];
	}

        var tempstr='"'+ic+'","'+name+'","'+tempprice+'","'+quantity+'","'+size+'","'+colour+'","'+url+'","'+mpr+'","'+img+'"';
	
	// Display price

	document.writeln("<TABLE BORDER='0' WIDTH='100%'><TR valign='top'><TD Align='center'>");
//	document.writeln("<B>Price: </B>$"+cartf.tdv(price));

        if (mpr>1)
        {
                document.writeln(" <I> ( "+mpr+" per pack. )</I><BR>");
        }
	else
	{
//		document.writeln("<BR>");
	}

        // Only show Add if not in cart.

        if (cartf.getQuantity(ic)<=0)
        {
	document.writeln("<A href='#' onClick='cartf.additem("+tempstr+")'>");

	// Image for add button goes in here .....

	document.writeln("<IMG src='images/addtoshop.gif' border=0></A>");
//	document.writeln("<INPUT TYPE='BUTTON' VALUE='Add' onclick='cartf.additem("+tempstr+")'>");
        }
        else
        {
        var tempsize=cartf.getSize(ic);
        if (tempsize > -1)
        {
                dispSize(tempsize,ic);
        }
        
        }

        showquan(ic);
        tempstr='"'+ic+'"';

        // Only display change quantity and delete if on cart.

        if (cartf.getQuantity(ic)>0)
        {
	document.writeln("<A href='#' onClick='cartf.removeItem("+tempstr+")'>");

	// Image for remove button goes in here ........

	document.writeln("<IMG src='images/remove.gif' border=0></A>");
//	document.writeln("&nbsp;&nbsp;<INPUT TYPE='BUTTON' VALUE='Remove' onclick='cartf.removeItem("+tempstr+")'>");
        }
        document.writeln("</TABLE>");
	cartf.storeUrl(url);
}

// Function to handle a request for a quantity change.

function newquan2(ic)
{
        var oldval=cartf.getQuantity(ic);

	// Check is on list. Should be as shouldnt display change quantity otherwise, but extra
	// checks can only be a good thing.

        if (oldval>0)
        {
            var newval=document.ITEMFORM.elements[ic].value;
            if (newval != oldval) 
            {

		// If they actually specified a different value.

		if (eval(newval)>0)
		{
          		cartf.changeVal(ic,'quan',Math.round(newval));
		}
		else
		{
			alert(ic+":"+newval);
//			cartf.removeItem(ic);
		}
            }
        }
}

// Function to show the current quantity ordered

function showquan(ic)
{

	// Get quantity from cart

	var tempval=cartf.getQuantity(ic);
	var tempstr='newquan2("'+ic+'")';

	// If on the cart

	if (tempval >0)
	{
		document.writeln("<Font size=2>");
		document.writeln("<INPUT TYPE='TEXT' SIZE='5' NAME='"+ic+"' VALUE='"+tempval+"' onChange='"+tempstr+"'></font>");
		var tempmpr=eval(cartf.getmpr(ic));

		// Add extra info if quantity is for multi-pack items

		if (tempmpr > 1)
		{
			document.writeln("<I><SMALL> ( "+(tempmpr*tempval)+" items. ) </SMALL></I>");
		}
//		document.writeln("<INPUT TYPE='BUTTON' VALUE='Update' onClick='"+tempstr+"'><BR>");
//		document.writeln("<BR>");
//		document.writeln("<A href='#' onclick='"+tempstr+"'>Update Item</A>&nbsp&nbsp|&nbsp&nbsp");
	}
	var tempcolour=cartf.getColour(ic);
        if ((tempcolour > -1) && (tempval>0))
        {
                dispColour(tempcolour,ic);
        }

}

