var objExpItem, objExpDiv, objExpSpan

function getChildren(objItem) {

	try {

		// Submit button if this is where we came from
		if (objItem.getAttribute("type") == "submit")
			hide(objItem.getAttribute("parentElement"));

		var strParentQuestion = objItem.getAttribute("Question")
		var nodChildDiv = elementById("QChilds_" + strParentQuestion);
		
		// Only get children if we haven't already got them
		if (nodChildDiv.innerHTML == "") {

			objExpSpan = document.createElement("<SPAN Class='GetQuestions'>");
			objExpSpan.innerHTML = "Getting questions";
			nodChildDiv.appendChild(objExpSpan);
			
			objExpItem = objItem;
			objExpDiv = nodChildDiv;
			
			setTimeout("getChildrenWorker()", 10);
		}

		return false;
	}
	catch(e) {
		logErrAndObj("getChildren", objItem, e)
		return false;
	}
}
		
function getChildrenWorker() {
		
	try {
		var strParentQuestion = objExpItem.Question;

		var strChildren = GetQuestionChildrenValuesFromServer(getValue(elementById("SurveyCode")), strParentQuestion);
		
		if (strChildren == -2)
			alert("Your session has timed out. Refresh the page to continue.")
		else {
			var nodChildDiv = elementById("QChilds_" + strParentQuestion);
			
			var strQuestions = ""
			
			var arrChildren = strChildren.split("\f");
			
			strLastFormatType = "0";
			
			// The remote function returns an extra form feed
			for (var i=0; i< arrChildren.length - 1; i++) {
			
				strQuestions += appendNewQuestion(nodChildDiv, arrChildren[i]);
				
				if (i< arrChildren.length - 2)
					strQuestions += ",";
				
			}
			
			elementById("QR_" + strParentQuestion).ChildQuestions = strQuestions;
		}
		
		hide(objExpSpan);
		
		return false;
	}
	catch(e) {
		logErr("getChildrenWorker", e)
		return false;
	}
}

var strlastOptionList, strLastOptionList2, strLastFormatType
var strLastOptionTitle, strLastOptionTitle2
var blnPreceedingChildren
var objTable

