/************************* Initialize page *************************/
var provinceSelect = document.getElementById('webshopProvincie');
var municipalitySelect = document.getElementById('webshopGemeente');
var selectedProduct = document.getElementById('WebShopCardsSelectedProduct');
var orderButtonSpan = document.getElementById('WebShopShowOrderButton');
var orderButton = document.getElementById('webshopOrder');
var orderTable = document.getElementById('webshopCardsOrderTable');
var recalculate = document.getElementById('webshopRecalculate');
var orderSubmit = document.getElementById('webshopSubmit');
var submitform = document.getElementById('shop-overview');
var totaalprijs = 0;
var administratiekosten = 7.5;

if(provinceSelect)
	addEvent(provinceSelect, 'click', _provinceChange);

if(municipalitySelect)
	addEvent(municipalitySelect, 'change', _municipalityChange);

if(orderButton)
	addEvent(orderButton, 'click', _orderButton);

if(recalculate)
	addEvent(recalculate, 'click', recalculateShop);

if(orderSubmit)
	addEvent(orderSubmit, 'click', _orderSubmit);

window.onbeforeunload = leavePage;

/**************************** Functions ****************************/
function _orderSubmit()
{
	submitform.action = base_href + 'index.php?goto=cart&oldItemID=556&PageId=27';
	var tablerows = orderTable.getElementsByTagName('tr');

	for(var i=1; i<tablerows.length; i++)
	{
		if(tablerows[i].className != 'totalorder')
		{
			submitform.action = submitform.action+'&ItemIDs[]='+tablerows[i].lastChild.firstChild.nodeValue;
		}
	}

	window.onbeforeunload = null;

	submitform.submit();
	return;
}

function removeButton()
{
	this.parentNode.parentNode.removeChild(this.parentNode);

	var tablerows = orderTable.getElementsByTagName('tr');
	var aantal = 0;

	for(var i=0; i<tablerows.length; i++)
	{
		if(tablerows[i].className != 'totalorder')
		{
			aantal++;
		}
	}

	if(aantal == 1)
	{
		totaalprijs = 0;
		document.getElementById('debestelling').className='hide';
	}
	else
	{
		var input = this.parentNode.childNodes[2].firstChild.nodeValue;
		var regexppattern = /([0-9]+\.[0-9]{2})/
		var result = input.match(regexppattern);
		totaalprijs -= this.parentNode.childNodes[1].firstChild.value * result[0];
		printTotalPrice();
	}
}

function inputKeyPress(ev)
{
	var key = getKeyCode(ev);
	var ENTER = 13;

	if(key == ENTER)
	{
		recalculateShop();
	}
}

function recalculateShop()
{
	totaalprijs = 0;
	var tablerows = orderTable.getElementsByTagName('tr');
	var aantal = 0;

	for(var i=1; i<tablerows.length; i++)
	{
		if(tablerows[i].className != 'totalorder')
		{
			aantal = tablerows[i].childNodes[1].firstChild.value;
			if(aantal == 0 || aantal == '')
			{
				aantal = 1;
				tablerows[i].childNodes[1].firstChild.value = 1;
			}
			prijs = 49;
			totaalprijs_item = prijs * parseInt(aantal);
			tablerows[i].childNodes[3].firstChild.nodeValue = '€ '+totaalprijs_item.toFixed(2);
			totaalprijs += totaalprijs_item;
		}
	}

	printTotalPrice();
}

function _provinceChange()
{
	var me=this;

	if(provinceSelect.value != "")
	{	/* Check if it is not changed to the 'chose' value */

		/*request municipalities via ajax
		this.ajaxTarget = 'search_webshop_cards.php';	/* The PHP file that produces the result
		this.ajaxReq = makeRequest();

		if (me.ajaxReq != undefined)
		{
			me.ajaxReq.open("POST", me.ajaxTarget, true);
			me.ajaxReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			me.ajaxReq.onreadystatechange = me.fillSelect;
			me.ajaxReq.send( 'string='+provinceSelect.value );
		}
		*/
		orderButtonSpan.className='show';
		selectedProduct.firstChild.nodeValue = 'Het door u geselecteerde product: Plattegrond van '+provinceSelect.options[provinceSelect.selectedIndex].text +' => Prijs: á € 49,00';
	}
	else
	{	/* Disable the selectbox for municipality */
		/*municipalitySelect.setAttribute('disabled', true);*/
		selectedProduct.firstChild.nodeValue = 'Het door u geselecteerde product: '
		orderButtonSpan.className='hide';
	}

	this.fillSelect = function()
	{
		try
		{	/* This try will catch any error that occurs in the AJAX request */
			if(me.ajaxReq.readyState == 4)
			{
				if(me.ajaxReq.status == 200)
				{	/* Check if the request returned HTTP OK */
					var items = me.ajaxReq.responseXML.getElementsByTagName('item');
					var itCnt = items.length;

					if(itCnt > 0)
					{	/* Check if there is a result */
						var selectBoxLength = municipalitySelect.options.length;
						for(i=selectBoxLength; i >= 0; i--)
						{
							municipalitySelect.options[i]=null;
						}

						municipalitySelect.options[0] = new Option('Kies...', '');
						for(i=0; i<itCnt; i++)
						{
							if(items[i].getAttribute("label") != null)
							{
								municipalitySelect.options[municipalitySelect.options.length] = new Option(items[i].getAttribute("label"), items[i].getAttribute("gemeenteid"));

								municipalitySelect.removeAttribute('disabled',true);
							}
						}
					}
				}
				else
				{	/* The status was not HTTP OK so give warning */
					alert('We konden de lijst van gemeentes voor deze provincie niet ophalen. Neem svp contact met ons op via de "Contact" pagina.');
				}
			}
		}
		catch (e)
		{
			/* only capture and silently ignore specific errors */
			alert('We konden de lijst van gemeentes voor deze provincie niet ophalen. Neem svp contact met ons op via de "Contact" pagina.\n\nInterne melding: ' + e);
		}
	}
}

function _municipalityChange()
{
	if(municipalitySelect.value != "")
	{
		selectedProduct.firstChild.nodeValue = 'Het door u geselecteerde product: Plattegrond van gemeente '+municipalitySelect.options[municipalitySelect.selectedIndex].text+', provincie '+provinceSelect.options[provinceSelect.selectedIndex].text+' => Prijs: á € 49,00';
	}
	else
	{
		selectedProduct.firstChild.nodeValue = 'Het door u geselecteerde product: Plattegrond van '+provinceSelect.options[provinceSelect.selectedIndex].text+' => Prijs: á € 49,00';
	}
}

function _orderButton()
{
	if ( provinceSelect.value )
	{
		document.location.href = base_href + 'index/cart/'+ TreeID +'/'+ provinceSelect.value + '/';
	}
}

this.getKeyCode = function(ev)
{
	if(ev)					/* Moz */
	{
		return ev.keyCode;
	}

	if(window.event)		/* IE */
	{
		return window.event.keyCode;
	}
};

