/*
	NineChime.com JavaScript
	Last update: December 24, 2007

	Some functions have dependencies, so make sure they are in order!

	This code is free software; you may redistribute it and/or modify it under
	the terms of the MIT license.  Contact the Open Source Initiative
	(www.OpenSource.org) for more information or a copy of the MIT license.
*/



/*
obj deib()
	: Wrapper for getting an element ID
	Returns: object reference
*/
function deib(myID) {
	var obj;

	if (document.getElementById) {
		// DOM
		obj = document.getElementById(myID);
	}
	else if (document.all) {
		// IE5 and other weird stuff
		obj = document.all[myID];
	}

	return obj;
}


/*
string mailform()
	: Simple mail formatter
*/
function mailform(name, server) {
	return 'mai' + 'lto:' + name + '&#064;' + server;
}


/*
string hcp()
	: HTML character preprocessor.  Masks illegal HTML chars in comments, as HTML does not support the XML 'CDATA' tag.  Also subs symbols snooped by spambots.
	Related: hcprint()

	Input:
		{{  -> "<"
		}}  -> ">"
		{e} -> "mailto:"
		{a} -> "@"
		{d} -> "."
		{s} -> "?subject="

	Ex:
		myvar = hcp('{{a href="{e}name{a}server{d}com{s}mysubject"}}Contact{{/a}}');
*/
function hcp(work) {
	work = work.replace( /\{e\}/g , "mailto:");
	work = work.replace( /\{a\}/g , "&#064;");
	work = work.replace( /\{d\}/g , ".");
	work = work.replace( /\{s\}/g , "?subject=");
	work = work.replace( /\{\{/g  , "<");
	work = work.replace( /\}\}/g  , ">");
	return work ;
}


/*
void hcprint()
	: writeln() wrapper for hcp()
	Dependency: hcp()

	Ex:
		hcprint('{{tag}}content{{/tag}}');
*/
function hcprint(input) {
	document.writeln(hcp(input));
}


/*
void jsprint()
	: Wrapper for Javascript writeln()
*/
function jsprint(input) {
	document.writeln(input);
}


/*
void insertText()
	: Adds content at cursor position to an HTML container by its ID

	Ex:
		<a onclick="javascript: insertText('mytextarea', smile);">
*/
function insertText(id, newtext) {
	// Works for IE and Mozilla, but not many others
	var box = document.getElementById(id);
	box.focus();

	if (box.createTextRange) {
		// IE (use this instead of caretPos)
		document.selection.createRange().text += newtext;
	} else if (box.setSelectionRange) {
		// Mozilla, some others
		var len = box.selectionEnd;
		box.value = box.value.substr(0, len) + newtext + box.value.substr(len);
		box.setSelectionRange(len + newtext.length, len + newtext.length);
	} else {
		// DOM
		box.value += newtext;
	}
}


/*
int getWidth()
	: Returns client viewpoint width
	Failure: returns 0
*/
function getWidth() {
	var width = 0;

	// Code taken from http://www.quirksmode.org
	if (self.innerWidth) {
		// all except Explorer
		width = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		// Explorer 6 Strict Mode
		width = document.documentElement.clientWidth;
	}
	else if (document.body) {
		// other browsers
		width = document.body.clientWidth;
	}
	//End code credit

	return width;
}


/*
int getHeight()
	: Get client viewpoint height
	Returns: height
	Failure: returns 0
*/
function getHeight() {
	var height = 0;

	if (self.innerHeight) {
		// Netscape (old)
		// Todo: "self" or "window", here?
		height = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		// IE6 Strict Mode
		height = document.documentElement.clientHeight;
	}
	else if (document.body) {
		// Most other browsers
		height = document.body.clientHeight;
	}

	return height;
}


/*
int toggleChecks()
	: Reverses all checkboxes by HTML name in a specific HTML form
	Returns: number of checkboxes parsed (not changed)
	Failure: alert() if no form found.  No alert() if zero matching elements
	Dependency: deib()
*/
function toggleChecks(checkName, myForm) {
	var counter = 0;

	if (deib(myForm) == null) {
		alert("toggleChecks(): no form '" + myForm + "'");
		return 0;
	}
	else {
		var el;
		for (var i = 0; i < myForm.elements.length; i++) {
			el = myForm.elements[i];
			if (el.name == checkName) {
				counter++;
				if (el.disabled == false) {
					if (el.checked == true) {
						el.checked = false;
					} else {
						el.checked = true;
					}
				}
			}
		}
	}
	return counter;
}



/*
	Furry.org.au JavaScript
	Last update: December 25, 2007
*/

/* -- Deprecated ---------------------------------------------------------- */

/* String replace mail function */
function eprint(work) {
	work = work.replace(/\{e\}/, "mai" + "lto" + ":");
	work = work.replace(/\{a\}/, "@");
	work = work.replace(/\{d\}/g, ".");
	work = work.replace(/\{s\}/, "?" + "subject=");
	document.writeln(work);
	return;
}