﻿/* Copyright (c) On Technology Australia Pty Ltd - All Rights Reserved. */

var g_CartDisplay = false;

var colProductPrice = 0;
var colFreightLocal = 1;
var colFreightDomestic = 2;
var colFreightInternational = 3;
var colProductWeight = 4;
var colTaxLocal = 5;
var colTaxDomestic = 6;
var colTaxInternational = 7;
var colProductCode = 0;
var colProductDesc = 1;
var colCustomFieldKeys = 2;

var bProcessCart = true;

function nA()
{
	var myProductArray = new Array(0,0,0,0,0,0,0,0);
	for (var x = 0; x < arguments.length; x++) 
	{
		myProductArray[x] = arguments[x];
	}
	return myProductArray;
}

function getProductPrice(curSymbol, thousandsSeparator, decPlaces, decSeparator, truncateDec, blnSymbolAtFront, pid, local, domestic, international, defaultRegion, blnIncludeTax)
{
	var curPrice = NaN;

	if (typeof(getProduct) == "function")
	{	
		var Info = getProduct(pid);
		if (Info)
		{
			switch(blnIncludeTax ? getRegion(defaultRegion) : 99)
			{
			case 0:
				curPrice = Info[colProductPrice] * (1 + Info[colTaxLocal]);		
				break;
			case 1:
				curPrice = Info[colProductPrice] * (1 + Info[colTaxDomestic]);		
				break;
			case 2: 
				curPrice = Info[colProductPrice] * (1 + Info[colTaxInternational]);		
				break;
			case 99:
				curPrice = Info[colProductPrice];
				break;
			}
		}
	}

	return "<span id=\"eziprice" + pid + "\">" + htmlEncode(formatCurrency(curSymbol, thousandsSeparator, decPlaces, decSeparator, truncateDec, blnSymbolAtFront, curPrice)) + "</span>";
}

function getPriceInfo(pid, local, domestic, international, defaultRegion, blnIncludeTax)
{
	var strTaxString;
	var curPrice;
	
	if (typeof(getProduct) == "function")
	{
        var Info = getProduct(pid);
     
        if (Info)
        {   
	        switch(blnIncludeTax ? getRegion(defaultRegion) : 99)
	        {
	        case 0:
		        curPrice = Info[colProductPrice] * (1.0 + Info[colTaxLocal]);		
		        if (curPrice != Info[colProductPrice])
			        strTaxString = local;
		        else
			        strTaxString = "";
		        break;
	        case 1:
		        curPrice = Info[colProductPrice] * (1.0 + Info[colTaxDomestic]);		
		        if (curPrice != Info[colProductPrice])
			        strTaxString = domestic;
		        else
			        strTaxString = "";
		        break;
	        case 2:   
		        curPrice = Info[colProductPrice] * (1 + Info[colTaxInternational]);
		        if (curPrice != Info[colProductPrice])
			        strTaxString = international;
		        else
			        strTaxString = "";
		        break;
	        case 99:
		        curPrice = Info[colProductPrice];
		        strTaxString = "";
		        break;
	        }

    	    return htmlEncode(strTaxString);
        }
    }
}


