
	var GLOBALNoOnClick = false;
	var GLOBALNoTotal 	= false;
	var GLOBALAvailable = "";
	var GLOBALLastCall  = [];
	var GLOBALTotal		= 0;

	// global selected
	var bcBSel	= [];
	var bcSel 	= [];
	var bcTotal = 0;

	// some other data holders
	var ppSel 	= [];
	var psSel 	= [];
	var jcSel 	= [];
	var blackSel = [];

	/**
	* v simple calendar
	*
	* used an existing one, will replace this with dom when get a chance.
	*/
	basiccalendar = function ()
	{

     	this.itemId 	= "bookingitems";
     	this.selectedBG	= "#3f9fdf";
     	this.soldSelectedBG = "#CF003f";
     	this.journeySelectedBG = "#AFBF2F";
     	this.type		= "";
     	this.onClick 	= "";
   		this.swingStart = 0;
		this.weekBooking 	= false;
		this.monthBooking 	= false;		
		
     	this.fadeOut = function (id)
     	{
     	    $("#" + id).animate({"opacity": "0"}, 	"fast");
     	}

     	this.fadeIn = function (id)
     	{
     	    $("#" + id).animate({"opacity": "1"}, 	"fast");
     	}

     	this.swingRight = function (id)
     	{
     	    //$("#swing-" + id).animate({"left": "+=300px"}, 	"slow");
     	}

     	this.swingLeft = function (id)
     	{
     	    //$("#swing-" + id).animate({"left": "-=300px"}, 	"slow");
     	}

     	this.buildData = function (m, y, cM, cH, cDW, cD, brdr, applyTo)
		{

			mstr = new String(m);

			if (mstr.length == 1)
				mstr = "0" + mstr;

			swingStart = this.swingStart;
			
			// set handler
			switch (this.type)
			{
				case "parkBooking":

					if (typeof ppSel[y + '-' + mstr] != "undefined")
					{
						this.build(m, y, cM, cH, cDW, cD, brdr, applyTo);
						//GLOBALLastCall["parkBooking"] = this;
						
						GLOBALLastCall["parkBooking"]  = "var GLOBALNoOnClick = false;var GLOBALParkingSpaceId = 0;var GLOBALNoTotal = true;";
						GLOBALLastCall["parkBooking"] += "bc = new basiccalendar();";
						GLOBALLastCall["parkBooking"] += "bc.selectedBG = '" + tmp.selectedBG + "';";
						GLOBALLastCall["parkBooking"] += "bc.type = 'parkBooking';";
						GLOBALLastCall["parkBooking"] += "bc.buildData(" + m + ", " + y + ", '" + cM + "', '" + cH + "', '" + cDW + "', '" + cD + "', " + brdr + ", '" + applyTo + "');";
						
					}else{

						GLOBALLastCall["tmp"] = this;
						
						Ext.Ajax.request({
							url: 		"/action.php?action=loadParkBookings&parkingspaceId=" + GLOBALParkingSpaceId + "&month=" + m + "&year=" + y,
							success: function(res, o)
							{
								//bookingData = Ext.util.JSON.decode(res.responseText);
								eval(res.responseText);
								
								tmp = GLOBALLastCall["tmp"];
								//tmp 		= new basiccalendar();
								//tmp.type 	= "parkBooking";
								//tmp.swingStart = swingStart;
								//tmp.selectedBG = this.selectedBG;
								tmp.build(m, y, cM, cH, cDW, cD, brdr, applyTo);

								GLOBALLastCall["parkBooking"]  = "var GLOBALNoOnClick = false;var GLOBALParkingSpaceId = 0;var GLOBALNoTotal = true;";
								GLOBALLastCall["parkBooking"] += "bc = new basiccalendar();";
								GLOBALLastCall["parkBooking"] += "bc.selectedBG = '" + tmp.selectedBG + "';";
								GLOBALLastCall["parkBooking"] += "bc.type = 'parkBooking';";
								GLOBALLastCall["parkBooking"] += "bc.buildData(" + m + ", " + y + ", '" + cM + "', '" + cH + "', '" + cDW + "', '" + cD + "', " + brdr + ", '" + applyTo + "');";
							}
						});
						return;
					}
				break;

				case "paypark":

					if (typeof ppSel[y + '-' + mstr] != "undefined")
					{
						this.build(m, y, cM, cH, cDW, cD, brdr, applyTo);
					}else{

						Ext.Ajax.request({
							url: 		"/action.php?action=loadParkBookings&month=" + m + "&year=" + y,
							success: function(res, o)
							{
								//bookingData = Ext.util.JSON.decode(res.responseText);
								eval(res.responseText);

								tmp 		= new basiccalendar();
								tmp.type 	= "paypark";
								tmp.swingStart = swingStart;
								tmp.build(m, y, cM, cH, cDW, cD, brdr, applyTo);
							}
						});
						return;
					}

				break;

				case "parksold":

					if (typeof ppSel[y + '-' + mstr] != "undefined")
					{
						this.build(m, y, cM, cH, cDW, cD, brdr, applyTo);
					}else{

						Ext.Ajax.request({
							url: 		"/action.php?action=loadParkBookings&out=1&month=" + m + "&year=" + y,
							success: function(res, o)
							{
								//bookingData = Ext.util.JSON.decode(res.responseText);
								eval(res.responseText);

								tmp 		= new basiccalendar();
								tmp.type 	= "parksold";
								tmp.swingStart = swingStart;
								tmp.build(m, y, cM, cH, cDW, cD, brdr, applyTo);

							}
						});
						return;
					}
				break;

				case "journeyBooking":
						this.build(m, y, cM, cH, cDW, cD, brdr, applyTo);
				break;

				case "journeybooked":
					var onClick = "bc = new basiccalendar();bc.handler(this);";
				break;


			}


		}

     	/**
     	* build the actual table etc
     	*/
		this.build = function (m, y, cM, cH, cDW, cD, brdr, applyTo)
		{


			// set handler
			switch (this.type)
			{
				case "parkBooking":
					var funcname = "buildData";
					var onClick = "bc = new basiccalendar();bc.selectedBG='" + this.selectedBG + "';bc.type='parkBooking';bc.handler(this);";
				break;

				case "journeyBooking":
					this.selectedBG = this.journeySelectedBG;
					var funcname 	= "buildData";
					var onClick 	= "bc = new basiccalendar();bc.type='journeyBooking';bc.selectedBG = bc.journeySelectedBG;bc.handler(this);";
					this.onClick = onClick;
				break;

				case "paypark":
					var funcname = "buildData";
					var onClick = "bc = new basiccalendar();bc.type='paypark';bc.forwardParkingspace(this);";
					this.onClick = onClick;
				break;

				case "parksold":
					var funcname = "buildData";
					var onClick = "bc = new basiccalendar();bc.type='parksold';bc.forwardEditParkingspace(this);";
					this.onClick = onClick;
				break;

				case "journeybooked":
					var funcname = "build";
					var onClick = "bc = new basiccalendar();bc.type='journeybooked';bc.handler(this);";
					this.onClick = onClick;
					var clickAll = "bc = new basiccalendar();bc.handler('all');";
				break;
			}


			var mn	= ['January','February','March','April','May','June','July','August','September','October','November','December'];
			var dim	= [31,0,31,30,31,30,31,31,30,31,30,31];

			var oD 	= new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
			oD.od	= oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

			var todaydate=new Date() //DD added
			var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

			dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))? 29 : 28;

			// sort out the next and prev months

			nlyear				= y;
			plyear				= y;

			nlmonth 			= m + 1;
			if (nlmonth == 13)
			{
				nlyear	= y + 1;
				nlmonth = 1;
			}

			plmonth 			= m - 1;
			if (plmonth == 0)
			{
				plyear	= y - 1;
				plmonth = 12;
			}

			// swing left and right
			//var pl	= '<a href="JAVASCRIPT: var bc = new basiccalendar();bc.swingLeft(\'' + applyTo + '\');bc.swingStart=300;bc.type=\'' + this.type + '\';bc.' + funcname + '(' + plmonth + ', ' + plyear + ', \'' + cM + '\', \'calMonth\', \'calDaysofweek\', \'calDays\', 1, \'' + applyTo + '\')">< Previous</a> ';
			//var nl	= ' <a href="JAVASCRIPT: var bc = new basiccalendar();bc.swingRight(\'' + applyTo + '\');bc.swingStart=-300;bc.type=\'' + this.type + '\';bc.' + funcname + '(' + nlmonth + ', ' + nlyear + ', \'' + cM + '\', \'calMonth\', \'calDaysofweek\', \'calDays\', 1, \'' + applyTo + '\')">Next ></a>';

			this.swingStart = 0;
			var pl	= '<a href="JAVASCRIPT: var bc = new basiccalendar();bc.selectedBG=\'' + this.selectedBG + '\';bc.fadeOut(\'' + applyTo + '\');bc.swingStart=300;bc.type=\'' + this.type + '\';bc.' + funcname + '(' + plmonth + ', ' + plyear + ', \'' + cM + '\', \'calMonth\', \'calDaysofweek\', \'calDays\', 1, \'' + applyTo + '\')">< Previous</a> ';
			var nl	= ' <a href="JAVASCRIPT: var bc = new basiccalendar();bc.selectedBG=\'' + this.selectedBG + '\';bc.fadeOut(\'' + applyTo + '\');bc.swingStart=-300;bc.type=\'' + this.type + '\';bc.' + funcname + '(' + nlmonth + ', ' + nlyear + ', \'' + cM + '\', \'calMonth\', \'calDaysofweek\', \'calDays\', 1, \'' + applyTo + '\')">Next ></a>';


			//if (plyear < curyear || (curyear == plyear && curmonth < plmonth))
			//	pl = '';

			var t	= '<div class="' + cM + '"><table class="' + cM + '" width="100%" cols="8" cellpadding="0" border="0" cellspacing="0"><tr align="center">';
			t		+= '<td style="background-color: white;" colspan="9" align="center" class="' + cH + '">';
			t		+= '<table cellpadding=\'0\' cellspacing=\'0\' width=\'100%\'><tr><td style=\'text-align:left;\' width=\'25%;\'> ' + pl + '</td><td width=\'50%;\' style=\'text-align:center;font-weight:bold;font-size:1.2em;\'>' + mn[m-1] + ' ' + y + '</td>';
			t		+= '<td width=\'25%;\' style=\'text-align:right;\'> ' + nl + ' </td></tr></table></td></tr></table>';

			//t		+= '<div style="overflow:hidden;position:relative;height: 175px;"><div id="swing-' + applyTo + '" style="left: ' + this.swingStart + 'px;">';
			t		+= '<table style="margin-top: -1px;" id="table-' + applyTo + '" class="' + cM + '" width="100%" cols="8" cellpadding="0" border="0" cellspacing="0"><tr align="center">';
			t		+= '<tr align="center">';

			t+='<td class="' + cDW + '">&nbsp;</td>';
			t+='<td style="border-bottom: 1px solid #C6CFCF;" class="' + cDW + '">Sun</td>';
			t+='<td style="border-bottom: 1px solid #C6CFCF;" class="' + cDW + '">Mon</td>';
			t+='<td style="border-bottom: 1px solid #C6CFCF;" class="' + cDW + '">Tue</td>';
			t+='<td style="border-bottom: 1px solid #C6CFCF;" class="' + cDW + '">Wed</td>';
			t+='<td style="border-bottom: 1px solid #C6CFCF;" class="' + cDW + '">Thur</td>';
			t+='<td style="border-bottom: 1px solid #C6CFCF;" class="' + cDW + '">Fri</td>';
			t+='<td style="border-bottom: 1px solid #C6CFCF;" class="' + cDW + '">Sat</td>';
			t+='<td class="' + cDW + '">&nbsp;</td>';

			t += '</tr><tr align="center"><td class="' + cDW + '" align="right"></td>';
			for(i = 1; i <= 42; i++)
			{
				var x = ((i-oD.od >= 0) && (i-oD.od < dim[m-1])) ? i-oD.od + 1 : '&nbsp;';
				var xno = new String(x);

				if (x == scanfortoday) //DD added
					x = '<span id="today">' + x + '</span>' //DD added

				if (x == '&nbsp;')
				{
					t += '<td style="border-left: 1px solid #C6CFCF;" class="'+cD+'" valign="top" align="right">&nbsp;' + x + '</td>';
				}
				else
				{
					m  	= new String(m);
					y  	= new String(y);

					if (m.length == 1)
						m = "0" + m;

					if (xno.length == 1)
						xno = "0" + xno;

					/*********************************************************/
					// check to see if its been selected
					tid = y + '-' + m + '-' + xno + '-' + this.type
					addyStyle = "";
					if (bcSel[tid] == 1 && this.type == "parkBooking")
							addyStyle = "background: " + this.selectedBG + ";color:white;";
					else if (bcSel[tid] == 1 && this.type == "journeyBooking")
							addyStyle = "background: " + this.journeySelectedBG + ";color:white;";
					else if (this.type == "paypark")
					{
						if (typeof ppSel[y + '-' + m] != "undefined")
					 	{
							if (typeof ppSel[y + '-' + m][xno] != "undefined")
								addyStyle = "background: " + this.selectedBG + ";color:white;cursor:pointer;";
					 	}
					}
					else if (this.type == "parksold")
					{
						if (typeof psSel[y + '-' + m] != "undefined")
					 	{
							if (typeof psSel[y + '-' + m][xno] != "undefined")
								addyStyle = "background: " + this.soldSelectedBG + ";color:white;cursor:pointer;";
					 	}
					}

					// god almighty, i wish i had thrown this away and started again
					noOnClick = false;
					if (this.type == "parkBooking" && bcBSel[tid.replace(/-parkBooking/, "")] == 1)
					{
						noOnClick = true;
						addyStyle = "background: #2f2f2f;color:white;";
					}


					if (this.type == "parkBooking" || this.type == "paypark")
					{

						if (GLOBALAvailable == "Weekends")
						{
							var nd = new Date(y + '/' + m + '/' + xno);
							var cd = nd.getDay();

							// check for weekend
							if (cd != 0 && cd != 6)
							{
								noOnClick = true;
								addyStyle = "background: #2f2f2f;color:white;";
							}
						}
						else if (GLOBALAvailable == "Weekdays")
						{
							var nd = new Date(y + '/' + m + '/' + xno);
							var cd = nd.getDay();

							// check for weekend
							if (cd == 0 || cd == 6)
							{
								noOnClick = true;
								addyStyle = "background: #2f2f2f;color:white;";
							}
						}
					}

					if (this.type == "parkBooking" && blackSel[tid] == 1)
					{
						noOnClick = true;
						addyStyle = "background: #2f2f2f;color:white;";
					}

					if (GLOBALNoOnClick && this.type != "parksold" && this.type != "paypark")
						noOnClick = true;

					if (!noOnClick)
						t += '<td id="' + tid + '" onclick="' + onClick + '" style="cursor:pointer;' + addyStyle + 'border-left: 1px solid #C6CFCF;" class="'+cD+'" valign="top" align="right">' + x + '</td>';
					else
						t += '<td id="' + tid + '" style="' + addyStyle + 'border-left: 1px solid #C6CFCF;" class="'+cD+'" valign="top" align="right">' + x + '</td>';


				}

				if (((i)%7 == 0) && (i < 36))
				{
					t += '<td style="background-color: white;border-left: 1px solid #C6CFCF;">&nbsp;</td></tr>';
					t += '</tr><tr align="center">';

					t += '<td class="' + cDW + '" align="right"></td>';

				}

			}

			t+= '<td style="background-color: white;border-left: 1px solid #C6CFCF;">&nbsp;</td></tr>';
			t+= '</tr>';

			if (this.type == "parkBooking")
			{
					t += '<tr><td style="background: white;padding:5px;text-align:right;" colspan="9">';
					
					if (GLOBALNoTotal !== true)
					{
						if (monthlyPrice > 0)
							t += '<a href="JAVASCRIPT: var bc = new basiccalendar();bc.itemId = \'' + this.itemId + '\';bc.type = \'' + this.type + '\';bc.selectMonth(' + y + ', ' + m + ');void(0);">Book whole month</a></td></tr>';
					}	
					else
					{	
							t += '<a href="JAVASCRIPT: GLOBALNoTotal = true;var bc = new basiccalendar();bc.selectedBG = \'' + this.selectedBG + '\';bc.itemId = \'' + this.itemId + '\';bc.type = \'' + this.type + '\';bc.selectMonth(' + y + ', ' + m + ');void(0);">Select whole month</a></td></tr>';
					}	
			}
			else	t += '<tr><td style="background: white;" colspan="9">&nbsp;</td></tr>';

			t+='</table>';
			//t+='</div></div>';
			t+='</div>';

			if (this.swingStart == 300)
				this.swingRight(applyTo);
			else if (this.swingStart == -300)
				this.swingLeft(applyTo);

			// apply the text to the apply to
			document.getElementById(applyTo).innerHTML = t;
			this.fadeIn(applyTo);

			// FOR SWING LEFT AND RIGHT
			//if (this.swingStart == 300)
			//	this.swingLeft(applyTo);
			//else if (this.swingStart == -300)
			//	this.swingRight(applyTo);

		}

		/**
		* default handler for booking a space
		*/
		this.handler = function (id)
		{

			// get some current dates, may be wise when i make
			// this code good to put these in to globals
			var todaydate	= new Date();
			var curmonth	= todaydate.getMonth() + 1;
			var curyear		= todaydate.getFullYear();
			var curday		= todaydate.getDate();

			var idars 	= new String(id.id);
			var idar 	= idars.split('-');

			// check the selected date
			if (curyear > idar[0])
				return;
			else if (curyear == idar[0])
			{
				if (curmonth > idar[1])
					return;
				else if (curmonth == idar[1])
				{
					if (curday > idar[2])
						return;
				}
			}


			// sort out some bits
			var dim	= [31,0,31,30,31,30,31,31,30,31,30,31];

			var oD 	= new Date(idar[0], idar[1], 1); //DD replaced line to fix date bug when current day is 31st
			oD.od	= oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

			var todaydate=new Date() //DD added
			var scanfortoday=(idar[0]==todaydate.getFullYear() && idar[1]==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

			dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))? 29 : 28;

			itemE 		= document.getElementById(this.itemId);

			// sort out if its been clicked, if we unselect it we change the color back and remove the booking item
			if (bcSel[idars] == 1)
			{	

				clearDom = document.getElementById("bitem-" + idars);
				if (clearDom != null)
				{	
					domName = clearDom.getAttribute("name");
					if (domName != null && domName.match(/daily/))
					{	
						bcSel[idars] = 0;
						id.style.backgroundColor 	= "#efefef";
						id.style.color 				= "#676767";
						removeItem("bitem-" + idars);	
						removeFromTotal(dailyPrice);
						return;	
					}	
				}	
				
				if (GLOBALNoTotal !== true)
				{	
					alert("Please use the clear link to remove weekly or monthly items");
					return;
				}	
				else
				{	
					bcSel[idars] = 0;
					id.style.backgroundColor 	= "#efefef";
					id.style.color 				= "#676767";
					removeItem("bitem-" + idars);
					return;
				}	
				
			}
			else if (bcSel[idars] == 0 || typeof bcSel[idars] == "undefined")
			{

				// check for a week highlight
				if ((this.weekBooking == false && this.monthBooking == false && GLOBALNoTotal == true) || (this.weekBooking == false && this.monthBooking == false && dailyPrice != '' && this.type == "parkBooking"))
				{

					if (typeof dailyPrice == "undefined")
						dailyPrice = 0;

					// add it to the list of selected items and create a date item
					bcSel[idars] = 1;
					id.style.backgroundColor = this.selectedBG;
					id.style.color			= "white";

					newElement 	= document.createElement("div");
					newElement.setAttribute("id", "bitem-" + idars);
					newElement.setAttribute("name", "daily-" + idars);

					if (this.type == "journeyBooking")
							var newElementHTML 		= "Available: " + idar[2] + "/" + idar[1] + "/" + idar[0] + " " + idar[3] + " ";
					else if ( GLOBALNoTotal == true)
							var newElementHTML 		= "Unavailable: " + idar[2] + "/" + idar[1] + "/" + idar[0] + " ";
					else	var newElementHTML 		= "Booking: " + idar[2] + "/" + idar[1] + "/" + idar[0] + " ";
					
					var remTotal = "";
					if (dailyPrice > 0)
						remTotal = "removeFromTotal(" + dailyPrice + ");";

					//				newElementHTML			+= "<a id=\"" + idars + "-ln\" href=\"JAVASCRIPT: " + this.onClick + "bcSel['" + idars;
					newElementHTML			+= "<a id=\"clear-" + idars + "\" href=\"JAVASCRIPT: removeItem('bitem-" + idars + "');bcSel['" + idars;
					newElementHTML			+= "'] = 0;setColours(new Array('" + idars + "'));" + remTotal + "void(0);\">< clear ></a>";
					newElementHTML			+= "<input type='hidden' name='bookingItem[]' value='" + idars + "' />";
					newElement.innerHTML 	= newElementHTML;

					itemE.appendChild(newElement);

					if (GLOBALNoTotal != true)
					{
						addToTotal(dailyPrice);
						calculateBetterPrice();
						return;
					}
				}
				else if (this.weekBooking == true || (this.monthBooking == false && this.type == "parkBooking" && GLOBALNoTotal != true && (dailyPrice == "" && (weeklyPrice != "" || weeklyPriceWeekend != ""))))
				{

					var dayCount = "";
					var lastDay	 = "";
					var toDay 	 = parseFloat(idar[2]) + 6;
					var changeDays = [];

					if (toDay > dim[idar[1]-1])
					{
						toYear 	= idar[0];
						toDay 	= toDay - dim[parseFloat(idar[1])];

						var toMonth = parseFloat(idar[1]) + 1;
						if (toMonth == 13)
						{
							toMonth = 1;
							toYear	= idar[0] + 1;
						}

						if (toMonth < 10)
							toMonth = "0" + toMonth;

						for (var k = parseFloat(idar[2]); k <= dim[idar[1]-1]; k ++)
						{

							if (k < 10)
									j = "0" + k;
							else 	j = k;

							// check its avail and then add to the list
							if (typeof blackSel[idar[0] + '-' + idar[1] + '-' + j + '-parkBooking']	== "undefined")
							{
								if (GLOBALAvailable == "Weekdays" && !this.isWeekend(idar[0] + '/' + idar[1] + '/' + j))
								{
									dayCount ++;
									lastDay 	= j + "/" + idar[1] + "/" + idar[0];
									changeDays.push(idar[0] + "-" + idar[1] + "-" + j + "-parkBooking");
								}
								else if (GLOBALAvailable == "Weekends" && this.isWeekend(idar[0] + '/' + idar[1] + '/' + j))
								{
									dayCount ++;
									lastDay = j + "/" + idar[1] + "/" + idar[0];
									changeDays.push(idar[0] + "-" + idar[1] + "-" + j + "-parkBooking");
								}
								else if (GLOBALAvailable == "" || GLOBALAvailable == "Any time")
								{
									dayCount ++;
									lastDay = j + "/" + idar[1] + "/" + idar[0];
									changeDays.push(idar[0] + "-" + idar[1] + "-" + j + "-parkBooking");
								}
							}
						}

						for (var k = 1; k <= (toDay - 1); k ++)
						{

							if (k < 10)
									j = "0" + k;
							else 	j = k;

							// check its avail and then add to the list
							if (typeof blackSel[toYear + '-' + toMonth + '-' + j + '-parkBooking']	== "undefined")
							{
								if (GLOBALAvailable == "Weekdays" && !this.isWeekend(toYear + '/' + toMonth + '/' + j))
								{
									dayCount ++;
									lastDay 	= j + "/" + toMonth + "/" + toYear;
									changeDays.push(toYear + "-" + toMonth + "-" + j + "-parkBooking");
								}
								else if (GLOBALAvailable == "Weekends" && this.isWeekend(toYear + '/' + toMonth + '/' + j))
								{
									dayCount ++;
									lastDay = j + "/" + toMonth + "/" + toYear;
									changeDays.push(toYear + "-" + toMonth + "-" + j + "-parkBooking");
								}
								else if (GLOBALAvailable == "" || GLOBALAvailable == "Any time")
								{
									dayCount ++;
									lastDay = j + "/" + toMonth + "/" + toYear;
									changeDays.push(toYear + "-" + toMonth + "-" + j + "-parkBooking");
								}
							}
						}

					}else{

						for (var k = parseFloat(idar[2]); k <= toDay; k ++)
						{
							if (k < 10)
									j = "0" + k;
							else 	j = k;

							// check its avail and then add to the list
							if (typeof blackSel[idar[0] + '-' + idar[1] + '-' + j + '-parkBooking']	== "undefined")
							{
								
								if (GLOBALAvailable == "Weekdays" && !this.isWeekend(idar[0] + '/' + idar[1] + '/' + j))
								{
									dayCount ++;
									lastDay 	= j + "/" + idar[1] + "/" + idar[0];
									changeDays.push(idar[0] + "-" + idar[1] + "-" + j + "-parkBooking");
								}
								else if (GLOBALAvailable == "Weekends" && this.isWeekend(idar[0] + '/' + idar[1] + '/' + j))
								{
									dayCount ++;
									lastDay = j + "/" + idar[1] + "/" + idar[0];
									changeDays.push(idar[0] + "-" + idar[1] + "-" + j + "-parkBooking");
								}
								else if (GLOBALAvailable == "" || GLOBALAvailable == "Any time")
								{
									lastDay = j + "/" + idar[1] + "/" + idar[0];
									changeDays.push(idar[0] + "-" + idar[1] + "-" + j + "-parkBooking");
									dayCount ++;
								}
							}
						}

					}

					if ((dayCount < 5 && GLOBALAvailable == "Weekdays") || (dayCount < 2 && GLOBALAvailable == "Weekends") || (dayCount < 7 && GLOBALAvailable == "Any time") || (dayCount < 7 && GLOBALAvailable == ""))
					{
						fret = confirm("Some of your selected dates are unvailable, do you want to continue?");
						if (!fret)
							return false;
					}

					// add it to the list of selected items and create a date item
					bcSel[idars] = 1;
					id.style.backgroundColor = this.selectedBG;
					id.style.color			= "white";

					newElement 	= document.createElement("div");
					newElement.setAttribute("id", "bitem-" + idars);

					var weekName = "";
					for (var i = 0; i < changeDays.length; i ++)
						lastIdars = changeDays[i];
						
					weekName = idars + "|" + lastIdars;

					var removeItemStr = "'" + idars + "'";
					var newElementsBookItems = "";
					for (var i = 0; i < changeDays.length; i ++)
					{

						bcSel[new String(changeDays[i])] = 1;
						lastIdars = changeDays[i];

						thisDom = document.getElementById(changeDays[i]);
						if (thisDom != null)
						{
							thisDom.style.backgroundColor = this.selectedBG;
							thisDom.style.color			= "white";
						}

						removeItemStr += ",'" + changeDays[i] + "'";

						newElementsBookItems += "<input type='hidden' name='bookingItemWeekIdividual[" + weekName + "][]' value='" + changeDays[i] + "' />";
					}

					var thisPrice = weeklyPrice;
					if (GLOBALAvailable == "Weekdays" || GLOBALAvailable == "Weekends")
					{
						thisPrice = weeklyPrice;
					}
					else
					{
						if (dayCount > 5)
							thisPrice = weeklyPriceWeekend;
					}

					newElement 	= document.createElement("div");
					newElement.setAttribute("id", "bitem-" + idars);
					newElement.setAttribute("name", "weekly-" + idars);

					var newElementHTML 		= "Booking: " + idar[2] + "/" + idar[1] + "/" + idar[0] + " to " + lastDay;

					newElementHTML			+= " <a id=\"clear-" + idars + "\" href=\"JAVASCRIPT: removeItem('bitem-" + idars + "');bcSel['" + idars;
					newElementHTML			+= "'] = 0;setColours(new Array(" + removeItemStr + "));removeFromTotal(" + thisPrice + ");void(0);\">< clear ></a>";
					newElementHTML			+= "<input type='hidden' name='bookingItemWeek[]' value='" + weekName + "' />";
					//newElementHTML			+= "<input type='hidden' name='bookingItemWeekIdividual[" + weekName + "][]' value='" + idars + "' />";
					newElementHTML			+= newElementsBookItems;

					newElement.innerHTML 	= newElementHTML;

					itemE.appendChild(newElement);

					addToTotal(thisPrice);
					calculateBetterPrice();

					return;

				}
				else if (this.monthBooking == true || (this.type == "parkBooking" && GLOBALNoTotal != true && dailyPrice == "" && weeklyPrice == "" && weeklyPriceWeekend == "" && (monthlyPrice != "" || monthlyPriceWeekend != "")))
				{

					var dayCount = "";
					var lastDay	 = "";
					var toDay 	 = idar[2];
					var changeDays = [];

					toYear 	= idar[0];

					var toMonth = parseFloat(idar[1]) + 1;
					if (toMonth == 13)
					{
						toMonth = 1;
						toYear	= idar[0] + 1;
					}

					if (toMonth < 10)
						toMonth = "0" + toMonth;

					someUnavail = false;

					for (var k = parseFloat(idar[2]); k <= dim[idar[1]-1]; k ++)
					{

						if (k < 10)
								j = "0" + k;
						else 	j = k;

						// check its avail and then add to the list
						if (typeof blackSel[idar[0] + '-' + idar[1] + '-' + j + '-parkBooking']	== "undefined")
						{
							if (GLOBALAvailable == "Weekdays" && !this.isWeekend(idar[0] + '/' + idar[1] + '/' + j))
							{
								dayCount ++;
								lastDay 	= j + "/" + idar[1] + "/" + idar[0];
								changeDays.push(idar[0] + "-" + idar[1] + "-" + j + "-parkBooking");
							}
							else if (GLOBALAvailable == "Weekends" && this.isWeekend(idar[0] + '/' + idar[1] + '/' + j))
							{
								dayCount ++;
								lastDay = j + "/" + idar[1] + "/" + idar[0];
								changeDays.push(idar[0] + "-" + idar[1] + "-" + j + "-parkBooking");
							}
							else if (GLOBALAvailable == "" || GLOBALAvailable == "Any time")
							{
								dayCount ++;
								lastDay = j + "/" + idar[1] + "/" + idar[0];
								changeDays.push(idar[0] + "-" + idar[1] + "-" + j + "-parkBooking");
							}
						}
						else
						{
							someUnavail = true;
						}

					}

					for (var k = 1; k <= (toDay - 1); k ++)
					{

						if (k < 10)
								j = "0" + k;
						else 	j = k;

						// check its avail and then add to the list
						if (typeof blackSel[toYear + '-' + toMonth + '-' + j + '-parkBooking']	== "undefined")
						{
							if (GLOBALAvailable == "Weekdays" && !this.isWeekend(toYear + '/' + toMonth + '/' + j))
							{
								dayCount ++;
								lastDay 	= j + "/" + toMonth + "/" + toYear;
								changeDays.push(toYear + "-" + toMonth + "-" + j + "-parkBooking");
							}
							else if (GLOBALAvailable == "Weekends" && this.isWeekend(toYear + '/' + toMonth + '/' + j))
							{
								dayCount ++;
								lastDay = j + "/" + toMonth + "/" + toYear;
								changeDays.push(toYear + "-" + toMonth + "-" + j + "-parkBooking");
							}
							else if (GLOBALAvailable == "" || GLOBALAvailable == "Any time")
							{
								dayCount ++;
								lastDay = j + "/" + toMonth + "/" + toYear;
								changeDays.push(toYear + "-" + toMonth + "-" + j + "-parkBooking");
							}

						}
						else
						{
							someUnavail = true;
						}
					}

					if (someUnavail)
					{
						fret = confirm("Some of your selected dates are unvailable, do you want to continue?");
						if (!fret)
							return false;
					}

					// add it to the list of selected items and create a date item
					bcSel[idars] = 1;
					id.style.backgroundColor = this.selectedBG;
					id.style.color			= "white";

					newElement 	= document.createElement("div");
					newElement.setAttribute("id", "bitem-" + idars);
					
					var monthName = "";
					for (var i = 0; i < changeDays.length; i ++)
						lastIdars = changeDays[i];
						
					monthName = idars + "|" + lastIdars;
					
					var removeItemStr = "'" + idars + "'";
					var newElementsBookItems = "";				
					for (var i = 0; i < changeDays.length; i ++)
					{

						bcSel[new String(changeDays[i])] = 1;
						lastIdars = changeDays[i];

						thisDom = document.getElementById(changeDays[i]);
						if (thisDom != null)
						{
							thisDom.style.backgroundColor = this.selectedBG;
							thisDom.style.color			= "white";
						}

						removeItemStr += ",'" + changeDays[i] + "'";

						newElementsBookItems += "<input type='hidden' name='bookingItemMonthIdividual[" + monthName + "][]' value='" + changeDays[i] + "' />";
					}

					if (GLOBALAvailable == "Weekdays" || GLOBALAvailable == "Weekends")
							thisPrice = monthlyPrice;
					else	thisPrice = monthlyPriceWeekend;

					newElement 	= document.createElement("div");
					newElement.setAttribute("id", "bitem-" + idars);
					newElement.setAttribute("name", "month-" + idars);

					var newElementHTML 		= "Booking: " + idar[2] + "/" + idar[1] + "/" + idar[0] + " to " + lastDay;
					newElementHTML			+= " <a id=\"clear-" + idars + "\" href=\"JAVASCRIPT: removeItem('bitem-" + idars + "');bcSel['" + idars;
					newElementHTML			+= "'] = 0;setColours(new Array(" + removeItemStr + "));removeFromTotal(" + thisPrice + ");void(0);\">< clear ></a>";
					
					newElementHTML			+= "<input type='hidden' name='bookingItemMonth[]' value='" + monthName + "' />";
					//newElementHTML			+= "<input type='hidden' name='bookingItemMonthIdividual[" + monthName + "][]' value='" + idars + "' />";
					newElementHTML			+= newElementsBookItems;
					
					newElement.innerHTML 	= newElementHTML;

					itemE.appendChild(newElement);

					addToTotal(thisPrice);
					calculateBetterPrice();

					return;

				}

			}

			if (this.type == "journeyBooking" || GLOBALNoTotal == true)
				return;

			removeItem('bookingitemsTotal');

			//weekendAMPrice, weekendPMPrice, AMPrice, PMPrice
			// calculate the total
			var total = 0;
			for (var k in bcSel)
			{
				if (bcSel[k] == 1)
				{
					var idar = k.split('-');
					var nd = new Date(idar[0] + '/' + idar[1] + '/' + idar[2]);
					var cd = nd.getDay();
					var idar = k.split('-');


					//total = total;

				}
			}

			// calc the total
			var totalStr 		= total.toFixed(2);
			totalNewElement 	= document.createElement("div");
			totalNewElement.setAttribute("id", "bookingitemsTotal");
			totalNewElement.innerHTML 	= "<br />Total: &pound;" + totalStr;

			itemE.appendChild(totalNewElement);

		}

		/**
		* isWeekend
		*/
		this.isWeekend = function (dateToCheck)
		{

			var nd = new Date(dateToCheck);
			var cd = nd.getDay();

			// check for weekend
			if (cd == 0 || cd == 6)
					return true;
			else	return false;

		}

		/**
		* selectMonth
		*
		* highlights a month
		*/
		this.selectMonth = function (y, m)
		{
			
	
			
			var mn	= ['January','February','March','April','May','June','July','August','September','October','November','December'];
			var dim	= [31,0,31,30,31,30,31,31,30,31,30,31];

			var oD 	= new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
			oD.od	= oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

			var todaydate=new Date() //DD added
			var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

			dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))? 29 : 28;

			if (m < 10)
					z = "0" + m;
			else 	z = m;

			if (typeof monthlyPrice == "undefined")
				monthlyPrice = 0;
				
			if (monthlyPrice > 0 && GLOBALNoTotal !== true)
			{	
				monthlyDom 			= document.getElementById (y + "-" + z + "-01-parkBooking");
				this.monthBooking 	= true;
				this.handler(monthlyDom);
				return;
			}			

			// loop through and press the days
			for (i = 1; i <= dim[m-1]; i ++)
			{
				if (i < 10)
						x = "0" + i;
				else 	x = i;

				var getId = y + '-' + z + '-' + x + '-' + this.type;

				if (GLOBALAvailable == "Weekends")
				{
					var nd = new Date(y + '/' + z + '/' + x);
					var cd = nd.getDay();

					// check for weekend
					if (cd == 0 || cd == 6)
					{
						var domid = document.getElementById(getId);
						this.handler (domid);
					}
				}
				else if (GLOBALAvailable == "Weekdays")
				{
					var nd = new Date(y + '/' + z + '/' + x);
					var cd = nd.getDay();

					// check for weekend
					if (cd != 0 && cd != 6)
					{
						var domid = document.getElementById(getId);
						this.handler (domid);
					}
				}
				else
				{
					var domid = document.getElementById(getId);
					this.handler (domid);
				}
			}

		}


		/**
		* forwardParkingspace - forwards to the parking space for this cell
		*/
		this.forwardParkingspace = function (id)
		{

			var idars = new String(id.id);
			var idar = idars.split('-');

			if (typeof ppSel[idar[0]+'-'+idar[1]] != "undefined")
			{
				if (typeof ppSel[idar[0]+'-'+idar[1]][idar[2]] != "undefined")
				{
					var parkingspaceId 	= ppSel[idar[0]+'-'+idar[1]][idar[2]];
					window.location		= "viewParkingSpace.php?parkingspaceId=" + parkingspaceId;
				}
			}

		}

		/**
		* forwardEditParkingspace - forwards to the edit parking space for this cell
		*/
		this.forwardEditParkingspace = function (id)
		{
			var idars = new String(id.id);
			var idar = idars.split('-');

			if (typeof psSel[idar[0]+'-'+idar[1]] != "undefined")
			{
				if (typeof psSel[idar[0]+'-'+idar[1]][idar[2]] != "undefined")
				{
					var parkingspaceId 	= psSel[idar[0]+'-'+idar[1]][idar[2]];
					window.location		= "/account/edit-space/?parkingspaceId=" + parkingspaceId;
				}
			}
		}

 	}

