function displayPrev(thisQuestion)
{
	document.getElementById('Display_' + thisQuestion).style.display = 'none';
		
	questionOrderStr = document.getElementById('questionOrder').value;
	questionOrderArr = questionOrderStr.split('|');
	
	previousItem = questionOrderArr.length-2;
	previousID = questionOrderArr[previousItem]

	document.getElementById('Display_' + previousID).style.display = '';
	document.getElementById('questionOrder').value = document.getElementById('questionOrder').value.replace(previousID + '|', '')
}

function displayNextNew(thisAnswerItems, thisQuestion, thisQuestionLogic, thisManadatoryQuestion, prevAnswerItems, nextAnswerItems)
{
	var selectedOptions = ',';
	var specifyQuestionFlag = 0;
	var specifyText = '';
	var showQuestionID = '';
	var hideQuestionID = '';

	var thisAnswerItemsArray = thisAnswerItems.split('|');
	var prevAnswerItemsArray = prevAnswerItems.split('|');
	var nextAnswerItemsArray = nextAnswerItems.split('|');
	var allOptionsCount = thisAnswerItemsArray.length - 7;
	var nextOptionsCount = ((nextAnswerItemsArray.length-7) - (nextAnswerItemsArray[nextAnswerItemsArray.length-1]));
	var thisSpecifyOption = thisAnswerItemsArray[2];
	var allOptionsArray = new Array(allOptionsCount)

	var thisItemObjectArray = eval('document.questionForm.q' + thisQuestion);
	var prevItemObjectArray = eval('document.questionForm.q' + (thisQuestion-1));
	for(i = 5; i < allOptionsCount-2; i++)
	{
		allOptionsArray[(i-5)] = thisAnswerItemsArray[i]
	}
	
	//Value and Flag array
	//[0] - Selected Options (Str)
	//[1] - Specify Text (Str)
	//[2] - Specify Question Flag (0 or 1)
	thisQuestionResults = getQuestionResults(thisAnswerItemsArray[0], allOptionsArray, thisItemObjectArray, thisAnswerItemsArray[2], thisQuestion, prevItemObjectArray).split('|');
	
	if(thisQuestionResults[2] == '1' && thisQuestionResults[1] == '')
	{
		alert('You must specify your \'Other\' option.');	
		return;
	}
	
	if(thisManadatoryQuestion == 1)
	{
		if(thisQuestionResults[0] == ',')
		{
			alert('You must select an option for this question before moving on.');	
			return;
		}
	}
	
	//Hide and Show IDs
	//[0] - HideID
	//[1] - ShowID
	thisHideShowIDsArray = getHideShowIDs(thisQuestionLogic, thisQuestion, nextAnswerItemsArray, thisQuestionResults[0], nextOptionsCount).split(',');
	document.getElementById('Display_' + thisHideShowIDsArray[0]).style.display = 'none';
	document.getElementById('Display_' + thisHideShowIDsArray[1]).style.display = '';
	
	if(thisQuestion == "1")
	{
		document.getElementById('questionOrder').value = thisQuestion + '|';		
	}
	else
	{
		document.getElementById('questionOrder').value = document.getElementById('questionOrder').value + thisQuestion + '|';		
	}
	return;
	
}

