window.onload = init;

var boekingProces = null;
var eHoliday_sessionId = '';

function init()
{
	boekingProces = new BoekingProces();
}

function BoekingProces()
{
	this.algemeneInformatie = 100;
	this.aankomstdatums = 200;
	this.accommodatieVoorkeur = 300;
	this.kortingsregelingen = 400;
	this.bijkomendeKosten = 500;
	this.contactgegevens = 600;
	this.prijsoverzicht = 700;
	this.bevestiging = 800;
	
	var instantie = this;
	var boekingProcesStapObject = null;
	
	var eHoliday_boekingProcesStap = null;
		
	var eHoliday_boekingTracker = false;
	var eHoliday_boekingMenu = false;
	var eHoliday_boekingBody = false;
	var eHoliday_boekingExtraInformatie = false;
	var eHoliday_boekingPrijsoverzicht = false;
	var eHoliday_boekingAccommodatieDetails = false;
	var eHoliday_boekingAccommodatieDetailsText = '';
	
	var init = function()
	{
		if (document.getElementById('eHoliday_boekingTracker') != null) eHoliday_boekingTracker = document.getElementById('eHoliday_boekingTracker');
		if (document.getElementById('eHoliday_boekingMenu') != null) eHoliday_boekingMenu = document.getElementById('eHoliday_boekingMenu');
		if (document.getElementById('eHoliday_boekingBody') != null) eHoliday_boekingBody = document.getElementById('eHoliday_boekingBody');
		if (document.getElementById('eHoliday_boekingExtraInformatie') != null) eHoliday_boekingExtraInformatie = document.getElementById('eHoliday_boekingExtraInformatie');
		if (document.getElementById('eHoliday_boekingPrijsoverzicht') != null) eHoliday_boekingPrijsoverzicht = document.getElementById('eHoliday_boekingPrijsoverzicht');
		if (document.getElementById('eHoliday_boekingAccommodatieDetails') != null) eHoliday_boekingAccommodatieDetails = document.getElementById('eHoliday_boekingAccommodatieDetails');
		if (document.getElementById('eHoliday_prijzenBody') != null) boekingPrijzen = new BoekingPrijzen();
		if (document.getElementById('eHoliday_lastminuteOverzichtBody') != null) boekingLastminuteOverzicht = new BoekingLastminuteOverzicht();
		if (eHoliday_boekingBody && eHoliday_boekingPrijsoverzicht)
		{
			eHoliday_boekingPrijsoverzicht.id = 'eHoliday_boekingPrijsoverzichtClear';
		}
		if (eHoliday_boekingBody && eHoliday_boekingAccommodatieDetails)
		{
			eHoliday_boekingAccommodatieDetailsText = eHoliday_boekingAccommodatieDetails.innerHTML;
			eHoliday_boekingAccommodatieDetails.innerHTML = '';
			eHoliday_boekingAccommodatieDetails.id = 'eHoliday_boekingAccommodatieDetailsClear';
		}
		if (eHoliday_boekingTracker && eHoliday_boekingBody)
		{
			alert('ERROR: boekingTracker and boekingBody found.');
			return false;
		}
		getOrCreateBoekingProces();
	}
	
	var getOrCreateBoekingProces = function()
	{
		try
		{
			var requestParameters = 'boekingTrackerOnPage=0';
			if (eHoliday_boekingTracker) var requestParameters = 'boekingTrackerOnPage=1';
			new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=get_createBoekingProces', instantie, 'receiveBoekingProces', null, 'POST', requestParameters, false);
		}
		catch(error)
		{
			throw error;
			alert('ERROR: Receiving data');
		}
	}
	
	this.receiveBoekingProces = function(data)
	{
		try
		{
			setCookie("eHoliday_sessionId", data.sessionId);
			if (eHoliday_boekingTracker) setBoekingTracker(data.boekingTrackerHTML);
			if (eHoliday_boekingBody)
			{
				if (getCookie('eHoliday_boekingProcesStap'))
				{
					loadBoekingProcesStap();
				}
				else
				{
					setCookie("eHoliday_boekingProcesStap", this.algemeneInformatie);
					boekingProcesStapObject = new BoekingProcesAlgemeneInformatie(instantie);
				}
			}
		}
		catch(error)
		{
			throw error;
			alert('ERROR: Parsing data');
		}
	}
	
	this.goToBoekingProcesStap = function(boekingProcesStap)
	{
		try
		{
			setCookie("eHoliday_boekingProcesStap", boekingProcesStap);
			boekingProcesStapObject.save();
		}
		catch(error)
		{
			alert('ERROR: Receiving saving data - Receiving');
		}
	}
	
	this.showVoorwaarden = function()
	{
		window.open(eHoliday_conditionsPage);
	}
	
	this.receiveSaveData = function(data)
	{
		try
		{
			if (data === true || data.status == "1")
			{
				loadBoekingProcesStap();
			}
			else
			{
				setCookie("eHoliday_boekingProcesStap", boekingProcesStapObject.huidigeStap);
				alert('ERROR: Receiving saving data - Saving failed');
			}
		}
		catch(error)
		{
			setCookie("eHoliday_boekingProcesStap", boekingProcesStapObject.huidigeStap);
			alert('ERROR: Receiving saving data - Parsing data');
		}
	}
	
	this.deleteBoekingProces = function()
	{
		deleteCookie('eHoliday_sessionId');
		deleteCookie('eHoliday_boekingProcesStap');
	}
	
	var loadBoekingProcesStap = function()
	{
		var boekingProcesStap = getCookie('eHoliday_boekingProcesStap');
		
		if (eHoliday_boekingPrijsoverzicht)
		{
			if (boekingProcesStap == instantie.prijsoverzicht)
			{
				eHoliday_boekingPrijsoverzicht.id = 'eHoliday_boekingPrijsoverzichtClear';
			}
			else
			{
				eHoliday_boekingPrijsoverzicht.id = 'eHoliday_boekingPrijsoverzicht';
			}
		}
		if (eHoliday_boekingAccommodatieDetails)
		{
			if (boekingProcesStap == instantie.accommodatieVoorkeur)
			{
				eHoliday_boekingAccommodatieDetails.innerHTML = eHoliday_boekingAccommodatieDetailsText;
				eHoliday_boekingAccommodatieDetails.id = 'eHoliday_boekingAccommodatieDetails';
			}
			else
			{
				eHoliday_boekingAccommodatieDetails.innerHTML = '';
				eHoliday_boekingAccommodatieDetails.id = 'eHoliday_boekingAccommodatieDetailsClear';
			}
		}
		
		if (boekingProcesStap == instantie.algemeneInformatie)
		{
			boekingProcesStapObject = new BoekingProcesAlgemeneInformatie(instantie);
		}
		else if (boekingProcesStap == instantie.aankomstdatums)
		{
			boekingProcesStapObject = new BoekingProcesAankomstdatums(instantie);
		}
		else if (boekingProcesStap == instantie.accommodatieVoorkeur)
		{
			boekingProcesStapObject = new BoekingProcesAccommodatieVoorkeur(instantie);
		}
		else if (boekingProcesStap == instantie.kortingsregelingen)
		{
			boekingProcesStapObject = new BoekingProcesKortingsregelingen(instantie);
		}
		else if (boekingProcesStap == instantie.bijkomendeKosten)
		{
			boekingProcesStapObject = new BoekingProcesBijkomendeKosten(instantie);
		}
		else if (boekingProcesStap == instantie.contactgegevens)
		{
			boekingProcesStapObject = new BoekingProcesContactgegevens(instantie);
		}
		else if (boekingProcesStap == instantie.prijsoverzicht)
		{
			boekingProcesStapObject = new BoekingProcesPrijsoverzicht(instantie);
		}
		else 
		{
			setCookie("eHoliday_boekingProcesStap", this.algemeneInformatie);
			boekingProcesStapObject = new BoekingProcesAlgemeneInformatie(instantie);
		}
	}	
	
	this.getBoekingProcesStapObject = function(boekingProcesStap)
	{
		if (boekingProcesStapObject != null && boekingProcesStapObject.huidigeStap == boekingProcesStap) return boekingProcesStapObject;
		return false;
	}
	
	var setBoekingTracker = function(html, doStripslashes)
	{
		if (doStripslashes == undefined) doStripslashes = true;
		if (doStripslashes) html = stripslashes(html);
		if (eHoliday_boekingTracker)
		{
			eHoliday_boekingTracker.innerHTML = html;
			
			document.getElementById('aankomstmaand').onchange = instantie.changeBoekingTracker;
			document.getElementById('verblijfsduur').onchange = instantie.changeBoekingTracker;
			document.getElementById('aantalPersonen').onchange = instantie.changeBoekingTracker;
			if (document.getElementById('huisdierGewenst')) document.getElementById('huisdierGewenst').onchange = instantie.changeBoekingTracker;
			if (document.getElementById('rokenGewenst')) document.getElementById('rokenGewenst').onchange = instantie.changeBoekingTracker;
			document.getElementById('aankomstenOpvragen').onclick = function() 
			{
				instantie.changeBoekingTracker(true);
			};
		}
	}
	
	this.setBoekingMenu = function(html, doStripslashes)
	{
		if (doStripslashes == undefined) doStripslashes = true;
		if (doStripslashes) html = stripslashes(html);
		if (eHoliday_boekingMenu) eHoliday_boekingMenu.innerHTML = html;	
	}
	
	this.setBoekingBody = function(html, doStripslashes)
	{
		if (doStripslashes == undefined) doStripslashes = true;
		if (doStripslashes) html = stripslashes(html);
		if (eHoliday_boekingBody) eHoliday_boekingBody.innerHTML = html;		
	}
	
	this.setBoekingExtraInformatie = function(html, doStripslashes)
	{
		if (doStripslashes == undefined) doStripslashes = true;
		if (doStripslashes) html = stripslashes(html);
		if (eHoliday_boekingExtraInformatie) eHoliday_boekingExtraInformatie.innerHTML = html;		
	}
	
	this.setBoekingPrijsoverzicht = function(html, doStripslashes)
	{
		if (doStripslashes == undefined) doStripslashes = true;
		if (doStripslashes) html = stripslashes(html);
		if (eHoliday_boekingPrijsoverzicht) eHoliday_boekingPrijsoverzicht.innerHTML = html;		
	}
	
	this.changeBoekingTracker = function(goToBookingPage)
	{
		if (goToBookingPage !== true) goToBookingPage = false;
		var receiveFunction = 'receiveBoekingTrackerUpdate';
		if (goToBookingPage) receiveFunction = 'receiveBoekingTrackerBookingPage';
		var requestParameters = 'tracker=1&aankomstmaand=' + encodeURI(document.getElementById('aankomstmaand').value) + '&verblijfsduur=' + encodeURI(document.getElementById('verblijfsduur').value) + '&aantalPersonen=' + encodeURI(document.getElementById('aantalPersonen').value) + '&huisdierGewenst=' + encodeURI(document.getElementById('huisdierGewenst').value) + '&rokenGewenst=' + encodeURI(document.getElementById('rokenGewenst').value);
		new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=getBoekingProcesAlgemeneInformatie', instantie, receiveFunction, null, 'POST', requestParameters);
	}
	
	this.receiveBoekingTrackerUpdate = function(data)
	{
		setBoekingTracker(data.boekingTrackerHTML);
	}
	
	this.receiveBoekingTrackerBookingPage = function(data)
	{
		setCookie("eHoliday_boekingProcesStap", boekingProces.aankomstdatums);
		window.location = eHoliday_bookingPage;
	}
	
	this.getSessionId = function()
	{
		return eHoliday_sessionId;
	}
	
	var getCookie = function(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);
		}

		if (name == 'eHoliday_sessionId') return eHoliday_sessionId;
		if (name == 'eHoliday_boekingProcesStap') return eHoliday_boekingProcesStap;
				
		return null;
	}
	
	var setCookie = function(name, value)
	{
		var locatie = window.location.toString();
		if (eHoliday_bookingPage.substr(-1, 1) == '/' && locatie.substr(-1, 1) != '/')
		{
			locatie = locatie + '/';
		}
		else if (eHoliday_bookingPage.substr(-1, 1) != '/' && locatie.substr(-1, 1) == '/')
		{
			locatie = locatie.substr(0, locatie.length - 1);
		}
		if (name == 'eHoliday_boekingProcesStap' && locatie.substr(0 - eHoliday_bookingPage.length, eHoliday_bookingPage.length) == eHoliday_bookingPage)
		{
			deleteCookie(name);
		}
		else
		{
			document.cookie = name + "=" + value + "; path=/";
		}
		
		if (name == 'eHoliday_sessionId') eHoliday_sessionId = value;
		if (name == 'eHoliday_boekingProcesStap') eHoliday_boekingProcesStap = value;
	}
	
	var deleteCookie = function(name)
	{
		var date = new Date();
		date.setTime(date.getTime() + (-1 * 24 * 60 * 60 * 1000));
		document.cookie = name + "=; expires=" + date.toGMTString() + "; path=/";
	}
	
	var stripslashes = function(str)
	{
	    return str.replace('/\0/g', '0').replace('/\(.)/g', '$1');
	}

	init();
}

	/*
	 *	Boekingsproces: algemene informatie
	 *
	 */
