var _catalogImagePlacement	= 'before';
var _catalogImagePLUS		= 'ulibCartImagePLUS.gif';
var _catalogImageMINUS		= 'ulibCartImageMINUS.gif';

var _catalogTableClass		= "catalog";
var _catalogDivId 			= "CATALOGDIV"
var _cartDivId				= "CARTDIV";
var _cartTableId		 	= "CARTTABLE";

var _catalogItemImage		= "itemImage";
var _catalogItemName		= "itemName";
var _catalogItemNumber		= "itemNumber";

var _catalogxmlCategory 	= "category"
var _catalogxmlItem		 	= "item"
var _catalogxmlDescription 	= "description";
var _catalogxmlImage 		= "image"
var _catalogxmlLink 		= "link";
var _catalogxmlPayPal		= "paypal";
var _catalogxmlPrice	 	= "price";
var _catalogxmlShipping		= "shipping";
var _catalogxmlTax	 		= "tax";

var _PayPalHandling	 		= 2.50;

var _cartitemColumns = [ 'name', _catalogxmlPrice, 'quantity', _catalogxmlShipping, _catalogxmlTax, 					
									_catalogxmlPayPal ];

function cart( cartname, email, returnsuccsss, returnfailure )
{
	this.cartitems		= new Array;
	this.cartname		= cartname;
	this.email			= email;
	this.returnfailure  = returnfailure;
	this.returnsuccess  = returnsuccess;
	
	this.additem = function( name, price, shipping, tax, paypal ) 
	{
		var i;
		var isnew = true;
		
		for( i = 0; i < this.cartitems.length && isnew; i++ )
			if ( this.cartitems[ i ].name == name ) 
			{
				this.cartitems[ i ].quantity++;
				isnew = false;
			}
		if( isnew )
		{
			var newItem = new cartitem();
			newItem.initialize( name, price, 1, shipping, tax, paypal );
			this.cartitems.push( newItem );
		}
		this.updatecookie();
		this.updatecart();
		return;
	};

	this.checkout = function()
	{
		var j;
		var strn  = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" +
		   			"&upload=1" +
		        	"&business=" + this.email + 
					"&currency_code=CAD" +
					"&return=" + this.returnsuccess +
					"&cancel_return=" + this.returnfailure +
					"&cpp_headerback_color=E4F6DC" + 
					"&cpp_headerborder_color=A29FE0" +
					"&cpp_payflow_color=E4F6DC" + 
					"&handling_cart=" + _PayPalHandling +
					"&lc=CA" //+ 
//					"&image_url=https://www.doulacare.ca/images/logo.gif" +
		var winpar = "scrollbars,location,resizable,status";
		var totalp;

		if ( this.cartitems.length == 0 )
			alert( "Your cart is empty!" );
		else
		{
			for ( j = 1; j <= this.cartitems.length; j++ )
			{ 
				tempItem = this.cartitems[ j - 1 ];
				totalp = parseFloat( tempItem.price ) + parseFloat( tempItem.paypal )
				strn += "&item_name_"    + j + "=" + tempItem.name +
						"&item_number_"  + j + "=" + j +
						"&quantity_"     + j + "=" + tempItem.quantity +
						"&amount_"       + j + "=" + miscNumberFormat( totalp, 2 ) +
						"&shipping_"     + j + "=" + miscNumberFormat( tempItem.shipping, 2 ) +
						"&tax_"          + j + "=" + miscNumberFormat( tempItem.tax, 2 )  +
						"&no_note_"   	 + j + "=" + "1";
			}
		//	window.open( strn, "paypal", winpar );
		document.location.replace(strn);
		}
		return false;
	};
	
	this.cartcontents = function()
	{
		var i;
		var disabled = this.cartitems.length ?  '' : 'disabled=1';
		var tot = 0;
		var tax = 0;
		var stot = 0;
		var shipping = 0;
		var paypal = 0;
		var html;
		var onclic;
		
		html = "<table id=" + this.carttableid + ">";
		html += "<tr>";
		html += "<th class='" + _catalogItemName + "'>Item</th>";
		html += "<th class='" + _catalogItemNumber + "'>Quantity</th>";
		html += "<th class='" + _catalogItemNumber + "'>Price</th>";
		html += "<th class='" + _catalogItemNumber + "'>Total</th>"
		html += "<th>&nbsp</th>"
		html += "</tr>";
		for ( i = 0; i < this.cartitems.length; i++ )
		{
			tax += this.cartitems[ i ].total( _catalogxmlTax );
			tot = this.cartitems[ i ].total( _catalogxmlPrice );
			stot += tot;
			shipping += this.cartitems[ i ].total( _catalogxmlShipping );
			html += "<tr>";
			html += "<td class='" + _catalogItemName + "'>" + this.cartitems[ i ].name + "</td>";
			html += "<td class='" + _catalogItemNumber + "'>" + this.cartitems[ i ].quantity + "</td>";
			html += "<td class='" + _catalogItemNumber + "'>" + miscNumberFormat( this.cartitems[ i ].price, 2 ) + "</td>";
			html += "<td class='" + _catalogItemNumber + "'>" + miscNumberFormat( tot, 2 ) + "</td>";
			onclic = this.cartname + '.deleteitem( "' + this.cartitems[ i ].name + '" );'
			html += "<td><input type='button' value='Remove' onclick='" + onclic + "' /></td>"
			html += "</tr>";
		}
		paypal = ( this.cartitems.length > 0 ) ? _PayPalHandling : 0;
		html += "<tr><td colspan=3 align='right'>Sub Total</td>" +
					"<td class='" + _catalogItemNumber + "'>" + miscNumberFormat( stot, 2 ) + "</td></tr>";
		html += "<tr><td colspan=3 align='right'>Tax</td>" + 
					"<td class='" + _catalogItemNumber + "'>" + miscNumberFormat( tax, 2 ) + "</td></tr>";
		html += "<tr><td colspan=3 align='right'>Handling (PayPal)</td>" + 
					"<td class='" + _catalogItemNumber + "'>" + miscNumberFormat( paypal, 2 ) + "</td></tr>";
		html += "<tr><td colspan=3 align='right'>Shipping</td>" + 
					"<td class='" + _catalogItemNumber + "'>" + miscNumberFormat( shipping, 2 ) + "</td></tr>";
		html += "<tr><td colspan=3 align='right'>Total Invoice</td>" +
					"<td class='" + _catalogItemNumber + "'>" +
					miscNumberFormat( stot + tax + shipping + paypal, 2 ) + "</td></tr>";
		html += "</table>";
		html += "<input type='button'" + disabled + " value='Empty Cart' onClick='" + this.cartname + ".empty();' />";
		html += "<input type='button'" + disabled + " value='Check Out' onClick='" + this.cartname + ".checkout();' />";
		return html;	
	};

	this.deleteitem = function( dname )
	{  
		var temp = new Array();
		var i;
		
		for ( i = 0; i < this.cartitems.length; i++ )
			if( this.cartitems[ i ].name != dname  )
				temp.push( this.cartitems[ i ] );
		this.cartitems = temp;
		this.updatecookie();
		this.updatecart();
		return;
	};

	this.empty = function() 
	{
		this.cartitems = new Array();
		this.updatecookie();
		this.updatecart();
		return false;
	};

	this.initialize = function()
	{
		var pair;
		var i;
		var itemData;
		var data = cookieRead( this.cartname )
		
		if ( data ) 
		{
			data = data.split( "&" );
			for ( i = 0; i < data.length; i++ )
			{
				var newItem = new cartitem();
				itemData = data[ i ].split( "," );
				for( j = 0; j < itemData.length; j++ )
				{
					pair = itemData[ j ].split( '=' );
					newItem[ pair[ 0 ] ] = pair[ 1 ]
				}
				this.cartitems[ i ] = newItem;
			}
		}
		this.updatecookie();
		this.updatecart();
		return;
	};
	
	this.total = function( what )
	{
		var i;
		var result = 0;

		for ( i = 0; i < this.cartitems.length; i++ )
			result += this.cartitems[ i ].total( what );
		return result;
	};
	
	this.updatecookie = function()
	{
		var i;
		var cookieString = '';

		for ( i = 0; i < this.cartitems.length; i++ )
			cookieString += ( ( i == 0 ) ? "" : "&" ) + this.cartitems[ i ].cookie();
		cookieCreate( this.cartname, cookieString, 30 );
	};

	this.updatecart = function()
	{
		divClear( _cartDivId );
		divWrite( _cartDivId, this.cartcontents() );
	};
	
	this.initialize();
}

