//==============================================================
//exitpop() - Executes MyFreeExitPopup() based upon cookie value
//==============================================================
//f should = 1 or 0. If f=1, the script will call
//MyFreeExitPop.  MyFreeExitPop will show the user popup windows
//based on the value of some cookies.
//To set up this function, use the body tag, and script tags below.
//<body onunload="exitpop(f)">
//<script LANGUAGE="javascript">var f=1</script>
//You must also add the onClick="f=0" parameter to ALL links on the html 
//page that is calling this script, or it will cause MyFreeExitPop to 
//display interstitials instead of exit windows.
//<a href="default.htm" onClick="f=0">
function exitpop(f) {
   if ( f == 1 ) {
	MyFreeExitPopup();
	}
        }

//************START OF POPUP EXIT CODE****************
function MyFreeExitPopup() {
//define URL Vars


//var url1="http://www.popularliving.com/pops/AMP9992.php";

var url1="http://www.popularliving.com/pops/AMP9999.php";

//var url3="http://www.popularliving.com/pops/AMP9995.php";

var url2="http://www.popularliving.com/pops/YFP9992.php";

var url3="http://www.popularliving.com/pops/AMP9994.php";

var url4="http://www.popularliving.com/pops/AMP9998.php";

var url5="http://www.popularliving.com/pops/AMP9991.php";

var url6="http://www.popularliving.com/pops/FSW9999.php";

var NoPopup="off"

//define # of popups
var NumberOfPopups=6;

//define interval between popups
var DaysBetweenPopups=1;

//define today's date
datevar = new Date(); 
month_now = datevar.getMonth();
day_now   = datevar.getDate();
year_now  = datevar.getFullYear();
date_now = month_now + " " + day_now + " " + year_now;

//------------ READ COOKIE SECTION ------------\\
var last_popped = GetCookie("MYFLastExitPopDate");

if ( last_popped )
	   {
//See if popup was within interval defined in "DaysBetweenPopus" 
	//Extract Date From Cookie
	date_last_popped = last_popped.split(" ");
	month_last_popped = date_last_popped[0];
	day_last_popped   = date_last_popped[1];
	year_last_popped  = date_last_popped[2];
	
	//Compare The Dates
	var DateDifference = CompareDates (month_now, day_now, month_last_popped, day_last_popped );
	if (DateDifference >= DaysBetweenPopups)
		{  
		//Set Popup URL 
		LastPopupID = GetCookie("MYFLastExitPopID");
		switch (LastPopupID) {
		case "1":
			PopupUrl = url2
			PopupID  = 2
			break
		case "2":
			PopupUrl = url3
			PopupID  = 3
			break
		case "3":
			PopupUrl = url4
			PopupID  = 4
			break
		case "4":
			PopupUrl = url5
			PopupID  = 5
			break
		case "5":
			PopupUrl = url6
			PopupID  = 6
			break
		case "6":
			PopupUrl = url7
			PopupID  = 7
			break
		case "7":
			PopupUrl = url8
			PopupID  = 8
			break
		case "8":
			PopupUrl = url1
			PopupID  = 1
			break
		case "9":
			PopupUrl = url2
			PopupID  = 2
			break

		default:
			NoPopup="on"
			}
		}   	
	
	if (DateDifference < DaysBetweenPopups)
		{
		NoPopup="on"
		}
}

//if no cookie, prepare for first URL
else
  	 {
	PopupUrl = url1;
	PopupID  = 1;
     }


//------------ POPUP SECTION ------------\\
//Set Expiration Date
expires = new Date(); 
expires.setTime(expires.getTime() + 3E11);


//Popup URL if needed

if (NoPopup == "off")
	{
	//Popup URL
	window.open(PopupUrl,"FreeOffer","scrollbars=yes,resizeable=yes,height=490,width=600,directories=no,location=no,status=no,toolbar=no,menubar=yes,left=0,top=0,screenX=0,screenY=0)");

	//Record This Popup and This Visit In Cookies
	SetCookie("MYFLastExitPopDate",date_now,expires,"");
	SetCookie("MYFLastVisit",date_now,expires,"");
	SetCookie("MYFLastExitPopID",PopupID,expires,"");
	SetCookie("MYFDomain","myfree.com",expires,"");
	}
else
	 {
	//reset Date Last Visited Cookie
	SetCookie("MYFLastVisit",date_now,expires,"");
	}
}
//************END OF POPUP EXIT CODE****************

//**************************************************************
//COOKIE FUNCTIONS from http://www.hidaho.com/cookies/cookie.txt
//**************************************************************
//
// "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.
//110    [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" : "");
}

//  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";
  }
}

//******** COMPARE DATES ********
//Finds the difference between 2 month date pairs.
//Month1 and Day1 should be the later date
//month2 and day2 should be the earlier date
function CompareDates (month1, day1, month2, day2 ) {
	switch (month1 - month2){
		case 0:
			compare_value = day1 - day2
			break
		case 1:
			compare_value = day1 + (30-day2)
			break
		case -11:
			compare_value = day1 + (31-day2)
			break
		default:
			if (month2 > month1) 
				{
				compare_value = day1 + (30-day2) + (11-month2) + (30*month1)
				}
			else  
				{
				compare_value = day1 - day2 + (30*(month1-month2))
				}
		}
		return compare_value
	   }