﻿function getBlok(id, start, eind, max)
{
	var td = document.getElementById('tdBlok');
	
	td.innerHTML = '<b>Blok ' + id + '</b><br />';
	td.innerHTML += '<b>' + start + ' - ' + eind + '</b><br />';
	td.innerHTML += 'Capaciteit: ' + max + '<br /><br />';
	
	td.innerHTML += 'Beschikbaar: ' + max;
}

function getReservering(id, start, eind, max, rStart, rEind, aantal, exclusief)
{
	exclusief = exclusief && exclusief != null ? exclusief : false;
	
	var td = document.getElementById('tdBlok');
	
	td.innerHTML = '<b>Blok ' + id + '</b><br />';
	td.innerHTML += '<b>' + start + ' - ' + eind + '</b><br />';
	td.innerHTML += 'Capaciteit: ' + max + '<br /><br />';
	
	td.innerHTML += '<b>Gereserveerd</b><br />';
	td.innerHTML += '<b>' + rStart + ' - ' + rEind + '</b><br /><br />';
	
	var beschikbaar = max - aantal;
	if (exclusief || beschikbaar < 0)
		beschikbaar = 0;
	td.innerHTML += 'Beschikbaar: ' + beschikbaar;
}

/**********************************************************************/

function FloatDiv()
{
	this.div = null;
	this.offsetLeft = 10;
	this.offsetTop  = 10;
	this.margin = 15;
	this.stable = 10;
	this.swapHorizontal = false;	
	this.swapVertical = false;
	this.timer = null;
	
//	this.fixE = function(e)
//	{
//		if (typeof e == 'undefined') e = window.event;
//		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
//		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
//		return e;
//	}
	
	this.positionDiv = function(e)
	{
		if (this.div)
		{
			e = fixE(e); //this.fixE(e);
			var eX = e.clientX + document.documentElement.scrollLeft;
			var eY = e.clientY + document.documentElement.scrollTop;
			
			if (this.swapHorizontal)
			{
				divX = eX - this.offsetLeft - this.div.offsetWidth;
				//EXTRA FOR ALIGNMENT BECAUSE IMG IN DIV IS DYNAMICLY SCALED
				//document.getElementById('alignTD').align = 'right';
			}
			else
			{
				divX = eX + this.offsetLeft;
				//EXTRA FOR ALIGNMENT BECAUSE IMG IN DIV IS DYNAMICLY SCALED
				//document.getElementById('alignTD').align = 'left';
			}
			
			if (this.swapVertical)
			{
				divY = eY - this.offsetTop - this.div.offsetHeight;
				//EXTRA FOR ALIGNMENT BECAUSE IMG IN DIV IS DYNAMICLY SCALED
				//document.getElementById('alignTD').vAlign = 'bottom';
			}
			else
			{
				divY = eY + this.offsetTop;
				//EXTRA FOR ALIGNMENT BECAUSE IMG IN DIV IS DYNAMICLY SCALED
				//document.getElementById('alignTD').vAlign = 'top';
			}
			
			this.div.style.left = divX + 'px';
			this.div.style.top = divY + 'px';
			
			var spaceBottom = document.documentElement.clientHeight - eY - this.div.offsetHeight - this.margin + document.documentElement.scrollTop;
			if (spaceBottom < 0 && !this.swapVertical)
				this.swapVertical = true;
			else if (this.swapVertical && (spaceBottom - this.stable) > 0)
				this.swapVertical = false;
			
			var spaceRight = document.documentElement.clientWidth - eX - this.div.offsetWidth - this.margin + document.documentElement.scrollLeft;
			var spaceLeft = eX - this.div.offsetWidth - this.margin;
			if (spaceRight > this.stable)
				this.swapHorizontal = false;
			else if (spaceLeft > this.stable)
				this.swapHorizontal = true;
			else
				this.swapHorizontal = false;
						
			this.div.style.visibility = 'visible';
			
			if (this.timer)
				clearTimeout(this.timer);
			this.timer = setTimeout("hideDiv();", 10000);
		}
	}
	
	this.showDiv = function(id, e)
	{
		var div = document.getElementById(id);
		document.body.appendChild(div);
		if (div)
		{
			if (this.div)
				this.hideDiv();
			this.div = div;
			document.onmousemove = positionDiv;
			this.positionDiv(e);
		}
	}
	
	this.hideDiv = function ()
	{
		if (this.timer)
			clearTimeout(this.timer);
		this.timer = null;
		document.onmousemove = null;
		if (this.div)
		{
			this.div.style.visibility = 'hidden';
			this.div = null;
		}
	}
}

