function addHistory(text) {
	if(xGetElementById("historyDiv").childNodes.length > 6) { xGetElementById("historyDiv").removeChild(xGetElementById('historyDiv').firstChild); }
	theSpan = document.createElement("span");
	theSpan.style.display = "block";
	theSpan.style.fontSize = "11px";
	theSpan.appendChild(document.createTextNode(text));
	xGetElementById("historyDiv").appendChild(theSpan);
}

function changeAlbumPage(which) {
	//change the pageid in the database for the photo album with albumid = which
	selectElem = xGetElementById("pageAlbumId"+which);
	newPage = selectElem.options[selectElem.selectedIndex].value;

	//no onreadystatechange function needed here
	queryString = new Object();
	queryString["function"] = "changeAlbumPid";
	queryString["albumid"] = which;
	queryString["pid"] = newPage;
	url = prepareURL("admin/ajaxCalls.php",queryString);
	changePID = GetXmlHttpObject();
	changePID.open("GET",url,true);
	changePID.send(null);
}

function changeCalPage(which) {
	queryString = new Object();
	queryString["function"] = "changeCal";
	queryString["page"] = which;
	
	url=prepareURL("admin/ajaxCalls.php",queryString);
	
	calPage = GetXmlHttpObject();
	calPage.open("GET",url,true);
	calPage.send(null);
}

function changeLinkName(id,table) {
	//change to an input element with a link "change name" underneath
	//increase the height of the desired UL element so that it will all fit
	theUL = xGetElementById(table+"id"+id);
	theUL.style.height = "45px";


	for(i=0;i<theUL.childNodes.length;i++) {
	 if(theUL.childNodes[i].className == "name") {
	  theLI = theUL.childNodes[i];
	  for(m=0;m<theLI.childNodes.length;m++) {
	   if(theLI.childNodes[m].nodeName == "A") {
		//remove theAnchor from the DOM and put it in a variable
		theAnchor = theLI.removeChild(theLI.childNodes[m]);
	   }
	  }
	 }
	}

	//get the text from the anchor
	currentName = theAnchor.innerHTML

	//create an input element
	theInput = document.createElement("input");
	theInput.value = currentName;
	theInput.className = "nameChange";

	//create an anchor to accept the name change
	theNewAnchor = document.createElement("a");
	theNewAnchor.href = "javascript:finishChangeName('"+id+"','"+table+"')";
	theNewAnchor.innerHTML = "Change Name";	

	//append both items to theLI
	theLI.appendChild(theInput);
	theLI.appendChild(theNewAnchor);

	theInput.focus();

}

function changeOrder(move,id) {

	//get the visible upload Divs for the div reload
	uploadDivs = getVisibleUploadDivs();

	//make an ajax call to swap zOrders and return the id of the row that we are swapping with
	queryString = new Object();
	queryString["function"] = "changeOrder";
	queryString["pageid"] = id;
	queryString["direction"] = move;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	newOrder = GetXmlHttpObject();
	newOrder.open("GET",url,true);
	newOrder.onreadystatechange = function() {
 	 if(newOrder.readyState == 4 && newOrder.status == 200) {

	    //reload the pageList div
		reloadDiv('pageList',newOrder.responseXML.documentElement);
		
	   //retain the visibility of the upload Divs after the Div reload
	   retainVisibleUploadDivs(uploadDivs);
		
		//reload the menu
		reloadMenu();
		
		//reload all select lists
		reloadSelects();
		
	 }
	}

	newOrder.send(null);

}

function confDelImage(which) {
	//get this LI and the albumid
	theLI = xGetElementById('photo'+which);
	theAlbumId = theLI.parentNode.id.match(/\d+/);
	

	//make an ajax call to delete the image from the database
	queryString = new Object();
	queryString["function"] = "delImage";
	queryString["photoid"] = which;
	queryString["albumid"] = theAlbumId;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	delThisImage = GetXmlHttpObject();
	delThisImage.open("GET",url,true);
	delThisImage.onreadystatechange = function() {
		if(delThisImage.readyState == 4 && delThisImage.status == 200) {
			//if there are no photos left in the database in this album, then 
			//change the display proprety of the parent div of theAlbumId to none
			if(delThisImage.responseText == "0") {
				theLI.parentNode.parentNode.style.display = "none";
				getNumImages(theAlbumId);
			}

			//remove the LI from the list
			theLI.parentNode.removeChild(theLI);
		}
	}
	delThisImage.send(null);
}

function confRemoveThis(which,type) {

	//get the visible uploadDivs
	uploadDivs = getVisibleUploadDivs();

	//send an AJAX call to remove the album from the album table and all associated photos from the photos table
	queryString = new Object();
	queryString["function"] = "removeThis";
	queryString["id"] = which;
	queryString["type"] = type;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	removeThisElem = GetXmlHttpObject();
	removeThisElem.open("GET",url,true);
	removeThisElem.onreadystatechange = function() {
		if(removeThisElem.readyState == 4 && removeThisElem.status == 200) {
			if(type == "page") {			
			
				//reload the menu
				reloadMenu();
				
				//reload the select Lists
				reloadSelects();
			}

			//reload the div - could be albumList or pageList
			reloadDiv(type+'List',removeThisElem.responseXML.documentElement);
			
			//retain the visibility of the visible divs before the div reload
			retainVisibleUploadDivs(uploadDivs);
			
		}
	}
	removeThisElem.send(null);

}

function convertToHtml(xml,parent) {
	var i;
	for(i=0;i<xml.childNodes.length;i++) {
		//create the HTML node
		if(xml.childNodes[i].nodeName == "#text") {
			theHtmlNode = document.createTextNode(xml.childNodes[i].nodeValue);
		} else {
			theHtmlNode = document.createElement(xml.childNodes[i].nodeName);
		}

		if(xml.childNodes[i].attributes != null) {
			for(m=0;m<xml.childNodes[i].attributes.length;m++) {
				//internet explorer fix for class vs className issue
				if(xml.childNodes[i].attributes[m].name == "class") {
					theHtmlNode.className = xml.childNodes[i].attributes[m].value;
				}
				
				//style parser to apply inline styles correctly in internet explorer
				if(xml.childNodes[i].attributes[m].name == "style") {
					//use an inline style parser to apply the elements dynamically
					theHtmlNode.style.cssText = xml.childNodes[i].attributes[m].value;
				}
				
				//event parser to apply events properly to elements in internet explorer
				if(xml.childNodes[i].attributes[m].name.match(/^on/)) {
					var theEvent = xml.childNodes[i].attributes[m].value.split(/return/);
					var theFunction = theEvent[0];
					var theReturn = theEvent[1];
					
					if(theReturn && theReturn.match(/false/)) {
						theHtmlNode.removeAttribute("href"); //this is the only way i can think of doing it....dammit
					}
						xAddEventListener(theHtmlNode,xml.childNodes[i].attributes[m].name.substr(2),function() { eval(theFunction) },true);
				}

				if(!xml.childNodes[i].attributes[m].name.match(/^on/) && xml.childNodes[i].attributes[m].name != "style") {
					theHtmlNode.setAttribute(xml.childNodes[i].attributes[m].name,xml.childNodes[i].attributes[m].value);
				}
			}
		}

		if(xml.childNodes[i].hasChildNodes()) {
			//append the newly created node to the parent and then create the next child by running the function again
			parent.appendChild(theHtmlNode);
			convertToHtml(xml.childNodes[i],theHtmlNode);
		} else {
			//append the newly created node to the parent
			parent.appendChild(theHtmlNode);
		}
		
	}
}

