String.prototype.toProperCase = function() {
	return this.toLowerCase().replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
}

Array.prototype.contains = function(s) {
	for (i = 0; i < this.length; ++i)
		if (this[i] === s) return true;
	return false;
}
Number.prototype.round2 = function() {
	return Math.round(this * 100) / 100;
}

var IE = (navigator.appName == "Microsoft Internet Explorer");
var admin = false;

function opacity(node, value) {
	var filter = "alpha(opacity=" + value + ")";
	if (value > 0) {
		node.style.visibility = "visible";
	} else {
		node.style.visibility = "hidden";
	}
	if (value == 0 || value == 100) {
		filter = "";
	}
	if (IE) {
		node.style.filter = filter;
		var children = node.getElementsByTagName("*");
		for (var i = 0; i < children.length; i++) {
			children[i].style.filter = filter;
		}
	} else {
		node.style.opacity = value / 100;
	}
}

function fade(node, range, step, dispose) {
	if (node.getAttribute("fadeID") != null) {
		window.clearInterval(node.getAttribute("fadeID"));
		range[0] = parseInt(node.getAttribute("fadeValue"));
	}
	node.style.display = "";
	if (range[0] > range[1]) {
		step = -Math.abs(step);
	}
	node.setAttribute("fadeID", window.setInterval(function () {
		node.setAttribute("fadeValue", range[0]);
		if (Math.abs(range[0] - range[1]) < Math.abs(step)) {
			window.clearInterval(node.getAttribute("fadeID"));
			node.removeAttribute("fadeID");
			opacity(node, range[1]);
			if (dispose) {
				node.style.display = "none";
			}
			return;
		} else {
			opacity(node, range[0]);
			range[0] += step;
		}
	}, 10));
}

function getElementsByClass(searchClass, domNode, tagName) {
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = "*";
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " " + searchClass + " ";
	for(i = 0, j = 0; i < tags.length; ++i) {
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
			el[j++] = tags[i];
	}
	return el;
}

function getChildValue(parentNode, childName, defaultValue) {
	if (defaultValue == null) defaultValue = "";
	var children = parentNode.getElementsByTagName(childName);
	try {
		defaultValue = children[0].firstChild.data;
	} catch (error) {
	}
	return defaultValue;
}

function insertText(control, value) {
	if (document.selection) {
		control.focus();
		var range = document.selection.createRange();
		range.text = value;
	} else if (control.selectionStart) {
		var start = control.selectionStart;
		var end = control.selectionEnd;
		control.value = control.value.substring(0, start) + value + control.value.substring(end, control.value.length);
		control.selectionStart = start;
		control.selectionEnd = start + value.length;
	} else {
		control.value += value;
	}
	control.focus();
}

function getXMLString(xy, _str) {
	var str = (_str == undefined) ? '' : _str;
	if (xy.nodeValue == undefined) {
		var multiStr=[], temp = '';
		for (var i = 0; i < xy.childNodes.length; ++i) {
			if (xy.childNodes[i].nodeName.toString().indexOf('#') < 0) {
				var nodeNameStart = '<' + xy.childNodes[i].nodeName, nodeNameEnd ='</' + xy.childNodes[i].nodeName + '>';
				var attsStr = ' ', atts = xy.childNodes[i].attributes;
				if (atts != undefined) {				
					for (var j = 0; j < atts.length; ++j)
						attsStr += atts[j].nodeName + '="' + atts[j].firstChild.nodeValue + '"';
				}
				temp = nodeNameStart + ((attsStr == ' ') ? '' : attsStr) + '>' + getXMLString(xy.childNodes[i], str) + nodeNameEnd;
				multiStr.push(temp);
				str = temp;
			} else {
				str = getXMLString(xy.childNodes[i], str);
				multiStr.push(str);
			}
		}
		str = multiStr.join('');
	} else {
	   return xy.nodeValue;
	}
	return str;
}

function XMLStream() {
	var xmlhttp, bComplete = false;
	if (window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (error) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (error) {
				return false;
			}
		}
	} else if (window.XMLHttpRequest) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (error) {
			return false;
		}
		xmlhttp.overrideMimeType("text/xml");
	}
	
	this.connect = function(sURL, sMethod, sRequest, fDone, pid) {
		bComplete = false;
		sMethod = sMethod.toUpperCase();
		try {
			if (sMethod == "GET") {
				xmlhttp.open(sMethod, sURL+"?"+sRequest, true);
				try {
					xmlhttp.setRequestHeader("Content-Type","text/xml");
					xmlhttp.setRequestHeader("Cache-Control", "no-cache");
				} catch (error) {};
				sRequest = null;
			} else {
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader("Connection", "close");
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && !bComplete) {
					if (xmlhttp.status == 200) {
						bComplete = true;
						var xmldoc = xmlhttp.responseXML;
						if (!xmldoc.documentElement && xmlhttp.responseStream) {
							xmldoc.load(xmlhttp.responseStream);
						}
						fDone(xmldoc, pid);
					}
				}
			};
		xmlhttp.send(sRequest);
		} catch (z) {
			return false;
		}
		return true;
	};
	return this;
}

