function loadDate(posted)
{
	//alert("(" + posted + ")");
	if (posted == "")
	{
		var today = new Date();
		//alert("today:" + today);
		//get timezone offset (hours this machine is off from UTC/GMT)
		var offset = today.getTimezoneOffset() / 60;
		//alert("offset:" + offset);
		/*Everything should be in EST.  If the offset isn't already at four, let's take the
		basic time (by adding on the offset, whatever it is), and then subtract the four hours 
		that should put us at EST.*/
		if (offset != 5)
		{
			//alert("offset not equal to 4; adding hours");
			addHours(today, offset);
			addHours(today, -5);
		}
		//due to stupid daylight savings, I'm subtracting an extra hour, just temporarily, to make this right.
		//addHours(today, 1);
		//alert("today:" + today);
		var dateString = "" + today.getFullYear();
		//alert("dateString:" + dateString);
		var dateArr = new Array(''+(today.getMonth()+1), ''+today.getDate(), 
			''+today.getHours(), ''+today.getMinutes(), ''+today.getSeconds());
		for (var i = 0; i < dateArr.length; i++)
		{
			if (dateArr[i].length == 1)
				dateArr[i] = "0" + dateArr[i];
			dateString += dateArr[i];
			//alert(dateString);
		}
		posted = dateString;
	}
	//alert(posted);
	return posted;
}

function addHours(today, add)
{
	var hrs = today.getHours() + add;
	var date = today.getDate();
	var month = today.getMonth();
	var year = today.getFullYear();
	if (hrs > 23)
	{
		hrs = hrs - 24;
		date++;
		if (date > getDaysInMonth(month,year))
		{
			month++;
			date = 1;
		}
		if (month > 11)
		{
			month = 0;
			year++;
		}
	}
	else if (hrs < 0)
	{
		hrs = 24 + hrs;
		date--;
		if (date < 1)
		{
			month--;
			date = getDaysInMonth(month,year);
		}
		if (month < 0)
		{
			month = 11;
			year--;
		}
	}
	today.setHours(hrs);
	today.setDate(date);
	today.setMonth(month);
	today.setFullYear(year);
}

function getDaysInMonth(month,year)
{
	switch(month)
	{
		case 11,4,6,9:
			return 30;
		case 1,3,5,7,8,10,12:
			return 31;
		case 2:
			//leap year
			if (year % 4 == 0)
				return 29;
			return 28;
	}
}

function doPreview(myForm,isEntry)
{
	var origTarget = myForm.target;
	myForm.target = "_blank";
	myForm.action = "preview.php?isEntry=" + isEntry;
	myForm.submit();
	myForm.target = origTarget;
}

function openComments(id)
{
	window.open('comments.php?id=' + id, 'blah', 
		'width=500,height=525,location=no,menubar=no,toolbar=no,status=yes,resizable=yes,scrollbars=yes');
}
var selectedText;
function storeCaret(textEl)
{
	//if (textEl.createTextRange)
	if (document.selection)
	{
		textEl.caretPos = document.selection.createRange().duplicate(); 
	}
	else if (window.getSelection)
	{
		selectedText = window.getSelection();
	}
} 

function insertAtCaret (textEl, tagType) 
{
	var text;
	var prefix = "";
	var suffix = "";
	if (tagType == "link")
	{
		prefix = '<a href="" target="_blank">';
		suffix = "</a>";
	}
	else if (tagType == "bold")
	{
		prefix='<strong>';
		suffix = '</strong>';
	}
	else if (tagType == "italics")
	{
		prefix='<em>';
		suffix='</em>';
	}
	else if (tagType == "under")
	{
		prefix='<u>';
		suffix='</u>';
	}
	else if (tagType == "ul")
	{
		prefix='<ul>';
		suffix='</ul>';
	}
	else if (tagType == "ol")
	{
		prefix='<ol>';
		suffix='</ol>';
	}
	else if (tagType == "li")
	{
		prefix='<li>';
		suffix='</li>';
	}
	if (textEl.createTextRange && textEl.caretPos) 
	{
		//ie
		var caretPos = textEl.caretPos;
		caretPos.text = prefix + caretPos.text + suffix;
		textEl.focus();
	} 
	else
	{
		//firefox
		var startPos = textEl.selectionStart;
		var endPos = textEl.selectionEnd;
		textEl.value = textEl.value.substring(0, startPos)
			+ prefix + textEl.value.substring(startPos, endPos) + suffix 
			+ textEl.value.substring(endPos, textEl.value.length);
		textEl.focus();
		textEl.selectionStart = textEl.value.indexOf(prefix,startPos);
		textEl.selectionEnd = endPos+prefix.length+suffix.length;
	}
}

