function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function FormValidator(theForm)
{

  if (theForm.email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.email.focus();
    return (false);
  }

  if (!isEmailAddr(theForm.email.value))
  {
    alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    theForm.email.focus();
    return (false);
  }
   
  if (theForm.email.value.length < 3)
  {
    alert("Please enter at least 3 characters in the \"email\" field.");
    theForm.email.focus();
    return (false);
  }
  return (true);
}

function writeTopTable(){
document.write(
'<table width="428" height="125">',
'<tr>',
'<td width="420" height="125">',
'</td></tr>',
'</table>'
)
}//writeTopTable

function writeHeader(){
document.write(
'<table border="0" cellpadding="3" width="799" height="50">',
'<tr>',
'<td colspan = 3 align="center"><p class = "header">',
'Historic Springwells Park',
'</p>',
'<p class = "subheader">Established 1939 by Edsel B. Ford</p></td>',
'</tr>',
'</table>'
)
}// end writeHeader

function writeHeader2(pageTitle){
document.write(
'<table border="0" width="789" height="50">',
'<tr>',
'<td width = "150"><img border="0" src="/images/springwells_welcome.jpg" width="144" height="121"></td>',
'<td width = "539" align="center" valign = "top" class = "header">',
'Historic Springwells Park<br><br>' + pageTitle + '</td>',
'</tr>',
'</table>'
)
}// end writeHeader

function writeHeader3(pageTitle){
document.write(
'<table border="0" cellpadding="3" width="799" height="50">',
'<tr>',
'<td width = "150"><img border="0" src="/images/springwells_welcome.jpg" width="144" height="121"></td>',
'<td align="center" valign = "top" class = "header">',
'Historic Springwells Park<br><br>' + pageTitle + '</td>',
'<td width = "150"></td>',
'</tr>',
'</table>'
)
}// end writeHeader


function writeExtraSpace(){
	//document.write(
	//	'Extra Space (for ads, other links, pictures?)'
	//	)
} //writeSearchMenu


function backgroundImage() {
	document.write (
		'<body background="http://www.springwellspark.org/images/springwells_backgrnd.gif">'
	)
}

function newsLetter() {
	var i=0;
	for (i=1; i<11 ; i++)
	{
		document.write (
		'<a href = "/Winter_2004_' + i + '.htm">Page ' + i + ' </a>&nbsp&nbsp&nbsp'
		)
	
		if (i == 5)
		{document.write (
		'<br><br><br>'
		)
		}//end if
	    
	}//end for
}

function navigateNews(page, season) {
	var i=0;
	var size = 0;   //number of pages in letter, TBD by 'season'
	var prev = 0;
	var next = 0;
	var season_text = "";
	
	prev = page - 1;
	next = page + 1;

	if (season == 'autumn03')
	{size = 20;
	season_text = "../Arch1vedFJG_Newslettrs/Autumn_2003_";
	}

	if (season == 'winter04' || season == 'winter04_sanitized')
	{size = 10;
	season_text = "/Winter_2004_";
	}

	if (page == 1)
	{prev = size;
	}

	if (page == size)
	{next = 1;
	}

	document.write (
		'<td width="277" align = "center" class="subheader2">',
		'<a href = "' + season_text + prev + '.htm"> ...prev</a>&nbsp&nbsp',
		'<a href = "' + season_text + next + '.htm">next... </a><br><br><br>',
		'Go To Page:'
		)

	for (i=1; i<size + 1 ; i++)
	{
		if (i != page)
		{document.write (
		'<br><br>',
		'<a href = "' + season_text + i + '.htm">Page ' + i + ' </a>'
		)
		}//end if

		if (i == page)
		{document.write (
		'<br><br>',
		'<i>This is page ' + i + '</i>'
		)
		}//end if
	    
	}//end for

	document.write (
		'</td>'
		)

	if (season == 'winter04')
	{
	
		document.write (
		'<td width="643" height="258" align = center>',
		'<img border="0" src="/images/winter_2004_' + page + '.jpg" width="634" height="898"></td>'
		)
	}

	if (season == 'winter04_sanitized')
	{
	
		document.write (
		'<td width="643" height="258" align = center>',
		'<img border="0" src="/images/winter_2004_' + page + '_sanitized.jpg" width="634" height="898"></td>'
		)
	}

}//end navigateNews

//newsImage was written for the autumn 2003 newsletter.  Not needed for winter 2004 and beyond
function newsImage(page){
	document.write (
		'<td width="643" height="258" align = center>',
		'<img border="0" src="/images/spa-page-' + page + '.jpg" width="634" height="898"></td>'
	)
}


//****Begin Cookie Management**************************************************************************
function setCookie(user_entry, page_ID){
	//remember to restrict all but numbers and letters client and server side
	// then write catches for errors
	//alert ("setCookie is running");
	//alert (user_entry);
	//alert (page_ID);

	//if user enters quit while setting cookie, set password to quit
	if (user_entry == 'quit')
	{
	var the_cookie = "spa_cookie="+ escape("password:"+user_entry+"&page:" +page_ID);
    document.cookie = the_cookie;
    alert("You will see no confidential data on this site.");
	}

	else if (user_entry != 'quit')
	{
	var the_cookie = "spa_cookie="+ escape("password:"+user_entry+"&page:" +page_ID);
    document.cookie = the_cookie;
	//alert ("cookie was just written");
	}
}

