// JavaScript Document
// JavaScript for LCN website

var xmlReq = false;
var version = navigator.appVersion.split("MSIE")
version = parseFloat(version[1])

function searchFocus() {
	if (document.getElementById('searchbox').value == "Search LCN") {
		document.getElementById('searchbox').value = "";
		document.getElementById('searchbox').style.color = "#000000";
	}
}

function searchBlur() {
	if (document.getElementById('searchbox').value == "") {
		document.getElementById('searchbox').style.color = "#999999";
		document.getElementById('searchbox').value = "Search LCN";
	}
}

function changeFontSize(size) {
	var theBody = document.getElementsByTagName("body").item(0);
	switch (size) {
		case 'medium':
			theBody.style.fontSize = "110%";
			break;
		case 'large':
			theBody.style.fontSize = "130%";
			break;
		case 'small':
		default:
			theBody.style.fontSize = "90%";
			break;
	}
	createCookie("basesize", size);
}

function initialPageLoad() {
	var baseSize = readCookie("basesize");
	if (baseSize) changeFontSize(baseSize);
	if (document.getElementById("rightSide") && document.getElementById("content") && document.getElementById("content").offsetHeight) {
		document.getElementById("rightSide").style.height = document.getElementById("content").offsetHeight+"px"
	}
}

window.onload = initialPageLoad;