function BoekingProcesAlgemeneInformatie(controller)
{
	this.vorigeStap = null;
	this.huidigeStap = boekingProces.algemeneInformatie;
	this.volgendeStap = boekingProces.aankomstdatums;
	
	var controller = controller;
	var instantie = this;
	
	var init = function()
	{
		try
		{
			var requestParameters = 'tracker=0';
			new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=getBoekingProcesAlgemeneInformatie', instantie, 'receiveAlgemeneInformatie', null, 'POST', requestParameters);
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesAlgemeneInformatie');
		}
	}
	
	this.changeAlgemeneInformatie = function()
	{
		var requestParameters = 'tracker=0&aankomstmaand=' + encodeURI(document.getElementById('aankomstmaand').value) + '&verblijfsduur=' + encodeURI(document.getElementById('verblijfsduur').value) + '&aantalPersonen=' + encodeURI(document.getElementById('aantalPersonen').value) + '&huisdierGewenst=' + encodeURI(document.getElementById('huisdierGewenst').value) + '&rokenGewenst=' + encodeURI(document.getElementById('rokenGewenst').value);
		new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=getBoekingProcesAlgemeneInformatie', instantie, 'receiveAlgemeneInformatie', null, 'POST', requestParameters);
	}
	
	this.receiveAlgemeneInformatie = function(data)
	{
		try
		{
			controller.setBoekingMenu(data.boekingMenuHTML);
			controller.setBoekingBody(data.boekingBodyHTML);
			controller.setBoekingExtraInformatie(data.boekingExtraInformatieHTML);
			controller.setBoekingPrijsoverzicht(data.boekingPrijsoverzichtHTML);
			document.getElementById('aankomstmaand').onchange = instantie.changeAlgemeneInformatie;
			document.getElementById('verblijfsduur').onchange = instantie.changeAlgemeneInformatie;
			document.getElementById('aantalPersonen').onchange = instantie.changeAlgemeneInformatie;
			if (document.getElementById('huisdierGewenst')) document.getElementById('huisdierGewenst').onchange = instantie.changeAlgemeneInformatie;
			if (document.getElementById('rokenGewenst')) document.getElementById('rokenGewenst').onchange = instantie.changeAlgemeneInformatie;
			document.getElementById('volgendeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.volgendeStap) 
			};
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesAlgemeneInformatie');
		}
	}
	
	this.save = function(data)
	{
		var requestParameters = 'aankomstmaand=' + encodeURI(document.getElementById('aankomstmaand').value) + '&verblijfsduur=' + encodeURI(document.getElementById('verblijfsduur').value) + '&aantalPersonen=' + encodeURI(document.getElementById('aantalPersonen').value) + '&huisdierGewenst=' + encodeURI(document.getElementById('huisdierGewenst').value) + '&rokenGewenst=' + encodeURI(document.getElementById('rokenGewenst').value);
		new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=saveBoekingProcesData', controller, 'receiveSaveData', null, 'POST', requestParameters);
	}
	
	init();
}

	/*
	 *	Boekingsproces: accommodatietype en aankomstdatum
	 *
	 */
