<!--
var SW_yearShowing = "";
var SW_monthShowing = "";
var SW_calStartDate = "";
var SW_bookingXML = "";
var SW_eventXML = "";

function jg_toggle_layer_vis(layerid,toggle){
	var layertotoggle =document.getElementById(layerid).style;//load the layers style into var
	layertotoggle.visibility = toggle;
}

function jg_log_me_in(){
	document.getElementById("userId").value = hex_md5(document.getElementById("userId").value);
	document.getElementById("userPwd").value = hex_md5(document.getElementById("userPwd").value);
	document.getElementById("logInForm").submit();
}

function jg_change_class(change_what,to_class){
	id_to_change = document.getElementById(change_what);
	id_to_change.className = to_class;
}

function jg_modify_event_handler(idToHandle,eventType,funcToCall,addOrRemove) {
	// check if admin is logged in - this varable assigned from php in calendar.php
	if(loginHash=="eeeb49ab8d237aa3f68ba3d5f6a5670a" || idToHandle.id.search("evIcon_")!=-1 || idToHandle.id == "monthSel" || idToHandle.id == "yearSel") // only apply events if logged in, for event icon clicks, monselect or year select
	{
		if (addOrRemove == "add") // requested to add event handler
		{
			if (idToHandle.addEventListener) //w3c event handling
			{
				idToHandle.addEventListener(eventType, funcToCall,false);
			}
			else if (idToHandle.attachEvent) //MSIE event handling
			{
				idToHandle.attachEvent("on" + eventType, funcToCall);
			}
			else // traditional model
			{
				idToHandle['on' + eventType] = funcToCall;
			}
		}
		if (addOrRemove == "remove") // requested to add event handler
		{
			if (idToHandle.addEventListener) //w3c event handling
			{
				idToHandle.removeEventListener(eventType, funcToCall,false);
			}
			else if (idToHandle.attachEvent) //MSIE event handling
			{
				idToHandle.detachEvent("on" + eventType, funcToCall);
			}
			else // traditional model
			{
				idToHandle['on' + eventType] = null;
			}
		}
	}
}

function jg_ret_month(zeroBasedMonth){
	switch (zeroBasedMonth) 
	{
		case 0:
			return "Jan";
			break;
		case 1:
            return "Feb";
			break;
		case 2:
            return "Mar";
			break;
		case 3:
            return "Apr";
			break;
		case 4:
            return "May";
			break;
		case 5:
            return "Jun";
			break;
		case 6:
            return "Jul";
			break;
		case 7:
            return "Aug";
			break;
		case 8:
            return "Sep";
			break;
		case 9:
            return "Oct";
			break;
		case 10:
            return "Nov";
			break;
		case 11:
            return "Dec";
			break;
	}
}
function jg_call_cal(){
	//check what month/year to load as default (vars set in php page)
	if(requestedLoadMonth!="none" && requestedLoadYear!="none")
	{
		SW_yearShowing = Number(requestedLoadYear);
		SW_monthShowing = Number(requestedLoadMonth);
	}
	else
	{
		//default starting month/year
		var today = new Date();
		var dispYear = today.getFullYear();
		var dispMon = today.getMonth(); // zero based
		
		// set the global vars to this month/year
		SW_yearShowing = dispYear;
		SW_monthShowing = dispMon;
	}
	
	// call init function with this starting point
	jg_init_cal();
}

