function GetString(name) {
	return document.getElementById(name).value;
}


var _activeMenu = null;

function OpenTopLevelMenu(li) {
	CloseActiveTopLevelMenu();	
	with (li) {
		className = "sfhover";
	}
	_activeMenu = li;
}

function CloseActiveTopLevelMenu() {
	if (_activeMenu != null)
		_activeMenu.className = "";
}

document.body.onclick = CloseActiveTopLevelMenu;

function OnMouseOutSubLevel(li) {
	with (li) {
		className = "";
	}
}

function OnMouseOverSubLevel(li) {
	with (li) {
		className = "sfhover";
	}
	//event.cancelBubble = true;
}

function sfHover(id) 
{
	try {
		var sfEls = document.getElementById(id).getElementsByTagName("li");
		for (var i = 0; i < sfEls.length; i++) {
			sfEls[i].onmouseover = function () {
				this._className = this.className;
				this.className += " sfhover";
			}
			sfEls[i].onmouseout = function () {
				this.className = this._className;
			}
		}
	}
	catch (e)
	{
		alert(e);
	}
}

function ShowCatalog(name)
{
	var options = 'scrollbars=no,resizable=yes,status=no,toolbar=no,menubar=no,location=no';
	options += ',width=' + screen.availWidth + ',height=' + screen.availHeight;
	options += ',screenX=0,screenY=0,top=0,left=0';
	var win = window.open(name + '.html', 'Catalogus', options);
	win.focus();
	win.moveTo(0, 0);
	win.resizeTo(screen.availWidth, screen.availHeight);
}

function AgeCheck(age) {
	var regEx,
		s;

	if (typeof(age) == "undefined" || age == "")
		return true;
	// Now replace the age in the string with the real value	
	regEx = /\{\$age\}/g;
	return window.confirm(GetString("agecheck").replace(regEx, age)) == 1;
}

function AddToShoppingCart(catalogID, itemID, hasTelegramText, age, chocoPrint, chocoLetter, marshmallow, chocoTablet)
{
	var cartItemID,
		xmlHttp;

	if (AgeCheck(age) == true) {
		xmlHttp = XmlHttpObject();
		xmlHttp.open("POST", applicationPath + "XmlHttp/AddToShoppingCart.aspx", false);
		xmlHttp.send("<root><catalogid>" + catalogID + "</catalogid><itemid>" + itemID + "</itemid><quantity>0</quantity></root>");
		if (XmlHttpResponse(xmlHttp, document.body, false) == 0) {
			cartItemID = responseRecordID(xmlHttp);
			GotoCheckout(cartItemID, hasTelegramText, chocoPrint, chocoLetter, marshmallow, chocoTablet);
		}
	}
}

function AddToFavorites(itemID)
{
	var xmlHttp;

	xmlHttp = XmlHttpObject();	
	xmlHttp.open("POST", applicationPath + "RS/STORE.Services/AddToFavorites.aspx", false);
	xmlHttp.send("<itemid>" + itemID + "</itemid>");
	if (XmlHttpResponse(xmlHttp, document.body, false) == 0)
	{
		alert(responseMessage(xmlHttp));
		window.location.reload();
	}
}

function RemoveFromFavorites(itemID)
{
	var xmlHttp;

	xmlHttp = XmlHttpObject();	
	xmlHttp.open("POST", applicationPath + "RS/STORE.Services/RemoveFromFavorites.aspx", false);
	xmlHttp.send("<itemid>" + itemID + "</itemid>");
	if (XmlHttpResponse(xmlHttp, document.body, false) == 0)
	{
		alert(responseMessage(xmlHttp));
		window.location.reload()
	}
}

function GotoCheckout(cartItemID, hasTelegramText, chocoPrint, chocoLetter, marshmallow, chocoTablet)
{
	if (hasTelegramText == 1)
		window.location.href = applicationPath + "checkout/telegram.aspx";
	else if (chocoPrint == 1)
		window.location.href = applicationPath + "checkout/chocoprint.aspx";
	else if (chocoLetter == 1)
		window.location.href = applicationPath + "checkout/chocoletter.aspx";
	else if (marshmallow == 1)
		window.location.href = applicationPath + "checkout/spekeditor.aspx";
	else if (chocoTablet == 1)
		window.location.href = applicationPath + "checkout/chocoeditor.aspx";
	else
		GotoStep("deliveryaddress");
}

function Login()
{
	SetRedirect(true);
	Submit(99, "login");
}

function Logoff()
{
	SetRedirect(true);
	AddSubmitParameter("returnurl", "default.aspx");
	Submit(99, "logoff");
}	