function close(which) {

	if(which) {
		xGetElementById("photoWindow"+which).style.display = "none";
		keypress = null;
		xRemoveEventListener(document, 'keydown', keypress, true);
	} else {
		if(xGetElementById("titleWindow")) {
			xGetElementById("titleWindow").parentNode.removeChild(xGetElementById("titleWindow"));
		} else {
			xGetElementById("photoWindow").parentNode.removeChild(xGetElementById("photoWindow"));
		}
	}
	xGetElementById("darkenBackground").parentNode.removeChild(xGetElementById("darkenBackground"));
}	

function createPage() {
	//remove the error div, if present from this #pages .create
	while(xGetElementById("error")) {
		xGetElementById("error").parentNode.removeChild(xGetElementById("error"));
	}
	
	//get the page name and the subpage
	theName = xGetElementById('newPageName');
	theNameValue = theName.value;
	
	theSubpage = xGetElementById('newSubpage');
	theNewSubpage = theSubpage.options[theSubpage.selectedIndex].value;
	
	if(theNewSubpage == 0) {
	 theNewSubpage = "";
	}

	theTitle = xGetElementById('newPageTitle');
	theTitleValue = theTitle.value;
	
	//error handling - add an error and display it in the div
	if(theTitleValue == "" || theNameValue == "") { 
		errorDiv = document.createElement("div");
		errorDiv.id = "error";
		errorDiv.style.position = "absolute";
		errorDiv.style.left = "250px";
		errorDiv.style.width = "275px";
		if(theNameValue == "") {
			errorDiv.innerHTML = "You must enter a name for the new page";
			errorDiv.style.marginTop = "30px";
		} else {
			errorDiv.innerHTML = "You must enter a title for the new page"
			errorDiv.style.marginTop = "50px";
		}
		
		divs = xGetElementById("pages").getElementsByTagName("div")
		for(i=0;i<divs.length;i++) {
			if(divs[i].className == "create") {
				divs[i].insertBefore(errorDiv,divs[i].firstChild);
				break;
			}
		}
		return false; 
	}

	//get all the subPageDivs that have a style of display == block
	uploadDivs = getVisibleUploadDivs();

	//send an AJAX call and return the XML of the pageList
	queryString = new Object();
	queryString["function"] = "newpage";
	queryString["name"] = theNameValue;
	queryString["parent"] = theNewSubpage;
	queryString["title"] = theTitleValue;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	newPage = GetXmlHttpObject();
	newPage.open("GET",url,true);
	newPage.onreadystatechange = function() {
	 if(newPage.readyState == 4 && newPage.status == 200) {
	   //determine if the query worked or not based on the responseXML
	   if(newPage.responseXML.documentElement.getElementsByTagName("already").length > 0) {
		errorDiv = document.createElement("div");
		errorDiv.id = "error";
		errorDiv.style.position = "absolute";
		errorDiv.style.left = "250px";
		errorDiv.style.width = "275px";
		errorDiv.style.marginTop = "30px";
		already = newPage.responseXML.documentElement.getElementsByTagName("already")[0].firstChild.nodeValue;
		errorDiv.innerHTML = "The name of this page is too similar to "+already;
		
		divs = xGetElementById("pages").getElementsByTagName("div")
		for(i=0;i<divs.length;i++) {
			if(divs[i].className == "create") {
				divs[i].insertBefore(errorDiv,divs[i].firstChild);
				break;
			}
		}
		return false;
	   }
	   
	   	//clear the inputs
		theTitle.value = "";
		theSubpage.options[theSubpage.selectedIndex].value = 0;
		theName.value = "";
	   
	   //reload the pageList div
	   reloadDiv('pageList',newPage.responseXML.documentElement);

	   //show all previously open subPageDivs
	   retainVisibleUploadDivs(uploadDivs);
	   
	   //reload the menu
		reloadMenu();
		
		//reload all select lists
		reloadSelects();
	   
	 }
	}
	newPage.send(null);	
}

function createAlbum() {
	//get the new album name and pageid
	theAlbumName = xGetElementById("newAlbumName").value;
	theAlbumPid = xGetElementById("newAlbumPage").options[xGetElementById("newAlbumPage").selectedIndex].value;

	//error check to make sure a name for the photo album was entered
	if(theAlbumName == "") { 		
		return false; 
	}

	//clear the inputs
	xGetElementById("newAlbumName").value = "";
	xGetElementById("newAlbumPage").options[xGetElementById("newAlbumPage").selectedIndex].value = "1";

	//perform an ajax call to append regnerate the albumList
	queryString = new Object();
	queryString["function"] = "createAlbum";
	queryString["name"] = theAlbumName;
	queryString["pid"] = theAlbumPid;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	creatingAlbum = GetXmlHttpObject();
	creatingAlbum.open("GET",url,true);
	creatingAlbum.onreadystatechange = function() {
		if(creatingAlbum.readyState == 4 && creatingAlbum.status == 200) {
		
			//reload the albumList div
			reloadDiv('albumList',creatingAlbum.responseXML.documentElement);
		}
	}
	creatingAlbum.send(null);
}

function delImage(which) {

	theLI = xGetElementById('photo'+which);

	//set the visibility of the img to hidden and the display of a.del to none
	for(i=0;i<theLI.childNodes.length;i++) {
		if(theLI.childNodes[i].nodeName == "A" || theLI.childNodes[i].nodeName == "IMG") {
			theLI.childNodes[i].style.display = "none";
		}
	}

	//create the div
	theDiv = document.createElement("div");
	theDiv.className = "confirmdelete";
	theText = document.createElement("strong");
	theText.innerHTML = "Are you sure you want to delete this photo?";

	theDiv.appendChild(theText);

	yes = document.createElement("img");
		yes.className = "yes";
		yes.src = "images/admin/yes.png";
		yes.width = "60";
		yes.height = "19";
		yes.alt = "Delete this photo";

	yesLink = document.createElement("a");
	yesLink.href = "javascript:confDelImage('"+which+"');";
	yesLink.appendChild(yes);

	no = document.createElement("img");
		no.className = "no";
		no.src = "images/admin/no.png";
		no.width = "60";
		no.height = "19";
		no.alt = "Do not delete";

	noLink = document.createElement("a");
	noLink.href = "javascript:restoreImage('"+which+"');";
	noLink.appendChild(no);

	theDiv.appendChild(yesLink);
	theDiv.appendChild(noLink);
	theLI.appendChild(theDiv);
}

function delThisEvent() {

 confDel = confirm("Are you sure you want to delete this event?");
 if(confDel == true) {
	while(xGetElementById("error")) {
		xGetElementById("error").parentNode.removeChild(xGetElementById("error"));
	}

	//delete the event through an ajax call
	queryString = new Object();
	queryString["function"] = "delEvent";
	queryString["id"] = xGetElementById("eventid").value;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	delEvent = GetXmlHttpObject();
	delEvent.open("GET",url,true);
	delEvent.onreadystatechange = function() {
		if(delEvent.readyState == 4 && delEvent.status == 200) {
			//reload the calendar
			changeCal('activities','1','');
	
			//reload the div
			events(xGetElementById("date").value);
			
			deleted = document.createElement("div");
			deleted.id = "error";
			deleted.style.width = "100%";
			deleted.innerHTML = "The event has been deleted";
	
			xGetElementById("eventcalInfo").insertBefore(deleted,xGetElementById("eventcalInfo").firstChild);
	
		}
	}
	delEvent.send(null);
	
 } else {
	return false;
 }
}	

