/* = Set Initial Properties
 *******************************/

// Cookie Props
var expDays = 14; // number of days the cookie should last
// Cookie Date properties
var exp = new Date();
exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));

/*******************************************************************************
 * = Initialization Function
 ******************************************************************************/
function initSurvey(frequency, allowedDomains) {

	/***************************************************************************
	 * = Bind to "ready" for "pre" survey as user enters site
	 **************************************************************************/
	jQuery(document).bind("ready", function() {
		showMessageWindow(frequency);
	});
    
    var isAllowedDomain = function(href) {
		for ( var i = allowedDomains.length - 1; 0 <= i; i -= 1) {
			if (-1 < href.indexOf(allowedDomains[i])) {
				return true;
			}
		}
		return false;
	};

	/***************************************************************************
	 * = Create an message window
	 **************************************************************************/

	function messageWindow() {
		// Build container
		var newwindow = '<div id="messagebox"><h2>Thanks for visiting ' + dealername + '.</h2><p>You have been selected to participate in our consumer survey. Just, answer a few short questions <b>before and after</b> your visit to our site today.</p><p>We promise all responses are strictly confidential.</p><div class="buttons"><a href="#" id="opensurvey"><img src="survey/images/survey_continue.jpg" alt="Continue" /></a><a href="#" id="nothanks"><img src="survey/images/survey_nothanks.jpg" alt="No Thanks" /></a></div></div>';
		jQuery("body").append(newwindow);
		// Set Open button
		jQuery("#messagebox #opensurvey").bind("click", function() {
			// Hide the disclaimer
			jQuery("#messagebox").remove();
			// Open the survey
			buildSurvey();
		});
		// Set Close button
		jQuery("#messagebox #nothanks").bind("click", function() {
			// Go to the survey
			jQuery("#messagebox").remove();
		});
	}

	function buildSurvey() {
		window.open("survey/survey_pre.html?dealername="
				+ encodeURIComponent(dealername)
				+ "&siteid=" + encodeURIComponent(siteid)
				+ "&sessionid="
				+ encodeURIComponent(sessionid)
				+ "&visitorid="
				+ encodeURIComponent(visitorid)
				+ "&dealerdomain="
				+ encodeURIComponent(dealerdomain), "",
				"width=450, height=500, scrollbars");

	}

	function showMessageWindow(frequency) {
		// Check whether to open window
		if ((Math.random() * 100) < frequency) {
			// Check for cookie
			if (checkCount("survey")) {
				messageWindow(); // Create the message window
			}
		}
	}
}

/*******************************************************************************
 * = Cookie Functions
 ******************************************************************************/
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 SetCookie(name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape(value)
			+ ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
			+ ((path == null) ? "" : ("; path=" + path))
			+ ((domain == null) ? "" : ("; domain=" + domain))
			+ ((secure == true) ? "; secure" : "");
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function checkCount(type) {
	var count = GetCookie(type);
	if (count == null) {
		count = 1;
		SetCookie(type, count, exp);
		return true
	} else {
		return false;
	}
}

