//***************************************************
//***** CODE COPYRIGHT PRACTICAL DATA, INC 2004 *****
//***** - USE WITHOUT PERMISSION IS PROHIBITED ******
//***************************************************
function getCookie(cookieName) {
	var allCookies = document.cookie;
	var cookieStart = allCookies.indexOf(cookieName)
	if(cookieStart == -1) return "";
	cookieStart = cookieStart + cookieName.length + 1;
	var cookieEnd = allCookies.indexOf(";", cookieStart);
	if(cookieEnd == -1) cookieEnd = allCookies.length;
	var cookieValue = allCookies.substring(cookieStart, cookieEnd);
	return cookieValue;
}

function getShoppingCart() {
	var storeCode = getStoreCode();
	var cartCookieInfoName = "pdsc_" + storeCode + "_info";
	var cartCookieName = "pdsc_" + storeCode + "_";
	var shoppingCart = new Object();
	shoppingCart.products = new Array();
	var pd_shoppingCartInfo = getCookie(cartCookieInfoName);
	var cookieValue = "";
	for(var i=0;i<pd_shoppingCartInfo;i++) {
		cookieValue += getCookie(cartCookieName + i);
	}
	cookieValue = unescape(cookieValue);
	
	if(cookieValue.length > 10) shoppingCart = decodeShoppingCart(cookieValue);
	shoppingCart.subtotal = 0;
	if(shoppingCart.products) {
		for(var i=0;i<shoppingCart.products.length;i++) {
			shoppingCart.subtotal += (shoppingCart.products[i].price * shoppingCart.products[i].qty); 
			//alert(shoppingCart.products[i].price + " * " + shoppingCart.products[i].qty + " = " + shoppingCart.subtotal);
		}
	}
	return shoppingCart;
}

function getCurrentPagePath() {
	var url = String(location);
	url = url.replace("http://", "");
	var slashIndex = url.lastIndexOf("/");
	if(slashIndex == -1) slashIndex = url.length;
	var pagePath = url.substring(0, slashIndex);
	return pagePath;
}

function decodeShoppingCart(encodedCart) {

	var shoppingCart = new Object();
	shoppingCart.isTrimmed = false;
	shoppingCart.products = new Array();
	
	//**************************
	//*** TEST FOR CART TRIM ***
	//**************************
	if(encodedCart.charAt(0) == '+') {
		shoppingCart.isTrimmed = true;
		encodedCart = encodedCart.substring(1, encodedCart.length);
	}
	
	//*************************
	//**** SPLIT CART ROWS ****
	//*************************	
	var tempArray = encodedCart.split("~");	
	var tempArray2 = new Array();
	var i = 0;
	var item = new Object();
	for(item in tempArray) {
		if(tempArray.hasOwnProperty(item)) {
			//************************************************
			//*** SPLIT APART ATTRIBUTES & BUILD CART ITEM ***
			//************************************************
			tempArray2 = tempArray[item].split("^");
			
			shoppingCart.products[i] = new Object();
			shoppingCart.products[i].productId = tempArray2[0];
			shoppingCart.products[i].name = decodeSpecialCharacters(tempArray2[1]);
			shoppingCart.products[i].options = decodeSpecialCharacters(tempArray2[2]);
			shoppingCart.products[i].price = tempArray2[3];
			shoppingCart.products[i].qty = tempArray2[4];
			shoppingCart.products[i].inStock = tempArray2[5];
			i++;
		}
	}
	//**************
	//*** RETURN ***
	//**************
	return shoppingCart;
}

function decodeSpecialCharacters(value) {
	
	//**************
	//*** DECODE ***
	//**************
	value = String(value);
	value = value.replace(/\\"/g, '"');
	value = value.replace(/\\r/g, '\r');
	value = value.replace(/\\n/g, '\n');
	
	//***************
	//*** RETURN ***
	//**************
	return value;
}


function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).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 (((sign)?'':'-') + '$' + num + '.' + cents);
}