function getHideShowIDs(thisQuestionLogic, QuestionNumber, nextAnswerItemsArray, selectedOptions, nextOptionsCount)
{
	var showQuestionID = '';

	if(thisQuestionLogic == "")
	{
		return QuestionNumber + ',' + ((QuestionNumber*1)+1);
	}
	else
	{
		var selectedItem = false;
		var individualOptionLogic = thisQuestionLogic.split('|');
		for(i = 0; i < individualOptionLogic.length; i ++)
		{
			var individualItemLogic = individualOptionLogic[i].split(',');
			if(selectedOptions.indexOf(',' + individualItemLogic[0] + ',') > -1)
			{
				if (nextAnswerItemsArray[0] == "fromCheckboxSpecifyToRadioSpecify" || nextAnswerItemsArray[0] == "fromCheckboxSpecifyToRadioGrid")
				{
					var thisItemObjectArray = eval('document.questionForm.q' + QuestionNumber);
					for(i = 0; i < thisItemObjectArray.length; i ++)
					{
						if(i < nextOptionsCount)
						{
							var mySpan = document.getElementById('q' + ((QuestionNumber*1)+1) + '_specificOptions_' + thisItemObjectArray[i].value);
							if(thisItemObjectArray[i].checked)
							{
								mySpan.style.display = '';
							} else {
								mySpan.style.display = 'none';							
							}
						}	
					}
					if (nextAnswerItemsArray[0] == "fromCheckboxSpecifyToRadioSpecify")
					{
						document.getElementById('q'+ ((QuestionNumber*1)+1) +'_specify').value = document.getElementById('q'+ QuestionNumber +'_specify').value;
					}
				}

				if(showQuestionID != '')
				{
					if((showQuestionID*1) > individualItemLogic[1])
					{
						showQuestionID = individualItemLogic[1];
					}
				}
				else
				{
					showQuestionID = individualItemLogic[1];				
				}
			}
		}
		
		return QuestionNumber + ',' + showQuestionID
	}
}

function getQuestionResults(QuestionType, thisOptionsArray, thisItemObjectArray, SpecifyOption, QuestionNumber, prevOptionsArray)
{
	var selectedOptions = ",";
	var specifyText = "";
	var specifyQuestionFlag = "0";
	
	if(QuestionType == "radio" || QuestionType == "radioSpecify" || QuestionType == "fromCheckboxSpecifyToRadioSpecify")
	{
		for(i = 0; i < thisOptionsArray.length; i ++)
		{
			if(thisItemObjectArray[i].checked)
			{
				selectedOptions = selectedOptions + (i+1) + ",";
				if(QuestionType == "radioSpecify") 
				{
					if(SpecifyOption == (i+1))
					{
						specifyText = eval('document.questionForm.q' + QuestionNumber + '_specify').value;
						specifyQuestionFlag = 1;
					}
				}
				if(QuestionType == "fromCheckboxSpecifyToRadioSpecify") 
				{
					if(SpecifyOption != (i+1))
					{
						eval('document.questionForm.q' + QuestionNumber + '_specify').value = '';
					}
				}
			}			
		}
	}
	else if(QuestionType == "checkbox" || QuestionType == "checkboxSpecify" || QuestionType == "checkboxGrid")
	{
		for(i=0; i < thisOptionsArray.length; i ++)
		{
			if(thisItemObjectArray[i].checked)
			{
				selectedOptions = selectedOptions + (i+1) + ",";	
				if(QuestionType == "checkboxSpecify") 
				{
					if(SpecifyOption == (i+1))
					{
						specifyText = eval('document.questionForm.q' + QuestionNumber + '_specify').value;
						specifyQuestionFlag = 1;
					}
				}
			}
		}	
	}
	else if (QuestionType == "radioGrid" || QuestionType == "radioGridFreeText" || QuestionType == "radioScale" || QuestionType == "fromCheckboxSpecifyToRadioGrid")
	{ 
		var possibleOptions = 0;
		for(i = 0; i < thisOptionsArray.length; i ++) 
		{
			var answerArray = eval('document.questionForm.q' + QuestionNumber + '_' + (i+1));
			for(j = 0; j < answerArray.length; j ++)
			{
				if(answerArray[j].checked)
				{
					if(QuestionType == "fromCheckboxSpecifyToRadioGrid")
					{
						selectedOptions = selectedOptions + (i+1) + "_" + (j+1) + ",";					
					}
					else
					{
						selectedOptions = selectedOptions + (j+1) + ",";										
					}
				}
			}
			if(QuestionType == "fromCheckboxSpecifyToRadioGrid")
			{
				var rowItem = document.getElementById('q' + QuestionNumber + '_specificOptions_' + (i+1));
				if(rowItem.style.display == '')
				{
					possibleOptions = ((possibleOptions*1)+ 1);				
				}
			} 
			else
			{
				possibleOptions = ((possibleOptions*1)+ 1);
			}
		}
		selectedIndexArray = selectedOptions.split(',');
		if(selectedIndexArray.length-2 < possibleOptions )
		{
			selectedOptions = ",";
		}	

	} 
	return selectedOptions + '|' + specifyText + '|' + specifyQuestionFlag;
}

