// Copyright Ed Salmon 2009 //
// Any unauthorised copying or distributing of this code in strictly prohibited //

var tGrid=new Array(28);
var duration=2;
var time=0;
var court=0;
var repeat=0;
var repeatnum=0;
var timeout=0;
var balloonDelay = 0;
var refreshEnabled=true;
var deleteMode = false;

function RefreshTable()
{
	if(refreshEnabled)
	{
		refreshEnabled=false;
		$("feedback").innerHTML="Refreshing schedule. Please wait...";
		if($("main")!=null)
		{
			var div=$("main");
			var child=div.childNodes[1];
			//for internet explorer
			if(!child)
			{
				child=div.childNodes[0];
			}
			div.removeChild(child);
		}
		
		ClearAll();
		DrawTable(28,8);
		
	}
}

function XMLRequest()
{
	var request;
	
	if(window.XMLHttpRequest)
	{
		request=new XMLHttpRequest;
	}
	else if(window.ActiveXObject)
	{
		request=new ActiveXObject("MSXML2.XMLHTTP.3.0"); 
	}
	
	return request;
}

function DrawTable(numRows, numColumns)
{
	var req=XMLRequest();
	var address="new/loadBookings.php?date="+date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
	
	if(req)
	{
		
		req.open("GET", address , true);
		req.send(null);
		req.onreadystatechange = function() 
		{
			if(req.readyState == 4 && req.status == 200) 
			{
				
				
				xml=req.responseXML;
				
				var datespan=$("datespan");
				datespan.innerHTML=date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear();
				var mBody = $("main");
				//fix stupid IE DOM bug -> check for IE first before going through normal motions
				try{
					var tbl=document.createElement("<table cellspacing='0' onmouseover='ChangeCursor(this)' onmouseout='ResetCursor(this)'>");
				}
				catch(e)
				{
					var tbl=document.createElement("table");
					tbl.setAttribute("cellspacing","0");
					tbl.setAttribute("onmouseover","ChangeCursor(this)");
					tbl.setAttribute("onmouseout","ResetCursor(this)");
					
				}
				
				tbl.style.border="1px solid #000000";
				//tbl.style.backgroundImage = 'url(/cb/images/background.jpg)';
				
				if(fullscreen)
				{
					try{
					tbl.style.width = window.innerWidth;
					tbl.style.height = window.innerHeight;
					}
					catch(e) {
						tbl.style.width = document.body.clientWidth;
						tbl.style.height = document.body.clientHeight;
					}
						
				}
				else{
					tbl.style.width="780px";
				}
				
				//tbl.style.emptyCells="show";
				tbl.style.marginLeft="auto";
				tbl.style.marginRight="auto";
				
				var tbody=document.createElement("tbody");
				var titlerow=document.createElement("tr");
				
				
				for(var i=0;i<numRows;i++)
				{
					tGrid[i]=new Array(7);
					var row = document.createElement("tr");
					
					for(var j=0;j<numColumns;j++)
					{
						//fix stupid IE DOM bug -> check for IE first before going through normal motions
						try{
							if(i%2==0)
							{
								var cell=document.createElement("<td class='cbrow2' onmouseover='HighlightCells("+i+","+j+")' onmouseout='ClearCells("+i+","+j+")' onclick='CheckCell("+i+","+j+")'>");
							}
							else
							{
								var cell=document.createElement("<td class='cbrow1' onmouseover='HighlightCells("+i+","+j+")' onmouseout='ClearCells("+i+","+j+")' onclick='CheckCell("+i+","+j+")'>");
							}
								
						}
						catch(e)
						{
							var cell=document.createElement("td");
							if(i%2==0)
							{
								cell.setAttribute("class","cbrow2");
							}
							else
							{
								cell.setAttribute("class","cbrow1");
							}
							cell.setAttribute("onmouseover","HighlightCells("+i+","+j+")");
							cell.setAttribute("onmouseout","ClearCells("+i+","+j+")");
							cell.setAttribute("onclick","CheckCell("+i+","+j+")");
						
						}
							var cellText=document.createTextNode("");
							cell.appendChild(cellText);
							row.appendChild(cell);
							tGrid[i][j]=row.childNodes[j];						
							
						
						//node MUST exist before we parse data
						if(xml.getElementsByTagName("id")[i]&&xml.getElementsByTagName("id")[i].childNodes[j]&&xml.getElementsByTagName("id")[i].childNodes[j].childNodes[0]&&xml.getElementsByTagName("id")[i].childNodes[j].childNodes[0].nodeValue)
						{
							cell.innerHTML=xml.getElementsByTagName("id")[i].childNodes[j].childNodes[0].nodeValue;
						}
						cell.innerHTML += "&nbsp;";
					}
					
					tbody.appendChild(row);
				}
				tbl.appendChild(tbody);
				mBody.appendChild(tbl);
				
				for(var k=0;k<8;k++)
				{
				tGrid[0][k].style.fontWeight="bold";
				tGrid[0][k].style.textDecoration="underline";
				}
				$("feedback").innerHTML="";
				refreshEnabled=true;
			}
		}
	}
	
}


