//###########################################################################
//# salesystems/doit24 ShoppingCart - Component including ShoppingCartEntry #
//# version: 1.0.1                                                          #
//# last-updated: 2009-03-25                                                #
//#                                                                         #
//# requirements                                                            #
//# ============                                                            #
//# - loadCookie: main_lib.js                                               #
//###########################################################################



ShoppingCartEntry = function() {
	var ProductID = 0, Quantity = 0, isAlternate = false;
	
	this.setProductID = function(pid) {ProductID = pid;}
	this.setProductQuantity = function(quan) {Quantity = quan;}
	this.setProductAlternate = function(isAlt) {isAlternate = isAlt;}
	
	this.getProductID = function() {return ProductID;}
	this.getProductQuantity = function() {return Quantity;}
	this.getProductIsAlternate = function() {return isAlternate;}
}


ShoppingCart = function() {
	
	CookieValueDelimiter = "_";
	
	
	cart_entry_arr = new Array();
	
	
	this.addProductById = function(pid) {
		var new_cart_entry = new ShoppingCartEntry();
		new_cart_entry.setProductID(pid);
		
		cart_entry_arr[cart_entry_arr.length] = new_cart_entry;
	}
	
	this.addProduct = function(entry) {
		for (i=0; i<cart_entry_arr.length; i++) {
			if (cart_entry_arr[i].getProductID()==entry.getProductID()) cart_entry_arr.splice(i, 1);
		}
		
		cart_entry_arr[cart_entry_arr.length] = entry;
	}
	
	this.deleteProduct = function(pid) {
		for (i=0; i<cart_entry_arr.length; i++) {
			if (cart_entry_arr[i].getProductID()==pid) cart_entry_arr.splice(i, 1);
		}
	}
	
	
	this.getProductCount = function() {
		return cart_entry_arr.length;
	}
	
	this.getProductQuantity = function(pid) {
		for (i=0; i<cart_entry_arr.length; i++) {
			if (cart_entry_arr[i].getProductID()==pid) {
				return cart_entry_arr[i].getProductQuantity();
				break;
			}
		}
		return 0;
	}
	
	this.getProductIsAlternate = function(pid) {
		for (i=0; i<cart_entry_arr.length; i++) {
			if (cart_entry_arr[i].getProductID()==pid) {
				return cart_entry_arr[i].getProductIsAlternate();
				break;
			}
		}
		return false;
	
	}
	
	this.swapProductIsAlternate = function(pid) {
		for (i=0; i<cart_entry_arr.length; i++) {
			if (cart_entry_arr[i].getProductID()==pid) {
				cart_entry_arr[i].setProductAlternate(!cart_entry_arr[i].getProductIsAlternate());
			}
		}
	}
	
	
	
	this.loadCookie = function() {
		c_val = getCookieValue("shoppingcart");
		if (c_val.substr(0, 1)=="\"" && c_val.substr(c_val.length-1, c_val.length)=="\"") c_val = c_val.substr(1, c_val.length-2);
		
		c_vars = c_val.split(CookieValueDelimiter);
		for (i=0; i<c_vars.length; i++) {
			if (c_vars[i].indexOf("=")>-1) {
				var cur_var = c_vars[i].split("=");
				var cur_entryprops = cur_var[1].split("|");
				var cur_entryprop_q = cur_entryprops[0].split(":");
				var cur_entryprop_a = cur_entryprops[1].split(":");
				var cur_quan = cur_entryprop_q[1];
				var cur_isAlt = (cur_entryprop_a[1]==1);
				
				var cart_entry = new ShoppingCartEntry();
				cart_entry.setProductID(cur_var[0]);
				cart_entry.setProductQuantity(cur_quan);
				cart_entry.setProductAlternate(cur_isAlt);
				
				this.addProduct(cart_entry);
			}
		}
	}
	
	this.saveCookie = function() {
		cookie_value = "\"";
		for (i=0; i<cart_entry_arr.length; i++) {
			cookie_value += cart_entry_arr[i].getProductID()+
				"=q:"+cart_entry_arr[i].getProductQuantity()+
				"|a:"+(cart_entry_arr[i].getProductIsAlternate()? "1": "0");
			
			if ((i+1)<cart_entry_arr.length) cookie_value += CookieValueDelimiter;
		}
		cookie_value += "\"";
		
		exp_date = new Date();
		exp_date.setYear(exp_date.getFullYear() + 1);
		
		document.cookie = "shoppingcart=" + cookie_value + "; expires=" + exp_date.toGMTString() + "; path=/";
	}
	
	
	this.loadCookie();

}


ShoppingCart = new ShoppingCart();
//### ShoppingCart - END ###