
			var activityTableId = "activityPlannerTable";
			var defaultCellHeight = 30;
			var totalTimeSlots = 26; // change -> 48 for 24 hours
			var hiddenRowsQuantity = 4;		 
			var hiddenRowsTop = hiddenRowsQuantity;
			var hiddenRowsBottom = hiddenRowsQuantity;
			var nextCellId = 0;
			var startHour = 7;
			var activityCache = new Array();
			var items;
			var weeks = new Array();
			var currentWeek = 0;
			
			function initialiseItemsArray()
			{
				items = new Array(new Array(null, null, null, null, null, null, null));
			}

			function prevWeek()
			{
				weeks[currentWeek] = items;

				if (currentWeek == 0)
				{
					initialiseItemsArray();
					weeks.unshift(items);
				}
				else
				{
					currentWeek--;
					items = weeks[currentWeek];
				}

				test();
			}

			function nextWeek()
			{
				weeks[currentWeek] = items;
				currentWeek++;

				if (weeks[currentWeek] != undefined)
				{
					items = weeks[currentWeek];
				}
				else
				{
					initialiseItemsArray();
				}

				test();
			}

			function Activity (title, details, type, duration, limit, setday, setslot, imageURL, moreURL)
			{
				this.title = title;
				this.details = details;
				this.type = type;
				this.duration = duration;
				this.limit = limit;
				this.setday = setday;
				this.setslot = setslot;
				this.imageURL = imageURL;
				this.moreURL = moreURL;
				this.providers = new Array();

				this.getDetails = function()
				{
					return { title: this.title, details: this.details, type: this.type, duration: this.duration, limit: this.limit, imageURL: this.imageURL, moreURL: this.moreURL };
				}

				this.addProvider = function(id, name)
				{
					provider = new Object();
					provider.id = id;
					provider.name = name;

					this.providers.push(provider);					
				}

				this.getProviders = function()
				{
					return this.providers;
				}
			}

			function Cell (name, ref) {
			    this.id = nextCellId++;
			    this.dummy = false;
			    this.nul = false;
			    this.cells = new Array();
			    this.parent = null;
				this.name = name;
				this.ref = ref;
				this.provider = -1;
			    this.getShortDescription = function(){
			    	return this.isNull() ? "&nbsp;" : this.name;
			    };
			    //this.setId = function(id){
			    //	this.id = id;
			    //}
				this.setProvider = function(id)
				{
					this.provider = id;
				}
				this.getProvider = function()
				{
					return this.provider;
				}
				this.setLimits = function(max, min)
				{
					this.limitMax = max;
					this.limitMin = min;
				}
				this.getLimits = function()
				{
					return { max: this.limitMax, min: this.limitMin };
				}
				this.setType = function(val)
				{
					this.type = val;
				}
				this.getType = function()
				{
					return this.type;
				}
				this.setIndex = function(x, y){
					this.x = x;
					this.y = y;
				}
				this.getIndex= function(){
					return { x: this.x, y: this.y };
				}
			    this.getId = function(){
			    	return this.id;
			    }
			    this.setElement = function(elem){
			    	this.element = elem;
			    }
			    this.getElement = function(){
			    	return this.element;
			    }
			    this.equals = function(anothercell){
			    	if(anothercell != null &&
			    		//anothercell.id &&
			    		anothercell.id==this.id){
			    		return true;
			    	}
			    	return false;
			    }
			    this.getSpan = function(){
			    	return this.cells.length+1;
			    }
			    this.addCell = function(id){
			    	this.cells[this.cells.length] = id;
			    }
			    this.setParent = function(anothercell){
			    	this.parent = anothercell;
			    }
			    this.getHeight = function(){
			    	if(this.element != null){
			    		return this.element.offsetHeight;
			    	}else if(this.parent != null){
			    		return this.parent.getHeight();
			    	}	
			    }
			    this.getTop = function(){
			    	if(this.element != null){
			    		return findPosY(this.element);
			    	}else if(this.parent != null){
			    		var top = this.parent.getTop()+this.parent.getHeight();
			    		for(var i = 0; i < this.parent.cells; i++){
			    			if(!this.parent.cells[i].equals(this)){
			    				top += defaultCellHeight;
			    			}
			    		}
			    		
			    		return top;
			    	}	
			    }
			    this.isDummy = function(){
			    	return this.dummy;
			    }
			    this.setDummy = function(dummy){
			    	this.dummy = dummy;
			    }
			    this.isNull = function(){
			    	return this.nul;
			    }
			    this.setNull = function(nul){
			    	this.nul = nul;
			    }
			};

			var configWindowElement = null;

			function ConfigWindow()
			{
				this.visible = false;

				this.show = function(id)
				{
					this.visible = true;
					configWindowElement.style.display = "inline";
				}

				this.isVisible = function(){
					return this.visible;
				}
				
				this.hide = function()
				{
					this.visible = false;
					configWindowElement.style.display = "none";
				}
				
				this.setPosition = function(x, y)
				{
					configWindowElement.style.top = y+"px";
					configWindowElement.style.left = x+"px";
				}
				
				this.getDimensions = function()
				{
					return { x: configWindowElement.style.width, y: configWindowElement.style.height };
				}

				this.updateHTML = function(html)
				{
					configWindowElement.innerHTML = html;
				}
				
				if(configWindowElement == null)
				{
					configWindowElement = document.createElement("div");
					configWindowElement.setAttribute("id", "ConfigWindow");
					configWindowElement.style.position = "absolute";
					configWindowElement.style.height = "260px";
					configWindowElement.style.width = "260px";
					configWindowElement.style.background = "#EEE";
					configWindowElement.style.opacity = "0.9";
					configWindowElement.style.border = "1px solid black";
					configWindowElement.style.display = "none";

					document.getElementsByTagName("body")[0].appendChild(configWindowElement);
					this.visible = false;
				}
			}
			
			function HintWindow(container){
				this.container = container;
				
				this.canvas = document.createElement("div");
				this.content = document.createElement("div");
				this.topHandle = document.createElement("div");
				this.bottomHandle = document.createElement("div");
				
				this.topHandleHeight = 6;
				this.bottomHandleHeight = 6;
				
				this.topHandle.style.position="relative";
				this.topHandle.style.height=this.topHandleHeight+"px";
				this.topHandle.style.backgroundColor = "#126532";
				this.topHandle.style.color = "white";
				this.topHandle.style.textAlign = "center";
				this.topHandle.style.fontWeight = "bold";
				this.topHandle.style.cursor = "n-resize";
				//this.topHandle.innerHTML = "--";
				
				this.bottomHandle.style.position="relative";
				this.bottomHandle.style.height=this.bottomHandleHeight+"px";
				this.bottomHandle.style.backgroundColor = "#126532";
				this.bottomHandle.style.color = "white";
				this.bottomHandle.style.textAlign = "center";
				this.bottomHandle.style.fontWeight = "bold";
				this.bottomHandle.style.cursor = "s-resize";
				//this.bottomHandle.innerHTML = "--";
				
				this.canvas.appendChild(this.topHandle);
				this.canvas.appendChild(this.content);
				this.canvas.appendChild(this.bottomHandle);
				
				this.canvas.style.position = "absolute";
				this.canvas.style.top = 0;
				this.canvas.style.left = 0;
				this.canvas.style.backgroundColor="transparent";
				this.canvas.style.color="black";
				this.canvas.style.display="none";
				
				document.getElementsByTagName("body")[0].appendChild(this.canvas);
				
				this.drag = null;
				this.dragTop = 0;
				this.dragLeft = 0;
				this.dragX = 0;
				this.dragY = 0;
				
				this.cellOver = null;
				
				this.host = null;
				
				this.attach = function(obj){
					this.host = obj;
					var x = findPosX(obj);
					var y = findPosY(obj);
					
					//echo(getCellFromElement(obj).getSpan());
					
					this.canvas.style.top=(y-1)+"px";
					this.canvas.style.left=x+"px";
					//this.canvas.innerHTML=x+"x"+y;
					
					this.topHandle.style.top = "0px";
					this.topHandle.style.left = "0px";
					
					this.bottomHandle.style.top = "0px";
					this.bottomHandle.style.left = "0px";
					
					this.content.style.height=(obj.offsetHeight-(this.topHandleHeight-1)-(this.bottomHandleHeight-1))+"px";
					// Maybe for IE
					//this.canvas.style.height = obj.offsetHeight;
					this.canvas.style.width = (obj.offsetWidth+1)+"px";
					
					this.canvas.style.display="inline";
				}
				
				this.detach = function(){
					this.host = null;
					this.drag = null;
					this.canvas.style.display="none";
				}
				
				this.up = function(event){
					//document.getElementById("out").innerHTML = "up";
					if(this.cellOver != null){
						//var posy = getMouseY(event);
						//var posx = findPosX(this.drag)+1;
						var src = getCellFromElement(this.host);
						
						if(src != null){
							this.mergeCells(src, event);
						}
					}
					
					this.cellOver = null;
					this.drag = null;
					this.detach();
					
					return false;
				}
				
				this.down = function(event){
					//document.getElementById("out").innerHTML = "down";
					this.drag = getEventTarget(event);
					
					this.dragLeft = this.drag.style.left;
					this.dragTop = this.drag.style.top;
					
					this.dragLeft=this.dragLeft.substring(0, this.dragLeft.length-2);
					this.dragTop=this.dragTop.substring(0, this.dragTop.length-2);
					
					this.dragX = getMouseX(event);
					this.dragY = getMouseY(event);
					
					return false;
				}
				
				this.move = function(event){
					if(this.drag == null)
						return false;
					
					//document.getElementById("out").innerHTML = "drag ["+this.container.offsetHeight+"]";
					
					var posy = getMouseY(event);
					var posx = getMouseX(event);
					
					if(this.drag != null){
						if(posy < this.container.offsetHeight+findPosY(this.container) && posy > 0){
							if(this.drag == this.topHandle){
								if(posy > findPosY(this.bottomHandle))
									return false;
							}
							
							this.drag.style.top = ((+posy)-(this.dragY-this.dragTop))+"px";
							this.cellOver = (findPosX(this.drag)+1, posy);
						}
						
						//this.drag.style.left = ((+posx)-(this.dragX-this.dragLeft))+"px";
					}
					
					return false;
				}
				
				
				this.mergeCells = function(src, evt){
					//this.host, this.cellOver, this.topHandle, this.bottomHandle
					
					//var position = this.host.getIndex();//getCellLocationFromElementName(this.host.getAttribute("name"));

					var position = getCellLocationFromElement(this.host);
					var top = findPosY(this.topHandle);
					var bottom = findPosY(this.bottomHandle);
					var limits = src.getLimits();

					// Redefine top or bottom to limit the size of the element. duration =< span =< limit
					if (roundNearest(top, 30) == roundNearest(getMouseY(evt), 30))
					{
						if (top < (bottom - (limits.max * defaultCellHeight)))
						{
							top = bottom - (limits.max * defaultCellHeight);
						}
						else if (top > (bottom - (limits.min * defaultCellHeight)))
						{
							top = bottom - ((limits.min * defaultCellHeight));
						}
					}
					else
					{
						if (bottom > (top + (limits.max * defaultCellHeight)))
						{
							bottom = top + (limits.max * defaultCellHeight);
						}
						else if (bottom < (top + (limits.min * defaultCellHeight)))
						{
							bottom = top + ((limits.min * defaultCellHeight));
						}
					}	

					var found = "";
					var removeList = new Array();
					
					var location = src.getIndex();//getCellLocationFromElementName(src.getElement().getAttribute("name"));
				
					if(src.getSpan() > 1){
						// If we've more than just one colspan...
	
						if(!(src.getTop() >= top && src.getTop()+src.getHeight() < bottom)){
							// .. And we don't fit within the next region
							
							if(src.getTop() >= top && src.getTop() < bottom){
								// ... But we start in the right place...
								var span = (bottom+this.topHandleHeight)-src.getTop();

								var keepCount = Math.round(span/defaultCellHeight);
								var keepStart = Math.round((src.getTop()-top)/50);
								
								for(var i = keepCount; i < src.cells.length+1; i++){
									var x = (+location.x)+(+i);
									
									items[x][location.y] = null;
								}
								
							}
							else if(src.getTop()+src.getHeight() >= top && src.getTop()+src.getHeight() < bottom+this.bottomHandleHeight)
							{
								var span = (src.getTop()+src.getHeight())-top;

								var keepCount = Math.round(span/defaultCellHeight);
								var keepStart = (src.cells.length+1)-keepCount;
								//var keepStart = src.cells.length-Math.round(((src.getTop()+src.getHeight())-top)/defaultCellHeight);
								
								// move this cell from it's current position
								// to keepStart x KeepCount (ish)
								
								//items[keepStart][location.y] = src;
								
								for(var i = 0; i < keepStart; i++){
									var x = (+location.x)+(+i);
									items[x][location.y] = null;
								}
							}
						}
					}

					var prompted = false;

					for(var i = 0; i < items.length; i++)
					{
						var nextRow = items[i];
						var next = nextRow[position.y];
						if(next == null)
							continue;
						
						var y = next.getTop();//findPosY(next.getElement());
						
						if(y >= top && y < bottom)
						{
							if (getCellFromCoords(getMouseX(evt), (bottom + defaultCellHeight)) == null)
							{// end of day
								items[i][position.y] = src;
							}
							else if (next.ref == -1 || (getCellFromCoords(getMouseX(evt), bottom).ref != getCellFromCoords(getMouseX(evt), (bottom + defaultCellHeight)).ref) || getCellFromCoords(getMouseX(evt), bottom).ref == -1)
							{
								items[i][position.y] = src;
							}
						}
					}

					test();
				}
				
				this.fallsWithin2 = function(x1, y1, height, x2, y2){
					
					if(y1 < y2 && y1+(y1-height) > y2){
					 	alert("yep. " + y1 + " and " + y2 + " and " + height);
						return true;
					}
					
					alert("nope. " + y1 + " and " + y2 + " and " + height);					
					return false;
				}
				
				this.fallsWithin = function(obj, x, y){
					//var xx = findPosX(obj);
					var yy = findPosY(obj);
					var height = obj.offsetHeight;
					
					if(yy-findPosY(this.container) < y && yy+(yy-height) > y){
					 	alert("yep. " + yy + " and " + y + " and " + height);
						return true;
					}
					
					alert("nope. " + yy + " and " + y + " and " + height);					
					return false;
				}
				
			}
			
			var items = new Array(new Array(null, null, null, null, null, null, null));
				/*new Array(new Cell("1,1", 1), new Cell("1,2", 1), new Cell("1,3", 1), new Cell("1,4", 1), new Cell("1,5", 1), new Cell("1,6", 1), new Cell("1,7", 1)),
				new Array(new Cell("2,1", 1), new Cell("2,2", 1), celltest, new Cell("2,4", 1), new Cell("2,5", 1), new Cell("2,6", 1), new Cell("2,7", 1)),
				new Array(new Cell("3,1", 1), new Cell("3,2", 1), celltest, new Cell("3,4", 1), new Cell("3,5", 1), new Cell("3,6", 1), new Cell("3,7", 1)),
				new Array(new Cell("4,1", 1), new Cell("4,2", 1), celltest, new Cell("4,4", 1), new Cell("4,5", 1), new Cell("4,6", 1), new Cell("4,7", 1))
			);*/
			
			function getCellFromCoords(x, y){
				for(var i = 0; i < items.length; i++){
					var nextRow = items[i];
					
					for(var j = 0; j < nextRow.length; j++){
						var nextItem = nextRow[j];
						
						var xx = findPosX(nextItem.getElement());
						var yy = findPosY(nextItem.getElement());
						
						//document.getElementById("out").innerHTML = "Not: " + nextItem.getShortDescription() + " xx = " + xx + " and x = " + x + " by " + ((+xx)+(+nextItem.getElement().offsetWidth)) + " and " + x;
						//document.getElementById("out").innerHTML = "Not: " + nextItem.getShortDescription() + " yy = " + yy + " and y = " + y + " by " + ((+yy)+(+nextItem.getElement().offsetWidth)) + " and " + y;
						
						if(x >= xx && x <= (+xx)+(nextItem.getElement().offsetWidth)){
							if(y >= yy && y <= (+yy)+nextItem.getElement().offsetHeight){
								//document.getElementById("out").innerHTML = ("Matched: " + nextItem.getShortDescription() + " xx = " + xx + " and x = " + x + " by " + ((+xx)+(+nextItem.getElement().offsetWidth)) + " and " + x);
								return nextItem;
							}
						}
					}
				}
					
				return null;
			}
			
			function echo(str){
				document.getElementById("out").innerHTML = str; 
			}

			function test()
			{
				clearToggle("toggleTop");
				clearToggle("toggleBottom");

				var table = document.getElementById(activityTableId);
				var tbody = table.getElementsByTagName("tbody")[0];
				var altParent = new Array();
				
				if(tbody == null)
					return;
				
				elem=tbody;
				
				if(elem.hasChildNodes()) {
					while(elem.childNodes.length >= 1 ) {
						elem.removeChild(elem.firstChild);
					}
				}
				
				var uniqueItems = new Array();
				

				var maxCol = 0;
				var maxRow = 0;
				
				for(i = 0; i < totalTimeSlots; i++){
					display = false;
					hideable = false;

					if (i < hiddenRowsQuantity || i >= (totalTimeSlots - hiddenRowsQuantity))
					{
						hideable = true;
					}

					if (i >= hiddenRowsTop && i < (totalTimeSlots - hiddenRowsBottom))
					{
						display = true;
					}

					var newRow = document.createElement("tr");
					if (display)
					{
						elem.appendChild(newRow);
					}

					var timeLabel = document.createElement("td");

					timeLabel.innerHTML = generateTime(i);
					timeLabel.className = "timeCell";

					if (hideable == true)
					{
						timeLabel.className += " t600";
					}

					newRow.appendChild(timeLabel);
					

					
					if(i == items.length){
						items[i] = new Array();
						for(j = 0; j < items[0].length; j++){
							items[i][j] = null;
						}
					}
					
					for(j = 0; j < items[i].length; j++){
						var nextCell = items[i][j];
						
						var newItem = document.createElement("td");
						var colourType = "";
						var dragType = "";
						
						if (nextCell != null && nextCell.type != undefined)
						{
							colourType = " t" + nextCell.type;
						}
						else if (hideable == true)
						{
							colourType = " t601";
						}
						
						if (nextCell != null && nextCell.ref != -1)
						{
							dragType = "activityDraggable";
						}

						newItem.setAttribute("class", dragType + colourType);
						
						if(nextCell == null || nextCell.isDummy()){
							// no activity or something...
							nextCell = new Cell(i+"x"+j, -1);
							nextCell.setNull(true);
							items[i][j] = nextCell;
						}
						
						var duplicate = false;
						var visibleChild = false;
						
						for(var index = 0; index < uniqueItems.length; index++){
							if(uniqueItems[index].equals(nextCell)){
								duplicate = true;
								break;
							}
						}

						if (duplicate)
						{
							parentIndex = nextCell.parent.getIndex();
							nextIndex = nextCell.getIndex();

							if (parentIndex.x < hiddenRowsTop && i == hiddenRowsTop)
							{
								visibleChild = true;
							}
						}

						
						if(!duplicate)
						{
							nextCell.setElement(newItem);
							nextCell.setIndex(i, j);
							nextCell.cells = new Array();
							uniqueItems[uniqueItems.length] = nextCell;
						}
						else if (visibleChild == true)
						{
							altParent[j] = new Cell(i+"x"+j, nextCell.ref);
							altParent[j].setElement(newItem);
							altParent[j].name = nextCell.name;
							altParent[j].setIndex(i, j);
							altParent[j].setParent(nextCell);

							newItem.setAttribute("class", colourType);
						}
						else
						{
							var dummy = new Cell(i+"x"+j, nextCell.ref);
							dummy.setIndex(i, j);
							dummy.setDummy(true);
							dummy.setParent(nextCell);

							if (parentIndex.x < hiddenRowsTop && i >= hiddenRowsTop)
							{
								altParent[j].addCell(dummy);
								altParent[j].getElement().setAttribute("rowspan", altParent[j].getSpan());
							}
							else
							{
								if (parentIndex.x < (totalTimeSlots - hiddenRowsBottom) && i >= (totalTimeSlots - hiddenRowsBottom))
								{
									nextCell.getElement().setAttribute("class", colourType);
								}

								nextCell.addCell(dummy);
								nextCell.getElement().setAttribute("rowspan", nextCell.getSpan());
							}
							continue;
						}
						
						//newItem.setAttribute("name", i+"x"+j);
						//document.createTextNode(nextCell.getShortDescription());
						var content = document.createElement("div");
						content.innerHTML = nextCell.getShortDescription();
						
						newItem.style.textAlign="center";
							
						newItem.appendChild(content);
							
						//for(i = hiddenRowsTop; i < (totalTimeSlots - hiddenRowsBottom); i++)
						if (display)
						{
							newRow.appendChild(newItem);
						}
						
						if(j > maxCol)
							maxCol = j;
					}
					
					if(i > maxRow)
						maxRow = i;
				}

				colourToggle("toggleTop");
				colourToggle("toggleBottom");
				
				//document.getElementsByTagName("body")[0].appendChild(hintWindow);
			}

			function setIntroLayer(table)
			{
				introLayer.style.left = findPosX(table) + 70 + 90 - (defaultCellHeight / 2) + "px";
				introLayer.style.top = findPosY(table) + (3.5 * defaultCellHeight) + "px";
				introLayer.style.width = (table.offsetWidth - 8 - 70 - 180 + defaultCellHeight) + "px";
				introLayer.style.height = (table.offsetHeight - (6 * defaultCellHeight)) + "px";
			}
			
			function init(){
				initGlobalFunctions();
				initialiseItemsArray();
				test();
				findNearestHotel();

				var table = document.getElementById(activityTableId);
				hintWindow = new HintWindow(table);
				initDraggable();

				configWindow = new ConfigWindow();

				introLayer = document.getElementById("introLayer");
				introLayer.style.display = "inline";

				setIntroLayer(table);

				loadXMLDoc("dynamic/activityData.php");
			}
			
			var dragSrc = null;
			var dragTop = 0;
			var dragLeft = 0;
			var dragX = 0;
			var dragY = 0;
			
			function getCellLocationFromElement(elem){
				
				for(i = 0; i < items.length; i++){
					for(j = 0; j < items[i].length; j++){
						if(items[i][j].getElement() != null){
							if(items[i][j].getElement() == elem){
								return { x: i, y: j };
							}
						}
					}
				}

				return null;
			}

			function getCellFromElement(elem){
				
				if(elem == null)
					return null;
				
				if(!elem.getAttribute)
					return null;
				
				var cell = null;
				
				var location = getCellLocationFromElement(elem);
				
				if(location != null)
					cell = items[location.x][location.y];
				
				return cell;
			}
			
			  function findPosY(obj){
			    var curtop = 0;
			    if(obj.offsetParent)
			        while(1)
			        {
			          curtop += obj.offsetTop;
			          if(!obj.offsetParent)
			            break;
			          obj = obj.offsetParent;
			        }
			    else if(obj.y)
			        curtop += obj.y;
			    return curtop;
			  }
			  
			  function findPosX(obj){
			    var curleft = 0;
			    if(obj.offsetParent)
			        while(1) 
			        {
			          curleft += obj.offsetLeft;
			          if(!obj.offsetParent)
			            break;
			          obj = obj.offsetParent;
			        }
			    else if(obj.x)
			        curleft += obj.x;
			    return curleft;
			  }
			  
			function findAttributeBackwardsRecursive(attribute, value, child){

				if(child.getAttribute && child.getAttribute(attribute) != null){
					var cl = child.getAttribute(attribute);

					if(cl.indexOf(value)!=-1){
						return child;
					}
				}
				
				if(child.parentNode != null){
					return findAttributeBackwardsRecursive(attribute, value, child.parentNode);
				}
				
			}
			
			var potentialDragEvent = null;

			function leavePageWarning()
			{
				var bLeave = 'Navigating away from this page will loose all unsaved activity planner data.';
				return bLeave;
			}
			
			var hintWindow = null;
			
			function initGlobalFunctions(){
				
				window.onresize = function (event)
				{
					if (introLayer.style.display == "inline")
					{
						setIntroLayer(document.getElementById("activityPlannerTable"));
					}
				}
				
				document.onclick = function (event)
				{
					var event = event ? event : window.event;

					var currentTarget = getEventTarget(event);
					var elem = findAttributeBackwardsRecursive("class", "activityDraggable", currentTarget);

					if (elem == null)
					{
						return;
					}

					cell = getCellFromElement(elem);

					if (cell != null)
					{
							if(!cell.isNull())
							{
								draggableWindow.setAttribute("name", cell.ref);
								configureActivity(event);
							}
					}
					else
					{
						displayActivityDetails(event, elem);
					}
				}
				
				document.onmousedown=function (event)
				{
					var event = event ? event : window.event;

					var currentTarget = getEventTarget(event);

					if(currentTarget == hintWindow.topHandle ||
						currentTarget == hintWindow.bottomHandle){
						return hintWindow.down(event);
					}
					
					potentialDragEvent = event;

					if (currentTarget.tagName != "SELECT" && currentTarget.tagName != "INPUT")
					{// for FireFox
						return false;
					}
				}
				
				document.onmouseup=function (event)
				{
					var event = event ? event : window.event;

					var eventTarget = getEventTarget(event);
					
					if(eventTarget == hintWindow.topHandle ||
						eventTarget == hintWindow.bottomHandle ||
						hintWindow.drag != null){
						return hintWindow.up(event);
					}else if(dragSrc != null){
						if(dragSrc == draggableWindow){
							handleDrop(event, potentialDragEvent);
						}
					}
					
					potentialDragEvent = null;
					dragSrc = null;
					return false;
				}
				
				document.onmousemove=function(event)
				{
					var event = event ? event : window.event;
					var cellSpan = 1;
					var posy = getMouseY(event);
					var posx = getMouseX(event);
						
					if(potentialDragEvent != null && dragSrc==null)
					{
						var potDragEventTarget = getEventTarget(potentialDragEvent);
						
						var elem = findAttributeBackwardsRecursive("class", "activityDraggable", potDragEventTarget);
						
						if(elem != null){
							var cell = getCellFromElement(elem);
							if(cell == null || !cell.isNull())
							{
								if (cell != null)
								{
									cellSpan = (cell.duration);
								}

								dragSrc = createDraggable(elem, getMouseX(potentialDragEvent), getMouseY(potentialDragEvent), 90, (30 * cellSpan));
							}
						}
						
						if(dragSrc == null)
							return false;
						
						dragLeft = dragSrc.style.left;
						dragTop = dragSrc.style.top;
						
						dragLeft=dragLeft.substring(0, dragLeft.length-2);
						dragTop=dragTop.substring(0, dragTop.length-2);
						
						dragX = getMouseX(event);
						dragY = getMouseY(event);
					}

					var eventTarget = getEventTarget(event);
					
					if(eventTarget == hintWindow.topHandle ||
						eventTarget == hintWindow.bottomHandle ||
						hintWindow.drag != null){
						return hintWindow.move(event);
					}else if(dragSrc != null){
						dragSrc.style.top = ((+posy)-(dragY-dragTop))+"px";
						dragSrc.style.left = ((+posx)-(dragX-dragLeft))+"px";
						
						//document.getElementById("mousepos").innerHTML=posx+"x"+posy + " - " + (posx+"x"+dragY);
					}else if(configWindow.isVisible()){
						var winX = findPosX(configWindowElement);
						var winY = findPosY(configWindowElement);
						
						if(posx > winX+configWindowElement.offsetWidth
							|| posx < winX ||
							posy > winY+configWindowElement.offsetHeight ||
							posy < winY){
							configWindow.hide();
						}
					}else{
						var check = eventTarget;
						var newCell = getCellFromElement(check);
						var host = hintWindow.host;
						var cell = null;
						var mouseY = getMouseY(event);

						if(host != null){
							cell = getCellFromElement(host);
						} 
						
						if(cell != null){
							if((mouseY-cell.getTop()) > 5 ||
								((cell.getTop()+cell.getHeight()) - mouseY) > 5){
								hintWindow.detach();
								cell = null;
							}
						}
						
						if(newCell != null && cell==null)
						{
							if((mouseY-newCell.getTop()) <= 5 ||
							((newCell.getTop()+newCell.getHeight()) - mouseY) <= 5){
								if(!newCell.isNull())
								{
									var limits = newCell.getLimits();

									//alert(limits.max + "x" + limits.min);

									if (limits.max != limits.min)
									{
										if ((parseInt(newCell.getElement().getAttribute("rowspan")) + newCell.x) <= (totalTimeSlots - hiddenRowsBottom))
										{
											hintWindow.attach(newCell.getElement());
										}
									}								
								}
								
							}
						}else
							hintWindow.detach();
						
					}
					
					return false;
				}

			} // end initGlobalFunctions()

			function getOffset( el ) {
				var _x = 0;
				var _y = 0;
				while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
					_x += el.offsetLeft - el.scrollLeft;
					_y += el.offsetTop - el.scrollTop;
					el = el.parentNode;
				}
				return { top: _y, left: _x };
			}

			function handleDrop(event, dragSrcEvent)
			{
				hideDraggable();
				var drgSrcTarget = getEventTarget(dragSrcEvent);

				if (findAttributeBackwardsRecursive("id", "apactivitybar", drgSrcTarget) && getCellFromCoords(getMouseX(event),getMouseY(event)) != null)
				{
					//document.getElementById("out2").innerHTML = "Dropped from sidebar";
					if  (copyItemToCell(event, dragSrcEvent) == "success")
					{
						configureActivity(event);
						findNearestHotel();
					}
				}
				else if (findAttributeBackwardsRecursive("id", "aptimetable", drgSrcTarget))
				{
					if (getCellFromCoords(getMouseX(event),getMouseY(event)) != null)
					{
						//document.getElementById("out2").innerHTML = "Dropped from timetable";

						if  (copyItemToCell(event, dragSrcEvent) == "success" && getCellFromCoords(getMouseX(event),getMouseY(event)).id != getCellFromCoords(getMouseX(dragSrcEvent),getMouseY(dragSrcEvent)).id)
						{
							removeActivity(dragSrcEvent);						
						}
					}
					else
					{
						//document.getElementById("out2").innerHTML = "Activity thrown away";
						removeActivity(dragSrcEvent);
						findNearestHotel();
					}
				}
			}			

			function getMouseY(e){
				var posx = 0;
				var posy = 0;
				if (!e) var e = window.event;
				if (e.pageX || e.pageY) 	{
					posx = e.pageX;
					posy = e.pageY;
				}
				//else if (e.clientX || e.clientY) 	{
				//	posx = e.clientX + document.body.scrollLeft
				//		+ document.documentElement.scrollLeft;
				//	posy = e.clientY + document.body.scrollTop
				//		+ document.documentElement.scrollTop;
				//}
				
				return posy;
			}			
			
			function getMouseX(e){
				var posx = 0;
				var posy = 0;
				if (!e) var e = window.event;
				if (e.pageX || e.pageY) 	{
					posx = e.pageX;
					posy = e.pageY;
				}
				//else if (e.clientX || e.clientY) 	{
				//	posx = e.clientX + document.body.scrollLeft
				//		+ document.documentElement.scrollLeft;
				//	posy = e.clientY + document.body.scrollTop
				//		+ document.documentElement.scrollTop;
				//}
				
				return posx;
			}
			
			/*==============================================================
						Side bar, preview window, drag 'n' drop functions
			==============================================================*/
			
			function initDraggable(){
					var newItem = document.createElement("div");
					newItem.style.position="absolute";
					newItem.style.backgroundColor="white";
					newItem.style.border="1px solid";
					newItem.setAttribute("name", "dragSrc");
					draggableWindow = newItem;
					draggableWindow.style.display = "none";
					document.getElementsByTagName("body")[0].appendChild(draggableWindow);
			}

			var draggableWindow = null;
			
			// Create's an absolute div ready for dragging
			function createDraggable(src, x, y, width, height){
				//document.getElementById("out").innerHTML = "-->" + src.getAttribute("name");
				
				draggableWindow.style.width=width+"px";
				draggableWindow.style.height=height+"px";
				draggableWindow.style.left = (x - Math.floor(width / 2)) + "px";
				draggableWindow.style.top = (y - Math.floor(defaultCellHeight / 2)) + "px";
				draggableWindow.style.position="absolute";
				draggableWindow.style.display="inline";
				draggableWindow.style.opacity = "0.7";
				draggableWindow.style.filter = "alpha(opacity=70)";
				draggableWindow.setAttribute("name", src.getAttribute("name"));

				if (src.getAttribute("name") == null)
				{
					var currentCell = getCellFromCoords(x, y);
					draggableWindow.setAttribute("name", currentCell.ref);

					//document.getElementById("out2").innerHTML = currentCell.ref;
				}

				return draggableWindow;
			}
			
			function hideDraggable(){
				draggableWindow.style.display = "none";
			}

			function removeActivity(event, cell)
			{
				var event = event ? event : window.event;

				if (cell)
				{
					var index = cell.getIndex();
				}
				else
				{
					var index = getCellFromCoords(getMouseX(event),getMouseY(event)).getIndex();
				}
				
				if(items[index.x][index.y].cells.length > 0)
				{
					for(k = 0; k < items[index.x][index.y].cells.length; k++)
					{
						items[items[index.x][index.y].cells[k].x] [items[index.x][index.y].cells[k].y] = null;
					}

				}
				items[index.x][index.y] = null;
				test();
				return;
			}

			function addActivityToArray(x, y, id, provider, loadedDuration, weekNumber)
			{
				if (activityCache[id] == undefined)
				{
					url = "dynamic/activityData.php?id=" + id;

					AjaxRequest.get({'url' : url,'onSuccess' : function(req)
						{
							// get data
							var xmlDoc = req.responseXML.documentElement;
							var duration = loadedDuration != undefined ? loadedDuration : xmlDoc.getElementsByTagName('duration')[0].childNodes[0].nodeValue * 2;
							var limit = xmlDoc.getElementsByTagName('limit')[0].childNodes[0].nodeValue * 2;

							cellToAdd = new Cell();
							cellToAdd.name = xmlDoc.getElementsByTagName('title')[0].childNodes[0].nodeValue;
							cellToAdd.ref = xmlDoc.getElementsByTagName('id')[0].childNodes[0].nodeValue;
							cellToAdd.type = xmlDoc.getElementsByTagName('type')[0].childNodes[0].nodeValue;
							cellToAdd.duration = duration;
							cellToAdd.limitMax = xmlDoc.getElementsByTagName('limit')[0].childNodes[0].nodeValue * 2
							cellToAdd.limitMin = xmlDoc.getElementsByTagName('duration')[0].childNodes[0].nodeValue * 2;
							cellToAdd.provider = provider;

							// display data
							for (spanLoop = 0; spanLoop < duration; spanLoop++)
							{
								if (items[x + spanLoop] != undefined)
								{
									items[x + spanLoop][y] = cellToAdd;
									items[x + spanLoop][y].parent = items[x][y];
								}																
							}

							// add to cache
							if (ignoreEmptyTags(xmlDoc.getElementsByTagName('imageURL')[0]) == "")
							{
								var currentImage = "/skins/boscomesparresorthotels/resources/filenotfound.png";
							}

							activityCache[cellToAdd.ref] = new Activity
							(
								cellToAdd.name, 
								xmlDoc.getElementsByTagName("details")[0].childNodes[0].nodeValue, 
								cellToAdd.type, 
								cellToAdd.duration, 
								cellToAdd.limitMax,
								ignoreEmptyTags(xmlDoc.getElementsByTagName("setday")[0]),
								ignoreEmptyTags(xmlDoc.getElementsByTagName("setslot")[0]),
								currentImage, 
								ignoreEmptyTags(xmlDoc.getElementsByTagName("moreURL")[0]) 
							);

							//providerscache
							var providers = xmlDoc.getElementsByTagName("providers")[0];

							for (providerLoop = 0; providerLoop < providers.getElementsByTagName("provider").length; providerLoop++)
							{
								activityCache[cellToAdd.ref].addProvider(providers.getElementsByTagName("pid")[providerLoop].childNodes[0].nodeValue, providers.getElementsByTagName("name")[providerLoop].childNodes[0].nodeValue);
							}
							
							test();
						}
					})
				}
				else
				{
					
					var currentActivity = activityCache[id].getDetails();
					var duration = loadedDuration != undefined ? loadedDuration : currentActivity.duration;
					var limit = currentActivity.limit;

					cellToAdd = new Cell();
					cellToAdd.name = currentActivity.title;
					cellToAdd.ref = id;
					cellToAdd.type = currentActivity.type;
					cellToAdd.duration = duration;
					cellToAdd.limitMax = currentActivity.limit;
					cellToAdd.limitMin = currentActivity.duration;
					cellToAdd.provider = provider;

					for (spanLoop = 0; spanLoop < duration; spanLoop++)
					{
						if (items[x + spanLoop] != undefined)
						{
							items[x + spanLoop][y] = cellToAdd;
							items[x + spanLoop][y].parent = items[x][y];
						}																
					}
				}
			}

			function generateWeekName(weekId)
			{
				switch (weekId)
				{
					case 0:
						return "Monday";
					case 1:
						return "Tuesday";
					case 2:
						return "Wednesday";
					case 3:
						return "Thursday";
					case 4:
						return "Friday";
					case 5:
						return "Saturday";
					case 6:
						return "Sunday";
					default:
						return "Unknown";
				}
			}

			function generateTime(timeslot)
			{
				var hours = Math.floor(startHour + (timeslot / 2));
				var minutes = timeslot % 2 == 0 ? "00" : "30";

				return hours + ":" + minutes;
			}

			function copyItemToCell(event, fromEvent)
			{
				var autoMoved = false;
				var needRemove = false;

				if (introLayer.style.display == "inline")
				{
					introLayer.style.display = "none";
				}

				if (getCellFromCoords(getMouseX(event), getMouseY(event)) == null)
				{
					return null;
				}

				var possibleCell = getCellFromCoords(getMouseX(event), getMouseY(event));
				var index = possibleCell.getIndex();

				var oldCell = getCellFromCoords(getMouseX(fromEvent), getMouseY(fromEvent));
				var oldCellRef = draggableWindow.getAttribute("name");
				var oldCellId = "";
				var oldCellProvider = -1;
				var temp = true;
				var overrun = false;

				if (oldCell != null)
				{
					oldIndex = oldCell.getIndex();

					if (oldIndex.x == index.x && oldIndex.y == index.y)
					{// is new position over the old position?
						if (roundNearest(getMouseY(fromEvent), defaultCellHeight) == roundNearest(getMouseY(event), defaultCellHeight))
						{// is new position the same as the old position?
							return null;
						}						
					}

					oldCellRef = oldCell.ref;
					oldCellId = oldCell.id;
					oldCellProvider = oldCell.provider;
				}
				
				if ((activityCache[oldCellRef].setday != index.y && activityCache[oldCellRef].setday != "") && (activityCache[oldCellRef].setslot != index.x && activityCache[oldCellRef].setslot != ""))
				{
					if (oldCell != null)
					{
						return null;
					}

					var choice = confirm("That activity is not availiable at the selected date and time, would you like to move it to " + generateWeekName(parseInt(activityCache[oldCellRef].setday)) + " at " + generateTime(parseInt(activityCache[oldCellRef].setslot)) + "?");

					if (choice == false)
					{
						return null;
					}

					index.x = parseInt(activityCache[oldCellRef].setslot);
					index.y = parseInt(activityCache[oldCellRef].setday);
				
					autoMoved = true;
				}
				else if (activityCache[oldCellRef].setday != index.y && activityCache[oldCellRef].setday != "")
				{
					if (oldIndex.x == index.x)
					{
						return null;
					}

					var choice = confirm("That activity is not availiable on the selected day, would you like to move it to " + generateWeekName(parseInt(activityCache[oldCellRef].setday)) + " at " + generateTime(index.x) + "?");

					if (choice == false)
					{
						return null;
					}

					//check for overlap
					if (index.x < oldIndex.x && (index.x + activityCache[oldCellRef].duration) > oldIndex.x)
					{
						needRemove = false;
					}
					else if (oldIndex.y != index.y)
					{
						needRemove = true;
					}

					index.y = parseInt(activityCache[oldCellRef].setday);
				
					autoMoved = true;
				}
				else if (activityCache[oldCellRef].setslot != index.x && activityCache[oldCellRef].setslot != "")
				{
					if (oldIndex.y == index.y)
					{
						return null;
					}

					var choice = confirm("That activity is not availiable at the selected time, would you like to move it to " + generateWeekName(index.y) + " at " + generateTime(parseInt(activityCache[oldCellRef].setslot)) + "?");

					if (choice == false)
					{
						return null;
					}

					index.x = parseInt(activityCache[oldCellRef].setslot);
				
					autoMoved = true;
					needRemove = true;
				}

				do
				{
					temp = true;

					for (slotLoop = 0; slotLoop < activityCache[oldCellRef].duration; slotLoop++)
					{
						if (index.x + (activityCache[oldCellRef].duration) > totalTimeSlots)
						{
							alert("Insufficient space to move activity there");
							return null;
						}

						if (items[index.x + slotLoop][index.y].ref != -1)
						{
							temp = false;
							possibleCell = items[index.x + slotLoop][index.y];
							break;
						}
					}

					if (temp == false)
					{
						if (possibleCell.id != oldCellId)
						{
							var choice = confirm("Are you sure you want to overwrite '" + activityCache[possibleCell.ref].title + "' with '" + activityCache[oldCellRef].title + "'?\n" + activityCache[possibleCell.ref].title + " will be removed from the timetable.")

							if (choice == false)
							{
								return null;
							}
						}

						removeActivity(event, possibleCell);

						if (autoMoved == false)
						{
							index = getCellFromCoords(getMouseX(event), getMouseY(event)).getIndex();
						}
					}

				}
				while (temp == false);
				
				// get new cell index now the move-to area has been unmerged
				var currentActivity = activityCache[draggableWindow.getAttribute("name")].getDetails();
				var duration = currentActivity.duration;
				var limit = currentActivity.limit;

				cellToAdd = new Cell();
				cellToAdd.name = currentActivity.title;
				cellToAdd.ref = draggableWindow.getAttribute("name");
				cellToAdd.type = currentActivity.type;
				cellToAdd.duration = duration;
				cellToAdd.limitMax = currentActivity.limit;
				cellToAdd.limitMin = currentActivity.duration;
				cellToAdd.provider = oldCellProvider;

				for (spanLoop = 0; spanLoop < duration; spanLoop++)
				{
					if (items[index.x + spanLoop] != undefined)
					{
						items[index.x + spanLoop][index.y] = cellToAdd;
						items[index.x + spanLoop][index.y].parent = items[index.x][index.y];
					}																
				}

				test();

				if (autoMoved == true && needRemove == false)
				{
					return null;
				}

				return "success";
			}

			function roundNearest(num, acc)
			{
				if ( acc < 0 ) 
				{
					return Math.round(num*acc)/acc;
				}
				else
				{
					return Math.round(num/acc)*acc;
				}
			}

			function generateElement(tag, content)
			{
				var xml;
				if (!content)
				{
					xml = '<' + tag + ' />\n';
				}
				else 
				{
					xml = '<'+ tag + '>' + content + '</' + tag + '>\n';
				}
				return xml;
			}

			function findNearestHotel()
			{
				var xmlDoc = xmlFromArray(items);

				AjaxRequest.post(
				{
				'url': "dynamic/activitySaver.php?function=nearestHotel", 'parameters': { 'xml': xmlDoc }, 'onSuccess' : function(xmlhttp) 
					{ 
						var xmlDoc = xmlhttp.responseXML.documentElement;

						var currentImage = ignoreEmptyTags(xmlDoc.getElementsByTagName('imageURL')[0]);
						var currentURL = "<br />";

						if (currentImage == "")
						{
							currentImage = "/skins/boscomesparresorthotels/resources/filenotfound.png";
						}
						if (ignoreEmptyTags(xmlDoc.getElementsByTagName('infoURL')[0]) != "")
						{
							currentURL = "<a href='" + ignoreEmptyTags(xmlDoc.getElementsByTagName('infoURL')[0]) + "'>More info...</a>";
						}
						

						var hotelOutput = document.getElementById("hotelOutput");
						//hotelOutput.innerHTML = "Suggested Hotel ID: " + req.responseText;

						hotelOutput.innerHTML = "<li class='activityList'><div name='suggestedhotel' class='activityPrev'><img class='activity' src='" + currentImage + "' alt='' /><p class='title'>" + xmlDoc.getElementsByTagName('title')[0].childNodes[0].nodeValue + "</p><p class='intro'>" + limitDescription(xmlDoc.getElementsByTagName('details')[0].childNodes[0].nodeValue) + "</p><p class='readMore'>" + currentURL + "</p></div></li>";
					}
				});
			}