var menu = new Object();
menu.meat = Array("Chicken Breast", "Salmon", "Hamburger", "Hotdog");
menu.salad = Array("Green Leaf Salad", "Potato Salad", "Pasta Salad", "Broccoli Salad");
menu.dessert = Array("Pumpkin Pie", "Apple Pie", "Sweet Potato Pie");

var sounds = new Object();
sounds.files = Array("graveyard_wind.wav", "sleigh_bells.wav", "turkey_gobble.wav");
sounds.stats = Array(1/5, 1/3, 1/2);

var tabs = new Array("perspective", "events", "gallery", "discussion", "registration");
var tools = new Array("setup", "notify", "report");
var where = new Array("address", "city", "state", "zip", "country", "map");
var variables = new Array("Guest Name:$guest_name", "Guest ID:$gid", "Guest Login URL:$login_url", "Host Name:$host_name", "Host Address:$host_address", "Host City:$host_city", "Host State:$host_state", "Host Zip:$host_zip", "Host Phone:$host_phone", "Host Map Link:$host_map", "Coordinator Name:$coordinator_name", "Coordinator Email Address:$coordinator_email", "Coordinator Phone:$coordinator_phone");
var encoded = { map: "b64" };
var clearResponse = true;
var timerID = new Array();
var today = new Date;
var track = "";
var timer;
var galleryX;
var deltaX = 0;
var dragging = false;

timerID[0] = 0;
timerID[1] = 0;

window.onload = function() {
	admin = (document.getElementById("tools") != null);
	var navigation = document.getElementById("navigation");
	for (i = 0; i < tabs.length; ++i) {
		var tab = document.createElement("img");
		tab.id = (tabs[i] != "empty") ? "_" + tabs[i] : "";
		tab.src = "images/tab_blank.png";
		tab.style.background = "transparent url(images/tab_" + tabs[i] + ".png) no-repeat top";
		tab.setAttribute("alt", tabs[i]);
		navigation.appendChild(tab);
		var selected = document.createElement("img");
		selected.src = "images/selected_" + tabs[i] + ".png";
	}
	var dates = getElementsByClass("date", document, "img");
	for (i = 0; i < dates.length; ++i) {
		dates[i].src = "images/date_blank.png";
		dates[i].style.backgroundImage = "url(images/" + dates[i].id + ".png)";
		var strDate = String(dates[i].id).substring(5);
		var objDate = new Date(strDate.substr(0, 4) * 1, (strDate.substr(4, 2) * 1) - 1, strDate.substr(6, 2) * 1);
		if (objDate < today) {
			dates[i].style.backgroundPosition = "top";
		} else {
			dates[i].style.backgroundPosition = "bottom";
		}
	}
	updateDiscussion();
	updateRegistration();
	if (admin) {
		fade(document.getElementById("_setup"), [100, 50], 1, false);
		fade(document.getElementById("_notify"), [100, 50], 1, false);
		fade(document.getElementById("_report"), [100, 50], 1, false);
		var nSelect = document.getElementById("variables");
		for (i = 0; i < variables.length; i++) {
			var values = variables[i].split(":");
			var nOption = document.createElement("option");
			nOption.setAttribute("value", values[1]);
			var nText = document.createTextNode(values[0]);
			nOption.appendChild(nText);
			nSelect.appendChild(nOption);
		}
	}
	timerID[0] = setTimeout("getAudio()", Math.round(45000 * Math.random()) + 45000);
};

function getAudio() {
	if (timerID[0])
		clearTimeout(timerID[0]);
	var n = Math.random();
	var x = 1;
	var j;
	for (i = 0; i < sounds.files.length; i++) {
		d = Math.abs(n - sounds.stats[i]);
		if (d < x && track != sounds.files[i]) {
			j = i;
			x = d;
		}
	}
	track = sounds.files[j];
	document.getElementById("audio").innerHTML = "<embed type='audio/wav' src='audio/" + track + "' controller='false' hidden='true' loop='false' autoplay='false' autostart='true' pluginspage='http://www.apple.com/quicktime/download/' />";
	timerID[0] = setTimeout("getAudio()", Math.round(45000 * (1 - n)) + 45000);
	updateDiscussion();
}

function getBounty() {
	if (timerID[1])
		clearTimeout(timerID[1]);
	var food = new XMLStream();
	food.connect("menu.php", "GET", "year=" + today.getFullYear(), function(oXML, sPID) {
		var bounty = oXML.getElementsByTagName("bounty");
		track = bounty[0].firstChild.data;
		document.getElementById("bounty").innerHTML = bounty[0].firstChild.data;
		timerID[1] = setTimeout("getBounty()", 30000);
	}, 1);
}

