//////////////////////////////////
// Validation utility functions //
//////////////////////////////////

// The Back (1) or Next (2) buttons
var iSelectedButton = null;

function validate(form) {

	// Only validate if the Next button is selected
	if (iSelectedButton != 1) // iSelectedButton not set when Enter pressed in form
	{
		if (checkMandatoryFields(form)) {
			return (checkDateFields(form));
		}
		else {
			return false;
		}
	}
	return true;
}

// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
// Determines whether a text box has been populated for the selected item in a dropdown, if there is an associated text box
function checkDropDownText(form, questionName) {
	var retValue = false;
	
	if (isIncluded("div_" + questionName)) {
		var question = eval("form." + questionName);

		// Ensure they have selected an item
		if(question.value != "")
		{
			// Loop through, hiding or showing the associated text box, where appropriate
			if(VariableExists("a" + questionName.substring(1, questionName.length) + "_" + question.value + "_Text"))
			{
				// It the selected item has an associated text box, ensure it is populated
				if(eval("form." + questionName + "_" + question.value + "_Text.value") == "")
					retValue = false;
				else
					retValue = true;
			}
			else
				retValue = true;
		}
		else
			retValue = true;
	}
	else
		retValue = true;
	
	return retValue;
}

function checkRadioGroup(form, questionName, hasOther) {
	var retValue = false;
	
	if (isIncluded("div_" + questionName)) {

		var question = eval("form." + questionName);

		// 2010.03.04 MS ADDITION TO HANDLE SINGLE RESPONSE QUESTIONS
		var qLength;

		// If there's only one response, it's not an arrey		
		if(question.length != undefined)
			qLength = question.length;
		else
			qLength = 1;

		if(qLength != 1)
		{
			for (var i = 0; i < question.length; i++) {
				if (question[i].checked) {
					retValue = true;
					break;
				}
			}
		}
		else
			if(question.checked)
			{
				retValue = true;
			}

	
		if (hasOther) {
			if (eval("form." + questionName + "_other.value.length > 0")) {
				retValue = true;
			}
		}
	}
	else {
		retValue = true;
	}
	
	return retValue;
}

// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
// Determines whether a text box has been populated for a specific radio box
function checkRadioGroupText(form, questionName, answerIndex) {
	var retValue = false;
	
	if (isIncluded("div_" + questionName)) {
		var question = eval("form." + questionName);
	
		// SR 92 - 2004-05-29 AM - get correct index
		if (question[getRadioGroupAnswerIndex(form, questionName, answerIndex)].checked) {

			// It is checked, so ensure the text box has some value.
			if(eval("form." + questionName + "_" + answerIndex + "_Text.value") == "")
				retValue = false;
			else
				retValue = true;
		}
		else
			retValue = true;
	}
	else {
		retValue = true;
	}
	
	return retValue;
}

// SR 92 - 2004-05-29 AM - rotated/random answers mean radio buttons can no longer be accessed by array index
function getRadioGroupAnswerIndex(form, questionName, answerIndex) {
	var question = eval("form." + questionName);

	for (var i = 0; i < question.length; i++) {
		if (question[i].value == answerIndex) {
			return i;
		}
	}

	// Default to first
	return 0;
}

// MS - Added to cater for the division which is not specific to each row of the matrix
function checkRadioGroupForMatrix(form, questionName, hasOther, matrixQuestionName) {
	var retValue = false;
	
	if (isIncluded("div_" + matrixQuestionName)) {

		var question = eval("form." + questionName);
	
		for (var i = 0; i < question.length; i++) {
			if (question[i].checked) {
				retValue = true;
				break;
			}
		}
	
		if (hasOther) {
			if (eval("form." + questionName + "_other.value.length > 0")) {
				retValue = true;
			}
		}
	}
	else {
		retValue = true;
	}
	
	return retValue;
}

// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
// Determines whether a text box has been populated for a specific radio box
function checkRadioGroupTextForMatrix(form, questionName, questionIndex, answerIndex) {
	var retValue = false;
		
	if (isIncluded("div_" + questionName)) {
		var question = eval("form." + questionName + "_" + questionIndex);
	
		if (question[answerIndex - 1].checked) {

			// It is checked, so ensure the text box has some value.
			if(eval("form." + questionName + "_" + questionIndex + "_" + answerIndex + "_Text.value") == "")
				retValue = false;
			else
				retValue = true;
		}
		else
			retValue = true;
	}
	else {
		retValue = true;
	}
	
	return retValue;
}

