function getCookie(name) {

    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    
    if (begin == -1) {
    
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
        
    } else {
        
        begin += 2;
        
    }
    
    var end = document.cookie.indexOf(";", begin);
    
    if (end == -1) {
    
        end = dc.length;
        
    }
    
    return unescape(dc.substring(begin + prefix.length, end));
    
}


function populateForm() {

	if(document.getElementById('username') != null) {
	
		document.getElementById('username').value = getCookie("username");
		document.getElementById('password').value = getCookie("password");
		
		if(getCookie("username") == null) document.getElementById('username').value = "";
		if(getCookie("password") == null) document.getElementById('password').value = "";
	
	}

}


function showIntro() {

	if(getCookie("username") != "" && getCookie("username") != null) {
	
		document.write('<div style="height:10px;"></div>');
		
	} else {
	
		document.write('<p style="font-size:smaller;"><b>Welcome.</b> This board is designed as a research tool for exposing e-Watchman (aka Robert King) and his <a href="http://www.jehovahsjudgment.co.uk/watchtower-un-ngo/">erroneous teachings</a> and <a href="http://thetruthaboutthetruthaboutthetruth.blogspot.com/2006/07/does-policy-of-watchtower-create-safe.html">outright lies</a>. This place is for real <i>Jehovah\'s Witnesses only</i>. Some of us used to be followers of e-Watchman but left him and returned to the truth as taught by Jehovah\'s Witnesses. If you are a member of the public or an ex-JW, please go elsewhere as this is not for you.</p>');
	
	}

}



function showHoursCount() {

		var clock = new Date().getTime(); // todays date
		var timeStart = new Date(2006, 9, 5).getTime(); //  date
		clock = (clock - 0) - (timeStart - 0); // todays date minus first date
		
		clock = (clock / 1000 / 60 / 60);
		if(clock <= 999) clock = "0" + clock;
		clock = clock + "";
	
		document.getElementById('number1').src = "counter" + clock.substr(0,1) + ".gif";
		document.getElementById('number2').src = "counter" + clock.substr(1,1) + ".gif";
		document.getElementById('number3').src = "counter" + clock.substr(2,1) + ".gif";
		document.getElementById('number4').src = "counter" + clock.substr(3,1) + ".gif";

}


function showTopMenu() {

	document.write('<span style="color:white;">');

	if(getCookie("username") != "" && getCookie("username") != null) {
	
		document.write('You are logged in as [ <a style="color:white;" href="viewprofile.php?user=' + getCookie("username") + '" title="Click to view your public profile."><b>' + getCookie("username") + '</b></a> ].<br/>');
			
		document.write('<input type="button" value="Edit profile" onclick="window.location=\'editprofile.php\';" ');
		document.write('onmouseover="this.style.borderColor=\'black\';this.style.backgroundColor=\'#E0E0E0\';" ');
		document.write('onmouseout="this.style.borderColor=\'#808080\';this.style.backgroundColor=\'#ECECEC\';" ');
		document.write('onfocus="this.style.backgroundColor=\'#ECECEC\';" ');
		document.write('style="background-color:#ECECEC;border:1px outset #808080;font-size:10px;margin-top:4px;" ');
		document.write('title="Click to edit your public profile." ');
		document.write(' /><br/>');
		
		document.write('<input type="button" value="Logout" onclick="window.location=\'logout.php\';" ');
		document.write('onmouseover="this.style.borderColor=\'black\';this.style.backgroundColor=\'#E0E0E0\';" ');
		document.write('onmouseout="this.style.borderColor=\'#808080\';this.style.backgroundColor=\'#ECECEC\';" ');
		document.write('onfocus="this.style.backgroundColor=\'#ECECEC\';" ');
		document.write('style="background-color:#ECECEC;border:1px outset #808080;font-size:10px;margin-top:4px;" ');
		document.write('title="Click here to log out." ');
		document.write(' />');
	
	} else {
	

		document.write('<form method="post" action="login.php?sendTo=' + location.href + '" onsubmit="disableLoginButton();">');
		
			document.write('Username: ');
			document.write('<input id="logU" name="logU" type="text" ');
			document.write('onfocus="this.style.backgroundColor=\'#C4DDF6\';" '); 
			document.write('onblur="this.style.backgroundColor=\'#73AAEA\';" '); 
			document.write('onmouseover="this.focus();" '); 
			document.write('style="background-color:#73AAEA;border:1px inset #73AAEA;width:60px;font-size:9px;padding:1px;height:13px;" '); 
			document.write('/>');
			
			document.write('<br/>');
			
			document.write('Password: ');
			document.write('<input id="logP" name="logP" type="password" ');
			document.write('onfocus="this.style.backgroundColor=\'#C4DDF6\';" '); 
			document.write('onblur="this.style.backgroundColor=\'#73AAEA\';" '); 
			document.write('onmouseover="this.focus();" '); 
			document.write('style="background-color:#73AAEA;border:1px inset #73AAEA;width:60px;font-size:9px;padding:1px;margin-top:4px;height:13px;" '); 
			document.write('/>');
			
			document.write('<br/>');
			
			document.write('<input type="submit" value="Login" title="Enter your userame and password, then click here to log in." id="loginButton" ');
			document.write('onmouseover="this.style.borderColor=\'black\';this.style.backgroundColor=\'#E0E0E0\';" ');
			document.write('onmouseout="this.style.borderColor=\'#808080\';this.style.backgroundColor=\'#ECECEC\';" ');
			document.write('onfocus="this.style.backgroundColor=\'#ECECEC\';" ');
			document.write('style="background-color:#ECECEC;border:1px outset #808080;font-size:10px;margin-top:4px;" ');
			document.write('/> ');
			
			document.write('<input type="button" value="Register" title="Enter your desired username above then click here to register it." onclick="window.location=\'register.php?desiredUsername=\' + getElementById(\'logU\').value;" ');
			document.write('onmouseover="this.style.borderColor=\'black\';this.style.backgroundColor=\'#E0E0E0\';" ');
			document.write('onmouseout="this.style.borderColor=\'#808080\';this.style.backgroundColor=\'#ECECEC\';" ');
			document.write('onfocus="this.style.backgroundColor=\'#ECECEC\';" ');
			document.write('style="background-color:#ECECEC;border:1px outset #808080;font-size:10px;margin-top:4px;" ');
			document.write('/>');
		
			document.write('</span>');
		
		document.write('</form>');
		
		
	}
	
	document.write('</span>');

}


