//### prototypes ###
/*** working, but not needed ***
Array.prototype.contains = function(a) {
	res = false;
	for (i=0; i<this.length; i++) {
		if (this[i]==a) {
			res = true;
			break;
		}
	}
	
	return res;
}*/
//### prototypes - end ###



//### standard-functions ###
function GE(obj_name) {
	var res = document.getElementById(obj_name);
	if (res==null) document.getElementsByName(obj_name)[0];
	
	return res;
}

function GEs(name) {
	var res = document.getElementsByName(name);
	if (res==null || res.length<1) res = document.getElementsByTagName(name);
	
	return res;
}

function GO(loc) {
	document.location.href = loc;
}



function objExists(obj_name) {
	return (GE(obj_name)!=null);
}


function boxhead_over(obj) {
	obj.className = "boxhead_ovr";
}


function boxhead_out(obj) {
	obj.className = "boxhead";
}


function objDisplayShow(obj_id) {
	GE(obj_id).style.display = '';
}

function objDisplayHide(obj_id) {
	GE(obj_id).style.display = 'none';
}


function toggleDisplay(obj_id) {
	ob = GE(obj_id);
	ob.style.display = (ob.style.display=='none')? '': 'none';
}


function AddEvent(obj, evt_name, fn_name) {
	if (obj.addEventListener) {
		//### Firefox, Opera, Safari
		obj.addEventListener(evt_name, fn_name, false);
	} else if (all_links[i].attachEvent) {
		//### IE
		obj.attachEvent("on"+evt_name, fn_name);
	} else {
		try {
			eval("obj.on"+evt_name+" = fn_name");
		} catch(e) {}
	}
}


function getCookieValue(c_name) {
	a = document.cookie;
	ac = a.split("; ");
	r = "";
	
	for (i=0; i<ac.length; i++) {
		if (c_name==ac[i].substr(0, ac[i].search('='))) {
			r = ac[i].substr(ac[i].search('=')+1, ac[i].search(';'));
			if (r=='') r = ac[i].substr(ac[i].search('=')+1, ac[i].length);
			
			break;
		}
	}
	
	return r;
}


function checkCookie() {
	//cd = new Date();
	//cd.setMinutes(cd.getMinutes()+1);
	
	//# test-cookie should expire on end of session, not in just 1 min.
	//document.cookie = "c_test=ok; expires="+cd.toGMTString();
	document.cookie = "c_test=ok; path=/";
	
	if (getCookieValue("c_test")!="ok") objDisplayShow("cookieNotSupported_div");
}


function MengeChange(prod_id, update_page) {
	f = GE("artikel");
	m = GE("menge" + prod_id);
	mv = m.value;
	fa = f.action;
	scrollY = (isIE)? document.body.scrollTop: window.pageYOffset;
	
	//### zusatz-parameter mit abschicken ###
	add_params = "";
	input_obs = GEs("input");
	for (i=0; i<input_obs.length; i++) {
		if (input_obs[i].name.indexOf("MengeChangeParam")>-1) {
			param_name = input_obs[i].name.split("_")[1];
			param_value = input_obs[i].value;
			add_params += "&" + param_name + "=" + param_value;
		}
	}
	
	if(mv==0) {
		if (confirm("Wollen Sie dieses Produkt aus dem Warenkorb löschen?")) {
			ShoppingCart.deleteProduct(prod_id);
			ShoppingCart.saveCookie();
			
			getData(js_domain+'templates/wkorb_klein_ajax.jsp', 'ShoppingCartSpace');
			if (update_page) GO(fa);
			
			//GO(fa+'&action=shoppingcart_delete&shoppingcart_product='+prod_id+'&quantity=0&scrollY='+scrollY+add_params);
		} else {
			m.value = 1;
		}
	}
	
	if(isNaN(parseFloat(mv))) m.value=0;
	if(mv>0) {
		if (objExists("load_div")) toggleDisplay("load_div");
		
		var new_cart_entry = new ShoppingCartEntry();
		new_cart_entry.setProductID(prod_id);
		new_cart_entry.setProductQuantity(mv);
		new_cart_entry.setProductAlternate(true);
		
		ShoppingCart.addProduct(new_cart_entry);
		ShoppingCart.saveCookie();
		
		getData(js_domain+'templates/wkorb_klein_ajax.jsp', 'ShoppingCartSpace');
		if (update_page) GO(fa);
		
		//GO(fa+'&action=shoppingcart_add&shoppingcart_product='+prod_id+'&quantity='+mv+'&scrollY='+scrollY+add_params);
	}
}



