var special_prefix = /^(business|living|community|kids)$/;
var current_prefix = '';
var category_name = '';
var board_name = '';
// Tidy Up Nav-Tree (Styles to Move Up)
if(document.getElementById('nav_tree')) {
	var n = document.getElementById('nav_tree');
	n.innerHTML = n.innerHTML.replace(/surrey-online\.co\.uk/, 'Forum Home Page');
	// n.innerHTML = "<a href=\"http://www.surrey-online.co.uk\">surrey-online.co.uk</a> &gt; " + n.innerHTML;
	n.innerHTML = n.innerHTML.replace(/::/g, '&gt;');
	if(n.innerHTML.match(/\/category\/(.+?)("|\s)>(.+?)</)) {
		var category_id = RegExp.$1;
		var category_name = RegExp.$3;
		if(category_id.match(special_prefix)) {
			current_prefix = "/" + RegExp.$1;
		}
	}
	if(n.innerHTML.match(/(&gt;|>)\s((\w|\s|&amp;|-|_|\.|,|~)+)$/i)) {
		var board_name = RegExp.$2;
	}
	if(n.innerHTML.match(/\/board\/(.+?)("|\s)>(.+?)</)) {
		var board_name = RegExp.$3;
	}
}

function $(id) {
	return document.getElementById(id);
}

function show_window(url) {
	if(window.open(url,'_blank','height=500,width=500,top=250,left=250,resizable=0,location=no,scrollbars=1',false)) {
		return false;
	}
}

var iExpandTimer = false;
var register_box_status = ("undefined" != typeof register_box_status)? register_box_status : 'closed';
function open_close_register_form() {
	box = $('register_form');
	current_height = box.style.height.replace(/[^0-9]/gi, "");
	clearTimeout(iExpandTimer);
	if(register_box_status == 'open') {
		box.style.overflow = 'hidden';
		box.style.display = 'block';
		register_box_status = 'closed';
		iExpandTimer = setTimeout('collapse(\'register_form\', \''+current_height+'\')', 10);
		document.cookie = 'register_form=closed';
	} else {
		box.style.overflow = 'hidden';
		if(current_height == '' || current_height < 5) {
			box.style.height = '1px';
			current_height = 5;
		}
		box.style.display = 'block';
		register_box_status = 'open';
		iExpandTimer = setTimeout('expand(\'register_form\', \''+ current_height +'\', \'198\')', 10);
		$('captcha_image').src = 'http://surrey-online.co.uk/images/captcha.php?bg=e5e5e5&id=' + $('captcha_id').value;
		document.cookie = 'register_form=open';
	}
	return false;
}
function expand(id, cur, max) {
	cur = parseInt(cur);
	max = parseInt(max);
	var box = $(id);
	if(cur == max) {
		box.style.height = cur + 'px';
		return;
	}
	box.style.height = cur + 'px';
	box.style.display = 'block';
	cur+=8;
	if(cur > max)
		cur = max;
	base_speed = (arguments[3])? arguments[3] : 100;
	percent = cur / max;
	speed = percent * base_speed;
	if(speed < 10)
		speed = 10;
	iExpandTimer = setTimeout('expand(\''+ id +'\', \''+ cur +'\', \''+ max +'\', \''+ base_speed +'\')',speed);
}
function collapse(id, cur) {
	var min = (arguments[2])? arguments[2] : -10;
	var box = $(id);
	cur = parseInt(cur);
	if(cur <= 1) {
		box.style.display = 'none';
		return;
	}
	box.style.height = cur + 'px';
	cur-=8;
	base_speed = (arguments[3])? arguments[3] : 150;
	speed = cur - min;
	if(speed > 100)
		speed = 100;
	speed = base_speed - speed;
	speed = base_speed;
	if(cur >= min)
		iExpandTimer = setTimeout('collapse(\''+ id +'\', \''+ cur +'\', \''+ min +'\', \''+ base_speed +'\')', speed);
	else
		box.style.height = min + 'px';
}


// Remove Borders
var table = document.getElementsByTagName('table');
for(i=0; i<table.length; i++) {
	if(table.item(i).className == 'border') {
		table.item(i).cellSpacing = '0';
	}
}


if(!location.href.match(/(action|board)/)) {
	// Home Page
	
	// Remove Category Borders
	var tables = get('table', TAG, get('forum', ID));
	for(t=0; t<tables.length; t++) {
		if(tables.item(t).align == 'center' && tables.item(t).className == 'border') {
			var a = get('a', TAG, tables.item(t));
			if(a.item(0) && a.item(0).name) {
				tables.item(t).cellSpacing = '0';
				tables.item(t).className = '';
				a.item(0).parentNode.innerHTML += '<span class="posts_heading">Posts:</span><span class="topics_heading">Topics:</span><span class="recent_heading">Last Post:</span>';
			}
		}
	}
	
	// Category Specific Headers & On/Off Icons
	var td = document.getElementsByTagName('td');
	for(i=0; i<td.length; i++) {
		if(td.item(i).className.match(/(categorytitle|boardstatus)/)) {
			var a = td.item(i).getElementsByTagName('a');
			if(a.item(0) && a.item(0).name) {
				if(a.item(0).name.match(special_prefix)) {
					current_prefix = "/" + a.item(0).name;
					td.item(i).style.backgroundImage = 'url(http://surrey-online.co.uk/images/forums'+ current_prefix +'/header.png)';
				} else {
					current_prefix = "";
				}
			}
			var img = td.item(i).getElementsByTagName('img');
			if(img.item(0)) {
				if(img.item(0).src.match(/^(.+?forums)(\/(no_)?new_posts.png$)/)) {
					img.item(0).src = RegExp.$1 + current_prefix + RegExp.$2;
				}
			}
		}
	}
		
	// Remove Login Box
	if(document.loginForm)
		document.loginForm.style.display = 'none';
}
if(location.href.match(/view_topic/)) {
	// Display first splitter (if page 1?)
	var div = get('div', TAG);
	for(d=0; d<div.length; d++) {
		if(div.item(d).className == 'post_splitter') {
			div.item(d).style.display = 'block';
			div.item(d).style.height = '9px';
			break;
		}
	}
	
	// Prefix Posted / Started Dates
	var td = get('td', TAG);
	for(t=0; t<td.length; t++) {
		if(td.item(t).className == 'subject') {
			td.item(t).innerHTML = td.item(t).innerHTML.replace(/<\/strong>\s\(/gi, '</strong><br />(');
//			td.item(t).innerHTML = td.item(t).innerHTML.replace(/<b>(Today|Yesterday)<\/b>/gi, '<strong>$1</strong>');
//			td.item(t).innerHTML = td.item(t).innerHTML.replace(/>\s\((\d+)/gi, '> (Posted On: $1');
		}
	}


	var first_post = 0;
	var span = get('span', TAG);
	for(s=0; s<span.length; s++) {
		if(span.item(s).className == 'mp_name') {
			span.item(s).parentNode.parentNode.className = span.item(s).parentNode.parentNode.className + ' mp_cell';
		}
	}
	
	var a = get('a', TAG);
	for(s=0; s<a.length; s++) {
		if(a.item(s).className == 'linktopost') {
			post_bottom_link = a.item(s).parentNode;
			post_bottom_link.className = 'bottom_post_buttons';
			if(signature = post_bottom_link.parentNode.parentNode.nextSibling) {
				signature = signature.firstChild;
				if(signature.className.match(/signature/i)) {
					if(first_post) {
						post_bottom_link.style.borderBottomColor = '#969696';
						post_bottom_link.style.borderBottomStyle = 'dashed';
						signature.firstChild.nextSibling.className = 'signature_holder';
					} else {
						signature.style.borderTopColor = '#969696';
						signature.style.borderTopStyle = 'dashed';
						signature.style.borderTopWidth = '1px';
					}
				}
			}
			post_bottom_link.removeChild(a.item(s).parentNode.firstChild);
			while(post_bottom_link.firstChild.nodeName.toLowerCase() != "a") {
				post_bottom_link.removeChild(post_bottom_link.firstChild);
			}
			if(!first_post) {
				// Add print Button
				var board_id = (location.href.match(/board\/((\w|-)+)(\/|$|#)/))? RegExp.$1 : "";
				var topic_id = (location.href.match(/topic\/(\d+)(\/|$|#)/))? RegExp.$1 : "";
				var print_url = "/action/print/board/"+ board_id +"/topic/" + topic_id;
				
				if(post_bottom_link.innerHTML.match(/IP: /)) {
					post_bottom_link.lastChild.innerHTML = '<div><a href="'+ print_url +'"><img src="http://surrey-online.co.uk/images/forums/print_icon.png" alt="Print Topic" /></a>' + post_bottom_link.lastChild.innerHTML.replace(/^\s?-\s?(IP: Hidden)?/, '')+'</div>';
				} else {
					post_bottom_link.innerHTML += '<span class="ip"><div><a href="'+ print_url +'"><img src="http://surrey-online.co.uk/images/forums/print_icon.png" alt="Print Topic" /></a></div></span>';
				}
			}
			first_post++;
		}
	}

	// Remove Print Button And Rearrange Top & Bottom Bars
	var top_bar = true;
	var a = get('a', TAG);
	for(i=0; i<a.length; i++) {
		if(a.item(i).innerHTML == 'Printable View') {
			a.item(i).style.display = 'none';
			var title_row = a.item(i).parentNode;
			while(title_row.parentNode && title_row.className != "title1") {
				//alert(top_bar +'. '+ title_row.nodeName + ' => '+ title_row.className);
				title_row = title_row.parentNode;
			}
			if(top_bar) {
				title_row.className = 'categorytitle';
				if(current_prefix != "") {
					title_row.style.backgroundImage = 'url(http://surrey-online.co.uk/images/forums'+ current_prefix +'/header.png)';
				}
				title_row.vAlign = 'bottom';
				title_row.getElementsByTagName('td').item(0).innerHTML = '<a class="board_name">'+ board_name +'</a>';
				title_row.getElementsByTagName('td').item(1).align = 'right';
				title_row.getElementsByTagName('td').item(1).width = '300';
				title_row.getElementsByTagName('td').item(1).style.textAlign = 'right';
				title_row.getElementsByTagName('td').item(1).style.paddingRight = '4px';
				top_bar = false;
				i++;
			} else {
				// Bottom Bar
				title_row.className = 'topic_bottom_bar';
				
				// Add to top image & Login / Register Info for Guests
				guest_help = '';
				if(vf_username == 'guest') {
					// Check if the Topic is Not Locked and We're not in a Locked Board
					if(!location.href.match(/board\/(local-news|service-submiss)\//i) && !get('locked_info', ID)) {
						guest_help = '<div style="color: #fff; font-size: 16px; position: relative; top: '+ ((first_post == 1)? '0' : '-15') +'px; left: 15px; height: 100px;">You must be logged in to post a reply to this topic.<ul style="padding-left: 17px;"><li><a href="/action/login" onclick="$(\'footer_login\').style.display = \'block\'; this.parentNode.style.display = \'none\'; return false;" style="padding: 0px;">Login to your account</a></li>';
						guest_help += '<li id="footer_login" style="display: none; list-style: none; margin-left: -10px;"><form method="post" id="header_login" action="http://www.surrey-online.co.uk/" style="height: 25px;"><input type="hidden" name="redirect" value="'+ location.href +'" /><div style="float: left; color: #333; font-weight: bold; text-transform: uppercase; font-size: 8px; padding-left: 3px; padding-right: 4px;">Login:</div>	<div class="input">Username: <input type="text" name="user" size="15" /></div><div class="input">Password:  <input type="password" name="pass" size="15" /></div><div style="float: left; position: relative; top: -14px; left: -9px;"><a href="http://forum.surrey-online.co.uk/action/forgotten_password" rel="nofollow"><img src="http://surrey-online.co.uk/images/forgotten-password.png" style="position: relative; top: 5px;" alt="Request a new password" title="Request a new password" /></a><input type="image" src="http://surrey-online.co.uk/images/login-arrow.png" class="submit input-image" /></div></form><br style="clear: both;" /></li>';
						guest_help += '<li><a href="#" onclick="if(register_box_status == \'closed\') { open_close_register_form(); } window.scroll(0, 0); return false;" style="padding: 0px;">Register a new Account for Free</a></li><li><a href="http://forum.surrey-online.co.uk/board/bugs/topic/388/action/view_topic/forum-structure-how-forums-work" target="_blank" style="padding: 0px;">Get Help with using these Forums</a></li></ul>';
						guest_help += '</div>';
					}
				}
				a.item(i).parentNode.innerHTML = guest_help + a.item(i).parentNode.innerHTML +'<div class="backtotop"><div onclick="window.scroll(0,0);"><img src="http://surrey-online.co.uk/images/forums/to_top_icon.png" alt="Back to Top" /></div>';
				break;
			}
		}
	}
} else if(location.href.match(/board\/((\w|-)+)(\/page\/\d+)?\/?(#\w+)?$/)) {
	// New / No-New Icons
	var reply_icons = get('reply_icons', NAME);
	for(n=0; n<reply_icons.length; n++) {
		reply_icons[n].src = reply_icons[n].src.replace(/forums\//, 'forums' + current_prefix + '/');
	}
	// Remove Print Button And Rearrange Top & Bottom Bars
	var top_bar = true;
	var a = get('a', TAG);
	for(i=0; i<a.length; i++) {
		if(a.item(i).innerHTML == 'Search Board') {
			var title_row = a.item(i).parentNode;
			while(title_row.className != "title1") {
				title_row = title_row.parentNode;
			}
			if(current_prefix != "") {
				title_row.style.backgroundImage = 'url(http://surrey-online.co.uk/images/forums'+ current_prefix +'/header.png)';
			}
			title_row.vAlign = 'bottom';
			title_row.className = 'categorytitle';
			title_row.getElementsByTagName('td').item(1).innerHTML = title_row.getElementsByTagName('td').item(0).innerHTML + title_row.getElementsByTagName('td').item(1).innerHTML;
			title_row.getElementsByTagName('td').item(0).innerHTML = '<a class="board_name">'+ board_name +'</a>';
			title_row.getElementsByTagName('td').item(1).align = 'right';
			title_row.getElementsByTagName('td').item(0).width = '25%';
			title_row.getElementsByTagName('td').item(1).width = '75%';
			title_row.getElementsByTagName('td').item(1).style.textAlign = 'right';
			title_row.getElementsByTagName('td').item(1).style.paddingRight = '4px';
			// Put Column Headers into td.item(1)
			var title_row = title_row.parentNode.nextSibling;
			title_td = title_row.getElementsByTagName('td');
			for(t=0; t<title_td.length; t++) {
				title_td.item(t).className = 'title2';
			}
			top_bar = false;
			break;
		}
	}

	var first_post = true;
	var span = get('span', TAG);
	for(s=0; s<span.length; s++) {
		if(span.item(s).className == 'mp_name') {
			span.item(s).parentNode.parentNode.className = span.item(s).parentNode.parentNode.className + ' mp_cell';
		}
		if(span.item(s).className == 'ip') {
			span.item(s).parentNode.className = 'bottom_post_buttons';
			span.item(s).parentNode.removeChild(span.item(s).parentNode.firstChild);
			while(span.item(s).parentNode.firstChild.nodeName.toLowerCase() != "a") {
				span.item(s).parentNode.removeChild(span.item(s).parentNode.firstChild);
			}
			if(first_post) {
				// Add print Button
				var board_id = (location.href.match(/board\/(\w+)(\/|$|#)/))? RegExp.$1 : "";
				var topic_id = (location.href.match(/topic\/(\d+)(\/|$|#)/))? RegExp.$1 : "";
				var print_url = "/action/print/board/"+ board_id +"/topic/" + topic_id;
				span.item(s).innerHTML = '<div><a href="'+ print_url +'"><img src="http://surrey-online.co.uk/images/forums/print_icon.png" alt="Print Topic" /></a>' + span.item(s).innerHTML.replace(/^\s?-\s?(IP: Hidden)?/, '')+'</div>';
				first_post = false;
			}
		}
	}
}



/* Email to Friend */
function validfriend(form) {
	errors = '';
	if(form.name.value=='') {
	  errors += "You have not entered your name\n";
	}
	 if(form.email.value=='') {
	  errors += "You have not entered your email address\n";
	}
	 if(form.message.value=='') {
	  errors += "You have not entered a message\n";
	}
	   if(form.friendemail.value=='') {
	  errors += "You have not entered an email to send this page to\n";
	}
	if(form.terms.checked!=true) {
	  errors += "You have not agreed to our terms and conditions\n";
	}
	if(errors!='') { 
		alert(errors);
		return false; 
	}
	return true; 
}
function quick_email_friend() {
	contact_form = '<div class="quick_contact">';
	contact_form += '<div class="quick_contact_title" style="margin-left:19px!important;">';
	contact_form += 'Email to a friend';
	contact_form += '</div>';
	contact_form += '</div>';
	contact_form += '<div class="quick_contact_message">';
	contact_form += '<form action="http://www.surrey-online.co.uk/contact/friend" method="post" id="quick_email_friend" onSubmit="return validfriend(this);">';
	contact_form += '<div class="quick_contact_left">';
	contact_form += '<input type="hidden" name="title" value="'+ document.title +'" class="blank" />';
	contact_form += '<input type="hidden" name="page" value="'+ location.href +'" class="blank" />';
	contact_form += '<label for="name">your name</label>';
	contact_form += '<input type="text" name="name" id="name" value="'+ vf_displayname +'" />';
	contact_form += '<label for="email">your email address</label>';
	contact_form += '<input type="text" name="email" id="email" value="" />';
	contact_form += '<label for="email">friend\'s email</label>';
	contact_form += '<input type="text" name="friendemail" id="friendemail"  />';
	contact_form += '</div><div class="quick_contact_right">';
	contact_form += '<label for="message">your message</label>';
	contact_form += '<textarea name="message" id="message" rows="5" cols="20"></textarea>';
	contact_form += '</div>';
	contact_form += '<br style="clear: both;" />';
	contact_form += '<div class="terms_agree">';
	contact_form += '<input type="checkbox" name="terms" value="1" class="checkbox" />';
	contact_form += '<a href="http://www.surrey-online.co.uk/popup/terms-and-conditions" target="_blank" onclick="return show_window(this.href);">I agree with surrey-online.CO.UK<br />';
	contact_form += 'Terms &amp; Conditions</a>';
	contact_form += '</div>';
	contact_form += '<br style="line-height: 12px;" />';
	contact_form += '<input type="image" src="http://www.surrey-online.co.uk/images/contact/submit.png" class="blank" style="float: left;" />';
	contact_form += '<input type="image" src="http://www.surrey-online.co.uk/images/contact/contact-close.png" onclick="close_friend_contact(); return false;" class="blank" style="float: right; position: relative; right: 18px;" />';
	contact_form += '<br style="clear: both;" />';
	contact_form += '</form>';
	contact_form += '</div>';
	contact_form += '<div class="quick_contact_footer">&nbsp;</div>';
	return contact_form;
}

function quick_friend(obj, name, url) {
	var position = get_position(obj);
		position[0] += (arguments[3])? arguments[3] : 0;
		position[1] += (arguments[4])? arguments[4] : 0;
	if($('friend_contact_holder')) {
		// Update Existing Form
		form_holder = $('friend_contact_holder');
			form_holder.style.display = '';
			form_holder.style.top = position[0] + 'px';
			form_holder.style.left = position[1] + 'px';
	} else {
		// Append new form to the page & try to position it
		var form_holder = document.createElement('div');
			form_holder.setAttribute('id', 'friend_contact_holder');
			form_holder.style.top = position[0] + 'px';
			form_holder.style.left = position[1] + 'px';
			form_holder.innerHTML = quick_email_friend();
		document.body.appendChild(form_holder);
	}
	return false;
}
function close_friend_contact() {
	if($('friend_contact_holder')) {
		$('friend_contact_holder').style.display = 'none';
	}
}