function navEvent(e) {
	if (e.target) {
		var node = e.target;
	} else {
		var e = window.event
		var node = e.srcElement;
	}
	if (node.id == "tools" || node.id == "navigation") { return false; }
	switch (e.type) {
		case "mouseover":
			if (node.parentNode.id == "tools") {
				fade(node, [50, 100], 5, false);
			} else {
				node.style.backgroundPosition = "bottom";
			}
			break;
		case "mouseout":
			if (node.parentNode.id == "tools") {
				fade(node, [100, 50], 5, false);
			} else {
				node.style.backgroundPosition = "top";
			}
			break;
		case "click":
			var header = document.getElementById("header");
			var splash = document.getElementById("splash");
			var special = document.getElementById("special");
			var welcome = document.getElementById("welcome");
			if (node.id == "splash") {
				splash.src = "images/splash_" + today.getFullYear() + ".png";
				special.style.display = "block";
				header.style.height = "460px";
				welcome.style.display = "block";
			} else {
				splash.src = "images/header_" + today.getFullYear() + ".png";
				special.style.display = "none";
				header.style.height = "240px";
				welcome.style.display = "none";
			}
			var tabID = String(node.id).substring(1);
			var tabNodes = document.getElementById("navigation").childNodes;
			if (tabID != "") {
				if (admin) {
					switch (tabID) {
						case "setup":
							try {
								updateSetup();
							} catch (error) {
							}
							break;
						case "notify":
							try {
								if (document.getElementById("recipients").childNodes.length <= 1) {
									updateSetup();
								}
								updateNotify();
							} catch (error) {
							}
							break;
						case "report":
							try {
								updateReport();
							} catch (error) {
							}
							break;
					}
					for (i = 0; i < tools.length; ++i) {
						var div = document.getElementById(tools[i]);
						if (tools[i] == tabID) {
							try {
								div.style.display = "block";
							} catch (error) {
							}
						} else {
							try {
								div.style.display = "none";
							} catch (error) {
							}
						}
					}
				}
				for (i = 0; i < tabs.length; ++i) {
					var div = document.getElementById(tabs[i]);
					if (tabs[i] == tabID) {
						try {
							div.style.display = "block";
						} catch (error) {
						}
						node.src = "images/selected_" + tabID + ".png";
					} else {
						try {
							div.style.display = "none";
						} catch (error) {
						}
						tabNodes[i].src = "images/tab_blank.png";
					}
				}
			}
			break;
	}
}

function eventsEvent(e) {
	if (e.target) {
		var node = e.target;
	} else {
		var e = window.event
		var node = e.srcElement;
	}
	var d = String(node.id).substring(5);
	switch (e.type) {
		case "mouseover":
			break;
			document.getElementById("event_" + d).style.display = "block";
			node.style.backgroundPosition = "bottom";
		case "click":
			var dates = getElementsByClass("date", node.parentNode, "img");
			for (i = 0; i < dates.length; ++i) {
				var strDate = String(dates[i].id).substring(5);
				var objDate = new Date(strDate.substr(0, 4) * 1, (strDate.substr(4, 2) * 1) - 1, strDate.substr(6, 2) * 1);
				if (strDate != d) {
					dates[i].style.backgroundPosition = "top";
					document.getElementById("event_" + strDate).style.display = "none";
				} else {
					dates[i].style.backgroundPosition = "bottom";
					document.getElementById("event_" + d).style.display = "block";
				}
			}
			break;
	}
}

function galleryLeft() {
	timer = setInterval("document.getElementById('pictures').scrollLeft -= 2", 15);
}

function galleryRight() {
	timer = setInterval("document.getElementById('pictures').scrollLeft += 2", 15);
}

function galleryEvent(e) {
	if (e.target) {
		var node = e.target;
	} else {
		var e = window.event
		var node = e.srcElement;
	}
	switch (e.type) {
		case "mouseover":
			switch (node.id) {
				case "arrow_left": {
					//galleryLeft();
					break;
				}
				case "arrow_right": {
					//galleryRight();
					break;
				}
			}
			break;
		case "mouseout":
			if (node.className == "arrow")
				clearInterval(timer);
			break;
		case "mousedown":
			if (node.className == "picture") {
				galleryX = e.pageX ? e.pageX : e.clientX;
				node.style.cursor = "move";
				dragging = true;
			}
			break;
		case "mouseup":
			if (node.className == "picture" && deltaX != 0) {
				node.style.cursor = "default";
				dragging = false;
				deltaX = 0;
			}
			break;
		case "mousemove":
			if (dragging) {
				var tempX = e.pageX ? e.pageX : e.clientX;
				deltaX = galleryX - tempX;
				document.getElementById('pictures').scrollLeft += deltaX;
				galleryX = tempX;
			}
			break;
		case "selectstart":
		case "dragstart":
			if (e.preventDefault) {
			  e.preventDefault();
			  e.stopPropagation();
			} else {
			  e.returnValue = false;
			  e.cancelBubble = true;
			}
			break;
		case "click":
			switch (node.className) {
				case "date":
					var d = String(node.id).substring(5);
					var gallery = new XMLStream();
					gallery.connect("gallery.php", "GET", "date=" + d, function(oXML, sPID) {
						var pictures = oXML.getElementsByTagName("pictures");
						document.getElementById("pictures").innerHTML = pictures[0].firstChild.data;
						var dates = getElementsByClass("date", node.parentNode, "img");
						for (i = 0; i < dates.length; ++i) {
							var strDate = String(dates[i].id).substring(5);
							var objDate = new Date(strDate.substr(0, 4) * 1, (strDate.substr(4, 2) * 1) - 1, strDate.substr(6, 2) * 1);
							if (objDate < today) {
								dates[i].style.backgroundPosition = "top";
							} else {
								dates[i].style.backgroundPosition = "bottom";
							}
						}
						node.style.backgroundPosition = "bottom";
					}, 2);
			}
			break;
		case "dblclick":
			if (node.className == "picture") {
				window.open(String(node.src).replace(".jpg", ".png"), "_blank", "directories=no,location=no,menubar=no,toolbar=no");
			}
			break;
	}
}

