// Class Questionnaire

Questionnaire = function () {
	this.username = "";
	this.company = "";
	this.lastVisited = "";
	this.groups = new Array();
}

new Questionnaire();

Questionnaire.prototype.deleteData = function() {
	expiry = new Date();
	expiry.setFullYear(expiry.getFullYear() - 1);
	document.cookie = "tracking=" + escape("none") + "; expires=" + expiry.toGMTString();
}

Questionnaire.prototype.existsData = function() {
	data = document.cookie;
	//alert(data)
	if (data.length == 0) {
		//alert(1);
		return false;
	} else if (unescape(data).indexOf("tracking=none") > -1) {
		//alert(2);
		return false;
	} else if (unescape(data).indexOf("tracking=") > -1) {
		//alert(3);
		return true;
	}
	else
	{
		//alert(4);
		return false;
	}
	//alert(5);
}

Questionnaire.prototype.readData = function() {
	data = unescape(document.cookie).replace("tracking=","").split("!!");
	for (var i = 0; i < data.length; i++) data[i] = data[i].split("}}");
	this.username = data[0][0];
	this.company = data[0][1];
	for (var i = 1; i < data.length - 1; i++) {
		for (x = 0; x < data[i].length; x += 3) {
			if (parseInt(data[i][x + 1]) == 1) {
				for (var z = 0; z < this.groups.length; z++) {
					if (this.groups[z].findQuestion(data[i][x])) {
						this.groups[z].getQuestion(data[i][x]).answer(parseInt(data[i][x + 2]) + 1);
						break;
					}
				}
			}
		}
	}
	return data[0][2];
}

Questionnaire.prototype.writeData = function() {
	this.deleteData();
	expiry = new Date();
	expiry.setFullYear(expiry.getFullYear() + 1);
	var data = this.username.replace("}}"," ").replace("!!"," ") + "}}" + this.company.replace("}}"," ").replace("!!"," ") + "}}" + this.lastVisited.toString().replace("#","") +  "!!";
	for (var i = 0; i < this.groups.length; i++) {
		for (var k = 0; k < this.groups[i].questions.length; k++) {
			data += this.groups[i].questions[k].ident + "}}";
			data += (this.groups[i].questions[k].answered) ? "1}}" : "0}}";
			data += this.groups[i].questions[k].choice.toString();
			data += (k == this.groups[i].questions.length - 1) ? "" : "}}";
		}
		data += "!!";
	}
	data = escape(data); 
	document.cookie = "tracking=" + data + "; expires=" + expiry.toGMTString();
}

Questionnaire.prototype.answer = function(where, choice) {
	for (var i = 0; i < this.groups.length; i++) {
		if (this.groups[i].findQuestion(where)) {
			this.groups[i].getQuestion(where).answer(choice);
			temp = mainFrame.document.location;
			this.lastVisited = temp.toString().replace("#","");
			break;
		}
	}
}

Questionnaire.prototype.clearAnswer = function(where) {
	for (var i = 0; i < this.groups.length; i++) {
		if (this.groups[i].findQuestion(where)) {
			this.groups[i].getQuestion(where).clearAnswer();
			this.lastVisited = temp.toString().replace("#","");
			break;
		}
	}
}

Questionnaire.prototype.getAnswer = function(where) {
	for (var i = 0; i < this.groups.length; i++) {
		if (this.groups[i].findQuestion(where)) {
			return this.groups[i].getQuestion(where).choice;
		}
	}
	return -1;
}