var floatDiv = new FloatDiv();

function showDiv(id, e)
{
	floatDiv.showDiv(id, e);
}

function hideDiv()
{
	floatDiv.hideDiv();
}

function positionDiv(e)
{
	floatDiv.positionDiv(e);
}

/**********************************************************************/

function ReserveFloatDiv()
{
	this.root = null;
	this.parent = null;
	this.img = null;
	this.imgVisible = false;
	this.boundries = null;
	
	this.div = null;
	this.offsetLeft = 0;
	this.offsetTop  = 2;
	this.margin = 0;
	this.stable = 0;
	this.timer = null;
	
	this.positionDiv = function(e)
	{
		if (this.div)
		{
			e = fixE(e);
			var eY = e.clientY + document.documentElement.scrollTop; //getOffsetTop(this.root, e);
			
			divY = eY + this.offsetTop;
			
			this.div.style.left = (getPosition(this.root)[1] + 2) + 'px'; // (parseInt(this.root.style.left, 10) + 2) + 'px'; //0 + 'px';
			this.div.style.top = divY + 'px';
			
			// CALCULATE IMG VISIBILITY
			// - 3 FOR LITTLE EXTRA MARGIN IN BLOCKS WITH THE SAME HEIGHT AS THE RESERVATION
			var top = getOffsetTop(this.root, e) - this.offsetTop - 3;
			var bottom = top + parseInt(this.div.style.height, 10);
			
			// CHECK BOTTOM BOUNDRY
			this.imgVisible = (bottom > (parseInt(this.root.style.height, 10) + this.offsetTop))
			
			// CHECK RESERVATION BOUNDRIES
			if (!this.imgVisible && this.boundries.length > 0)
			{
				for (var i = 0; i < this.boundries.length; i = i + 2)
				{
					if ((bottom > this.boundries[i] && bottom < this.boundries[i+1]) || (top > this.boundries[i] && top < this.boundries[i+1]) || (top <= this.boundries[i] && bottom >= this.boundries[i+1]))
						this.imgVisible = true;
				}
			}
			
			// ROBIN; OP VERZOEK VAN OPDR-GEVER, HELE BLOK KLIKKEN, NIET OP TIJD
			//if (this.img != null)
			//{
			//	this.img.style.visibility = this.imgVisible ? 'visible' : 'hidden';
			//	this.parent.style.cursor = this.imgVisible ? 'default' : 'pointer';
			//}
			
			//if (this.div != null)		
			//	this.div.style.visibility = 'visible';
		}
	}
	
	this.showDiv = function(id, root, parent, img, hid, e)
	{
		var root = document.getElementById(root);
		var img = document.getElementById(img);
		var hid = document.getElementById(hid);
		var div = document.getElementById(id);
		document.body.appendChild(div);
		if (div)
		{
			if (this.div)
				this.hideDiv();
			this.div = div;
			this.root = root;
			this.parent = parent;
			this.img = img;
			if (hid.value != '')
				this.boundries = hid.value.split(',');
			else
				this.boundries = new Array();
			document.onmousemove = positionRDiv;
			this.positionDiv(e);
		}
	}
	
	this.hideDiv = function ()
	{
		if (this.timer)
			clearTimeout(this.timer);
		this.timer = null;
		document.onmousemove = null;
		if (this.div)
		{
			this.div.style.visibility = 'hidden';
			this.div = null;
			if (this.root)
				this.root = null;
			if (this.parent)
				this.parent = null;
			if (this.img)
			{
				this.img.style.visibility = 'hidden';
				this.img = null;
			}
			if (this.boundries)
				this.boundries = null;
		}
	}
}

var reserveFloatDiv = new ReserveFloatDiv();

function showRDiv(id, root, parent, img, hid, e)
{
	reserveFloatDiv.showDiv(id, root, parent, img, hid, e);
}

function hideRDiv()
{
	reserveFloatDiv.hideDiv();
}

function positionRDiv(e)
{
	reserveFloatDiv.positionDiv(e);
}

