

// This file will write out all of the Javascrpt function relating to the shopping
// cart.



	function getCookieVal(offset) {
		var endstr = document.cookie.indexOf(";", offset);
		if(endstr == -1)
			endstr = document.cookie.length;
		return(unescape(document.cookie.substring(offset, endstr)));
	}

	function GetCookie(name) {
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
	
		while(i < clen) {
			var j = i + alen;
			if(document.cookie.substring(i, j) == arg) {
				return (getCookieVal(j));
			}
			i = document.cookie.indexOf(" ", i) + 1;
			if(i == 0 )
				break;
		}
		return null;
	}


	function DeleteCookie(name, path, domain) {
		if( GetCookie(name)) {
			document.cookie = name + "=" +
			((path) ? "; path=" + path : "" ) +
			((domain) ? "; domain=" + domain : "" ) +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	}


	function SetCookie(name, value, expires, path, domain, secure) {
		var escvalue = escape(value);
		document.cookie = name + "=" + escvalue +
		((expires) ? "; expires=" + expires : "" ) +
		((path) ? "; path=" + path : "" ) +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure=" + secure : "");
	}

	function GetShoppingCartItems() {
	// Check to see if there are any items in the shopping cart.
		var itemCount = parseInt(GetCookie('shopcount'));
		if(itemCount <= 0)
			return null;
	
	// Get the items cookie string.
	// This string contains the items in the shopping cart.  Each item is separated by 
	// an ampersand(&).  Each field in the item is separated by a # sign.
	// Each item consists of a catalogid#quantity#cost(#optional extra shipping)
		var items = GetCookie('shopitems');
		if(!items)
			return null;
		
		var itemArray = items.split('&');
		return (itemArray);
	}

// This function will add an item to the shopping cart
	function Add_Item_toCart(qty, catalogID, cost, shipping) {
		var itemCount = parseInt(GetCookie('shopcount'));
		if(isNaN(itemCount))
			itemCount = 1;
		else
		  itemCount++;
			
		var itemString = GetCookie('shopitems');
	
		var newItem;
		if(itemCount > 1)
			newItem = "&";
		else
			newItem = "";
		newItem += catalogID +"#";
		newItem += qty + "#" + cost;
		if(shipping)
			newItem += "#" + shipping;
		
		if(itemString != null)
			itemString += newItem;
		else
			itemString = newItem;
		
	//alert(\"Adding item to cart. String is \" + itemString + \" count is \" + itemCount);
		SetCookie('shopcount', itemCount, null, '/', null, 0);
		SetCookie('shopitems', itemString, null, '/', null, 0);
	
	}
	
	// Remove items all items from the cart
	function Reset_Cart() {
		SetCookie('shopcount', 0, null, null, null, 0);
		SetCookie('shopitems', null, null, null, null, 0);
	}
	
	function Change_Qty_inCart(catalog_id, new_qty, form) {
	
			var ItemArray = GetShoppingCartItems();
		// Split each item into separate parts
			var cost = 0.0;
			var Item;
			var index = 0;
		
			if(ItemArray == null)
			{
				return 0.0;
			}
		
		// Reset the cart
		//	Reset_Cart();
			
			SetCookie('shopcount', 0, null, '/', null, 0);
			SetCookie('shopitems', "", null, '/', null, 0);
			while(index < ItemArray.length) {
				Item = ItemArray[index].split('#');
				if(Item[0] == catalog_id)
				{
					Item[1] = new_qty;
				}
				Add_Item_toCart(Item[1], Item[0], Item[2], Item[3]);
				index++;
			
			}
	
			return true;
	
		}
	
		function Remove_Item_fromCart(catalog_id) {
	
			var ItemArray = GetShoppingCartItems();
		// Split each item into separate parts
			var Item;
			var index = 0;
		
			if(ItemArray == null)
				return 0.0;
		
		// Reset the cart
		//	Reset_Cart();
			SetCookie('shopcount', 0, null, '/', null, 0);
			SetCookie('shopitems', "", null, '/', null, 0);
			while(index < ItemArray.length) {
				Item = ItemArray[index].split('#');
				if(Item[0] != catalog_id)
		{
					Add_Item_toCart(Item[1], Item[0], Item[2], Item[3]);
		}
				index++;
			
			}
	
			return true;
		}
	
	
	
	function formatCurrency(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num)) num = "0";
			cents = Math.floor((num*100+0.5)%100);
		num = Math.floor(num).toString();
		if(cents < 10) cents = "0" + cents;
	//		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	//			num =";
	//				num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		return (num + '.' + cents);
	}

	function format_curr_to_string(num) {
		if(isNaN(num)) num = "0";
			cents = Math.floor((num*100+0.5)%100);
		num = Math.floor(num).toString();
		if(cents < 10) cents = "0" + cents;
			for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
				num =					num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		return (num + '.' + cents);
	}


	function CalculateCost() {
		var ItemArray = GetShoppingCartItems();
	// Split each item into separate parts
		var cost = 0.0;
		var Item;
		var index = 0;
	
		if(ItemArray == null)
			return 0.0;
	
		while(index < ItemArray.length) {
			Item = ItemArray[index].split('#');
		// cost = quantity * price per item
			cost += parseFloat(Item[1]) * parseFloat(Item[2]);
			index++;
		}

		var formatted_cost = formatCurrency(cost);

		return (parseFloat(formatted_cost));
	}


	function Display_Cost() {
		var cost = CalculateCost();
		var curr_string = format_curr_to_string(cost);
		return(curr_string);
	}

	function CalculateShipping() {
	// Split each item into separate parts
		var ItemArray = GetShoppingCartItems();
		var shipping = 0.0;
		var Item;
		var index = 0;		
        var g_costCeiling1 = 100;
        var g_costCeiling2 = 299;
        var g_costCeiling3 = 499; 
        var g_costCeiling4 = 699;
        var g_costCeiling5 = 999; 
        var g_costCeiling6 = 1499;
        var g_costCeiling7 = 1999;
        var g_costCeiling8 = 2499;
        var g_costCeiling9 = 2999;
        var g_costCeiling10 = 3999;
        var g_costCeiling11 = 4999;
        var g_costCeiling12 = 5999;        
        
         // Amount you'll have to pay if you fall into corresponding category
        var g_costamount1 = 7;
        var g_costamount2 = 8.25;
        var g_costamount3 = 9.75;
        var g_costamount4 = 12.75;
        var g_costamount5 = 14.75;
        var g_costamount6 = 16.75;
        var g_costamount7 = 19.75;
        var g_costamount8 =21.75;
        var g_costamount9 = 26.75;
        var g_costamount10 = 36.75;
        var g_costamount11 = 41.75;
        var g_costamount12 = 50;		
		
	
		if(ItemArray == null)
			return 0.0;
	
		while(index < ItemArray.length) {
			Item = ItemArray[index].split('#');
			if(Item.length > 3)
				shipping += parseFloat(Item[3]) * parseFloat(Item[1]);
			index++;
		}
	
		var cost = CalculateCost();
		if(cost == 0)
			shipping = 0.00;
		else if(cost < g_costCeiling1)
			shipping += g_costamount1;
		else if(cost < g_costCeiling2)
			shipping += g_costamount2;
		else if(cost < g_costCeiling3)
			shipping += g_costamount3;
		else if(cost < g_costCeiling4)
			shipping += g_costamount4;
		else if(cost < g_costCeiling5)
			shipping += g_costamount5;
		else if(cost < g_costCeiling6)
			shipping += g_costamount6;
    	else if(cost < g_costCeiling7)
			shipping += g_costamount7;
		else if(cost < g_costCeiling8)
			shipping += g_costamount8;
    	else if(cost < g_costCeiling9)
    		shipping += g_costamount9;
    	else if(cost < g_costCeiling10)
    		shipping += g_costamount10;
    	else if(cost < g_costCeiling11)
    		shipping += g_costamount11;
    	else if(cost < g_costCeiling12)
    		shipping += g_costamount12;
    	else
    		shipping += g_costamount12;
	
		return (parseFloat(shipping));
	}

	function CalculateItems() {
		var itemcount = GetCookie('shopcount');
		if(itemcount == null)
			return(0);
		return(parseInt(itemcount));
	}