function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
document.getElementById("statusBar").innerHTML = dumped_text;
}


			function xmlFromArray(week)
			{
				var xmlDoc = "<week>\n<date>temp</date>\n<activities>\n";
				var dayDetails;

				for (i = 0; i < 7; i++)
				{
					dayDetails = "";

					for (j = 0; j < week.length; j++)
					{
						dayDetails += "<timeslot>\n";
						dayDetails += generateElement("id", week[j][i].id);
						dayDetails += generateElement("ref", week[j][i].ref);
						dayDetails += generateElement("provider", week[j][i].provider);
						dayDetails += "</timeslot>\n";
					}

					xmlDoc += (dayDetails.length > 0) ? generateElement("day", dayDetails) : generateElement("day", "");

				}

				xmlDoc += "</activities>\n</week>\n";

				return xmlDoc;
			}

			function createXmlOutput()
			{
				var xmlDoc = "<planner>\n";

				weeks[currentWeek] = items;

				for (weekLoop = 0; weekLoop < weeks.length; weekLoop++)
				{
					xmlDoc += xmlFromArray(weeks[weekLoop]);
				}

				xmlDoc += "</planner>";
				
				AjaxRequest.post(
				  {
					'url': 'dynamic/activitySaver.php?function=save'
					,'parameters':{ 'xml': xmlDoc }
					,'onSuccess':function(req) { alert(req.responseText); }
				  }
				);
			}
			

			function createXmlInput()
			{
				introLayer.style.display = "none";
				weeks = new Array();

				AjaxRequest.get(
				{
					'url': "dynamic/activitySaver.php?function=load"
					,'onSuccess':function(req)  
					{ 
						var xmlDoc = req.responseXML.documentElement;
						var addedIds = new Array;

						var weeksXML = xmlDoc.getElementsByTagName("week");

						for (weekLoop = 0; weekLoop < weeksXML.length; weekLoop++)
						{
							currentWeekXML = weeksXML[weekLoop];

							for (i = 0; i < 7; i++)
							{
								currentDay = currentWeekXML.getElementsByTagName("day")[i];
								
								for (j = 0; j < items.length; j++)
								{
									cellToAdd = new Cell();

									if (currentDay.getElementsByTagName("ref")[j].childNodes[0].nodeValue != -1)
									{
										if (addedIds[currentDay.getElementsByTagName("id")[j].childNodes[0].nodeValue] == undefined)
										{																	
											var currentDuration = 0;
											for (xLoop = 0; xLoop < (items.length - j); xLoop++)
											{
												if (currentDay.getElementsByTagName("id")[j + xLoop].childNodes[0].nodeValue != currentDay.getElementsByTagName("id")[j].childNodes[0].nodeValue)
												{
													break;
												}

												currentDuration++;
											}

											addActivityToArray(j, i, currentDay.getElementsByTagName("ref")[j].childNodes[0].nodeValue, currentDay.getElementsByTagName("provider")[j].childNodes[0].nodeValue, currentDuration, weekLoop);
											addedIds[currentDay.getElementsByTagName("id")[j].childNodes[0].nodeValue] = "added";
										}
									}
									else
									{
										items[j][i] = null;
									}
								}
							}
							
							weeks.push(items);
						}
						
						test();
						findNearestHotel();
					}
				});
			}

			function displayActivityDetails(event, elem)
			{
				var activityDetails = activityCache[elem.getAttribute("name")];
				var extrahtml = "";

				if (activityDetails.setday != "")
				{
					extrahtml = "Availiable: " + generateWeekName(parseInt(activityDetails.setday)) + " " + generateTime(parseInt(activityDetails.setslot));
				}

				configWindow.setPosition((getMouseX(event) - 130), (getMouseY(event) - 130));
				configWindow.updateHTML(activityDetails.title + "<br />" + activityDetails.details + "<br />Duration: " + activityDetails.duration + " (max " + activityDetails.limit + ")<br /><br />" + extrahtml);

				configWindow.show();
			}
										

			function configureActivity(event)
			{
				var activityProviders = activityCache[draggableWindow.getAttribute("name")].getProviders();
				var selectOptions = "";
				var index = getCellFromCoords(getMouseX(event), getMouseY(event)).getIndex();

				for (providerLoop = 0; providerLoop < activityProviders.length; providerLoop++)
				{
					selectOptions += "<option value='" + activityProviders[providerLoop].id + "'";
					
					if (activityProviders[providerLoop].id == items[index.x][index.y].provider || (providerLoop == 0 && items[index.x][index.y].provider == -1))
					{
						selectOptions += " selected='selected'";
					}
					
					selectOptions += ">" + activityProviders[providerLoop].name + "</option>";
				}

				configWindow.setPosition((getMouseX(event) - 130), (getMouseY(event) - 130));
				configWindow.updateHTML("Configure Activity<br /><br />Select provider: <select id='confId'>" + selectOptions + "</select><br /><br /><input type='button' value='Save' onclick='saveConfig(" + index.x + ", " + index.y + ")' />");
				configWindow.show();				
			}

			function saveConfig(x, y)
			{
				items[x][y].setProvider(document.getElementById("confId").value)
				configWindow.hide();
			}

			function clearWeek()
			{
				var containsActivity = false;

				for (i = 0; i < items.length; i++)
				{
					for (j = 0; j < items[i].length; j++)
					{
						if (items[i][j].ref != -1)
						{
							containsActivity = true;
							break;
						}
					}

					if (containsActivity == true)
					{
						var response = confirm("Are you sure you want to clear the week's activities?");

						if (response)
						{
							initialiseItemsArray();
							test();
							introLayer.style.display = "inline";
						}

						break;
					}
				}
			}

			function clearToggle(barId)
			{
				var labelBar = document.getElementById(barId);
				var columnLabels = labelBar.getElementsByTagName("td");
				var classLabel = "early";

				if (barId == "toggleBottom")
				{
					classLabel = "late";
				}

				for (columnLoop = 1; columnLoop < columnLabels.length; columnLoop ++)
				{
					columnLabels[columnLoop].style.background = "#CCC";
				}
			}

			function colourToggle(barId)
			{
				var labelBar = document.getElementById(barId);
				var columnLabels = labelBar.getElementsByTagName("td");

				for (columnLoop = 1; columnLoop < columnLabels.length; columnLoop ++)
				{
					if (barId == "toggleTop")
					{
						for (rowLoop = hiddenRowsTop - 1; rowLoop >= 0; rowLoop--)
						{
							if (items[rowLoop][columnLoop - 1].ref != -1)
							{
								var newColour = activityCache[items[rowLoop][columnLoop - 1].ref].type;

								if (rowLoop == (hiddenRowsTop - 1))
								{
									imageType = "top";
								}
								else
								{
									imageType = "bottom";
								}

								columnLabels[columnLoop].style.background = "#CCC url(/skins/boscomesparresorthotels/resources/t" + newColour + "_" + imageType + ".png) repeat-x";
								break;
							}
						}
					}

					if (barId == "toggleBottom")
					{
						for (rowLoop = (items.length - hiddenRowsBottom); rowLoop < items.length; rowLoop++)
						{
							if (items[rowLoop][columnLoop - 1].ref != -1)
							{
								var newColour = activityCache[items[rowLoop][columnLoop - 1].ref].type;

								if (rowLoop == (items.length - hiddenRowsBottom))
								{
									imageType = "bottom";
								}
								else
								{
									imageType = "top";
								}

								columnLabels[columnLoop].style.background = "#CCC url(/skins/boscomesparresorthotels/resources/t" + newColour + "_" + imageType + ".png) repeat-x";
								break;
							}
						}
					}
				}
			}
			
			function toggleRows(tableLocation)
			{
				if (tableLocation == "top")
				{
					if (hiddenRowsTop == 0)
					{
						hiddenRowsTop = hiddenRowsQuantity;
					}
					else
					{
						hiddenRowsTop = 0;
					}
				}
				else if (tableLocation == "bottom")
				{
					if (hiddenRowsBottom == 0)
					{
						hiddenRowsBottom = hiddenRowsQuantity;
					}
					else
					{
						hiddenRowsBottom = 0;
					}
				}

				test();
			}	