function jg_init_cal(){	
	// set headers
	var dateShowing = new Date();
	dateShowing.setFullYear(SW_yearShowing, SW_monthShowing, 1, 0, 0, 0, 0);
	var lastMonth = new Date();
	lastMonth.setFullYear(dateShowing.getFullYear(), (dateShowing.getMonth()-1), dateShowing.getDate(), 0, 0, 0, 0);
	var prevMonth = jg_ret_month(lastMonth.getMonth());
	var comingMonth = new Date();
	comingMonth.setFullYear(dateShowing.getFullYear(), (dateShowing.getMonth()+1), dateShowing.getDate(), 0, 0, 0, 0);
	var nextMonth = jg_ret_month(comingMonth.getMonth());
	document.getElementById("prevMonth").innerHTML = prevMonth;

	// create month/year drop boxes
	monthYearDiv = document.getElementById("currentMonth");
	monthSelect = document.createElement('select'); // create empty new select box
	monthSelect.setAttribute('id','monthSel'); // set id
	jg_modify_event_handler(monthSelect,'change',jg_nav_month,'add'); // assign onchange event handler
	monthYearDiv.appendChild(monthSelect); //append
	for(x=0;x<=11;x++)
	{
		monthOption = document.createElement('option');
		monthOption.setAttribute('name','monthOpt-'+x); // set name
		monthOption.setAttribute('value',x); // set value
		monthOption.innerHTML = jg_ret_month(x);
		if(x==dateShowing.getMonth())
		{
			monthOption.setAttribute('selected','selected'); // set selected if current month showing
		}
		monthSelect.appendChild(monthOption); //append
	}
	//just insert 3 years before and after current year	
	yearSelect = document.createElement('select'); // create empty new select box
	yearSelect.setAttribute('id','yearSel'); // set id	
	jg_modify_event_handler(yearSelect,'change',jg_nav_month,'add'); // assign onchange event handler
	monthYearDiv.appendChild(yearSelect); //append
	for((y=Number(dateShowing.getFullYear())-3);(y<=Number(dateShowing.getFullYear())+3);y++)
	{
		yearOption = document.createElement('option');
		yearOption.setAttribute('name','yearOpt-'+y); // set name
		yearOption.setAttribute('value',y); // set value
		yearOption.innerHTML = y;
		if(y==dateShowing.getFullYear())
		{
			yearOption.setAttribute('selected','selected'); // set selected if current year showing
		}
		yearSelect.appendChild(yearOption); //append
	}
	
	document.getElementById("nextMonth").innerHTML = nextMonth;
	
	// find first day of month
	var firstOfMontDate = new Date(SW_yearShowing, SW_monthShowing, 1, 0, 0, 0, 0);
	var firstDateOffset = firstOfMontDate.getDay()-1;
	if(firstDateOffset<0){firstDateOffset=6};// if first day of month is sunday will normally result in -1 as getDay is zero based starting sunday
	//create new date minus the offset to find the first date the calendar will show
	SW_calStartDate = new Date(SW_yearShowing, SW_monthShowing, 1, 0, 0, 0, 0);
	SW_calStartDate.setFullYear(SW_yearShowing, SW_monthShowing, (1-firstDateOffset), 0, 0, 0, 0);
	
	jg_gen_cal();
}

function jg_gen_cal() {
	var iterateDate = SW_calStartDate;
	// loop through each row
	for(z=1;z<=6;z++)
	{
		var rowContainer = document.getElementById("wk"+z);
		// loop through each day of the row
		for(x=1;x<=7;x++)
		{
			//create day container
			newDayContainer = document.createElement('div'); // create empty new div
			newDayContainer.setAttribute('id','dayCont_'+iterateDate.getDate()+"_"+(iterateDate.getMonth()+1)); // set id
			newDayContainer.className = 'dayContainer'; // assign active class
			rowContainer.appendChild(newDayContainer); //append
			//create title containers
			newTitleContainer = document.createElement('div'); // create empty new div
			newTitleContainer.setAttribute('id','titleCont~'+iterateDate.getDate()+"_"+(iterateDate.getMonth()+1)+"_"+iterateDate.getFullYear()); // set id
			if(iterateDate.getMonth()==SW_monthShowing)
			{
				newTitleContainer.className = 'titleContainer'; // assign active class
				jg_modify_event_handler(newTitleContainer,'click',jg_click_day_title,'add'); // assign click event handler
			}
			else
			{
				newTitleContainer.className = 'titleContainerNotActive'; // assign not active class
			}
			newTitleContainer.innerHTML = iterateDate.getDate();
			newDayContainer.appendChild(newTitleContainer); //append
			//create room rows
			for(y=1;y<=6;y++)
			{
				newRoomRow = document.createElement('div'); // create empty new div
				newRoomRow.setAttribute('id','room-'+ y + '_' +iterateDate.getDate()+"_"+(iterateDate.getMonth()+1)+"_"+iterateDate.getFullYear()); // set id
				if(iterateDate.getMonth()==SW_monthShowing)
				{
					newRoomRow.className = 'vacantRoom'; // assign active class
					jg_modify_event_handler(newRoomRow,'click',jg_click_vacant,'add'); // assign click event handler
				}
				else
				{
					newRoomRow.className = 'vacantRoomNotActive'; // assign not active class
				}
				newDayContainer.appendChild(newRoomRow); //append
			}
			
			iterateDate.setFullYear(iterateDate.getFullYear(), iterateDate.getMonth(), (iterateDate.getDate()+1), 0, 0, 0, 0);
		}
	}
	
	//call ajax func to grab data - assign returned data to var
	jg_ajax_get('scripts/bookingXML.php?getYear='+SW_yearShowing+'&getMonth='+Number(SW_monthShowing+1),'getCal');
}