function dragResize(theDiv,type,event) {
	
	if(xGetElementById("headerImages")) { //make sure the function is being called on the correct page
		if(!theDiv) { //if this is the onload function, then add the appropriate mousedown event listeners
		theDragDiv = xGetElementById("drag");
		theResizeDiv = xGetElementById("resize");
		
			xAddEventListener(theDragDiv,'mousedown',function(e) { dragResize(theDragDiv,"drag",e) },true);
			
			xAddEventListener(theResizeDiv,'mousedown',function(e) { dragResize(theResizeDiv,"resize",e) },true);
		
		} else { //if the mouse has been clicked on the drag or resize elements - add the mousemove and mouseup event listeners to the appropriate element directing it to the appropriate function
			//original mouse coordinates when it initially clicked
			ev = event || window.event;
			mouseX = ev.pageX;
			mouseY = ev.pageY;
			
			//current offset of the div
			divX = theDiv.parentNode.offsetLeft;
			divY = theDiv.parentNode.offsetTop;

			xGetElementById("drag").innerHTML = "";

			xAddEventListener(document,'mouseup',finishDragResize,false);
			xAddEventListener(window,'mouseclick',removeAll,false);
			
			switch(type) {
				case "drag":
					document.body.style.cursor = "move";
					xAddEventListener(document,'mousemove',doThis = function(e) { moveThis(theDiv.parentNode,e,mouseX,mouseY,divX,divY) },false);
					break;
				case "resize":
					document.body.style.cursor = "n-resize";
					xAddEventListener(document,'mousemove',doThis = function(e) { resizeThis(theDiv.parentNode,e,mouseY) },false);
					break;
			}
		}
	}
}

function editTitle(which) {
	//darken the background
	theDiv = document.createElement("div");
	theDiv.id = "darkenBackground";
	document.body.appendChild(theDiv);
	
	//get the image information from the database
	queryString = new Object();
	queryString["function"] = "getImg";
	queryString["photoid"] = which;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	getPhoto = GetXmlHttpObject();
	getPhoto.open("GET",url,true);
	getPhoto.onreadystatechange = function() {
		if(getPhoto.readyState == 4 && getPhoto.status == 200) {
			convertToHtml(getPhoto.responseXML.documentElement,titleWindow);
		}
	}
	getPhoto.send(null);
	
	
	titleWindow = document.createElement("div");
	titleWindow.id = "titleWindow";
	document.body.appendChild(titleWindow);
}

function events(date) {
	while(xGetElementById("error")) {
		xGetElementById("error").parentNode.removeChild(xGetElementById("error"));
	}
	
	//start off at the beginning, set everything to the necessary values
	xGetElementById("date").value = date;
	xGetElementById("title").value = "";
	xGetElementById("eventid").value = "";

	//no events found - leave everything blank, and remove the label and the list, if present
	xGetElementById("title").value = "";
	xGetElementById("eventid").value = "";
				
	if(xGetElementById("events")) {
		xGetElementById("eventcalInfo").removeChild(xGetElementById("events"));
		xGetElementById("eventcalInfo").removeChild(xGetElementById("eventslabel"));
	}
				
	//change the buttons, if necessary
	if(xGetElementById("delevent")) {
		xGetElementById("eventcalInfo").removeChild(xGetElementById("delevent"));
		xGetElementById("postevent").value = "Post Event";
	}

	theEditor = FCKeditorAPI.GetInstance('FCKeditor');
	theEditor.SetHTML('');
	
	//check the database to see if there are any events on this day
	queryString = new Object();
	queryString["function"] = "getEvents";
	queryString["date"] = date;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	getEvents = GetXmlHttpObject();
	getEvents.open("GET",url,true);
	getEvents.onreadystatechange = function() {
		if(getEvents.readyState == 4 && getEvents.status == 200) {
			
			if(getEvents.responseXML) {
				//found events - create and populate the drop down menu, and load the first event
				theDate = getEvents.responseXML.documentElement.getElementsByTagName("date")[0].firstChild.nodeValue;
				theLabel = document.createElement("label");
					theLabel.innerHTML = "Events for "+theDate+":";
					theLabel.id = "eventslabel";
				
				theList = document.createElement("select");
					theList.name = "events"
					theList.id = "events";
				
				xAddEventListener(theList,'change',function() { loadEvent(this.options[this.selectedIndex].value); },false);
				
					newEvent = document.createElement("option");
					newEvent.value = "0";
					newEvent.innerHTML = "New Event";
					
				theList.appendChild(newEvent);
							
				//add the label and the list to the DOM
				xGetElementById("eventcalInfo").insertBefore(theLabel,xGetElementById("title").nextSibling.nextSibling);
				xGetElementById("eventcalInfo").insertBefore(theList,theLabel.nextSibling);						
			
			
				theEvents = getEvents.responseXML.documentElement.getElementsByTagName("event");
				for(i=0;i<theEvents.length;i++) {
					//create the new option and add it to theList
					newOption = document.createElement("option");
					newOption.value = theEvents[i].getElementsByTagName("id")[0].firstChild.nodeValue;
					newOption.innerHTML = theEvents[i].getElementsByTagName("title")[0].firstChild.nodeValue;
					
					theList.appendChild(newOption);
				}	
			}
		}
	}
	getEvents.send(null);
	
	xGetElementById('eventcalInfo').style.display = "block";
}

function findPos(obj){
	var posX = obj.offsetLeft;
	var posY = obj.offsetTop;
	while(obj.offsetParent){
			if(obj==document.getElementsByTagName('body')[0]){
				break;
			} else {
			posX=posX+obj.offsetParent.offsetLeft;
			posY=posY+obj.offsetParent.offsetTop;
			obj=obj.offsetParent;
		}
	}
	var posArray=[posX,posY]
	return posArray;
}

function finishChangeName(id,table) {	

	//get the newName
	theUL = xGetElementById(table+"id"+id);
	for(i=0;i<theUL.childNodes.length;i++) {
		if(theUL.childNodes[i].className == "name") {
			for(m=0;m<theUL.childNodes[i].childNodes.length;m++) {
				if(theUL.childNodes[i].childNodes[m].nodeName == "INPUT") {
					theNewName = theUL.childNodes[i].childNodes[m].value;
					break;
				}
			}
			
			break;
		}
	}

	//get the visible upload Divs before the list reload
	uploadDivs = getVisibleUploadDivs();
	
	//make an ajax call and change the name in the navigation menu and reload the list that we are dealing with	
	queryString = new Object();
	queryString["function"] = "nameChange";
	queryString["newName"] = theNewName;
	queryString["table"] = table;
	queryString["id"] = id;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	nameChange = GetXmlHttpObject();
	nameChange.open("GET",url,true);
	nameChange.onreadystatechange = function() {
		if(nameChange.readyState == 4 && nameChange.status == 200) {
			
			//reload the div we are dealing with
			reloadDiv(table+"List",nameChange.responseXML.documentElement);
			
			//if we are changing a name in the pageList, reload the menu to reflect that
			if(table == "page") {
					reloadMenu();
					
					//retain the visibility of the upload Divs after the div reload
					retainVisibleUploadDivs(uploadDivs);
					
					//reload the select menus
					reloadSelects();
			}
		}
	}
	nameChange.send(null);
}

function finishDragResize(div) {
	xGetElementById("drag").innerHTML = "Drag this selection to crop your image";
	
	document.body.style.cursor = "default";
	
	//remove the mousemove and mouseup event listeners as we don't need them anymore
	xRemoveEventListener(document,'mousemove',doThis,false);
	xRemoveEventListener(document,'mouseup',finishDragResize,false);
	xRemoveEventListener(document,'mouseclick',removeAll,true);
}

