// Main Routines to create and populate page layout
function PageStart(prefix)
{
	if (prefix == null) { prefix = ""; }
	document.write('<meta name="HandheldFriendly" content="true">');
	document.write('<meta name="apple-mobile-web-app-capable" content="yes">');
	document.write('<meta name="viewport" content="width=device-width">');
	document.write('<!--[if IE]>\n<LINK REL=STYLESHEET TYPE="text/css" href="' + prefix + 'elcalc_ie.css" TITLE="Style">\n<![endif]-->\n');
	document.write('<BODY>');
	document.write('<A name="top"></A>');
	document.write('<div id=banner>');
	document.write('<h1><span>Electoral Calculus</span></h1>');
	document.write('</div>');
	document.write('<div id=topmenu class="menuclass mobileonly">');
	document.write('<UL>');
	document.write('<LI><A href="' + prefix + 'homepage.html">Home</A>');
	document.write('<LI><A href="#menu">Menu</A>');
	document.write('<LI><A href="' + prefix + 'userpoll.html">Make prediction</A>');
	document.write('</UL>');
	document.write('</div> <!-- topmenu -->');
	document.write('<div id=main>');
	return;
}

function PageEnd(prefix)
{
	if (prefix == null) { prefix = ""; }
	document.write('</div> <!-- end of main -->');
	document.write('<div id=menu class=menuclass>');
	document.write('<h2 id=menuhead class=mobileonly><A name=menu>Menu</A></h2>');
	document.write('<UL>');
	document.write('<LI><h2><A href="' + prefix + 'homepage.html">Home page</A></h2>');
	document.write('<LI><h3><A href="' + prefix + 'scotland.html">Scotland pages</A></h3>');
	document.write('<LI><h4>Electoral Analysis</h4>');
	document.write('<LI><A href="' + prefix + 'analysis_index.html">Analysis Articles</A>');
	document.write('<LI><A href="' + prefix + 'boundaries2013.html">New Boundaries</A>');
	document.write('<LI><h4>Seat predictions</h4>');
	document.write('<LI><A href="' + prefix + 'userpoll.html">Make your prediction</A>');
	document.write('<LI><A href="' + prefix + 'gainloss.html">Vulnerable seats</A>');
	document.write('<LI><A href="' + prefix + 'dynamicmap.html">Dynamic map</A>');
	document.write('<LI><A href="' + prefix + 'orderedseats.html">Ordered seats</A>');
	document.write('<LI><A href="' + prefix + 'regions.html">Region by region</A>');
	document.write('<LI><A href="' + prefix + 'userregpoll.html">Regional predictor</A>');
	document.write('<LI><A href="' + prefix + 'conlist_a_b.html">England A-B</A>');
	document.write('<LI><A href="' + prefix + 'conlist_c_e.html">England C-E</A>');
	document.write('<LI><A href="' + prefix + 'conlist_f_k.html">England F-K</A>');
	document.write('<LI><A href="' + prefix + 'conlist_l_q.html">England L-Q</A>');
	document.write('<LI><A href="' + prefix + 'conlist_r_s.html">England R-S</A>');
	document.write('<LI><A href="' + prefix + 'conlist_t_z.html">England T-Z</A>');
	document.write('<LI><A href="' + prefix + 'conlist_wales.html">Wales</A>');
	document.write('<LI><A href="' + prefix + 'conlist_scot.html">Scotland</A>');
	document.write('<LI><A href="' + prefix + 'battlemap.html">Battlemap</A>');

	document.write('<LI><h4>Polls and data</h4>');
	document.write('<LI><A href="' + prefix + 'polls.html">Opinion polls</A>');
	document.write('<LI><A href="' + prefix + 'mapr1983.html">TimeLine with maps</A>');
	document.write('<LI><A href="' + prefix + 'flatfile.html">Historical data &amp; plots</A>');
	document.write('<LI><A href="' + prefix + 'regional_defns.html">Regional maps</A>');
	
	document.write('<LI><h4>General</h4>');
	document.write('<LI><A href="' + prefix + 'newseatlookup.html">Find a constituency</A>');
	document.write('<LI><A href="' + prefix + 'faq.html">FAQs - help</A>');
	document.write('<LI><A href="' + prefix + 'links.html">Useful Links</A>');
	document.write('<LI><A href="' + prefix + 'sitemap.html">Sitemap</A>');
	document.write('<LI><A href="' + prefix + 'subscribe.html">Register for updates</A>');
	document.write('<LI><A type="application/rss+xml" href="' + prefix + 'rssfeed.rss">RSS feed');
	document.write('	&nbsp;&nbsp;&nbsp;<IMG src=' + prefix + 'rssicon.gif border=0 valign=middle height=16></A>');
	document.write('<LI><A href=mailto:feedback11@electoralcalculus.co.uk>Email us</A>');
	document.write('<LI class=mobileonly><A href="#top">Top of page</A>');
	document.write('</UL>');
	document.write('<HR>');
	document.write('<DIV class=small><A href="http://www.martinbaxter.co.uk">&copy; 2011 Martin Baxter</A></DIV>');
	document.write('</div> <!-- End of Menu -->');
	document.write('</BODY>');
	return;
}

