﻿// determine if the user is eligable for the new Mercury beta opt-in.

var __validRecommendationTypes__ = "JKLMNO";
var __validRecommendationTypesFIT__ = "JKLMNO";

function MercuryShowOverlay() {
	if (hasMercuryDeclined() == true) {
		return;
	}
	if (noDisplayCode() == false) {
		return;
	}
	if (isValidPageName() == false) {
		return;
	}
	if (isBetaBrowserType() == false) {
		return;
	}
	if (isBetaRecommendationType() == false) {
		return;
	}
	
	//will append all the querystring parameters
	var mercuryUrl = "/reservations-core-ui/default.html";
	var currentUrl = window.location.href;
	var questionMarkPos = currentUrl.indexOf("?");
	var queryString = (questionMarkPos < 0) ? "" : currentUrl.substr(questionMarkPos);
	window.location = mercuryUrl + queryString;
}

function isBetaRecommendationType() {
	var recommendationType = getRecommendationType();
	var forcevalue = readQueryString('force');
	if (forcevalue != '') recommendationType = '';
	if (recommendationType == '') recommendationType = genRecommendationType(forcevalue); //cookie doesn't exist so we will create it

	var betaGroup = __validRecommendationTypes__;
	if (isLikelyTR()) {
		betaGroup = __validRecommendationTypes__	
	}
	else {
		betaGroup = __validRecommendationTypesFIT__	
	}
	
	if (betaGroup.indexOf(recommendationType) > -1) {
		return true;
	}
	return false;
}

function isBetaBrowserType() {
	var userAgent = window.navigator.userAgent.toLowerCase();
	var idx;
	
	// exclude the following browser types:
	//	IE 6 and prior; Safari; AOL; Firefox 2.x and prior; Opera
	idx = userAgent.indexOf("firefox/")
	if (idx > -1 && userAgent.length > idx + 9 && parseInt(userAgent.substr(idx + 8, 1)) < 3) {
		return false;
	}
	idx = userAgent.indexOf("msie ")
	if (idx > -1 && userAgent.length > idx + 6 && parseInt(userAgent.substr(idx + 5, 1)) < 7) {
		return false;
	}
	if (userAgent.indexOf(" aol ") > -1) {
		return false;
	}
	if (userAgent.indexOf("opera/") > -1) {
		return false;
	}
	if (userAgent.indexOf("safari") > -1 && userAgent.indexOf("chrome") == -1) {
		return false;
	}

	return true;
}

function isLikelyTR() {
	var accountId = readCookie("accountId");
	var TRMemberFullName = readCookie("TRMemberFullName");
	
	if (accountId != "" || TRMemberFullName != "") {
		return true;
	}
	return false;
}

function hasMercuryDeclined() {	
	var force = readQueryString("force");
	if (force != "") {
		return false;
	}
	
	var mercuryCookie = readCookie("mercury");
	if (mercuryCookie.toLowerCase() == "n" || mercuryCookie.toLowerCase() == "y" ) {
		return true;
	}
	return false;
}

function noDisplayCode() {
	//figure out if there is a displaycode
	var dcode = readQueryString("displayCode");
	if (dcode == "") {
		//try to get from a form
		var dcodeDomObj = document.getElementById("displayCode");
		if (dcodeDomObj) {
			dcode = dcodeDomObj.value;
		}
	}
	
	if (dcode == null || dcode == "") {
		return true;
	}
	return false;
}

function isValidPageName() {
	var loc = window.location.href.toLowerCase();
	var validPages = new Array();
	validPages[0] = "makereservation.do";
	validPages[1] = "testpage.htm";
	validPages[2] = "packagesearch.do";
	validPages[3] = "availabilitycalendar.do";
	
	for (var i = 0; i < validPages.length; i++) {
		if (loc.indexOf(validPages[i]) > -1) {
			return true;
		}
	}
	
	return false;
}

///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
///UTILITY TYPE FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function getRecommendationType()
{
	return readCookie('PZN_userRecommendationType');
}

function genRecommendationType(forcevalue)
{
	if(forcevalue!='')
	{
		writeCookie('PZN_userRecommendationType',forcevalue,120,'/','');
		return forcevalue;
	}
	
	var arr = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X');
	var rnd = Math.floor(Math.random()*arr.length);
	if(arr.length<rnd) rnd = 0; //SHOULD NEVER HAPPEN BUT JUST INCASE
	var val = arr[rnd];
	writeCookie('PZN_userRecommendationType',val,120,'/','');
	return val;
}
///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//BECAUSE SOME PAGES DONT HAVE UTILITY.JS 
		
function getValue(str, pattern, delim) {
	var q_start = str.toLowerCase().indexOf(pattern.toLowerCase());
	var value = '';
	if (q_start != -1) {
		q_start = q_start + pattern.length;
		var q_end = str.indexOf(delim, q_start);
		if (q_end == -1) q_end = str.length;
		value = str.substring(q_start, q_end);
	}
	return unescape(value);
}

function readQueryString(name) {
	var url = window.location.href;
	url = url.replace(/\?/, '&');
	url = url.replace(/#/, '&');

	var pattern = '&' + name + '=';
	return getValue(url, pattern, '&');
}

function readCookie(name) {
	var cookie = unescape(document.cookie);
	cookie = '; ' + cookie;

	var pattern = '; ' + name + '=';
	return getValue(cookie, pattern, ';');
}

function eraseCookie(name) {
	//writeCookie(name, '', -1);
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = name += "=; expires=" + cookie_date.toGMTString() + "; path=/";
}


function writeCookie(name, value, days, path, domain) {

	var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = 'expires=' + date.toGMTString() + ";"
	}
	path = 'path=' + path + ';';
	domain = ''; //domain=(!domain)?'':'domain='+domain+';'; //-- domain must have 2 dots in it
	document.cookie = name + '=' + escape(value) + ';' + expires + path + domain;
}

//END DUPLICATION
///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//
//LOAD WHEN THE PAGE LOADS
//
MercuryShowOverlay();

