$(document).ready(function(){
	///////////////////////////////////////////////////////////////////////////////////////
	//
	// Exit code will display a pop-up window when exiting the website to an external link
	// or after viewing X pages on the website.  If you've viewed more then X pages and have
	// been shown the survey, you will not be surveyed again for 193 days.
	// 
	// Copyright © Intuit, Inc. All rights reserved.
	// Code Developed by Matthew Gisonno
	//
	///////////////////////////////////////////////////////////////////////////////////////
	
	// Check if cookies are enabled
	if (document.cookies == "") {
		// If cookies are not enabled, do nothing
	} else {
		// Variable to hold how many page views before asking the user
		var strPageViews = 10
		
		// Variable to hold the survey url
		var strSurveyAskPop = "/vocTools/vocSurvey.jsp?url=" + location.href
		
		// Variable to hold what we append to the body if the popup is blocked
		var strSurveyAskNonPop = "<div id=\"contSurveyPopup\"><iframe src=" + strSurveyAskPop + " width=\"500\" height=\"330\" frameborder=\"0\"></iframe></div>"
		
		// If the cookie is not present, create it.  This will track the page users page views
		if ($.cookie("AccountantSurvey") == null) {
			// Set the survey cookie
			$.cookie("AccountantSurvey", "0", {expires: 60, path: "/", domain: "intuit.com"});
		}
		// Variable to hold the cookie information
		var strSurveyCookie = $.cookie("AccountantSurvey")
		
		// If the Cookie value is less then or equal to 5 and they have not taken the survey before then
		if (strSurveyCookie <= strPageViews && strSurveyCookie != "true") {
			// Increase the page view counter by 1
			strSurveyCookie++
			// Reset cookie with updated page view count
			$.cookie("AccountantSurvey", strSurveyCookie, {expires: 60, path: "/", domain: "intuit.com"});
		}
		
		// Function to check for popup blocking
		function detectPopupBlocker() {
			// Generate a random number from 0 to 99
			var ran_number = Math.floor(Math.random()*99);
			// If the random number is greater then 79, they meet the 20% and we can survey them
			if (ran_number > 79) {
				// Variable to open the popup window
				var strSurveyTestWindow = window.open(null,"","width=100,height=100");
				// Try
				try {
						// If the window we created will close, popups are not blocked and we can survey them
						strSurveyTestWindow.close();
						window.open(strSurveyAskPop,"AccountantSurveyWindow","width=500,height=330");
						
				// If there is no window to close, popups are blocked and we move to plan B...  The CSS window...
				} catch (e) {
						// Pop-ups blocked, attach CSS window to the DOM.
						$("body").append(strSurveyAskNonPop);
						$("#contSurveyPopup").show(500);
						
				}
			}
		}
		
		// If the page views is 5 and they have not taken the survey before, then survey them
		if (strSurveyCookie == strPageViews && strSurveyCookie != "true") {
			// Open the survey
			detectPopupBlocker();
		}
		
		/*  We'll have to worry about this later...
		// Mousedown event for all links to catch site exits... This is experimental, but seems to work ;)
		$("a").mousedown(function(){
			var strSurveyURL = $(this).attr("href");
			// If the survey Cookie is empty or they have not taken the survey before, we can survey them
			if (strSurveyCookie == null || strSurveyCookie != "true") {
				// If the HREF attribute contains an HTTP://, then they are leaving the site and we should survey them
				var pageurl = location.href;
				if (pageurl.match(/.+\.intuit.com/ig)) {
					// If we find any kind of intuit.com domain, don't survey them
					return false;
				} else {
					detectPopupBlocker();
					if ($(this).attr("target")) {
						window.open($(this).attr("href"));
					} else {
						window.location.reload($(this).attr("href"));
					}
				}
			}
		});
		*/
	}
});