function checkCheckBoxGroup(form, questionName, answerCount, hasOther) {
	var retValue = false;

	if (isIncluded("div_" + questionName)) {
		for (var i = 1; i <= answerCount; i++) {
			if (eval("form." + questionName + "_" + i + ".checked")) {
				retValue = true;
				break;
			}
		}
	
		if (hasOther) {
			if (eval("form." + questionName + "_other.value.length > 0")) {
				retValue = true;
			}
		}
	}
	else {
		retValue = true;
	}
	
	return retValue;
}

// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
// Determines whether a text box has been populated for a specific check box
function checkCheckBoxText(form, questionName, answerIndex) {
	var retValue = false;
	
	if (isIncluded("div_" + questionName)) {
		if (eval("form." + questionName + "_" + answerIndex + ".checked")) {
			// It is checked, so ensure the text box has some value.
			if(eval("form." + questionName + "_" + answerIndex + "_Text.value") == "")
				retValue = false;
			else
				retValue = true;
		}
		else
			retValue = true;
	}
	else {
		retValue = true;
	}
	
	return retValue;
}

function checkMatrix(form, questionName, verticalCount) {
	var retValue = true;
	
	for (var i = 1; i <= verticalCount; i++) {
		if (!checkRadioGroupForMatrix(form, questionName + "_" + i, false, questionName)) {
			retValue = false;
			break;
		}
	}
	
	return retValue;
}

function checkMultiMatrix(form, questionName, verticalCount, horizCount) {
	var retValue = true;

	for (var i = 1; i <= verticalCount; i++) {
		if (!checkCheckBoxGroupForMatrix(form, questionName + "_" + i, horizCount, questionName)) {
			retValue = false;
			break;
		}
	}
	
	return retValue;
}

function checkCheckBoxGroupForMatrix(form, questionName, horizCount, matrixQuestionName) {
	var retValue = false;
	
	if (isIncluded("div_" + matrixQuestionName) && document.getElementById("div_" + matrixQuestionName) != null && document.getElementById("div_" + matrixQuestionName).style.display != "none") {
		for (var i = 1; i <= horizCount; i++) {
			var question = eval("form." + questionName + "_" + i);
			if (question.checked) {
				retValue = true;
				break;
			}
		}
	}
	else {
		retValue = true;
	}
	
	return retValue;
}

// Determines whether a text box has been populated for a specific check box
function checkCheckBoxTextForMatrix(form, questionName, questionIndex, answerIndex) {
	var retValue = false;
		
	if (isIncluded("div_" + questionName) && document.getElementById("div_" + questionName) != null && document.getElementById("div_" + questionName).style.display != "none") {
		var question = eval("form." + questionName + "_" + questionIndex + "_" + answerIndex);
	
		if (question.checked) {
			// It is checked, so ensure the text box has some value.
			if(eval("form." + questionName + "_" + questionIndex + "_" + answerIndex + "_Text.value") == "")
				retValue = false;
			else
				retValue = true;
		}
		else
			retValue = true;
	}
	else {
		retValue = true;
	}
	
	return retValue;
}

function checkDate(form, questionName) {
	if (isIncluded("div_" + questionName)) {
		if (eval("form." + questionName + "_day.value.length > 0") &&
				eval("form." + questionName + "_month.value.length > 0") &&
				eval("form." + questionName + "_year.value.length > 0")) {
			return true;
		}
		else {
			return false;
		}
	}
	else {
		return true;
	}
}

function checkSingle(questionName) {
	if (isIncluded("div_" + questionName)) {
		var question = eval("document.survey." + questionName);
	
		if (question.value.length > 0) {
			return true;
		}
		else {
			return false;
		}
	}
	else {
		return true;
	}
}

function isNumeric(field) {
	var retValue = !isNaN(field.value);
	
	if (!retValue) {
		alert("Please enter a valid numeric value.");
		field.focus();
	}
		
	return retValue;
}

