// This script defines the timely opening of a pop-up 
// window in the document only once per day for three 
// days and then once per week. The longevity of the 
// cookie can be changed by altering the global variable
// expireInDays to be however many days you want the 
// cookie to last. The URL of the popup window is contained
// in the global variable page and can be altered to any 
// URL you want. Also, the properties of the popup window
// can be customized through the pageProperties global
// variable. 



// GLOBAL VARIABLE CONFIGURATION

//URL of popup window with full letter and newsletter signup
var page = "http://iparenting.com/coreg/popup.htm"

//properties of popup window with full letter and newsletter signup
var pageProperties = "width=500,height=570,location=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes";

//number of days you want the cookie to be valid
var expireInDays = 1;	

//sets expiration as a date so many days from now as defined
//by expireInDays
var expiration = new Date();
expiration.setTime(expiration.getTime() + (expireInDays*24*60*60*1000));



// FUNCTION DEFINITIONS


// getCookieVal() looks at the cookie and determines 
// the index (relative to the start of the string) of 
// the ; beginning at the value offset (which must be 
// defined in the method call). This index is set to 
// the length of the cookie string if the string being 
// searched is empty. The function then returns encoded 
// string between the offset and the determined index.


function getCookieVal (offset) {

	var endstr = document.cookie.indexOf (";", offset);

	if (endstr == -1) {
		endstr = document.cookie.length;
	}

	return unescape(document.cookie.substring(offset, endstr));

}

// getCookie() begins by setting the global variable arg 
// to the defined parameter name with an equals sign. The 
// variable alen is equal to the length of the name variable 
// plus 1. The variable clen is the length of the cookie 
// string for the document. While a counting variable (i) 
// is less than the length of the cookie string, a second 
// counting variable (j) is set to alen plus the value of 
// i (Therefore, initially j = i). If the substring between 
// the characters at positions i and j in the cookie string
// are equal to the global variable arg, then the value of j 
// is passed to the getCookieVal() function and the encoded
// cookie string is returned. The variable i is then 
// incremented to be one higher than the index of the first 
// empty space after the position to which i is currently set. 
// The function ends and returns null if i becomes 0.
// This process of searching the cookie string for the arg
// string continues over the entire cookie string until a
// match is found. If a match is found, the value of the
// cookie is returned via the getCookieVal() function. 
// If no match is found then a null value is returned.


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

// setCookie() is used to place a cookie in the browser. 
// Required parameters are name and value. The function 
// unencodes the value parameter and uses the unencoded 
// version to designate the text to be stored as the 'name' 
// cookie. The function then checks to see if any of the 
// other parameters have been included, and if so, sets 
// them accordingly. If no other parameter is defined, 
// the parameter is set to the empty string.

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" : "");

}

// deleteCookie() deletes a cookie from the browser. If a 
// cookie with the given name exists for the given document, 
// the function sets the expiration date to 1 second into 
// 1970. 


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



// setsignedup() sets a cookie on the browser with an 
// infinite expiration. It creates a Date object called 
// noExpiration and sets the time of this variable 
// to 100000 days in the future.  It then checks to see 
// if the signedUp cookie already exists. If not,
// it sets this cookie with its infinite expiration. If 
// the cookie is already present, it increments the value 
// of the cookie and resets it with infinite expiration.



function setsignedup () {

	//set noExpiration to a date that is 100,000 days from now
	var noExpiration = new Date();
	var cookieDays = 100000;
	noExpiration.setTime(noExpiration.getTime() + (cookieDays*24*60*60*1000)); 
	
	//retrieve the values of the signedup and noWindow cookies
	var signedup = getCookie('signedup');
	var noWindow = getCookie('noWindow');


	//if the signedup cookie does not exist
	if (!signedup) {
	
		//and the noWindow cookie does exist
		if (noWindow) {
		   //delete the noWindow cookie
		   deleteCookie('noWindow');
		}
		
		signedup = 1;  
		
		//set the signedup cookie with a value of
		//1 and an expiration of 100,000 days from now
		setCookie('signedup',signedup,noExpiration);
	}
	
	//if the signedup cookie exists
	else {
	
	   //if the noWindow cookie exists
           if (noWindow) {
	   
	   //delete the noWindow cookie
	   deleteCookie('noWindow');
           }
           
           //increment the value of the signedup variable
           signedup++;
           
           //reset the signed up cookie with a value that
           //is one higher than its previous value with a new expiration of
           //100,000 days from now
           setCookie('signedup',signedup,noExpiration);
	}
}


// The windowOpener() function is the meat and potatoes of this script 
// so to speak. The function starts by searching for three different cookies: 
// the signedup, noWindow, and counter cookies. Then two date objects 
// are created to establish time points of one week and one year past 
// the expiration time of 24 hours past the current time established 
// earlier in the script. 