function appendNewQuestion(objDiv, strQuestion) {

	try {
		var arrItems = strQuestion.split("\t");
		
		if (arrItems.length != 17)
			throw ("Number of question items " + arrItems.length + " != 17")
		else {
			var objQuestionRow;
			
			switch (arrItems[3]) {
		
				case "1": // List
				case "9": // Multilist
				case "2": // Text
				case "10": // Textarea
				
					// create table
					objTable = document.createElement("<TABLE>");
					objTable.id = "QR_" + arrItems[0];
					objTable.Question= arrItems[0];
					objTable.ParentQuestion = arrItems[5];
					objTable.ValidateRule = arrItems[5];
					objTable.IndentRule = arrItems[7];
					objTable.IndentRuleList = arrItems[16];
					AppendQuestionToRuleTable(arrItems[0]);
					objQuestionRow = objTable;
					objTable.className = "DoSurveySingleQuestion";
					
					// question row
					var objRow = objTable.insertRow();
					objDiv.appendChild(objTable);
	
					var objCell = objRow.insertCell();
					objCell.className = "QText";
					objCell.title = arrItems[2];
					objCell.innerHTML = arrItems[1];
					
					var objDescDiv = document.createElement("<div>");
					objDescDiv.className = "QDescription";
					objDescDiv.innerHTML = arrItems[2];
					objCell.appendChild(objDescDiv)
	
					var objCell = objRow.insertCell();
					objCell.className = "QSingleResponse";
	
					switch (arrItems[3]) {
				
						case "1": // List
						case "9": // Multilist
						
							var strMulti = (arrItems[3] == "9") ? "Multiple='True' Class='QMultilist'" : "Class='QSelect'"
							var strSelect = "<SELECT id='QN" + arrItems[0] + "' name='QN" + arrItems[0] + "' " + strMulti + "'>"
							var objSelect = document.createElement(strSelect);
							
							var objOption = document.createElement("<OPTION value='0'>")
							objOption.innerHTML = "- - Select - -"
							objSelect.appendChild(objOption);
							
							if (arrItems[14] == "-1") {
								arrOptions = strlastOptionList.split("\n");
							}
							else {
								arrOptions = arrItems[14].split("\n");
								strlastOptionList = arrItems[14];
							}
	
							for (i=0; i< arrOptions.length - 2; i+=2) {
								var objOption = document.createElement("<OPTION value='" + arrOptions[i] + "'>")
								objOption.innerHTML = arrOptions[i+1]
								objSelect.appendChild(objOption);
							}
							objCell.appendChild(objSelect)
							
							break;
						
						case "2": // Text
					
							var objNode = document.createElement("<INPUT name='QT" + arrItems[0] + "'" +
																	" id='" + arrItems[0] + "'" +
																	" Class='QText'" +
																	" size='" + arrItems[2] + "'" +
																	" Maxlength='4000'>");
							objCell.appendChild(objNode)
							break;
							
						case "10": // Textarea
	
							var objNode = document.createElement("<TEXTAREA name='QT" + arrItems[0] + "'" +
																	" id='" + arrItems[0] + "'" +
																	" Class='QTextarea'" +
																	" Cols='" + arrItems[2] + "'" +
																	" Rows='" + arrItems[3] + ">");
							objNode.onclick = function(){maximiseLength(this,4000)};
							objNode.onpaste = function(){maximiseLength(this,4000)};
							objCell.appendChild(objNode)
							break;
					}
					break;
		
				case "3": // Radio
				case "4": // Checkbox
				
					var arrOptions, blnSameOptionList
					if (arrItems[14] == "-1") {
						arrOptions = strlastOptionList.split("\n");
						blnSameOptionList = true;
					}
					else {
						arrOptions = arrItems[14].split("\n");
						strlastOptionList = arrItems[14];
						blnSameOptionList = false;
					}
						
					if (!blnSameOptionList || 
						blnPreceedingChildren ||
						arrItems[3] != strLastFormatType) {
						
						// create table
						objTable = document.createElement("<TABLE>");
						objTable.className = "DoSurveyMultiQuestion";
						
						// header row
						var objRow = objTable.insertRow();
						
						var objCell = objRow.insertCell();
						objCell.className = "QHeadText";
						objCell.innerHTML = "&nbsp;"
		
						var i;
		
						for (i=0; i< arrOptions.length - 2 ; i += 2) {
						
							var objCell = objRow.insertCell();
							objCell.className = "QHead";
							objCell.innerHTML = arrOptions[i+1];
							objCell.width = 75 / ((arrOptions.length - 1)/2) + "%"
						}
		
						objDiv.appendChild(objTable);
	
					}
					
					if (arrItems[16] == "")
						blnSameQuestionType = true
					else
						blnSameQuestionType = false;
					
					// question row
					objRow = objTable.insertRow();
					objRow.id = "QR_" + arrItems[0];
					objRow.Question= arrItems[0];
					objRow.FormatType = arrItems[3];
					objRow.ParentQuestion = arrItems[5];
					objRow.ValidateRule = arrItems[6];
					objRow.IndentRule = arrItems[7];
					objRow.IndentRuleList = arrItems[16];
					AppendQuestionToRuleTable(arrItems[0]);
					objQuestionRow = objRow;
	
					var objCell = objRow.insertCell();
					objCell.className = "QText";
					objCell.title = arrItems[2];
					objCell.innerHTML = arrItems[1];
					
					var objDescDiv = document.createElement("<div>");
					objDescDiv.className = "QDescription";
					objDescDiv.innerHTML = arrItems[2];
					objCell.appendChild(objDescDiv)
	
					var i;
	
					for (i=0; i< arrOptions.length - 2; i+=2) {
					
						var objCell = objRow.insertCell();
						objCell.className = "QResponse";
						
						var objNode = document.createElement("<INPUT name='QN" + arrItems[0] + "'>");
						if (arrItems[3] == "3") {
							objNode.type = "Radio"
							objNode.className = "QRadio";
							objNode.onclick = function(){radC(this)};
						}
						else {
							objNode.type = "checkbox";
							objNode.className = "QCheckbox";
						}
						objNode.id = "QN" + arrItems[0] + "_" + arrOptions[i];
						objNode.value = arrOptions[i];
						objNode.title = arrOptions[i+1];
						objNode.Question = arrItems[0];
						objCell.appendChild(objNode)
					}
	
					break;
		
				case "5": // DualRadio
				case "6": // DualCheckbox
	
					var arrOptions, arrOptions2
					if (arrItems[14] == "-1") {
						arrOptions = strlastOptionList.split("\n");
					}
					else {
						arrOptions = arrItems[14].split("\n");
						strlastOptionList = arrItems[14];
					}
						
					if (arrItems[15] == "-1") {
						arrOptions2 = strlastOptionList2.split("\n");
					}
					else {
						arrOptions2 = arrItems[15].split("\n");
						strlastOptionList2 = arrItems[15];
					}
					
					if (arrItems[14] != "-1" ||
						arrItems[15] != "-1" ||
						arrItems[8] != strLastOptionTitle ||
						arrItems[9] != strLastOptionTitle2) {
						blnSameOptionList = false;
					}
					else
						blnSameOptionList = true;
						
					if (!blnSameOptionList || 
							blnPreceedingChildren ||
							arrItems[3] != strLastFormatType) {
						
						strLastOptionTitle = arrItems[8];
						strLastOptionTitle2 = arrItems[9];
	
						// create table
						objTable = document.createElement("<TABLE>");
						objTable.className = "DoSurveyMultiQuestion";
						
						// title row
						var objRow = objTable.insertRow();
						
						var objCell = objRow.insertCell();
						objCell.className = "QHeadText";
						objCell.innerHTML = "&nbsp;"
						objCell.rowSpan = 2;
	
						objCell = objRow.insertCell();
						objCell.className = "QHead";
						objCell.colSpan = (arrOptions.length - 1) / 2;
						objCell.innerHTML = arrItems[8];
	
						objCell = objRow.insertCell();
						objCell.innerHTML = "&nbsp;";
						objCell.rowSpan = 2;
	
						objCell = objRow.insertCell();
						objCell.className = "QHead";
						objCell.colSpan = (arrOptions2.length - 1) / 2;
						objCell.innerHTML = arrItems[9];
	
						// header row
						objRow = objTable.insertRow();
						
						var i;
						for (i=0; i< arrOptions.length - 2 ; i += 2) {
						
							var objCell = objRow.insertCell();
							objCell.className = "QHead";
							objCell.innerHTML = arrOptions[i+1];
							objCell.width = 75 / ((arrOptions.length + arrOptions2.length - 2)/2) + "%"
						}
		
						for (i=0; i< arrOptions2.length - 2 ; i += 2) {
						
							var objCell = objRow.insertCell();
							objCell.className = "QHead";
							objCell.innerHTML = arrOptions2[i+1];
							objCell.width = 75 / ((arrOptions.length + arrOptions2.length - 2)/2) + "%"
						}
						objDiv.appendChild(objTable);
	
					}
					
					// question row
					objRow = objTable.insertRow();
					objRow.id = "QR_" + arrItems[0];
					objRow.Question= arrItems[0];
					objRow.FormatType = arrItems[3];
					objRow.ParentQuestion = arrItems[5];
					objRow.ValidateRule = arrItems[6];
					objRow.IndentRule = arrItems[7];
					objRow.IndentRuleList = arrItems[16];
					AppendQuestionToRuleTable(arrItems[0]);
					objQuestionRow = objRow;
	
					var objCell = objRow.insertCell();
					objCell.className = "QText";
					objCell.title = arrItems[2];
					objCell.innerHTML = arrItems[1];
					
					var objDescDiv = document.createElement("<div>");
					objDescDiv.className = "QDescription";
					objDescDiv.innerHTML = arrItems[2];
					objCell.appendChild(objDescDiv)
	
					var i;
					for (i=0; i< arrOptions.length - 2; i+=2) {
					
						var objCell = objRow.insertCell();
						objCell.className = "QResponse";
						
						var objNode = document.createElement("<INPUT name='QN" + arrItems[0] + "'>");
						if (arrItems[3] == "5") {
							objNode.type = "Radio"
							objNode.className = "QRadio";
							objNode.onclick = function(){radC(this);}
						}
						else {
							objNode.type = "checkbox";
							objNode.className = "QCheckbox";
							objNode.onclick = function(){radC(this);}
						}
						objNode.id = "QN" + arrItems[0] + "_" + arrOptions[i];
						objNode.Question = arrItems[0];
						objNode.value = arrOptions[i];
						objNode.title = arrOptions[i+1];
						objCell.appendChild(objNode)
					}
	
					objCell = objRow.insertCell();
					objCell.innerHTML = "&nbsp;";
	
					for (i=0; i< arrOptions2.length - 2; i+=2) {
					
						var objCell = objRow.insertCell();
						objCell.className = "QResponse";
						
						var objNode = document.createElement("<INPUT name='QNI" + arrItems[0] + "'>");
						if (arrItems[3] == "5") {
							objNode.type = "Radio"
							objNode.className = "QRadio";
							objNode.onclick = function(){radC(this);}
						}
						else {
							objNode.type = "checkbox";
							objNode.className = "QCheckbox";
							objNode.onclick = function(){radC(this);}
						}
						objNode.id = "QNI" + arrItems[0] + "_" + arrOptions2[i];
						objNode.Question = arrItems[0];
						objNode.value = arrOptions2[i];
						objNode.title = arrOptions2[i+1];
						objCell.appendChild(objNode)
					}
					break;
		
				case "8": // Heading
				
					var objHeadDiv = document.createElement("<DIV>");
					objHeadDiv.id = "QR_" + arrItems[0];
					objHeadDiv.Question = arrItems[0];
					objHeadDiv.ParentQuestion = arrItems[5];
					objHeadDiv.ValidateRule = arrItems[6];
					objHeadDiv.IndentRule = arrItems[7];
					objHeadDiv.IndentRuleList = arrItems[16];
					AppendQuestionToRuleTable(arrItems[0]);
					objDiv.appendChild(objHeadDiv)
					objQuestionRow = objHeadDiv;
		
					if (arrItems[4] != "0" && arrItems[4] != "") {
		
						// Head tag
						var objHead = document.createElement("<H" + arrItems[4] + ">");
						objHead.innerHTML = arrItems[1];
						objHead.title = arrItems[2];
						objHead.className = "Survey"
						objHeadDiv.appendChild(objHead)
		
						// Get Children button and callout
						if (arrItems[5] == "1") {
							var objSpan = document.createElement("<SPAN>");
							objHeadDiv.appendChild(objSpan);
							var objSubmit = document.createElement("<INPUT name='QM_" + arrItems[0] + "'>");
							objSubmit.type = "SUBMIT";
							objSubmit.id = "QM_" + arrItems[0];
							objSubmit.Question = arrItems[0];
							objSubmit.title = arrItems[2];
							objSubmit.className = "button";
							objSubmit.value = "More";
							objSubmit.title = "Select to show more questions.";
							objSubmit.onclick = function(){return(getChildren(this))};
							objSpan.appendChild(objSubmit);
		
							var objCallout = document.createElement("<img>");
							objCallout.src = "_image/InfoUp.gif";
							objCallout.downSrc="_image/InfoDown.gif";
							objCallout.onmouseover="infoIn(this)";
							objCallout.onmouseout="infoOut(this)";
							objCallout.className ="calloutIcon";
							objCallout.callout="Select to show more questions in this category.";
							objSpan.appendChild(objCallout);
						}
						
					}
					else { // Heading as a paragraph
						var objHead = document.createElement("<P>");
						objQuestionRow = objHead;
						objHead.innerHTML = arrItems[1];
						objHead.title = arrItems[2];
						objHead.className = "QuestionHead"
						objHeadDiv.appendChild(objHead)
					}
					
					// Description
					
					var objHeadDescription = document.createElement("<P>");
					objHeadDescription.innerHTML = arrItems[2];
					objHeadDescription.className = "H" + arrItems[4] + "SurveyDesc"
					objHeadDiv.appendChild(objHeadDescription)
		
					break;
		
				case "7": // Comment
				
					var objHeadDiv = document.createElement("<DIV>");
					objHeadDiv.id = "QR_" + arrItems[0];
					objHeadDiv.Question= arrItems[0];
					objHeadDiv.ParentQuestion = arrItems[5];
					objHeadDiv.ValidateRule = arrItems[6];
					objHeadDiv.IndentRule = arrItems[7];
					objHeadDiv.IndentRuleList = arrItems[16];
					AppendQuestionToRuleTable(arrItems[0]);
					objDiv.appendChild(objHeadDiv)
					objQuestionRow = objHeadDiv;
	
					var objComment = document.createElement("<P>");
					objComment.innerHTML = arrItems[1];
					objComment.title = arrItems[2];
					objComment.className = "QuestionComment"
					objHeadDiv.appendChild(objComment)
					
					// Description
					var objCommentDescription = document.createElement("<P>");
					objCommentDescription.innerHTML = arrItems[2];
					objCommentDescription.className = "QuestionCommentDesc";
					objHeadDiv.appendChild(objCommentDescription);
					
					break;
		
				default:
					var objNode = document.createElement("<p>");
					objNode.innerHTML = "Type:" + arrItems[3] + "; " + strQuestion;
					objNode.id = "QR_" + arrItems[0];
					objNode.Question= arrItems[0];
					objNode.ParentQuestion = arrItems[5];
					objNode.ValidateRule = arrItems[6];
					objNode.IndentRule = arrItems[7];
					objNode.IndentRuleList = arrItems[16];
					AppendQuestionToRuleTable(arrItems[0]);
					objDiv.appendChild(objNode);
				
			}
			
			strLastFormatType = arrItems[3];
			
			// Children Question Div
			if (arrItems[16] != "") {
				var objChildDiv = document.createElement("<div>");
				objChildDiv.id = "QChilds_" + arrItems[0];
				objDiv.appendChild(objChildDiv);
				blnPreceedingChildren = true;
			}
			else
				blnPreceedingChildren = false;
			
			// See if we need to show the question or not
			if (testQuestion(objQuestionRow))
				show(objQuestionRow)
			else
				hide(objQuestionRow);
				
			return(arrItems[0]);

		}

	}
	catch(e) {
		logErrAndObj("appendNewQuestion", objDiv, e)
		return false;
	}

}

