// Functions to (s/g)et timezone
function Get_TZCookie() {
  var search = "tz=";
  if (document.cookie.length > 0) { // if there are any cookies
    offset = document.cookie.indexOf(search);
    if (offset != -1) { // if cookie exists
      offset += search.length;
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1)
         end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end));
      }
    }
  }

function Set_TZCookie(newtz) {
  var now = new Date();
  var nowTime = now.getTime();
  var expires = new Date(now.getTime() + (5 * 365 * 86400000));
  document.cookie = "tz=" + newtz + ";path=/";
  document.cookie = "tz=" + newtz + ";expires=" + expires.toGMTString() + ";path=/";
  }

var now = new Date();
var nowtz = now.getTimezoneOffset() / -60;
var tz = Get_TZCookie();

if (!tz || tz == null || tz != nowtz) Set_TZCookie(nowtz);
// End Timezone functions


// Get current browser dimensions
var viewportwidth = 0, viewportheight = 0;

function getViewPort() {
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof( window.innerWidth ) == 'number') {
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}

	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
		}

	// older versions of IE
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
		}
	}


function replace(string,text,by) {
	var strLength = string.length, txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0)) return string;

	var i = string.indexOf(text);
	if ((!i) && (text != string.substring(0,txtLength))) return string;
	if (i == -1) return string;

	var newstr = string.substring(0,i) + by;

	if (i+txtLength < strLength)
		newstr += replace(string.substring(i+txtLength,strLength),text,by);

	return newstr;
}

// Begin Message editing
function doArray() {
	var arylnth = doArray.arguments.length;
	for (i = 0; i < arylnth; i++ ) { this[i]=doArray.arguments[i]; }
}

function privatenote(myField) {
	enclose_or_prompt(myField, '[Private note: ', ']', 'Please enter the private note:');
}

// Heaps of thanks to Alex King (http://www.alexking.org) for some of the Mozilla code!
function enclose_or_prompt(myField, start_code, end_code, prompt_text) {

	// Simple IE support
	if (document.selection) {

		myField.focus();

		strSelection = document.selection.createRange();

		if (strSelection.text.length > 0)	{
			strSelection.text = start_code + strSelection.text + end_code;
		} else {
			var phrase = prompt(prompt_text, "");
			if (phrase != "" && phrase != null) document.selection.createRange().text = start_code + phrase + end_code;
		}
	}
	// Far far more complicated Mozilla/Netscape support
	else if (myField.selectionStart || myField.selectionStart == '0') {

		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos) + start_code
										+ myField.value.substring(startPos, endPos) + end_code
										+ myField.value.substring(endPos, myField.value.length);

			myField.selectionStart = startPos;
			myField.selectionEnd = endPos + start_code.length + end_code.length;
		} else {
			var phrase = prompt(prompt_text, "");
			if (phrase != "" && phrase != null) {

				myField.value = myField.value.substring(0, startPos) + start_code + phrase + end_code
						+ myField.value.substring(endPos, myField.value.length);

			myField.selectionStart = startPos + start_code.length + end_code.length + phrase.length;
			myField.selectionEnd = myField.selectionStart;
			}
		}

		myField.scrollTop = scrollTop;

	} else {
		var phrase = prompt(prompt_text, "");
		if (phrase != "" && phrase != null) {
			myField.value += start_code + phrase + end_code;
 		}
	}

	stop=0;
	myField.focus();
	return false;
}


function enclose_or_insert(myField, start_code, end_code) {

	if (end_code == undefined) end_code = '';

	// IE support
	if (document.selection) {

		myField.focus();

		strSelection = document.selection.createRange();

		if (strSelection.text.length > 0)	{
			strSelection.text = start_code + strSelection.text + end_code;
		} else {
			document.selection.createRange().text = start_code + end_code;

			if (start_code.length != 0) {
				// unpleasant code to move to the middle of the input
				var range = myField.createTextRange();
				range.moveToBookmark(strSelection.getBookmark());
				range.collapse(false);

				//var range = myField.createTextRange();
				//range.collapse(true);
				range.move('character', -range.moveEnd("character", -myField.value.replace(/\r\n/g, "\n").length) + start_code.length);	// it's amazing what you can do in one line!
				range.select();
			}
		}
	}
	// Mozilla/Netscape support
	else if (myField.selectionStart || myField.selectionStart == '0') {

		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos) + start_code
										+ myField.value.substring(startPos, endPos) + end_code
										+ myField.value.substring(endPos, myField.value.length);

			myField.selectionStart = startPos;
			myField.selectionEnd = endPos + start_code.length + end_code.length;
		} else {
			myField.value = myField.value.substring(0, startPos) + start_code + end_code
					+ myField.value.substring(endPos, myField.value.length);

			if (start_code.length == 0) myField.selectionStart = startPos + end_code.length;
			else myField.selectionStart = startPos + start_code.length; // + end_code.length;

			myField.selectionEnd = myField.selectionStart;
		}

		myField.scrollTop = scrollTop;

	} else {
		myField.value += start_code + end_code;
	}

	stop=0;
	myField.focus();
	return false;
}


