//******************************************************************************
// File:			popup.js
// Author:			Fred Snyder
// Company:			Castwide Technologies (http://www.castwide.com)
// Date Created:	April 1, 2004
// Description:		This file requires startbuffer.inc.php, endbuffer.inc.php,
//					and popup.js to be installed on the web site.
//
// Revisions:		April 7, 2004: Improved cookie management to track
//					alternative entry/exit popups, and added an option to
//					override the cookie. - FS
//******************************************************************************

//******************************************************************************
// HOW TO USE THIS FILE
//
// 1. Copy popup.js, startbuffer.inc.php, and endbuffer.inc.php to your
//    web site.
//
// 2. Set the default entry and exit popup info in this file to your
//    preferences.  (See the first few lines of code directly below these
//    instruction comments.)
//
// 3. Include startbuffer.inc.php at the top of your PHP pages and
//    endbuffer.inc.php at the bottom.  These files will automatically add
//    Javascript code that keeps the exit popup from appearing when the
//    user follows a local link.  Example:
//
//        <? include "startbuffer.inc.php"; ?>
//        <HTML>
//            (YOUR HTML GOES HERE)
//        </HTML>
//        <? include "endbuffer.inc.php"; ?>
//
// 4. Add the following code between your HTML HEAD tags:
//
//        <script language="javascript" src="popup.js"></script>
//
// 5. Modify your HTML BODY tag so it includes the following event attributes:
//
//        <body onload="entryPopup();"
//              onbeforeunload="exitPopup();"
//              onunload="exitPopup();"
//        >
//
//    It is recommended that you call exitPopup() in both the onbeforeunload
//    and onunload events to insure that it functions correctly on all
//    browsers.  In browsers that support both events (currently IE only),
//    the cookie setting will keep the exit popup from appearing twice.
//
// 6. If you want to override the default popup on any page, you can specify
//    the URL, width, and height of the desired popup as arguments to the
//    entryPopup or exitPopup functions.  Example:
//
//        <body onload="entryPopup('alternate_popup.html', 320, 240);">
//
//    If you do not pass arguments to the entryPopup() and exitPopup()
//    functions, the default popups defined below will be used.
//
// 7. If you want the popup to appear whether or not its cookie has been
//    set, you can pass "true" as the last argument to the entryPopup()
//    or exitPopup() functions.  Example:
//
//        <body onload="entryPopup('popup.html', 320, 240, true);">
//
//    NOTE: Using this method to force the exit popup may cause unexpected
//    results, since JavaScript is unable to differentiate between exiting
//    the site and refreshing a page.
//
// 8. Once you have followed all these steps, entry and exit popups should
//    be automatically managed on your pages.
//
// 9. You can also call the openModal(url, width, height) and
//    openPopup(url, width, height) functions directly if you ever want to
//    activate a popup for any other event (e.g., the onclick event of a
//    button).
//******************************************************************************


// DEFAULT ENTRY POPUP INFO
// This is the information used by the entryPopup() function if no
// arguments are passed to it.
var entryURL = '/';
var entryWidth = 480;
var entryHeight = 360;

// DEFAULT EXIT POPUP INFO
// This is the information used by the exitPopup() function if no
// arguments are passed to it.
var exitURL = '/';
var exitWidth = 480;
var exitHeight = 360;


var gLocal = false;		// Flag to determine if a local link was clicked.
						// DO NOT CHANGE THIS FLAG.  It should ONLY be
						// be changed programmatically.

function localLink() {
	// This function is called by local links to tell the exitPopup function
	// not to open the exit popup
	gLocal = true;
}

function canShowModal() {
	// Only MSIE supports the showModalDialog function.
	if (navigator.appName == 'Microsoft Internet Explorer') {
		return true;
	}
	return false;
}

function setCookie(name, value, days) { 
	if (!days) days = 1; // default to 1 day if empty
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + days*24*60*60*1000); 
	document.cookie = name + "=" + escape(value) + "; expires=" + expdate.toGMTString(); 
} 

function getCookie (name) {
	var dc = document.cookie;
	var cname = name + "=";
	var clen = dc.length;
	var cbegin = 0;
	while (cbegin < clen) { 
		var vbegin = cbegin + cname.length;
		if (dc.substring(cbegin, vbegin) == cname) { 
			var vend = dc.indexOf (";", vbegin);
			if (vend == -1) vend = clen;
			return unescape(dc.substring(vbegin, vend));
		}
		cbegin = dc.indexOf(" ", cbegin) + 1;
		if (cbegin== 0) break;
	}
	return '';
}

function resetPopups() {
	// Clear the popup cookies and refresh the page.
	//setCookie('entryPopped', '', 1);
	//setCookie('exitPopped', '', 1);
	aCookies = String(document.cookie).split(";");
	for (t = 0; t < aCookies.length; t++) {
		if ((aCookies[t].indexOf('entry-') != -1) || (aCookies[t].indexOf('exit-') != -1)) {
			c = String(aCookies[t]).split("=");
			setCookie(c[0], '');
		}
	}
	window.location = window.location;
}

function exitPopup() {
	// Call this function in the body onbeforeunload or onunload
	// events to open an exit popup.
	// If arguments are passed to this function, they override
	// the default exit popup parameters.
	if (arguments.length > 0) {
		exitURL = arguments[0];
		exitWidth = arguments[1];
		exitHeight = arguments[2];
		// If last argument is true, pop the modal regardless
		// of the cookie setting
		if (arguments[3] == true) {
			openModal(exitURL, exitWidth, exitHeight);
			return;
		}
	}
	// Check the cookie to see if an exit popup has already
	// been opened.
	if (getCookie('exit-' + exitURL) != 'popped') {
		if (!gLocal) {
			setCookie('exit-' + exitURL, 'popped', 1);
			openModal(exitURL, exitWidth, exitHeight);
		}
	}
}

function entryPopup() {
	// Call this function in the body onload event to open
	// an entry popup.
	// If arguments are passed to this function, they override
	// the default entry popup parameters.
	if (arguments.length > 0) {
		entryURL = arguments[0];
		entryWidth = arguments[1];
		entryHeight = arguments[2];
		// If last argument is true, pop the modal regardless
		// of the cookie setting
		if (arguments[3] == true) {
			openModal(entryURL, entryWidth, entryHeight);
			return;
		}
	}
	// Check the cookie to see if an entry popup has already
	// been opened.
	if (getCookie('entry-' + entryURL) != 'popped') {
		setCookie('entry-' + entryURL, 'popped', 1);
		openModal(entryURL, entryWidth, entryHeight);
	}
}

function openPopup(url, width, height) {
	// Open a popup window.
	var screenLeft = (screen.width - width) / 2;
	var screenTop = (screen.height - height) / 2;
	var w = window.open(url, "_blank", "width=" + width + "px, height=" + height + "px, left=" + screenLeft + ", top=" + screenTop);
	w.focus();
}

function openModal(url, width, height) {
	// Open a modal dialog.  If the browser is not MSIE,
	// a normal popup window is opened instead.
	if (canShowModal()) {
		showModalDialog(url, "_blank", "status=no; directorybar=no; menubar=no; titlebar=no; scrollbars=yes; help=no; dialogHeight=" + height + "px; dialogWidth=" + width + "px;");
	} else {
		openPopup(url, width, height);
	}
}