function AppendQuestionToRuleTable(strQuestion) {

	try {	

		arrRuleQuestions.push(strQuestion);
	}
	catch(e) {
		logErr("AppendQuestionToRuleTable", e)
		return (false);
	}

}

// This function gets an updated survey question from the server
function GetQuestionChildrenValuesFromServer(SurveyCode, Question) {

	try {	

		testAspObject();
		var co = aspObject.rsGetQuestionChildrenValues(SurveyCode, Question)
		
		if (co.status != 0) 
			myCallBack(co)
		else {
			return(co.return_value)
		}
	}
	catch(e) {
		logErr("GetQuestionChildrenValuesFromServer", e)
	}
}

function radC(objItem) {
	try {
		if (objItem.nodeName == "INPUT" && objItem.getAttribute("type") == "radio")
			myR(objItem);
			
		var objQuestion = elementById("QR_" + objItem.getAttribute("Question"));
		
		// Test if the question passes its validation rules
		var blnValidate
		if (objQuestion.getAttribute("ValidateRule") != "" && objQuestion.getAttribute("ValidateRule") != "0")
			blnValidate = valR(objQuestion)
		else
			blnValidate = true;
				
		// See if we need to create any indented questions
		if (blnValidate && objQuestion.getAttribute("IndentRuleList") != "")
			indR(objQuestion);
			
		// See if any other questions need to be updated as a result of this change
		for (var i=0; i < arrRuleQuestions.length; i++) {
			var objQuestionRow = elementById("QR_" + arrRuleQuestions[i]);
			if (testQuestion(objQuestionRow))
				show(objQuestionRow)
			else {
				// reset(elementById("QChilds_" + objQuestion.getAttribute("Question")));
				hide(objQuestionRow);
			}
			var strFormatType = objQuestionRow.FormatType;
			if (strFormatType == "3" ||
				strFormatType == "4" ||
				strFormatType == "5" ||
				strFormatType == "6")
				checkParentDisplay(objQuestionRow);
		}
		
		// If we have been unselected then hide our children
		if (objQuestion.getAttribute("IndentRuleList") != "")
			if (SelectedValues(objQuestion.getAttribute("Question")) == "") {
				// reset(elementById("QChilds_" + objQuestion.getAttribute("Question")));
				hide(elementById("QChilds_" + objQuestion.getAttribute("Question")))
			}
			else
				show(elementById("QChilds_" + objQuestion.getAttribute("Question")));
		
	}
	catch(e) {
		logErrAndObj("radC", objItem, e)
	}
}