function DeInit()
{
	for(var i=0;i<28;i++)
	{
		for(var j=0;j<8;j++)
		{
			if(tGrid[i][j].innerHTML == "RESERVED")
			{
				tGrid[i][j].innerHTML = "&nbsp;";
			}
		}
	}
}

function ClearAll()
{

	var req=XMLRequest();
	var address="new/removeBooking.php";
	
	if(req)
	{
		req.open("GET", address, true);
		
		req.send(null);
		
		req.onreadystatechange = function() 
		{
			if(req.readyState == 4 && req.status == 200) 
			{
				
			}
		}
	}
}

function ChangeCursor(table)
{
	table.style.cursor='default';
}

function ResetCursor(table)
{
	table.style.cursor='auto';
}
	
function UpdateDuration(form)
{
	duration=form.rg1.value;	
}

function UpdateDeleteMode( box ) {
	
	deleteMode=!deleteMode;
}

function UpdateRepeat(form)
{
	var len=form.rp.length;
	
	for(var i=0;i<len;i++)
	{
		if(form.rp[i].checked)
		{
			
			repeat=form.rp[i].value;
			
		}
	}
	repeatnum=form.repeatnumber.value;
	
	if(repeat==0)
	{
		$("repeatspan").innerHTML="Off";
	}
	else if(repeat==1)
	{
		$("repeatspan").innerHTML="On - Daily - "+repeatnum+" Day(s)";
	}
	else if(repeat==2)
	{
		$("repeatspan").innerHTML="On - Weekly - "+repeatnum+" Week(s)";
	}
	
}