function finishEditingTitle(which) {
	//change the title and caption of the image in the database
	
	queryString = new Object();
	queryString["function"] = "editImg";
	queryString["title"] = xGetElementById("imgtitle").value;
	queryString["caption"] = xGetElementById("imgcaption").value;
	queryString["photoid"] = which;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	editPhoto = GetXmlHttpObject();
	editPhoto.open("GET",url,true);
	editPhoto.onreadystatechange = function() {
		if(editPhoto.readyState == 4 && editPhoto.status == 200) {
			//remove #darkenBackground and #windowTitle
			if(xGetElementById("darkenBackground")) {
				xGetElementById("darkenBackground").parentNode.removeChild(xGetElementById("darkenBackground"));
			}
			
			if(xGetElementById("titleWindow")) {
				xGetElementById("titleWindow").parentNode.removeChild(xGetElementById("titleWindow"));
			}
		}
	}
	editPhoto.send(null);
}

function finishUpload(code,result,type) {
	var resultArray = result.split("|");
	albumid = "";
	if(type == "albums") {
		if(code == 0) {
			albumid = resultArray[1];
		} else {
			albumid = resultArray[5];
		}
	}

	theLoadingDiv = xGetElementById("uploading"+albumid);
	if(code == 0) {
		while(theLoadingDiv.hasChildNodes()) {
			theLoadingDiv.removeChild(theLoadingDiv.firstChild);
		}

		//display the error code in the div
		errorCode = document.createElement("strong");
		errorCode.innerHTML = resultArray[0];
		errorCode.style.paddingTop = "45px";
		errorCode.style.display = "block";
		theLoadingDiv.appendChild(errorCode);

	} else {
		if(type == "albums") {
			//change the number of images
			getNumImages(albumid);

			//change the loading DIV span to show "Image Uploaded. Creating Thumbnail"
			theTag = xGetElementById("uploading"+albumid).getElementsByTagName("strong")[0];
			theTag.innerHTML = "Image Upload Complete<br />Creating Thumbnail...";
		
			//resize the image
			resizeThisImage(resultArray,resultArray[4],resultArray[1],resultArray[2],albumid);
			setTimeout(function() { loadTheRest(resultArray,albumid) },4000);
		} else {
			//run a php script to convert it to a jpeg and update the database with the proper scale ratio
			queryString = new Object();
			queryStrng['function'] = "headerImg";
			queryString['img'] = resultArray[0];
			queryString['headerid'] = resultArray[1];
			
			url = prepareURL("admin/ajaxCalls.php",queryString);
			
			convertHeaderImg = GetXmlHttpObject();
			convertHeaderImg.open("GET",url,true);
			convertHeaderImg.onreadystatechange = function() {
				if(convertHeaderImg.readyState == 4 && convertHeaderImg.status == 200) {
					//show the thumbnail in the loading div
			
					//add the image to the list
			
					//display the image thumbnail  in theHeaderImage
			
					//resize the selection div according to the appropriate scale ratio				
				}
			}
			convertHeaderImg.send(null);			
		}
	}
}

function getNumImages(which) {

	queryString = new Object();
	queryString["function"] = "numImages";
	queryString["albumid"] = which;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	numImages = GetXmlHttpObject();
	numImages.open("GET",url,true);
	numImages.onreadystatechange = function() {
		if(numImages.readyState == 4 && numImages.status == 200) {
			theUL = xGetElementById("albumid"+which);
			for(i=0;i<theUL.childNodes.length;i++) {
				if(theUL.childNodes[i].className == "pictures") {
					while(theUL.childNodes[i].hasChildNodes()) {
						theUL.childNodes[i].removeChild(theUL.childNodes[i].firstChild);
					}
						
					newNum = numImages.responseText;
					if(newNum == 0) {
						theUL.childNodes[i].appendChild(document.createTextNode("0 photos"));
					} else {
						if(newNum == 1) { photos = "photo"; } else { photos = "photos"; }
						
						theAnchor = document.createElement("a");
						theAnchor.href = "javascript:togglePhotos('"+which+"');";
						theAnchor.innerHTML = newNum+" "+photos;
						theUL.childNodes[i].appendChild(theAnchor);
					}
					break;
				}
			}
		}
	}
	numImages.send(null);
}

function getVisibleUploadDivs() {
	
	var uploadDivs = new Array();
	
	allDivs = document.getElementsByTagName("div");
	for(i=0;i<allDivs.length;i++) {
		if(allDivs[i].className == "uploadDiv" && allDivs[i].style.display == "block") {
			uploadDivs.push(allDivs[i].id);
		}
	}
	
	return uploadDivs;
}

function loadEvent(eventid) {
	while(xGetElementById("error")) {
		xGetElementById("error").parentNode.removeChild(xGetElementById("error"));
	}
	
	queryString = new Object();
	queryString["function"] = "getThisEvent";
	queryString["id"] = eventid;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	thisEvent = GetXmlHttpObject();
	thisEvent.open("GET",url,true);
	thisEvent.onreadystatechange = function() {
		if(thisEvent.readyState == 4 && thisEvent.status == 200) {
			theEditor = FCKeditorAPI.GetInstance('FCKeditor');
			if(thisEvent.responseXML) {
			
				root = thisEvent.responseXML.documentElement;
			
				xGetElementById("eventid").value = eventid;
				xGetElementById("title").value = root.getElementsByTagName("title")[0].firstChild.nodeValue;

				//set the html of the FCKeditor
				html = (new XMLSerializer()).serializeToString(root.getElementsByTagName("html")[0]);
				theEditor.SetHTML(html);
				
				//add a "Remove this Event" button right next to the Post Event
				if(xGetElementById("delevent")) {
				} else {
					delEvent = document.createElement("input");
					delEvent.type = "button";
					delEvent.value = "Delete Event";
					delEvent.id = "delevent";
					xAddEventListener(delEvent,'click',delThisEvent,false);
				
					xGetElementById("eventcalInfo").insertBefore(delEvent,xGetElementById("postevent").nextSibling);
					xGetElementById("eventid").value = eventid;
					//change the value of the Post Event button to Post Changes
					xGetElementById("postevent").value = "Post Event Changes";
				}
			} else {
				//set everything to blank
				theEditor.SetHTML("");
				xGetElementById("postevent").value = "Post Event";
				xGetElementById("eventcalInfo").removeChild(xGetElementById("delevent"));
				xGetElementById("eventid").value = "";
				xGetElementById("title").value = "";
			}
		}
	}
	thisEvent.send(null);
}

