function changeLoc(address){
  window.location.href = address;
}

function onOver(li){
  if(document.getElementById||(document.all && !(document.getElementById))){
    li.style.backgroundColor="#F7F5EE";
    li.style.cursor="hand";
   // li.style.borderBottom="1px solid #cccccc";
  }
}

function onOut(li){
  if(document.getElementById||(document.all && !(document.getElementById))){
    li.style.backgroundColor="";
  //  li.style.borderBottom="1px dashed #dddddd";
  }
}

function showstuff(boxid){ 
	document.getElementById(boxid).style.display="block"; 
 } 

 function hidestuff(boxid){ 
	document.getElementById(boxid).style.display="none"; 
 }
 
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}



/**
 * Create a few global scope variables for mmail's submission form scripts
 */
var pubdateMenu;
var calTableId = "cal-table";
var nextIssue;
var maxCharacters = 500;
var rightNow = new Date();

/**
 * Adds a calendar display next to the select which issue widget.
 *
 */
function transformPubDate() {

	if(!document.forms[1] || !document.forms[1].pubdate) return;
	
	pubdateMenu = document.forms[1].pubdate;
	
	var secondDate = pubdateMenu.options[1].value;
	var lastDate = pubdateMenu.options[pubdateMenu.options.length-1].value;
	var secondIssue = dateFromString(secondDate);
	var lastIssue = dateFromString(lastDate);
	nextIssue = new Date(secondIssue.getFullYear(), secondIssue.getMonth(), secondIssue.getDate()-1);

	var cal = makeHTMLCalendar(nextIssue, lastIssue);

	pubdateMenu.parentNode.insertBefore(cal, pubdateMenu.parentNode.firstChild);
//	pubdateMenu.parentNode.appendChild(cal);

	var pn = document.createElement("div");
	pn.className = "note";
	pn.id = "next-issue-warning";
	pubdateMenu.parentNode.appendChild(pn);

	// initialize the calendar's value (in case this is an edit)
	chooseDate(pubdateMenu.value);
	
	// add event listeners to that the calendar updates when the select does
	pubdateMenu.onchange = function () { chooseDate(this.value); };

	// Add a CSS hook on the parent node of the select menu
	pubdateMenu.parentNode.className += " cal-table-parent";

}


/**
 * builds an HTML calendar based on a start and end date.
 *
 * @param	object	start a Date object
 * @param	object	end a Date object
 * @return	object	t a DOM segment that contains an HTML table
 *
 */

function makeHTMLCalendar(start, end) {

	var days = new Array("S","M","T","W","T","F","S");
	var startMonth = new Date(start.getFullYear(), start.getMonth()-1, 1);
		
	var t = document.createElement("table");
	t.id = calTableId;
	t.cellspacing = 0;
	var thr = document.createElement("thead");
	
	
	// create the header row
	var tr = document.createElement("tr");

	// build the header row
	for(var d=0; d<days.length; d++) {
		var day = document.createTextNode(days[d]);
		var th = document.createElement("th");
		th.appendChild(day);
		tr.appendChild(th);
	}
	// insert the header row
	thr.appendChild(tr);
	t.appendChild(thr);

	var tb = document.createElement("tbody");
	t.appendChild(tb);
	
	// build out the month
	
	var firstDayOfNextMonth = new Date(start.getFullYear(), start.getMonth()+1, 1);
	var lastDayOfTheMonth = new Date(firstDayOfNextMonth-1);
	var firstDayOfTheMonth = new Date(start.getFullYear(), start.getMonth(), 1);

	var currentMonth = true;
	var calDay = 0;
	var dayOfTheWeek = 0;
	var weekCount = 0;
	
	var dateOffset = firstDayOfTheMonth.getDay();
	
	var today = new Date();

	while(currentMonth == true) {
	
		// get the current calendar day in the loop
		var currentDay = new Date(start.getFullYear(), start.getMonth(), calDay - (dateOffset-1));

		if(calDay % 7 == 0) {
			weekCount++;
			dayOfTheWeek = 0;
			if(calDay > 0) {  // only append tr if we've completed at least a week.
				tb.appendChild(tr);
			}
			var tr = document.createElement("tr");
			
			if(currentDay > end) { currentMonth = false; }
		}
		
		var td = document.createElement("td");
		var day = document.createTextNode(currentDay.getDate());
		var a = document.createElement("a");
		a.appendChild(day);
		
		if(currentDay < start || currentDay > end) {
			td.className = "out-of-range";
		} else {
			td.className = "in-range";
			a.onclick = function() { chooseDate(this.rel); };
			a.rel = dateToString(currentDay);
		}
		
		if(dateToString(currentDay) == dateToString(nextIssue)) {
			a.rel = "next_issue";
		}
		
		td.appendChild(a);
		tr.appendChild(td);

		calDay++;
		dayOfTheWeek++;
		
	}
	
	return t;
	
} // end makeHTMLCalendar


