// checkin1st: Require browser to view greeting page.
//
// Copyright Idleswell Software Creations, 2005
//
// 0.00 25Jun2005 AI Initial version
// 0.01 25Jun2005 AI Check1st, Checkin1st
// 0.02 25Jun2005 AI Documentation
// 0.03 25Jun2005 AI this.guest = "/index_new.html";
// 0.04 27Jun2005 AI onclick='Checkin1st("guest");'
// 0.05 27Jun2005 AI this.guest = "/index.html";

// This module allows a webmaster to require a greeting page to be
// mandatory viewing before accessing other pages on a website.
// If the greeting page has not been viewed prior to loading a
// protected page, the browser is directed to the greeting page.

// A greeting page is modified to load a cookie into the browser.
// When a protected page is loaded: if the cookie from the greeting
// page is not present, the browser is redirected to the greeting page.

// Pages are prepared as follows:

// 1. Add the greeting page to check1st (in this module):

//    Insert a line into the function Check1st() of this format:

//    this.cookie = "greeting page";

//    where -
//       cookie = an identifier for the cookie loaded by the greeting page
//       "greeting page" = a URL to redirect the browser if cookie
//              is not found when loading a protected page
//              A URL may be absolute (http://...) or relative.

// 2. Insert code to create a cookie into the greeting page:

//    Somewhere on the page (in the <HEAD> section)...

//    <SCRIPT language=javascript src="/checkin1st.js"></SCRIPT>

//    Then, attach the routine to insert the cookie when a link on the page
//    is clicked:

//    <a ... href="HomePages/Home_Page.htm" onclick='Checkin1st("guest");'>

//    The argument to Checkin1st() must match the cookie defined in Check1st().

//    There are other ways to activate the cookie, but this suffices for
//    the purposes of this initial requirement.

// 3. Insert verification code on page protected by this greeting page:

//    Somewhere on each protected page (in the <HEAD> section)...

//    <SCRIPT language=javascript src="/checkin1st.js"></SCRIPT>
//    <SCRIPT>Checked1st("guest");</SCRIPT>

//    The argument to Checked1st() must match the cookie defined in Check1st().

//    Multiple pages may be protected by each cookie, but each page must
//    include the verification call to Checked1st().

var check1st = new Check1st();

// Check1st():
//    Initialize check1st.

function Check1st() {
	this.guest = "/index.html";
}

// Checkin1st(cook):
//    Load checkin cookie on greeting page.

function Checkin1st(cook) {
	for ( chk in check1st )
		if (cook == chk)
			document.cookie = cook + "=" + escape(check1st[cook]) + ";path=/";
}

// Checked1st(cook):
//    Unless cookie checked first, redirect browser to greeting page.

function Checked1st(cook) {
	for ( chk in check1st ) {
		if (chk != cook) continue;
		var greet = CookieValue(chk);
		if (greet != check1st[cook])
			window.open(check1st[cook], "_self");
	}
}

// CookieValue(cook):
//    What is cookie value?

function CookieValue(cook) {
	if (document.cookie.length == 0) return("");    // no cookies
	var offset = document.cookie.indexOf(cook+"="); // cookie key
	if (offset == -1) return("");           // no cookie
	offset += cook.length + 1;              // start of value
	var end = document.cookie.indexOf(";", offset);
	if (end == -1) end = document.cookie.length;
	return(unescape(document.cookie.substring(offset, end))); // cookie value
}