function loadTheRest(resultArray,albumid) {
		theLoadingDiv = xGetElementById("uploading"+albumid);
		while(theLoadingDiv.hasChildNodes()) {
			theLoadingDiv.removeChild(theLoadingDiv.firstChild);
		}


				//resize the image - landscape = >= 190 x 133
				//		   - portrait = >= 133 x 190
				if(resultArray[1] > resultArray[2]) { //landscape
					newWidth = 190;
					newHeight = Math.round(resultArray[2]*(1/(resultArray[1]/190)));

					thumbWidth = 167;
					thumbHeight = Math.round(resultArray[2]*(1/(resultArray[1]/167)));
					imageType = "landscape";
				} else {
					if(resultArray[2] > resultArray[1]) { //portrait
						newHeight = 130;
						newWidth = Math.round(resultArray[1]*(1/(resultArray[2]/140)));

						thumbHeight = 140;
						thumbWidth = Math.round(resultArray[1]*(1/(resultArray[2]/167)));
						imageType = "portrait";

					} else { //square
						newHeight = 130;
						newWidth = 130;

						thumbHeight = 167;
						thumbWidth = 167;
						imageType = "square";
					}
				}

				theLoadingDiv.style.width = newWidth+"px";
				theLoadingDiv.style.height = newHeight+"px";

				theFileName = document.createElement("small");
				theFileName.innerHTML = resultArray[0];
				theFileName.style.top = eval(newHeight)+2;

				theImage = document.createElement("img");
				theImage.alt = "Uploaded Photo";
				theImage.src = "images/albums/"+resultArray[0];
				theImage.height = newHeight;
				theImage.width = newWidth;

				theLoadingDiv.appendChild(theImage);
				theLoadingDiv.appendChild(theFileName);
		
				//create a new LI and append it to the list
				photoList = xGetElementById("photoListAlbumId"+albumid);
		
				newLI = document.createElement("li");
				newLI.className = imageType;
				newLI.id = "photo"+resultArray[3];
				var theId = resultArray[3];
					xAddEventListener(newLI,'mouseover',function() { toggleDelImage(theId); },true);
					xAddEventListener(newLI,'mouseout',function() { toggleDelImage(theId); },true);
		
				theErrAnchor = document.createElement("a");
				theErrAnchor.className = "del";
				theErrAnchor.href = "javascript:delImage('"+resultArray[3]+"');";
		
				theErrImg = document.createElement("img");
				theErrImg.width = "25";
				theErrImg.height = "25";
				theErrImg.src = "images/admin/delImage.png";
					theErrAnchor.appendChild(theErrImg);
		
				theImgAnchor = document.createElement("a");
				theImgAnchor.href = "javascript:editTitle('"+resultArray[3]+"');";
		
				theImg = document.createElement("img");
				theImg.src = "images/albums/"+resultArray[0];
				theImg.width = thumbWidth;
				theImg.height = thumbHeight;
		
				theImgAnchor.appendChild(theImg);
		
					newLI.appendChild(theImgAnchor);
					newLI.appendChild(theErrAnchor);
					photoList.appendChild(newLI);
		
}

function moveThis(theDiv,event,mouseX,mouseY,divX,divY) {
	ev = event || window.event;
	
	//relative position of mouse compared to when it first clicked
	newMouseX = ev.pageX-mouseX;
	newMouseY = ev.pageY-mouseY;
	
	newDivX = divX+newMouseX;
	newDivY = divY+newMouseY;
	
	//as long as the bounds of the div don't go outside the parentNode, then perform the change
	if(newDivX + theDiv.offsetWidth < theDiv.parentNode.offsetWidth) {
		theDiv.style.left = newDivX+"px";
		if(theDiv.offsetLeft < 0) {
			theDiv.style.left = "0";
		}
	}
	
	if(newDivY + theDiv.offsetHeight < theDiv.parentNode.offsetHeight) {
		theDiv.style.top = newDivY+"px";
		if(theDiv.offsetTop < 0) {
			theDiv.style.top = "0";
		}
	}
	
	
}

function postEvent() {
	while(xGetElementById("error")) {
		xGetElementById("error").parentNode.removeChild(xGetElementById("error"));
	}
	
	//get the XHTML inside the editor
	theEditor = FCKeditorAPI.GetInstance('FCKeditor');
	theXHTML = theEditor.GetXHTML();
	
	//determine if we are posting a new event, or editing an old event
	eventId = xGetElementById("eventid").value;
	if(eventId == "") { eventId = "0"; }
	
	//get the title
	title = xGetElementById("title").value;
	
	//get the date
	date = xGetElementById("date").value;
	
	//error handle before we go any further
	if(theXHTML == "" || title == "") {
		errorDiv = document.createElement("div")
		errorDiv.id = "error";
		errorDiv.style.width = "100%";
		
		if(title == "") {
			errorDiv.innerHTML = "You must enter a title for the event";
		} else {
			errorDiv.innerHTML = "You must enter some information about the event";
		}
		
		xGetElementById("eventcalInfo").insertBefore(errorDiv,xGetElementById("eventcalInfo").firstChild);
		
		return false;
	}
	
	//no function needed here onreadystatechange, we will just reload this div by calling the events function	
	queryString = new Object();
	queryString["function"] = "postEvent";
	queryString["date"] = date;
	queryString["title"] = title;
	queryString["eventid"] = eventId;
	queryString["info"] = theXHTML;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	postThisEvent = GetXmlHttpObject();
	postThisEvent.open("GET",url,true);
	postThisEvent.send(null);
	
	//reload the event div using the events function
	events(date);
	
	posted = document.createElement("div");
	posted.id = "error";
	posted.style.border = "1px solid #1c7d02";
	posted.style.color = "#1c7d02";
	posted.style.width = "100%";
	posted.style.backgroundColor = "#77e45a";
	posted.innerHTML = "Your event has been posted";
	
	xGetElementById("eventcalInfo").insertBefore(posted,xGetElementById("eventcalInfo").firstChild);
	
	//reload the calendar
	changeCal('activities','1','');
	
}

function prepareURL(prefix,pairs) {
	
	//create a query string from the url
	url = prefix+"?";
	for(key in pairs) {
		//encode all the parameters to be paased
		url = url+"&"+key+"="+escape(pairs[key]);
	}

	//prevent caching of ajax calls
	t = new Date();
	url = url+"&t="+t.getTime();
	return url;
}

function reloadDiv(which,xml) {
		//remove all elements in the current div
	    thisDiv = xGetElementById(which);
		while(thisDiv.hasChildNodes()) {
			thisDiv.removeChild(thisDiv.firstChild);
		}
		
		//parse the XML into HTML and append it to the div
	   convertToHtml(xml,thisDiv);   
}

function reloadMenu() {

	queryString = new Object();
	queryString["function"] = "reloadMenu";
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	newMenu = GetXmlHttpObject();
	newMenu.open("GET",url,true);
	newMenu.onreadystatechange = function() {
		if(newMenu.readyState == 4 && newMenu.status == 200) {
			//remove all menu elements and return a reference to the UL
			theMenu = removeMenu();
			
			//replace the menu with responseXML
			convertToHtml(newMenu.responseXML.documentElement,theMenu);
		}
	}
	newMenu.send(null)
}

function removeMenu() {
	allULs = document.getElementsByTagName("ul");
	for(i=0;i<allULs.length;i++) {
		if(allULs[i].className == "menu") {
			theMenu = allULs[i];
			break;
		}
	}
	
	while(theMenu.hasChildNodes()) {
		theMenu.removeChild(theMenu.firstChild);
	}
	
	return theMenu;
}

function reloadPagination(start,albumid) {
	
	queryString = new Object();
	queryString["function"] = "reloadPagination";
	queryString["start"] = start;
	queryString["albumid"] = albumid;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	getPagination = GetXmlHttpObject();
	getPagination.open("GET",url,true)
	getPagination.onreadystatechange = function() {
		if(getPagination.readyState == 4 && getPagination.status == 200) {
			while(xGetElementById("pagination").firstChild) {
				xGetElementById("pagination").removeChild(xGetElementById("pagination").firstChild);
			}
			
			convertToHtml(getPagination.responseXML.documentElement,xGetElementById("pagination"));
		}
	}
	getPagination.send(null);
}