// Hides the parent table if none of its contents are being displayed
function checkParentDisplay(objItem) {
	try {
		
		// Find enclosure
		var objParent = objItem.getAttribute("parentElement");
		
		// Find all children
		var colRows = objParent.rows;
		
		var blnShown = false;
		// See if any are displayed
		for (var i=0; i< colRows.length; i++) {
			if (typeof colRows[i].Question == "string" &&
				colRows[i].style.display == "") {
				
				blnShown = true;
				break;
			}
		}
		
		// Show parent if any shown otherwise hide
		if (blnShown)
			show(objParent.parentElement)
		else
			hide(objParent.parentElement);
		
	}
	catch(e) {
		logErrAndObj("checkParentDisplay", objItem, e)
	}
}

// Validation and question rule functions
function indR(objItem) {

	try {
		
		if (testRules(objItem))
			return (getChildren(objItem))
		else
			return (false);

	}
	catch(e) {
		logErrAndObj("indR", objItem, e)
	}
}

function testQuestion(objItem) {
	try {
		// if there is a parent and nothing is selected then return false
		var objParent
		if (objItem.getAttribute("ParentQuestion") != "" && objItem.getAttribute("ParentQuestion") != "0") {
			objParent = elementById("QR_" + objItem.getAttribute("ParentQuestion"));
			
			var objSelectedValues = SelectedValues(objItem.getAttribute("ParentQuestion"));
			// If there are no selectable answers eg heading
			if (objSelectedValues == false)
				return(true);
			// If no answers are selected
			if (objSelectedValues == "" || objSelectedValues == "0")
				return(false);
		}

		if (objItem.getAttribute("IndentRule") == "" || objItem.getAttribute("IndentRule") == "0")
			return(true)
		else {
			var strRule = "qr_" + objItem.getAttribute("IndentRule") + "(objParent, objItem)"
			return(eval(strRule));
		}
	
	}
	catch(e) {
		logErrAndObj("testQuestion", objItem, e)
	}
}