function getOffsetTop(div, e)
{
	e = fixE(e);
	var eY = e.clientY + document.documentElement.scrollTop - getPosition(div)[0];
		
	return eY;
}

function getPosition(div)
{
	var obj = div;
	var top, left = 0;
	if (obj.offsetParent);
	{
		top = obj.offsetTop;
		left = obj.offsetLeft;
		while (obj = obj.offsetParent)
		{
			top += obj.offsetTop;
			left += obj.offsetLeft;
		}
	}
	return new Array(top, left);
}

function fixE(e)
{
	if (typeof e == 'undefined') e = window.event;
	if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
	if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
	return e;
}

function getClickTijd(top, start, soort)
{
	return _Default.GetClickTijd(top, start, soort).value;
}

function getOverlap(start, eind)
{
	return _Default.GetOverlap(start, eind).value;
}

/**********************************************************************/

function showReservationDiv(divId, imgId, top, start, aantal, soort, exclusief)
{
	var div = document.getElementById(divId);
	var img = document.getElementById(imgId);
	
	var tijd = getClickTijd(top, start, soort);
	
	// ROBIN; OP VERZOEK VAN OPDR-GEVER, HELE BLOK KLIKKEN, NIET OP TIJD
	//if (div.style.visibility == 'visible' && img.style.visibility == 'hidden')
	//{
		var tijd = getClickTijd(top, start, soort);
		
		var overlap = getOverlap(tijd[0], tijd[3]);
		if (overlap != null)
		{
			showHideDisplay('trOverlap', true);
			hfOverlap.value = overlap.replace(/</g, '#<#');
			lblOverlap.innerHTML = overlap;
		}
	
		var divR = document.getElementById('divReservation');
		
//		var top = document.documentElement.scrollTop + 25;
//		var left = document.documentElement.scrollLeft + 250;
//		divR.style.top = top + 'px';
//		divR.style.left = left + 'px';
		positionReservationDiv(divR);
	    
		divR.style.display = '';
	    
		hfBlokId.value = imgId.substring(imgId.lastIndexOf('_') + 1, imgId.length);
		hfStartTijd.value = tijd[0];
		
		hfDatum.value = tijd[1];
		hfTijd.value = tijd[2] + ' - ' + tijd[3];
	    
		lblDatum.innerHTML = tijd[1];
		lblTijd.innerHTML = tijd[2] + ' - ' + tijd[3];
		
		//if (!exclusief)
		    getPrijs(soort, aantal, '0', '0', '0', '0', exclusief);
	//}
	//else
	//{
	//	alert('Intekenen voor ' + tijd[2] + ' - ' + tijd[3] + ' is helaas niet mogelijk!');
	//}
}

function positionReservationDiv(div)
{
	var top = document.documentElement.scrollTop + 25;
	var left = document.documentElement.scrollLeft + 250;
	div.style.top = top + 'px';
	div.style.left = left + 'px';
}

function hideReservationDiv()
{
	var div = document.getElementById('divReservation');    
    div.style.display = 'none';
    
    showHideDisplay('trOverlap', false);
    lblOverlap.innerHTML = '';
    
    tbAantalBegeleiders.value = '';
    if (tbAantalKoffie != null)
        tbAantalKoffie.value = '';
    if (tbAantalThee != null)
        tbAantalThee.value = '';
    if (tbAantalChocomel != null)
        tbAantalChocomel.value = '';
    cbVoorwaarden.checked = false;

    if (ddlBetaling != null)
        ddlBetaling.selectedIndex = null;
}

function getPrijs(soort, aantal, aantalBegeleiders, aantalKoffie, aantalThee, aantalChocomel, exclusief)
{
    lblPrijs.innerHTML = _Default.GetPrijs(soort, aantal, aantalBegeleiders, aantalKoffie, aantalThee, aantalChocomel, exclusief).value;
}

function setBedrijf(bos)
{
	//if (tbBedrijf.value == '')
		tbBedrijf.value = bos;
}

function checkCBVoorwaarden(source, args)
{
	args.IsValid = cbVoorwaarden.checked;
}

function checkCBContantVoorwaarden(source, args)
{
	args.IsValid = cbContantVoorwaarden.checked;
}

function checkBos(checked)
{
	showHideDisplay('trBos', checked);
}

function checkExclusief(checked)
{
	enableValidation(cvAantal2, !checked);
	enableValidation(cvAantal3, checked)
}