function enclose_each_line_or_insert(myField, start_code) {

	// Simple IE support
	if (document.selection) {

		myField.focus();

		strSelection = document.selection.createRange();

		if (strSelection.text.length > 0)	{
			if (strSelection.text.indexOf('\r\n') != -1)
				;	// looks ok, IE on Windows etc.
			else if (strSelection.text.indexOf('\r') != -1)
				strSelection.text = strSelection.text.replace ( /\r/g, "\r\n" );	// IE on Mac
			else
				;	// no newlines, or just '\n' which is fine (FireFox)

			strSelection.text = strSelection.text.replace(/^/, start_code).replace(/\r\n/g, "\r\n" + start_code);
		} else {
			document.selection.createRange().text = start_code;
		}
	}
	// Far far more complicated Mozilla/Netscape support
	else if (myField.selectionStart || myField.selectionStart == '0') {

		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			var newString = myField.value.substring(startPos, endPos);
			var fromEnd = myField.value.length - endPos;

			if (newString.indexOf('\r\n') != -1)
				;	// looks ok, IE on Windows etc.
			else if (newString.indexOf('\r') != -1)
				newString.text = newString.replace ( /\r/g, "\r\n" );	// IE on Mac
			else
				;	// no newlines, or just '\n' which is fine (FireFox)

			newString = newString.replace(/^/, start_code).replace(/\n/g, "\n" + start_code);


			myField.value = myField.value.substring(0, startPos) + newString
					+ myField.value.substring(endPos, myField.value.length);

			myField.selectionStart = startPos;
			myField.selectionEnd = myField.value.length - fromEnd;
		} else {
			myField.value = myField.value.substring(0, startPos) + start_code
					+ myField.value.substring(endPos, myField.value.length);

			myField.selectionStart = startPos + start_code.length;
			myField.selectionEnd = myField.selectionStart;
		}

		myField.scrollTop = scrollTop;

	} else {
		myField.value += start_code;
	}

	stop=0;
	myField.focus();
	return false;
}


function enclose_and_each_line_or_insert(myField, start_code, end_code, each_line) {

	// Simple IE support
	if (document.selection) {

		myField.focus();

		strSelection = document.selection.createRange();

		if (strSelection.text.length > 0)	{
			if (strSelection.text.indexOf('\r\n') != -1 || strSelection.text.indexOf('\n') != -1 ) // IE on Windows or FireFox
				strSelection.text = start_code + strSelection.text.replace(/^/, each_line).replace(/\n/g, "\n" + each_line) + end_code;
			else if (strSelection.text.indexOf('\r') != -1)	// IE on Mac
				strSelection.text = start_code + strSelection.text.replace(/^/, each_line).replace(/\r/g, "\r" + each_line) + end_code;
			else
				strSelection.text = start_code + each_line + strSelection.text + end_code;

		} else {
			document.selection.createRange().text = start_code + each_line + end_code;
		}
	}
	// Far far more complicated Mozilla/Netscape support
	else if (myField.selectionStart || myField.selectionStart == '0') {

		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			var newString = myField.value.substring(startPos, endPos);
			var fromEnd = myField.value.length - endPos;

			if (newString.indexOf('\r\n') != -1 || newString.indexOf('\n') != -1 ) // IE on Windows or FireFox
				newString = newString.replace(/^/, each_line).replace(/\n/g, "\n" + each_line);
			else if (newString.indexOf('\r') != -1)	// IE on Mac
				newString = newString.replace(/^/, each_line).replace(/\r/g, "\r" + each_line);
			else
				newString = each_line + newString;

			myField.value = myField.value.substring(0, startPos) + start_code + newString + end_code
					+ myField.value.substring(endPos, myField.value.length);

			myField.selectionStart = startPos;
			myField.selectionEnd = myField.value.length - fromEnd;
		} else {
			myField.value = myField.value.substring(0, startPos) + start_code + each_line + end_code
					+ myField.value.substring(endPos, myField.value.length);

			myField.selectionStart = startPos + start_code.length + each_line.length;
			myField.selectionEnd = myField.selectionStart;
		}

		myField.scrollTop = scrollTop;

	} else {
		myField.value += start_code + each_line + end_code;
	}

	stop=0;
	myField.focus();
	return false;

}