function testRules(objItem) {
	try {
		if (objItem.getAttribute("IndentRuleList") == "" || (objItem.getAttribute("IndentRuleList") + ",").indexOf("0,") >= 0)
			return(true);
		else {
			var arrRules = objItem.getAttribute("IndentRuleList").split(",");
			for (var i=0; i < arrRules.length; i++) {
				if (arrRules[i] == 0)
					return(true);
				else {
					return(eval("qr_" + arrRules[i] + "(objItem, null)"));
				}
			}		
		}
		return(false);		
	}
	catch(e) {
		logErrAndObj("testRules", objItem, e)
	}
}

function valR(objItem) {

	try {
		var objQuestion = elementById("QR_" + objItem.getAttribute("Question"));
		if (objQuestion.getAttribute("ValidateRule") != "0" && objQuestion.getAttribute("ValidateRule") != "") {
	
			var strRule
			var objParent;
			var objPreviousSibling;
			var objNextSibling;
			var arrSiblings = getSiblings(objItem.getAttribute("Question"));
			var intMyOrder = thisSibling(arrSiblings, objItem.getAttribute("Question"));
			
			if (objQuestion.getAttribute("ParentQuestion") != "")
				objParent = elementById("QR_" + objQuestion.getAttribute("ParentQuestion"))
			else 
				objParent = null;

			if (intMyOrder < 0)
				objPreviousSibling = elementById("QR_" + arrSiblings[intMyOrder-1])
			else
				objPreviousSibling = null;
				
			if (intMyOrder <  arrSiblings.length-2)
				objNextSibling = elementById("QR_" + arrSiblings[intMyOrder+1])
			else
				objNextSibling = null;
			
			strRule = "qr_" + objQuestion.getAttribute("ValidateRule") + "(objParent, objItem, objPreviousSibling, objNextSibling);";
				
			var blnRes = eval(strRule);
			if (blnRes)
				return(true)
			else
				return(false);
		}
		else
			return(true);
	}
	catch(e) {
		logErrAndObj("valR", objItem, e)
	}
}

