var maxLenCategoryName = 20;
var fixedLenCategoryId =  8;
var maxLenGroupName    = 20;
var fixedLenGroupId    =  8;

var controlWin;
var controlForm;
var contentWin;

var categoryList;
var groupList;

var currGroup    = 0;
var currCategory = 0;
var currQuestion = 0;

var quesShown = false;
var showQuestionsGrpID    = "";
var showQuestionsCatID    = "";
var showQuestionsQueID    = "";
var showQuestionsQueValue = 0;

var quesToEdit;
var quesNumInCat;

var groupToSelect;
var categoryToSelect;
var questionToSelect;

function ControlsDisable()
{
	controlForm.group_menu.disabled         = true;
	controlForm.category_menu.disabled      = true;
}

function ControlsEnable()
{
	controlForm.group_menu.disabled         = (controlForm.group_menu.options.length == 1);
	controlForm.category_menu.disabled      = (controlForm.category_menu.options.length == 1);
}

function dbGetCategoryCount()
{
	return top.submit.categoryData.length - 1;
}

function dbGetCategoryID(cat)
{
	return top.submit.categoryData[cat][0];
}

function dbGetCategoryNumberFromQuestionNumber(quesNum)
{
	quesNumInCat = quesNum;
	var mnu = controlForm.category_menu;
	var opt = 1;
	var cat = mnu.options[opt].value;
	while (quesNumInCat > dbGetCategoryQuestionCount(cat))
	{
		quesNumInCat -= dbGetCategoryQuestionCount(cat);
		opt ++;
		cat = mnu.options[opt].value;
	}
	return cat;
}

function dbGetCategoryQuestionCount(cat)
{
	return top.submit.categoryData[cat][2];
}

function dbGetCategoryQuestionID(cat,que)
{
	return top.submit.categoryData[cat][3][que][0];
}

function dbGetCategoryTitle(cat)
{
	return top.submit.categoryData[cat][1];
}

function dbGetGroupCount()
{
	return top.submit.groupData.length - 1;
}

function dbGetGroupID(grp)
{
	return top.submit.groupData[grp][0];
}

function dbGetGroupTitle(grp)
{
	return top.submit.groupData[grp][1];
}

function Init()
{
	if (typeof(top.submit.groupData) == "undefined")
	{
		setTimeout("Init()",1000);
	}
	else if (typeof(top.content) == "undefined")
	{
		setTimeout("Init()",1000);
	}
	else if (typeof(top.main) == "undefined")
	{
		setTimeout("Init()",1000);
	}
	else if (typeof(top.main.document.forms[0]) == "undefined")
	{
		setTimeout("Init()",1000);
	}
	else
	{
		controlWin = top.main;
		controlForm = controlWin.document.forms[0];
		UpdateGroupList();
		UpdateCategoryList();
		MenuUpdateGroup(0);
		MenuUpdateCategory(0);
		ControlsEnable();
		MessageClear(controlWin);
		UpdateQuestionList(true);
	}
}

function MenuSelectionCategory()
{
	var mnu = controlForm.category_menu;
	currCategory = mnu.options[mnu.selectedIndex].value;
	UpdateQuestionList(true);
	ControlsEnable();
}

function MenuSelectionGroup()
{
	var mnu = controlForm.group_menu;
	currGroup = mnu.options[mnu.selectedIndex].value;
	MenuUpdateCategory(0);
	UpdateQuestionList(true);
	ControlsEnable();
}

function MenuUpdateCategory(newCurr)
{
	if (newCurr == -1)
	{
		for (var c = 1; (c <= dbGetCategoryCount()) && (newCurr == -1); c ++)
		{
			if (dbGetCategoryID(c) == categoryToSelect)
			{
				newCurr = c;
			}
		}
	}
	var mnu = controlForm.category_menu;
	mnu.selectedIndex = 0;
	mnu.options.length = 1;
	for (var cat = 1; cat < categoryList.length; cat ++)
	{
		if ((currGroup == 0) || (top.submit.associationMap[categoryList[cat][0]][currGroup]))
		{
			mnu.options[mnu.options.length] = new Option(categoryList[cat][1],categoryList[cat][0],false,false);
		}
	}
	if (mnu.options.length == 1)
	{
		mnu.options[0].text = "(no categories)";
	}
	else
	{
		mnu.options[0].text = "All";
	}
	for (var opt = 1; opt < mnu.options.length; opt ++)
	{
		if (mnu.options[opt].value == newCurr)
		{
			mnu.selectedIndex = opt;
		}
	}
	currCategory = newCurr;
}