var pre_on;

function toggle_html_text(myField, thisLink, html_code) {

	var insertTag = "<" + (pre_on == 1 ? "/" : "") + html_code + ">";

	// Simple IE support
	if (document.selection) {

		myField.focus();

		strSelection = document.selection.createRange();

		if (strSelection.text.length > 0)	{
			strSelection.text = '<' + html_code + '>' + strSelection.text + '</' + html_code + '>';
		} else {
			document.selection.createRange().text = insertTag;
			pre_on = (pre_on == 1 ? 0 : 1);
		}
	}
	// Far far more complicated Mozilla/Netscape support
	else if (myField.selectionStart || myField.selectionStart == '0') {

		var myField = myField;

		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos) + '<' + html_code + '>'
										+ myField.value.substring(startPos, endPos) + '</' + html_code + '>'
										+ myField.value.substring(endPos, myField.value.length);

			myField.selectionStart = startPos;
			myField.selectionEnd = endPos + (html_code.length * 2) + 5;
		} else {
			myField.value = myField.value.substring(0, startPos) + insertTag
					+ myField.value.substring(endPos, myField.value.length);

			myField.selectionStart = startPos + insertTag.length;
			myField.selectionEnd = myField.selectionStart;
			pre_on = (pre_on == 1 ? 0 : 1);
		}

		myField.scrollTop = scrollTop;

	} else {
  		myField.value += insertTag + (pre_on == 1 ? "\n" : "");
		pre_on = (pre_on == 1 ? 0 : 1);
	}

	thisLink.className = (pre_on == 1 ? "button_on" : "");
//	if (document.all) thisLink.innerText = (pre_on == 1 ? "/pre" : " pre");
//	else if (document.getElementById) thisLink.firstChild.nodeValue = (pre_on == 1 ? "/pre" : " pre");

	stop=0;
	myField.focus();
	return false;
}


function scrollBottom(idName) {

	var input = document.getElementById(idName);

	if (input.setSelectionRange) {
		input.focus();
		input.setSelectionRange(input.value.length, input.value.length);
	} else if (input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', input.value.length);
		range.moveStart('character', input.value.length);
		range.select();
	}
}

// End Message editing

// RPoL Help!
function OpenRPOLHelp(page) {
  var width = 750, height = 550;
  var posx = screen.width - (width + 12), posy = screen.height - (height + 68);

  helpWindow=window.open(page,"rpolhelp",config='height='+height+',width='+width+'top='+posy+',screenY='+posy+',left='+posx+',screenX='+posx+',toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');

  // helpWindow.moveTo(posx,posy);

  // helpWindow.document.focus();
  }


// Sticky List
function OpenStickyList(page) {
  var width = 220, height = 500;
  var posx = screen.width - (width + 12), posy = screen.height - (height + 68);

  jumpWindow=window.open(page,'stickylist',config='height='+height+',width='+width+'top='+posy+',screenY='+posy+',left='+posx+',screenX='+posx+',toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');

  jumpWindow.moveTo(posx,posy);

  // jumpWindow.document.focus();
  }


// RPoL Help!
function OpenRPOLInfoBox(e, page) {
  var width = 400, height = 200;
  var posx = (screen.width - (width + 12)) / 2, posy = (screen.height - (height + 68)) / 2;

  boxWindow=window.open('/help/box.php?' + page,"rpolbox",config='height='+height+',width='+width+'top='+posy+',screenY='+posy+',left='+posx+',screenX='+posx+',toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');

  if (window.event || window.Event || document.layers)
    {
    if(!e) e = window.event||window.Event;

    boxWindow.moveTo(e.screenX + 10,e.screenY - height - (((document.all)&&(navigator.appVersion.indexOf("MSIE 7.")!=-1)) ? 80 : 55));
    }
  else
    {
    boxWindow.moveTo(posx,posy);
    }

  // boxWindow.focus();
  }


// Protect the email addresses - Perl decrements all the characters, JS increments
function protect_email(email, mail_blurb) {
	var i = 0, real_email = '';

	for (var text = '',i=0;i<email.length;i++) {
		real_email += String.fromCharCode(email.charCodeAt(i)+1);
	}

	document.write("<a href=mailto:" + real_email + ">");

	if (mail_blurb)
		document.write(mail_blurb + "</a>");
	else
		document.write(real_email + "</a>");
}

