//---------------------------------------------------------
//
// Library of Common Javascript routines
//
//
//
//*********************************************************
//
// Open a separate, smaller window with the name popup
//
//---------------------------------------------------------
//
// Global variables
//
//---------------------------------------------------------
function openPopup(pageName)
{
//if (GetCookie('ITPETdelay') == "off")
//	{
		var features = "width=420,height=420,left=0,top=0"
		window.open(pageName, "popup", features)
		//set popup window "flag" to open	
		SetCookie('ITPETpopup', 'open')
//	}
//	else
//	{
//	alert('The page was not finished loading.  Please wait until it is finished.')
//	SetCookie('ITPETdelay', 'off')
//	}
}
//---------------------------------------------------------
function openViewer(pageName, toolBar)
{	
var features = "height=420,left=0,top=0,scrollbars=yes,resizable=yes"
if (toolBar == 1)
	{
	var features = "height=420,left=0,top=0,scrollbars=yes,resizable=yes,toolbar=yes"
	}
window.open(pageName, "viewer", features)
}
//---------------------------------------------------------
function openGlossary(pageName)
{
	if (GetCookie('ITPETglossary') == 'open')
	{
		var features = "width=360,height=400,left=0,top=0"
		var glossaryWindow = window.open(pageName, "glossary", features)
		//set popup window "flag" to open	
		SetCookie('ITPETglossary', 'closed')
		glossaryWindow.close()
	}
	else
	{
		var features = "width=360,height=400,left=0,top=0"
		var glossaryWindow = window.open(pageName, "glossary", features)
		//window.open(pageName, "glossary", features)
		//set popup window "flag" to open	
		SetCookie('ITPETglossary', 'open')
	}
}
//---------------------------------------------------------
//
function closePopup()
{
	//set popup window "flag" to closed	
	SetCookie('ITPETpopup', 'closed')
	self.close()
}
//---------------------------------------------------------
//
function resumeSession(pageName)
{
	////SetCookie('ITPETresume', "/beginner/begin01.html")
	////pageName = GetCookie('ITPETresume')
	////alert(GetCookie('ITPETresume'))
	//if (GetCookie('ITPETresume') != null)
	//{
	//	parent.window.location.replace(GetCookie('ITPETresume'))
 	//}
	//else
	//{
		parent.window.location.replace(pageName)
	//}
}
//---------------------------------------------------------
//
function resetCounter(maximumActivities)
{
	//var maximumActivities = GetCookie('ITPETpageActivityTotal')
	SetCookie('ITPETmaximumActivities', maximumActivities)
	SetCookie('ITPETpageTotal', 0)
	SetCookie('ITPETpageFinished', "NO")
	if (maximumActivities == 0)
	{
		SetCookie('ITPETpageFinished', "YES")	
	}	
	//alert('ITPETpageTotal = ' + GetCookie('ITPETpageTotal'))
}
//---------------------------------------------------------
//
//  I want to use this for a delay function but the page ends up with a null
//  value for the cookie even though it echos back OK here. Why??
function loadCheck()
{
	SetCookie('IPTETdelay', "off")
}