function jg_ajax_passback(http_request,callID){
	//called upon succesfull loading of data
	switch (callID) // returns the value of the option selected, zero index
	{
		case "getCal":
			var returnedData = http_request.responseXML;
			SW_bookingXML = returnedData.getElementsByTagName('detail'); // passing all bookigns to array
			SW_eventXML = returnedData.getElementsByTagName('event'); // passing all bookigns to array
			//alert(SW_bookingXML[0].childNodes[0].firstChild.nodeValue); // test first booking first child value (booking ref)
			if(SW_bookingXML.length!=0) //if no bookings will return zero length
			{
				//some bookings were found for this month/year - populate calendar
				jg_populate_bookings();
			}
			else
			{
				//hide the loading layer
				jg_toggle_layer_vis('workingLayer','hidden');
			}
		break;
	}
}

function jg_nav_month(movdate){
	//show the loading layer
	jg_toggle_layer_vis('workingLayer','visible');
	// remove any previously generated days in each row
	for(y=1;y<=6;y++)
	{
		var rowElement = document.getElementById("wk"+y);
		var numOfChildsExisting = (rowElement.childNodes.length-1); // the array of children is zero based - length give 1 based
		for (x=numOfChildsExisting;x>-1;x--)
		{
			if(rowElement.childNodes[x].nodeType == 1){rowElement.removeChild(rowElement.childNodes[x])}
		}
	}
	//remove generated select boxs for month/year
	//!!need to assign their values to vars as needed below to select new month/year
	var monthSel = document.getElementById("monthSel");
	var moveToMonth = monthSel.value;
    monthSel.parentNode.removeChild(monthSel);
	var yearSel = document.getElementById("yearSel");
	var moveToYear = yearSel.value;
    yearSel.parentNode.removeChild(yearSel);
	
	if(movdate=="prev")
	{
		newMonth = new Date();
		newMonth.setFullYear(SW_yearShowing, (SW_monthShowing-1), 1, 0, 0, 0, 0);
		SW_yearShowing = newMonth.getFullYear();
		SW_monthShowing = newMonth.getMonth();

		jg_init_cal();
	}
	else if (movdate=="next")
	{
		newMonth = new Date();
		newMonth.setFullYear(SW_yearShowing, (SW_monthShowing+1), 1, 0, 0, 0, 0);
		SW_yearShowing = newMonth.getFullYear();
		SW_monthShowing = newMonth.getMonth();
		jg_init_cal();
	}
	else
	{
		//is a date from drop downs as their event handles cant assign params - but seems to need formatting as date otherwise weird things happen
		newMonthYear = new Date();
		newMonthYear.setFullYear(Number(moveToYear), Number(moveToMonth), 1, 0, 0, 0, 0);
		SW_yearShowing = newMonthYear.getFullYear();
		SW_monthShowing = newMonthYear.getMonth();
		jg_init_cal();
	}
}

function jg_click_vacant(e){
	// find element called from and extract id
	if (!e) var e = window.event;
	// e gives access to the event in all browsers
	if (e.target) targ = e.target; // find the element the event occured - for W3C...
	else if (e.srcElement) targ = e.srcElement; // ...for MSIE...
	if (targ.nodeType == 3) // defeat Safari bug
	{
		targ = targ.parentNode;
	}
	document.getElementById("passData").value = targ.id;
	document.getElementById("returnTo").value = SW_monthShowing + "~" + SW_yearShowing;
	document.getElementById("createBooking").action= "admin/createBooking.php";
	document.getElementById("createBooking").submit();
}
function jg_click_booking(e){
	// find element called from and extract id
	if (!e) var e = window.event;
	// e gives access to the event in all browsers
	if (e.target) targ = e.target; // find the element the event occured - for W3C...
	else if (e.srcElement) targ = e.srcElement; // ...for MSIE...
	if (targ.nodeType == 3) // defeat Safari bug
	{
		targ = targ.parentNode;
	}
	document.getElementById("passData").value = targ.id.split("~")[1];
	document.getElementById("returnTo").value = SW_monthShowing + "~" + SW_yearShowing;
	document.getElementById("createBooking").action= "admin/showBooking.php";
	document.getElementById("createBooking").submit();
}

