// 20060412.

function setstyleSSI(value)
{
 	setCookie("enctheme", value, 365*24, "/");
	location.reload();
}

function setCookie(name, value, hours_to_live, path, domain, secure) {
  var expireDate = "";
  if(hours_to_live)
  {
    expireDate = (new Date((new Date()).getTime() + hours_to_live*3600000)).toGMTString();
  }

  var curCookie = name + "=" + escape(value) +
	((hours_to_live) ? "; expires=" + expireDate : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

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 deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function setScheme(s)
{
	setCookie("ndscheme", s, 30*24, "/");
	location.reload();
}

function print_vlog_iframe()
{
	document.write('<iframe name="theiframeNew" id="vlogiframe" src="about:blank" width="1" height="1" scrolling="no" frameborder="0" align="right" marginwidth="0" marginheight="0" class="nomrgnobrdnopad">Your browser does not support inline frames. Get a <a href="http://www.mozilla.org/" target="_blank">new one</a>.</iframe>');
}

function print_vlog_iframedb()
{
	document.write('<iframe name="theiframeNewdb" id="vlogiframedb" src="about:blank" width="1" height="1" scrolling="no" frameborder="0" align="right" marginwidth="0" marginheight="0" class="nomrgnobrdnopad">Your browser does not support inline frames. Get a <a href="http://www.mozilla.org/" target="_blank">new one</a>.</iframe>');
}

function setdelay(num_seconds)
{
 	setCookie("nodiv_slideshow_delay", num_seconds, 365 * 24, "/");
	alert("Display time set to " + num_seconds + " seconds, starting with the next image.");
	return false;
}
function setsize(set)
{
 	setCookie("nd_photos_big", set, 365 * 24, "/");
	if(getCookie("nd_photopage_mode") == "framed")
	{
		parent.ndphotoframe.location.reload();
	}
	else
	{
		location.reload();
	}
}
function play()
{
 	setCookie("nd_slideshow_paused", 0, 365*24, "/");
	location.reload();
}
function pause()
{
 	setCookie("nd_slideshow_paused", 1, 365*24, "/");
	location.reload();
}
function setphotopagemode(set)
{
 	setCookie("nd_photopage_mode", set, 365*24, "/");
	parent.location.reload();
}
function setphotoframelayout(set)
{
 	setCookie("nd_photoframe_layout", set, 365*24, "/");
	parent.location.reload();
}

function submit_post_to_CMS()
{
	if( !(document.board.body.value) )
		{ alert("You'll need to type a message before you press that button."); }
	else
	{ if((!document.board.guestname.value))
		{ alert("You forgot to type your name!");  }
	else
		{ document.board.submit(); }
	}
}

function toggleSpellcheck(newstate)
{
	var i, span;
	for(i=0; (span = document.getElementsByTagName("span")[i]); i++)
	{
		if(newstate == 'off')
		{
			if(span.className.indexOf("spellerror") != -1)
			{
				span.className = "disabledspellerror";
			}
		}
		else
		{
			if(span.className.indexOf("disabledspellerror") != -1)
			{
				span.className = "spellerror";
			}
		}
	}
}

function ebtextbold()
{
	document.getElementById("ebbody").value += ' [b]your text here[/b]';
}

function ebtextitalic()
{
	document.getElementById("ebbody").value += ' [i]your text here[/i]';
}

function ebtextunderline()
{
	document.getElementById("ebbody").value += ' [u]your text here[/u]';
}

function ebtextquote()
{
	document.getElementById("ebbody").value += "\n[blockquote=\"Author of Quote\"]\nyour text here\n[/blockquote]\n";
}

function ebinslink()
{
	var text = document.getElementById("ebbody");
	var linkURL = prompt("Enter the URL (address) for the link:", "http://");
	var linktext = prompt("Enter the text that should be linkified:", "your text here");
	var num = 1;
	while(text.value.indexOf('][' + num + ']') != -1)
		num++;
	if(linktext && linkURL)
		text.value += ' [' + linktext + '][' + num + '] ' + "\n\n" + '[' + num + ']: ' + linkURL + "\n";
}

function ebinsimage()
{
	var text = document.getElementById("ebbody");
	var tag = '[image1here]';
	var num = 1;
	var showed_warning = 0;
	while(text.value.indexOf(tag) != -1)
	{
		tag = '[image' + (++num) + 'here]';
		showed_warning = 1;
	}
	text.value += "\n" + tag + "\n";
	if(!showed_warning)
		alert("Inserting the image tag now; you will be prompted to upload your image at the end of the posting process.");
}

function setTextSize(title)
{
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		var t = a.getAttribute("title");
		if(t   &&   a.getAttribute("rel").indexOf("style") != -1)
		{
			if(t == "textsm"   ||   t == "textmd"   ||   t == "textlg"   ||   t == "textxl")
			{
				if(t == title)
				{
					//alert("setTextSize: enabling " + t);
					a.disabled = false;
				}
				else
				{
					//alert("setTextSize: disabling " + t);
					a.disabled = true;
				}
			}
		}
	}
	setCookie("textsize", title, 365*24, "/");
}

//function getCurrentTextSize()
//{
//	var i, a;
//	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
//	{
//		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled)
//		{
//			return a.getAttribute("title");
//		}
//	}
//	return null;
//}

function getPreferredTextSize()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		var t = a.getAttribute("title");
		if(t   &&   a.getAttribute("rel").indexOf("style") != -1   &&   a.getAttribute("rel").indexOf("alt") == -1)
		{
			if(t == "textsm"   ||   t == "textmd"   ||   t == "textlg"   ||   t == "textxl")
			{
				return t;
			}
		}
	}
	return null;
}

function setstyle(title)
{
	// This if() needs testing on various permutations of sites that have or don't
	// have a selectable stylesheet and/or a selectable text-size stylesheet.
	if(!title)
	{
		return null;
	}

	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		var t = a.getAttribute("title");
		if(t   &&   a.getAttribute("rel").indexOf("style") != -1)
		{
			if(t != "textsm"   &&   t != "textmd"   &&   t != "textlg"   &&   t != "textxl")
			{
				if(t == title)
				{
					//alert("setstyle: enabling " + t);
					a.disabled = false;
				}
				else
				{
					//alert("setstyle: disabling " + t);
					a.disabled = true;
				}
			}
		}
	}
	setCookie("enctheme", title, 365*24, "/");
}

function getCurrentStyle()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled)
		{
			return a.getAttribute("title");
		}
	}
	return null;
}

function getPreferredStyle()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		var t = a.getAttribute("title");
		if(t   &&   a.getAttribute("rel").indexOf("style") != -1   &&   a.getAttribute("rel").indexOf("alt") == -1)
		{
			if(t != "textsm"   &&   t != "textmd"   &&   t != "textlg"   &&   t != "textxl")
			{
				return t;
			}
		}
	}
	return null;
}

// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
// so we can use multiple onload events if necessary.
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


var cookie1 = getCookie("textsize");
var title1 = cookie1 ? cookie1 : getPreferredTextSize();
setTextSize(title1);

var cookie2 = getCookie("enctheme");
var title2 = cookie2 ? cookie2 : getPreferredStyle();
setstyle(title2);