function displayNext(allOptionsArray, thisQuestion, questionType, questionLogic, manadatoryQuestion, optionCount, nextQuestionType, nextQuestionOptions, specifyOption)
{
	var selectedOptions = ',';
	var specifyQuestionFlag = 0;
	var specifyText = '';
	var showQuestionID = '';
	var hideQuestionID = '';

	if(questionType == "radio" || questionType == "radioSpecify" || questionType == "fromCheckboxSpecifyToRadioSpecify")
	{
		for(i = 0; i < allOptionsArray.length; i ++)
		{
			if(allOptionsArray[i].checked)
			{
				selectedOptions = selectedOptions + (i+1) + ",";
				if(questionType == "radioSpecify") 
				{
					if(specifyOption == (i+1))
					{
						specifyText = eval('document.questionForm.q' + thisQuestion + '_specify').value;
						specifyQuestionFlag = 1;
					}
				}
				if(questionType == "fromCheckboxSpecifyToRadioSpecify") 
				{
					if(specifyOption != (i+1))
					{
						eval('document.questionForm.q' + thisQuestion + '_specify').value = '';
					}
				}
			}			
		}
	}
	else if(questionType == "checkbox" || questionType == "checkboxSpecify" || questionType == "checkboxGrid")
	{
		for(i=0; i < allOptionsArray.length; i ++)
		{
			if(allOptionsArray[i].checked)
			{
				selectedOptions = selectedOptions + (i+1) + ",";	
				if(questionType == "checkboxSpecify") 
				{
					if(specifyOption == (i+1))
					{
						specifyText = eval('document.questionForm.q' + thisQuestion + '_specify').value;
						specifyQuestionFlag = 1;
					}
				}
			}
		}	
	}
	else if (questionType == "radioGrid" || questionType == "radioGridFreeText" || questionType == "fromCheckboxSpecifyToRadioGrid")
	{ 
		for(i = 0; i < optionCount; i ++) 
		{
			var answerArray = eval('document.questionForm.q' + thisQuestion + '_' + (i+1));
			for(j = 0; j < answerArray.length; j ++)
			{
				if(answerArray[j].checked)
				{
					selectedOptions = selectedOptions + (j+1) + ",";
				}
			}
		}
		
		if(questionType == "fromCheckboxSpecifyToRadioGrid")
		{
			var previousAnswerArray = eval('document.questionForm.q' + ((thisQuestion*1)-1));
			var possibleOptions = 1;
			for(i=0; i < theArray.length; i ++)
			{
				if(theArray[i].checked)
				{
					possibleOptions = ((possibleOptions*1)+ 1);
				}
			}
			optionCount = possibleOptions;
		}

		selectedIndexArray = selectedOptions.split(',');
		if(selectedIndexArray.length-2 < optionCount )
		{
			selectedOptions = ",";
		}
	} 
	 else if(questionType == "textBox")	{
		if(document.getElementById('q' + thisQuestion).value != "")
		{
			selectedOptions = selectedOptions + document.getElementById('q' + thisQuestion).value + ",";	
		}
	}
	if(specifyQuestionFlag == '1' && specifyText == '')
	{
		alert('You must specify your \'Other\' option.');	
		return;
	}
	
	if(manadatoryQuestion == 1)
	{
		if(selectedOptions == ',')
		{
			alert('You must select an option for this question before moving on.');	
			return;
		}
	}

	if(questionLogic == "")
	{
		//No Logic, so go to next question
		if (nextQuestionType == "fromCheckboxSpecifyToRadioSpecify" || nextQuestionType == "fromCheckboxSpecifyToRadioGrid")
		{
			for(i = 0; i < allOptionsArray.length; i ++)
			{
				if(allOptionsArray[i].checked)
				{
					var mySpan = document.getElementById('q' + ((thisQuestion*1)+1) + '_specificOptions_' + ((i*1)+1));
					mySpan.style.display = '';				
				}
			}
		}
		showQuestionID = ((thisQuestion*1)+1);
		hideQuestionID = thisQuestion;
	} 	
	else 
	{
		//There is some logic, so work out next question number.
		selectedItem = false;
		individualOptionLogic = questionLogic.split('|');
		
		for(i = 0; i < individualOptionLogic.length; i ++)
		{
			individualItemLogic = individualOptionLogic[i].split(',');
			
			if(selectedOptions.indexOf(',' + individualItemLogic[0] + ',') > -1)
			{
				if (nextQuestionType == "fromCheckboxSpecifyToRadioSpecify" || nextQuestionType == "fromCheckboxSpecifyToRadioGrid")
				{
					var theArray = eval('document.questionForm.q' + thisQuestion);
					for(i=0; i < theArray.length; i ++)
					{
						if(i < nextQuestionOptions)
						{
							var mySpan = document.getElementById('q' + ((thisQuestion*1)+1) + '_specificOptions_' + theArray[i].value);
							if(theArray[i].checked)
							{
								mySpan.style.display = '';
							} else {
								mySpan.style.display = 'none';							
							}
						}	
					}
					document.getElementById('q'+ ((thisQuestion*1)+1) +'_specify').value = document.getElementById('q'+ thisQuestion +'_specify').value;
				}
				showQuestionID = individualItemLogic[1];
				hideQuestionID = thisQuestion;
				document.getElementById('Display_' + hideQuestionID).style.display = 'none';
				document.getElementById('Display_' + showQuestionID).style.display = '';
				selectedItem = true;
				if(thisQuestion == "1")
				{
					document.getElementById('questionOrder').value = thisQuestion + '|';		
				}
				else
				{
					document.getElementById('questionOrder').value = document.getElementById('questionOrder').value + thisQuestion + '|';		
				}
				return;
			}
		}

		if(!selectedItem)
		{
			showQuestionID = ((thisQuestion*1)+1);
			hideQuestionID = thisQuestion;
		}
	}
	
	if(thisQuestion == "1")
	{
		document.getElementById('questionOrder').value = thisQuestion + '|';		
	}
	else
	{
		document.getElementById('questionOrder').value = document.getElementById('questionOrder').value + thisQuestion + '|';		
	}

	document.getElementById('Display_' + hideQuestionID).style.display = 'none';
	document.getElementById('Display_' + showQuestionID).style.display = '';
}