function BoekingProcesAankomstdatums(controller)
{
	this.vorigeStap = boekingProces.algemeneInformatie;
	this.huidigeStap = boekingProces.aankomstdatums;
	this.volgendeStap = boekingProces.accommodatieVoorkeur;
	
	var controller = controller;
	var instantie = this;
	
	var aankomstenTabelContainerAccommodatietypen = null;
	var aankomstenTabelContainerAankomsten = null;
	
	var init = function()
	{
		try
		{
			var requestParameters = '';
			new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=getBoekingProcesAankomstdatums', instantie, 'receiveAankomstdatums', null, 'POST', requestParameters);
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesAankomstdatums');
		}
	}
	
	this.receiveAankomstdatums = function(data)
	{
		try
		{
			controller.setBoekingBody(data.boekingBodyHTML);
			controller.setBoekingMenu(data.boekingMenuHTML);
			controller.setBoekingExtraInformatie(data.boekingExtraInformatieHTML);			
			controller.setBoekingPrijsoverzicht(data.boekingPrijsoverzichtHTML);
			document.getElementById('vorigeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.vorigeStap) 
			};
			document.getElementById('volgendeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.volgendeStap) 
			};
			
			aankomsten = data.aankomsten;
			resetAankomstenTabel();
		
			// Eerste (lege) )cel accommodatietype
			var accommodatietypeRij = document.createElement('tr');
			var accommodatietypeEersteCel = document.createElement('td');
			accommodatietypeEersteCel.innerHTML = '&nbsp;';
			accommodatietypeRij.appendChild(accommodatietypeEersteCel);
			aankomstenTabelContainerAccommodatietypen.appendChild(accommodatietypeRij);
			//
			var aankomstenRij = document.createElement('tr');
			aankomstenTabelContainerAankomsten.appendChild(aankomstenRij);
			
			var i = 0;
			while (i < aankomsten.length)
			{
				var cel = document.createElement('td');
				var dagString = aankomsten[i].aankomstdatum.substr(6, 2) + '-' + aankomsten[i].aankomstdatum.substr(4, 2);
				cel.innerHTML = aankomsten[i].dagVDWeekText + ' ' + dagString;
				cel.className = 'aankomst';
				aankomstenRij.appendChild(cel);
				i++;
			}
									
			var accommodatietypen = getAccommodatietypen(aankomsten);
			var i = 0;
			while (i < accommodatietypen.length)
			{
				// Accommodatie type aan tabel toevoegen
				var accommodatietypeRij = document.createElement('tr');
				var accommodatietypeCel = document.createElement('td');
				accommodatietypeCel.innerHTML = accommodatietypen[i].naam + ':';
				accommodatietypeCel.className = 'label';
				accommodatietypeRij.appendChild(accommodatietypeCel);
				aankomstenTabelContainerAccommodatietypen.appendChild(accommodatietypeRij);
				//
				// Aankomsten aan tabel toevoegen
				var aankomstenRij = document.createElement('tr');
				//				
				var j = 0;
				while (j < aankomsten.length)
				{
					var aankomstenCel = document.createElement('td');
					var gevonden = false;
					var k = 0;
					while (k < aankomsten[j].accommodatietypen.length && !gevonden)
					{
						if (aankomsten[j].accommodatietypen[k].id == accommodatietypen[i].id) gevonden = true;
						k++;
					}
					if (gevonden)
					{
						aankomstenCel.className = 'bezet';
						if (aankomsten[j].accommodatietypen[k - 1].isBeschikbaar == '1')
						{
							var radioButton = '';
							if (data.altijdMetVoorkeur == undefined || data.altijdMetVoorkeur == 0) radioButton = createAankomstdatumAccommodatietypeRadio(aankomsten[j].aankomstdatum, aankomsten[j].accommodatietypen[k - 1].id)
							var prijs = (parseInt(aankomsten[j].accommodatietypen[k - 1].prijs, 10) * 100) - parseInt(aankomsten[j].accommodatietypen[k - 1].korting, 10);
							var prijs = '' + prijs;
							var prijsLength = prijs.length;
							if (prijsLength == 2) prijs = '0' + prijs;
							if (prijsLength == 1) prijs = '00' + prijs;
							var prijsLength = prijs.length;
							prijs = prijs.substr(0, prijsLength - 2) + ',' + prijs.substr(prijsLength - 2, 2);
							if (aankomsten[j].accommodatietypen[k - 1].isLastminute == 0)
							{
								aankomstenCel.innerHTML = '<br />' + radioButton + '<br />&euro;' + prijs + '<br /><span class="aantalNachtenText">' + aankomsten[j].accommodatietypen[k - 1].aantalNachten + ' ' + data.nachtenText + '</span>';
								aankomstenCel.className = 'beschikbaar';
							}
							else
							{
								aankomstenCel.innerHTML = '<span class="lastminuteText">' + data.lastminuteText + '</span>&nbsp;<br />' + radioButton + '<br />&euro;' + prijs + '<br /><span class="aantalNachtenText">' + aankomsten[j].accommodatietypen[k - 1].aantalNachten + ' ' + data.nachtenText + '</span>';
								aankomstenCel.className = 'beschikbaarLastminute';
							}
						}		
					}
					else
					{
						aankomstenCel.innerHTML = '&nbsp;';
						aankomstenCel.className = 'nietMogelijk';
					}
					aankomstenCel.style.width = eHoliday_arrivalCelWidth + 'px';
					aankomstenRij.appendChild(aankomstenCel);
					j++;
				}
				aankomstenTabelContainerAankomsten.appendChild(aankomstenRij);
				i++;
			}
			// Laatste (lege) cel accommodatietype
			var accommodatietypeRij = document.createElement('tr');
			var accommodatietypeLaatsteCel = document.createElement('td');
			accommodatietypeLaatsteCel.innerHTML = '&nbsp;';
			accommodatietypeRij.appendChild(accommodatietypeLaatsteCel);
			aankomstenTabelContainerAccommodatietypen.appendChild(accommodatietypeRij);
			//
			// Scrollbalk instellen
			document.getElementById('tblAankomstenAankomsten').style.width = (aankomsten.length * eHoliday_arrivalCelWidth + 6 * aankomsten.length) + 'px';
			document.getElementById('tblAankomstenScroll').style.width = (eHoliday_arrivalsVisible * eHoliday_arrivalCelWidth + 6 * eHoliday_arrivalsVisible) + 'px';
			//
			// Geselecteerde invullen
			var radioOptions = document.forms.eHoliday_boekingBodyForm.aankomstdatumAccommodatietype;
			var i = 0;
			if (radioOptions && radioOptions.value && radioOptions.value == data.aankomstdatumAccommodatietype)
			{
				radioOptions.checked = true;
			}
			else while (radioOptions && i < radioOptions.length)
			{
				if (radioOptions[i].value == data.aankomstdatumAccommodatietype) radioOptions[i].checked = true;
				i++;
			}
			//
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesAankomstdatums');
		}
	}
	
	var resetAankomstenTabel = function()
	{
		aankomstenTabelContainerAccommodatietypen = document.getElementById('tblAankomstenAccommodatietypen').tBodies[0];
		if (aankomstenTabelContainerAccommodatietypen == undefined) aankomstenTabelContainerAccommodatietypen = document.getElementById('tblAankomstenAccommodatietypen');
		aankomstenTabelContainerAankomsten = document.getElementById('tblAankomstenAankomsten').tBodies[0];
		if (aankomstenTabelContainerAankomsten == undefined) aankomstenTabelContainerAankomsten = document.getElementById('tblAankomstenAankomsten');
	}
	
	var getAccommodatietypen = function(aankomsten)
	{
		var accommodatietypen = new Array();
		
		var i = 0;
		while (i < aankomsten.length)
		{
			var j = 0;
			while (j < aankomsten[i].accommodatietypen.length)
			{
				var k = 0;
				var gevonden = false;
				while (k < accommodatietypen.length && !gevonden)
				{
					if (accommodatietypen[k].id == aankomsten[i].accommodatietypen[j].id) gevonden = true;
					k++;
				}
				if (!gevonden) accommodatietypen[k] = aankomsten[i].accommodatietypen[j];
				j++;
			}
			i++;
		}
		return accommodatietypen;
	}
	
	var createAankomstdatumAccommodatietypeRadio = function(aankomstdatum, accommodatietypeId)
    {
        var name = 'aankomstdatumAccommodatietype';
        var value = aankomstdatum + '_' + accommodatietypeId;
        var style = '';
        if (accommodatietypeId.substr(0, 8) == 'aankomst') style=' style="background-color: #ffffff;"';
		return '<input type="radio" value="' + value + '" id="' + name + '" name="' + name + '"' + style + ' />';            
    }
    
    this.save = function(data)
	{
		var requestParameters = 'aankomstdatumAccommodatietype=';
		var radioOptions = document.forms.eHoliday_boekingBodyForm.aankomstdatumAccommodatietype;
		var i = 0;
		if (radioOptions && radioOptions.value)
		{
			requestParameters += encodeURI(radioOptions.value);
		}
		else while (radioOptions && i < radioOptions.length)
		{
			if (radioOptions[i].checked) requestParameters += encodeURI(radioOptions[i].value);
			i++;
		}
		new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=saveBoekingProcesData', controller, 'receiveSaveData', null, 'POST', requestParameters);
	}
    
	init();
}

	/*
	 *	Boekingsproces: accommodatie voorkeur
	 *
	 */
