/*
 * Setup for this script to work:
 * It expects that there are two initially hidden content blocks (div,td,span,etc)
 *  with the IDs "topics_area" and "subtopics_area."  These blocks should also contain their
 *  respective select boxes
 * The select boxes inside the content containers should have NAMEs of course, topics, subtopics
 *  with js of onchange="loadTopics(this.selectedIndex)", onchange="loadSubTopics(this.selectedIndex)",
 *  onchange="subTopicSelect(this.selectedIndex)" respectively.
 * The html page body tag should have: onload="loadCourses('$js')" to do any auto-building/reloading
 */

var level;
var courseLevel = new Array();

function loadCourses(reload,owner) {
	courseLevel[owner] = 0;
	if (reload) {
		/* reload means that we need to "simulate" the user making the the drop down
		   selections.  We do this by looping through the proper box each time a change
		   happens until we're at the sub topic box, or until the browse has been
		   completely defined. */
		loadCourses(false, owner);
		var parts = reload.split(",");
		var boxOptions = document.forms[owner].course.options;
		for (var j=0;j<parts.length;j++) {
			for (var i=1;i<boxOptions.length;i++) {
				if (boxOptions[i].value == parts[j]) {
					if (j == 0) { loadTopics(i, owner);    boxOptions = document.forms[owner].topics.options; }
					if (j == 1) { loadSubTopics(i, owner); boxOptions = document.forms[owner].subtopics.options; }
					if (j == 2) subTopicSelect(i, owner);
					break;
				}
			}
		}
	} else {
		var courseOptions    = document.forms[owner].course.options;
		courseOptions.length = 0;
		courseOptions.length = topics.length;

		var extra = 0 ;
		if ( owner == "choice" )
		{
			extra = 1 ;
			courseOptions[0] = new Option("Pick a Course", "");
		}

		courseOptions[0+extra] = new Option("All Courses", "ALL");
		for (var i=1;i<topics.length;i++) {
			courseOptions[i+extra] = new Option(topics[i][0][0], topics[i][0][1]);
		}

		if ( owner == "choice" )
		{
			document.forms[owner].co.value = "";
		}
		else
		{
			document.forms[owner].co.value = "ALL";
			document.forms[owner].tp.value = "ALL";
		}

		document.forms[owner].course.selectedIndex = 0;
	}

	if (owner != 'choice' && document.forms.choice)
	{	loadCourses(reload, 'choice');	}
}

function loadTopics(selIndex, owner) {

	// If the user selected All Grade Level/Subjects option; very easy case.
	if (document.forms[owner].course.options[selIndex].value == "ALL") {
		document.forms[owner].co.value = "ALL";
		document.forms[owner].tp.value = "ALL";
		resetBoxes(owner);
		return;
	}
	// Mark the index as selected (for Reload purposes)
	document.forms[owner].course.selectedIndex = selIndex;
	resetBoxes(owner);
	document.getElementById(owner+"_topics_area").style.display = '';

	var topicOptions    = document.forms[owner].topics.options;
	topicOptions.length = 0;
	topicOptions.length = topics[selIndex].length;

	topicOptions[0] = new Option("All "+topics[selIndex][0][0]+" Topics", "ALL");
	for (var i=1;i<topics[selIndex].length;i++) {
		topicOptions[i] = new Option(topics[selIndex][i][0][0], topics[selIndex][i][0][1]);
	}
	// set the course level to the selected level so that we can navigate topics to build
	//   sub  topics.
	courseLevel[owner] = selIndex;
	// set the browse course field to the currently selected course
	document.forms[owner].co.value = topics[selIndex][0][1];
	document.forms[owner].tp.value = "ALL";
	document.forms[owner].topics.selectedIndex = 0;
}