// np: no portrait image error handler
function np(that_img) {
  that_img.src='/images/no-portrait.gif';
}

// Make text visible
function vis(this_text,vis) {
	if (vis)
		this_text.className=null;
	else
		this_text.className='hide';
}

// Hide/Show ID blocks.
function toggleLayer(whichLayer) {

	var style2;

	if (document.getElementById) {
		// this is the way the standards work
		style2 = document.getElementById(whichLayer).style;
	} else if (document.all) {
		// this is the way old msie versions work
		style2 = document.all[whichLayer].style;
	} else if (document.layers) {
		// this is the way nn4 works
		style2 = document.layers[whichLayer].style;
	}

	if (style2) {
		if (document.images["i_" + whichLayer]) document.images["i_" + whichLayer].src = style2.display == "block" ? replace(document.images["i_" + whichLayer].src, "minus.gif", "plus.gif") : replace(document.images["i_" + whichLayer].src, "plus.gif", "minus.gif");
		style2.display = style2.display == "block" ? "none" : "block";
	}
}

// Redefine js_collapse via a bit o' JS
document.write("\n<style>\n.js_collapse {\ndisplay:none;'\n}</style>\n");

// From FAQTS
function getStyleClass(className) {
	for (var s = 0; s < document.styleSheets.length; s++)
	{
		if(document.styleSheets[s].rules)
		{
			for (var r = 0; r < document.styleSheets[s].rules.length; r++)
			{
				if (document.styleSheets[s].rules[r].selectorText == '.' + className)
				{
					return document.styleSheets[s].rules[r];
				}
			}
		}
		else if(document.styleSheets[s].cssRules)
		{
			for (var r = 0; r < document.styleSheets[s].cssRules.length; r++)
			{
				if (document.styleSheets[s].cssRules[r].selectorText == '.' + className)
					return document.styleSheets[s].cssRules[r];
			}
		}
	}

	return null;
}

// Checkbox selection control
function select_all(fm) {
	fm=eval("document." + fm);
	for (i=0; i<fm.elements.length; i++) {
		if (fm.elements[i].type == 'checkbox') fm.elements[i].checked=true;
	}
	return false;
}

function select_none(fm) {
	fm=eval("document." + fm);
	for (i=0; i<fm.elements.length; i++) {
		if (fm.elements[i].type == 'checkbox') fm.elements[i].checked=false;
	}
	return false;
}

function select_invert(fm) {
	fm=eval("document." + fm);
	for (i=0; i<fm.elements.length; i++) {
		if (fm.elements[i].type == 'checkbox') fm.elements[i].checked=!fm.elements[i].checked;
	}
	return false;
}

// Change Text
function ct(id,txt) {
if(document.layers) { document.layers[id].document.open(); document.layers[id].document.write(txt); document.layers[id].document.close;}
else document.getElementById(id).innerHTML = txt;
}

// Change Text in a Form
function ctf(where,txt) {
where.value = txt;
}

var stop=0;

// Jump on row click
function jl(o,jmp) {
return;
var i=0,c1,c2;
if (o.id == null || o.id == "") return false;
if (stop) { stop = 0; return true; }

while (c1 = document.getElementById(o.id).childNodes.item(i++)) {
  if (c1.childNodes.item(0).tagName == 'A') {
    j=0;
    while (c2 = c1.childNodes.item(j++)) {
      if (c2.href && c2.className != 'clr') {
        if (jmp == 1) return seth(c2.href);
        else {location.href=c2.href;stop=1;return true;}
        }
      }
    }
  }
}