/**
 * Sets the highlighted class name on the calendar
 *
 */
function setCalendarHighlight(v) {
	var table = document.getElementById(calTableId);
	var anchors = table.getElementsByTagName("a");

	for (var i = 0; i < anchors.length; i++) {
		if(anchors[i].className == "highlighted") {
			anchors[i].className = "";
		}
		if(anchors[i].rel == v) {
			anchors[i].className = "highlighted";
		}
	}
}


/**
 * Changes the select menu to the selected date.
 *
 * @return	boolean
 */
function chooseDate(v) {
	var i = 0;
	var ok = false;
	
	var pn = document.getElementById("next-issue-warning");
	// this doesn't work in IE at the moment, so leave it out until it works 
	if(rightNow.getHours() >= 15 && v=="next_issue") {
	//	pn.innerHTML = "Note: Submissions after 3pm may not appear in tomorrow's mail."
	} else {
	//	pn.innerHTML = "";
	}

	while(!ok) {
		if(pubdateMenu.options[i].value == v) {
			pubdateMenu.options[i].selected = "selected";
			setCalendarHighlight(v);
			ok = true;
		}
		
		if(i == pubdateMenu.options.length-1) { ok = true; }
		i++;
	}
	
	return ok;
}


/**
 * Converts a string YYYY-MM-DD to a date object
 *
 * @param	string	s A string formatted "YYYY-MM-DD"
 * @return	object	a Date object
 *
 */
function dateFromString(s) {
	return new Date(s.slice(0,4), (s.slice(5,7)-1), s.slice(8,10));
}


/**
 * Converts a date object to a string YYYY-MM-DD
 *
 * @param	object	a Date object
 * @return	string	s A string formatted "YYYY-MM-DD"
 *
 */
function dateToString(date) {
	var m = (date.getMonth()+1);
	if (m < 10) m = "0" + m;

	var d = (date.getDate());
	if (d < 10) d = "0" + d;

	return date.getFullYear() + "-" + m + "-" + d;

}


/**
 * shows how many characters are left
 *
 */
function showCharacterCount() {
	var display = document.getElementById("remLen");
	var textarea = document.getElementById("messagetext");
	display.value = maxCharacters - textarea.value.length;
	textarea.className = (display.value < 0) ? "too-many" : "";
}

/**
 * adds the "focused" class name to the remaining characters element
 */
function highlightCharacterCount() {
	var display = document.getElementById("remLen");
	display.className="focused";
	showCharacterCount();
}

/**
 * empties the class name on the remaining characters element
 */
function unHighlightCharacterCount() {
	var display = document.getElementById("remLen");
	display.className="";
	showCharacterCount();
}

/**
 * sets up the "characters remaining" widget
 */
function initMessageField() {
	if(!document.getElementById("messagetext")) return;
	var textarea = document.getElementById("messagetext");
	
	var remLen = document.createElement("input");
	remLen.readonly = "readonly";
	remLen.id = "remLen";
	remLen.type = "text";
	remLen.name = "remLen";
	remLen.size = "4";
	textarea.parentNode.appendChild(remLen);
	
	var note = document.createElement("span");
	note.appendChild(document.createTextNode("characters remaining: "));
	remLen.parentNode.insertBefore(note, remLen);
	// slip in a linebreak between the text area and the counter for IE's benefit.
	remLen.parentNode.insertBefore(document.createElement("br"), note);
	
	// show the count now to initialize it (for when it already has data in it)
	showCharacterCount();
	
	// add the event listeners
	textarea.onkeydown = showCharacterCount;
	textarea.onkeyup = showCharacterCount;
	textarea.onfocus = highlightCharacterCount;
	textarea.onblur = unHighlightCharacterCount;

}

window.onload = function() { transformPubDate(); initMessageField(); }