function HighlightCells(row,column)
{
	//clearTimeout(balloon);
	//timeout=setTimeout('DeInit()',60000);
	
	//ShowPopup( row, column );
	
	if(row>=(27 - duration + 2))
	{
		
		row=27-(duration-1);
		
	}
	
	
	//set column to dark grey
	tGrid[0][column].style.backgroundColor='#E0E0E0';
	tGrid[1][column].style.backgroundColor='#E0E0E0';
	tGrid[2][column].style.backgroundColor='#E0E0E0';
	tGrid[3][column].style.backgroundColor='#E0E0E0';
	tGrid[4][column].style.backgroundColor='#E0E0E0';
	tGrid[5][column].style.backgroundColor='#E0E0E0';
	tGrid[6][column].style.backgroundColor='#E0E0E0';
	tGrid[7][column].style.backgroundColor='#E0E0E0';
	tGrid[8][column].style.backgroundColor='#E0E0E0';
	tGrid[9][column].style.backgroundColor='#E0E0E0';
	tGrid[10][column].style.backgroundColor='#E0E0E0';
	tGrid[11][column].style.backgroundColor='#E0E0E0';
	tGrid[12][column].style.backgroundColor='#E0E0E0';
	tGrid[13][column].style.backgroundColor='#E0E0E0';
	tGrid[14][column].style.backgroundColor='#E0E0E0';
	tGrid[15][column].style.backgroundColor='#E0E0E0';
	tGrid[16][column].style.backgroundColor='#E0E0E0';
	tGrid[17][column].style.backgroundColor='#E0E0E0';
	tGrid[18][column].style.backgroundColor='#E0E0E0';
	tGrid[19][column].style.backgroundColor='#E0E0E0';
	tGrid[20][column].style.backgroundColor='#E0E0E0';
	tGrid[21][column].style.backgroundColor='#E0E0E0';
	tGrid[22][column].style.backgroundColor='#E0E0E0';
	tGrid[23][column].style.backgroundColor='#E0E0E0';
	tGrid[24][column].style.backgroundColor='#E0E0E0';
	tGrid[25][column].style.backgroundColor='#E0E0E0';
	tGrid[26][column].style.backgroundColor='#E0E0E0';
	tGrid[27][column].style.backgroundColor='#E0E0E0';
		
	tGrid[row][column].style.borderTopColor='#FF0000';
	
	for( var i=0; i<duration; i++ ) {
		
		tGrid[row + i][column].style.borderLeftColor = '#FF0000';
		tGrid[row + i][column].style.borderRightColor = '#FF0000';
		
		tGrid[row + i][0].style.backgroundColor='#E0E0E0';
		tGrid[row + i][1].style.backgroundColor='#E0E0E0';
		tGrid[row + i][2].style.backgroundColor='#E0E0E0';
		tGrid[row + i][3].style.backgroundColor='#E0E0E0';
		tGrid[row + i][4].style.backgroundColor='#E0E0E0';
		tGrid[row + i][5].style.backgroundColor='#E0E0E0';
		tGrid[row + i][6].style.backgroundColor='#E0E0E0';
		tGrid[row + i][7].style.backgroundColor='#E0E0E0';
		
		
		
		if( deleteMode ) {
			
			if( (tGrid[row + i][column].innerHTML==(user+'&nbsp;')) ) {
			
				tGrid[row + i][column].style.backgroundColor='#B5C776';
			}
			else {
				
				tGrid[row + i][column].style.backgroundColor='#ff0000';
			
			}
			
		}
		else {
			
			if( tGrid[row + i][column].innerHTML != '&nbsp;' ) {
				
				tGrid[row + i][column].style.backgroundColor='#ff0000';
			
			}
			else {
				
				tGrid[row + i][column].style.backgroundColor='#ffffff';
			}
			
		}
	
	}
		
	tGrid[row + (duration - 1)][column].style.borderBottomColor='#FF0000';
				
}