function cartitem()
{
	this.cookie = function()
	{
		var fld;
		var i;
		var result = '';
	
		for( i = 0; i < _cartitemColumns.length; i++ )
		{
			fld = _cartitemColumns[ i ];
			result += ( ( i == 0 ) ? '' : ',' ) + fld + "=" + this[ fld ];
		}
		return result;
	};
	
	this.initialize = function( name, price, quantity, shipping, tax, paypal )
	{
		this.name = name;
		this.price = price;
		this.quantity = quantity;
		this.shipping = shipping;
		this.tax = tax;
		this.paypal = paypal;
	};
	
	this.total = function( what )
	{
		return this[ what ] * this.quantity;
	};

}

function catalogLoader( xmlDoc, cartname )
{
	divClear( _catalogDivId );
	divWrite( _catalogDivId, catalogTable( xmlDoc, cartname ) );
	eval( cartname + ".updatecart()" );
}

function catalogShow( dname )
{
	divToggle( dname );	
}

function catalogTable( xmlDoc, cartname )
{
	var i;
	var j;
	var clist = xmlDoc.documentElement.getElementsByTagName( _catalogxmlCategory )
	var ccount = clist.length;
	var ilist;
	var icount;
	var it;
	var html = '';
	var onclic;
	var name;
	var price;
	var paypal = 0;
	var shipping;
	var tax;
	var temp;

	for ( j = 0; j < ccount; j++ ) 
	{
		onclic = "catalogtable" + j;
		temp = 'javascript:catalogShow( "' + onclic + '")';
		html += "<a href='" + temp + "')><h2>" + clist[ j ].firstChild.text + "</h2></a>";
		html += "<div id='" + onclic + "' style='display:none'><table class='" + _catalogTableClass + "'>";
		html += "<tr>";
		html += "<th class='" + _catalogItemName + "'>Item</th>";
		html += "<th class='" + _catalogItemNumber + "'></th>";
		html += "<th class='" + _catalogItemNumber + "'>Price</th>";
		html += "<th class='" + _catalogItemNumber + "'>Shipping</th>";
		html += "<th class='" + _catalogItemNumber + "'>Tax</th>"
	//	html += "<th class='" + _catalogItemNumber + "'>PayPal</th>"
		html += "<th class='" + _catalogItemNumber + "'>Total</th>"
		html += "</tr>";
		ilist = clist[ j ].getElementsByTagName( _catalogxmlItem );
		icount = ilist.length;
		for ( i = 0; i < icount; i++ )
		{
			html += "<tr>";
			name = ilist[ i ].firstChild.text;
			it = name;
			lnk = ilist[ i ].getElementsByTagName( _catalogxmlLink )[ 0 ].text;
			if ( lnk )
				it = "<a target='_blank' href='" + lnk + "'>" + it + "</a>";
			html += "<td class='" + _catalogItemName + "'>" + it + "</td>";
			lnk = ilist[ i ].getElementsByTagName( _catalogxmlImage )[ 0 ].text;
			if ( lnk )
				lnk = "<img src='" + lnk + "' class='" + _catalogItemImage + "'>";
			html += "<td>" + lnk + "</td>";
			price = catalogNumber( ilist[ i ], _catalogxmlPrice );
			shipping = catalogNumber( ilist[ i ], _catalogxmlShipping );
			tax = catalogNumber( ilist[ i ], _catalogxmlTax );
	//		paypal = catalogNumber( ilist[ i ], _catalogxmlPayPal );
			html += "<td class='" + _catalogItemNumber + "'>" + miscNumberFormat( price, 2 ) + "</td>";
			html += "<td class='" + _catalogItemNumber + "'>" + miscNumberFormat( shipping, 2 ) + "</td>";
			html += "<td class='" + _catalogItemNumber + "'>" + miscNumberFormat( tax, 2 ) + "</td>";
	//		html += "<td class='" + _catalogItemNumber + "'>" + miscNumberFormat( paypal, 2 ) + "</td>";
			html += "<td class='" + _catalogItemNumber + "'>" + 
							miscNumberFormat( price + shipping + tax + paypal, 2 ) + "</td>";
			onclic = cartname + '.additem("' + name + '", ' + price + ', ' + shipping + ', ' + tax + ', ' 
										  + paypal + ')';
			html += "<td><input type='button' value='Add' onclick='" + onclic + "' /></td>"
			html += "</tr>";
		}
		html += "</table></div>";
	}
	return html;
}

function catalogNumber( inod, fname )
{
	var v = inod.getElementsByTagName( fname )[ 0 ].text;
	
	v = ( v.length == 0 ) ? 0 : parseFloat( v );
	return v;
	
}