function formatTime(timestamp) {

	//document.write("JAVASCRIPT TIMESTAMP");
	
	var theDate = new Date(timestamp * 1000);
	
	document.write("&nbsp;");
	if(theDate.getHours() < 10) document.write("0");
	document.write(theDate.getHours());
	document.write(":");
	if(theDate.getMinutes() < 10) document.write("0");
	document.write(theDate.getMinutes());
	
	document.write("&nbsp;-&nbsp;");
	
	if(theDate.getDate() < 10) document.write("0");
	document.write(theDate.getDate());
	document.write("/");
	if(theDate.getMonth() < 10) document.write("0");
	document.write(theDate.getMonth() + 1);
	document.write("/");
	document.write(theDate.getFullYear());
	document.write("&nbsp;");


}


function longFormatTime(timestamp) {
	
	var theDate = new Date(timestamp * 1000);
	
	document.write(theDate.toLocaleString());

}




function isItMe(username) {

	if(getCookie("username") != "" && getCookie("username") != null) {
	
		if(getCookie("username") == username) {
		
			document.write("<b>" + username + "</b>");
			
		} else {
	
			document.write(username);
	
		}
	
	} else {
	
		document.write(username);
	
	}

}


function disableSubmitButton() {

	var theButton = document.getElementById('submitButton');
	theButton.value = "Please wait...";
	theButton.disabled = true;

}


function disableLoginButton() {

	var theButton = document.getElementById('loginButton');
	theButton.value = "Please wait...";
	theButton.disabled = true;

}


function redirectToPost(uri) {

	window.location = uri;

}


function showPreview() {

	var prevPane = window.open(null,"preview","width=500,height=350,scrollbars=yes,location=no,status=no,resizable=yes,menubar=no");
	
	var previewedSubject = document.getElementById('subject').value;
	var previewedText = document.getElementById('message').value;
	
	previewedText = previewedText.replace(/\n/g,"<br/>");
	
	prevPane.document.write("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"./includes/style.css\" /><title>Preview your message</title></head><body>");
	prevPane.document.write("<h3>" + previewedSubject + "</h3>");
	prevPane.document.write(previewedText);
	prevPane.document.write("<p>&nbsp;</p>");
	prevPane.document.write("<p style=\"text-align:center;\"><input type=\"button\" onclick=\"window.close()\" value=\"Close window\" onmouseover=\"this.style.borderColor='black';this.style.backgroundColor='#E0E0E0';\" onmouseout=\"this.style.borderColor='#808080';this.style.backgroundColor='#ECECEC';\" onfocus=\"this.style.backgroundColor='#ECECEC';\" style=\"background-color:#ECECEC;border:1px outset #808080;\" /></p>");
	prevPane.document.write("</body></html>");
	
	prevPane.document.close();

}


function removeCookies() {

	document.cookie = "username=; expires=Sat, 1 Jan 2005 00:00:01 GMT;";
	
	document.cookie = "password=; expires=Sat, 1 Jan 2005 00:00:01 GMT;";
	
	setTimeout('window.location=\'./\'', 1500);
	
}


var timer = null;


function stopClock() {

	if(document.getElementById('clockDisplay') != null) clearTimeout(timer);
	
}


function startClock() {

	if(document.getElementById('clockDisplay') != null) {

		var time = new Date();
		var clock = time.toLocaleString();
		document.getElementById('clockDisplay').value = clock;
		timer = setTimeout("startClock()",1000);
	
	}

}


function add(smilieCode) {

	document.getElementById('message').value = document.getElementById('message').value + " " + smilieCode;

}


function toggleCheckBox(number) {

	var checkBox = document.getElementById('agree' + number);
	
	if(checkBox.checked == true) {
		checkBox.checked = false;
	} else if(checkBox.checked == false) {
		checkBox.checked = true;
	}

}
