function refreshVisibilityOfQuestions() {
	var followupQuestions = $(".question[name]").get();
	if (followupQuestions) {
		for (var ii = 0; ii < followupQuestions.length; ii++) {
			if (followupShouldBeVisible(followupQuestions[ii])) {
				$(followupQuestions[ii]).slideDown(300);
			} else {
				$(followupQuestions[ii]).fadeOut(300);
			}
		}
	}
}

function followupShouldBeVisible(fup) {
	var shouldBeVisible = false;
	var followUpRulesDiv = $(fup).find(".followUpRules").get(0);
	if (followUpRulesDiv) {
		// rule-driven behavior
		var rulesArray = eval('(' + followUpRulesDiv.innerHTML + ')');
		var parentAnswerVal = findParentAnswerValue(fup);
		if (parentAnswerVal) {
			shouldBeVisible = applyFollowUpRules(parentAnswerVal, rulesArray);
		}
	} else {
		// default behavior
		var answerId = $(fup).attr("name");
		var selector = "*[answerId='" + answerId + "']";
		var answerElement = $(selector).get(0);
		var shouldBeVisible = false;
		if (answerElement) {
			if (answerElement.tagName.toLowerCase() == "input") {
				var checked = $(answerElement).attr("checked");
				if (checked) shouldBeVisible = true;
			} else if (answerElement.tagName.toLowerCase() == "option") {
				var selected = $(answerElement).attr("selected");
				if (selected) shouldBeVisible = true;
			}
		}
	}
	return shouldBeVisible;
}

function findParentAnswerValue(fup) {
	var parentQuestion = $(fup).parents(".question").get(0);
	if (!parentQuestion) return null;
	var temp = $(parentQuestion).find("*").filter(function (index) {
			var pq = $(this).parents(".question").get(0);
			return (pq == parentQuestion);
		}).filter("input[checked]").get(0);
	// TODO: this only works for likert -- must expose parent question type to add flexibility
	if (temp) return temp.value;
}

function applyFollowUpRules(answerVal, rulesArray) {
	if (!rulesArray) return false;
	answerVal = parseInt(answerVal);
	for (var ii = 0; ii < rulesArray.length; ii++) {
		var opt = rulesArray[ii].operator;
		var matchVal = parseInt(rulesArray[ii].matchValue);
		if (!opt || !matchVal) continue;
		if (opt == "eq" && answerVal == matchVal)  return true;
		if (opt == "lt" && answerVal < matchVal) return true;
		if (opt == "lte" && answerVal <= matchVal) return true;
		if (opt == "gt" && answerVal > matchVal) return true;
		if (opt == "gte" && answerVal >= matchVal) return true;
	}
	return false;
}

function initTableLayouts(contextNode) { 
	var tables = null;
	if (!contextNode) {
		tables = $(".answerTable").get();
	} else {
		tables = $(contextNode).find(".answerTable").get();
	}
	if (tables) {
		for (var ii = 0; ii < tables.length; ii++) {
			var parent = tables[ii].parentNode;
			if (parent.offsetWidth && parent.offsetWidth > 0) {
				$(tables[ii]).css("width", (parent.offsetWidth-10) + "px");
			}
		}
	}
}

/**
 * Submit form
 **/
function formSubmit(step) {
	var form = document.forms[0];
	form.step.value = step;
	form.submit();
}

function switchLanguage(selectElmt) {
	if (!selectElmt) return;
	var selectedOption = selectElmt.options[selectElmt.selectedIndex];
	if (selectedOption) {
		var form = document.forms[0];
		var temp = $(form).find("input[name='locale']").get(0);
		temp.value = selectedOption.value;
		temp = $(form).find("input[name='action']").get(0);
		if (temp.value == "submitPreview")
			temp.value = "startPreview";
		else
			temp.value = "start";
		temp = $(form).find("input[name='hack']").get(0);
		temp.value = "true";
		form.submit();
	}
}