function showCommentForm(parent, parentauthor) {
	if (parent != 0) {
		document.getElementById('parentcontainer').style.display = 'inherit';
		document.getElementById('parentname').innerText = "You are replying to " + parentauthor + "'s comment.";
		document.getElementById("parent").value = parent;
	}
	document.getElementById('submitcomment').style.display = 'none';
	document.getElementById('commentform').style.display = 'inherit';
}

function showCommentModeration() {
	document.getElementById('commentmoderation').style.display = 'none';
	document.getElementById('inmoderation').style.display = 'inherit';
}

function enableURLTitle() {
	document.getElementById('enablelink').style.display = 'none';
	document.getElementById('urltitlefield').style.display = 'inherit';
}

function clearSearchBox() {
	var searchBox = document.getElementById('searchbox');
	if (searchBox.value === "Search...") {
		searchBox.value = "";
	}
}

function fillSearchBox() {
	var searchBox = document.getElementById('searchbox');
	if (!searchBox.value) {
		searchBox.value = "Search...";
	}
}

function validateSearch() {
	var	query = document.getElementById('searchbox').value,
		querylength = query.length;
	if ((!query) || (query === "Search...")) {
		alert("Enter a search query.");
		return false;
	}
	if (querylength < 3) {
		alert("Your search query must have at least three characters.");
		return false;
	}
	return true;
}

function fillURLTitle() {
	var	title = document.getElementById('title'),
		temp = title.value.toLowerCase();
	temp = temp.replace(/ /g, "-");
	temp = temp.replace(/[^a-z-0-9 ]+/g, '');
	document.getElementById('urltitle').value = temp;
}

function showCategoryForm() {
	document.getElementById('categories').style.display = 'none';
	document.getElementById('newcategory').style.display = 'none';
	document.getElementById('newcategoryfield').style.display = 'inline';
}

function validEmail(email) {
	var	emailLength = email.length,
		atPosition = email.indexOf("@"),
		periodPosition = email.lastIndexOf("."),
		lastchar = email.charAt(emailLength - 1);
	if ((atPosition <= 0) || (atPosition >= periodPosition - 1) || (periodPosition === emailLength + 1) || (periodPosition === -1) || (lastchar === '.')) {
		return false;
	} else {
		return true;
	}
}

function validateComment() {
	var	author = document.getElementById('name').value,
		email = document.getElementById('email').value,
		comment = document.getElementById('comment').value;
	if (!author) {
		alert("You must provide a name.");
		return false;
	}
	if (!email) {
		alert("You must provide an email address.");
		return false;
	}
	if (!validEmail(email)) {
		alert("You must provide a valid email address.");
		return false;
	}
	if (!comment) {
		alert("You need to enter your comment!");
		return false;
	}
	return true;
}