function ClearCells(row,column)
{
	if(row>=(27 - duration + 2))
	{
		
		row=27-(duration-1);
		
	}
	
	
	
	tGrid[row][column].style.borderTopColor='#ffffff';
	
	for( var i=0; i<duration; i++ ) {
		
		tGrid[row + i][column].style.borderLeftColor = '#ffffff';
		tGrid[row + i][column].style.borderRightColor = '#ffffff';
		
		tGrid[row + i][0].style.backgroundColor='#E0E0E0';
		tGrid[row + i][1].style.backgroundColor='#E0E0E0';
		tGrid[row + i][2].style.backgroundColor='#E0E0E0';
		tGrid[row + i][3].style.backgroundColor='#E0E0E0';
		tGrid[row + i][4].style.backgroundColor='#E0E0E0';
		tGrid[row + i][5].style.backgroundColor='#E0E0E0';
		tGrid[row + i][6].style.backgroundColor='#E0E0E0';
		tGrid[row + i][7].style.backgroundColor='#E0E0E0';
		
		if((row+i) % 2) {
			
			tGrid[row+i][0].style.backgroundColor='#FFFFFF';
			tGrid[row+i][1].style.backgroundColor='#FFFFFF';
			tGrid[row+i][2].style.backgroundColor='#FFFFFF';
			tGrid[row+i][3].style.backgroundColor='#FFFFFF';
			tGrid[row+i][4].style.backgroundColor='#FFFFFF';
			tGrid[row+i][5].style.backgroundColor='#FFFFFF';
			tGrid[row+i][6].style.backgroundColor='#FFFFFF';
			tGrid[row+i][7].style.backgroundColor='#FFFFFF';
			
		}
		else {
			
			tGrid[row+i][0].style.backgroundColor='#F0F0F0';
			tGrid[row+i][1].style.backgroundColor='#F0F0F0';
			tGrid[row+i][2].style.backgroundColor='#F0F0F0';
			tGrid[row+i][3].style.backgroundColor='#F0F0F0';
			tGrid[row+i][4].style.backgroundColor='#F0F0F0';
			tGrid[row+i][5].style.backgroundColor='#F0F0F0';
			tGrid[row+i][6].style.backgroundColor='#F0F0F0';
			tGrid[row+i][7].style.backgroundColor='#F0F0F0';
						
		}
	}
	
	tGrid[row + (duration - 1)][column].style.borderBottomColor='#ffffff';
	
	
	tGrid[0][column].style.backgroundColor='#F0F0F0';
	tGrid[1][column].style.backgroundColor='#FFFFFF';
	tGrid[2][column].style.backgroundColor='#F0F0F0';
	tGrid[3][column].style.backgroundColor='#FFFFFF';
	tGrid[4][column].style.backgroundColor='#F0F0F0';
	tGrid[5][column].style.backgroundColor='#FFFFFF';
	tGrid[6][column].style.backgroundColor='#F0F0F0';
	tGrid[7][column].style.backgroundColor='#FFFFFF';
	tGrid[8][column].style.backgroundColor='#F0F0F0';
	tGrid[9][column].style.backgroundColor='#FFFFFF';
	tGrid[10][column].style.backgroundColor='#F0F0F0';
	tGrid[11][column].style.backgroundColor='#FFFFFF';
	tGrid[12][column].style.backgroundColor='#F0F0F0';
	tGrid[13][column].style.backgroundColor='#FFFFFF';
	tGrid[14][column].style.backgroundColor='#F0F0F0';
	tGrid[15][column].style.backgroundColor='#FFFFFF';
	tGrid[16][column].style.backgroundColor='#F0F0F0';
	tGrid[17][column].style.backgroundColor='#FFFFFF';
	tGrid[18][column].style.backgroundColor='#F0F0F0';
	tGrid[19][column].style.backgroundColor='#FFFFFF';
	tGrid[20][column].style.backgroundColor='#F0F0F0';
	tGrid[21][column].style.backgroundColor='#FFFFFF';
	tGrid[22][column].style.backgroundColor='#F0F0F0';
	tGrid[23][column].style.backgroundColor='#FFFFFF';
	tGrid[24][column].style.backgroundColor='#F0F0F0';
	tGrid[25][column].style.backgroundColor='#FFFFFF';
	tGrid[26][column].style.backgroundColor='#F0F0F0';
	tGrid[27][column].style.backgroundColor='#FFFFFF';
			
		
}

function CheckCell(row,column)
{
	
	if(deleteMode)
	{
		
		DeleteBooking(row,column);
	}
	else
	{
		
		if(row>=(27 - duration + 2))
		{
			
			row=27-(duration-1);
			
		}
		
		var allowed=true;	
	
		if( heirarchy != "Top" ) {
			
			if( VerifyDate( date ) ) {
				
				allowed = true;
				
			} else {
				
				alert( "You cannot make a booking greater than 8 days in advance." );
				return false;
				
			}
			

		}

		tGrid[row][column].style.borderTopColor='#000000';
	
		for(var i=0; i<duration; i++) {
			
			tGrid[row+i][column].style.borderLeftColor='#000000';
			tGrid[row+i][column].style.borderRightColor='#000000';
			
			if( tGrid[row+i][column].innerHTML != '&nbsp;' ) {
				
				allowed = false;
			}
			
		}
	
		
		
		tGrid[row + (duration-1)][column].style.borderBottomColor='#000000';	
		
		time=row-1;
		court=(column-1);
		
		if(allowed)
		{
			var feedbackspan=$("feedback");
			feedbackspan.innerHTML="Reserving court. Please wait...";
			
			var req=XMLRequest();
			var address="new/reserveBooking.php?date="+date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+"&time="+time+"&court="+court+"&duration="+(parseInt(duration)-1)+"&override="+user+"&repeat="+repeatnum+"&repeatType="+repeat;
		
			if(req)
			{
				req.open("GET", address, true);
				
				req.send(null);
				req.onreadystatechange = function() 
				{
					if(req.readyState == 4 && req.status == 200) 
					{
						if(req.responseText!="")
						{
							if( req.responseText == 'error1' ) {
								
								alert("You cannot make this booking as your total duration of bookings today would exceed the permitted 1 hour 30 minutes.");
								
							} else {
								
								DeInit();
								
								for(var i = 0; i<duration; i++) {
									
									tGrid[row + i][column].innerHTML="RESERVED";
									
								}
													
								feedbackspan.innerHTML="";
								clearTimeout(timeout);
								timeout=setTimeout('DeInit()',60000);
								
							}
						}
					}
				}
			}
			
		allowed=false;
		
		}
		else {
			
			alert( "You cannot book this time slot." );
		}
	}
}
	
