function SearchState(id)
{
	window.location.href = "zoek-requiem-uitvaartondernemer.aspx?state=" + id.toString();
}

function OnSetFontSize(size)
{
	var expr,
		url,
		m;
	
	url = window.location.href;
	expr = /(.)(fontsize=\w+)/i;
	if ((m = url.match(expr)) == null)
		url += (window.location.search.length == 0 ? "?" : "&") + "fontsize=" + size;
	else
		url = url.replace(expr, m[1] + "fontsize=" + size);

	window.location.href = url;
}		

function go(id, count1, count2, returnUrl, target)
{
	var xmlRequest;
	
	xmlRequest = new XmlRequest();
	xmlRequest.ServerPage = applicationPath + "XmlHttp/CountStats.aspx";
	xmlRequest.Execute(
	"<root>" + 
		"<id>" + id + "</id>" +
		"<count1>" + count1 + "</count1>" +
		"<count2>" + count2 + "</count2>" +
	"</root>");
	if (typeof(target) == "string")
		window.open(returnUrl, "", "", target);
	else
		window.location.href = returnUrl;
}

function PreviewBook(target)
{
	var xmlRequest,
		url;
	
	xmlRequest = new XmlRequest();
	xmlRequest.ServerPage = applicationPath + "XmlHttp/GetPreviewURL.aspx";
	url = xmlRequest.ExecuteScalar("<root />");
	if (typeof(url) == "string")
		window.open(url, "", "", target);
	else
		alert("Van dit Afscheidsboek is geen preview beschikbaar");
}

function ExecuteAndErrors(serverPage, xml)
{
	var httpRequest,
		xmlRequest,
		errors;
	
	xmlRequest = new XmlRequest(serverPage, null);
	//xmlRequest.Debug = true;
	try
	{
		if ((httpRequest = xmlRequest.ExecuteRequest(xml)) != null)
		{
			if ((errors = responseErrors(httpRequest)) != 0)
				alert(evaluateXPath(httpRequest.responseXML, "response/error"));
		}
	}
	catch (exception)
	{
		document.body.innerHTML = exception.message;
	}
	return errors;
}

function AddToShoppingCart(itemID)
{
	var cartItemID,
		xmlHttp;

	xmlHttp = XmlHttpObject();
	xmlHttp.open("POST", applicationPath + "XmlHttp/AddToShoppingCart.aspx", false);
	xmlHttp.send("<root><itemid>" + itemID + "</itemid><quantity>1</quantity></root>");
	if (XmlHttpResponse(xmlHttp, document.body, false) == 0)
		ShoppingCart();
}

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 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 DeleteItem(id)
{
	ExecuteAndRefresh(applicationPath + "XmlHttp/DeleteItem.aspx", "<root id=\"" + id.toString() + "\" />");
}

function CheckFill(id, value)
{
	ExecuteAndRefresh(applicationPath + "XmlHttp/DeleteItem.aspx", "<root action=\"fill\" value=\"" + (value ? "1" : "0") + "\" id=\"" + id.toString() + "\" />");
}

function CheckFingerprintSet(id, value)
{
	ExecuteAndRefresh(applicationPath + "XmlHttp/DeleteItem.aspx", "<root action=\"finger\" value=\"" + (value ? "1" : "0") + "\" id=\"" + id.toString() + "\" />");
}

function CheckRingSizer(id, value)
{
	ExecuteAndRefresh(applicationPath + "XmlHttp/DeleteItem.aspx", "<root action=\"ring\" value=\"" + (value ? "1" : "0") + "\" id=\"" + id.toString() + "\" />");
}

function DeleteWishcard(id)
{
	ExecuteAndRefresh(applicationPath + "XmlHttp/DeleteItem.aspx", "<root action=\"delcard\" id=\"" + id.toString() + "\" />");
}

function DeleteLinten(id)
{
	ExecuteAndRefresh(applicationPath + "XmlHttp/DeleteItem.aspx", "<root action=\"dellint\" id=\"" + id.toString() + "\" />");
}

function SetSize(id, value)
{
	ExecuteAndRefresh(applicationPath + "XmlHttp/DeleteItem.aspx", "<root action=\"size\" value=\"" + value + "\" id=\"" + id.toString() + "\" />");
}

function ShoppingCart()
{
	window.location.href = "winkelmandje";
}

function RenderProductDetails(itemID) {
	var xsltDocument,
		httpRequest,
		xmlDocument,
		xmlRequest,
		xml,
		div;

	if ((div = document.getElementById("productdetails_placeholder")) != null) {
		xmlRequest = new XmlRequest(applicationPath + "Thuiswinkel/ProductDetails.ashx", applicationPath + "Thuiswinkel/ProductDetails.xsl");
		//xmlRequest.Debug = true;
		try {
			xml = "<root id=\"" + itemID.toString() + "\" />";
			if ((httpRequest = xmlRequest.ExecuteRequest(xml)) != null) {
				if (responseErrors(httpRequest) == 0) {
					if ((xmlDocument = httpRequest.responseXML) != null) {
						if ((xsltDocument = xmlRequest.LoadStyleSheet()) != null) {
							xmlRequest.TransformNodeToElement2(div, xmlDocument, xsltDocument);
						}
					}
				}
				else
					alert(pulkError(httpRequest));
			}
		}
		catch (exception) {
			div.innerHTML = exception.message;
		}
	}
}

XmlRequest.prototype.TransformNodeToElement2 =
function(targetDiv, responseXML, xslRef) {
	var output;

	try {
		output = responseXML.transformNode(xslRef);
		if (this.Debug == true)
			alert(output);
		targetDiv.innerHTML = output;
	}
	catch (mozilla) {
		var processor,
		fragment,
		old;

		if (this.Debug == true)
			alert(mozilla.message);

		processor = new XSLTProcessor();
		processor.importStylesheet(xslRef);

		fragment = processor.transformToFragment(responseXML, document);
		// First empty div, otherwise fragment is appended. Removing
		// does not work yet. Is workaround.
		targetDiv.innerHTML = "";
		targetDiv.appendChild(fragment);
		// Firefox won't do disable-output-escaping in transformation. 
		// Do replace for < and > to get functional html in result
		targetDiv.innerHTML = targetDiv.innerHTML.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
	}
} 