function sniFDQuiz() {
	this._domain = '.frontdoor.com';
	this.aryCorrectAnswers = new Array();
	this._domain = '';
	this._bonusQHref = 'bonusq.htm';
	this._bHeader = new Array();
	this._bDesc = new Array();
}

sniFDQuiz.prototype.getIQCookie = function(c_name) {
	var c_start = document.cookie.indexOf(c_name + '=');
	if (c_start != -1) { 
		c_start = c_start + c_name.length + 1;
		var c_end = document.cookie.indexOf(';', c_start);
		if (c_end == -1) {
			c_end = document.cookie.length;
		}
	}
	return unescape(document.cookie.substring(c_start, c_end));
}

sniFDQuiz.prototype.setIQCookie = function(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime(today.getTime());
	if (expires) expires = expires * 1000 * 60 * 60 * 24;
	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name + '=' +escape( value ) + ((expires) ? ';expires=' + expires_date.toGMTString() : '') + ((path) ? ';path=' + path : '') + ((domain) ? ';domain=' + domain : '') + ((secure) ? ';secure' : '');
}

sniFDQuiz.prototype.resetCookies = function() {
	for (i=1; i<=10; i++) this.setIQCookie("guestAnswer" + i, "", 1, '/', this._domain, '');
}

sniFDQuiz.prototype.addBeratement = function(header, desc) {
	this._bHeader.push(header);
	this._bDesc.push(desc);
}

sniFDQuiz.prototype.getAnswers = function() {
	var isTied = this.getIQCookie('isTied');
	var cookieValue = '';
	if (eval(isTied)) {
		var mGuestAnswerCookies = new Array(
			this.getIQCookie('guestAnswer1'),
			this.getIQCookie('guestAnswer2'),
			this.getIQCookie('guestAnswer3'),
			this.getIQCookie('guestAnswer4'),
			this.getIQCookie('guestAnswer5'),
			this.getIQCookie('guestAnswer6'),
			this.getIQCookie('guestAnswer7'),
			this.getIQCookie('guestAnswer8'),
			this.getIQCookie('guestAnswer9'),
			this.getIQCookie('guestAnswer10'),
			this.getIQCookie('guestAnswer11')
		);
		for (i=0; i<11; i++) cookieValue += mGuestAnswerCookies[i] + ',';
	} else {
		var mGuestAnswerCookies = new Array(
			this.getIQCookie('guestAnswer1'),
			this.getIQCookie('guestAnswer2'),
			this.getIQCookie('guestAnswer3'),
			this.getIQCookie('guestAnswer4'),
			this.getIQCookie('guestAnswer5'),
			this.getIQCookie('guestAnswer6'),
			this.getIQCookie('guestAnswer7'),
			this.getIQCookie('guestAnswer8'),
			this.getIQCookie('guestAnswer9'),
			this.getIQCookie('guestAnswer10')
		);
		for (i=0; i<10; i++) cookieValue += mGuestAnswerCookies[i] + ',';
	}
	
	var paramStr = '';
	for (i=0; i<arguments.length; i++) {
		eval('var num' + arguments[i] + ' = this.getNumOccurrences(\'' + arguments[i] + '\', cookieValue);');
		paramStr += 'num' + arguments[i] + ', ';
	}
	eval('var theHighest = this.getMaxOfNumberSet(' + paramStr.substring(0, paramStr.length - 2) + ');');
	if (theHighest.length > 1) {
		_sniFDQuiz.setIQCookie('tiedAnswers', theHighest, 1, '/', _sniFDQuiz._domain, '');
		_sniFDQuiz.setIQCookie('isTied', 'true', 1, '/', _sniFDQuiz._domain, '');
		window.location.href = this._bonusQHref;
	}
	document.getElementById('berater').innerHTML = this._bHeader[parseInt(theHighest)];
	document.getElementById('beraterTag').innerHTML = this._bDesc[parseInt(theHighest)];
}

sniFDQuiz.prototype.fixProgressbar = function(elmnt, pageNum, w, h) {
	document.getElementById(elmnt).style.left = '-' + ((w / 2) - (pageNum * h)) + 'px';
}

sniFDQuiz.prototype.checkInvalidQuizEntry = function(pLandingURL) {
	var mGlobalPath = this.getIQCookie('globalPath');
	if (mGlobalPath == '') {
		if (mGlobalPath == 'undefined') {
			if (mGlobalPath == null) {
				alert('It appears you have entered this quiz from a page other than the start page, or your browser is not set to accept cookies. You will be redirected to the beginning of the quiz.');
				document.location.href = pLandingURL;
			}
		}
	}
}

sniFDQuiz.prototype.validateAnswers = function(mForm, pPageNum) {
	var chosen = false;
	for (i=0; i<mForm.radAnswer.length; i++) {
		if (mForm.radAnswer[i].checked) {
			this.setIQCookie('guestAnswer' + pPageNum, mForm.radAnswer[i].value, 1, '/', this._domain, '');
			chosen = true;
		}
	}
	if (!chosen) {
		alert('Please choose an answer to continue.');
		return false;
	}
}

sniFDQuiz.prototype.getMaxOfNumberSet = function() {
	var largest = arguments[0];
	var tmp = new Array();
	var mArrayCounter = 0;
	for (i=0; i<arguments.length; i++) {
		if (arguments[i] >= largest) {
			if (arguments[i] > largest) {tmp = new Array(); mArrayCounter = 0;}
			tmp[mArrayCounter] = i;
			largest = arguments[i];
			mArrayCounter++;
		}
	}
	return tmp;
}