function checkValidDate(form, questionName) {
	var retValue = true;
	
	if (isIncluded("div_" + questionName)) {
		var day = eval("parseInt(form." + questionName + "_day.value)");
		var month = eval("parseInt(form." + questionName + "_month.value)");
		var year = eval("parseInt(form." + questionName + "_year.value)");

		if (day < 1 || month < 1 || month > 12 || year < 0) {
			retValue = false;
		}
		else {
			switch (month) {
				case 4:
				case 6:
				case 9:
				case 11:
					if (day > 30) {
						retValue = false;
					}
					break;
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					if (day > 31) {
						retValue = false;
					}
					break;
				case 2:
					if (year % 4 == 0) {
						if (day > 29) {
							retValue = false;
						}
					}
					else {
						if (day > 28) {
							retValue = false;
						}
					}
					break;
				default:
					break;
			}
		}
	}
	
	return retValue;
}


/////////////////////////////////////
// Prepopulation utility functions //
/////////////////////////////////////

function populateDropDown(question) {
	var dropdown = eval("document.survey.q_" + question);
	
	for (var i = 0; i < dropdown.length; i++) {
		if (dropdown.options[i].value == eval("a_" + question)) {
		
			dropdown.selectedIndex = i;
			
			// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
			// Determine if there is an associated text field, and prepopulate answers, if the item is selected
			if(VariableExists("a_" + question + "_" + i + "_Text"))
			{
				var textField = eval("document.survey.q_" + question + "_" + i + "_Text");
				textField.value = eval("a_" + question + "_" + i + "_Text");
			}
		}
	}
}

function populateRadioGroup(question) {
	var radioGroup = eval("document.survey.q_" + question);
	
	for (var i = 0; i < radioGroup.length; i++) {
		if (radioGroup[i].value == eval("a_" + question)) {
			radioGroup[i].checked = true;
			
			// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
			// Determine if there is an associated text field, and prepopulate answers, if the radio button is selected
			if(VariableExists("a_" + question + "_" + (i+1) + "_Text"))
			{
				var textField = eval("document.survey.q_" + question + "_" + (i+1) + "_Text");
				textField.value = eval("a_" + question + "_" + (i+1) + "_Text");
			}

		}
		
	}
}

function populateCheckBoxGroup(question, count) {
	for (var i = 1; i <= count; i++) {
		eval("document.survey.q_" + question + "_" + i + ".checked = a_" + question + "_" + i);
		
		// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
		// Determine if there is an associated text field, and prepopulate answers, if the checkbox is checked
		if(eval("a_" + question + "_" + i) == "on")
		{
			if(VariableExists("a_" + question + "_" + i + "_Text"))
			{
				var textField = eval("document.survey.q_" + question + "_" + i + "_Text");
				textField.value = eval("a_" + question + "_" + i + "_Text");
			}
		}
	}
}

function populateMatrix(question, count) {
	for (var i = 1; i <= count; i++) {
		populateRadioGroup(question + "_" + i);
	}
}

function populateMultiMatrix(question, rowCount, colCount) {
	for (var i = 1; i <= rowCount; i++) {
		populateCheckBoxGroup(question + "_" + i, colCount);
	}
}

function populateDate(question) {
	populateDropDown(question + "_day");
	populateDropDown(question + "_month");
	populateText(question + "_year");
}

function populateText(question) {
	var text = eval("document.survey.q_" + question);

	text.value = eval("a_" + question);
}

function populateRadioGroupOther(question) {
	var other = eval("document.survey.q_" + question + "_other");
	
	// Clear radio group if other field populated
	if (other.value.length > 0) {
// MS - 27.05.03 - When Other is entered, the answers should still remain
//		var radioGroup = eval("document.survey.q_" + question);
		
//		for (var i = 0; i < radioGroup.length; i++) {
//			radioGroup[i].checked = false;
//		}
	}
}

// Prepopulation default to none
var hasData = false;


////////////////////////////
// HTML utility functions //
////////////////////////////

function onLoadFunction(psEncryptedDocumentID) {

	// MS - 2006.09.05 - Check that they are coming from this website
	// This was put in place to ensure that if someone gives out the link to the html rather than the initialiser that
	// it takes them back to the initialiser
	// Note: This now expects the encrypted DocumentID to be passed in to the onLoadFunction
// 	checkReferrer(psEncryptedDocumentID);

	// Set ParticipationID hidden variable
	document.survey.ParticipationID.value = escape(getQueryStringVariable("ParticipationID"));

	// Prepopulate data
	prepopulate();
	
	// Refresh answers from page
	refreshAnswersFromPage();
	
	// Show/hide sections and questions for current page
	showHide();
	
	// MS - 2004.03.28 - Answer Collection Issue - Show the Back/Next/Submit buttons now that the page is fully loaded
	showMainButtons();
}

