/**
 * JavaScript Popup Script
 * Copyright (c) 2005 Mirchev Ideas Ltd. All rights reserved.
 */

/**
 * Documentation
 * How do we do it: We use the javascript event onload.
 * In the HTML files where you'd need the popup functionality, please load the
 * the javascript with (in that order) :
 * <script src="config.js" type="text/javascript" language="javascript"></script>
 * <script src="popup.js" type="text/javascript" language="javascript"></script>
 * where 'config.js' is the config file for the desired popup mode.
 * This is advisably done in the <head> section. Aslo please put inside the body tag:
 * onload="popup()"
 *
 * Please note that some browsers (for example Firefox) has a built-in popup blocking protection,
 * in that case the script will not work.
 * The script has been successfully tested on Internet Explorer 6 and Firefox 1.0 on Windows,
 * Firefox 1.0 and Mozilla 1.6 on Linux.
 */


function popup()
{

	// coockie expiration date
	var cookieExpDate = 60;
	var expDate = new Date();
	expDate.setDate(expDate.getDate() + cookieExpDate);

	// today
	var d = new Date();
	var monthname = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	var today = (Date.parse(parseInt(d.getDate()) + " " + monthname[d.getMonth()] + " " + d.getFullYear()));

	// no cookie: first visit
	if (getCookie("popup['startDate']") == null) {
		setCookie("popup['startDate']", today, expDate);
		// general cookies
		setCookie("popup['lastShown']", -1, expDate);
		// pageViews options cookies
		setCookie("popup['pagesView']", 0, expDate);
		// visitsAfter cookies
		setCookie("popup['visitsAfter']", 1, expDate);
	}

	// cookies expiration date
	cookieExpDate = getExpDate(today, getCookie("popup['startDate']"));
	// the current popup index to be processed
	var popupToShow = (getCookie("popup['lastShown']") * 1) + 1;
	// get the current page visit
	var pagesView = (getCookie("popup['pagesView']") * 1) + 1;
	// todays visit is # days after first
	var daysAfter = countDays(today, getCookie("popup['startDate']"));
	// current visit is # visits after first
	var visitsAfter = getCookie("popup['visitsAfter']")


	for (var i = (popupToShow + 1); i <= 4; i++) {
		if (typeof(popupMode[i]) != 'undefined' && popupMode[i] == 'pagesView') {
			for (popupPage in popupUrl[i]) {
				if (parseInt(popupPage) == parseInt(pagesView)) {
					// show the pop-up ti the site visitor
					popupWin = openPopup(popupUrl[i][pagesView], 'pagesView - ' + popupToShow, popupWidth[popupToShow], popupHeight[popupToShow]);
					// set the viewed-popup-in-this-session flag to true
					setCookie("popup['visitsAfterCurrent']", true);
					break;
				}
			}
			break;
		}
	}

	for (var i = popupToShow; i <= 4; i++) {
		if (typeof(popupMode[i]) != 'undefined') {
			switch (popupMode[i]) {
				case 'pagesView' :
					pagesViewPopup(i, pagesView, cookieExpDate);
					break;
				case 'daysAfter' :
					daysAfterPopup(i, daysAfter, cookieExpDate);
					break;
				case 'visitsAfter' :
					visitsAfterPopup(i, visitsAfter, cookieExpDate);
					break;
			}
			break;
		}
	}

	// save the current page number
	setCookie("popup['pagesView']", pagesView, expDate);
	// increase the visits counter
	if (getCookie("popup['visitsAfterCurrent']") == null) {
		setCookie("popup['visitsAfter']", (visitsAfter * 1) + 1, expDate);
	}
	return true;
}


function pagesViewPopup(popupToShow, pagesView, expDate)
{
	for (popupPage in popupUrl[popupToShow]) {
		if (parseInt(popupPage) == parseInt(pagesView)) {
			// show the pop-up ti the site visitor
			popupWin = openPopup(popupUrl[popupToShow][pagesView], 'pagesView - ' + popupToShow, popupWidth[popupToShow], popupHeight[popupToShow]);
			// save the shown pop-up index
			setCookie("popup['lastShown']", popupToShow, expDate);
			setCookie("popup['visitsAfterCurrent']", true);
			break;
		}
	}

	return true;
}


function daysAfterPopup(popupToShow, daysAfter, expDate)
{
	for (days in popupUrl[popupToShow]) {
		if (popupUrl[popupToShow][days] != 'undefined' && daysAfter >= days) {
			// show the pop-up ti the site visitor
			popupWin = openPopup(popupUrl[popupToShow][days], 'pagesView - ' + popupToShow, popupWidth[popupToShow], popupHeight[popupToShow]);
			// save the shown pop-up index
			setCookie("popup['lastShown']", popupToShow, expDate);
			setCookie("popup['visitsAfterCurrent']", true);
			break;
		}
	}
	return true;
}


function visitsAfterPopup(popupToShow, visitsAfter, expDate)
{
	if (!getCookie("popup['visitsAfterCurrent']")) {
		for (visits in popupUrl[popupToShow]) {
			if (popupUrl[popupToShow][visits] != 'undefined' || visitsAfter >= visits) {
				// show the pop-up ti the site visitor
				popupWin = openPopup(popupUrl[popupToShow][visits], 'visitsAfter - ' + popupToShow, popupWidth[popupToShow], popupHeight[popupToShow]);
				// save the shown pop-up index
				setCookie("popup['lastShown']", popupToShow, expDate);
				setCookie("popup['visitsAfterCurrent']", true);
				break;
			}
		}
	}
	return true;
}


function getExpDate(today, startDate)
{
	var expDate = new Date();
	expDate.setDate(expDate.getDate() + (15 * 2 * 2 - countDays(today, startDate)));
	return expDate;
}


function countDays(timestamp1, timestamp2)
{
	return Math.round(Math.abs(timestamp1 - timestamp2) / 86400000);
}


// check clients browser and platform
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));


/**
 * Open a window: modal for ie, non-modal for other browsers
 */
function openPopup(url,name,width,height)
{

	// temp fix
	if (navigator.cookieEnabled != true)
		return false;
	// end of temp fix

	// set center of screen values
	var leftPx = ((screen.width  - width)  / 2);
	var topPx  = ((screen.height - height) / 2);

	if (is_ie && is_win) {
		return window.showModalDialog(url,name,'dialogWidth:'+width+'px;dialogHeight:'+height+'px;dialogTop:'+topPx+'px;dialogLeft:'+leftPx+'px');
	} else {
		return window.open(url,name,'scrollbars=yes,width='+width+',height='+height+',top='+topPx+',left='+leftPx);
	}
}


/*
 * Resize a given window to a given dimensions
 * Make sure it's centered
 * This function is not used, but it's provided just in case as you may need it
 * onLoad(window,700,700) will resize the current window to this dimensions
 * for example if you need the thank you screen to be resized
 */
function resizeAndCenter(window,width,height)
{
	window.resizeTo(width,height);
	window.moveTo((screen.width-width)/2, (screen.width-height)/2);
}


/*
 * Timer to focus the current window
 */
function FocusTimer()
{
	if (timerID) {
		clearTimeout(timerID);
	}
	if (is_ie && is_win) {
		document.focus();
	} else {
		window.focus();
	}
	timerID = setTimeout("FocusTimer()", 500);
}


/*
 * Make a window modal
 * at least make it look that way although it's not
 */
function setModal()
{
	timerID = setTimeout("FocusTimer()", 500);
}


/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
	var path = '/'; // temp fix
	var domain = '.eslemployment.com'; // temp fix

	document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) {
        	return null;
        }
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}


/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create 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";
    }
}