//---------------------------------------------------------
//
function pageFinished(numberOfActivity)
{	
	var total = GetCookie('ITPETpageTotal')
	total = parseInt(GetCookie('ITPETpageTotal')) + 1
	SetCookie('ITPETpageTotal', total)
	//alert('Check of total after a click = ' + total)
	//alert('Check of ITPETpageTotal after a click = ' + GetCookie('ITPETpageTotal'))
	//alert('Chekc of ITPETmaximumActitvities = ' + GetCookie('ITPETmaximumActivities'))
	if (total >= GetCookie('ITPETmaximumActivities'))
	{
		SetCookie('ITPETpageFinished', "YES")
	}
}
//---------------------------------------------------------
//
function checkPage(pageName)
{
	// Proceed to a specific URL within current window after making
	// sure action was taken on the current page
	if (GetCookie('ITPETpopup') == "open")
	{
		alert('You forgot to close the popup window.  Please find the popup window and click the button to close.')
	}
	else
	{
		//Easter Egg
		//alert('Initial Setup Cookie = ' + GetCookie('ITPETinitialSetup'))
		if (GetCookie('ITPETinitialSetup') == "ON")
//	if (bypass == "on")
		{
			SetCookie('ITPETpageFinished', "YES")
		}
		if (GetCookie('ITPETpageFinished') == "YES")
		{
			//Make a cookie that will last 30 days to mark the page from which to resume
			var expDate = new Date()
			FixCookieDate (expDate)
			expDate.setTime (expDate.getTime() + (30 * 24 * 60 * 60 * 1000))
			SetCookie('ITPETresume', pageName, expDate)
			//The following line is from Netscape Developer's site
			//document.cookie = cookieName + "=" + escape (pageName) + "; expires=" + expDate.toGMTString()
			//alert('resume cookie: ' + GetCookie('ITPETresume') + 'expires ' + expDate)
			//SetCookie('ITPETdelay', "on")
			nextPage(pageName)
		}
			else
		{
				//alert (GetCookie('ITPETpageFinished'))
				alert('You forgot to do something.  Please read the directions.')
 				//alert('You forgot to do something. pageFinished cookie = ' + GetCookie('ITPETpageFinished'))
		}
	}
}
//---------------------------------------------------------
//
function nextFrame(pageName)
{
	// Proceed to a specific URL within current window
	//window.location.replace(pageName)
	// 0 = header frame
	// 1 = body frame
	// 2 = message frame
	parent.message.location=pageName
	//window.frames[frameNumber].location.replace(pageName)
	//window.location.replace(pageName)
}
//---------------------------------------------------------
//
function gotoPage(pageName)
{
	// Proceed to a specific URL within current window
	// without any cookie verification
	var expdate = new Date ()
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000))
	//SetCookie('ITPETdelay', 'on', expdate)
	//alert('ITPETdelay & expdate = ' + GetCookie('ITPETdelay') + '  ' + expdate)
	//document.cookie = "ITPETdelay" + escape ('set') + expdate.toGMTString() + "" + "" + ""
	//alert('ITPETdelay & expdate sen in the gotoPage function = ' + GetCookie('ITPETdelay') + '  ' + expdate)
	if (pageName == "firstpage")
	{
		pageName = "../intro/intro01.html"
	}
		moduleWindow = window.location.replace(pageName)
}
//---------------------------------------------------------
//
function nextPage(pageName)
{
	// Proceed to a specific URL within current window first making sure the delay cookie is off
//	if (GetCookie('ITPETdelay') == 'off')
	{
		// ??? is this used for the resume function?
		SetCookie('ITPETpageName', pageName)
		var currentPage = GetCookie('ITPETpageName')
//resume page could also be assigned here
		moduleWindow = window.location.replace(pageName)
		//  I don't think the delay is working  ????????????????????
//	}
//	else
//	{
//		//alert('The page was not finished loading.  Please wait until it is finished.')
//		alert('The page was not finished loading.  ITPETdelay = ' + GetCookie('ITPETdelay'))
//		SetCookie('ITPETdelay', 'off')
//		alert('ITPETdelay = ' + GetCookie('ITPETdelay'))
//		SetCookie('ITPETpageName', pageName)
	}
}
//---------------------------------------------------------
//
function refreshPage()
{
	var currentPage = GetCookie('ITPETpageName')
	moduleWindow = window.location.replace(currentPage)
}
//---------------------------------------------------------
//
function cookieTest()
{
	// Write and then read a test cookie.
	// This will determine if the browser has cookies enabled
	// and will give the user a message if not enabled.
	//	SetCookie('ITPETpopup', null)
	SetCookie('ITPETtestcookie', 'TEST')
		if (GetCookie('ITPETtestcookie') == "TEST")
		{
			SetCookie('ITPETtestcookie', null)
		}
		else
		(
			alert('The COOKIE option for this browser does not appear to be enabled.  Please ENABLE COOKIES NOW or you will not be able to continue.')
		)
}
//---------------------------------------------------------
//
function exitModule()
{
	if (confirm("Are you sure you want to exit?"))
	{	
		moduleWindow = close()
	}
	else
	{
		moduleWindow = window.location.replace("../start/start.html")
	}
}
//---------------------------------------------------------
//
function initialSetup()
{
	var expDate = new Date()
	//FixCookieDate (expdate)
	expDate.setTime (expDate.getTime() + (30 * 24 * 60 * 60 * 1000))
	SetCookie('ITPETinitialSetup', "ON")
	alert('Initial Setup Cookie = ' + GetCookie('ITPETinitialSetup'))
}
//---------------------------------------------------------
//
//
//
//  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
//
//  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
//  The following functions are released to the public domain.
//
//  This version takes a more aggressive approach to deleting
//  cookies.  Previous versions set the expiration date to one
//  millisecond prior to the current time; however, this method
//  did not work in Netscape 2.02 (though it does in earlier and
//  later versions), resulting in "zombie" cookies that would not
//  die.  DeleteCookie now sets the expiration date to the earliest
//  usable date (one second into 1970), and sets the cookie's value
//  to null for good measure.
//
//  Also, this version adds optional path and domain parameters to
//  the DeleteCookie function.  If you specify a path and/or domain
//  when creating (setting) a cookie**, you must specify the same
//  path/domain when deleting it, or deletion will not occur.
//
//  The FixCookieDate function must now be called explicitly to
//  correct for the 2.x Mac date bug.  This function should be
//  called *once* after a Date object is created and before it
//  is passed (as an expiration date) to SetCookie.  Because the
//  Mac date bug affects all dates, not just those passed to
//  SetCookie, you might want to make it a habit to call
//  FixCookieDate any time you create a new Date object:
//
//    var theDate = new Date();
//    FixCookieDate (theDate);
//
//  Calling FixCookieDate has no effect on platforms other than
//  the Mac, so there is no need to determine the user's platform
//  prior to calling it.
//
//  This version also incorporates several minor coding improvements.
//
//  **Note that it is possible to set multiple cookies with the same
//  name but different (nested) paths.  For example:
//
//    SetCookie ("color","red",null,"/outer");
//    SetCookie ("color","blue",null,"/outer/inner");
//
//  However, GetCookie cannot distinguish between these and will return
//  the first cookie that matches a given name.  It is therefore
//  recommended that you *not* use the same name for cookies with
//  different paths.  (Bear in mind that there is *always* a path
//  associated with a cookie; if you don't explicitly specify one,
//  the path of the setting document is used.)
//  
//  Revision History:
//
//    "Toss Your Cookies" Version (22-Mar-96)
//      - Added FixCookieDate() function to correct for Mac date bug
//
//    "Second Helping" Version (21-Jan-96)
//      - Added path, domain and secure parameters to SetCookie
//      - Replaced home-rolled encode/decode functions with Netscape's
//        new (then) escape and unescape functions
//
//    "Free Cookies" Version (December 95)
//
//
//  For information on the significance of cookie parameters, and
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.html    
//
//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
//document.cookie = "ITPETdelay" + escape ('on') + expdate.toGMTString() + "" + "" + ""

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//
//  Examples
//
//var expdate = new Date ();
//FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
//expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now 
//SetCookie ("ccpath", "http://www.hidaho.com/colorcenter/", expdate);
//SetCookie ("ccname", "hIdaho Design ColorCenter", expdate);
//SetCookie ("tempvar", "This is a temporary cookie.");
//SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");
//SetCookie ("paranoid", "This cookie requires secure communications",expdate,"/",null,true);
//SetCookie ("goner", "This cookie must die!");
//document.write (document.cookie + "<br>");
//DeleteCookie ("goner");
//document.write (document.cookie + "<br>");
//document.write ("ccpath = " + GetCookie("ccpath") + "<br>");
//document.write ("ccname = " + GetCookie("ccname") + "<br>");
//document.write ("tempvar = " + GetCookie("tempvar") + "<br>");
