// include comma delimited attributes for each entry
// the comma delimited fields are:
// 		permanent popupPageId (never changes)
//		active flag (1=active, 2=inactive
//		width of popup in pixels
//		height of popup in pixels
//		top of popup in pixels
//		left of popup in pixels
// 		the order
//
//		the array index is in priority order (highest priorities have lowest numbers
//
// the cookie saved will include:
//		a list of permananet popupPageIds served (in order served)
//		date last served popup
//		version number of popupPage data
//	in format popupPageId list 1|2|3!dateserved!version
//		popupPageId are delimited by vertical bars and they are separated from the other data by exclamation points

// Note: don't ever serve popups for those without cookies activated

var popupInterval = 7; // in days 
var expDays = 365*10; // number of days the cookie should last
// note windowprops is used by getwin()
var windowprops = "width=700,height=446,location=no,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes";
var commonWindowProperties = "location=no,toolbar=no,menubar=yes,scrollbars=yes,resizable=yes";
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
var popupPages = new Array();
var j;
var duplicatePage = false;
var cookiePath = "";
var cookieDomain = "";

popupPages[0] = "0,1,700,446,100,100,http://register.casinocity.com/rules.cfm";
popupPages[1] = "1,1,470,315,100,100,http://casinocity.com/Promotions/CasinoCityTimesPopup.html";

function checkCount() {

	var popupPageCookie = GetCookie('popupPage');
	var cookiePieces = new Array(); 
	var lastPopupIndex;
	var lastPopupPriority;
	var today = new Date();
	var daysSinceLastPopup;
	var	previouslyVisitedPages = new Array();
	var	lastVisitedPage;
	var newCookieValue = "";
	var currentPopup = new Array();

	if (popupPageCookie != null) {
		cookiePieces = popupPageCookie.split('!');
		daysSinceLastPopup = (today.getTime() - Date.parse(cookiePieces[1]))/1000/60/60/24;
		previouslyVisitedPages = cookiePieces[0].split('|');
		lastVisitedPage = previouslyVisitedPages[previouslyVisitedPages.length - 1];

	}
	else { 
	
	//		no popupPageCookie, try to set it and if that fails then bail
			SetCookie('TestCookie', 1);
			cookiePieces[0] = "";
			var testCookie = GetCookie('TestCookie');
			if (testCookie == null) {
				return;
			}
			if (GetCookie('count') > 0) {
	
//				if no popupPageCookie then use old "count" cookie as the first one in the queue (register.casinocity.com)
				currentPopup = popupPages[0].split(','); 
				newCookieValue = currentPopup[0] + "!" + today + "!0.9";
				SetCookie('popupPage',newCookieValue,exp);
				return;
			}
	}

//	if we got here then the user's browser supports cookies
// 	only proceed if we haven't served a popup in the popup interval
	if ((daysSinceLastPopup >= popupInterval) || popupPageCookie == null) {
		if (previouslyVisitedPages.length > 0) {
		
//			loop through the array of PopupPages to find the highest priority page never served
			for (var i=0;i < popupPages.length;i++) {
				duplicatePage = false;
				currentPopup = popupPages[i].split(',');
				
				// search for this page in the popup array in the cookie of pages served
				j = 0;
				while (j < previouslyVisitedPages.length) {
					if (currentPopup[0] == previouslyVisitedPages[j]) {
						duplicatePage = true;
					}
					j++;
				}
				if (duplicatePage == false) {
					break;
				}
			}
		}
		else {
		
//			serve the highest priority popup (first in the popupPages array) when no popups have yet been served
			currentPopup = popupPages[0].split(',');
		}

		if (cookiePieces[0] != "") newCookieValue = cookiePieces[0] + "|";
		newCookieValue = newCookieValue + currentPopup[0] + "!" + today + "!1.0";
		if (duplicatePage == false) {
			SetCookie('popupPage',newCookieValue,exp);
			window.open(currentPopup[6], "", "width=" + currentPopup[2] + ",height=" + currentPopup[3] + ",top=" + currentPopup[4] + ",left=" + currentPopup[5] + "," + commonWindowProperties);
		}
	}

}

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 SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	var cookieString = name + " = " + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	("; path=" + "/") + ("; domain=" + ".casinocity.com") +
	((secure == true) ? "; secure" : "");
	document.cookie = cookieString;
}

function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}



function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function getwin(url) {
	window.open(url, "", windowprops);
}