/*escape all bad characters--semicolons and line breaks.  Make sure to unescape them when pulling the cookie
value back out.*/
function prepare(postText)
{
	postText = doEscape(postText, '"');
	postText = doEscape(postText, ';');
	postText = doEscape(postText, '\n');
	postText = doEscape(postText, '\r');
	return postText;
}

function doEscape(postText, myChar)
{
	var n=0;
	while (n >= 0)
	{
		n = postText.indexOf(myChar,n);
        if (n < 0)
			return postText;
        else
        {
			postText = postText.substring(0,n) + escape(myChar) + postText.substring(n+1,postText.length);
			n++;
		}
	}
	return postText;
}

function move(srcList, tarList)
{
	var sel, val;
	for (var i = srcList.options.length - 1; i >= 0; i--)
	{
		if (srcList.options[i].selected)
		{
			sel = srcList.options[i].text;
			val = srcList.options[i].value;
			break;
		}
	}
	for (var i = tarList.options.length - 1; i >= 0; i--)
	{
		if (tarList.options[i].text == sel)
			return;
	}
	var newOption = new Option(sel, val);
	tarList.options[tarList.options.length] = newOption;
}

function removeSel(list)
{
	for (var i = list.options.length - 1; i >= 0; i--)
	{
		if (list.options[i].selected)
			list.options[i] = null;
	}
}

function doSearch()
{
	leaving = false;
	var ary = document.forms['searchForm'].elements['selTags'].options;
	for (var i = 0; i < ary.length; i++)
		ary[i].selected = true;
	document.forms['searchForm'].submit();
}

function tagInfo()
{
	window.open('tagInfo.php', 'TagInfo', 
		'width=500,height=525,location=no,menubar=no,toolbar=no,status=yes,resizable=yes,scrollbars=yes');
}

function openTagger(opener)
{
	childWindow = window.open("tagger.php","tagger", "width=500,height=525,location=no,menubar=no,toolbar=no,status=yes,resizable=yes,scrollbars=yes");
	if (childWindow.opener == null)
		childWindow.opener = opener;
}

function dumpTag(w, tag)
{
	var field = w.opener.document.getElementById('tags');
	if (field.value == "")
		field.value = tag;
	else
		field.value = field.value + ", " + tag;
}

function doThumbsUp(opener, id)
{
	var xmlHttp;
	var mydiv = opener.document.getElementById('thumbsdiv' + id);
	mydiv.innerHTML = "Extending thumb...";
	try
	{   
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{    // Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			//update thumbs up text on opening window, and set cookie so this machine can't give this post a thumbs up again. at least, not for a while.
			var divtxt = "<strong>" + xmlHttp.responseText;
			if (xmlHttp.responseText == "1")
				divtxt += " Thumb"
			else
				divtxt += " Thumbs"
			divtxt += " Up</strong>";
			mydiv.innerHTML = divtxt;
			//set cookie
			var exdate=new Date();
			exdate.setDate(exdate.getDate()+7);
			opener.document.cookie='cookiethumb' + id + "=" + "+1" + ";expires="+exdate.toGMTString();
		}
	}
	xmlHttp.open("POST","thumbsCode.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send("id=" + id);
}