// MS - 2006.09.05 - Determines the referrer and then sends them to the initialiser if it isn't coming from our server
function checkReferrer(psEncryptedDocumentID)
{
	// MS NOTE: We can't get the referrer from the initialiser so they could get through to the first page if they clicked on a direct
	// link to it. However, if they clicked on a link from another website or a link that automatically redirects then we should
	// capture it and they should be sent back
	if(psEncryptedDocumentID != null && psEncryptedDocumentID != "" && document.referrer != "")
	{
		// Ensure that it's coming from the initialiser or one of the other html documents
		if(document.referrer.toLowerCase().indexOf("http://localhost/informevm/") != 0 && document.referrer.toLowerCase().indexOf("http://localhost/informevmstatic/") != 0)
		{
			alert("Sorry, the link you clicked on is not correct. You will now be redirected to the start of the survey.\n\nThe link was: " + document.referrer.toLowerCase());
			
			// It's not coming from InformEVM, so send them back to the initialiser
			location.href = "http://localhost/InformEVM/Presentation/Receiver/SurveyInitialiser.aspx?DocumentID=" + psEncryptedDocumentID;
		}
	}
}

// MS - 2004.03.28 - Answer Collection Issue - Show the Back/Next/Submit buttons now that the page is fully loaded
function showMainButtons()
{
	if(document.getElementById && document.getElementById("btnBackward") != null)
		node = document.getElementById("btnBackward").style.visibility = "visible";

	if(document.getElementById && document.getElementById("btnForward") != null)
		node = document.getElementById("btnForward").style.visibility = "visible";
}

function getQueryStringVariable(name) {
	// Get Query String
	var queryString = location.search.substring(1); 
	
	// Split query at the &
	var pairs = queryString.split("&");

	// Begin loop through the querystring
	for(var i = 0; i < pairs.length; i++) {
		
		// Look for "name=value"
		var pos = pairs[i].indexOf('=');
		
		// if not found, skip to next
		if (pos >= 0) {
			
			// Extract the variable name
			var varName = pairs[i].substring(0,pos);
			
			if (varName.toUpperCase() == name.toUpperCase()) {
				// Extract and return the variable value
				return varValue = unescape(pairs[i].substring(pos+1));
			}
		}
	}
	
	// Could not find variable in Query String
	return null;
}

function getPageVariable(name) {
	var varValue = getQueryStringVariable(name);
	if (varValue == null) {
		document.write("<INPUT TYPE='HIDDEN' ID='" + name + "' NAME='" + name + "' VALUE=''>");
	}
	else {
		document.write("<INPUT TYPE='HIDDEN' ID='" + name + "' NAME='" + name + "' VALUE='" + varValue + "'>");
	}
}

// Determine if a variable exists
function VariableExists(variableName)
{
	return eval("window." + variableName) != undefined;
}

// Show an element (or hide it)
function ShowElement(elementName, showElement)
{
	// MS - 2004.03.28 - Answer Collection Issue - Netscape was failing on these lines without the preliminary check
	if(document.getElementById && document.getElementById(elementName) != null)
	{
		if(showElement)
			document.getElementById(elementName).style.display = "";
		else
			document.getElementById(elementName).style.display = "none";
	}
}

/////////////////////////////////////////////////////////////
// Question/Section inclusion/exclusion  utility functions //
/////////////////////////////////////////////////////////////

function updateRadioGroup(questionName, answerName, value) {
	var question = eval("document.survey." + questionName);

	for (var i = 0; i < question.length; i++)
	{
		// Set the answer value	
		if (question[i].checked) {
			eval(answerName + " = \"" + value + "\"");
		}
		
		// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
		// If there is a textanswer variable, there is an associated text answer with the single answer
		// Show or hide the text box, depending on whether the answer is selected
		if(VariableExists(answerName + "_" + question[i].value + "_Text"))
		{
			ShowElement(questionName + "_" + question[i].value + "_Text", question[i].checked);
			
			// Set focus to it, as they would have just clicked on the radio button
			if(question[i].checked)
				eval("document.survey." + questionName + "_" + question[i].value + "_Text.focus()");
				// AM20090402: This was throwing a javascript error since not all textboxes have an id (they have a name and getElementById doesn't work for firefox)
				//document.getElementById(questionName + "_" + question[i].value + "_Text").focus();
		}
	}

	showHide();
}