sniFDQuiz.prototype.getNumOccurrences = function(pLookFor, pLookIn) {
	var numCount = 0;
	for (var i=0; i<pLookIn.length; i++) if (pLookIn.charAt(i) == pLookFor) numCount++;
	return parseInt(numCount);
}


sniFDQuiz.prototype.hintwindow = function(url) {
	var needhint = window.open(url, "hint", "width=800, height=600, resizable=1, scrollbars=1, status=1, toolbar=0, location=0, menubar=0");
	needhint.moveTo(0, 0);
}

sniFDQuiz.prototype.LandingCheckCookie = function() {
	if (BrowserDetect.browser != 'Safari') {
		// do the cookie test/check
		FLQuizID = 'FLIQV3';
		this.setIQCookie('FLQuizID', FLQuizID, 365, '/', this._domain, '');
		cookieCheck = this.getIQCookie('FLQuizID');
		if (cookieCheck!='FLIQV3') alert('There was a problem with setting a cookie on your computer and this quiz requires them to function.\n\n Please enable cookies in the settings of your browser.');
	}
}

sniFDQuiz.prototype.getNumCorrect = function() {
	// get a comparison match of the cookied answers against
	// aryCorrectAnswers[] and see how many are correct
	var numCorrect = 0;
	var mGuestAnswerCookies = new Array(
		this.getIQCookie("guestAnswer1"),
		this.getIQCookie("guestAnswer2"),
		this.getIQCookie("guestAnswer3"),
		this.getIQCookie("guestAnswer4"),
		this.getIQCookie("guestAnswer5"),
		this.getIQCookie("guestAnswer6"),
		this.getIQCookie("guestAnswer7"),
		this.getIQCookie("guestAnswer8"),
		this.getIQCookie("guestAnswer9"),
		this.getIQCookie("guestAnswer10")
	);
	for (i=0; i<10; i++) {
		var cookieValue = mGuestAnswerCookies[i];
		if (cookieValue == this.aryCorrectAnswers[i]) {
			numCorrect++;
		}
	}
	
	if (numCorrect < 3) {
		document.getElementById("berater").innerHTML = this._bHeader[0];
		document.getElementById("beraterTag").innerHTML = this._bDesc[0];	
	} else if (numCorrect > 2 && numCorrect < 6) {
		document.getElementById("berater").innerHTML = this._bHeader[1];
		document.getElementById("beraterTag").innerHTML = this._bDesc[1];
	} else if (numCorrect > 5 && numCorrect < 9) {
		document.getElementById("berater").innerHTML = this._bHeader[2];
		document.getElementById("beraterTag").innerHTML = this._bDesc[2];
	} else {
		document.getElementById("berater").innerHTML = this._bHeader[3];
		document.getElementById("beraterTag").innerHTML = this._bDesc[3];
	}
	document.getElementById("numAnsweredCorrectly").innerHTML = numCorrect;
}

sniFDQuiz.prototype.resetCookies = function() {
	for (i=1; i<=10; i++) this.setIQCookie("guestAnswer" + i, "", 1, '/', this._domain, '');
}

/* Cookie enablement check */
sniFDQuiz.prototype.LandingCheckCookie = function() {
	if (BrowserDetect.browser!="Safari") {
		// do the cookie test/check
		FLQuizID = "FLIQV3";
		this.setIQCookie('FLQuizID', FLQuizID, 365, '/', this._domain, '');
		cookieCheck = this.getIQCookie('FLQuizID'); // <-- problem here
		if (cookieCheck!="FLIQV3") {
			alert("There was a problem with setting a cookie on your computer and this quiz requires them to function.\n\n Please enable cookies in the settings of your browser.");
		}
	}
}


sniFDQuiz.prototype.setCorrectAnswers = function() {
	for (var i=0; i<arguments.length; i++) this.aryCorrectAnswers.push(arguments[i]);
}


/* Browser detect */
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || 'An unknown browser';
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| 'an unknown version';
		this.OS = this.searchString(this.dataOS) || 'an unknown OS';
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: 'OmniWeb',
			versionSearch: 'OmniWeb/',
			identity: 'OmniWeb'
		},
		{
			string: navigator.vendor,
			subString: 'Apple',
			identity: 'Safari'
		},
		{
			prop: window.opera,
			identity: 'Opera'
		},
		{
			string: navigator.vendor,
			subString: 'iCab',
			identity: 'iCab'
		},
		{
			string: navigator.vendor,
			subString: 'KDE',
			identity: 'Konqueror'
		},
		{
			string: navigator.userAgent,
			subString: 'Firefox',
			identity: 'Firefox'
		},
		{
			string: navigator.vendor,
			subString: 'Camino',
			identity: 'Camino'
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: 'Netscape',
			identity: 'Netscape'
		},
		{
			string: navigator.userAgent,
			subString: 'MSIE',
			identity: 'Explorer',
			versionSearch: 'MSIE'
		},
		{
			string: navigator.userAgent,
			subString: 'Gecko',
			identity: 'Mozilla',
			versionSearch: 'rv'
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: 'Mozilla',
			identity: 'Netscape',
			versionSearch: 'Mozilla'
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: 'Win',
			identity: 'Windows'
		},
		{
			string: navigator.platform,
			subString: 'Mac',
			identity: 'Mac'
		},
		{
			string: navigator.platform,
			subString: 'Linux',
			identity: 'Linux'
		}
	]

};
BrowserDetect.init();