function reloadSelects() {
	
	queryString = new Object();
	queryString["function"] = "changeSelects";
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	allSelects = GetXmlHttpObject();
	allSelects.open("GET",url,true);
	allSelects.onreadystatechange = function() {
		if(allSelects.readyState == 4 && allSelects.status == 200) {
			allSelectTags = document.getElementsByTagName("select");
			for(i=0;i<allSelectTags.length;i++) {
				if(allSelectTags[i].className) {
					if(allSelectTags[i].className == "photoselect") {
						//remove all options elements from all of these tags
						while(allSelectTags[i].hasChildNodes()) {
							allSelectTags[i].removeChild(allSelectTags[i].firstChild);
						}
						
						//add all options elements to all of the select elements
						convertToHtml(allSelects.responseXML.documentElement,allSelectTags[i]);						
					}
				}
			}
			
			//remove all option elements with the child className from select#newSubPage
			subpageSelect = xGetElementById("newSubpage");
			for(i=0;i<subpageSelect.childNodes.length;i++) {
				if(subpageSelect.childNodes[i].className) {
					if(subpageSelect.childNodes[i].className == "child") {
						subpageSelect.removeChild(subpageSelect.childNodes[i]);
					}
				}
			}
						
			//change the selected option of the select lists in the photo album
			for(i=0;i<allSelectTags.length;i++) {
				if(allSelectTags[i].id) {
					if(allSelectTags[i].id.match("pageAlbumId")) {
						setSelectedOption(allSelectTags[i],allSelectTags[i].id.match(/\d+/));
					}
				}
			}
		}
	}
	allSelects.send(null)

}

function removeAll() {
	//remove all event listeners from the document
	xRemoveEventListener(document,'mousemove',doThis,false);
	xRemoveEventListener(document,'mouseup',finishDragResize,false);
	xRemoveEventListener(window,'mouseclick',removeAll,false);
}

function removeThis(which,type) {
	//set all of the LI in the type UL to display: none;
	theUL = xGetElementById(type+"id"+which);
	for(i=0;i<theUL.childNodes.length;i++) {
		if(theUL.childNodes[i].nodeName == "LI") {
			theUL.childNodes[i].style.display = "none";
		}
	}

	//if we are dealing with an album
	if(type == "album") {
		//determine if the upload div or the photolist div is open
		if(xGetElementById("showAlbumId"+which)) {
			if(xGetElementById("showAlbumId"+which).style.display == "block") {
				//toggle it
				togglePhotos(which);
			}
		}
		
		if(xGetElementById("uploadAlbumId"+which)) {
			if(xGetElementById("uploadAlbumId"+which).style.display == "block") {
				//toggle it
				upload(which);
			}
		}
	} else {
		if(xGetElementById("subpage"+which)) {
			if(xGetElementById("subpage"+which).style.display == "block") {
				//toggle it
				toggleSubpages(which);
			}
		}
	}
	
	//add a new LI that stretches the whole way across
	theConfLI = document.createElement("li");
	theConfLI.className = "confdelete";
		yes = document.createElement("img");
		yes.src = "images/admin/yes.png";
		yes.height = "19";
		yes.width = "60";
			yesLink = document.createElement("a");
			yesLink.href = "javascript:confRemoveThis('"+which+"','"+type+"');";
			yesLink.appendChild(yes);
		no = document.createElement("img");
		no.src = "images/admin/no.png";
		no.height = "19";
		no.width = "60";
			noLink = document.createElement("a");
			noLink.href = "javascript:restoreThis('"+which+"','"+type+"');";
			noLink.appendChild(no);
		theConfText = document.createElement("strong");
		theConfText.innerHTML = "Are you sure you would like to delete this "+type+"?";
	theConfLI.appendChild(theConfText);
	theConfLI.appendChild(yesLink);
	theConfLI.appendChild(noLink);
	theUL.appendChild(theConfLI);
}

function resizeThis(theDiv,event,mouseY) {
	
	ev = event || window.event;
	newMouseY = ev.pageY-mouseY; //the difference in the original height and the new height
	
	//if the event takes place below the area of #theHeaderImage or above the bottom of #selection, then forget about it
	theHeaderImageOffset = findPos(xGetElementById('theHeaderImage'));
	theSelectionOffset = findPos(xGetElementById('selection'));
	theBottomOftheHeaderImage = theHeaderImageOffset[1]+xGetElementById('theHeaderImage').offsetHeight;
	theBottomOfSelection = theSelectionOffset[1]+xGetElementById('selection').offsetHeight-4; //the -4 is just to give some leeway
	if(ev.pageY <= theBottomOftheHeaderImage && ev.pageY > theBottomOfSelection) {
		if(theDiv.offsetHeight+theDiv.offsetTop+newMouseY < theDiv.parentNode.offsetHeight && theDiv.offsetHeight+newMouseY > 75) { //prevent it from becoming too small or going out of bounds
			theDiv.style.height = theDiv.offsetHeight+newMouseY+'px';
			xRemoveEventListener(document,'mousemove',doThis,false); //remove the old event listener
			xAddEventListener(document,'mousemove',doThis = function(e) { resizeThis(theDiv,e,ev.pageY) },false); //and replace it with one that calls a function on the new mouseY
		
			if(theDiv.offsetTop + theDiv.offsetHeight > theDiv.parentNode.offsetHeight) {
				while(theDiv.offsetTop + theDiv.offsetHeight > theDiv.parentNode.offsetHeight) {
					theDiv.style.height = theDiv.offsetHeight-1+'px';
				}
			}
		}
	}
}

function resizeThisImage(resultArray,albumid) {
	
	
	queryString = new Object();
	queryString["function"] = "createThumb";
	queryString["path"] = resultArray[4];
	queryString["width"] = resultArray[1];
	queryString["height"] = resultArray[2];
	queryString["photoid"] = resultArray[3];
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	resizeImage = GetXmlHttpObject();
	resizeImage.open("GET",url,true)
	resizeImage.onreadystatechange = function() {
		if(resizeImage.readyState == 4 && resizeImage.status == 200) {
		}
	}
	resizeImage.send(null);
}

function restoreImage(which) {
	theLI = xGetElementById('photo'+which);

	//remove the confirm delete div and set the display for a.del and the img to block
	for(i=0;i<theLI.childNodes.length;i++) {
		if(theLI.childNodes[i].nodeName == "A" || theLI.childNodes[i].nodeName == "IMG") {
			theLI.childNodes[i].style.display = "block";
		}

		if(theLI.childNodes[i].className == "confirmdelete") {
			theLI.removeChild(theLI.childNodes[i]);
		}
	}

	toggleDelImage(which); //restore the delete image link in the top right corner
}

function restoreThis(which,type) {
	theUL = xGetElementById(type+"id"+which);
	for(i=0;i<theUL.childNodes.length;i++) {
		if(theUL.childNodes[i].nodeName == "LI") {
			theUL.childNodes[i].style.display = "block";
		}
		if(theUL.childNodes[i].className == "confdelete") {
			theUL.removeChild(theUL.childNodes[i]);
		}
	}
}

function retainVisibleUploadDivs(uploadDivs) {
		for(i=0;i<uploadDivs.length;i++) {
			//first check to see if the node still exists after the list has been reloaded
			if(xGetElementById(uploadDivs[i])) {
				if(xGetElementById(uploadDivs[i]).parentNode.id.match(/pageList/)) {
					//if it is a subpage menu - execute toggleSubpages
					toggleSubpages(uploadDivs[i].match(/\d+/))
				} else {
					if(uploadDivs[i].match(/showAlbum/)) {
						//if it is a photoAlbum - execute togglePhotos();
						togglePhotos(uploadDivs[i].match(/\d+/))
					} else {
						//if it is an uploadDiv - execute upload();
						upload(uploadDivs[i].match(/\d+/));
					}
				}
			}
		}
}

function setSelectedOption(theList,theId) {
	
	queryString = new Object();
	queryString["function"] = "getPageId";
	queryString["albumid"] = theId;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	var setOption = GetXmlHttpObject();
	setOption.open("GET",url,true);
	setOption.onreadystatechange = function() {
		if(setOption.readyState == 4 && setOption.status == 200) {
			theValue = setOption.responseText;
			for(i=0;i<theList.childNodes.length;i++) {
				if(theList.childNodes[i].nodeName == "OPTION") {
					if(theList.childNodes[i].value == theValue) {
						theList.childNodes[i].selected = "selected";
						break;
					}
				}
			}
		}
	}
	setOption.send(null);
}