function removeChildren(id) {
	var parent = document.getElementById(id);
	if (parent.hasChildNodes()) {
		while (parent.childNodes.length >= 1)
			parent.removeChild(parent.firstChild);
	}
}

function remove(e) {
	if (e.target) {
		var node = e.target;
	} else {
		var e = window.event
		var node = e.srcElement;
	}
	switch (node.name) {
		case "item.remove":
			var nTR = node.parentNode.parentNode;
			var item = nTR.parentNode;
			item.removeChild(nTR);
			break;
		case "guest.remove":
			var nTR = node.parentNode.parentNode;
			var guest = nTR.parentNode;
			guest.removeChild(nTR);
			break;
	}
}

function getRemoveButton(object) {
	try {
		var nIMG = document.createElement('<img name="' + object + '.remove" class="button inactive" src="images/button_remove.png" onclick="remove(event)" />');
	} catch (error) {
		var nIMG = document.createElement("img");
		nIMG.name = object + ".remove";
		nIMG.className = "button inactive";
		nIMG.src = "images/button_remove.png";
		if (document.attachEvent) {
			nIMG.attachEvent("onclick", remove(event));
		} else {
			nIMG.setAttribute("onclick", "remove(event)");
		}
	}
	return nIMG;
}

function addItem(type, option, value) {
	switch (type) {
		case "provision":
		case "standard":
		case "optional":
			var items = document.getElementById("provision");
			break;
		default:
			var items = document.getElementById("consumption");
			break;
	}
	var index = items.childNodes.length;
	var color = (index % 2 == 0) ? "#eaeaea" : "#f2f2f2"
	var nTD = new Array();
	try {
		var nTR = document.createElement('<tr name="' + type + '">');
	} catch (error) {
		var nTR = document.createElement("tr");
		nTR.name = type;
	}
	nTD[0] = document.createElement("td");
	nTR.appendChild(nTD[0]);
	if (type != "provision")
		nTD[0].appendChild(getRemoveButton("item"));
	nTD[1] = document.createElement("td");
	nTR.appendChild(nTD[1]);
	if (type == "provision" || type == "standard") {
		var nText = document.createTextNode(value);
		nTD[1].appendChild(nText);
		nTD[1].style.color = "#111";
		if (type == "provision") {
			nTD[1].style.backgroundColor = color;
		}
		nTD[1].style.textAlign = "right";
		nTD[1].style.paddingTop = "4px";
		nTD[1].style.paddingBottom = "4px";
		nTD[1].style.paddingRight = "4px";
		nTD[1].style.borderRight = "2px solid #fff";
	} else {
		try {
			var nInput = document.createElement('<input name="' + type + '.count" class="short" type="text" value="' + value + '" />');
		} catch (error) {
			var nInput = document.createElement("input");
			nInput.name = type + ".count";
			nInput.className = "short";
			nInput.type = "text";
			nInput.value = value;
		}
		nTD[1].appendChild(nInput);
	}
	nTD[2] = document.createElement("td");
	nTR.appendChild(nTD[2]);
	if (type == "provision") {
		var nText = document.createTextNode(option);
		nTD[2].appendChild(nText);
		nTD[2].style.color = "#111";
		nTD[2].style.backgroundColor = color;
		nTD[2].style.paddingTop = "4px";
		nTD[2].style.paddingBottom = "4px";
		nTD[2].style.paddingLeft = "4px";
	} else {
		switch (type) {
			case "standard":
				var nSelect = document.createElement("select");
				for (var key in menu) {
					try {
						var nGroup = document.createElement('<optgroup label="' + key.toProperCase() + '">');
					} catch (error) {
						var nGroup = document.createElement("optgroup");
						nGroup.label = key.toProperCase();
					}
					nSelect.appendChild(nGroup);
					for (i = 0; i < menu[key].length; i++) {
						var nOption = document.createElement("option");
						nOption.setAttribute("value", menu[key][i]);
						var nText = document.createTextNode(menu[key][i]);
						nOption.appendChild(nText);
						if (menu[key][i] == option)
							nOption.setAttribute("selected", true);
						nSelect.appendChild(nOption);
					}
				}
				nTD[2].appendChild(nSelect);
				break;
			case "optional":
				try {
					var nInput = document.createElement('<input name="' + type + '.count" class="large" type="text" value="' + option + '" />');
				} catch (error) {
					var nInput = document.createElement("input");
					nInput.name = type + ".count";
					nInput.className = "large";
					nInput.type = "text";
					nInput.value = option;
				}
				nTD[2].appendChild(nInput);
				break;
			default:
				try {
					var nSelect = document.createElement('<select name="' + type + '.name">');
				} catch (error) {
					var nSelect = document.createElement("select");
					nSelect.name = type + ".name";
				}
				for (i = 0; i < menu[type].length; i++) {
					var nOption = document.createElement("option");
					nOption.setAttribute("value", menu[type][i]);
					var nText = document.createTextNode(menu[type][i]);
					nOption.appendChild(nText);
					if (menu[type][i] == option)
						nOption.setAttribute("selected", true);
					nSelect.appendChild(nOption);
				}
				nTD[2].appendChild(nSelect);
				break;
		}
	}
	items.appendChild(nTR);
}