function BoekingProcesAccommodatieVoorkeur(controller)
{
	this.vorigeStap = boekingProces.aankomstdatums;
	this.huidigeStap = boekingProces.accommodatieVoorkeur;
	this.volgendeStap = boekingProces.kortingsregelingen;
	var alternatieveVolgendeStap = boekingProces.bijkomendeKosten;
	
	var controller = controller;
	var instantie = this;
	
	var accommodaties = new Array();
	
	var init = function()
	{
		try
		{
			new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=getBoekingProcesAccommodatieVoorkeur', instantie, 'receiveAccommodatieVoorkeur', null, 'POST', '');
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesAccommodatieVoorkeur');
		}
	}
	
	this.receiveAccommodatieVoorkeur = function(data)
	{
		try
		{
			if (data.status == 0)
			{
				controller.setBoekingMenu(data.boekingMenuHTML);
				controller.setBoekingBody(data.message);
				if (data.vorigeStap) document.getElementById('vorigeButton').onclick = function() 
				{
					boekingProces.goToBoekingProcesStap(data.vorigeStap); 
				};
				instantie.save = function() { controller.receiveSaveData(true); };
				return;
			}
			if (data.kortingsregelingen == 0) instantie.volgendeStap = alternatieveVolgendeStap;
			controller.setBoekingMenu(data.boekingMenuHTML);
			controller.setBoekingBody(data.boekingBodyHTML);
			controller.setBoekingExtraInformatie(data.boekingExtraInformatieHTML);			
			controller.setBoekingPrijsoverzicht(data.boekingPrijsoverzichtHTML);
			var accommodatieSelect = document.getElementById('accommodatie');
			var accommodatieCount = data.accommodaties.length;
			var i = 0;
			while (i < accommodatieCount)
			{
				accommodaties[data.accommodaties[i].naam] = data.accommodaties[i].id;
				accommodatieSelect.options[i + 1] = new Option(data.accommodaties[i].naam, data.accommodaties[i].id);
				i++;
			}
			accommodatieSelect.value = data.accommodatie;
			try
			{
				accommodatieSelect.onchange = changeParkmap;
				changeParkmap();
			}
			catch (error) { /* do nothing, no parkmap in site */ }
			
			document.getElementById('vorigeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.vorigeStap) 
			};
			document.getElementById('volgendeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.volgendeStap) 
			};
			if (document.getElementById('vorigeButton_')) document.getElementById('vorigeButton_').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.vorigeStap) 
			};
			if (document.getElementById('volgendeButton_')) document.getElementById('volgendeButton_').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.volgendeStap) 
			};
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesAccommodatieVoorkeur');
		}
	}
	
	this.save = function(data)
	{
		var requestParameters = 'accommodatie=' + encodeURI(document.getElementById('accommodatie').value);
		new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=saveBoekingProcesData', controller, 'receiveSaveData', null, 'POST', requestParameters);
	}
	
	this.getAccommodatieId = function(naam)
	{
		return accommodaties[naam];
	}
	
	init();
}

	/*
	 *	Boekingsproces: kortingsregelingen
	 *
	 */
