/* Cookie controller for popup window
** Constraints: Cookie is set when popup fires, cookie contains timestamp for next cookie
**            : If this page is loaded before timestamp, popup doesn't fire
**            : Cookie value is set to "completed" when form is submitted, this stops any future popups

** This js file and dortch_cookies.js should be called on the page where the popup should appear
** and on the thankyou page for the form redirect

<script language="javascript" src="/scripts/dortch_cookies.js"></script>
<script language="javascript" src="/scripts/popup_survey.js"></script>

**/

// BEGIN USER VARIABLES //

// name of cookie
var cookie_name = "gallery";

// url to popup html page, can be absolute or relative
var popup_location_url = "/popup/gallery.html";

// domain that cookie will be available to
// this is to solve a www. subdomain problem encountered when using with a popup window
var domain = "floristsreview.com";

// popup width, height, resizable, scrollbars, etc.
var popup_window_properties = "width=350,height=200,resizable=yes,scrollbars=yes";

// popup will not execute after this date
var popup_end_date = "1/29/2014";

// set this AFTER popup_end_date
var cookie_expiration_date = "1/30/2014";

// hours until next popup occurs
var disable_popup_duration = 24;

// url for form completion
var completion_string = "_completion_is_disabled_";

// END USER VARIABLES //

msg = "";

function checkDate(){
// return true if date is before popup_end_date
	var killdate = new Date(popup_end_date);
	var today = new Date();
	return (today.getTime() < killdate.getTime())?true:false;
// function is working properly, no modification needed
}

function checkSubmit(){
// return true if coming from completion_string, otherwise false
	var teststring = completion_string;
	return (location.toString().indexOf(teststring) != -1)?true:false;
// function is working properly, no modifications needed
}

function checkCookie(form_submitted){
// if form_submitted is true, disable the popup
// check cookie and open popup if necessary

	var cookiename = cookie_name;
	var expiry = new Date(cookie_expiration_date);
	if(form_submitted){
msg += "setting cookie: " + document.cookie + "\nnew value: completed" + "\n\n";
		DeleteCookie(cookiename,"/");
		SetCookie(cookiename, "completed", expiry, "/", domain);
msg += "cookie set: " + document.cookie + "\n\n";
	}
	else{
		var cookieval = GetCookie(cookiename);
msg += "getting cookie: " + document.cookie + "\nretrieved data: " + cookieval + "\n\n";
		if(cookieval != "completed"){
			var today = new Date();
			if(cookieval != null){ // cookie exists, timecode available
				var nextpopup = new Date();
				nextpopup.setTime(cookieval);
				if(nextpopup.getTime() < today.getTime()){ // today is after next available popup
// set new nextpopup in cookie and open popup window
// this is set to 48 hours (2 days)
					nextpopup.setTime(today.getTime() + (disable_popup_duration * 60 * 60 * 1000));
					nextpopup.setHours(0);
					nextpopup.setMinutes(0);
					nextpopup.setSeconds(0);
msg += "setting cookie: " + document.cookie + "\nnew value: " + nextpopup.getTime() + "\n\n";
					SetCookie(cookiename, nextpopup.getTime(), expiry, "/", domain);
msg += "cookie set: " + document.cookie + "\n\n";
					openPopup();
				}
			}
			else{ // cookie doesn't exist yet, please open popup and set cookie
				var nextpopup = new Date();
				nextpopup.setTime(today.getTime() + (disable_popup_duration * 60 * 60 * 1000));
				nextpopup.setHours(0);
				nextpopup.setMinutes(0);
				nextpopup.setSeconds(0);
msg += "setting cookie: " + document.cookie + "\nnew value: " + nextpopup.getTime() + "\n\n";
				SetCookie(cookiename, nextpopup.getTime(), expiry, "/", domain);
msg += "cookie set: " + document.cookie + "\n\n";
				openPopup();
			}
		}
		else{
			;
msg += "popup is disabled" + "\n\n";
		}
	}
}

function openPopup(){
// opens the popup window pointing to the form
	var popup = window.open(popup_location_url,'popup',popup_window_properties);
	popup.focus();
}

// functionalized for use onload
function go_popup(){
	if(checkDate()){
		checkCookie(checkSubmit());
	}
}