function addGuest(role, id, name, email, note) {
	var guests = document.getElementById("guests");
	var nTD = new Array();
	var nTR = document.createElement('tr');
	nTD[0] = document.createElement("td");
	nTR.appendChild(nTD[0]);
	if (role == "Guest")
		nTD[0].appendChild(getRemoveButton("guest"));
	nTD[1] = document.createElement("td");
	nTR.appendChild(nTD[1]);
	if (role == "Administrator") {
		var nText = document.createTextNode(name);
		nTD[1].appendChild(nText);
		nTD[1].style.color = "#111";
		nTD[1].style.backgroundColor = "#eee";
		nTD[1].style.paddingTop = "4px";
		nTD[1].style.paddingBottom = "4px";
		nTD[1].style.paddingLeft = "4px";
		nTD[1].style.borderRight = "2px solid #fff";
	} else {
		try {
			var nInput = document.createElement('<input name="id" type="hidden" value="' + id + '" />');
		} catch (error) {
			var nInput = document.createElement("input");
			nInput.name = "id";
			nInput.type = "hidden";
			nInput.value = id;
		}
		nTD[1].appendChild(nInput);
		try {
			var nInput = document.createElement('<input name="name" class="longer" type="text" value="' + name + '" />');
		} catch (error) {
			var nInput = document.createElement("input");
			nInput.name = "name";
			nInput.className = "longer";
			nInput.type = "text";
			nInput.value = name;
		}
		nTD[1].appendChild(nInput);
	}
	nTD[2] = document.createElement("td");
	nTR.appendChild(nTD[2]);
	if (role == "Administrator") {
		var nText = document.createTextNode(email);
		nTD[2].appendChild(nText);
		nTD[2].style.color = "#111";
		nTD[2].style.backgroundColor = "#eee";
		nTD[2].style.paddingTop = "4px";
		nTD[2].style.paddingBottom = "4px";
		nTD[2].style.paddingLeft = "4px";
		nTD[2].style.borderRight = "2px solid #fff";
	} else {
		try {
			var nInput = document.createElement('<input name="email" class="large" type="text" value="' + email + '" />');
		} catch (error) {
			var nInput = document.createElement("input");
			nInput.name = "email";
			nInput.className = "large";
			nInput.type = "text";
			nInput.value = email;
		}
		nTD[2].appendChild(nInput);
	}
	nTD[3] = document.createElement("td");
	nTR.appendChild(nTD[3]);
	if (role == "Administrator") {
		var nText = document.createTextNode(note);
		nTD[3].appendChild(nText);
		nTD[3].style.color = "#111";
		nTD[3].style.backgroundColor = "#eee";
		nTD[3].style.paddingTop = "4px";
		nTD[3].style.paddingBottom = "4px";
		nTD[3].style.paddingLeft = "4px";
		nTD[3].style.borderRight = "2px solid #fff";
	} else {
		try {
			var nInput = document.createElement('<input name="note" class="longer" type="text" value="' + note + '" />');
		} catch (error) {
			var nInput = document.createElement("input");
			nInput.name = "note";
			nInput.className = "longer";
			nInput.type = "text";
			nInput.value = note;
		}
		nTD[3].appendChild(nInput);
	}
	guests.appendChild(nTR);
}

function addRecipient(name) {
	var recipients = document.getElementById("recipients");
	var nTD = new Array();
	var nTR = document.createElement('tr');
	nTD[0] = document.createElement("td");
	nTR.appendChild(nTD[0]);
	try {
		var nInput = document.createElement('<input name="recipient.send" type="checkbox" />');
	} catch (error) {
		var nInput = document.createElement("input");
		nInput.name = "send";
		nInput.type = "checkbox";
	}
	nTD[0].appendChild(nInput);
	nTD[1] = document.createElement("td");
	nTR.appendChild(nTD[1]);
	try {
		var nInput = document.createElement('<input name="recipient.name" class="longer" type="text" value="' + name + '" disabled="disabled" />');
	} catch (error) {
		var nInput = document.createElement("input");
		nInput.name = "recipient.name";
		nInput.className = "longer";
		nInput.type = "text";
		nInput.value = name;
		nInput.disabled = "disabled";
	}
	nTD[1].appendChild(nInput);
	nTD[2] = document.createElement("td");
	nTR.appendChild(nTD[2]);
	try {
		var nSelect = document.createElement('<select name="recipient.history">');
	} catch (error) {
		var nSelect = document.createElement("select");
		nSelect.name = "recipient.name";
	}
	nTD[2].appendChild(nSelect);
	nTD[3] = document.createElement("td");
	nTR.appendChild(nTD[3]);
	try {
		var nInput = document.createElement('<input name="recipient.private" class="large" type="text" value="" />');
	} catch (error) {
		var nInput = document.createElement("input");
		nInput.name = "recipient.private";
		nInput.className = "large";
		nInput.type = "text";
		nInput.value = "";
	}
	nTD[3].appendChild(nInput);
	recipients.appendChild(nTR);
}