function sha1(msg) {

	function rotate_left(n, s) {
		var t4 = (n<<s) | (n>>>(32 - s));
		return t4;
	}

	function cvt_hex(val) {
		var	str = "",
			i,
			v;
 
		for (i = 7; i >= 0; i--) {
			v = (val>>>(i * 4))&0x0f;
			str += v.toString(16);
		}
		return str;
	}
 
 
	function utf8Encode(string) {
		string = string.replace(/\r\n/g, "\n");
		var	utftext = "",
			n,
			c;
			
		for (n = 0; n < string.length; n++) {
			c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			} else if ((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			} else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	}
 
	var	blockstart,
		i, j,
		W = new Array(80),
		H0 = 0x67452301,
		H1 = 0xEFCDAB89,
		H2 = 0x98BADCFE,
		H3 = 0x10325476,
		H4 = 0xC3D2E1F0,
		A,
		B,
		C,
		D,
		E,
		temp,
		msg_len,
		word_array;
 
	msg = utf8Encode(msg);
	msg_len = msg.length;
	word_array = [];
	for (i = 0; i < msg_len - 3; i += 4) {
		j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i + 1)<<16 | msg.charCodeAt(i + 2)<<8 | msg.charCodeAt(i + 3);
		word_array.push(j);
	}
 
	switch (msg_len % 4) {
	case 0:
		i = 0x080000000;
		break;

	case 1:
		i = msg.charCodeAt(msg_len - 1)<<24 | 0x0800000;
		break;

	case 2:
		i = msg.charCodeAt(msg_len - 2)<<24 | msg.charCodeAt(msg_len - 1)<<16 | 0x08000;
		break;

	case 3:
		i = msg.charCodeAt(msg_len - 3)<<24 | msg.charCodeAt(msg_len - 2)<<16 | msg.charCodeAt(msg_len - 1)<<8	| 0x80;
		break;
	}
 
	word_array.push(i);
 
	while ((word_array.length % 16) !== 14) {
		word_array.push(0);
	}
 
	word_array.push(msg_len>>>29);
	word_array.push((msg_len<<3)&0x0ffffffff);
 
 
	for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
		for (i = 0; i < 16; i++) {
			W[i] = word_array[blockstart + i];
		}
		for (i = 16; i <= 79; i++) {
			W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
		}
 
		A = H0;
		B = H1;
		C = H2;
		D = H3;
		E = H4;
 
		for (i = 0; i <= 19; i++) {
			temp = (rotate_left(A, 5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
			E = D;
			D = C;
			C = rotate_left(B, 30);
			B = A;
			A = temp;
		}
 
		for (i = 20; i <= 39; i++) {
			temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
			E = D;
			D = C;
			C = rotate_left(B, 30);
			B = A;
			A = temp;
		}
 
		for (i = 40; i <= 59; i++) {
			temp = (rotate_left(A, 5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
			E = D;
			D = C;
			C = rotate_left(B, 30);
			B = A;
			A = temp;
		}
 
		for (i = 60; i <= 79; i++) {
			temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
			E = D;
			D = C;
			C = rotate_left(B, 30);
			B = A;
			A = temp;
		}
 
		H0 = (H0 + A) & 0x0ffffffff;
		H1 = (H1 + B) & 0x0ffffffff;
		H2 = (H2 + C) & 0x0ffffffff;
		H3 = (H3 + D) & 0x0ffffffff;
		H4 = (H4 + E) & 0x0ffffffff;
 
	}
 
	temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
 
	return temp.toLowerCase();
 
}

function hashPass() {
	var	jssalt = "0keoiA6Rxf5w6euYNE1XWe659taJM4Qu8v0u5wCV32eH7hVR0eZePgYs671eaLIu",
		ptext = "";
	ptext = document.getElementById('pass').value + jssalt;
	document.getElementById('hashpass').value = sha1(ptext);
	document.getElementById('pass').value = "";
	if (document.getElementById('pass2')) {
		document.getElementById('pass2').value = "";
	}
}

function validateUser() {
	var	username = document.getElementById('username').value,
		email = document.getElementById('email').value,
		password = document.getElementById('pass').value,
		verifypassword = document.getElementById('pass2').value;
	if ((!username) || (!email) || (!password) || (!verifypassword)) {
		alert("Please ensure that you have entered values into all of the fields.");
		return false;
	}
	if (!validEmail(email)) {
		alert("Enter a valid email address.");
		return false;
	}
	if (password !== verifypassword) {
		alert("The passwords that you entered do not match. Please enter them both again.");
		return false;
	}
	hashPass();
	return true;
}

function alterTables(source) {
	var	elements = document.getElementsByTagName('*'),
		sources = ["pageviews", "access", "info", "admin", "error"],
		i;
		for (i = 0; i < elements.length; i++) {
			if (sources.indexOf(elements[i].className) >= 0) {
				if (elements[i].className === source) {
					elements[i].style.display = "block";
				} else {
					elements[i].style.display = "none";
				}
			}
		}
}

function applyTableRules() {
	alterTables("pageviews");
}

function alterRows(source) {
	var	checksource = document.getElementById(source).checked,
		elements = document.getElementsByTagName('*'),
		i;
	if (checksource) {
		for (i = 0; i < elements.length; i++) {
			if (elements[i].className.indexOf(source) >= 0) {
				elements[i].style.display = "table-row";
			}
		}
	} else {
		for (i = 0; i < elements.length; i++) {
			if (elements[i].className.indexOf(source) >= 0) {
				elements[i].style.display = "none";
			}
		}
	}
}

function alterHiddenText(elemid, action) {
	var	parent,
		elems,
		subelems;
	parent = document.getElementById(elemid);
	elems = parent.childNodes;
	
	for (i = 0; i < elems.length; i++) {
		if (elems[i].className == "hiddentext") {
			if (action == "in") {
				elems[i].style.visibility = "visible";
			} else {
				elems[i].style.visibility = "hidden";
			}
		}
	}
}
