﻿
//////////////////////////////////////////////////////////
// Exit Survey Constants
//////////////////////////////////////////////////////////

var EXIT_ALLOW_FLAG = 'hidExitSurveyEnabled';
var EXIT_COOKIE_KEY = 'XRC_surv_cookieEnabled';
var EXIT_SURVEY_URL = '/exitsurvey/ExitSurvey.html';
var EXIT_ZOOMERANG_URL = 'http://www.zoomerang.com/Survey/survey.zgi?p=WEB228Y77WDGKF';
var EXIT_SURVEY_DIV_ID = 'survey';
var EXIT_START_WIDTH = 50;
var EXIT_START_HEIGHT = 50;
var EXIT_END_WIDTH = 720;
var EXIT_END_HEIGHT = 600;


var monitorInterval;

//////////////////////////////////////////////////////////
// ExitSurvey
// The ExitSurvey object handles exit survey operations
//////////////////////////////////////////////////////////
var ExitSurvey = {

	//Checks to see if we have the field that allows us to use the exit survey
	//for a particular page. This is configurable via pages properties.
	//Also checks if the exit survey has been viewed. If both criteria are
	//met, the exit survey is shown and cookied.
	checkExitSurveyCriteria: function() {
		var hidAllowSurvey = document.getElementById(EXIT_ALLOW_FLAG);
		if (hidAllowSurvey) {
			if (hidAllowSurvey.value == '1') {
				var bCookied = ExitSurvey.isExitSurveyCookied();
				if (!bCookied) {
				
					//Cookie the exit survey so we don't show it again.
					ExitSurvey.cookieExitSurvey();
				
					//Show the survey options div.
					ExitSurvey.showSurveyDiv();
		
				}
			}
		}
	} 
	,

	isExitSurveyCookied: function() {
		if (Storage.readCookie(EXIT_COOKIE_KEY) == '1') {
			return true;
		}
		else {
			return false;
		}
	} 
	,
	
	cookieExitSurvey: function() {
		Storage.createCookie(EXIT_COOKIE_KEY,'1', null);
	} 
	,
	
	answerExitSurveyYes: function() {
		ExitSurvey.openSurveyWin();
		ExitSurvey.hideSurveyDiv();
		
	} 
	,

	answerExitSurveyNo: function() {
		ExitSurvey.hideSurveyDiv();
	} 
	,

	showSurveyDiv: function() {
		$('div#' + EXIT_SURVEY_DIV_ID + '').show();
	}
	, 

	hideSurveyDiv: function() {
		$('div#' + EXIT_SURVEY_DIV_ID + '').hide();
	}
	, 

	openSurveyWin: function() {
		window.open(EXIT_SURVEY_URL, 'exitSurvey', 'height='+EXIT_START_HEIGHT+',width='+EXIT_START_WIDTH+',scrollbars=yes');
	}
	,
	
	minimizeSurveyWin: function() {
		window.resizeTo(EXIT_START_WIDTH,EXIT_START_HEIGHT);
		window.blur();
		try {
			window.opener.focus();
		}
		catch (err) {
			//Catch the errors that result from having the survey ALREADY popped, then re-popping it.
			//No need to display a message, survey will repop under the opener.
			//alert('Sorry. There was a problem performing that operation.');
		}
	}
	,
	
	maximizeSurveyWin: function() {
		clearInterval(monitorInterval);
		window.resizeTo(EXIT_END_WIDTH,EXIT_END_HEIGHT);
		window.focus();
		window.location = EXIT_ZOOMERANG_URL;
	}
	,
	
	initMonitor: function() {
		monitorInterval = setInterval("ExitSurvey.monitorParent()", 100);
	}
	,
	
	monitorParent: function(){
		try {
			var domain = window.opener.document.domain;
		}
		catch(exception) {
			ExitSurvey.maximizeSurveyWin();
		}
	}


}; //ExitSurvey


//////////////////////////////////////////////////////////
// Dom Ready Events
//////////////////////////////////////////////////////////
$(document).ready(function() {
	ExitSurvey.checkExitSurveyCriteria();
});