// Tests that one of the values is entered
function valReq(objItem, strValues, strValues2) {

	try {
		if ((typeof strValues == "undefined" ||
			strValues == "") && 
			(typeof strValues2 == "undefined" || 
			strValues2 == ""))
			return(true)
		else {
			var arrQuestion = elementsByName("QN" + objItem.getAttribute("Question"));
			
			if (typeof strValues != "undefined") {
				var arrValues = strValues.replace(" ","").split(",")
				for (var i=0; i< arrQuestion.length; i++) {
					if (arrQuestion[i].checked) {
						for (var j=0; j< arrValues.length; j++) {
							if (i+1 == arrValues[j])
								return(true);
						}
					}
				}
			}

			if (typeof strValues2 != "undefined") {
				arrValues = strValues2.replace(" ","").split(",")
				for (var i=0; i< arrQuestion.length; i++) {
					if (arrQuestion[i].checked) {
						for (var j=0; j< arrValues.length; j++) {
							if (i+1 == arrValues[j])
								return(true);
						}
					}
				}
			}
		}
		return(false);

	}
	catch(e) {
		logErrAndObj("valReq", objItem, e)
	}
}

// Return true if one of the values entered are selected
function expVal(objItem, strValues, strValues2) {

	try {
		var arrQuestion = elementsByName("QN" + objItem.getAttribute("Question"));
		
		if (typeof strValues != "undefined") {
			var arrValues = strValues.replace(" ","").split(",")
			for (var i=0; i< arrQuestion.length; i++) {
				if (arrQuestion[i].checked) {
					for (var j=0; j< arrValues.length; j++) {
						if (i+1 == arrValues[j])
							return(true);
					}
				}
			}
		}

		if (typeof strValues2 != "undefined") {
			arrValues = strValues2.replace(" ","").split(",")
			for (var i=0; i< arrQuestion.length; i++) {
				if (arrQuestion[i].checked) {
					for (var j=0; j< arrValues.length; j++) {
						if (i+1 == arrValues[j])
							return(true);
					}
				}
			}
		}

	}
	catch(e) {
		logErrAndObj("expVal", objItem, e)
	}
}