function jg_click_day_title(e){
	// find element called from and extract id
	if (!e) var e = window.event;
	// e gives access to the event in all browsers
	if (e.target) targ = e.target; // find the element the event occured - for W3C...
	else if (e.srcElement) targ = e.srcElement; // ...for MSIE...
	if (targ.nodeType == 3) // defeat Safari bug
	{
		targ = targ.parentNode;
	}
	document.getElementById("passData").value = targ.id.split("~")[1];
	document.getElementById("returnTo").value = SW_monthShowing + "~" + SW_yearShowing;
	document.getElementById("createBooking").action= "admin/manageEvent.php";
	document.getElementById("createBooking").submit();
}

function jg_populate_bookings(){
	//cycle through each booking returned
	for(x=0;x<SW_bookingXML.length;x++)
	{
		//set the parts from this booking to more friendly vars
		var thisBookingStarts = SW_bookingXML[x].childNodes[2].firstChild.nodeValue;
		var thisBookingEnds = SW_bookingXML[x].childNodes[3].firstChild.nodeValue;
		var thisBookingRoom = SW_bookingXML[x].childNodes[1].firstChild.nodeValue;
		var thisBookingRef = SW_bookingXML[x].childNodes[0].firstChild.nodeValue;
		//set the startdate of the booking - if an earlier month then set to 1st of current month
		thisBookingStartsArray = thisBookingStarts.split("-");
		if(thisBookingStartsArray[0]!=SW_yearShowing || thisBookingStartsArray[1]!=(SW_monthShowing+1))
		{
			bookStart = 1;
		}
		else
		{
			bookStart = thisBookingStartsArray[2];
			while(bookStart.substr(0,1)=="0" && bookStart.length>1){bookStart = bookStart.substr(1)} //strip off any leading zeros
		}
		//set the enddate of the booking - if a later month then set to last day of current month
		thisBookingEndsArray = thisBookingEnds.split("-");
		if(thisBookingEndsArray[0]!=SW_yearShowing || thisBookingEndsArray[1]!=(SW_monthShowing+1))
		{
			//find last day of current month
			lastDayOfMonth = new Date();
			lastDayOfMonth.setFullYear(SW_yearShowing,(SW_monthShowing+1),0);
			bookEnd = lastDayOfMonth.getDate();
		}
		else
		{
			bookEnd = thisBookingEndsArray[2];
			while(bookEnd.substr(0,1)=="0" && bookEnd.length>1){bookEnd = bookEnd.substr(1)} //strip off any leading zeros
		}

		//set all as default first
		for(y=Number(bookStart);y<=Number(bookEnd);y++)
		{
			//remove previous event handler
			jg_modify_event_handler(document.getElementById("room-"+thisBookingRoom+"_"+y+"_"+(SW_monthShowing+1)+"_"+SW_yearShowing),'click',jg_click_vacant,'remove');
			//change class
			document.getElementById("room-"+thisBookingRoom+"_"+y+"_"+(SW_monthShowing+1)+"_"+SW_yearShowing).className = 'bookedRoomMid';
			//change id
			document.getElementById("room-"+thisBookingRoom+"_"+y+"_"+(SW_monthShowing+1)+"_"+SW_yearShowing).id = "room-"+thisBookingRoom+"_"+y+"_"+(SW_monthShowing+1)+"_"+SW_yearShowing+"~"+thisBookingRef;
			//set new event handler
			jg_modify_event_handler(document.getElementById("room-"+thisBookingRoom+"_"+y+"_"+(SW_monthShowing+1)+"_"+SW_yearShowing+"~"+thisBookingRef),'click',jg_click_booking,'add');
			
			//set start and end css
			// for start date actualy starting in this month/year
			if(y==Number(bookStart) && thisBookingStartsArray[0]==SW_yearShowing && thisBookingStartsArray[1]==(SW_monthShowing+1))
			{
				document.getElementById("room-"+thisBookingRoom+"_"+y+"_"+(SW_monthShowing+1)+"_"+SW_yearShowing+"~"+thisBookingRef).className = 'bookedRoomStart';
			}
			// for start dates starting in earlier months/years
			if((y==Number(bookStart) && thisBookingStartsArray[0]!=SW_yearShowing) || (y==Number(bookStart) && thisBookingStartsArray[1]!=(SW_monthShowing+1)))
			{
				document.getElementById("room-"+thisBookingRoom+"_"+y+"_"+(SW_monthShowing+1)+"_"+SW_yearShowing+"~"+thisBookingRef).className = 'bookedRoomMid';
			}
			
			// for end date actualy ending in this month/year
			if(y==Number(bookEnd) && thisBookingEndsArray[0]==SW_yearShowing && thisBookingEndsArray[1]==(SW_monthShowing+1))
			{
				document.getElementById("room-"+thisBookingRoom+"_"+y+"_"+(SW_monthShowing+1)+"_"+SW_yearShowing+"~"+thisBookingRef).className = 'bookedRoomEnd';
			}
			// for end dates ending in later months/years
			if((y==Number(bookEnd) && thisBookingEndsArray[0]!=SW_yearShowing) || (y==Number(bookEnd) && thisBookingEndsArray[1]!=(SW_monthShowing+1)))
			{
				document.getElementById("room-"+thisBookingRoom+"_"+y+"_"+(SW_monthShowing+1)+"_"+SW_yearShowing+"~"+thisBookingRef).className = 'bookedRoomMid';
			}
			
			//for single day bookings - bookStart and bookEnd will match - as even from different months/years they will be different
			if(Number(bookStart)==Number(bookEnd))
			{
				document.getElementById("room-"+thisBookingRoom+"_"+y+"_"+(SW_monthShowing+1)+"_"+SW_yearShowing+"~"+thisBookingRef).className = 'bookedRoomOneDay';
			}
		}
	}
	if(SW_eventXML.length!=0) //if no events will return zero length
	{
		//some events were found for this month/year - populate calendar
		jg_populate_events();
	}
	else
	{
		//hide the loading layer
		jg_toggle_layer_vis('workingLayer','hidden');
	}
}