function BoekingProcesKortingsregelingen(controller)
{
	this.vorigeStap = boekingProces.accommodatieVoorkeur;
	this.huidigeStap = boekingProces.kortingsregelingen;
	this.volgendeStap = boekingProces.bijkomendeKosten;
	
	var controller = controller;
	var instantie = this;
	
	var init = function()
	{
		try
		{
			new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=getBoekingProcesKortingsregelingen', instantie, 'receiveKortingsregelingen', null, 'POST', '');
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesKortingsregelingen');
		}
	}
	
	this.receiveKortingsregelingen = function(data)
	{
		try
		{
			if (data.status == 0)
			{
				controller.setBoekingMenu(data.boekingMenuHTML);
				controller.setBoekingBody(data.message);
				if (data.vorigeStap) document.getElementById('vorigeButton').onclick = function() 
				{
					boekingProces.goToBoekingProcesStap(data.vorigeStap); 
				};
				instantie.save = function() { controller.receiveSaveData(true); };
				return;
			}
			controller.setBoekingMenu(data.boekingMenuHTML);
			controller.setBoekingBody(data.boekingBodyHTML);
			controller.setBoekingExtraInformatie(data.boekingExtraInformatieHTML);
			controller.setBoekingPrijsoverzicht(data.boekingPrijsoverzichtHTML);
			document.getElementById('vorigeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.vorigeStap) 
			};
			document.getElementById('volgendeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.volgendeStap) 
			};
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesKortingsregelingen');
		}
	}
	
	this.save = function(data)
	{
		var requestParameters = '';		
		if (document.getElementById('jongsteGeboorteDag') != null)
		{
			var jongsteGeboorteDatum = new Date();
			jongsteGeboorteDatum.setFullYear(document.getElementById('jongsteGeboorteJaar').value, document.getElementById('jongsteGeboorteMaand').value - 1, document.getElementById('jongsteGeboorteDag').value);
			var month = jongsteGeboorteDatum.getMonth() + 1;
			if (month < 10) month = '0' + month;
			var day = jongsteGeboorteDatum.getDate();
			if (day < 10) day = '0' + day;
			requestParameters += 'jongsteGeboorteDatum=' + jongsteGeboorteDatum.getFullYear() + month + day + '&';
		}
		if (document.getElementById('oudsteGeboorteDag') != null)
		{
			var oudsteGeboorteDatum = new Date();
			oudsteGeboorteDatum.setFullYear(document.getElementById('oudsteGeboorteJaar').value, document.getElementById('oudsteGeboorteMaand').value - 1, document.getElementById('oudsteGeboorteDag').value);
			var month = oudsteGeboorteDatum.getMonth() + 1;
			if (month < 10) month = '0' + month;
			var day = oudsteGeboorteDatum.getDate();
			if (day < 10) day = '0' + day;
			requestParameters += 'oudsteGeboorteDatum=' + oudsteGeboorteDatum.getFullYear() + month + day + '&';
		}
		if (document.getElementById('couponCode') != null)
		{
			requestParameters += 'couponCode=' + encodeURI(document.getElementById('couponCode').value) + '&';
		}
		if (requestParameters.length > 0) requestParameters = requestParameters.substring(0, requestParameters.length - 1);
		new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=saveBoekingProcesData', controller, 'receiveSaveData', null, 'POST', requestParameters);
	}
	
	init();
}

	/*
	 *	Boekingsproces: bijkomende kosten
	 *
	 */