// Then a series of possible situations are examined. If the script 
// finds the signedup cookie present, then nothing should happen. 
// However, the setsignedup() function is called in order to use the 
// signedup cookie as a hit counter and reset the expiration such that 
// the cookie never expires then the script ends. If the signedup 
// cookie is not found but the noWindow cookie is found, nothing 
// happens and the script ends. If both the signedup and noWindow 
// cookies are not found, the script then the status of the counter 
// cookie is determined as one of three possible states, less than 4, greater 
// than or equal to 4 or null. If both noWindow and counter  
// are null, the the cookie has never been set or it has been more than a year
// since the user has been to the site. The noWindow cookie 
// is set with an expiration of 24 hours from the current time and 
// the counter cookie is set with an initial value of 1. Then a popup 
// window is opened from the main page. If the noWindow cookie is null 
// but the counter cookie exists and is less than or equal to 4, then the user 
// has visited the site before at least once, but the cookie that was set 
// when they last viewed the popup window has expired. When this is 
// the case, they need to view the popup window again. Thus the 
// noWindow and counter cookies are incremented and reset and the popup
// window is opened.  The same thing happens when the counter cookie 
// equals 4, except now they have viewed the window 5 times and the
// expiration of the noWindow cookie is set for a week past the 
// current time. If the noWindow is null but counter is greater than 4, 
// then the user has seen the popup window on five consecutive 
// visits to the site but the cookie set at their last visit has 
// expired (one week). Now they need only see the window every week. 
// Thus noWindow and counter are incremented again and reset, and again  
// noWindow is given an expiration of one week past the current 
// time and the popup window is opened again.

function windowOpener () {

	//retrieve the signedup, noWindow and counter cookies 
	var signedup = getCookie('signedup');  
	var noWindow = getCookie('noWindow');		
	var counter = getCookie('counter');
	
	//set weekPastExpiration as a date 6 days past the expiration
	//defined above
	var weekPastExpiration = new Date();
	weekPastExpiration.setTime(expiration + (6*24*60*60*1000));	
	
	//set yearPastExpiration as a date 365 days past the expiration
	//defined above
	var yearPastExpiration = new Date();
	yearPastExpiration.setTime(expiration + (364*24*60*60*1000));
	
	
	
	//if the signedup cookie exists
	if (signedup) {		
		//reset cookie for people who are already signed up
	    	setsignedup();	
	}
	
	
	//otherwise if the noWindow cookie exists
	else if (noWindow) {	
	    ;	//do nothing
	}
	
	
	// otherwise if the noWindow cookie does not exist
	else if (!noWindow)	
	
	   //and the counter cookie does not exist
	   //(page has not been viewed at all in the last year)
	   if (!counter) {	
	   	//set noWindow to 1
	        noWindow = 1;	
	        
	        //set the counter to 1
	        counter = 1;		
	        
	        //set the noWindow cookie which expires in 24 hours
	        setCookie('noWindow',noWindow,expiration);	
	        
	        //set the counter cookie which expires in 365 days from now
	        setCookie('counter',counter,yearPastExpiration);	
	        
	        //open a popup window with URL and properties 
	        //as described above 
	        window.open(page,"popupWindow",pageProperties);	  
	   }
	   
	   
	   //or counter does exist and equals 1 
	   //(initial cookie was set no less than 24 hours ago - first five times)
	   else if (counter < 4) {	
	   
	  	 //set noWindow to the value of the counter cookie
	        noWindow = counter;	
	        
	        //increment the counter variable
	        counter++;	
	        
	        //reset the noWindow cookie which expires
	        //in 24 hours
	        setCookie('noWindow',noWindow,expiration);	
	        
	        //reset the counter cookie which expires
	        //in 365 days from now
	        setCookie('counter',counter,yearPastExpiration);	
	        
	        //open a popup window with URL and properties
	        // as described above
	        window.open(page,"popupWindow",pageProperties);	
	   }
	   
	   
	   //or counter does exist and is greater than 4
	   //(cookie was reset no less than 24 hours ago for the 5th time)
	   else if (counter >= 4) {	
	   
	   	//set noWindow to the value of the counter cookie
	        noWindow = counter;	
	        
	        //increment the counter variable
	        counter++;			
	        
	        //reset the noWindow cookie which expires
	        //in 7 days
	        setCookie('noWindow',noWindow,weekPastExpiration);	
	        
	        //reset the counter cookie which expires
	        //in 365 days from now
	        setCookie('counter',counter,yearPastExpiration);	
	        
	        //open a popup window with URL and properties
	        //as described above
	        window.open(page,"popupWindow",pageProperties);	
	   }	   
}




// The function windowCloser() does exactly as it is titled, 
// it simply closes the window from which the function is called.


function windowCloser () {
	
	//close the window from which this function is called.
	self.close();
	
}

//End of script