LOGIN = "* You must be logged in to post, reply, or watch.";
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function setCookie(c_name,value,path,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((path==null) ? "" : ";path="+path)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

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

function deleteCookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function hsP(pid)
{
	var e = document.getElementById(pid);
	
	if (e.style.display == "none" || e.style.display == "")
	{
		e.style.display = "block";
	}
	else
	{
		e.style.display = "none";
	}
}

function hsThr(e, tid)
{
	thr = document.getElementById(tid + "");
	cookie = getCookie("yt");
	if (e.innerHTML == "[-]")
	{
		// collapse post, store tid in cookie
		cookie = cookie + tid + ".";
		setCookie("yt", cookie, "/");
		e.innerHTML = "[+]";
		if (thr != undefined)
			thr.style.display = "none";
	}
	else 	
	{
		// expanding post, delete tid from cookie
		tid = tid + ".";
		cookie = cookie.replace(tid,"");
		setCookie("yt", cookie, "/");
		e.innerHTML = "[-]";
		if (thr != undefined)
			thr.style.display = "block";
	}
}

function rP(mid, pid)
{
	sF('reply');
	subject = document.getElementById(mid).innerHTML;
	//subject = subject.replace(/\<span class.*\.\.\.\(more\)\<\/span\>/, "...(more)");
	subject = subject.replace(/(.*)\<span class="search-text"\>(.*)\<\/span\>(.*)/, "$1$2$3");
	if (subject.length > 45)
	{
		var header = subject.substring(0,44) + "...";
		document.getElementById('reply-header').innerHTML = header;
	
	}
	else
	{
		document.getElementById('reply-header').innerHTML = subject;
	}
	subject = "RE: " + subject;
	document.reply.pid.value = pid;
	document.reply.subject.value = subject;
	document.reply.message.value = "";
	document.reply.subject.focus(); 
	document.reply.subject.select();
}

function nP()
{
	sF('post');
	document.post.subject.value = "- no subject -";
	document.post.message.value = "";
	document.post.subject.focus();
	document.post.subject.select();
}

function sP(tid)
{
	location.href = "/classic/watch.php?t=" + tid;
}

function usP(tid)
{
	location.href = "/classic/unwatch.php?t=" + tid;
}

function displayLoginMessage(message)
{
	e = document.getElementById('error-message');
	e.innerHTML = message;
	e.style.visibility = "visible";
	document.login.email.focus(); 
}
	
function saveSettings()
{
	document.settings.selected_boards.value = "(";
	
	for (i=0; i < document.settings.elements.length; i++) 
	{
		e = document.settings.elements[i];

		if ((e.type == 'checkbox') && (e.name.substring(0,5) == 'board') && e.checked)
		{
			if (document.settings.selected_boards.value != '(')
			{
				document.settings.selected_boards.value += ',';
			}
			document.settings.selected_boards.value += e.value;
		}
	}
	document.settings.selected_boards.value += ")";
	if (document.settings.selected_boards.value == "()")
	{
		return false;
	}
		
	document.settings.submit();
}

function toggleCheckboxValue(ele)
{ 
	if (ele.value == 1)
	{
		ele.value = 0;
	}
	else
	{
		ele.value = 1;
	} 
}

function loadForum()
{
	if (document.post != null)
	{
		cookie = getCookie("yb");
		if (cookie != "")
		{
			document.post.board.selectedIndex = cookie;
		}
	}
	document.getElementById("anchor").focus();
}

function validateSearch()
{
	e = document.getElementById("search-box");
	if (e.value.length <= 3)
	{
		document.getElementById("search-message").innerHTML = "* search term must be <strong>longer than 3 characters</strong>";
		e.focus();
		e.select();
		return false;
	}
	else
	{
		document.search.submit();
	}
	return true;
}

function selectBoard()
{
	index = document.post.board.selectedIndex;
	setCookie("yb", index, "/");
}

function validatePost()
{
	var subj = document.post.subject.value;
	var msg = document.post.message.value;

	if (((trim(subj) == "") || (subj == "- no subject -")) && (trim(msg) == ""))
	{
		return false;
	}
	
	/*str = document.post.subject.value;
	str = str.replace(/</g,"&lt;");
	str = str.replace(/>/g,"&gt;");
	document.post.subject.value = str;
	
	str = document.post.message.value;
	str = str.replace(/</g,"&lt;");
	str = str.replace(/>/g,"&gt;");
	
	document.post.message.value = str;*/

	document.post.submit();
	return false;
}

function validateReply()
{
	var subj = document.reply.subject.value;
	var msg = document.reply.message.value;

	if (((trim(subj) == "") || (subj == "- no subject -")) && (trim(msg) == ""))
	{
		return false;
	}
	
	/* str = document.reply.subject.value;
	str = str.replace(/</g,"&lt;");
	str = str.replace(/>/g,"&gt;");
	document.reply.subject.value = str;
	
	str = document.reply.message.value;
	str = str.replace(/</g,"&lt;");
	str = str.replace(/>/g,"&gt;");
	document.reply.message.value = str;
	*/

	document.reply.submit();
	return false;
}
/*
function checkTextbox(tb,ta)
{
    if (tb.value.length > 185)
    {
        ta.focus();
        ta.value = tb.value.substring(185, 200) + ta.value;
        tb.value = tb.value.substring(0,185);
    }
}
*/

function checkTextbox(tb,ta)
{
    if (tb.value.length > 185)
    {
		ta.focus(); 
		
		var re = /(.*)\s(.*)$/;
		var arr = re.exec(tb.value);
		
		ta.value =  arr[2] + ta.value;
		tb.value = arr[1];
    }
}

function hF()
{
	document.getElementById("footer-panel").style.height="30px";
	document.getElementById("footer-panel").style.paddingTop="0";
	document.getElementById("reply-form").style.display = "none";
	document.getElementById("post-form").style.display = "none";
	document.body.style.paddingBottom = "50px";
	document.getElementById("content").style.bottom = "50px";
	document.getElementById("footer").style.height = "50px";
	document.getElementById("footer-panel-button").style.paddingTop = "7px";
}

function sF(formName)
{
	document.getElementById("footer-panel").style.height="180px";
	document.getElementById("footer-panel").style.paddingTop = "5px";
	document.body.style.paddingBottom = "200px";
	document.getElementById("content").style.bottom = "200px";
	document.getElementById("footer").style.height = "200px"; 
	document.getElementById("footer-panel-button").style.paddingTop = "5px"; 
	
	if (formName == "post")
	{
		document.getElementById("reply-form").style.display = "none";
		document.getElementById("post-form").style.display = "block"; 
	}
	else
	{
		document.getElementById("reply-form").style.display = "block";
		document.getElementById("post-form").style.display = "none";
	}
	
}
	
function validatePassword(p1,p2,f)
{
	if (p1.value != p2.value)
	{
		document.getElementById("password-message").innerHTML = "* Your passwords did not match. Please try again.";
		return false;
	}
	else
	{
		f.submit();
	}
}

function changeView(e)
{
	if (e.value != "-1")
	{
		setCookie("yv", e.value, "/");
		window.location.reload();
	}
}



function changeSort(e)
{
	setCookie("ys", e.value, "/");
	window.location.reload();
}

function textCounter(e, counterId, maxlimit)
{
	counter = document.getElementById(counterId);	
	counter.style.visibility = "visible";
	if (e.value.length > maxlimit)
	{
		e.value = e.value.substring(0, maxlimit);
		counter.innerHTML = "(0 characters left)";
	}
	else
	{
		count = maxlimit - e.value.length; 
		counter.innerHTML = "(" + count + " characters left)";
	}
}