function finish(thisQuestion, questionType, manadatoryQuestion, logonField)
{
	var selectedIndex = ',';
	
	if(questionType == "radio"||questionType == "radioSpecify")
	{
		var theArray = eval('document.questionForm.q' + thisQuestion);
		for(i=0; i < theArray.length; i ++)
		{
			if(theArray[i].checked)
			{
				selectedIndex = selectedIndex + ((i*1)+1) + ",";
			}
		}
	} else if (questionType == "radioGrid"||questionType == "radioGridFreeText"){ 
		for(j=0; j < optionCount; j ++) 
		{
			var theArray = eval('document.questionForm.q' + thisQuestion + '_' + (j+1));
			for(i=0; i < theArray.length; i ++)
			{
				if(theArray[i].checked)
				{
					selectedIndex = selectedIndex + ((i*1)+1) + ",";
				}
			}
		}
		selectedIndexArray = selectedIndex.split(',');
		if(selectedIndexArray.length-2 < optionCount )
		{
			selectedIndex = ",";
		}
	} else if (questionType == "checkbox"||questionType == "checkboxSpecify"||questionType == "checkboxGrid") {
		var theArray = eval('document.questionForm.q' + thisQuestion);
		for(i=0; i < theArray.length; i ++)
		{
			if(theArray[i].checked)
			{
				selectedIndex = selectedIndex + ((i*1)+1) + ",";	
			}
		}
	} else if(questionType == "textBox")	{
		if(document.getElementById('q' + thisQuestion).value != "")
		{
			selectedIndex = selectedIndex + document.getElementById('q' + thisQuestion).value + ",";	
		}
	}
	
	if(manadatoryQuestion == 1)
	{
		if(selectedIndex == ',')
		{
			alert('You must select an option for this question before moving on.');	
			return;
		}
	}
	document.getElementById('Display_' + thisQuestion).style.display = 'none';
	document.getElementById('questionOrder').value = document.getElementById('questionOrder').value + thisQuestion + '|';		
	x = showAllSections(thisQuestion, logonField);
}

