/*
	CMSZWO Shop Public JS Scripts
*/

// ------------------------------------ 
// Helper Functions
// ------------------------------------ 	
var spChars =  "*+-./_@";
function escapeSpChars(text, find, repl)
{
	var found = text.indexOf(find);
	var retVal= ""; var start = 0;
	while(found != -1)
	{
		retVal += text.substring(start, found) + repl;
		start = found + find.length;
		found =text.indexOf(find, start);
	}
	retVal += text.substring(start, text.length);
	return retVal;
}

function setWindowValue(eingabe) 
{
	var a=escape(eingabe);
	for(var i=0; i<spChars.length; i++)
	{
		a = escapeSpChars(a,spChars.charAt(i), "%X"+i);
	}
	a = escapeSpChars(a, "%", "_");
	self.name=a;
}

function getWindowValue() 
{
	var b = "" + self.name;
	b = escapeSpChars(b, "_", "%");
	for(var i=0; i<spChars.length; i++)
	{
		b = escapeSpChars(b, "%X"+i, spChars.charAt(i));
	}	
	b = unescape(b);
	return b;
}

// ------------------------------------ 
// Class ItemCart
// ------------------------------------ 	
function ItemCart()
{
	this.items = new Array();
	
	function addItem(id, price, count, bulk)
	{
		foundIndex = -1;
		for(i=0; i<this.items.length; i++)
		{
			if(this.items[i] != null && this.items[i].id == id)
			{
				foundIndex = i;
				break;
			}
		}
		
		if(foundIndex != -1)
		{
			this.items[i].count = (this.items[i].count*1) + (count*1);
			if(this.items[i].count <= 0)
			{
				this.removeItem(id);
			}
		}
		else
		{
			obj = new CartItem(id, price, count);
			this.items[this.items.length] = obj;
		}
		
		if(bulk != true)
		{
			this.saveCart();
		}
	}	
	this.addItem = addItem;
	
	function removeItem(id)
	{
		for(i=0; i<this.items.length; i++)
		{
			if(this.items[i] != null && this.items[i].id == id)
			{
				this.items[i] = null;
				break;
			}
		}
		
		this.saveCart();
	}
	this.removeItem = removeItem;
	
	function setItemCount(id, count)
	{
		if(count <= 0)
		{
			this.removeItem(id);
		}
		else
		{
			for(i=0; i<this.items.length; i++)
			{
				if(this.items[i] != null && this.items[i].id == id)
				{
					this.items[i].count = count;
					break;
				}
			}
		}
		
		this.saveCart();
	}
	this.setItemCount = setItemCount;
	
	function toString()
	{
		result = "";
		for(i=0; i<this.items.length; i++)
		{
			if(this.items[i] != null)
			{
				result += this.items[i].toString();
			}
		}
		return result;
	} 
	this.toString = toString;
	
	function empty()
	{
		this.items = new Array();
		this.saveCart();
	} 
	this.empty = empty;

	function getTotal()
	{
		result = 0;
		for(i=0; i<this.items.length; i++)
		{
			if(this.items[i] != null)
			{
				if(this.items[i].price != null)
				{
					result += this.items[i].price * this.items[i].count;
				}
			}
		}
		return result;
	} 
	this.getTotal = getTotal;

	function loadCart()
	{
		raw = getWindowValue();	
		if(raw==null || raw == "")
		{
			raw = "" + readCookie("itemCart");
			setWindowValue(raw);
		}
		
		tokens = raw.split('#');
		for(i=0; i<tokens.length; i++)
		{
			prop = tokens[i].split('=');
			uprop = prop[0].split(':');
			if(prop.length > 1)
			{
				this.addItem(uprop[0], uprop[1], prop[1], true);
			}
		}
	}
	this.loadCart = loadCart;
	
	function saveCart()
	{
		setWindowValue(this.toString());
		writeCookie("itemCart", this.toString());
	}
	this.saveCart = saveCart;

	this.loadCart();
}

itemCart = new ItemCart();

// ------------------------------------ 
// Class CartItem
// ------------------------------------ 	
function CartItem(id, price, count)
{
	this.id = id;
	this.price = price;
	this.count = count;
	
	function toString()
	{
		return this.id + ":" + this.price + "=" + this.count + "#";
	}
	this.toString = toString;
}

// ------------------------------------ 
// BÖHLER Interface Functions
// ------------------------------------ 	
function renderShopItemToTarget(targetId, type, p1, p2, p3, p4, p5, tm, ma, dim, price)
{
	setDivHTML(targetId, renderShopItem(type, p1, p2, p3, p4, p5, tm, ma, dim, price));
}

function renderShopItem(type, p1, p2, p3, p4, p5, tm, ma, dim, price)
{
	var result = "";
	if(type == "prz")
	{
		result = tm + "<br>Pr&auml;zisionsflach- u. Vierkantstahl / " + p1 + " x " + p2 + " x " + p3;
	}
	else if(type == "prza")
	{
		result = tm + "<br>Pr&auml;zisionsflachstahl mit Bearbeitungsaufma&szlig; / " + p1 + " x " + p2 + " x " + p3;
	}
	else if(type == "prz0")
	{
		result = tm + "<br>Pr&auml;zisionsflachstahl / " + p1 + " x " + p2 + " x " + p3;
	}
	else if(type == "vw")
	{
		result = tm + "<br>Vorgeschliffener Werkzeugstahl / " + p1 + " x " + p2 + " x " + p3;
	}
	else if(type == "bl")
	{
		result = tm + "<br>Bleche / " + p2 + " x " + p1 + " x " + p3;
	}
	else if(type == "gpl")
	{
		result = tm + "<br>Geschliffene Platten / " + p2 + " x " + p1 + " x " + p3;
	}
	else if(type == "blc")
	{
		result = tm + "<br>Bleche Ausf&uuml;hrung C1 / " + p2 + " x " + p1 + " x " + p3;
	}
	else if(type == "blg")
	{
		result = tm + "<br>Gefr&auml;ste Bleche / " + p2 + " x " + p1 + " x " + p3;
	}
	else if(type == "ero")
	{
		result = tm + "<br>Erodierbl&ouml;cke / " + p3 + " x " + p2;
	}
	else if(type == "fla")
	{
		result = tm + "<br>Flachstahl / " + p1 + " x " + p2;
	}
	else if(type == "fl")
	{
		if(p3 != '0')
		{
			result = tm + "<br>B&Ouml;HLER-Flex / " + p2 + " x " + p4 + " Faktor: " + p3;
		}
		else
		{
			result = tm + "<br>B&Ouml;HLER-Flex / " + p2 + " x " + p4;
		}
	}
	else if(type == "hol")
	{
		result = tm + "<br>Hohlstahl / " + p1 + " x " + p2;
	}
	else if(type == "runi")
	{
		result = tm + "<br>Rundstahl IBO / " + p1;
	}
	else if(type == "runie")
	{
		result = tm + "<br>Rundstahl IBO ECOMAX / " + p1;
	}
	else if(type == "run")
	{
		result = tm + "<br>Rundstahl / " + p1;
	}
	else if(type == "rune")
	{
		result = tm + "<br>Rundstahl ECOMAX / " + p1;
	}
	else if(type == "blan")
	{
		result = tm + "<br>Blankstahl / " + p1;
	}
	else if(type == "sil")
	{
		result = tm + "<br>Silberstahl / " + p1;
	}
	else if(type == "vie")
	{
		result = tm + "<br>Vierkantstahl / " + p1;
	}
	
	return result;
}