function BoekingProcesBijkomendeKosten(controller)
{
	this.vorigeStap = boekingProces.kortingsregelingen;
	this.huidigeStap = boekingProces.bijkomendeKosten;
	this.volgendeStap = boekingProces.contactgegevens;
	var alternatieveVorigeStap = boekingProces.accommodatieVoorkeur;
	
	var controller = controller;
	var instantie = this;
	
	var bijkomendeKostenTabel = null;
	var bijkomendeKostenIds = null;
	
	var init = function()
	{
		try
		{
			new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=getBoekingProcesBijkomendeKosten', instantie, 'receiveBijkomendeKosten', null, 'POST', '');
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesBijkomendeKosten');
		}
	}
	
	this.receiveBijkomendeKosten = function(data)
	{
		try
		{
			if (data.status == 0)
			{
				controller.setBoekingMenu(data.boekingMenuHTML);
				controller.setBoekingBody(data.message);
				if (data.vorigeStap) document.getElementById('vorigeButton').onclick = function() 
				{
					boekingProces.goToBoekingProcesStap(data.vorigeStap); 
				};
				instantie.save = function() { controller.receiveSaveData(true); };
				return;
			}
			if (data.kortingsregelingen == 0) instantie.vorigeStap = alternatieveVorigeStap;
			controller.setBoekingMenu(data.boekingMenuHTML);
			controller.setBoekingBody(data.boekingBodyHTML);
			controller.setBoekingExtraInformatie(data.boekingExtraInformatieHTML);
			controller.setBoekingPrijsoverzicht(data.boekingPrijsoverzichtHTML);
			
			bijkomendeKostenTabel = document.getElementById('tblBijkomendeKosten').tBodies[0];
			if (bijkomendeKostenTabel == undefined) bijkomendeKostenTabel = document.getElementById('tblBijkomendeKosten');
			bijkomendeKostenIds = new Array();
			
			var bijkomendeKosten = data.bijkomendeKosten;
			var i = 0;
			while (i < bijkomendeKosten.length)
			{
				var rij = document.createElement('tr');
	    		var naamCel = document.createElement('td');
				naamCel.innerHTML = bijkomendeKosten[i].naam + ':&nbsp;&nbsp;';
				naamCel.className = 'label';
				rij.appendChild(naamCel);
				var euroCel = document.createElement('td');
				euroCel.innerHTML = '&euro;';
				euroCel.className = 'euro';
				rij.appendChild(euroCel);
				var prijsCel = document.createElement('td');
				prijsCel.innerHTML = '&nbsp;';
				prijsCel.className = 'prijs';
				rij.appendChild(prijsCel);
				var eenheidCel = document.createElement('td');
				eenheidCel.innerHTML = '&nbsp;';
				eenheidCel.className = 'eenheid';
				rij.appendChild(eenheidCel);
	    		bijkomendeKostenTabel.appendChild(rij);
	    		var aantalCel = document.createElement('td');
				aantalCel.innerHTML = '-';
				aantalCel.className = 'aantal';
				rij.appendChild(aantalCel);
				
	    		if (bijkomendeKosten[i].minimumAantal == bijkomendeKosten[i].maximumAantal)
	    		{
	    			var aantalNachten = parseInt(data.aantalNachten, 10);
	    			var maximumAantalNachten = parseInt(data.bijkomendeKosten[i].maximumAantalNachten, 10);
					if (maximumAantalNachten > 0 && aantalNachten > maximumAantalNachten) aantalNachten = maximumAantalNachten;
	    			var aantalPersonen = data.aantalPersonen;
					var prijs = bijkomendeKosten[i].prijs * bijkomendeKosten[i].minimumAantal;
					if (bijkomendeKosten[i].eenheid == 2) prijs = (prijs * aantalPersonen);
					if (bijkomendeKosten[i].eenheid == 3) prijs = (prijs * aantalNachten);
					if (bijkomendeKosten[i].eenheid == 4) prijs = (prijs * aantalPersonen * aantalNachten);
					prijs = '' + prijs;
				}
				else
				{
					aantalCel.innerHTML = createBijkomendeKostenAantalSelect(bijkomendeKosten[i].id, bijkomendeKosten[i].minimumAantal, bijkomendeKosten[i].maximumAantal, bijkomendeKosten[i].aantal);
					var prijs = bijkomendeKosten[i].prijs;
					eenheidCel.innerHTML = '&nbsp;&nbsp;&nbsp;' + bijkomendeKosten[i].eenheidText;
				}
				var prijsLength = prijs.length;
				if (prijsLength == 2) prijs = '0' + prijs;
				if (prijsLength == 1) prijs = '00' + prijs;
				var prijsLength = prijs.length;
				prijsCel.innerHTML = prijs.substr(0, prijsLength - 2) + ',' + prijs.substr(prijsLength - 2, 2);
	    		i++;
			}
			document.getElementById('vorigeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.vorigeStap) 
			};
			document.getElementById('volgendeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.volgendeStap) 
			};
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesBijkomendeKosten');
		}
	}
	
	this.save = function(data)
	{
		var requestParameters = '';
		
		var bijkomendeKostenIdsCount = bijkomendeKostenIds.length;
		var i = 0;
		while (i < bijkomendeKostenIdsCount)
		{
			requestParameters += 'bijkomendeKostenAantal' + bijkomendeKostenIds[i] + '=' + document.getElementById('bijkomendeKostenAantal' + bijkomendeKostenIds[i]).value + '&';
			i++;
		}
		if (requestParameters.length > 0) requestParameters = requestParameters.substring(0, requestParameters.length - 1);
		new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=saveBoekingProcesDataBijkomendeKosten', controller, 'receiveSaveData', null, 'POST', requestParameters);
	}
	
	var createBijkomendeKostenAantalSelect = function(bijkomendeKostenId, minimumAantal, maximumAantal, aantal)
    {
    	var select = '<select id="bijkomendeKostenAantal' + bijkomendeKostenId + '" name="bijkomendeKostenAantal' + bijkomendeKostenId + '" class="formfield50">'
    	while (minimumAantal <= maximumAantal)
    	{
    		var selected = '';
    		if (minimumAantal == aantal) selected = ' selected="selected"';
    		select += '<option value="' + minimumAantal + '"' + selected + '>' + minimumAantal + '</option>';
			minimumAantal++;
		}
		select += '</select>';
		bijkomendeKostenIds[bijkomendeKostenIds.length] = bijkomendeKostenId;
		return select;
	}
		
	init();
}

	/*
	 *	Boekingsproces: contactgegevens
	 *
	 */
