var productCount = 0;
var totalAmount  = 0;

function AddToCart(ProdID, ProdPrise, GoToURL)
{
	var name = "productid";
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf(prefix);
	var productIDs;

	ProdPrise = ProdPrise.replace(",", ".");

	if (begin == -1) 
	{
		productCount	= 1;
		totalAmount		= parseFloat(ProdPrise);
		productIDs		= escape(ProdID);
	}		
	else 
	{
		productIDs		= readCookie("productid");
		productCount	= parseInt(readCookie("pCount"));
		totalAmount		= parseFloat(readCookie("total"));
		
		productCount	+= 1;
		totalAmount		+= parseFloat(ProdPrise);
		productIDs		= productIDs + "," + escape(ProdID);
	}
	createCookie("productid", productIDs, 7);
	createCookie("pCount", productCount, 7);
	createCookie("total", totalAmount, 7);

	var cartTotal_InCart	= parseFloat("0" + readCookie('total_InCart'));
	var cartItems_InCart	= parseInt("0" + readCookie('pCount_InCart'));
	var totalAmountTemp		= totalAmount*100 + cartTotal_InCart;
	var productCountTemp	= productCount + cartItems_InCart;

	totalAmountTemp = totalAmountTemp/100;
	totalAmountTemp = totalAmountTemp.toFixed(2);		

	var a = document.getElementById('AddToCartLink');
	a.href = GoToURL + productIDs;
	
	var cartItems = document.getElementById('CartItems');
	cartItems.innerHTML = productCountTemp;
	
	var cartTotal = document.getElementById('CartTotal');
	cartTotal.innerHTML = totalAmountTemp;
}

function GoToCart(AddToCartURL, CartURL)
{
	productIDs = readCookie("productid");
	ClearCookies();
	if (productIDs == null)
		window.location.href = AddToCartURL + "-1";	
	else
		window.location.href = AddToCartURL + productIDs;
}

function ClearCookies()
{
	eraseCookie("productid");
	eraseCookie("pCount");
	eraseCookie("total");
	
	eraseCookie("total_InCart");
	eraseCookie("pCount_InCart");
}

function InitCartInfo()
{
	var name = "productid";
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf(prefix);
	var cookiecontent;

	var cartItems = document.getElementById('CartItems');
	var cartTotal = document.getElementById('CartTotal');
	
	if (parseInt(cartItems.innerHTML) == 0)
	{
		try
		{
			productCount	= readCookie("pCount");
			totalAmount		= readCookie("total");
			if (productCount == null)
			{
				productCount	= 0;
				totalAmount		= parseFloat(0);
			}
			totalAmount		= parseFloat(totalAmount)
		}
		catch (err)
		{
			productCount	= 0;
			totalAmount		= parseFloat(0);
		}

		var cartTotal_InCart	= parseFloat("0" + readCookie('total_InCart'));
		var cartItems_InCart	= parseInt("0" + readCookie('pCount_InCart'));
		var totalAmountTemp		= totalAmount*100 + cartTotal_InCart;
		var productCountTemp	= parseInt(productCount) + cartItems_InCart;
		
		totalAmountTemp = totalAmountTemp/100;
		totalAmountTemp = totalAmountTemp.toFixed(2);
				
		cartItems.innerHTML = productCountTemp;
		cartTotal.innerHTML = totalAmountTemp;
	}
}

function MM_showHideLayers()
{
	var i,p,v,obj,args=MM_showHideLayers.arguments;	
	for (i=0; i<(args.length-2); i+=3) 
		if ((obj=MM_findObj(args[i]))!=null) 
		{ 
			v=args[i+2];
			if (obj.style) 
			{ 
				obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; 
			}
			obj.visibility=v; 
		}
}

function MM_findObj(n, d) 
{
	var p,i,x;  
	if(!d) 
		d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) 
	{
		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) 
		x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) 
		x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
		x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) 
		x=d.getElementById(n); 
	return x;
}

function getScrollWidth() 
{
	return document.all ? Math.max(Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth), document.body.scrollWidth) : (document.body ? document.body.scrollWidth : ((document.documentElement.scrollWidth != 0) ? document.documentElement.scrollWidth : 0));
}

function getScrollHeight()
{
		return document.all ? Math.max(Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.body.scrollHeight)) : (document.body ? document.body.scrollHeight : ((document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : 0));
}

function f_scrollLeft() 
{
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function f_scrollTop() 
{
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) 
{
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function showOverlayLayer(src)
{
	var dv = document.createElement('div');
	dv.setAttribute('id', 'overlay');
	dv.className="overlay"; 
	dv.style.position="absolute";
	dv.style.zIndex = 9;
	dv.style.left=0 + "px";
	dv.style.top=0 + "px";
	dv.style.width = document.body['clientWidth'] + "px";
	dv.style.height = getScrollHeight() + "px";
	dv.style.backgroundColor="#000000";
	document.body.appendChild(dv);
	
	var dvIF = document.createElement('div');
	dvIF.setAttribute('id', 'dvIF');
	dvIF.style.position="absolute";
	dvIF.style.zIndex = 10;
	dvIF.style.left= 0 + "px";
	dvIF.style.top= (f_scrollTop() + 300) + "px";
	dvIF.style.width= document.body['clientWidth'] + "px";
	dvIF.style.height= "400px";
	dvIF.innerHTML = "<table align='center'><tr><td valign='top'>" + src + "</td></tr></table>";
	document.body.appendChild(dvIF);
	
	var selects = document.getElementsByTagName("SELECT");
	for (var i = 0; i < selects.length; i++)
	{
		selects[i].style.visibility = 'hidden';
	}
}

function hideOverlayLayer()
{
	var dvIF = document.getElementById('dvIF');
	document.body.removeChild(dvIF);
	
	var dv = document.getElementById('overlay');
	document.body.removeChild(dv);
	
	var selects = document.getElementsByTagName("SELECT");
	for (var i = 0; i < selects.length; i++)
	{
		selects[i].style.visibility = 'visible';
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


