// JavaScript Document
function closeTheRoom() {
	var marvin = new XMLHttpRequest();
	marvin.open("GET", "brain.php?command=logoutByStealth", false);
	marvin.send(null);
	//alert(window.closed);
/*	var answer = marvin.responseText;
	alert(answer);*/
}
function lightEmAllUp(who) {
	var lightswitch = who.checked;
	var overfeild = who.form;
	var bitsNPieces = overfeild.elements;
	for(var x=0;x<bitsNPieces.length;x++) {
		if(bitsNPieces[x].type == "checkbox") {
			bitsNPieces[x].checked = lightswitch;
			turnOnDelete(who);
		}
	}
}

function startTheUpload(what) {
	viewer = false;
	if(document.getElementById("changeroom")) {
		document.getElementById("changeroom").style.display = "block";
		document.getElementById("createorno").style.display = "block";
		document.getElementById("changedir").style.display = "block";
	}
	document.getElementById("viewfiles").value = "VIEW FILES";
	document.getElementById("deletefiles").style.display = "none";
	document.getElementById("scroll").style.display = "none";
	var stat = document.getElementById("container").style.display;
	if(stat == "none") {
		document.getElementById("container").style.display = "block";
		timer = setInterval("updateQtyString()", 3000);
		what.value = "HIDE UPLOAD";
	} else {
		document.getElementById("container").style.display = "none";
		timer = clearInterval(timer);
		what.value = "UPLOAD";
	}
}
function turnOnFiles(what) {
	if(document.getElementById("changeroom")) {
		document.getElementById("changeroom").style.display = "block";
		document.getElementById("createorno").style.display = "block";
		document.getElementById("changedir").style.display = "block";
	}
	document.getElementById("container").style.display = "none";
	document.getElementById("upload").value = "UPLOAD";
	timer = clearInterval(timer);
	if(viewer) {
		what.value = "VIEW FILES";
		document.getElementById("deletefiles").style.display = "none";
		document.getElementById("scroll").style.display = "none";
		viewer = false;
	} else {
		what.value = "HIDE FILES";
		document.getElementById("scroll").innerHTML = returnQuery("returnlist");
		document.getElementById("scroll").style.display = "block";
		viewer = true;
	}
}
function toItalic(what, how) {
	if(how) {
		what.style.color = "#FFCCCC";
	} else {
		what.style.color = "#FF9999";
	}
}
function turnOff() {
	document.getElementById("summary").style.display = "none";
	rightTheDirMenu();
}
function clearTheDummyText(who) {
	who.style.color = "#666600";
	who.value = "";
	who.type = "password";
}
function checkTheInviteForm (what) {
	var name = document.getElementById("invite_name").value;
	var email = document.getElementById("invite_email").value;
	if(checkName(name)) {
		if(!checkEmail(email)) {
			alert("You have entered an invalid email address");
		} else {
			var isItCustom = document.getElementById("invite_custom").value;
			if(isItCustom == 0) {
				what.form.submit();
			} else {
				var customPassword = document.getElementById("invite_password").value;
				if(customPassword.length >= 4 && customPassword.indexOf("password") == -1) {
					what.form.submit();
				} else {
					alert("Please enter a valid custom password, consisting of at least four characters");
				}
			}
		}
	}
}
function checkName(name) {
	if(name.length == 0) {
		alert("Please enter a name");
		return false;
	} else {
		if(name.indexOf(" ") == -1) {
			alert("You must enter a first name and a last name");
			return false;
		}
	}
	return true;
}
function decide(who, menu, textbox) {
	if(!who.checked) {
		whatsThePassword(who, menu);
		whatsThePassword(who, textbox);
	} else {
		whatsThePassword(who, menu);
	}
}
function whatsThePassword(who, element) {
	if(who.type != "checkbox") {
		var customStatus = parseInt(who.value);
	} else {
		var customStatus = who.checked;
	}
	var element = eval("document.getElementById('"+element+"')");
	if(customStatus == 1 || customStatus == true) {
		element.style.backgroundColor = "#FFFF00";
		element.style.border = "inset";
		element.disabled = false;
		if(element.type != "select-one") {
			element.style.color = "#AAAAAA";
			element.type = "text";
			element.value = "password...";
		}
	} else {
		element.style.backgroundColor = "#FFFFCC";
		element.style.border = "#FF0000 1px solid";
		element.disabled = true;
		if(element.type != "select-one") {
			element.value = "";
		} else {
			element.selectedIndex = 1;
		}
	}
}
function doWhat(number) {
	var actions = new Array;
	var victim = parseInt(number);
	var myname = eval("document.getElementById('whosthis_"+victim+"')");
	var myname_delete = eval("document.getElementById('delete_"+victim+"')");
	var myname_newpass = eval("document.getElementById('newpassword_"+victim+"')");
	var myname_status = eval("document.getElementById('status_"+victim+"')");
	var myname_initstatus = eval("document.getElementById('initstatus_"+victim+"')");
	if(myname_delete.checked) {
		actions[actions.length] = "Delete "+myname.value;
	}
	if(myname_newpass.checked) {
		actions[actions.length] = "Send "+myname.value+" a new password";
	}
	var newStatus = parseInt(myname_status[myname_status.selectedIndex].value);
	var oldStatus = parseInt(myname_initstatus.value);
	if(newStatus != oldStatus) {
		if(newStatus > oldStatus) {
			actions[actions.length] = "Promote "+myname.value+" to Manager";
		} else {
			actions[actions.length] = "Demote "+myname.value+" to Guest";
		}
	}
	return actions; 
}
function extractListedItems(whatSource) {
	var spitback = "";
	for(var y=0;y<whatSource.length;y++){
		spitback += "<li>"+whatSource[y]+"</li>";
	}
	return(spitback);
}
function compileSummary(what) {
	var whotoReport = new Array;
	var contents = what.elements;
	for(var x=0;x<contents.length;x++) {
		who = contents[x].name;
		if(who.indexOf("mucked") != -1 && contents[x].value.length > 0) {
			whotoReport[whotoReport.length] = who.substr(11);
		}
	}
	var ulist = "<ul>";
	for(var t=0;t<whotoReport.length;t++) {
		var thisfellow = doWhat(whotoReport[t]);
		if(thisfellow.length > 0) {
			ulist += extractListedItems(thisfellow);
		}
	}
	ulist +="</ul>";
	if(ulist == "<ul></ul>") {
		document.getElementById("summary").style.display = "none";
		what.submit();
	} else {
		var summary = "<span><u>SUMMARY</u><br><br>You have chosen to:<br><br>"+ulist+"</span>";
		document.getElementById("source").innerHTML = summary;
	}
}
function processTheName(string) {
	if(string.indexOf("_") != -1) {
		string = string.split("_");
		string = string.join(" ");
	}
	return(string.toUpperCase());
}
function checkEmail(email) {
	var stringCount = email.length;
	if(email.indexOf("@") == -1) {
		return false;
	}
	if(email.indexOf(".") == -1) {
		return false;
	}
	if(email.lastIndexOf(".") > (stringCount - 3)) {
		return false;
	}
	if(email.indexOf("@") < 1) {
		return false;
	}
	return true;
}
function checkIt(what) {
	var contents = what.elements;
	var errors = 0;
	for(var x=0;x<contents.length;x++) {
		who = contents[x].name;
		if(who != "phone") {
			if(contents[x].value.length < 1) {
				pwho = processTheName(who);
				alert("You have not entered a value for the "+pwho+" field");
				errors++;
				break;
			} else {
				if(who == "email") {
					var result = checkEmail(contents[x].value);
					if(!result) {
						alert("You have entered an invalid email address");
						errors++;
						break;
					}
				}
			}
		}
	}
	if(errors == 0) {
		what.submit();
	}
}
function readTheDeadList(deadlist) {
	if(deadlist.length == 2) {
		deadlist = deadlist.join(" and ");
	}
	if(deadlist.length > 2) {
		var lastEntry = deadlist.length - 1;
		var lastClause = "and "+deadlist[lastEntry];
		deadlist[lastEntry] = lastClause;
	}
	return(deadlist);
}
function areYouSure(that) {
	var contents = that.elements;
	var whosGone = new Array();
	for(var x=0;x<contents.length;x++) {
		if(contents[x].type == "select-one" && contents[x].value == "deny") {
			var whatnumber = contents[x].name.substr(8);
			var whoIsIt = eval("document.getElementById('whois_"+whatnumber+"')");
			whosGone[whosGone.length] = whoIsIt.value;
		}
	}
	if(whosGone.length > 0) {
		var theDeadList = readTheDeadList(whosGone);
		var question = "Are you sure you wish to delete "+theDeadList+"? This action cannot be undone once it's done";
		if(confirm(question)) {
			that.submit();
		}
	} else {
		that.submit();
	}
}
function changeColour (what, toWhat) {
	what.style.backgroundColor = toWhat;
}
function changeColourAdvanced (what, toWhat, text) {
	what.style.backgroundColor = toWhat;
	what.style.color = text;
}
function logOut() {
	var question = "Are you sure you want to log out?";
	if(confirm(question)) {
		document.location.href="central.php?logout=true";
	}
}
function jumpTo(fromButton) {
	document.getElementById("jumpto").value = fromButton.id;
	fromButton.form.submit();
}
function marThis(number) {
	var whichObject = eval("document.getElementById('muckedwith_"+number+"')");
	whichObject.value = "true";
}
function checkForMan(what) {
	var newvalue = parseInt(what.options[what.selectedIndex].value);
	if(newvalue == 1) {
		var question = "Promoting this person to manager will give him or her full administrative privileges in The Hotel. He or she will be able to demote or delete anyone on the guestlist, including you. Are you sure you want to do this?";
		if(!confirm(question)) {
			what.selectedIndex = 1;
		}
	}
}
function unclick(who) {
	var deleteButton = eval("document.getElementById('delete_"+who+"')");
	var newPasswordButton = eval("document.getElementById('newpassword_"+who+"')");
	if(deleteButton.checked) {
		newPasswordButton.checked = false;
	}
}
function onlyManager(what) {
	var whotoCheck = what.split("|");
	var tally = 0;
	for(var m=0;m<whotoCheck.length;m++) {
		var deleteButton = eval("document.getElementById('delete_"+whotoCheck[m]+"')");
		if(deleteButton.checked) {
			tally++;
		}
	}
	if(tally < whotoCheck.length) {
		return false;
	}
	return true;
}
function processSeconds (seconds) {
	if(seconds <= 0 || isNaN(seconds)) {
		var timeString = "";
	} else {
		if(seconds < 60) {
			var seclabel = (seconds == 1) ? "second" : "seconds";
			var timeString = seconds+" "+seclabel;
		} else {
			if(seconds > 3600) {
				var hours = Math.floor(seconds / 3600);
				var hourlabel = (hours == 1) ? "hour" : "hours";
				var newSeconds = (seconds - (3600 * hours));
				var minutes = Math.floor(newSeconds / 60);
				var minutelabel = (minutes == 1) ? "minute" : "minutes";
				var lastSeconds = (newSeconds - (minutes * 60));
				var seclabel = (lastSeconds == 1) ? "second" : "seconds";
				if(isNaN(hours) || isNaN(minutes) ||  isNaN(lastSeconds)) {
					var timestring = "";
				} else {
					var timeString = hours+" "+hourlabel+" "+minutes+" "+minutelabel+" "+lastSeconds+" "+seclabel;
				}
			} else {
				var minutes = Math.floor(seconds / 60);
				var minutelabel = (minutes == 1) ? "minute" : "minutes";
				var lastSeconds = (seconds - (minutes * 60));
				var seclabel = (lastSeconds == 1) ? "second" : "seconds";
				if(isNaN(minutes) || isNaN(lastSeconds)) {
					var timestring = "";
				} else {
					var timeString = minutes+" "+minutelabel+" "+lastSeconds+" "+seclabel;
				}
			}
		}
	}
	if(timeString.length > 0) {
		timeString = " - "+timeString+" remaining";
	}
	return(timeString);
}
function calculateTimeRemaining(total, portionLoaded, speed) {
	var KBtotal = total/1000;
	var KBloaded = portionLoaded/1000;
	var portionRemaining = (KBtotal - KBloaded);
	var secondsRemaining = Math.floor(portionRemaining / speed);
	var timeRemaining = processSeconds(secondsRemaining);
	return(timeRemaining);
}
function checkGuestList(thelist) {
	var stuff = thelist.elements;
	var manag = new Array();
	var managenum = 0;
	var deletechex = new Array(); 
	var screwups = 0;
	for(var t=0;t<stuff.length;t++) {
		if(stuff[t].type == "select-one") {
			var currEl = stuff[t];
			var statusT = parseInt(currEl.options[currEl.selectedIndex].value);
			if(statusT == 1) {
				managenum++;
				manag[manag.length] = parseInt(currEl.name.substr(7));
			}
		}
		if(stuff[t].type == "checkbox" && stuff[t].name.substr(0, 3) == "del") {
			if(stuff[t].checked) {
				deletechex[deletechex.length] = stuff[t].name;
			}
		}
		if(stuff[t].name.substr(0, 8) == "password" && !stuff[t].disabled && stuff[t].value.length < 4 || stuff[t].value.indexOf("password") != -1) {
			alert("Please enter a valid password, consisting of at least four characters");
			screwups++;
		}
	}
	if(deletechex.length > 0) {
		var question = "You have marked at least one guest for deletion. Anything that is in this guest's room will be deleted also. Are you sure you wish to delete the selected guest(s)? This action cannot be undone once it's done.";
		if(!confirm(question)) {
			screwups++;
		}
	}
	if(managenum == 0) {
		alert("There must be at least one manager");
		screwups++;
	} else {
		managStr = manag.join("|");
		if(onlyManager(managStr)) {
			if(managenum > 1) {
				var msg = "You can't delete all the managers. There must be at least one";
			} else {
				var msg = "You can't delete the only manager";
			}
			alert(msg);
			screwups++;
			for(g=0;g<manag.length;g++) {
				var deleteButtonFinal = eval("document.getElementById('delete_"+manag[g]+"')");
				deleteButtonFinal.checked = false;
			}
		}
	}
	if(screwups == 0) {
		document.getElementById("summary").style.display = "block";
		compileSummary(thelist);
	}
}
function adjustDirMenu(who) {
	var position = who.selectedIndex;
	var mybutton = document.getElementById("changedir");
	if(position != 0) {
		who.style.color = "#FF0000";
		mybutton.style.color = "#FF0000";
		mybutton.style.cursor = "pointer";
		mybutton.disabled = false;
		var label = who.options[who.selectedIndex].value;
		if(label == 1) {
			mybutton.value = "CREATE";
		} else {
			mybutton.value = "DELETE";
		}
	} else {
		who.style.color = "#FFFFFF";
		mybutton.style.color = "#999999";
		mybutton.style.cursor = "default";
		mybutton.disabled = true;
		mybutton.value = "SELECT ONE";
	}
}
function rightTheDirMenu() {
	if(document.getElementById("createorno")) {
		document.getElementById("createorno").selectedIndex = 0;
		adjustDirMenu(document.getElementById("createorno"));
	}
	updateQtyString();
}
