// FMI generic scripts
// Copyright Family Matters Institute 2007-2009

// Default root of menu targets. Can be overridden
var menuRoot="./";

// Write some header stuff for all pages
document.write(
//	'<base href="http://www.familymatters.org.uk/">'+
	'<link rel="shortcut icon" href="favicon.ico" />');


// Date functions

  function GetMonth(intMonth){
    var MonthArray = new Array("January", "February", "March",
                               "April", "May", "June",
                               "July", "August", "September",
                               "October", "November", "December") 
    return MonthArray[intMonth] 	  	 
    }

  function getDateStr(){
    var today = new Date()
    var year = today.getYear()
    if(year<1000) year+=1900
    var todayStr = GetMonth(today.getMonth()) + " " + today.getDate()
    todayStr += ", " + year
    return todayStr
    }

// Printing

var gAutoPrint = true; // Flag for whether or not to automatically call the print function

function printSpecial()
{
	if (document.getElementById != null)
	{
		var html = '<HTML>\n<HEAD>\n';

		if (document.getElementsByTagName != null)
		{
			var headTags = document.getElementsByTagName("head");
			if (headTags.length > 0)
				html += headTags[0].innerHTML;
		}
		
		html += '\n</HE' + 'AD>\n<BODY class="printing">\n';
		
		var printReadyElem = document.getElementById("printReady");
		
		if (printReadyElem != null)
		{
				html += printReadyElem.innerHTML;
		}
		else
		{
			alert("Could not find the printReady section in the HTML");
			return;
		}
			
		html += '\n</BO' + 'DY>\n</HT' + 'ML>';
		
		var printWin = window.open("","printSpecial");
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		if (gAutoPrint)
			printWin.print();
	}
	else
	{
		alert("Sorry, the print ready feature is unavailable.");
	}
}

// Send to a friend

function send_to_friend()
{
	var title = document.title;
	title = title.replace(/&/g,"and");
	return 'mailto:?subject=Page from FMI web site&body=Take a look at the <'+
		title+'> page on the Family Matters Institute web-site: '+
		document.URL;
}

// Page construction

function write_related( items, head, root )
{
	if (head==undefined) head = "Related pages";
	if (root==undefined) root = "./";
	document.write(
		'<table class="tabRelated"><tbody>'+
		'<tr><th class="thRelated">'+
		'  <p>'+head+'</p>' +
		'</th></tr>\n' );
	for (var i=0; i<items.length; i++)
	{
	  var page = items[i].toLowerCase();
	  var name = "";
	  var type = ".php";
	  var addr = undefined;
	  switch (page)
	  {
	  case "index":		name = "Home"; break;
	  case "about":		name = "About FMI"; break;
	  case "who":		name = "Who's Who"; break;
	  case "contact":	name = "Contact Us"; break;
	  case "find":		name = "Find Us"; break;
	  case "donate":	name = "Make a donation"; break;

	  case "flatpack":	name = "Flat Pack Parent"; break;
	  case "dads":		name = "Energizing Dads"; break;
	  case "puzzle":	name = "Parenting Puzzle"; break;
	  case "wrapc":		name = "Want to Run a Parenting Course?"; break;
	  case "fpp":		name = "Flat Pack Training"; break;
	  case "twp":		name = "Training for Work with Parents"; break;
	  case "dadtalk":	name = "DadTalk"; break;

	  case "money":		name = "Family Money"; break;
	  case "pocket":	name = "Pocket Money Course"; break;
	  case "moneytree":	name = "Money Tree"; break;
	  case "wramas":	name = "Want to Run a Money Activity Session?"; break;

	  case "workforce":	name = "Workforce Development"; break;
	  case "wspp":		name = "Workforce Strategy Partners"; break;
	  case "wsppnational":	name = "WSPP Nationally"; break;
	  case "wsppbeds":	name = "WSPP Bedfordshire"; break;
	  case "wsppherts":	name = "WSPP Hertfordshire"; break;
	  case "corporate":	name = "Corporate Services"; break;

	  case "published":	name = "Research Reports"; break;
	  case "grandparents":	name = "Grandparents Research"; break;

	  case "articles":	name = "Articles Archive"; break;
	  case "store":		name = "Resources Store"; break;
	  case "media":		name = "Press Room"; break;
	  case "jobs":		name = "Job Vacancies"; break;

	  case "admin":		name = "Admin Page"; addr="admin/index.php"; break;

	  default:		name = page.substr(0,1).toUpperCase()+page.substr(1);
	  }
	  if (addr==undefined)
		addr=page+type;
	  document.write(
		'<tr><td valign=center>'+
		'  <table border=0 cellpadding=0 width="100%"><tr>'+
		'    <td width=16 valign=top><a href="'+root+addr+'"><img src="'+root+'icon/book_next.png" class="icon"></a></td>'+
		'    <td class="tdRelated"> <a href="'+root+addr+'">'+name+'</a></td>'+
		'  </tr></table>'+
		'</td></tr>\n' );
	}
	document.write(
		'</tbody></table>\n' );

}

