/* Futaba / Wakaba Functions */
function replace_all (str, from, to)
{
	var idx = str.indexOf (from);

	while (idx > -1 )
	{
		str = str.replace (from, to);
		idx = str.indexOf (from);
	}
	return str;
}

function quote (text)
{
	var textarea	= document.forms.postform.com;

	text	= '>>' + text;

	if (textarea)
	{
		if (textarea.createTextRange && textarea.caretPos) // IE
		{
			var caretPos	= textarea.caretPos;
			caretPos.text	= caretPos.text.charAt (caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		}
		else if (textarea.setSelectionRange) // Firefox
		{
			var start	= textarea.selectionStart;
			var finish	= textarea.selectionEnd;
			textarea.value	= textarea.value.substr (0, start) + text + textarea.value.substr (finish);
			textarea.setSelectionRange (start + text.length, start + text.length);
		}
		else
		{
			textarea.value	+= text + ' ';
		}
//		textarea.focus ();
	}
}

function highlight (post)
{
	var cello	= document.getElementsByTagName ('blockquote');
	var cells	= document.getElementsByTagName ('td');
	var reply	= document.getElementById ('post' + post);

	for (var i = 0; i < cello.length; i ++)
	{
		if (cello[i].className == 'highlight')
		{ cello[i].className	= 'firstpost'; }
	}

	for (var i = 0; i < cells.length; i ++)
	{
		if (cells[i].className == 'highlight')
		{ cells[i].className	= 'reply'; }
	}

	if (reply)
	{
		reply.className	= 'highlight';
		var match	= /^([^#]*)/.exec (document.location.toString () );
		document.location	= match[1] + '#' + post;
	}
}

function size_textarea (id, rows) { document.getElementById(id).com.setAttribute('rows', rows); }

function get_password (name)
{
	var pass	= get_cookie (name);
	if (pass) { return pass; }

	var chars	= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	var pass	= '';

	for (var i = 0; i < 8; i ++)
	{
		var rnd	= Math.floor (Math.random () * chars.length);
		pass	+= chars.substring (rnd, rnd + 1);
	}

	return (pass);
}

function get_cookie (name)
{
	with (document.cookie)
	{
		var reg	= new RegExp ('(^|;\\s+)' + name + '=(.*?)(;|$)');
		var hit	= reg.exec (document.cookie);

		if (hit && hit.length > 2)
		{ return unescape (replace_all (hit[2], '+', '%20') ); }
		else return '';
	}
}

function set_cookie (name, value, days)
{
	if (days)
	{
		var date	= new Date ();
		date.setTime (date.getTime () + (days * 24 * 60 * 60 * 1000) );
		var expires	= '; expires=' + date.toGMTString ();
	}
	else expires	= '';

	document.cookie	= name + '=' + value + expires + '; path=/';
}

function set_stylesheet (styletitle, norefresh)
{
	set_cookie ('fuu_style', styletitle, 365);

	var links	= document.getElementsByTagName ('link');
	var found	= false;

	for (var i = 0; i < links.length; i ++)
	{
		var rel	= links[i].getAttribute ('rel');
		var ttl	= links[i].getAttribute ('title');

		if (rel.indexOf ('style') != -1 && ttl)
		{
			links[i].disabled	= true; // IE needs this to work. IE needs to die.

			if (styletitle == ttl)
			{
				links[i].disabled	= false;
				found	= true;
			}
		}
	}

	if (!found) set_preferred_stylesheet ();
}

function set_preferred_stylesheet ()
{
	var links	= document.getElementsByTagName ('link');

	for (var i = 0; i < links.length; i ++)
	{
		var rel	= links[i].getAttribute ('rel');
		var ttl	= links[i].getAttribute ('title');

		if (rel.indexOf ('style') != -1 && ttl)
		{ links[i].disabled	= (rel.indexOf ('alt') != -1); }
	}
}

function get_active_stylesheet ()
{
	var links	= document.getElementsByTagName ('link');

	for (var i = 0; i < links.length; i ++)
	{
		var rel	= links[i].getAttribute ('rel');
		var ttl	= links[i].getAttribute ('title');

		if (rel.indexOf ('style') != -1 && ttl && !links[i].disabled)
		{ return ttl; }
	}
	return null;
}

function get_preferred_stylesheet ()
{
	var links	= document.getElementsByTagName ('link');

	for (var i=0;i<links.length;i++)
	{
		var rel	= links[i].getAttribute ('rel');
		var ttl	= links[i].getAttribute ('title');

		if (rel.indexOf ('style') != -1 && rel.indexOf ('alt') == -1 && ttl)
		{ return ttl; }
	}
	return null;
}


// Fill postform / delform fields
function set_inputs (id)
{
	if (document.getElementById (id) )
	{
		with (document.getElementById (id) )
		{
			if (document.getElementById ('name') && !name.value)
			{ name.value	= get_cookie ('fuu_name'); }
			if (document.getElementById ('link') && !link.value)
			{ link.value	= get_cookie ('fuu_mail'); }
			if (document.getElementById ('pwd') && !pwd.value)
			{ pwd.value	= get_password ('fuu_pass'); }
			if (document.getElementById ('noko') && get_cookie ('fuu_noko') == 1)
			{ noko.options[1].selected = true; }
		}
	}
}

function set_delpass (id)
{
	if (document.getElementById (id) )
	{
		with (document.getElementById (id) )
		{
			if (document.getElementById ('pwd') )
			{ pwd.value	= get_cookie ('fuu_pass'); }
		}
	}
}


window.onunload	= function (e)
{
	if (style_cookie)
	{
		var title	= get_active_stylesheet ();
		set_cookie (style_cookie, title, 365);
	}
}

window.onload	= function (e)
{
	var match;

	if (match	= /#q([0-9]+)/.exec (document.location.toString () ) )
	if (!document.forms.postform.com.value)
	quote (match[1]);

	if (match	= /#([0-9]+)/.exec (document.location.toString () ) )
	highlight (match[1]);

	set_inputs ('postform');
	set_delpass ('delform');
}


if (style_cookie)
{
	var cookie	= get_cookie (style_cookie);
	var title	= cookie ? cookie : get_preferred_stylesheet ();
	set_stylesheet (title);
}