function showPhoto(photoid) {
	var position;
	var ie6;
	if(navigator.appVersion.match(/MSIE 6.0/)) {
		ie6 = true;
		position = "absolute";
		var docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
	} else {
		position = "fixed";
	}
	
	//darken the background
	if(!xGetElementById("darkenBackground")) {
		theDiv = document.createElement("div");
		theDiv.id = "darkenBackground";
		theDiv.style.position = position;
		if(docHeight)
			theDiv.style.height = docHeight + 100 + "px";
		document.body.appendChild(theDiv);
		keypress = null;
		xRemoveEventListener(document, 'keydown', keypress, true);
	}
	
	if(xGetElementById("photoWindowDiv")) { //if the photo window Div is present, then just set the display of the div from that area
		theDivs = xGetElementById("photoWindowDiv").getElementsByTagName("div");
		//set all the divs to display = none, except for the requested one
		
		
		for(i=0;i<theDivs.length;i++) {
			if(theDivs[i].id.match(/photoWindow/)) { //only deal with immediate children divs of photoWindowDiv
				theDivs[i].style.display = "none";
			}
		}
		
		xGetElementById("photoWindow"+photoid).style.display = "block";
		
	} else { //if the photoWindowDiv has not yet been created
		//get the image information from the database
		queryString = new Object();
		queryString["function"] = "showImg";
		queryString["photoid"] = photoid;
	
		url = prepareURL("admin/ajaxCalls.php",queryString);
	
		
		getPhoto = GetXmlHttpObject();
		getPhoto.open("GET",url,true);
		getPhoto.onreadystatechange = function() {
		if(getPhoto.readyState == 4 && getPhoto.status == 200) {
			if(xGetElementById("photoWindow")) {
				xGetElementById("photoWindow").parentNode.removeChild(xGetElementById("photoWindow"));
			}	
			
			photoWindowDiv = document.createElement("div");
			photoWindowDiv.id = "photoWindowDiv";
			document.body.appendChild(photoWindowDiv);
			
			convertToHtml(getPhoto.responseXML.documentElement,photoWindowDiv);
			showPhoto(photoid);
			e=null;

			xAddEventListener(document,'keydown',keypress = swapPhoto,true);
			theDivs = xGetElementById("photoWindowDiv").getElementsByTagName("div");
			for(i=0;i<theDivs.length;i++) {
				if(theDivs[i].className == "photoWindow") {
					if(ie6) {
						theDivs[i].style.marginTop = "0";
					}
					theDivs[i].style.position = position;
				}
			}
			
			}
		}
		getPhoto.send(null);
	}
	
}

function swapPhoto(e) {
	//first determine if the window is open - if it isn't then remove the event listener ond do nothing
	if(xGetElementById("photoWindowDiv") && xGetElementById("darkenBackground")) {
		//get all children elements and make sure one of them is display: block
		theDivs = xGetElementById("photoWindowDiv").getElementsByTagName("div");
		for(i=0;i<theDivs.length;i++) {
			if(theDivs[i].parentNode.id == "photoWindowDiv") {
				if(theDivs[i].style.display == "block") {
					found = theDivs[i];
					break;
				}
			}
		}
		
		if(found) {
			var ev=(!e)?window.event:e;//IE:Moz
			//get the next and previous values
			nextprev = found.getElementsByTagName("span");
			for(i=0;i<nextprev.length;i++) {
				switch(nextprev[i].className) {
					case "next": next = nextprev[i].innerHTML; break;
					case "prev": prev = nextprev[i].innerHTML; break;
				}
			}
			switch(ev.keyCode) {
				case 37: showPhoto(prev); break;
				case 39: showPhoto(next); break;
			}
		}
	} else {
		keypress = null;
		xRemoveEventListener(document, 'keydown', keypress, true);
	}
}

function toggleBooking(unixtime,defaultClass) {

	//get the radio button that is checked to determine what cottage we are dealing with
	cottageDiv = xGetElementById('cottages');
	for(i=0;i<cottageDiv.childNodes.length;i++) {
		if(cottageDiv.childNodes[i].nodeName == "INPUT") {
			if(cottageDiv.childNodes[i].checked == true) {
				cottage = cottageDiv.childNodes[i].value;
			}
		}
	}

	queryString = new Object();
	queryString["function"] = "toggleBooking";
	queryString["unixtime"] = unixtime;
	queryString["cottage"] = cottage;
	
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	changeBooking = GetXmlHttpObject();
	changeBooking.open("GET",url,true);
	changeBooking.onreadystatechange = function() {
		if(changeBooking.readyState == 4 && changeBooking.status == 200) {
			theResponse = changeBooking.responseText;
			if(theResponse == "booked") {
				xGetElementById("bookingCal"+unixtime).className = cottage;
			} else {
				xGetElementById("bookingCal"+unixtime).className = defaultClass;;
			}
			
			//this reference is to refresh the booking calendar in the sidebar - modify this function
			changeCal('booking','','both');
			
		}
	}
	changeBooking.send(null);
}

function toggleDelImage(which) {
	theLI = xGetElementById('photo'+which);
	for(i=0;i<theLI.childNodes.length;i++) {
		if(theLI.childNodes[i].className == "del") {
			if(theLI.childNodes[i].style.visibility == "" || theLI.childNodes[i].style.visibility == "hidden") {
				theLI.childNodes[i].style.visibility = "visible";
			} else {
				theLI.childNodes[i].style.visibility = "hidden";
			}
		}
	}
}

function toggleDiv(id) {
	objectRef = xGetElementById(id);

	if(objectRef.style.display == "none" || objectRef.style.display == "") {
		objectRef.style.display = "block";
	} else {
		//close all the uploadDiv elements in this div
		allChildDivs = objectRef.getElementsByTagName("DIV");
		for(i=0;i<allChildDivs.length;i++) {
			if(allChildDivs[i].className == "uploadDiv" && allChildDivs[i].style.display == "block" ) {
				//perform the appropriate toggle function
				if(allChildDivs[i].id.match(/subpage/)) {
					toggleSubpages(allChildDivs[i].id.match(/\d+/));
				} else {
					if(allChildDivs[i].id.match(/showAlbumId/)) {
						togglePhotos(allChildDivs[i].id.match(/\d+/));
					} else {
						upload(allChildDivs[i].id.match(/\d+/));
					}
				}	
			}
		}
		
		if(objectRef.id == 'eventcal') {
			xGetElementById('eventcalInfo').style.display = "none";
		}
		
		objectRef.style.display = "none";
	}
	
}

function togglePhotos(which) {
	//determine if the showAlbumId+which is already open
	thePhotoListDiv = xGetElementById("showAlbumId"+which);
	
	//get the anchor text
	theUL = xGetElementById("albumid"+which);
	for(i=0;i<theUL.childNodes.length;i++) {
		if(theUL.childNodes[i].className == "pictures") {
			for(m=0;m<theUL.childNodes[i].childNodes.length;m++) {
				if(theUL.childNodes[i].childNodes[m].nodeName == "A") {
					theAnchor = theUL.childNodes[i].childNodes[m];
					break;
				}
			}
			break;
		}
	}
	
	if(thePhotoListDiv.style.display == "" || thePhotoListDiv.style.display == "none") {
		thePhotoListDiv.style.display = "block";
		theAnchor.innerHTML = "Hide Photos";

		//determine if the upload div is open - if it is, then perform upload(which) to toggle the upload div
		theUploadDiv = xGetElementById("uploadAlbumId"+which);
		if(theUploadDiv.style.display == "block") {
			upload(which);
		}
		
	} else {
		thePhotoListDiv.style.display = "none";
		
		//get the number of images in this album
		getNumImages(which);
	}

}