// Routine to create on-the-fly styles for a table to control the text-align of each column
// Usage Example: (format a four-column table: Left, Right, Right, Center
// <SCRIPT>TableColumnFormat("lrrc")</SCRIPT>         <!-- create a class called .lrrc -->
// <TABLE border=1 class=lrrc>                        <!-- class name must match JS call -->
// ...
function TableColumnFormat(template)
	{
		document.write('<style type="text/css">\n');
		var ncols = template.length;
		var i, j;
		for (i=0; i<ncols; i++)
		{
			var stylestring = 'table.' + template + ' td:first-child ';
			for (j=0;j<i;j++)
			{
				stylestring = stylestring + '+ td ';
			}
			stylestring = stylestring + '{ text-align: ';
			var thischar = template.charAt(i);
			var alignstr = 'none';
			if (thischar == 'l')
				alignstr = 'left';
			if (thischar == 'c')
				alignstr = 'center';
			if (thischar == 'r')
				alignstr = 'right';
			stylestring = stylestring + alignstr + '; }\n';
			document.write(stylestring);
		}
		document.write('</style>');
		return;
	}

// Routine for ConList pages to give Safe/Unsafe status for each seat
function safe_seat(safeflag, winners)
{
	if (safeflag == true)
	{
		document.write('<TR><TD colspan=5 Align=Left><IMG src="safe.gif" align=top>&nbsp;<A href="safeseat.html?win=' + winners + '">This seat is <B>Safe</B></A> ');
		document.write('and voters have little say on their MP</TR>');
	}
	if (safeflag == false)
	{
		document.write('<TR><TD colspan=5 Align=Left><IMG src="unsafe.gif" align=top>&nbsp;<A href="unsafeseat.html?win=' + winners + '">This seat is not safe</A> ');
		document.write('and voters have a real say on their MP</TR>');
	}
	return;
}

function ShowHideDiv(element, divname)
{
	var oldstate = document.getElementById(divname).className;
	if (oldstate == "ShowHide_Hide")
	{
		document.getElementById(divname).className = "ShowHide_Show";
		element.innerHTML = "Hide";
	}
	else
	{
		document.getElementById(divname).className = "ShowHide_Hide";
		element.innerHTML = "Show";
	}
	return;
}

function log(message)
{
	if (!log.window_ || log.window_.closed) 
	{
		var win = window.open("", null, "width=400,height=200," +
							  "scrollbars=yes,resizable=yes,status=no," +
							  "location=no,menubar=no,toolbar=no");
		if (!win) return;
		var doc = win.document;
		doc.write("<html><head><title>Debug Log</title></head>" +
				  "<body></body></html>");
		doc.close();
		log.window_ = win;
	}
	var logLine = log.window_.document.createElement("div");
	logLine.appendChild(log.window_.document.createTextNode(message));
	log.window_.document.body.appendChild(logLine);
}