function loadSubTopics(selIndex, owner) {
	// If the user selected All * Topics
	if (document.forms[owner].topics.options[selIndex].value == "ALL") {
		document.forms[owner].tp.value = "ALL";
		document.getElementById(owner+"_subtopics_area").style.display = 'none';
		return;
	}
	document.forms[owner].topics.selectedIndex = selIndex;
	document.getElementById(owner+"_subtopics_area").style.display = '';

	var subTopicOptions    = document.forms[owner].subtopics.options;
	subTopicOptions.length = 0;
	subTopicOptions.length = topics[courseLevel[owner]][selIndex].length;
					// "+topics[courseLevel][selIndex][0][0]+"
	subTopicOptions[0] = new Option("All Sub-Topics", topics[courseLevel[owner]][selIndex][0][1]);
	for (var i=1;i<topics[courseLevel[owner]][selIndex].length;i++) {
		subTopicOptions[i] = new Option(topics[courseLevel[owner]][selIndex][i][0][0], topics[courseLevel[owner]][selIndex][i][0][1]);
	}

	// set the browse topic field to the currently selected topic
	document.forms[owner].tp.value = document.forms[owner].topics[selIndex].value;
	document.forms[owner].subtopics.selectedIndex = 0;
}

function subTopicSelect(selIndex, owner) {
	// Mark the index as selected (for Reload purposes)
	document.forms[owner].subtopics.selectedIndex = selIndex;
	document.forms[owner].tp.value = document.forms[owner].subtopics.options[selIndex].value;
}

function doBrowse(owner) {
	/*alert("Course: "+document.forms[owner].co.value+"\n"+
		"Topic: "+document.forms[owner].tp.value);*/
	document.forms[owner].submit();
}

function doPrettyBrowse(owner) {
	var database = document.forms[owner].database.value;
	var co = document.forms[owner].co.value;
	var tp = document.forms[owner].tp.value;
	var rc = document.forms[owner].rc.value;
	var pl = document.forms[owner].pl.value;
	if (database == "catalog") {
		var all = new Array(co, tp, rc, pl);
		all = all.join(",");
		location.href = "http://mathforum.org/mathtools/cell/"+all+"/";
	} else {
		var context = (co == "ALL" && tp == "ALL" && rc == "ALL" &&  pl == "ALL") ? "all" : "cell";
		var qs = new Array();
		if (context == "cell") {
			if (co != "ALL") qs[qs.length] = "co="+co;
			if (tp != "ALL") qs[qs.length] = "tp="+tp;
			if (rc != "ALL") qs[qs.length] = "rc="+rc;
			if (pl != "ALL") qs[qs.length] = "pl="+pl;
			if (qs.length) context += "&"+qs.join("&");
		}
		location.href = "http://mathforum.org/mathtools/discuss.html?context="+context+"&database="+database;
	}
}

function doSearch(owner) {
	var database = document.forms[owner].database.value;
	var action_url = "";
	if (database == "discussions") action_url = "search/discuss.html";
	else if (database == "catalog") action_url = "search/index.html";
	document.forms[owner].action = "http://mathforum.org/mathtools/"+action_url;
	document.forms[owner].submit();
	return 0;
}

function resetBoxes(owner) {
	document.forms[owner].topics.options.length = 0;
	document.getElementById(owner+"_topics_area").style.display = "none";
	document.forms[owner].subtopics.options.length = 0;
	document.getElementById(owner+"_subtopics_area").style.display = "none";
}


 //  Form selectOwner collects info about a user's choice of location of his message
 //  Form valueOwner stores old_msg and database
function get_msg(selectOwner , valueOwner)
{
        var selIndex;
	var loc_error = "";
        //-------------------------------------------------------------------------//
        selIndex =  document.forms[selectOwner].course.selectedIndex;
	var co = document.forms[selectOwner].course.options[selIndex].value;
	
	if (co == "")	loc_error = loc_error + "<P style=\"color:purple\">Course</P>";
	if ( co == "ALL") co = "";

        var tp = "" ;

        selIndex = document.forms[selectOwner].platform.selectedIndex;
        var pl = document.forms[selectOwner].platform.options[selIndex].value;
	if (pl == "")	loc_error = loc_error + "<P style=\"color:brown\">Platform type</P>";
	if ( pl == "ALL") pl = "";

        selIndex = document.forms[selectOwner].resource.selectedIndex;
        var rc = document.forms[selectOwner].resource.options[selIndex].value;
	if (rc == "") loc_error = loc_error + "<P style=\"color:red\">Resource type</P>";
	if ( rc == "ALL") rc = "";

        //-------------------------------------------------------------------------//

        document.forms[valueOwner].location_error.value = loc_error;

        //-------------------------------------------------------------------------//
        var id = "" ;
        var dtype = "g";
        document.forms[valueOwner].old_msg.value = co+"_"+tp+"_"+pl+"_"+rc+"_"+id+"_"+dtype;
}