function uncheckOtherCheckboxes(questionNumber, optionsTotal, skipThisOption, clearFlag, optionsToLeave, specifyOption)
{
	if(clearFlag == "1") 
	{
		for(i = 1; i < (optionsTotal*1) + 1; i ++)
		{
			if(i != skipThisOption)
			{
				var myCheckbox = eval('document.questionForm.q' + questionNumber);
				myCheckbox[i-1].checked = false;	
			}
		}
		if(specifyOption != "0")
		{
			document.getElementById('q' + questionNumber + '_specify').value = '';
		}
	} else {
		for(i = ((optionsTotal*1) + 1) - optionsToLeave; i < (optionsTotal*1) + 1; i ++)
		{
			if(i != skipThisOption)
			{
				var myCheckbox = eval('document.questionForm.q' + questionNumber);
				myCheckbox[i-1].checked = false;	
			}
		}
	}
}

function showAllSections(sectionCount, logonField)
{
		for(i = 0; i < sectionCount; i ++)
		{
			document.getElementById('Display_' + ((i*1)+1)).style.position = 'absolute';
			document.getElementById('Display_' + ((i*1)+1)).style.left = '-5000';
			document.getElementById('Display_' + ((i*1)+1)).style.display = '';
		}
		if(logonField != '')
		{
			document.getElementById(logonField).style.position = 'absolute';
			document.getElementById(logonField).style.left = '-5000';
			document.getElementById(logonField).style.display = '';
		}
		timeOutId = setTimeout("document.questionForm.submit();", 1);
		

}