function CartLine()
{
	this.toString = function()
	{
		var CartLine = this.id + "&" + this.quantity + "&";
		
		if (this.priceCustom)
		    CartLine += this.price + "&";
		else
		    CartLine += "&";

		if (this.customfieldvalues)
		{
		    var CustomData = "";
		
		    for(var cf in this.customfieldvalues)
                CustomData += encodeURIComponent(this.customfieldvalues[cf]) + "&";
                
            CartLine += escape(CustomData.slice(0, -1));
		}

	    return CartLine;
	};
	
	this.populate = function()
	{
		if (this.price || this.price == 0)
		    this.priceCustom = true;
		else
		    this.priceCustom = false;

		if (typeof(getProductInfo) == "function")
		{
			var Info = getProductInfo(this.id);

			this.code = Info[colProductCode];
			this.name = Info[colProductDesc];
			
		    if (this.customfieldvalues)
		    {
		        var CustomFieldNames = Info[colCustomFieldKeys];
		        
		        this.customfields = new Array();
		        
		        for(var CustomField in this.customfieldvalues)
		        {
		            var oCustomField = new Object();
		            
		            oCustomField.name = getCustomField(CustomFieldNames[CustomField]);
		            oCustomField.value = this.customfieldvalues[CustomField];
		        
		            this.customfields.push(oCustomField);		           
		        }		       	      		  
		    }			
		}

		if (typeof(getProduct) == "function")
		{
			var Info = getProduct(this.id);

			if (Info)
			{
				if (!this.price && this.price != 0)
					this.price = Info[colProductPrice];

				this.prices = new Array(this.price * (1.0 + Info[colTaxLocal]), this.price * (1.0 + Info[colTaxDomestic]), this.price * (1.0 + Info[colTaxInternational]));
				this.prices[99] = this.price;
				this.taxes = new Array(Info[colTaxLocal], Info[colTaxDomestic], Info[colTaxInternational]);
				this.weight = Info[colProductWeight];
				this.freight = new Array(Info[colFreightLocal], Info[colFreightDomestic], Info[colFreightInternational]);
			}
		}	
	}

	this.fromString = function(line)
	{
		var LineData = line.split("&");
		var ProductID = parseInt(LineData[0]);
		var Quantity = parseInt(LineData[1]);
		var Price = parseFloat(LineData[2]);
		var CustomData = LineData[3];

		this.id = ProductID;
		this.quantity = Quantity;
		this.price = Price;	

		if (CustomData)
		{
			this.customfieldvalues = unescape(CustomData).split("&");

			if (this.customfieldvalues.length == 0)
			{
				delete this.customfieldvalues;
			}
			else
			{				
		        for(var cf in this.customfieldvalues)
		            this.customfieldvalues[cf] = decodeURIComponent(this.customfieldvalues[cf]);
		    }
		}
		
		this.populate();
	};

	this.equals = function(RHS)
	{
		if (this.id == RHS.id)
		{
		    if (this.price == RHS.price)
		    {
		        if (!this.customfieldvalues && !RHS.customfieldvalues)
		            return true;

                if (this.customfieldvalues && RHS.customfieldvalues)
                {
                    if (this.customfieldvalues.join("&") == RHS.customfieldvalues.join("&"))
                        return true;
                }
            }
		}

		return false;
	};

	this.valid = function()
	{
		if (this.id && this.quantity && getProduct(this.id))
			return true;

		return false;
	};

	if (arguments.length == 1 && typeof(arguments[0]) == "string")
	{
		this.fromString(arguments[0]);
		return;
	}

	if (arguments.length == 4)
	{
		this.id = arguments[0];
		this.quantity = arguments[1];
		this.price = arguments[2];
		this.customfieldvalues = arguments[3];
		
		this.populate();
	}
}