// Highlight and Colour
// Table row highlighting and form style sheet application
function hac() {
var c1, c2, i = 0, j = 0;
while (c1 = document.getElementsByTagName('div').item(i++)) {
  if (c1.className == 'hl') {
    j = 0;
    while (c2 = c1.getElementsByTagName('input').item(j++)) {
      c2.onclick = function() {stop=1;}
      }

    j=0;
    while (c2 = c1.getElementsByTagName('a').item(j++)) {
      if (c2.className != 'clr') {
        c2.onclick = function() {
          if (navigator.appName == 'Microsoft Internet Explorer' || document.all) this.blur();
          //location.href=this.href;
          stop=1;
          }
        }
      }

    j = 0;
    while (c2 = c1.getElementsByTagName('tr').item(j++)) {
      if (c2.className != 'clr' && c2.childNodes.item(0).tagName == 'TD') {
        c2.onmouseover = function() {this.className='highlight';return jl(this,1);}
        c2.onmouseout = function() {this.className='';status='';return true;}
        c2.onclick = function() {jl(this);}
        }
      }
    }
  }

//i = 0;
//while (c1 = document.getElementsByTagName('form').item(i++)) {
//  j=0;
//  while (c2 = c1.getElementsByTagName('input').item(j++)) {
//    if (c2.type == 'submit' || c2.type == 'button') {
//      if (c2.className) {
//        if (c2.className.indexOf("submit") == -1) c2.className = c2.className + ' submit';
//      } else c2.className = 'submit';
//      c2.onmouseover = function() {this.className=this.className.substring(0,this.className.indexOf("submit")) + 'submit-hover' + this.className.substring((this.className.indexOf("submit")+6),this.className.length);return true;}
//      c2.onmouseout = function() {this.className=this.className.substring(0,this.className.indexOf("submit-hover")) + 'submit' + this.className.substring((this.className.indexOf("submit-hover")+12),this.className.length);return true;}
//		}
//    else if (c2.type == 'checkbox') {
//			if (c2.className) {
//				if (c2.className.indexOf("smc") == -1) {
//					c2.className=c2.className + ' smc';
//				}
//			} else this.className='smc';
//		}
//    else if (c2.type != 'hidden' && c2.type != 'radio') {
//      this.className='input';
//			}
//    }
//  }
}

onload = function() { hac(); }

function seth(h) {
var s,l,gn;
if (h.indexOf("?")==-1){l=h;h="";}
else {l=h.substring(0,h.indexOf("?"));h=h.substring(h.indexOf("?")+1,h.length);}

if (h.indexOf("gn=") != -1){
  gn=h.substring(h.indexOf("gn=")+3,h.length);
  gn=" " + gn.substring(0,gn.indexOf("&"))
  gn=replace(gn,'+',' ');
  gn=unescape(gn);
  if (gn.lastIndexOf("!") != gn.length-1 && gn.lastIndexOf(".") != gn.length-1 && gn.lastIndexOf("?") != gn.length-1) gn = gn + ".";
  }
else if (h.indexOf("show=") == 0){
  gn=" "+h.substring(5,h.indexOf("&"));
  gn=replace(gn,'+',' ');gn=replace(gn,"'","\'");gn=replace(gn,'x ','');
  }

if (l.indexOf("display.cgi") != -1) {
  if (gn) s="View this thread in" + gn;
  else s="View this thread.";
  }
else if (l.indexOf("game.cgi") != -1) {
  if (gn) s="Enter"+gn;
  else s="Enter the game.";
  }
else if ((l.indexOf("rpol-main.cgi") != -1 || l.length == l.lastIndexOf("/") + 1) && gn) s="Expand the"+gn+" games.";

if (s) {status=s;return true;}
}

// Annoy a user
function playSound(sfile) {
  document.all.soundspan.innerHTML="<embed src='/sounds/"+sfile+"' hidden='true' autostart='true' loop='false' />";
}

// Get the real length of text input, counting enters as a carriage return and line feed no matter the browser
function trueLength(textInput) {
	if (textInput.indexOf('\r\n') != -1)
		;	// looks ok, IE on Windows etc.
	else if (textInput.indexOf('\r') != -1)
		textInput = textInput.replace ( /\r/g, "\r\n" );	// IE on Mac
	else if (textInput.indexOf('\n') != -1)
		textInput = textInput.replace ( /\n/g, "\r\n" );	// Firefox
	else
		;	// no newlines
	return textInput.length;
}


// Focus the window to the contents div
var dofocus = 0;

function contents_focus()
{
if (dofocus == 1)
	{
	if (window.focus && ! document.AddMessage && ! document.newmessage && ! document.threadsearch && ! document.gamesearch && ! document.subform)
		{
		if (window.location.hash)
			{
			if (document.getElementById(window.location.hash.substring(1)))	// substring is used to trim '#'
				{
				document.getElementById(window.location.hash.substring(1)).focus();
				}
			}
		else
			{
			document.getElementById('contents').getElementsByTagName('a')[0].focus();	// get the first anchor tag within contents, and focus on it
			}
		}

	dofocus = 0;

	document.body.onfocus = null;
	document.body.onkeydown = null;
	document.body.onmousemove = null;
	document.body.onmousedown = null;
	}
}