function displayNextSpecial(thisAnswerItems, thisQuestion, thisQuestionLogic, thisManadatoryQuestion, prevAnswerItems, nextAnswerItems)
{
	var selectedOptions = ',';
	var specifyQuestionFlag = 0;
	var specifyText = '';
	var showQuestionID = '';
	var hideQuestionID = '';

	var thisAnswerItemsArray = thisAnswerItems.split('|');
	var prevAnswerItemsArray = prevAnswerItems.split('|');
	var nextAnswerItemsArray = nextAnswerItems.split('|');
	var allOptionsCount = thisAnswerItemsArray.length - 7;
	var nextOptionsCount = ((nextAnswerItemsArray.length-7) - (nextAnswerItemsArray[nextAnswerItemsArray.length-1]));
	var thisSpecifyOption = thisAnswerItemsArray[2];
	var allOptionsArray = new Array(allOptionsCount)

	var thisItemObjectArray = eval('document.questionForm.q' + thisQuestion);
	for(i = 5; i < allOptionsCount-2; i++)
	{
		allOptionsArray[(i-5)] = thisAnswerItemsArray[i]
	}
	
	//Value and Flag array
	//[0] - Selected Options (Str)
	//[1] - Specify Text (Str)
	//[2] - Specify Question Flag (0 or 1)
	thisQuestionResults = getQuestionResults(thisAnswerItemsArray[0], allOptionsArray, thisItemObjectArray, thisAnswerItemsArray[2], thisQuestion, prevAnswerItemsArray).split('|');
	
	if(thisQuestionResults[2] == '1' && thisQuestionResults[1] == '')
	{
		alert('You must specify your \'Other\' option.');	
		return;
	}
	
	if(thisManadatoryQuestion == 1)
	{
		if(thisQuestionResults[0] == ',')
		{
			alert('You must select an option for this question before moving on.');	
			return;
		}
	}
	
	if(thisQuestion == "15")
	{
		var optionsSelected = 0;
		var thisItemObjectArray = eval('document.questionForm.q1');	
		for(j = 0; j < thisItemObjectArray.length; j ++)
		{
			if(thisItemObjectArray[j].checked)
			{
				if(thisItemObjectArray[j].value != '16' && thisItemObjectArray[j].value != '17')
				{
					optionsSelected = ((optionsSelected*1) + 1)				
				}
			}
		}
	
		document.getElementById('Display_' + thisQuestion).style.display = 'none';	
		if(optionsSelected > 0)
		{
			document.getElementById('Display_16').style.display = '';
		} else {
			document.getElementById('Display_25').style.display = '';	
		}
	}
	
	if(thisQuestion == "18")
	{
		var checkedItem = 0;
		var thisQuestionArray = eval('document.questionForm.q18');	
		for(k = 0; k < thisQuestionArray.length; k ++)
		{
			if(thisQuestionArray[k].checked)
			{
				checkedItem = thisQuestionArray[k].value;
			}
		}

		if(checkedItem == '17' || checkedItem == '18')
		{
			var optionsSelected = 0;
			for(i = 1; i < 3; i++)
			{
				var thisItemObjectArray = eval('document.questionForm.q2_' + i);	
				for(j = 0; j < thisItemObjectArray.length; j ++)
				{
					if(thisItemObjectArray[j].checked)
					{
						optionSelectedArray = thisItemObjectArray[j].value.split('_');
						if(optionSelectedArray[0] == "1")
						{
							if(optionSelectedArray[1] < 5)
							{
								optionsSelected = ((optionsSelected*1) + 1)				
							}
						}
					}
				}
			}
			document.getElementById('Display_' + thisQuestion).style.display = 'none';	
			if(optionsSelected > 0)
			{
				document.getElementById('Display_20').style.display = '';
			} else {
				document.getElementById('Display_25').style.display = '';	
			}
		}
		else 
		{
			document.getElementById('Display_' + thisQuestion).style.display = 'none';	
			document.getElementById('Display_19').style.display = '';	
		}
	}
	
	if(thisQuestion == "19")
	{
		var optionsSelected = 0;
		for(i = 1; i < 3; i++)
		{
			var thisItemObjectArray = eval('document.questionForm.q2_' + i);	
			for(j = 0; j < thisItemObjectArray.length; j ++)
			{
				if(thisItemObjectArray[j].checked)
				{
					optionSelectedArray = thisItemObjectArray[j].value.split('_');
					if(optionSelectedArray[0] == "1")
					{
						if(optionSelectedArray[1] < 5)
						{
							optionsSelected = ((optionsSelected*1) + 1)				
						}
					}
				}
			}
		}

		document.getElementById('Display_' + thisQuestion).style.display = 'none';	
		if(optionsSelected > 0)
		{
			document.getElementById('Display_20').style.display = '';
		} else {
			document.getElementById('Display_25').style.display = '';	
		}
	}
	
	
	
	if(thisQuestion == "1")
	{
		document.getElementById('questionOrder').value = thisQuestion + '|';		
	}
	else
	{
		document.getElementById('questionOrder').value = document.getElementById('questionOrder').value + thisQuestion + '|';		
	}
	return;
}