function addRegistration(name, count, values) {
	var nTR = new Array();
	var nTD = new Array();
	var items;
	if (name == null) {
		items = document.getElementById("unregistered");
	} else {
		items = document.getElementById("registered");
		nTR[0] = document.createElement("tr");
		nTD[0] = document.createElement("td");
		nTR[0].appendChild(nTD[0]);
		var nText = document.createTextNode(name + " (" + count.getAttribute("adult") + ":" + parseInt("0" + count.getAttribute("child")) + ":" + parseInt("0" + count.getAttribute("other")) + ")");
		nTD[0].appendChild(nText);
		nTD[0].style.color = "#111";
		nTD[0].style.textAlign = "left";
		nTD[0].style.fontWeight = "bold";
		nTD[0].style.padding = "4px";
		nTD[0].setAttribute("colspan", "5");
		items.appendChild(nTR[0]);
	}
	for (var key in values) {
		var consumption = (values[key].getAttribute("type") == "consumption");
		var servings = parseFloat(values[key].firstChild.data);
		nTR.push(document.createElement("tr"));
		iTR = nTR.length - 1;
		var color = (iTR % 2 == 0) ? "#f2f2f2" : "#eaeaea";
		nTD.push(document.createElement("td"));
		iTD = nTD.length - 1;
		nTR[iTR].appendChild(nTD[iTD]);
		var nText = document.createTextNode(consumption ? "-" : "+");
		nTD[iTD].appendChild(nText);
		nTD[iTD].style.color = "#111";
		nTD[iTD].style.textAlign = "center";
		nTD[iTD].style.padding = "4px";
		nTD.push(document.createElement("td"));
		iTD++;
		nTR[iTR].appendChild(nTD[iTD]);
		var nText = document.createTextNode(values[key].getAttribute("name").replace(/_/g, " "));
		nTD[iTD].appendChild(nText);
		nTD[iTD].style.color = "#111";
		nTD[iTD].style.backgroundColor = color;
		nTD[iTD].style.textAlign = "left";
		nTD[iTD].style.padding = "4px";
		nTD.push(document.createElement("td"));
		iTD++;
		nTR[iTR].appendChild(nTD[iTD]);
		var nText = document.createTextNode(servings.round2());
		nTD[iTD].appendChild(nText);
		nTD[iTD].style.color = "#111";
		nTD[iTD].style.backgroundColor = color;
		nTD[iTD].style.textAlign = "center";
		nTD[iTD].style.padding = "4px";
		if (consumption || servings == 0 || !values[key].hasAttribute("portion")) {
			nTD.push(document.createElement("td"));
			iTD++;
			nTR[iTR].appendChild(nTD[iTD]);
			var nText = document.createTextNode("");
			nTD[iTD].appendChild(nText);
			nTD[iTD].style.color = "#111";
			nTD[iTD].style.backgroundColor = color;
			nTD[iTD].style.textAlign = "center";
			nTD[iTD].style.padding = "4px";
			nTD[iTD].setAttribute("colspan", "2");
		} else {
			nTD.push(document.createElement("td"));
			iTD++;
			nTR[iTR].appendChild(nTD[iTD]);
			var nText = document.createTextNode(parseFloat(values[key].getAttribute("portion")).round2());
			nTD[iTD].appendChild(nText);
			nTD[iTD].style.color = "#111";
			nTD[iTD].style.backgroundColor = color;
			nTD[iTD].style.textAlign = "center";
			nTD[iTD].style.padding = "4px";
			nTD.push(document.createElement("td"));
			iTD++;
			nTR[iTR].appendChild(nTD[iTD]);
			var nText = document.createTextNode(parseFloat(values[key].getAttribute("weight")).round2());
			nTD[iTD].appendChild(nText);
			nTD[iTD].style.color = "#111";
			nTD[iTD].style.backgroundColor = color;
			nTD[iTD].style.textAlign = "center";
			nTD[iTD].style.padding = "4px";
		}
		items.appendChild(nTR[iTR]);
	}
}

function register() {
	var members = document.getElementById("members").getElementsByTagName("input");
	var data = "count=" + members[0].value + ":" + members[1].value + ":" + members[2].value + "&";
	
	var consumption = new Object();
	consumption.servings = document.getElementById("consumption").getElementsByTagName("input");
	consumption.items = document.getElementById("consumption").getElementsByTagName("select");
	consumption.list = new Object();
	for (i = 0; i < consumption.items.length; ++i) {
		if (consumption.list[consumption.items[i].value]) {
			consumption.list[consumption.items[i].value] += consumption.servings[i].value * 1;
		} else {
			consumption.list[consumption.items[i].value] = consumption.servings[i].value * 1;
		}
	}
	for (var key in consumption.list)
		data += "consumption:" + key + "=" + consumption.list[key] + "&";
	
	var standard = new Object();
	standard.items = document.getElementById("provision").getElementsByTagName("select");
	standard.list = new Object();
	for (i = 0; i < standard.items.length; ++i)
		standard.list[standard.items[i].value] = "TBD";
	for (var key in standard.list)
		data += "standard:" + key + "=TBD&";
	
	var optional = new Object();
	optional.items = document.getElementById("provision").getElementsByTagName("input");
	optional.list = new Object();
	for (i = 0; i < optional.items.length; i += 2) {
		if (optional.list[optional.items[i + 1].value]) {
			optional.list[optional.items[i + 1].value] += optional.items[i].value * 1;
		} else {
			optional.list[optional.items[i + 1].value] = optional.items[i].value * 1;
		}
	}
	for (var key in optional.list)
		data += "optional:" + key + "=" + optional.list[key] + "&";
	
	var submit = new XMLStream();
	submit.connect("register.php", "POST", data, function(oXML, sPID) {
		var message = oXML.getElementsByTagName("message");
		document.getElementById("message").innerHTML = message[0].firstChild.data;
		document.getElementById("carte").style.display = "none";
		document.getElementById("results").style.display = "block";
		updateRegistration();
	}, 1);
}

