window.onload = init;
var wd = window.document;
var clx ='#ff9900';
var mray = '';


function init() 
			{
				history.go(+1); // disable back button
			//
			//alert(mray); - true
			  if (!document.getElementsByTagName) 
			  		{
							return false;
					}
				else
					{
						/*
						var rows = window.document.getElementsByTagName("tr");
						
						for(var i = 0; i<rows.length; i++)
							{
								if (rows[i].className == "hiddenrow") 
									{
									//alert('hi'); - true
									wd.getElementById(rows[i].id).style.visibility = 'hidden';
									}
							}
						*/
						
						
						if(wd.getElementById('cell_hts'))
							{
								mray = wd.getElementById('cell_hts').value;
								//alert(mray); - true
							}
							
					  var lnks = window.document.getElementsByTagName("td");
					  
					  //alert(lnks.length); - true
					
					  for (var i=0; i<lnks.length; i++) 
						{
							
							if (lnks[i].className == "xtblcell") 
								{
									//alert('hi'); - true
									wd.getElementById(lnks[i].id).style.backgroundColor = '#ffffff';
									/*
									if(wd.getElementById(lnks[i].id).style.backgroundColor = '#ffffff')
										{
											alert('hip'); - true
										}
									*/ 
									lnks[i].onclick = function() 
										{
											//alert(this.getAttribute("id")); - true
											click_cell(this.getAttribute("id"));
											return false;
										}
								}
							else if (lnks[i].className == "xtblselected") 
								{
									wd.getElementById(lnks[i].id).style.backgroundColor = clx;
									lnks[i].onclick = function() 
										{
											
											click_cell(this.getAttribute("id"));
											return false;
										}
								}
						}
						
					var lnks = window.document.getElementsByTagName("th");
					
					  for (var i=0; i<lnks.length; i++) 
						{
							
							if (lnks[i].className == "xtblhead") 
								{
									
									lnks[i].onclick = function() 
										{
											
											book_day(this.getAttribute("id"));
											
											return false;
										}
								}
							
						}
						
						
					}
			}
			
function click_cell(obid)
	{
		//alert(obid);
		$c = wd.getElementById(obid).style.backgroundColor;
		//alert($c);
		if($c == '#ffffff' || $c == 'rgb(255, 255, 255)')
			{
				//alert('add ' + obid);
				add_cell(obid);
			}
		else
			{
				//alert('clear ' + obid);
				clear_cell(obid);
				
			}
			
	}
	
function clear_cell(obid)
	{
								wd.getElementById(obid).style.backgroundColor = '#ffffff';
				
								mray = wd.getElementById('cell_hts').value;
								if(mray != '')
									{
										if (mray.indexOf(obid) !=-1)
												{
													mray = mray.replace('.' + obid,''); 
													wd.getElementById('cell_hts').value = mray;
													
												}
									}
										
								// check if previous and latter blocks have been booked if there is only one then we need to wipe this 
								
								
	}
	
	
	
function add_cell(obid)
	{
		//alert(obid);
		//alert(mray);
		wd.getElementById(obid).style.backgroundColor = clx;
		
		if(mray != null)
			{
			if (mray.indexOf(obid) ==-1) // check if it is already in the array
				{
					mray = mray + '.' + obid;
				}
			}
		else
			{
				mray = '.' + obid;
			}
		
		wd.getElementById('cell_hts').value = mray;
	}
	
function add_thirty(str)
	{
		str1 = str.substring(0,2);
		str2 = str.substring(2,4);
		// work out what the next time slot is
		if(str2 == '30')
			{
				str2 = '00';
				str = str1 + '' + str2;
			}
		else
			{
				str2 = '30';
				if(str1.substring(0,1) == 0)
					{
						str1 = str1.substring(1,2);
					}
				
				str1 = parseInt(str1);
				str1 = str1 - 1;
				str1 = str1 + ''; // turn it into a string again.
				if(str1.length == 1)
					{
						str1 = '0' + str1;
					}
				str = '' + str1 + '' + str2;
			}
		 return str;
	}
	
	