function BoekingProcesContactgegevens(controller)
{
	this.vorigeStap = boekingProces.bijkomendeKosten;
	this.huidigeStap = boekingProces.contactgegevens;
	this.volgendeStap = boekingProces.prijsoverzicht;
	
	var controller = controller;
	var instantie = this;
	
	var init = function()
	{
		try
		{
			new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=getBoekingProcesContactgegevens', instantie, 'receiveContactgegevens', null, 'POST', '');
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesContactgegevens');
		}
	}
	
	this.receiveContactgegevens = function(data)
	{
		try
		{
			controller.setBoekingMenu(data.boekingMenuHTML);
			controller.setBoekingBody(data.boekingBodyHTML);
			controller.setBoekingExtraInformatie(data.boekingExtraInformatieHTML);
			controller.setBoekingPrijsoverzicht(data.boekingPrijsoverzichtHTML);
			document.getElementById('vorigeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.vorigeStap) 
			};
			document.getElementById('volgendeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.volgendeStap) 
			};
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesContactgegevens');
		}
	}
	
	this.save = function(data)
	{
		var requestParameters = 'aanhef=' + Base64.encode(document.getElementById('aanhef').value) + '&voorletters=' + Base64.encode(document.getElementById('voorletters').value) + '&achternaam=' + Base64.encode(document.getElementById('achternaam').value) + '&straat=' + Base64.encode(document.getElementById('straat').value) + '&huisnummer=' + Base64.encode(document.getElementById('huisnummer').value) + '&postcode=' + Base64.encode(document.getElementById('postcode').value) + '&plaats=' + Base64.encode(document.getElementById('plaats').value) + '&land=' + Base64.encode(document.getElementById('land').value) + '&telefoon=' + Base64.encode((document.getElementById('telefoon').value)) + '&eMail=' + Base64.encode(document.getElementById('eMail').value);
		new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=saveBoekingProcesDataContactgegevens', instantie, 'receiveSaveData', null, 'POST', requestParameters);
	}
	
	this.receiveSaveData = function(data)
	{
		try
		{
			if (data.isCompleet == 1)
			{
				controller.receiveSaveData(data);
			}
			else
			{
				alert(data.message);
			}
		}
		catch(error)
		{
			controller.receiveSaveData(false);
		}
	}
	
	var Base64 = {
	    // private property
	    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	    // public method for encoding
	    encode : function (input) {
	        var output = "";
	        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	        var i = 0;
	
	        input = Base64._utf8_encode(input);
	
	        while (i < input.length) {
	
	            chr1 = input.charCodeAt(i++);
	            chr2 = input.charCodeAt(i++);
	            chr3 = input.charCodeAt(i++);
	
	            enc1 = chr1 >> 2;
	            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	            enc4 = chr3 & 63;
	
	            if (isNaN(chr2)) {
	                enc3 = enc4 = 64;
	            } else if (isNaN(chr3)) {
	                enc4 = 64;
	            }
	
	            output = output +
	            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
	            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
	
	        }
	
	        return output;
	    },
	    // private method for UTF-8 encoding
	    _utf8_encode : function (string) {
	        string = string.replace(/\r\n/g,"\n");
	        var utftext = "";
	
	        for (var n = 0; n < string.length; n++) {
	
	            var c = string.charCodeAt(n);
	
	            if (c < 128) {
	                utftext += String.fromCharCode(c);
	            }
	            else if((c > 127) && (c < 2048)) {
	                utftext += String.fromCharCode((c >> 6) | 192);
	                utftext += String.fromCharCode((c & 63) | 128);
	            }
	            else {
	                utftext += String.fromCharCode((c >> 12) | 224);
	                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
	                utftext += String.fromCharCode((c & 63) | 128);
	            }
	
	        }
	
	        return utftext;
	    }
	}
		
	init();
}

	/*
	 *	Boekingsproces: prijsoverzicht
	 *
	 */