function jg_populate_events(){
	var evDate, titleContainer, newEventIcon;
	for(x=0;x<SW_eventXML.length;x++)
	{
		//build titlecontainrer id from date of event format: titleCont~29_1_2007
		evDate = SW_eventXML[x].childNodes[0].firstChild.nodeValue.split("-");
		while(evDate[2].substr(0,1)=="0" && evDate[2].length>1){evDate[2] = evDate[2].substr(1)} //strip off any leading zeros
		while(evDate[1].substr(0,1)=="0" && evDate[1].length>1){evDate[1] = evDate[1].substr(1)} //strip off any leading zeros
		titleContainer = document.getElementById("titleCont~" + evDate[2] + "_" + evDate[1] + "_" + evDate[0]);
		// addchild for event icon
		newEventIcon = document.createElement('img'); // create empty new img
		newEventIcon.setAttribute('src','icons/'+SW_eventXML[x].childNodes[2].firstChild.nodeValue); // set src
		newEventIcon.setAttribute('id','evIcon_'+x); // set id
		newEventIcon.setAttribute('alt',SW_eventXML[x].childNodes[3].firstChild.nodeValue); // set alt as link
		newEventIcon.setAttribute('title',SW_eventXML[x].childNodes[1].firstChild.nodeValue); // set title
		jg_modify_event_handler(newEventIcon,'click',jg_click_event,'add'); // assign click event handler
		newEventIcon.className = 'eventIcons';
		titleContainer.appendChild(newEventIcon); //append
		//alert(titleContainer.innerHTML);	
	}
	//hide the loading layer
	jg_toggle_layer_vis('workingLayer','hidden');
}

function jg_click_event(e){
	// prevent this event from triggering the parent (day title) click event
	if( e.stopPropagation ) { e.stopPropagation(); }
	e.cancelBubble = true;
	
	// find element called from and extract id
	if (!e) var e = window.event;
	// e gives access to the event in all browsers
	if (e.target) targ = e.target; // find the element the event occured - for W3C...
	else if (e.srcElement) targ = e.srcElement; // ...for MSIE...
	if (targ.nodeType == 3) // defeat Safari bug
	{
		targ = targ.parentNode;
	}
	
	url = document.getElementById(targ.id).alt;
	//goto page spcd in longdesc
	eval("parent.location='"+url+"'");
}
-->