// SR 31 - MS 2003.12.24 - Routing not working from drop-downs
function updateDropDown(questionName, answerName, value) {

	var iQuestionLength = eval("document.survey." + questionName + ".length");
	var	bSelected = false;
	
	// Update the underlying answer value
	eval(answerName + " = \"" + value + "\"");

	// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
	// If there is a textanswer variable, there is an associated text answer with the single answer
	// Show or hide the text box, depending on whether the answer is selected
	for (var i = 1; i < iQuestionLength; i++)
	{
		// Loop through, hiding or showing the associated text box, where appropriate
		if(VariableExists(answerName + "_" + (i) + "_Text"))
		{
			bSelected = value == i.toString();
			ShowElement(questionName + "_" + (i) + "_Text", bSelected);			
			
			// Set focus to it, as they would have just selected it from the list
			if(bSelected)
				document.getElementById(questionName + "_" + (i) + "_Text").focus();
		}
	}
	
	showHide();
}

function updateCheckBox(question, answerName) {
	var value = "";
	
	if (question.checked) {
		value = "on";
	}
	
	// Update answer
	eval(answerName + " = \"" + value + "\"");

	// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
	// If there is a textanswer variable, there is an associated text answer with the single answer
	// Show or hide the text box, depending on whether the answer is selected
	if(VariableExists(answerName + "_Text"))
	{
		ShowElement("q" + answerName.substring(1, answerName.length) + "_Text", question.checked);
		
		// Set focus to it, as they would have just clicked on the checkbox
		if(question.checked)	
			document.getElementById("q" + answerName.substring(1, answerName.length) + "_Text").focus();
	}
	
	showHide();
}

function showHide() {

	// Reset inclusion array
	resetArrInclusion();
	
	// Loop through inclusion rules
	for (var i = 0; i < arrInclusionRules.length; i++) {
		var inclusionRuleSet = arrInclusionRules[i];
		
		// loop through inclusion rules
		var divName = null;
		var divArray = null;
		var allRulesMet = true;
		var arrInclusionRule = inclusionRuleSet[0];
		
		for (var j = 0; j < arrInclusionRule.length; j++) {
			var inclusionRule = arrInclusionRule[j];
			var questionId = inclusionRule[0];
			var answerNbr = inclusionRule[1];
			var answer = inclusionRule[2];
			
			// Check only if question is included
			
			// Get completed answer from data javascript file
			var completedAnswer;

			if (answerNbr == null) {
				completedAnswer = eval("a_" + questionId);
			}
			else {
				completedAnswer = eval("a_" + questionId + "_" + answerNbr);
			}
			
			// Check if completed answer equals rule answer and is question included
			if (completedAnswer == answer && isIncluded("div_q_" + questionId)) {
				// Set div name
				divArray = inclusionRuleSet[1];
			}
			else {
				// Set allRulesMet and break out of for loop
				allRulesMet = false;
				break;
			}
		}
		
		// If both allRulesMet is true and divArray is not null (set only if completed answer equals answer)
		if (allRulesMet && divArray != null) {
			setDivVisible(divArray);
		}
	}

	// Loop through arrInclusion for items with current page
	var pageNbr = document.survey.PageNumber.value;
	
	for (i = 0; i < arrInclusion.length; i++) {
		// Check page number
		if (arrInclusion[i][1] == pageNbr) {
			// MS - 2004.07.02 - The next line was put in place as some div's are removed in custom-modified pages, yet inclusions may still try to hide them
			if(document.getElementById(arrInclusion[i][0]) != null)
			{
				if (arrInclusion[i][2]) {
					document.getElementById(arrInclusion[i][0]).style.display = "";
				}
				else {
					document.getElementById(arrInclusion[i][0]).style.display = "none";
				}
			}
		}
	}
}