function MenuUpdateGroup(newCurr)
{
	if (newCurr == -1)
	{
		for (var g = 1; (g <= dbGetGroupCount()) && (newCurr == -1); g ++)
		{
			if (dbGetGroupID(g) == groupToSelect)
			{
				newCurr = g;
			}
		}
	}
	var mnu = controlForm.group_menu;
	mnu.selectedIndex = 0;
	mnu.options.length = 1;
	for (var grp = 1; grp < groupList.length; grp ++)
	{
		mnu.options[mnu.options.length] = new Option(groupList[grp][1],groupList[grp][0],false,false);
	}
	for (var opt = 1; opt < mnu.options.length; opt ++)
	{
		if (mnu.options[opt].value == newCurr)
		{
			mnu.selectedIndex = opt;
		}
	}
	currGroup = newCurr;
}

function ShowQuestions()
{
	showQuestionsGrpID    = "";
	showQuestionsCatID    = "";
	showQuestionsQueID    = "";
	if (currGroup != 0)
	{
		showQuestionsGrpID = dbGetGroupID(currGroup);
		if (currCategory != 0)
		{
			showQuestionsCatID = dbGetCategoryID(categoryList[currCategory][0]);
			if (currQuestion != 0)
			{
				showQuestionsQueID = dbGetCategoryQuestionID(categoryList[currCategory][0],currQuestion);
			}
		}
		else
		{
			if (currQuestion != 0)
			{
				showQuestionsQueID = dbGetCategoryQuestionID(dbGetCategoryNumberFromQuestionNumber(currQuestion),quesNumInCat);
			}
		}
	}
	else
	{
		if (currCategory != 0)
		{
			showQuestionsCatID = dbGetCategoryID(currCategory);
			if (currQuestion != 0)
			{
				showQuestionsQueID = dbGetCategoryQuestionID(currCategory,currQuestion);
			}
		}
		else
		{
			if (currQuestion != 0)
			{
				showQuestionsQueID = dbGetCategoryQuestionID(dbGetCategoryNumberFromQuestionNumber(currQuestion),quesNumInCat);
			}
		}
	}
	if ((showQuestionsGrpID == "") && (showQuestionsCatID == "") && (showQuestionsQueID == ""))
	{
		alert("You must choose a group, a category, or both.");
	}
	else
	{
		var f = contentWin.document.forms[0];
		f.group_id.value       = showQuestionsGrpID;
		f.category_id.value    = showQuestionsCatID;
		f.question_id.value    = showQuestionsQueID;
		f.question_value.value = showQuestionsQueValue;
		f.submit();
	}
}

function UpdateCategoryList()
{
	var tmp;
	categoryList = new Array();
	for (var c = 1; c <= dbGetCategoryCount(); c++)
	{
		categoryList[c] = new Array(c,dbGetCategoryTitle(c));
	}
}

function UpdateGroupList()
{
	var tmp;
	groupList = new Array();
	for (var g = 1; g <= dbGetGroupCount(); g++)
	{
		groupList[g] = new Array(g,dbGetGroupTitle(g));
	}
}

function UpdateQuestionList(forceBlank)
{
	if (quesShown && !forceBlank)
	{
		top.content.location.replace("content_review_ques_list.jsp?group_id=" + showQuestionsGrpID + "&category_id=" + showQuestionsCatID + "&question_id=" + showQuestionsQueID + "&question_value=" + showQuestionsQueValue);
	}
	else
	{
		var hrf = top.content.location.href;
		if ((hrf.indexOf("content_review_ques_list.jsp") == -1) || (hrf.indexOf("content_review_ques_list.jsp?") != -1))
		{
			top.content.location.replace("content_review_ques_list.jsp");
		}
	}
}