function MakeBooking()
{
	if(refreshEnabled)
	{
		refreshEnabled=false;
		
		var feedbackspan=$("feedback");
		feedbackspan.innerHTML="Booking court. Please wait...";
		var req=XMLRequest();
		
		if(req)
		{
			req.open("GET", "new/addBooking.php", true);
			
			req.send(null);
			
			req.onreadystatechange = function() 
			{
				if(req.readyState == 4 && req.status == 200) 
				{
					for(var i=0;i<28;i++)
					{
						for(var j=0;j<8;j++)
						{
							if(tGrid[i][j].innerHTML == "RESERVED")
							{
								//var str = decodeURIComponent(req.responseText);
								tGrid[i][j].innerHTML = req.responseText +'&nbsp;';
							}
						}
					}
					
				feedbackspan.innerHTML="";
				clearTimeout(timeout);
				refreshEnabled=true;
				
				}
			}
		}
	}
}

function OverrideName()
{
	var txtName=$("txtOverride").value;
	user=txtName;
	if(user=="") {
		user = userFull;
	}
	$("currentname").innerHTML=user;
	
}


function DeleteBooking(row,column)
{
	var fullname=user;
	var allowed = true;
	
	if(row>=(27 - duration + 2))
	{
		
		row=27-(duration-1);
		
	}
	
	for(var i=0; i<duration; i++) {
		
		if( tGrid[row+i][column].innerHTML != (fullname+'&nbsp;') ) {
			
			allowed = false;
		
		}
	}
	
	if(allowed)
	{
		var check=confirm("Please confirm that you wish to delete this booking.");
		
		if(check)
		{
			var req=XMLRequest();
			var address="new/deleteBooking.php?date="+date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+"&time="+(row-1)+"&court="+(column-1)+"&duration="+(parseInt(duration)-1)+"&override="+user+"&repeat="+repeatnum+"&repeatType="+repeat;
			if(req)
			{
				$("feedback").innerHTML="Deleting booking. Please wait...";
				
				req.open("GET", address , true);
				
				req.send(null);
				
				req.onreadystatechange = function() 
				{
					if(req.readyState == 4 && req.status == 200) 
					{
						
						if(req.responseText==1)
						{
							for(var i=0;i<duration; i++) {
								
								tGrid[row+i][column].innerHTML = '&nbsp;';
								
							}
						}
						
						$("feedback").innerHTML="";
					}
				}
			}
		}
	}
	else
	{
		alert("You cannot delete this booking.");
	}
}

function VerifyDate( date ) {
	
	var today = new Date();
	
	today.setDate(today.getDate() + 8);

	if( date > today ) {
				 
		return false;
		
	} else {
		
		return true;
		
	}	
}

function ShowPopup( x, y ) {
	
	var time = y-1;
	var court = x-1;
	
	var req = XMLRequest();
	var address = 'loadInformation.php?date='+date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+"time="+time+"&court="+court;
	
	if(req)
	{		
		req.open("GET", address , true);
		
		req.send(null);
		
		req.onreadystatechange = function() 
		{
			if(req.readyState == 4 && req.status == 200) 
			{
				
				$('balloon').innerHTML = req.responseText;
				$('balloon').style.top = tGrid[y].style.top;
				$('balloon').style.top = tGrid[x].style.left + tGrid[x].style.width;
				$('balloon').toggle();
				
			}
		}
	}
	
}

	
