/* Basic stuff */

/* The "right" way to clear a document element. */
function delete_all_children(element) {
	if (element) {
		while (element.hasChildNodes()) {
			element.removeChild(element.firstChild);
		}
	}
}

/* Ganked from somewhere */
function new_XHR () {
        try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {};
        try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
        try { return new XMLHttpRequest(); } catch(e) {};

        return null;
}
  
/* load_page: initiator */
function load_page(pagename) {
	pageloader = new_XHR();
	pageloader.onreadystatechange = load_page_handler;
	pageloader.open("GET", "/ajax.php?a=load_page&page=" + encodeURIComponent(pagename));
	pageloader.send();
	return false; 
}

function load_page_handler() {
	if (this.readyState == 4 && this.status == 200) {
		delete_all_children(document.getElementById("arenatxt"));
		document.getElementById("arenatxt").innerHTML = this.responseText; /* Yeah, this is "bad", but every browser supports it (or seems to). */
	}
}

/* try_secret: initiator */
function try_secret() {
	lulz = document.getElementById("sekrit").value;
	secret_trier = new_XHR();
	secret_trier.onreadystatechange = try_secret_handler;
	secret_trier.open("GET", "/ajax.php?a=sekrit&s=" + encodeURIComponent(lulz));
	secret_trier.send();
	return false;
}

function try_secret_handler() {
	if (this.readyState == 4 && this.status == 200) {
		ret = eval("(" + this.responseText + ")");
		if (ret.result == "success" && ret.action) {
			switch (ret.action) {
				case "put_image":
					new_img = document.createElement("img");
					new_img.src = ret.image;
					document.getElementById("arenatxt").appendChild(document.createElement("br"));
					document.getElementById("arenatxt").appendChild(document.createElement("br"));
					document.getElementById("arenatxt").appendChild(new_img);
					break;
				case "go_to":
					document.location.href = ret.href;
					break;
				default:
					alert("Don't know how to do " + ret.action);
					break;
			}
		} else {
			alert("Error: " + ret.error);
		}
	}
}
					