function ResendPassword()
{
	SetRedirect(true);
}

function OnKeyPressPWD(e)
{
	var keynum;

	if (window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if (e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	if (keynum == 13) // cr
		Login();

	return true;
}

function ShoppingCart()
{
	window.location.href = applicationPath + "checkout/shoppingcart.aspx";
}

function OnSearchEnter(e, condition)
{
	var keynum;

	if (window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if (e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	if (keynum == 13) // cr
	{
		event.cancelBubble = true;
		Search(condition);
	}
	return false;
}

function Search(condition)
{
	window.location.href = "search.aspx?condition=" + condition;
}

function AddSubmitParameterNoEscape(paramName, paramValue)
{
	var params;
	
	params = document.thisform.elements["SUBMIT_PARAMS"].value;
	params += "<var name=\"" + paramName + "\">" + ToXmlString(paramValue) + "</var>";
	document.thisform.elements["SUBMIT_PARAMS"].value = params;
}

function ExecuteAndRefresh(serverPage, xml)
{
	var xsltDocument,
		httpRequest,
		xmlDocument,
		xmlRequest,
		div;
	
	if ((div = document.getElementById("shoppingcart_placeholder")) != null)
	{
		xmlRequest = new XmlRequest(serverPage, applicationPath + "Checkout/IE5.0/Shoppingcart.xsl");
		//xmlRequest.Debug = true;
		try
		{
			if ((httpRequest = xmlRequest.ExecuteRequest(xml)) != null)
			{
				if (responseErrors(httpRequest) == 0)
				{
					if ((xmlDocument = httpRequest.responseXML) != null)
					{
						if ((xsltDocument = xmlRequest.LoadStyleSheet()) != null)
						{
							xmlRequest.TransformNodeToElement(div, xmlDocument, xsltDocument);
						}
					}
				}
				else
					alert(pulkError(httpRequest));
			}
		}
		catch (exception)
		{
			div.innerHTML = exception.message;
		}
	}
}

function pulkError(request)
{
	return evaluateXPath(request.responseXML, "response/error");
}

function SetCoupon(coupon)
{
	ExecuteAndRefresh(applicationPath + "XmlHttp/SetCoupon.aspx", "<root coupon=\"" + coupon + "\" />");
}

function DeleteItem(id)
{
	ExecuteAndRefresh(applicationPath + "XmlHttp/DeleteItem.aspx", "<root id=\"" + id.toString() + "\" />");
}

function DeleteDeliveryAddress(id)
{
	ExecuteAndRefresh(applicationPath + "XmlHttp/DeleteAddress.aspx", "<root id=\"" + id.toString() + "\" />");
}

function Item(itemID)
{
	window.location.href = applicationPath + "item.aspx?id=" + itemID;
}

function Telegramtext(id)
{
	SetCurrentLine(id, 0);
	window.location.href = applicationPath + "checkout/telegram.aspx";
}

function Chocoletter(id) {
	SetCurrentLine(id, 0);
	window.location.href = applicationPath + "checkout/chocoletter.aspx";
}

function ChocoPrint(id) {
	SetCurrentLine(id, 0);
	window.location.href = applicationPath + "checkout/chocoprint.aspx";
}

function ExecuteNextStep(xml)
{
	var xmlRequest;
	
	xmlRequest = new XmlRequest();
	xmlRequest.ServerPage = applicationPath + "XmlHttp/NextStep.aspx";
	window.location.href = xmlRequest.ExecuteScalar(xml, document.body);
}

function NextStep(current)
{
	ExecuteNextStep("<root current=\"" + current + "\" />");
}

function GotoStep(next)
{
	ExecuteNextStep("<root next=\"" + next + "\" />");
}

function SetCurrentLine(cartItemID, cartItemLineID)
{
	var xmlRequest;
	
	xmlRequest = new XmlRequest();
	xmlRequest.ServerPage = applicationPath + "XmlHttp/SetCurrentLine.aspx";
	xmlRequest.ExecuteNonQuery("<root cartitemid=\"" + cartItemID.toString() + "\" cartitemlineid=\"" + cartItemLineID.toString() + "\" />", document.body);
}

function ToTheShop()
{
	window.location.href = applicationPath + "default.aspx";
}

function OnChangeLanguage(language) {
	var expr,
		url,
		m;

	url = window.location.href;
	expr = /(.)(lang=\w+)/i;
	if ((m = url.match(expr)) == null)
		url += (window.location.search.length == 0 ? "?" : "&") + "lang=" + language;
	else
		url = url.replace(expr, m[1] + "lang=" + language);

	window.location.href = url;
}