// Returns the values that have been selected in a question
// If the question doesn't have any selectable values then false is returned
function SelectedValues(strQuestion) {
	try {
		var arrQuestion = elementsByName("QN" + strQuestion);
		var strAnswers = ""
		
		if (arrQuestion.length == 0)
			return(false);
		
		for (var i=0; i< arrQuestion.length; i++) {
			if (arrQuestion[i].checked) {
				if (strAnswers != "") strAnswers += ","
				strAnswers += (i + 1)
			}
		}
		return(strAnswers);
	}
	catch(e) {
		logErr("SelectedValues", e)
	}
}

// Returns the questions siblings as an array
// Not tested
function getArrSiblings(strQuestion) {
	try {
		var objQuestion = elementById("QR_" + strQuestion);
		var objParent = objQuestion.getAttribute("ParentQuestion");
		
		return(objParent.getAttribute("ChildQuestions").split(","));
	}
	catch(e) {
		logErr("getArrSiblings", e)
	}
}

function thisSibling(arrSiblings, strQuestion) {
	try {
		for (var i=0; i<arrSiblings.length; i++) {
			if (arrSiblings[i] == strQuestion)
				return(i);
		}
		return(-1);
	}
	catch(e) {
		logErr("thisSibling", e)
	}
}

function showTop() {
	
	var objControlTop = elementByIdIfExists("PageControlTop");
	var objControlBottom = elementByIdIfExists("PageControlBottom");
	if (objControlTop != null && objControlBottom != null) 
	{
		if ((objectY(objControlBottom) + objectHeight(objControlBottom)) > document.body.clientHeight)
		{
			show(objControlTop);
		}
	}
	
	var arrElements=document.getElementsByTagName("a");
	for (var i=0;i<arrElements.length; i++) {
		if (arrElements[i].getAttribute("showForJavascript"))
			if (arrElements[i].getAttribute("showForJavascript") == "True")
				show(arrElements[i]);
	}
	
	arrElements=document.getElementsByTagName("span");
	for (var i=0;i<arrElements.length; i++) {
		if(arrElements[i].getAttribute("showForJavascript"))
			if(arrElements[i].getAttribute("showForJavascript") == "True")
				show(arrElements[i]);
	}
}

function showHelp(objItem) {
	
	var objHelpCell = elementByIdIfExists("helpCell");
	var strItemText = objItem.getAttribute("itemText");
	var strHelp = objItem.getAttribute("itemHelp");
	if (objHelpCell != null ) 
		objHelpCell.innerHTML = "<p class=HelpHeading>" + strItemText + "</p><p class=HelpBody>" + strHelp + "</p>"
	else
		objHelpCell.innerHTML = "";
	return false;
}