function toggleSubpages(id) {
	//1 - get the display property of the subpage div for this id
	//2 - if it is none, then display it and change the link to "Hide Subpages"
	//3 - if it is display, then change it to none and change the link back to # Subpages - requires AJAX

	//get the link
	theUL = xGetElementById("pageid"+id);
	for(i=0;i<theUL.childNodes.length;i++) {
	 if(theUL.childNodes[i].className == "subpages") {
	  for(m=0;m<theUL.childNodes[i].childNodes.length;m++) {
	   if(theUL.childNodes[i].childNodes[m].nodeName == "A") {
	    theAnchor = theUL.childNodes[i].childNodes[m];
		break;
	   }
	  }
	  break;
	 }
	}

	if(xGetElementById("subpage"+id)) {
		theSubpageDiv = xGetElementById("subpage"+id);
		if(theSubpageDiv.style.display == "none" || theSubpageDiv.style.display == "") {
			theSubpageDiv.style.display = "block";
				theAnchor.innerHTML = "Hide Subpages";
		} else {
			theSubpageDiv.style.display = "none";
			
			queryString = new Object();
			queryString["function"] = "subpages";
			queryString["parentid"] = id;
			
			url = prepareURL("admin/ajaxCalls.php",queryString);
			numSubpages = GetXmlHttpObject();
			numSubpages.open("GET",url,true);
			numSubpages.onreadystatechange = function() {
				if(numSubpages.readyState == 4 && numSubpages.status == 200) {
					theNumber = numSubpages.responseText;
					if(theNumber == 1) { pages = "subpage"; } else { pages = "subpages"; }
					theAnchor.innerHTML = theNumber + " " + pages;
				}
			}
			numSubpages.send(null);
		}
	}
}

function toggleVisibility(id) {
	
	queryString = new Object();
	queryString["function"] = "visibility";
	queryString["pageid"] = id;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	theNewVisibility = GetXmlHttpObject();
	theNewVisibility.open("GET",url,true);
	theNewVisibility.onreadystatechange = function() {
		if(theNewVisibility.readyState == 4 && theNewVisibility.status == 200) {
			//reload the menu
			reloadMenu();
			
			//reload the pageList
			reloadDiv('pageList',theNewVisibility.responseXML.documentElement)
		}
	}
	theNewVisibility.send(null);

}

function updatePhotos(start,albumid) {
	//reload the pagination links
	reloadPagination(start,albumid);
	//remove all the contents of all the LI tags in #album
	theLIs = xGetElementById("album").getElementsByTagName("li");
	for(i=0;i<theLIs.length;i++) {
		theLIs[i].innerHTML = "";
		if(i == 4) {
			//set the innerHTML to a loading image
			theLoadingImg = document.createElement("img");
			theLoadingImg.src = "images/admin/uploading.gif";
			theLoadingImg.style.margin = "60px auto 10px auto";
			theLoadingImg.style.borderWidth = "0";
			theLoadingImg.style.padding = "0";
			theLoadingImg.width = "50";
			theLoadingImg.height = "50";
			theLoadingImg.alt = "Uploading Image";
			theLIs[i].appendChild(theLoadingImg);
			
			loadingSpan = document.createElement("span");
			loadingSpan.innerHTML = "Loading...";
			loadingSpan.style.display = "block";
			loadingSpan.style.width = "100%";
			loadingSpan.style.textAlign = "center";
			
			theLIs[i].appendChild(loadingSpan);
		}
	}
	
	//make an ajax call and populate each LI in #album with the list of XML images returned from the ajax call
	queryString = new Object();
	queryString["function"] = "getImages";
	queryString["start"] = start;
	queryString["albumid"] = albumid;
	
	url = prepareURL("admin/ajaxCalls.php",queryString);
	
	loadImages = GetXmlHttpObject();
	loadImages.open("GET",url,true);
	loadImages.onreadystatechange = function() {
		if(loadImages.readyState == 4 && loadImages.status == 200) {
			//remove the old list
			xGetElementById("album").parentNode.removeChild(xGetElementById("album"));
			
			//put all the LIs in the list
			convertToHtml(loadImages.responseXML.documentElement,xGetElementById("photoalbum"));
		}
	}
	loadImages.send(null);
	
	
}

function upload(which) {

	theDiv = xGetElementById("uploadAlbumId"+which);

	if(theDiv.style.display == "none" || theDiv.style.display == "") {
		theDiv.style.display = "block";
		newAnchorText = "Hide Upload";

		//determine if the upload div is open - if it is, then perform upload(which) to toggle the upload div
		theShowAlbumDiv = xGetElementById("showAlbumId"+which);
		if(theShowAlbumDiv.style.display == "block") {
			togglePhotos(which);
		}

	} else {
		theDiv.style.display = "none";
		newAnchorText = "Upload Images";
	}

	//change the link text
	theUL = xGetElementById("albumid"+which);
	for(i=0;i<theUL.childNodes.length;i++) {
		if(theUL.childNodes[i].className == "upload") {
			newAnchor = document.createElement("a");
			newAnchor.innerHTML = newAnchorText;
			newAnchor.href = "javascript:upload('"+which+"');";

			while(theUL.childNodes[i].hasChildNodes() == true) {
				theUL.childNodes[i].removeChild(theUL.childNodes[i].firstChild);
			}

			theUL.childNodes[i].appendChild(newAnchor);
		}
	}
}

function uploadThis(which,type) {
	if(type == "photo") {
		theUploadDiv = xGetElementById('uploadAlbumId'+which);
	} else {
		theUploadDiv = xGetElementById('headerImageUpload');
	}

	//remove the loading div that is currently there
	for(i=0;i<theUploadDiv.childNodes.length;i++) {
		if(theUploadDiv.childNodes[i].className == "loading") {
			theUploadDiv.removeChild(theUploadDiv.childNodes[i]);
			break;
		}
	}

	//add the loading div dynamically to theUploadDiv
	theLoadingDiv = document.createElement("div");
	theLoadingDiv.id = "uploading"+which;
	theLoadingDiv.className = "loading";

	theLoadingImg = document.createElement("img");
	theLoadingImg.src = "images/admin/uploading.gif";
	theLoadingImg.width = "30";
	theLoadingImg.height = "30";
	theLoadingImg.alt = "Uploading Image";
	theLoadingImg.className = "uploading";

	theText = document.createElement("strong");
	theText.innerHTML = "Uploading image...";

	theLoadingDiv.appendChild(theLoadingImg);
	theLoadingDiv.appendChild(theText);

	//put theLoadingDiv in theUploadDiv
	theUploadDiv.insertBefore(theLoadingDiv,theUploadDiv.firstChild.nextSibling);
	
}

/*DEBUGGING TOOL
	uncomment the code below to append a history div in the upper left corner of the page
	send alerts to it using the syntax -> addHistory('my alert here');
	
		debugging historyDiv - remove when finished
		historyDiv = document.createElement("div");
		historyDiv.id = "historyDiv";
		historyDiv.style.position = "fixed";
		historyDiv.style.overflow = "auto";
		historyDiv.style.height = "100px";
		historyDiv.style.width = "550px";
		historyDiv.style.top = "0";
		historyDiv.style.left = "0";
		historyDiv.style.border = "1px solid #600";
		historyDiv.style.backgroundColor = "#c93";
		document.body.appendChild(historyDiv);
END DEBUGGING TOOL*/