function setDivVisible(divArray) {
	var pageNbr = document.survey.PageNumber.value;
	
	// Loop through arrInclusion
	for (var i = 0; i < arrInclusion.length; i++) {
		// Loop through divArray
		for (var j = 0; j < divArray.length; j++) {
			var divName = divArray[j];
			
			if (divName == arrInclusion[i][0]) {
				// Set answer and hidden to true
				arrInclusion[i][2] = true;

				// MS If the DocumentID is for one created before 27.07.03 then only work on the current page
				var documentID = document.survey.DocumentID.value
				if (documentID == "29" || documentID == "gi" || documentID == "ui" || documentID == "sk" || documentID == "mi" || documentID == "qi" || documentID == "ki" || documentID == "gk" || documentID == "oi" || documentID == "ii" || documentID == "og" || documentID == "im" || documentID == "si" || documentID == "km" || documentID == "gm" || documentID == "uk")
				{
					if (arrInclusion[i][1] == pageNbr)
						eval("document.survey." + divName + ".value = \"true\"");
				}
				else
				{
					eval("document.survey." + divName + ".value = \"true\"");
				}
// End TK
			}
		}
	}
}

function isIncluded(divName) {
	var questionIncluded = false;
	var sectionIncluded = false;
	
	for (var i = 0; i < arrInclusion.length; i++) {
		if (arrInclusion[i][0] == divName) {
			questionIncluded = arrInclusion[i][2];
			sectionIncluded = isSectionIncluded(arrInclusion[i][3]);
			break;
		}
	}

	return questionIncluded && sectionIncluded;
}

function isSectionIncluded(divName) {
	var retValue = false;
	
	for (var i = 0; i < arrInclusion.length; i++) {
		if (arrInclusion[i][0] == divName) {
			retValue = arrInclusion[i][2];
			break;
		}
	}

	return retValue;
}

function setPrevPage() {
	document.survey.NextPage.value = "0";
	var pageNbr = document.survey.PageNumber.value;

	for (var i = 0; i < arrInclusion.length; i++) {
		// If current page hit, break from loop
		if (arrInclusion[i][1] == pageNbr) {
			break;
		}
		
		// if question is included, set prev page
		if (arrInclusion[i][0].substring(0, 5) == "div_q" && arrInclusion[i][2] && isSectionIncluded(arrInclusion[i][3])) {
			eval("document.survey.PrevPage.value = \"" + arrInclusion[i][1] + "\"");
		}
	}
}

function setNextPage() {
	document.survey.NextPage.value = "0";
	var pageNbr = document.survey.PageNumber.value;

	for (var i = 0; i < arrInclusion.length; i++) {
		if (pageNbr < arrInclusion[i][1]) {
			// if question is included, set next page
			if (arrInclusion[i][0].substring(0, 5) == "div_q" && arrInclusion[i][2] && isSectionIncluded(arrInclusion[i][3])) {
				eval("document.survey.NextPage.value = \"" + arrInclusion[i][1] + "\"");
				break;
			}
		}
	}
}

function getRadioGroupValue(questionName) {
	var retValue = "";
	var question = eval("document.survey." + questionName);
	var answerName;
	
	for (var i = 0; i < question.length; i++) {
		if (question[i].checked) {
			retValue = question[i].value;
		}
		
		// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
		// If there is a textanswer variable, there is an associated text answer with the single answer
		// Show or hide the text box, depending on whether the answer is selected
		answerName = "a" + questionName.substring(1, questionName.length) + "_" + (i+1) + "_Text";

		if(VariableExists(answerName))
			ShowElement("q" + answerName.substring(1, answerName.length), question[i].checked);
	}
	
	return retValue;
}

function getDropDownValue(questionName) {
	var retValue = "";
	var question = eval("document.survey." + questionName);
	var answerName;
	
	retValue = question.value;
	
	for (var i = 1; i <= (question.length - 1); i++) {

		// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
		// If there is a textanswer variable, there is an associated text answer with the single answer
		// Show or hide the text box, depending on whether the answer is selected
		answerName = "a" + questionName.substring(1, questionName.length) + "_" + i + "_Text";

		if(VariableExists(answerName))
			ShowElement(questionName + "_" + i + "_Text", retValue == i.toString());
	}
	
	return retValue;
}

function getCheckBoxValue(questionName) {
	var retValue = "";
	var isChecked = eval("document.survey." + questionName + ".checked");
	var answerName = "a" + questionName.substring(1, questionName.length) + "_Text";

	if (isChecked)
		retValue = "on";

	// SR 16 - 2003-01-02 MS - Allow text boxes to be associated with all answers.
	// If there is a textanswer variable, there is an associated text answer with the single answer
	// Show or hide the text box, depending on whether the answer is selected
	if(VariableExists(answerName))
		ShowElement("q" + answerName.substring(1, answerName.length), isChecked);

	return retValue;
}