// tvs.js: JavaScript functions common to all Tri-Valley Stargazer web pages

// Last modified: October 25, 2011

var contactWidth = "220px";		// The width of the contact table entries
var lastUpdateDate;			// The time this web page was last updated

// Display information about a contact.  If the title is non-null, format the contact's name
// and title in separate columns to facilitate preparation of a table.  The width of the
// table's entries is specified by global variable contactWidth.  A default value is used,
// but the user can change it if need be.
function contact(e_name, e_site, fullname, title) {
	if (title != null) {
		document.write("<div style='min-width:" + contactWidth + ";float:left;'>" + title + "&nbsp;</div>");
		document.write("<div style='min-width:" + contactWidth + ";float:left;'>");
	}

	if (e_name == "" || e_site == "") {
		// No email address supplied; so don't provide a link
		document.write(fullname);
	}
	else {
		// Email address supplied, so present the contact's name as a link
		var email = e_name + "@" + e_site;
		var mailto = "mailto:" + email;
		document.write("<a href='" + mailto + "'" + " title='" + mailto + "'>" + fullname + "</a>");
	}
	if (title != null) {
		document.write("&nbsp;</div>");
		document.write("<br>");
	}
};


// Set up the newsletter web page to show all years for which newsletters are available.
// We assume they are always available from any year since 1996 up to the present date.
function defineNewsletterYears() {
	var thisYear = Number((new Date()).getFullYear());
	var select = document.getElementById("theYear");
	var year;
	for (year=1996; year<=thisYear; year++) {
		var option = document.createElement("option");
		option.text = String(year);
		option.value = String(year);
		if (year == thisYear)
			option.selected = true;
		try {
			// for IE earlier than version 8
			select.add(option, select.options[null]);
		}
		catch (e) {
			select.add(option, null);
		}
	}
}


function goFetch() {
	var theMonth = document.getElementById("theMonth").value;
	var theYear = document.getElementById("theYear").value;		// eg. 2001
	var shortYear = theYear.substring(2);				// eg. 01
	var filename = "newsletters/" + theYear + "/";


	// Give user a warning if he tries to display a file that probably doesn't exist
	// This check won't know whether this month's newsletter has been published yet,
	// so it will try to display it, even if that triggers the 1&1 bug.

	if (theYear == 1996 && theMonth < 3) {
		alert("There is no newsletter for that date");
		return;
	}
	var thisYear = Number((new Date()).getFullYear());
	var thisMonth = Number((new Date()).getMonth()) + 1;
	if (theYear == thisYear && theMonth > thisMonth) {
		alert("That newsletter hasn't been published yet ");
		return;
	}

	// Files before 09'01 are HTML files, and files on or after that are PDF files

	if ((theYear < 2001) || (theYear == 2001 && theMonth < 9))
		filename += theMonth + shortYear + "/index.html";
	else
		filename += "tvsnews" + theMonth + shortYear + ".pdf#zoom=100&pagemode=none";

	// On 1&1's servers, if the file doesn't exist, they may try to do you a "favor"
	// by substituting a file that does exist.  For example, tvsnews0196/index.html
	// doesn't exist; so they substitute tvsnew1096/index.html assuming you have made a
	// typo.  This caused me many hours of debugging before I realized what was happening.

	window.location.href = filename;		// display the selected newsletter
}


// Change the image for the element whose id is given
function showImage(id, filename) {
	document.getElementById(id).src = filename;
}


// Display the date when this page was last updated
function showLastUpdate() {
	var html = "";
	html += 
'	 <div class="lastUpdate">' +
'	 Last modified on<br>' + lastUpdateDate +
'	 <address>' +
'	  by <a href = "mailto:hdjones@pacbell.net" title="mailto:hdjones@pacbell.net">Hilary Jones</a>' +
'	 </address>' +
'	</div>';

	document.write(html);
}