function updateRegistration() {
	var members = document.getElementById("members").getElementsByTagName("input");
	var update = new XMLStream();
	update.connect("update.php", "POST", "page=registration", function(oXML, sPID) {
		try {
			var count = oXML.getElementsByTagName("count");
			members[0].value = count[0].getAttribute("adult");
			members[1].value = count[0].getAttribute("child");
			members[2].value = count[0].getAttribute("other");
		} catch (error) {
		}
		try {
			removeChildren("consumption");
			removeChildren("provision");
		} catch (error) {
		}
		var items = oXML.getElementsByTagName("item");
		var length = items.length;
		for (var i = 0; i < length; ++i) {
			var fare = items[i].getAttribute("type");
			if (fare == "provision") {
				var name = items[i].getAttribute("name").replace(/_/g, " ");
				var text = items[i].firstChild.data;
				addItem(fare, name, text);
			}
		}
		for (var i = 0; i < length; ++i) {
			var fare = items[i].getAttribute("type");
			if (fare == "consumption") {
				var name = items[i].getAttribute("name").replace(/_/g, " ");
				var text = items[i].firstChild.data;
				for (var type in menu) {
					if (menu[type].contains(name)) {
						addItem(type, name, text);
						break;
					}
				}
			}
			if (fare == "standard") {
				var name = items[i].getAttribute("name").replace(/_/g, " ");
				var text = items[i].firstChild.data;
				addItem(fare, name, text);
			}
		}
		for (var i = 0; i < length; ++i) {
			var fare = items[i].getAttribute("type");
			if (fare == "optional") {
				var name = items[i].getAttribute("name").replace(/_/g, " ");
				var text = items[i].firstChild.data;
				addItem(fare, name, text);
			}
		}
	}, 2);
}

function discuss() {
	var comment = document.getElementById("comment").value;
	var signature = document.getElementById("signature").value;
	if (comment == "" || signature == "") {
		alert("Both comment and signature are required.");
		return;
	}
	var data = "comment=" + comment + "&" + "signature=" + signature;
	var submit = new XMLStream();
	submit.connect("discuss.php", "POST", data, function(oXML, sPID) {
		document.getElementById("comment").value = "";
		updateDiscussion();
	}, 3);
}

function updateDiscussion() {
	var update = new XMLStream();
	update.connect("update.php", "POST", "page=discussion", function(oXML, sPID) {
		var comments = oXML.documentElement;
		document.getElementById("comments").innerHTML = getXMLString(comments);
	}, 4);
}

function setup() {
	var data = "";
	var guests = new Object();
	guests.details = document.getElementById("guests").getElementsByTagName("input");
	guests.list = new Object();
	for (i = 0; i < guests.details.length; i += 4)
		guests.list[guests.details[i + 1].value] = guests.details[i].value + ";" + guests.details[i + 2].value + ";" + guests.details[i + 3].value;
	for (var key in guests.list)
		data += "guests:" + key + "=" + guests.list[key] + "&";
	data += "datetime=" + document.getElementsByName("event.datetime")[0].value + "&";
	data += "description=" + document.getElementsByName("event.description")[0].value + "&";
	data += "host:name=" + document.getElementById("host").value + "&";
	for (var i = 0; i < where.length; ++i) {
		var value = document.getElementsByName("host." + where[i])[0].value;
		if (encoded[where[i]] == "b64")
			value = value.bin2b64();
		data += "host:" + where[i] + "=" + value + "&";
	}
	data += "coordinator:name=" + document.getElementById("coordinator").value + "&";
	data += "coordinator:phone=" + document.getElementsByName("coordinator.phone")[0].value;
	var submit = new XMLStream();
	submit.connect("setup.php", "POST", data, function(oXML, sPID) {
		updateSetup();
		var message = oXML.getElementsByTagName("message");
		alert(message[0].firstChild.data);
	}, 5);
}

