function getKeyCode(e) {
	try {
		return event.keyCode;
	}
	catch(err) {
		return e.which;
	}
}

function testRegEx(e, re) {
	var kc = getKeyCode(e);
	var key = String.fromCharCode(kc);
	return (re.test(key) || kc == 0 || kc == 8 || kc == 9);
}

function loginChrOnly(e) {
	return testRegEx(e, /[^ ]/);
}

var errMsg;

function addErr(txt) {
	errMsg += (errMsg == "" ? "" : "\n") + txt;
}

function chkText(val, minChr, fldName) {
	if (val == "") {
		addErr(fldName + " must be provided!");
	}
	else if (val.length < minChr) {
		addErr(fldName + " must consist of at least " + minChr + " characters!");
	}
}

function chkLoginForm() {
	var val;
	errMsg = "";
	val = g("user").value;
	chkText(val, 4, "User Name");
	if (val.indexOf(" ") != -1) {
		addErr("User Name cannot contain spaces!");
	}
	val = g("pw").value;
  chkText(val, 6, "Password");
	if (val.indexOf(" ") != -1) {
		addErr("Password cannot contain spaces!");
	}
	if (errMsg != "") {
		alert(errMsg);
		return false;
	}
	return true;
}

// Button Mouse Events

function butHover() {
	if (!this.disabled) {
		this.style.color = "#557";
		this.style.backgroundColor = "#bbe";
		this.style.borderColor = "#eef #88b #88b #eef";
	}
}

function butOut() {
	if (!this.disabled) {
		this.style.color = "#bbe";
		this.style.backgroundColor = "#557";
		this.style.borderColor = "#779 #223 #223 #779";
	}
}

function butDown() {
	if (!this.disabled) this.style.borderColor = "#88b #eef #eef #88b";
}

function butUp() {
	if (!this.disabled) this.style.borderColor = "#eef #88b #88b #eef";
}

function loginButHover() {
	this.style.color = "#eef";
}

function loginButOut() {
	this.style.color = "#ccf";
}

function ovOver() {
	var i, j;
	if (this.childNodes.length > 6) {
		j = (this.id.indexOf("thread_") == 0 ? 8 : 7);
		for (i=3;i<j;i++) {
			this.childNodes[i].style.backgroundColor = "#445";
		}
	}
	else {
		this.style.backgroundColor = "#445";
	}
}

function ovOut() {
	var i, j;
	if (this.childNodes.length > 6) {
		j = (this.id.indexOf("thread_") == 0 ? 8 : 7);
		for (i=3;i<j;i++) {
			this.childNodes[i].style.backgroundColor = "transparent";
		}
	}
	else {
		this.style.backgroundColor = "transparent";
	}
}

function ovClick() {
	window.location.href = this.href;
}

function OvItem(o) {
	this.item = o;
	this.item.link = gt(o,"a")[0];
	this.item.href = this.item.link.href;
	this.item.style.cursor = "pointer";
	this.item.onmouseover = ovOver;
	this.item.onmouseout = ovOut;
	this.item.onclick = ovClick;
	this.item.link.onfocus = linkFocus;
	this.item.link.onclick = linkClick;
}

var ovItems;

function setOvItems() {
	var i, j = 0, arr, arrLen, id;
	ovItems = [];
	arr = gt(d,"tr");
	arrLen = arr.length;
	for (i=0;i<arrLen;i++) {
		id = arr[i].id;
		if (id.indexOf("forum_") == 0 || id.indexOf("thread_") == 0) {
			ovItems[j++] = new OvItem(arr[i]);
		}
	}
}



function chkCookie() {
	if (d.cookie.indexOf("up=") != -1) {
		if (!confirm("You have Auto Login enabled!\n\nIf you sign out this function will be disabled!\n\nAre you sure you want to sign out?")) return false;
	}
	return true;
}

function initForum() {
	var i, o, arr, arrLen;
	init();
	if (g("login-form")) {
		if (!navigator.cookieEnabled) g("cookie-info").innerHTML = '<p>Cookies must be enabled!</p>';
		g("user").onkeypress = loginChrOnly;
		g("pw").onkeypress = loginChrOnly;
		o = g("send-button");
		o.onmouseover = loginButHover;
		o.onmouseout = loginButOut;
		g("login-form").onsubmit = chkLoginForm;
	}
	setOvItems();
	if (g("form-buttons")) {
		arr = gt("form-buttons", "input");
		arrLen = arr.length;
		for (i=0;i<arrLen;i++) {
			arr[i].onmouseover = butHover;
			arr[i].onmouseout = butOut;
			arr[i].onmousedown = butDown;
			arr[i].onmouseup = butUp;
		}
	}
	if (g("sign-out")) {
		g("sign-out").onclick = chkCookie;
	}
}

window.onload = initForum;