var xmlhttpshoppingcart = null;
var shoppingcartele = null
var shoppingcarttodo = null;
var refreshsite = null;

function showinfodiv(id)
{
	if (document.getElementById(id) != null)
	{
		if(navigator.appName.search(/Microsoft Internet Explorer/) != -1)
		{
			if (navigator.appVersion.search(/MSIE [7-9]/) == -1)
			{
				document.getElementById(id).style.position = 'absolute';
				document.getElementById(id).style.top = document.documentElement.scrollTop + 100
			}
		}
		document.getElementById(id).style.display = 'block';
	}
}

function hideinfodiv(id)
{
	if (document.getElementById(id) != null)
	{
		document.getElementById(id).style.display = 'none';
	}
}

function add2cart(artno)
{
	add2cart_ajax(artikel['idartikel'], artno, 0, false);
}

function add2cart(artno, menge)
{
	add2cart_ajax(artikel['idartikel'], artno, menge, false);
}

function add2cart(artid, artno, menge)
{
	add2cart_ajax(artid, artno, menge, false);
}

function delcart(ele, artno, refresh)
{
	refreshsite = refresh;
	shoppingcartele = ele;
	shoppingcarttodo = 'delete';
	add2cart_ajax(-1, artno, 0, 'del');
}

function changecart(ele, artno, refresh)
{
	refreshsite = refresh;
	var menge = ele.value;
	shoppingcartele = ele;
	shoppingcarttodo = 'change';		
	add2cart_ajax(-1, artno, menge, 'change');
}

function add2cart_ajax(artid, artno, menge, del)
{
	try 
	{
		xmlhttp = new XMLHttpRequest();
	}
	catch (error)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (error)
		{
			xmlhttp = false;
		}
	}

	if (xmlhttp)
	{
		xmlhttp.onreadystatechange = add2cart_statusveraendert;
		xmlhttp.open("POST", PATH_PROJECT_HTTP + '/module/shop/frontend/standard/add2cart_ajax.php', true);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		var postparam = 'idartikel=' + artid + '&artikelnummer=' + artno;
		if (menge != null && menge > 0)
		{
			postparam += '&anzahl=' + menge;
		}
		if (del == 'del')
		{
			postparam += '&del=true';
		}
		else if (del == 'change')
		{
			postparam += '&change=true';
		}
		postparam += '&retvalue=all&sprache_idsprache=' + artikel['sprache_idsprache'];
		xmlhttp.send(postparam);
	}
}

function add2cart_statusveraendert()
{
	if (xmlhttp.readyState == 4)
	{
		if (xmlhttp.status == 404)
		{
			alert("Seite nicht gefunden");
		}
		if (xmlhttp.status == 200)
		{
			var shoppingcartdata = xmlhttp.responseText.split('|');
			for (var i = 0; i < shoppingcartdata.length; i++)
			{
				var shoppingcartitem = shoppingcartdata[i].split('=');
				refreshshoppingcartinfo(shoppingcartitem[0], shoppingcartitem[1]);		
				refreshshoppingcart(shoppingcartitem[0], shoppingcartitem[1]);
				if (refreshsite != null && refreshsite) 
				{
					refreshsite = null;
					location.reload(true);
				}
			}
		}
	}
}	

function refreshshoppingcartinfo(elename, elevalue)
{
	var eles = document.getElementsByName(elename);
	for (var n = 0; n < eles.length; n++)
	{
		eles[n].value = elevalue;
	}
}

function refreshshoppingcart(elename, elevalue)
{
	var eles = document.getElementsByName(elename);	
	for (var n = 0; n < eles.length; n++)
	{
		eles[n].value = elevalue;
	}
	if (shoppingcartele != null)
	{
		if (shoppingcarttodo == 'delete')
		{
			shoppingcartele.parentNode.parentNode.parentNode.removeChild(shoppingcartele.parentNode.parentNode);
			shoppingcartele = null;
			shoppingcarttodo = '';
		}
		else if (shoppingcarttodo == 'change')
		{
			var childs = shoppingcartele.parentNode.parentNode.childNodes;
			for (var i = 0; i < childs.length; i++)
			{
				if (childs[i].className == elename)
				{
					childs[i].innerHTML = elevalue;
				}
			}
		}
	}
}