function write_sidefooter( root )
{
	var closer = "false";
	if (root=="extract") {
		closer = "true";
		root = "../";
	}
	else if (root==undefined) root = "./";

	// Survey link
	//document.write(
	//	'<p class="pSidebar">Take our <br><a href="http://www.surveymonkey.com/s.aspx?sm=o_2bnQTRtzqqRWPhOM6H64gg_3d_3d"><img src="'+root+'icon/surveymonkeylogo.gif" width=33 height=29 border=0 align=left>Fathers Social Media Site survey</a></p>');

	// Doctored URL
	var url = document.URL;
	//url = url.replace(/the-park.net\/fmi\//,"www.familymatters.org.uk/");
	var title = document.title;
	title = title.replace(/&/g,"and");

	// Print & Send to friend
	document.write(
		'<p class="pSidebar"><a href="javascript:printSpecial()"><img src="'+root+'icon/printer.png" width=16 height=16 border=0> Print this page</a></p>' +
		'<p class="pSidebar"><a href="mailto:?subject=Page from FMI web site&body=Take a look at the &quot;'+
		 title+'&quot; page on the Family Matters Institute web-site: '+url+
		'"><img src="'+root+'icon/email_go.png" width=16 height=16 border=0> Send to a friend</a></p>');

	// Close window
	if (closer=="true") document.write(
		'<p class="pSidebar"><a href="javascript:window.close()"><img src="'+root+'icon/door_out.png" width=16 height=16 border=0> Close window</a></p>');
}


var sidebar_images = ["col12.gif", "col14.jpg", "col15.jpg", "col16.jpg", "col17.jpg", "col18.jpg"];

function write_sidebar( related1, title1, related2, title2, root )
{
	if (root==undefined) root = "./";
	document.write(
		'<TD class="tdSidebar" valign=top width=135>\n' );

	write_related( related1, title1, root );

	if (related2 != undefined)
		write_related( related2, title2, root );

	// DadTalk link
	document.write(
		'<br><p class="pSidebar"><a href="http://www.dadtalk.co.uk">Calling all Dads:</a><br><a href="http://www.dadtalk.co.uk"><img height=70 width=120 src="'+root+'image/dadtalk120.png" title="DadTalk social networking and support for fathers" alt="DadTalk social networking and support for fathers" border=0></a><br><a href="http://www.dadtalk.co.uk">Forums, articles, news etc for Dad</a></p>\n' );

	// Random picture
	//var next = Math.floor( Math.random() * 0.99 * sidebar_images.length );

	//document.write(
	//	'<br> <IMG height=50 alt="" src="'+root+'image/'+sidebar_images[next]+'" width=120 border=0 style="margin-left:9px">\n' );

	// Footer links
	write_sidefooter(root);

	document.write( '</TD>\n' );
}

function write_article_sidebar( type )
{
	document.write(
		'<TD class="tdSidebar" valign=top width=135>\n' );

	if (type=="review")
		write_related( ["reviews"], undefined, "../" );
	else
		write_related( ["articles"], undefined, "../" );

	var next = Math.floor( Math.random() * 0.99 * sidebar_images.length );

	document.write(
		'<br> <IMG height=50 alt="" src="../image/'+sidebar_images[next]+'" width=120 border=0 style="margin-left:9px">\n' );


	write_sidefooter( "../" );

	document.write( '</TD>\n' );
}

function write_pagefooter( root )
{
	if (root==undefined) root = "./";
	document.write(
		'</tbody></table>\n'+  //end of main body table
		'<TABLE cellSpacing=0 cellPadding=0 width=770 xborder=0 align=center class="tabFoot"><tbody><tr>\n'+
		'<TD class="tdFootLeft" vAlign=top>'+
		'<p class="pFoot">FMI is a Charitable Company, limited by guarantee.<br>'+
		'Regd Company No: 4746205<br>'+
		'Regd Charity No: 1101587<br>'+
		'VAT Registration number: 859429867<BR></p>'+
		'</TD>\n'+
		'<TD class="tdFootRight" vAlign=top align=right>'+ // background="./image/sp2bot.gif">'+
		'<p class="pFoot">Copyright &copy; FMI 2007-2009<br>'+
		'<A class="aFoot" href="'+root+'privacy.html">Privacy Policy</A> | '+
		'<A class="aFoot" href="'+root+'disclaimer.html">Disclaimer</A><br>'+
		'<A class="aFoot" href="'+root+'sitemap.php">Site map</A><br>'+
		'<a class="aFoot" href="http://www.famfamfam.com/lab/icons/silk/">Silk icons</a> by Mark James</p>'+
		'</TD>\n</TR></TBODY></TABLE>\n' );
}

function write_topbanner( root )
{
	if (root==undefined) root = "./";
	document.write(
		'<TABLE cellSpacing=0 cellPadding=0 width=770 border=0 align=center class="tabHead"><TBODY>\n'+
		'  <TR><TD class="tdTopLeft">'+
		'    <A href="'+root+'index.php">'+
		'    <IMG height=40 alt="" src="'+root+'image/clear.gif" width=354 border=0></A>'+
		'    <p class="pHead">Learning together to enrich<br>family life &amp; relationships</p>'+
		'  </TD></TR>\n'+
		'</tbody></table>\n'+
		// start of main body table
		'<TABLE cellSpacing=0 cellPadding=0 width=770 border=0 align=center class="tabMain"><TBODY>\n' );
}

function start_page( related1, title1, related2, title2, root )
{
	write_topbanner(root);

  	document.write("<TR>");

	write_sidebar(related1, title1, related2, title2, root);

  	document.write('<TD class="tdMain" valign=top>');
}

function end_page( root )
{
	document.write("</TD></TR>");

	write_pagefooter(root);
}

function start_article( type )
{
	write_topbanner("../");

  	document.write("<TR>");

	write_article_sidebar( type );

  	document.write('<TD class="tdMain" valign=top>');
}

function the_s_word( how )
{
	if( how=="cap" )
		document.write("S"+"e"+"x");
	else
		document.write("s"+"e"+"x");
}

function more( url )
{
	document.write( ' &nbsp; &nbsp; <a href="'+url+'"><img src="./icon/book_next.png" class="icon">&nbsp;<b>More...</b></a>' );
}

function read_extract( file )
{
	window.open("./extract/"+file,'Extract','scrollbars=yes,toolbar=no,width=800,height=600');
}