function createCookie(name, value)
{
	var date = new Date();
	date.setTime(date.getTime()+(3*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var cookieidentifier = name + "=";
	var cookiearray = document.cookie.split(";");
	for(var i=0; i < cookiearray.length; i++)
	{
		var cookiestring = cookiearray[i];
		while (cookiestring.charAt(0) == " ") cookiestring = cookiestring.substring(1, cookiestring.length);
		if (cookiestring.indexOf(cookieidentifier) == 0) return cookiestring.substring(cookieidentifier.length, cookiestring.length);
	}
	return null;
}

function fixPng(theelement) {
	if (theelement.style.display != 'none') return; // Prevent loop onload...
	theelement.style.display = '';
	if (document.body.filters && (version >= 5.5) && (version < 7)) {
		oldwidth = theelement.width;
		oldheight = theelement.height;
		theelement.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(theelement.src)+"', sizingMethod='scale');";
		theelement.src = "/lib/img/blank.gif";
		theelement.style.width = oldwidth+"px";
		theelement.style.height = oldheight+"px";
	}
}

// Changes all the "reply" and "quote" links on the forums to remove their
// actual links - this means the page can be outputted to work even when
// javascript isn't available, but when it is only js functions are used.
function changeReplyLinks(idArray) {
	for (i=0; i<idArray.length; i++) {
		if (document.getElementById("quote"+idArray[i])) document.getElementById("quote"+idArray[i]).href = "javascript:void(0)";
		if (document.getElementById("reply"+idArray[i])) document.getElementById("reply"+idArray[i]).href = "javascript:void(0)";
	}
}

// Shows or hides the reply area of the page.  postID gives the id of the post
// so the reply area can be inserted at the right point (or shown if one has
// been previously inserted).  isQuote is a boolean determining whether the
// post should be quoted.  doHide is a boolean determining whether to show or
// hide the area.
function showReply(postID, isQuote, doHide) {

	// So first thing to do: Hide/Show all the reply/quote links
	hideLinks(!doHide);
	
	// Now alter the Reply/Close link as appropriate
	document.getElementById("reply"+postID).innerHTML = ((doHide)?"Reply":"Close");
	document.getElementById("reply"+postID).style.display = "inline";
	document.getElementById("reply"+postID).onclick = function() {showReply(postID, false, !doHide);};
	
	// If we're hiding the area...
	if (doHide) {
		document.getElementById("replyrow"+postID).style.display = "none";
		lastReply = false;
	
	// If we're showing the area...	
	} else {
		lastReply = postID;
	
		// ... if the area already exists, just show it
		if (document.getElementById("replyrow"+postID)) {
			document.getElementById("replyrow"+postID).style.display = "";
			
		// ...otherwise, create a new reply area.
		} else {
		
			// Create a new TR element.
			newTR = document.createElement("TR");
			newTR.className = "replyArea";
			newTR.id = "replyrow"+postID;
			
			// Create a new TD element (innerHTML doesn't work on TRs)
			newTD = document.createElement("TD");
			newTD.colSpan = 2;
			
			// Grab the reply code from the page
			innerCodeToInsert = document.getElementById("replypreview").innerHTML;
			
			// Replace "qqq" with the id of this post to customise the reply.
			while (innerCodeToInsert.indexOf("qqq") > 0) {
				innerCodeToInsert = innerCodeToInsert.replace("qqq", postID);
			}
			
			// Apply the innerHTML to the td and add it to the tr
			newTD.innerHTML = innerCodeToInsert;
			newTR.appendChild(newTD);
			
			// Grab the main tbody of the posts table, and add the reply area
			// in the correct location.
			var tableNode = document.getElementById("forumpoststable").tBodies[0];
			tableNode.insertBefore(newTR, document.getElementById("replylinksforpost"+postID).nextSibling);
		}
		if (isQuote) {
			document.getElementById("replytext"+postID).disabled = true;
			document.getElementById("replytext"+postID).style.color = "#888888";
			document.getElementById("replytext"+postID).value = "Please wait.  Quoting post...";
			getQuote(postID);
		}
	}
}

// Shows or hides all the "reply" type links on a forum topic page...
function hideReplyLinks(idArray, doHide) {
	for (i=0; i<idArray.length; i++) {
		if (document.getElementById("quote"+idArray[i])) document.getElementById("quote"+idArray[i]).style.display = ((doHide)?"none":"inline");
		if (document.getElementById("reply"+idArray[i])) document.getElementById("reply"+idArray[i]).style.display = ((doHide)?"none":"inline");
		if (document.getElementById("edit"+idArray[i])) document.getElementById("edit"+idArray[i]).style.display = ((doHide)?"none":"inline");
	}
}

function startInlineReply(postID) {
	replyFeedback = document.getElementById("reply"+postID+"feedback");
	replyFeedback.style.color = "#555555";
	replyFeedback.innerText = "Submitting post, please wait...";
	document.getElementById("preview"+postID).disabled = true;
	document.getElementById("submit"+postID).disabled = true;
	document.getElementById("replyform"+postID).submit();
}

function inlineReplyError(errorText) {
	if (lastReply) {
		replyFeedback = document.getElementById("reply"+lastReply+"feedback");
		replyFeedback.style.color = "#FF0000";
		replyFeedback.innerText = errorText;
		resetInlineReply(lastReply);
	}
}

function resetInlineReply(postID) {
	document.getElementById("preview"+postID).disabled = false;
	document.getElementById("submit"+postID).disabled = false;
}

function addForumAttachment(postID) {
	newSpan = document.createElement("span");
	newSpan.innerHTML = "<input type=\"file\" name=\"attachments[]\" style=\"display: block; margin-bottom: 2px;\" />";
	document.getElementById("attachmentsdiv"+postID).appendChild(newSpan);
	document.getElementById("attachmentstext"+postID).innerText = "Attach another file";
}

function givePreview(postID) {
	var theForm = document.getElementById("replyform"+postID);
	if (theForm.enctype) theForm.enctype = "application/x-www-form-urlencoded";
	else theForm.encoding = "application/x-www-form-urlencoded";
	theForm.target = "_blank";
	theForm.action = "previewreply";
	theForm.submit();
	theForm.target = "uploadTarget";
	theForm.action = "inlinereply";
	if (theForm.enctype) theForm.enctype = "multipart/form-data";
	else theForm.encoding = "multipart/form-data";
}

function checkForNewPosts(topicID) {
	if (window.ActiveXObject) {
		xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlReq = new XMLHttpRequest();
	}
	if (xmlReq) {
		xmlReq.onreadystatechange = newItemXmlHttpRequestStateChange;
		xmlReq.open("GET", '/extranet/forum/xml/getnewposts?id='+topicID+'&time='+lastCheck, true);
		xmlReq.send(null);
	}
	setTimeout("checkForNewPosts("+topicID+")", 60000);
}

// Handle onreadystatechange events from the xmlHttpRequest
function newItemXmlHttpRequestStateChange() {
	if (xmlReq.readyState == 4) {
		if (xmlReq.status == 200) {
			response = xmlReq.responseXML.documentElement;
			if (!response.getElementsByTagName('itemstype').length) return;
			itemtype = response.getElementsByTagName('itemstype')[0].firstChild.data;
			if (itemtype == "none") return;
			lastCheck = response.getElementsByTagName('lastcheck')[0].firstChild.data;
			allNewItems = response.getElementsByTagName('newitem');
			if (onLastPage) {
				var tableNode = document.getElementById("forumpoststable").tBodies[0];
				for (i=0; i<allNewItems.length; i++) {
					thisItem = allNewItems[i];
					if (itemtype == "posts") {
						postID = thisItem.getElementsByTagName('id')[0].firstChild.data;
						headerClass = thisItem.getElementsByTagName('trclass')[0].firstChild.data;
						if (headerClass == "none") {
							headerClass = ((lastRowEven)?"odd":"even")+"Message";
							lastRowEven = !lastRowEven;
						}
						newTR = document.createElement("TR");
						newTR.id = "row"+postID;
						newTR.className = headerClass;
						newTD = document.createElement("TD");
						newTD.className = "authorInfo";
						newTD.innerHTML = thisItem.getElementsByTagName('row1td1')[0].firstChild.data;
						newTR.appendChild(newTD);
						newTD = document.createElement("TD");
						newTD.className = "subject";
						newTD.innerHTML = thisItem.getElementsByTagName('row1td2')[0].firstChild.data;
						newTR.appendChild(newTD);
						tableNode.appendChild(newTR);
						newTR = document.createElement("TR");
						newTD = document.createElement("TD");
						newTD.colSpan = 2;
						newTD.className = "message";
						newTD.innerHTML = thisItem.getElementsByTagName('row2')[0].firstChild.data;
						newTR.appendChild(newTD);
						tableNode.appendChild(newTR);
						newTR = document.createElement("TR");
						newTR.id = "replylinksforpost"+postID;
						newTD = document.createElement("TD");
						newTD.className = "authorInfo";
						newTD.innerHTML = thisItem.getElementsByTagName('row3td1')[0].firstChild.data;
						newTR.appendChild(newTD);
						newTD = document.createElement("TD");
						newTD.className = "rightAligned";
						newTD.innerHTML = thisItem.getElementsByTagName('row3td2')[0].firstChild.data;
						newTR.appendChild(newTD);
						tableNode.appendChild(newTR);
					}
				}
			} else {
				window.location("viewtopic?id="+topicID+"&page=last#postsend");
			}
		}
	}
}

function getQuote(postID) {
	if (window.ActiveXObject) {
		xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlReq = new XMLHttpRequest();
	}
	if (xmlReq) {
		xmlReq.onreadystatechange = getQuoteXmlHttpRequestStateChange;
		xmlReq.open("GET", '/extranet/forum/xml/getquote?id='+postID, true);
		xmlReq.send(null);
	}
}

// Handle onreadystatechange events from the xmlHttpRequest
function getQuoteXmlHttpRequestStateChange() {
	if (xmlReq.readyState == 4) {
		if (xmlReq.status == 200) {
			response = xmlReq.responseXML.documentElement;
			postID = response.getElementsByTagName('postid')[0].firstChild.data;
			quoteText = response.getElementsByTagName('quotetext')[0].firstChild.data;
			updateQuoteField(postID, quoteText);
		} else {
			updateQuoteField(lastReply, false);
		}
	}
}

function updateQuoteField(postID, theText, response) {
	var replyArea = document.getElementById("replytext"+postID);
	replyArea.disabled = false;
	replyArea.style.color = "#000000";
	if (theText) replyArea.value = theText;
	else replyArea.value = "[The post could not be quoted!]";
	replyArea.focus();
}