function BoekingProcesPrijsoverzicht(controller)
{
	this.vorigeStap = boekingProces.contactgegevens;
	this.huidigeStap = boekingProces.prijsoverzicht;
	this.volgendeStap = null;
	
	var controller = controller;
	var instantie = this;
	var akkoordMetVoorwaarden = null;
	
	var init = function()
	{
		try
		{
			new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=getBoekingProcesPrijsoverzicht', instantie, 'receivePrijsoverzicht', null, 'POST', '');
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesPrijsoverzicht');
		}
	}
	
	this.receivePrijsoverzicht = function(data)
	{
		try
		{
			if (data.status == 0)
			{
				controller.setBoekingMenu(data.boekingMenuHTML);
				controller.setBoekingBody(data.message);
				if (data.vorigeStap) document.getElementById('vorigeButton').onclick = function() 
				{
					boekingProces.goToBoekingProcesStap(data.vorigeStap); 
				};
				instantie.save = function() { controller.receiveSaveData(true); };
				return;
			}
			controller.setBoekingMenu(data.boekingMenuHTML);
			controller.setBoekingBody(data.boekingBodyHTML);
			controller.setBoekingExtraInformatie(data.boekingExtraInformatieHTML);
			akkoordMetVoorwaarden = document.getElementById('akkoordMetVoorwaarden');
			akkoordMetVoorwaarden.onclick = instantie.changeAkkoordMetVoorwaarden;
			document.getElementById('vorigeButton').onclick = function() 
			{
				boekingProces.goToBoekingProcesStap(instantie.vorigeStap) 
			};
			document.getElementById('volgendeButton').onclick = function() 
			{
				bevestigBoeking();
			};
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesPrijsoverzicht');
		}
	}
	
	this.changeAkkoordMetVoorwaarden = function()
	{
		document.getElementById('volgendeButton').disabled = true;
		if (akkoordMetVoorwaarden.checked) document.getElementById('volgendeButton').disabled = false;
	}
	
	var bevestigBoeking = function()
	{
		new ServerRequest('eHoliday_HttpRequestPostProxy.php?requestFunctionName=saveBoekingProcesBevestiging', instantie, 'receiveBevestigBoeking', null, 'POST', '');
	}
	
	this.receiveBevestigBoeking = function(data)
	{
		try
		{
			if (data.status == 0)
			{
				controller.setBoekingMenu(data.boekingMenuHTML);
				controller.setBoekingBody(data.message);
				if (data.vorigeStap) document.getElementById('vorigeButton').onclick = function() 
				{
					boekingProces.goToBoekingProcesStap(data.vorigeStap); 
				};
				instantie.save = function() { controller.receiveSaveData(true); };
				return;
			}
			else if (data.boekingSaved == 1)
			{
				controller.setBoekingMenu('');
				controller.setBoekingBody(data.message);
				controller.setBoekingExtraInformatie('');
				controller.deleteBoekingProces();
			}
			else
			{
				alert(data.message);
			}
		}
		catch(error)
		{
			alert('ERROR: BoekingProcesPrijsoverzicht');
		}
	}
	
	this.save = function(data)
	{
		controller.receiveSaveData(true);
	}
	
	init();	
}