function updateSetup() {
	var update = new XMLStream();
	update.connect("update.php", "POST", "page=setup", function(oXML, sPID) {
		try {
			removeChildren("guests");
			removeChildren("recipients");
		} catch (error) {
		}
		var names = new Array();
		var guests = oXML.getElementsByTagName("guest");
		for (i = 0; i < guests.length; ++i) {
			var role = getChildValue(guests[i], "role", "Guest");
			var id = getChildValue(guests[i], "id");
			var name = getChildValue(guests[i], "name");
			var email = getChildValue(guests[i], "email");
			var note = getChildValue(guests[i], "note");
			names.push(name);
			addGuest(role, id, name, email, note);
			addRecipient(name);
		}
		try {
			//removeChildren("details");
		} catch (error) {
		}
		try {
			var events = oXML.getElementsByTagName("event");
			var host = events[0].getElementsByTagName("host");
			var coordinator = events[0].getElementsByTagName("coordinator");
			document.getElementsByName("event.datetime")[0].value = getChildValue(events[0], "datetime");
			document.getElementsByName("event.description")[0].value = getChildValue(events[0], "description");
			var hostName = getChildValue(host[0], "name");
			var coordinatorName = getChildValue(coordinator[0], "name");
			removeChildren("host");
			removeChildren("coordinator");
			nSelectHost = document.getElementById("host");
			nSelectCoordinator = document.getElementById("coordinator");
			for (i = 0; i < names.length; ++i) {
				var nOptionHost = document.createElement("option");
				var nOptionCoordinator = document.createElement("option");
				nOptionHost.setAttribute("value", names[i]);
				nOptionCoordinator.setAttribute("value", names[i]);
				var nTextHost = document.createTextNode(names[i]);
				var nTextCoordinator = document.createTextNode(names[i]);
				nOptionHost.appendChild(nTextHost);
				nOptionCoordinator.appendChild(nTextCoordinator);
				if (names[i] == hostName)
					nOptionHost.setAttribute("selected", true);
				if (names[i] == coordinatorName)
					nOptionCoordinator.setAttribute("selected", true);
				nSelectHost.appendChild(nOptionHost);
				nSelectCoordinator.appendChild(nOptionCoordinator);
			}
			for (var i = 0; i < where.length; ++i) {
				var value = decodeURIComponent(getChildValue(host[0], where[i]));
				if (encoded[where[i]] == "b64")
					value = value.b642bin();
				document.getElementsByName("host." + where[i])[0].value = value;
			}
			document.getElementsByName("coordinator.phone")[0].value = getChildValue(coordinator[0], "phone");
		} catch (error) {
		}
	}, 6);
}

function notify() {
	var data = "";
	var recipients = new Object();
	recipients.details = document.getElementById("recipients").getElementsByTagName("input");
	recipients.list = new Object();
	for (i = 0; i < recipients.details.length; i += 3) {
		if (recipients.details[i].checked)
			recipients.list[recipients.details[i + 1].value] = recipients.details[i + 2].value;
	}
	for (var key in recipients.list)
		data += "guests:" + key + "=" + recipients.list[key] + "&";
	data += "personal=" + document.getElementById("personal").checked.toString() + "&";
	data += "type=" + document.getElementsByName("notification.type")[0].value + "&";
	data += "custom=" + document.getElementById("custom").value + "&";
	data += "test=" + document.getElementById("test").checked.toString() + "&";
	var submit = new XMLStream();
	submit.connect("notify.php", "POST", data, function(oXML, sPID) {
		try {
			updateNotify();
		} catch (error) {
		}
		var message = oXML.getElementsByTagName("message");
		alert(message[0].firstChild.data);
	}, 7);
}

function updateNotify() {
	var update = new XMLStream();
	update.connect("update.php", "POST", "page=notify", function(oXML, sPID) {
		var notifications = oXML.documentElement;
		var recipients = new Object();
		recipients.details = document.getElementById("recipients").getElementsByTagName("input");
		recipients.history = notifications.getElementsByTagName("guest");
		for (var i = 0; i < recipients.details.length; i += 3) {
			var nSelect = recipients.details[i + 1].parentNode.nextSibling.firstChild;
			while (nSelect.length > 0) nSelect.remove(0);
			for (var j = 0; j < recipients.history.length; ++j) {
				if (recipients.history[j].firstChild.firstChild.data == recipients.details[i + 1].value) {
					var entries = recipients.history[j].getElementsByTagName("notification");
					for (var k = 0; k < entries.length; ++k) {
						var nOption = document.createElement("option");
						var datetime = entries[k].firstChild.firstChild.data;
						var nValue = datetime.substr(5, 5) + " " + datetime.substr(11, 8) + " " + entries[k].getAttribute("type").toProperCase();
						nOption.value = nValue;
						nOption.text = nValue;
						if (k == entries.length - 1)
							nOption.selected = true;
						try {
							nSelect.add(nOption, null);
						} catch (error) {
							nSelect.add(nOption);
						}
					}
					try {
						recipients.history.splice(j, 1);
					} catch (error) {
					}
					break;
				}
			}
			if (recipients.history.length == 0) break;
		}
	}, 8);
}

function updateReport() {
	var update = new XMLStream();
	update.connect("update.php", "POST", "page=report", function(oXML, sPID) {
		var registrations = oXML.documentElement;
		var registration = registrations.getElementsByTagName("registration");
		removeChildren("registered");
		for (var key in registration) {
			try {
				var name = registration[key].getElementsByTagName("name");
				var count = registration[key].getElementsByTagName("count");
				var items = registration[key].getElementsByTagName("item");
				addRegistration(name[0].firstChild.data, count[0], items);
			} catch (error) {
			}
		}
		var items = registrations.lastChild.getElementsByTagName("item");
		removeChildren("unregistered");
		try {
			addRegistration(null, null, items);
		} catch (error) {
		}
	}, 9);
}