//### Session-Cookie Component ###
SessionCookie = function() {
	
	CookieValueDelimiter = "_";
	
	UserID = 0, CountryID = 0, ShipToID = 0, BillToID = 0, SubMenuLevel = 0, 
		CurrentProductID = 0, ProductsOnPage = 0, CatID = 0, SubCatID = 0;
	JSessionID = "", CurrentManufacturer = "", ProductsSortBy = "",VariableField1 = "",
	VariableField2 = "",VariableField3 = "",VariableField4 = "",VariableField5 = "";
	
	
	this.setSessionID = function(sid) {JSessionID = sid;}
	this.setUserID = function(uid) {UserID = uid;}
	this.setCountryID = function(cid) {CountryID = cid;}
	this.setShipToID = function(stid) {ShipToID = stid;}
	this.setBillToID = function(btid) {BillToID = btid;}
	this.setCatID_old = function(cid) {CatID = cid;}
	this.setCatID = function(cid) {CatID = cid;}
	this.setSubCatID = function(cid) {SubCatID = cid;}
	this.setSubMenuLevel = function(sml) {SubMenuLevel = sml;}
	this.setCurrentProductID = function(pid) {CurrentProductID = pid;}
	this.setProductsOnPage = function(pop) {ProductsOnPage = pop;}
	this.setCurrentManufacturer = function(s_man) {CurrentManufacturer = s_man;}
	this.setProductsSortBy = function(sort) {ProductsSortBy = sort;}
	
	//##HB# die variablen Felder füllen ##
	this.setProductsVariableField1 = function(field1) {VariableField1 = field1;}
	this.setProductsVariableField2 = function(field2) {VariableField2 = field2;}
	this.setProductsVariableField3 = function(field3) {VariableField3 = field3;}
	this.setProductsVariableField4 = function(field4) {VariableField4 = field4;}
	this.setProductsVariableField5 = function(field5) {VariableField5 = field5;}
	
	this.getProductsVariableField1  = function() {return VariableField1;}
	this.getProductsVariableField2  = function() {return VariableField2;}
	this.getProductsVariableField3  = function() {return VariableField3;}
	this.getProductsVariableField4  = function() {return VariableField4;}
	this.getProductsVariableField5  = function() {return VariableField5;}
	
	
	this.getSessionID = function() {return JSessionID;}
	this.getUserID = function() {return UserID;}
	this.getCountryID = function() {return CountryID;}
	this.getShipToID = function() {return ShipToID;}
	this.getBillToID = function() {return BillToID;}
	this.getCatID_old = function() {return CatID;}
	this.getCatID = function() {return CatID;}
	this.getSubCatID = function() {return SubCatID;}
	this.getSubMenuLevel = function() {return SubMenuLevel;}
	this.getCurrentProductID = function() {return CurrentProductID;}
	this.getProductsOnPage = function() {return ProductsOnPage;}
	this.getCurrentManufacturer = function() {return CurrentManufacturer;}
	this.getProductsSortBy = function() {return ProductsSortBy;}
	
	
	
	this.saveCookie = function() {
		cookie_value =  "\"" +
			"SessionID=" + JSessionID + CookieValueDelimiter +
			"UserID=" + UserID + CookieValueDelimiter +
			"CountryID=" + CountryID + CookieValueDelimiter +
			"ShipToID=" + ShipToID + CookieValueDelimiter +
			"BillToID=" + BillToID + CookieValueDelimiter +
			"CatID=" + CatID + CookieValueDelimiter +
			"SubCatID=" + SubCatID + CookieValueDelimiter +
			"SubMenuLevel=" + SubMenuLevel + CookieValueDelimiter +
			"CurrentProductID=" + CurrentProductID + CookieValueDelimiter +
			"ProductsOnPage=" + ProductsOnPage + CookieValueDelimiter +
			"CurrentManufacturer=" + CurrentManufacturer + CookieValueDelimiter +
			"VariableField1=" + VariableField1 + CookieValueDelimiter +
			"VariableField2=" + VariableField2 + CookieValueDelimiter +
			"VariableField3=" + VariableField3 + CookieValueDelimiter +
			"VariableField4=" + VariableField4 + CookieValueDelimiter +
			"VariableField5=" + VariableField5 + CookieValueDelimiter +
			"ProductsSortBy=" + ProductsSortBy + "\"";
		
		exp_date = new Date();
		exp_date.setYear(exp_date.getFullYear() + 1);
		
		document.cookie = "session=" + cookie_value + "; expires=" + exp_date.toGMTString() + "; path=/";
	}
	
	
	this.loadCookie = function() {
		c_val = getCookieValue("session");
		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) {
				cur_var = c_vars[i].split("=");
				
				if (cur_var[0]=="SessionID") this.setSessionID(cur_var[1]);
				if (cur_var[0]=="UserID") this.setUserID(cur_var[1]);
				if (cur_var[0]=="CountryID") this.setCountryID(cur_var[1]);
				if (cur_var[0]=="ShipToID") this.setShipToID(cur_var[1]);
				if (cur_var[0]=="BillToID") this.setBillToID(cur_var[1]);
				if (cur_var[0]=="CatID") this.setCatID(cur_var[1]);
				if (cur_var[0]=="SubCatID") this.setSubCatID(cur_var[1]);
				if (cur_var[0]=="SubMenuLevel") this.setSubMenuLevel(cur_var[1]);
				if (cur_var[0]=="CurrentProductID") this.setCurrentProductID(cur_var[1]);
				if (cur_var[0]=="ProductsOnPage") this.setProductsOnPage(cur_var[1]);
				if (cur_var[0]=="CurrentManufacturer") this.setCurrentManufacturer(cur_var[1]);
				if (cur_var[0]=="VariableField1") this.setProductsVariableField1(cur_var[1]);
				if (cur_var[0]=="VariableField2") this.setProductsVariableField2(cur_var[1]);
				if (cur_var[0]=="VariableField3") this.setProductsVariableField3(cur_var[1]);
				if (cur_var[0]=="VariableField4") this.setProductsVariableField4(cur_var[1]);
				if (cur_var[0]=="VariableField5") this.setProductsVariableField5(cur_var[1]);
				if (cur_var[0]=="ProductsSortBy") this.setProductsSortBy(cur_var[1]);
			}
		}
	}
	
	
	this.loadCookie();
	
}

SessionCookie = new SessionCookie();
//### Session-Cookie - END ###



//### ShoppingCart - Component including ShoppingCartEntry ###
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 ###