function readTheCookie(the_info)
{	// load the cookie into a variable and unescape it
	var the_cookie = document.cookie;
	var the_cookie = unescape(the_cookie);

	// separate the values from the cookie name
	var broken_cookie = the_cookie.split("=");
	var the_values = broken_cookie[1];
	
	// break each name:value pair into an array
	var separated_values = the_values.split("&");

	// loop through the list of name:values and load up the associate array
	var property_value = "";
	for (var loop = 0; loop < separated_values.length; loop++)
	{
		property_value = separated_values[loop];
		var broken_info = property_value.split(":");
		var the_property = broken_info[0];  //i.e. 'password' or 'page'
		var the_value = broken_info[1];		//i.e. 'cat, dog, etc' or '/blah.blah.php
		the_info[the_property] = the_value;
	}
}
 



//html pages call this function and pass the desired .php pages through page_ID
function readCookie(page_ID){
	//for some help see http://hotwired.lycos.com/webmonkey/98/29/index1a_page10.html?tw=programming
	//alert ("running readCookie");
	var user_entry = "";
	var user_password = "";
	var stored_page = "";

	// if cookie is null, ask user to enter password or to enter 'quit' as password
	if (document.cookie == "")
	{	//alert ("we found a null cookie");
		user_entry = prompt("This page has confidential data.  Enter password (type 'quit' to quit)", "");
		if (user_entry == 'quit')
		{//alert ("you want to quit - going to setCookie");
			setCookie('quit', page_ID)
		}

		else 
		{//alert ("going to setCookie");
			setCookie(user_entry, page_ID)
		}
	}

	//if there was a cookie when readCookie was called, or after writing the cookie, read the cookie
	var cookie_information = new Array();
	readTheCookie(cookie_information);
	user_password = cookie_information["password"];
	stored_page = cookie_information["page"];

	//alert ("we just read the cookie after writing it - password and stored page");
	//alert (user_password);
	//alert (stored_page);

	// if password 'quit', load the php page. It will show only public data.  
	// No need to rewrite cookie.  We'll just use page_ID with each page load.
	if (user_password == 'quit') 
	{
		
	//alert ("password was quit - password and stored page");
	//alert (user_password);
	//alert (stored_page);
		location.replace(page_ID)
	}
		;
	// if password not 'quit', check if new page is called (cookie's stored_page = page_ID called by readCookie ?)
	// if new page not called, send cookie (w/password and stored_page) to validation form submit page
	// if new page called, delete and rewrite cookie w/same password but new page from page_ID. Th
	if (user_password != 'quit') 
	{
		if (stored_page == page_ID)
		{
	//alert ("readCookie 'stored_page == page_ID' - user password and stored page");
	//alert (user_password);
	//alert (stored_page);
			location.replace('/submit_cookie_data.htm');
		}

		else if (stored_page != page_ID)
		{	//delete the cookie by setting its time to expire in the past
			//alert ("trying to kill cookie");
			var the_date = new Date("December 31, 2003");
			var the_cookie_date = the_date.toGMTString();
			var the_cookie = "spa_cookie=" + escape(user_password);
			the_cookie = the_cookie + ";expires=" + the_cookie_date;
			document.cookie = the_cookie;
		
			// killed cookie but retained user's password in user_password variable - now rewrite cookie to new page
			setCookie(user_password, page_ID)
			//alert ("cookie should be rewritten - redirect for testing");
			location.replace('/submit_cookie_data.htm');
		}
	}
}

function submitCookieData(){
	//alert ("submitCookieData");
	var user_entry = "";
	var user_password = "";
	var stored_page = "";

	if (document.cookie == null)
	{	
	//alert ("cookie is null");
	//write a cookie while testing this function
	var the_cookie = "spa_cookie="+ escape("password:"+user_entry+"&page:" +page_ID);
    document.cookie = the_cookie;
	}

	
	var cookie_information = new Array();
	readTheCookie(cookie_information);
	user_password = cookie_information["password"];
	stored_page = cookie_information["page"];

	//alert ("submitCookieData user password and stored page");
	//alert (user_password);
	//alert (stored_page);



	document.write (
		'<form name="frmPassword" method="post" action="' + stored_page + '">',
		'<input type="Hidden" name="pasPassword" value ="' + user_password + '"><br>'
	)

	//automatically submit the form
	document.forms['frmPassword'].submit();
}

function resetCookie(){
//read the cookie to get page_ID
	var stored_page ="";

	var cookie_information = new Array();
	readTheCookie(cookie_information);
	//user_password = cookie_information["password"]; we don't care because it's wrong
	stored_page = cookie_information["page"]

// kill the cookie
	var the_date = new Date("December 31, 2003");
	var the_cookie_date = the_date.toGMTString();
	var the_cookie = "spa_cookie=" + escape("");
	the_cookie = the_cookie + ";expires=" + the_cookie_date;
	document.cookie = the_cookie;

	readCookie (stored_page)

}

function tryAgain() {
	document.write (
		'<input type="button" value="Try Again" onClick="newCookie()">'
	)

}

function newCookie() {
	var stored_page ="";
		
	//read the cookie for stored page
	var cookie_information = new Array();
	readTheCookie(cookie_information);
	//user_password = cookie_information["password"];
	stored_page = cookie_information["page"];


	// after reading coookie for stored page, destroy it
	var the_date = new Date("December 31, 2003");
	var the_cookie_date = the_date.toGMTString();
	var the_cookie = "spa_cookie=" + escape("");
	the_cookie = the_cookie + ";expires=" + the_cookie_date;
	document.cookie = the_cookie;

	// now rewrite cookie with user entered password and stored page
	readCookie (stored_page)
}