function expand_table()
	{
				
		obj = getElementsByClass('hiddenrow');
		
		$c = obj.length;
		
		if($c == 0)
			{
				obj = getElementsByClass('srow2');
				$c = obj.length;
			}
		
		for(i = 0; i < $c;i++)
			{
				//alert(obj[i].id);
				oid = obj[i].id;
				//alert(wd.getElementById(oid).style.display);
				if(wd.getElementById(oid).className == 'srow2')
					{
						wd.getElementById(oid).className = 'hiddenrow';
					}
				else
					{
						
						wd.getElementById(oid).className= 'srow2';
						
					}
				
			}
		
		
	}
	
	
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function book_day(obid)
	{
		
			 			var lnks = window.document.getElementsByTagName("td");
						//mray = wd.getElementById('cell_hts').value;
						//alert(mray)
						obid3 = next_day(obid); // next day
						
						  for (var i=0; i<lnks.length; i++) 
							{
								obid2 = lnks[i].id;
								
								if (obid2.substring(0,12) == obid && ((parseFloat(obid2.substring(12,14)) == 7 && parseFloat(obid2.substring(14,16)) == 30) || (parseFloat(obid2.substring(12,14)) > 7))) 
									{
										
										
										add_cell(obid2);
									}
								else if (obid2.substring(0,12) == obid3 && ((parseFloat(obid2.substring(12,14)) == 7 && parseFloat(obid2.substring(14,16)) == 00) || (parseFloat(obid2.substring(12,14)) < 7))) 
									{
										add_cell(obid2);
									}
								
									
								
							}
							
						// show the whole table
						
						obj = getElementsByClass('hiddenrow');
		
						$c = obj.length;
						
						
						
						for(i = 0; i < $c;i++)
							{
								//alert(obj[i].id);
								oid = obj[i].id;
								
								if(wd.getElementById(oid).className == 'hiddenrow')
									{
										wd.getElementById(oid).className= 'srow2';
									}
							}
							
						wd.getElementById('cell_hts').value = mray;
						
						wd.getElementById(obid).onclick = function() 
												{
													clear_day(this.getAttribute("id"));
													return false;
												}
						
	}

function next_day(obid)
	{
		var year = obid.substring(4,8);
		var month = parseFloat(obid.substring(8,10));
		var day = parseFloat(obid.substring(10,12));
		var md = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
			{ //leap year
				md[1] = 29;
			}
			
		day = day + 1;
		//alert(md[month]);
		
		if(day <= md[month - 1])
			{
				month = month + '';
				day = day + '';
				if(month.length == 1)
					{
						month = '0' + month;
					}
				if(day.length == 1)
					{
						day = '0' + day;
					}
				obid = 'cell' + year + '' + month + '' + day;
			}
		else
			{
				day = 1;
				month = month + 1;
				if(month == 13)
					{
						month = 1;
						year = year + 1;
					}
				month = month + '';
				day = day + '';
				if(month.length == 1)
					{
						month = '0' + month;
					}
				if(day.length == 1)
					{
						day = '0' + day;
					}
				obid = 'cell' + year + '' + month + '' + day;
				
			}
		
		return obid;

	}
	
function clear_day(obid)
	{
		
			 			var lnks = window.document.getElementsByTagName("td");
						//mray = wd.getElementById('cell_hts').value;
						//alert(mray)
						obid3 = next_day(obid); // next day
						
						  for (var i=0; i<lnks.length; i++) 
							{
								obid2 = lnks[i].id;
								
								if (obid2.substring(0,12) == obid && ((parseFloat(obid2.substring(12,14)) == 7 && parseFloat(obid2.substring(14,16)) == 30) || (parseFloat(obid2.substring(12,14)) > 7))) 
									{
										
										
										clear_cell(obid2);
									}
								else if (obid2.substring(0,12) == obid3 && ((parseFloat(obid2.substring(12,14)) == 7 && parseFloat(obid2.substring(14,16)) == 00) || (parseFloat(obid2.substring(12,14)) < 7))) 
									{
										clear_cell(obid2);
									}
								
									
								
							}
							
						wd.getElementById('cell_hts').value = mray;
						
						wd.getElementById(obid).onclick = function() 
												{
													book_day(this.getAttribute("id"));
													return false;
												}
	}