function Cart()
{
	this.fromString = function(cart)
	{
		var lines = cart.replace(/^\s*lines=|^\s*lines/, "").split("#");

		if (lines.length == 0 || (lines.length == 1 && !lines[0]))
			return;

		this.lines.length = 0;

		for(var idx in lines)
			this.lines.push(new CartLine(lines[idx]));
	};

	this.toString = function()
	{
		return "lines=" + this.lines.join("#");
	};

	this.toHTML = function()
	{
	    var args = arguments[0];
	    var decPlaces = args[2];
	    var decSeparator = args[3];
	    var truncateDec = args[4];
	    var showExTax = args[6];
	    var showIncTax = args[7];
	    var showTaxPercent = args[8];
	    var showTaxAmount = args[9];
		var region = getRegion(args[10]);	    
		var removeText = args[26];
		var imagePath = args[29];
		var headerFont = args[11];
		var headerSize = args[12];
		var headerBackgnd = args[13];
		var headerForegnd = args[14];
		var headerText = args[15];
		var cartFontOdd = args[16];
		var cartFontSizeOdd = args[17];
		var cartBackgndOdd = args[18];
		var cartForegndOdd = args[19];
		var cartFontEven = args[20];
		var cartFontSizeEven = args[21];
		var cartBackgndEven = args[22];
		var cartForegndEven = args[23];
		var totTaxLabel = args[24];
		var totLabel = args[25];
		var emptyCart = args[27];
		var verStr = args[28];
		var NotRegisteredMessage = args[32];
		var columns =  4 + showExTax + showTaxPercent + showTaxAmount + showIncTax;
		
		var format = function(amt)
		{					
		    return formatCurrency(args[0], args[1], args[2], args[3], args[4], args[5], amt);
		}

		if (!document.cookie)
		{
			document.cookie = "test";

			if (!document.cookie)
			{				
				return "<table border=\"1\" cellspacing=\"0\" width=\"80%\" bgcolor=\"#FFFFFF\" bordercolor=\"#808080\" cellpadding=\"6\"><tr><td width=\"100%\">" +
				        "<p align=\"center\"><font face=\"Tahoma\"><b>We're Sorry</b></font></td>" +
				        "</tr><tr><td width=\"100%\"><font face=\"Tahoma\" size=\"2\"><br>" +
				        "It appears your browser is set to refuse cookies. Our online purchasing " +
				        "system requires that you have cookies enabled on your browser. </font>" +
				        "<p><font face=\"Tahoma\" size=\"2\">If you don't know how to enable this " +
				        "feature we have provided instructions on enabling cookies for various " +
				        "browsers at <a href=\"http://www.ezimerchant.com/linkin/shopping_cart.asp?link=CookiesNotEnabled\">www.ezimerchant.com</a>" +
				        "</font></p><p>&nbsp;</td></tr></table>";
			}
		}

		var CartHtml = "<table id=\"eziCart\" border=\"0\" cellspacing=\"0\" cellpadding=\"4\">";
		
	    if (this.lines.length == 0)
	    {
	        CartHtml += "<tr bgcolor=\"" + cartBackgndEven + "\"><td colspan=" + (columns + 1) + " align=center valign=center nowrap><font face=\"" + cartFontOdd + "\" color=\"" + cartForegndEven + "\" size=\"" + cartFontSizeOdd + "\">" + htmlEncode(emptyCart) + "</font></td></tr>";
	    }
	    else
	    {
		    CartHtml += "<tr id=\"heading\" bgcolor=" + headerBackgnd + ">";
		    CartHtml += "<th nowrap valign=\"bottom\"><font face=\""+ headerFont + "\" color=\"" + headerForegnd + "\" size=" + headerSize +"><b>" + headerText[0] + "</b></font></th>";
		    CartHtml += "<th valign=\"bottom\"><font face=\"" + headerFont +"\" color=\"" + headerForegnd + "\" size=" + headerSize + "><b>" + headerText[1] + "</b></font></th>";
		    CartHtml += "<th valign=\"bottom\"><font face=\""+ headerFont +"\" color=\"" + headerForegnd + "\" size=" + headerSize + "><b>" + headerText[2] + "</b></font></th>";
		    CartHtml += showExTax ? "<th valign=\"bottom\"><font face=\""+ headerFont +"\" color=\"" + headerForegnd + "\" size=" + headerSize + "><b>" + headerText[3] + "</b></font></th>" : "";
		    CartHtml += showTaxPercent ? "<th valign=\"bottom\"><font face=\""+ headerFont +"\" color=\"" + headerForegnd + "\" size=" + headerSize + "><b>" + headerText[4] + "</b></font></th>" : "";
		    CartHtml += showTaxAmount ? "<th valign=\"bottom\"><font face=\""+ headerFont +"\" color=\"" + headerForegnd +"\" size=" + headerSize + "><b>" + headerText[5] + "</b></font></th>" : "";
		    CartHtml += showIncTax ? "<th valign=\"bottom\"><font face=\""+ headerFont +"\" color=\"" + headerForegnd + "\" size=" + headerSize + "><b>" + headerText[6] + "</b></font></th>" : "";
		    CartHtml += "<th valign=\"bottom\"><font face=\""+ headerFont +"\" color=\"" + headerForegnd + "\" size=" + headerSize + "><b>" + headerText[7] + "</b></font></th>";
		    CartHtml += "<th>&nbsp;</th></tr>";	    

            var TaxTotal = 0;
            var SubTotal = 0;
            var LineTotal;
	    
		    for(var line in this.lines)
		    {
			    var oLine = this.lines[line];

			    if (oLine.valid())
			    {			    
			        if (truncateDec)
			        {
			            LineTotal = Math.floor(oLine.quantity * oLine.prices[region] * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
			            TaxTotal += Math.floor(oLine.quantity * (oLine.prices[region] - oLine.price) * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
			        }
			        else
			        {
			            LineTotal = Math.round(oLine.quantity * oLine.prices[region] * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
			            TaxTotal += Math.round(oLine.quantity * (oLine.prices[region] - oLine.price) * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
			        }
			        SubTotal += LineTotal;
			    
			        CartHtml += "<tr bgcolor=\"" + ((line % 2) == 0 ? cartBackgndEven : cartBackgndOdd) + "\">" +
						        "<td><input type=text size=4 name=\"quantity\" value=\"" + oLine.quantity + "\" onblur=\"updateCartLine(" + line + ", this.value);\" onkeypress=\"if (event.which == 0 || event.keyCode == 13) updateCartLine(" + line + ", this.value);\"></td>" +
						        "<td><font face=\"" + ((line % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((line % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\">" + htmlEncode(oLine.name) + "</font></td>" +
						        "<td><font face=\"" + ((line % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((line % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\">" + htmlEncode(oLine.code) + "</font></td>";
						        
                    if (showExTax)
                        CartHtml += "<td align=\"right\"><font face=\"" + ((line % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((line % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\">" + htmlEncode(format(oLine.price)) + "</font></td>";
                        
                    if (showTaxPercent)
                        CartHtml += "<td><font face=\"" + ((line % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((line % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\">" + oLine.taxes[region] * 100 + "%</td>";
                        
                    if (showTaxAmount)
                        CartHtml += "<td align=\"right\"><font face=\"" + ((line % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\"  color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((line % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\">" + htmlEncode(format(oLine.price * oLine.taxes[region])) + "</td>";
                        
                    if (showIncTax)
                        CartHtml += "<td align=\"right\"><font face=\"" + ((line % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((line % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\">" + htmlEncode(format(oLine.prices[region])) + "</td>";
                                                                                      
                    CartHtml += "<td align=\"right\"><font face=\"" + ((line % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((line % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\">" + htmlEncode(format(LineTotal)) + "</td>";
			        CartHtml += "<td><a href=\"javascript:deleteCartLine(" + line + ");\"><img src=\"" + imagePath + "/cartremove.gif\" border=0 title=\"" + htmlEncode(removeText) + "\"></a>";
        	
			        // hidden fields sent to server per orderline			       
			        CartHtml += "<input type=hidden name=\"prodweight\" value=\"" + FormatSeparator(oLine.weight,decSeparator) + "\">";
			        CartHtml += "<input type=hidden name=\"localtax\" value=\"" + oLine.taxes[0] + "\">";
			        CartHtml += "<input type=hidden name=\"domestictax\" value=\"" + oLine.taxes[1] + "\">";
			        CartHtml += "<input type=hidden name=\"internationaltax\" value=\"" + oLine.taxes[2] + "\">";
			        CartHtml += "<input type=hidden name=\"localdelivery\" value=\"" + oLine.freight[0] + "\">";
			        CartHtml += "<input type=hidden name=\"interstatedelivery\" value=\"" + oLine.freight[1] + "\">";
			        CartHtml += "<input type=hidden name=\"internationaldelivery\" value=\"" + oLine.freight[2] + "\">";
			        CartHtml += "<input type=hidden name=\"unitprice\" value=\"" +  FormatSeparator(oLine.price,decSeparator) + "\">";
			        CartHtml += "<input type=hidden name=\"prodname\" value=\"" + htmlEncode(oLine.name) + "\">";
			        CartHtml += "<input type=hidden name=\"prodcode\" value=\"" + htmlEncode(oLine.code) + "\">";			        

			        // process custom fields into hidden fields to be sent to server
			        var CustomFieldNames = "";
			        var CustomFieldValues = "";
			        
			        if (oLine.customfields)
			        {
			            for(var CustomField in oLine.customfields)
			            {
			                if (CustomField > 0)
			                {
			                    CustomFieldNames += "|";
			                    CustomFieldValues += "|";
			                }
    			        
			                CustomFieldNames += escape(oLine.customfields[CustomField].name);
			                CustomFieldValues += escape(oLine.customfields[CustomField].value);
			            }
    			        
			            if (oLine.customfields.length > 0)
			            {
				            CartHtml += "<tr bgcolor=\"" + ((line % 2) == 0 ? cartBackgndEven : cartBackgndOdd) + "\"><td>&nbsp;</td><td colspan=\"" + columns + "\"><table border=\"0\">";
				            for(var CustomField in oLine.customfields)
    					    {
						        if (!oLine.customfields[CustomField].name)
							        CartHtml += "<tr><td><font face=\"" + ((line % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\"  color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((line % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\"><b><i>" + htmlEncode(oLine.customfields[CustomField].value) + "</i></b></font></td></tr>";
						        else
                                    CartHtml += "<tr><td><font face=\"" + ((line % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((line % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\"><b><i>" + htmlEncode(oLine.customfields[CustomField].name) + ":</i></b></font></td><td><font face=\"" + ((line % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\"  color=\"" + ((line % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((line % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\">" + htmlEncode(oLine.customfields[CustomField].value) + "</font></td></tr>";
                            }
                            
				            CartHtml += "</table></td></tr>";
			            }			        
                    }			        
		        
			        CartHtml += "<input type=hidden name=\"customfieldname\" value=\"" + htmlEncode(CustomFieldNames) + "\">";
			        CartHtml += "<input type=hidden name=\"customfieldvalue\" value=\"" + htmlEncode(CustomFieldValues) + "\">";  

			        CartHtml += "</td></tr>";			   
			    }
		    }
		    
		    if (TaxTotal > 0)
		    {
			    CartHtml += "<tr><td colspan=\"" + (columns - 1) + "\" align=\"right\">" +
					    "<font face=\"" + ((this.lines.length % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((this.lines.length % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((this.lines.length % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\">" +
					    "<b>" + htmlEncode(totLabel) + " (" +  htmlEncode(totTaxLabel) + " " + htmlEncode(format(TaxTotal)) + "):</b>" + 
					    "</font>" +
					    "</td>" +
					    "<td align=\"right\" bgcolor=\"" + ((this.lines.length % 2) ? cartBackgndEven : cartBackgndOdd) + "\">" +
					    "<font face=\"" + ((this.lines.length % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((this.lines.length % 2) == 0 ? cartForegndEven : cartForegndOdd)  + "\" size=\"" + ((this.lines.length % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\">" +
					    "<b>" + htmlEncode(format(SubTotal)) + "</b>" +
					    "</font>" +
					    "</td>" +
				    "</tr>";				    
		    }
		    else
		    {
			    CartHtml += "<tr><td colspan=\"" + (columns - 1) + "\" align=\"right\"><font face=\"" + ((this.lines.length % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((this.lines.length % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\"  size=\"" + ((this.lines.length % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\"><b><i>" + htmlEncode(totLabel) + ":</i></b></font></td><td align=\"right\" bgcolor=\"" + ((this.lines.length % 2) ? cartBackgndEven : cartBackgndOdd) + "\"><font face=\"" + ((this.lines.length % 2) == 0 ? cartFontEven : cartFontOdd) + "\" color=\"" + ((this.lines.length % 2) == 0 ? cartForegndEven : cartForegndOdd) + "\" size=\"" + ((this.lines.length % 2) == 0 ? cartFontSizeEven : cartFontSizeOdd) + "\"><b>" + htmlEncode(format(SubTotal)) + "</b></font></td></tr>";		
		    }		    
	    
		    CartHtml += "<tr><td colspan=\"" + (columns + 1) + "\" align=right>";
		    CartHtml += "<input type=hidden name=\"productcount\" value=\"" + this.lines.length + "\">";
		    CartHtml += "<input type=hidden name=\"version\" value=\"" + htmlEncode(verStr) + "\">";
		    CartHtml += "</td></tr>";

		    CartHtml += "<tr><td colspan=\"9\" align=right>";
		    CartHtml += "<br><a href=\"#\" onclick=\"return false;\"><img src=\"" + imagePath + "/cartrecalculate_off.gif\" vspace=\"3\" border=\"0\" onmousedown=\"this.src='" + imagePath + "/cartrecalculate_on.gif'\" onmouseup=\"this.src='" + imagePath + "/cartrecalculate_off.gif'\" onmouseout=\"this.src='" + imagePath + "/cartrecalculate_off.gif'\"></a>";
		    CartHtml += "<br><a href=\"#\" onclick=\"return submitCart(&quot;" + htmlEncode(NotRegisteredMessage) + "&quot;);\"><img src=\"" + imagePath + "/cartcheckout.gif\" border=\"0\"></a>";
		    CartHtml += "</td></tr>";		    
		}

		CartHtml += "</table><input type=\"hidden\" name=\"requestedaction\" value=\"CreateNewOrder\"/>";

		return CartHtml;
	}

	this.addLine = function(id, qty, price, cf)
	{
		var oCartLine = new CartLine(id, qty, price, cf);

		for(var i = 0; i < this.lines.length; i++)
		{
			if (this.lines[i].equals(oCartLine))
			{
				this.lines[i].quantity += qty;
				return;
			}
		}

		this.lines.push(oCartLine);
	};

	this.updateLine = function(item, qty)
	{
		if (isNaN(qty))
			return;

		if (qty < 1)
			this.lines.splice(item, 1);

		else
			this.lines[item].quantity = qty;
	};

	this.deleteLine = function(item)
	{
		if (typeof(item) == "number")
		{
			this.lines.splice(item, 1);
		}
	};
	
	this.total = function(defaultRegion)
	{
	    var region = getRegion(defaultRegion);
	    var total = 0;
	
	    for(var Line in this.lines)
	    {
	        var oLine = this.lines[Line];
	    
	        total += oLine.prices[region] * oLine.quantity;
	    }
	    
	    return total;
	}

	this.save = function()
	{
		var dtExpiry = new Date();

		dtExpiry.setFullYear(dtExpiry.getFullYear() + 5);

		document.cookie = this.toString() +"; expires=" + dtExpiry.toGMTString() + ";";
	};

	this.lines = new Array();

	if (arguments.length == 1 && typeof(arguments[0]) == "string")
	{
		var cookies = arguments[0].split(";");
		for(var cookie in cookies)
		{		
			if (/^\s*lines/i.test(cookies[cookie]))
				this.fromString(cookies[cookie]);
		}
	}
}

function addProductToCart(frm, strAddMessage, strErrMsg, strFileName, Price)
{
	var ProductID = frm && frm.elements["pid"] ? parseInt(frm.elements["pid"].value) : NaN;
	var Quantity = frm && frm.elements["qty"] ? parseInt(frm.elements["qty"].value) : NaN;
	var CustomFields;

	if (!Price && frm && frm.elements["price"])
		Price = parseFloat(frm.elements["price"].value);
		
	if (!Quantity)
	{
	    if (Quantity == 0)
	        return;
	        
		Quantity = 1;
    }
		
	for(var i = 0; i < frm.elements.length; i++)
	{
		if (frm.elements[i].name == "cf")
		{
			if (!CustomFields)
				CustomFields = new Array();

			switch(frm.elements[i].type)
			{
			case "checkbox":

				CustomFields.push(frm.elements[i].checked ? "Yes" : "No");
				break;

			default:

				CustomFields.push(frm.elements[i].value);
				break;
			}
		}		
	}

	var oCart = new Cart(document.cookie);
	oCart.addLine(ProductID, Quantity, Price, CustomFields);
	oCart.save();

	if (typeof(strAddMessage) == "undefined")
	{
		window.location = "viewcart.htm";
		return true;
	}

	if (strAddMessage != "")
	{
		alert(strAddMessage);
		if (g_CartDisplay)
		    window.location.reload();
		return false;
	}
}

function updateCartLine(x, qty)
{
	var oCart = new Cart(document.cookie);

	oCart.updateLine(x, qty);	oCart.save();

	window.location.reload();
}

function deleteCartLine(x)
{
	var oCart = new Cart(document.cookie);

	oCart.deleteLine(x);
	oCart.save();

	window.location.reload();
}

function submitCart()
{
    if (arguments.length == 1 && arguments[0])
    {
        alert(arguments[0]);
        return;
    }
    
    document.forms["ordercart"].target="_top";
    document.forms["ordercart"].submit();
    
    return false;
}

function getCart()
{
	return new Cart(document.cookie).toHTML(arguments);
}

function getCartCount()
{
    g_CartDisplay = true;

    return new Cart(document.cookie).lines.length;
}

function getCartItemCount()
{
    g_CartDisplay = true;

    var count = 0;
    var cart = new Cart(document.cookie);
    
    for(var line in cart.lines)
        count += cart.lines[line].quantity;
        
    return count;
}

function getCartTotal(curSymbol, thousandsSeparator, decPlaces, decSeparator, truncateDec, blnSymbolAtFront, defaultRegion)
{
    g_CartDisplay = true;

    return htmlEncode(formatCurrency(curSymbol, thousandsSeparator, decPlaces, decSeparator, truncateDec, blnSymbolAtFront, new Cart(document.cookie).total(defaultRegion)));
}

function formatCurrency(curSymbol, thousandsSeparator, decPlaces, decSeparator, truncateDec, blnSymbolAtFront, curValue)
{
    var hashCurrency = {"&pound;" : 163, "&euro;" : 8364, "&curren;" : 164, "&yen;" : 165, "&cent;" : 162};

    curSymbol = new String(curSymbol).replace(/&#(\d+);|(&[A-Za-z]+;)/ig, function() 
	{
		var charCode = parseInt(arguments[1], 10);

		if (!isNaN(charCode))
			return String.fromCharCode(charCode);

		charCode = hashCurrency[new String(arguments[0]).toLowerCase()];

		if (charCode)
			return String.fromCharCode(charCode);

		return arguments[0];
	});
    
	var strCurrency;
	if (truncateDec) {
	  strCurrency = new String(Math.floor(parseFloat(curValue) * Math.pow(10, decPlaces)));
	}else {
	  strCurrency = new String(Math.round(parseFloat(curValue) * Math.pow(10, decPlaces)));
	}

	while (strCurrency.length <= decPlaces)
		strCurrency = "0" + strCurrency;

	var decValue = 	strCurrency.substr(strCurrency.length - decPlaces, strCurrency.length);			

	strCurrency = strCurrency.substr(0, strCurrency.length - decPlaces);
	var s;

	if (strCurrency.length > 3) {
	        s = strCurrency.substr(strCurrency.length-3, 3);

		for (var i = 4; i <= strCurrency.length; i++){
        	   if (((i - 4) % 3) == 0){
	              s = strCurrency.substr(strCurrency.length - i,1) + thousandsSeparator + s;
		    } 
		    else{
		      s = strCurrency.substr(strCurrency.length - i,1) + s;
		    }
	
		}
	} else {
	  s = strCurrency;
	}

	if (decPlaces != 0) {
   	  strCurrency =  s + decSeparator + decValue;
	}else{
   	  strCurrency =  s;
	}

	return blnSymbolAtFront ? curSymbol + strCurrency : strCurrency + curSymbol;
}

function setRegion(region)
{
	var dtExpiry = new Date();

	dtExpiry.setFullYear(dtExpiry.getFullYear() + 5);

	document.cookie = "region=" + escape(region);

	window.location.reload();

	return false;
}

function getRegion(defaultRegion)
{
	if (document.cookie)
	{
		var cookies = document.cookie.split(";");

		for(var cookie in cookies)
		{
			if (/^\s*region=\d{1,2}/i.test(cookies[cookie]))
			{
				var cookieData = cookies[cookie].split("=");

				if (cookieData[1] == "0" ||
					cookieData[1] == "1" ||
					cookieData[1] == "2" ||
					cookieData[1] == "99")
				{
					return parseInt(cookieData[1]);
				}
	
				break;
			
			}			
		}
	}
	return parseInt(defaultRegion);
}

function getLocalButton(defaultregion, OnlyInCart)
{
  if (OnlyInCart) {
	var oCart = new Cart(document.cookie);
	if (oCart.lines.length == 0)
		return "";
  }
     
  var strBtn = "<a href=\"javascript:setRegion(0);\">";
  strBtn += getRegion(defaultregion) == 0 ? "<img border=\"0\" src=\"images/cartlocal_on.gif\"></a>" : "<img border=\"0\" src=\"images/cartlocal_off.gif\"></a>";
  return strBtn;

}

function getDomesticButton(defaultregion, OnlyInCart)
{
  if (OnlyInCart) {
	var oCart = new Cart(document.cookie);
	if (oCart.lines.length == 0)
		return "";
  }

  var strBtn = "<a href=\"javascript:setRegion(1);\">";
  strBtn += getRegion(defaultregion) == 1 ? "<img border=\"0\" src=\"images/cartdomestic_on.gif\"></a>" : "<img border=\"0\" src=\"images/cartdomestic_off.gif\"></a>";
  return strBtn;

}

function getInternationalButton(defaultregion, OnlyInCart)
{
  if (OnlyInCart) {
	var oCart = new Cart(document.cookie);
	if (oCart.lines.length == 0)
		return "";
  }

  var strBtn = "<a href=\"javascript:setRegion(2);\">";
  strBtn += getRegion(defaultregion) == 2 ? "<img border=\"0\" src=\"images/cartinternational_on.gif\"></a>" : "<img border=\"0\" src=\"images/cartinternational_off.gif\"></a>";
  return strBtn;

}


function FormatSeparator(strValue,decSeparator)
{
	var re = /\./;
	var newValue = strValue.toString();
	newValue = newValue.replace(re, decSeparator);
	return newValue;
}

function htmlEncode(str)
{
    return new String(str).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;"); 
}
