///////////////////////////////////////////////////////////////////////////////
//	schedule.js - Schedule of Classes Javascript source 
//	Author: Joe Parlett
//  Date: Aug. 18, 2000
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
//	Error Handling functions
//	
///////////////////////////////////////////////////////////////////////////////
function showJavaScriptErrors()
{
	window.location = "javascript:";
}

///////////////////////////////////////////////////////////////////////////////
//	Message functions
//	
///////////////////////////////////////////////////////////////////////////////
function changeTerm()
{	msg = "Reloading the form based on new term selection.\n";
	msg += "Please wait until the page reloads before choosing another option.\n";
	msg += "Click 'Ok' to continue and wait for the message that the form has finished reloading.";
	alert(msg);
	document.forms[0].submit();
}

function formLoaded()
{
	status = "Page has finished loading.  Please make your selections.";
	alert(status);
}

///////////////////////////////////////////////////////////////////////////////
//	Initialization functions
//	
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
//	Function: initializeControls()
//	Purpose: called from onload event on the body tag
///////////////////////////////////////////////////////////////////////////////
function initializeControls()
{

	//alert("initializeControls start");
	initializeTermComboBox();
	initializeLocationComboBox();
	initializeAcademicLevelComboBox();
	initializeSubjectComboBox();
	initializeTitleAndCourseNumComboBox();
	formLoaded();
}
///////////////////////////////////////////////////////////////////////////////
//	Function: initializeTermComboBox()
//	Purpose: initialize the semester select box
///////////////////////////////////////////////////////////////////////////////
function initializeTermComboBox()
{
	//alert("initializeTermComboBox start");
	with (document.TermSelect)
	{
		//Clear any current options from the select
		Term.options.length = 0;
		
		//For each course record
				
		for(rowNum = 0; rowNum < aTerms.getRowCount(); rowNum++)
		{
			//Create new option
			NewOpt = new Option;
			NewOpt.value = aTerms.terms_id[rowNum];
			NewOpt.text = aTerms.term_reporting_text[rowNum];
			
			//Add the new object to the select list
			Term.options[Term.options.length] = NewOpt;
			
			if(Default_Term == aTerms.terms_id[rowNum])
			{				
				//Select the default value	
				Term.selectedIndex = rowNum;
				//alert(Default_Term + " == " + aTerms.sec_reporting_term[rowNum]);
			}
			else
			{
				//alert(Default_Term + " != " + aTerms.terms_id[rowNum]);
			}
			
		}
	
	}
	//formLoaded();
	//alert("initializeTermComboBox finish");
}

///////////////////////////////////////////////////////////////////////////////
//	Function: initializeLocationComboBox()
//	Purpose: initialize the location select box
///////////////////////////////////////////////////////////////////////////////
function initializeLocationComboBox()
{
	//status = "initializeLocationComboBox() start";
	//alert(status);
	aUniqueLocation=new Array();
	//status = "Initializing values to false";
	//alert(status);
	
	for(rowNum = 0; rowNum < aLocations.getRowCount(); rowNum++ )
	{
		aUniqueLocation[aLocations.locations[rowNum]] = false;
		//status = "aUniqueLocation[" + aLocations.LOCATIONS[rowNum]+ "] == false. rowNum == " + rowNum;
		//alert(status);
		
	}
	//status = "Initialized values to false";
	//alert(status);
	
	//status = "Initializing values to true";
	//alert(status);
	
	for(rowNum = 0; rowNum < aClasses.getRowCount(); rowNum++ )
	{
		aUniqueLocation[aClasses.location[rowNum]] = true;
		//status = "aUniqueLocation[" + aClasses.locations[rowNum] + "] == true. rowNum == " + rowNum ;
		//alert(status);
		
		
		//if(aClasses.locations[rowNum] == "Internet")
		//{
			//alert("Internet class tested true.")
		//}

	
	}

	with (document.SelectLists)
	{
		//Clear any current options from the select
		Location.options.length = 0;
		
		//For each unique course record
		//status = "locations.course_sort_value.length == " + locations.course_sort_value.length;
		//alert("Testing locations.course_sort_value.length");
		
		for(rowNum = 0; rowNum < aLocations.getRowCount(); rowNum++)
		{	
			//status = "aUniqueLocation[" + locations.course_sort_value[rowNum] + "] == " + 
			//	aUniqueLocation[aLocations.locations[rowNum]];
			
			//alert("Testing aUniqueLocation[] value");

			if( aUniqueLocation[aLocations.locations[rowNum]] == true )
			{	
				//alert("aUniqueLocation[] value == true");
				//Create new option
				NewOpt = new Option;
				NewOpt.value = aLocations.locations[rowNum];
				NewOpt.text = aLocations.locations_text[rowNum];
				//Add the new object to the select list
				Location.options[Location.options.length] = NewOpt;
				//Select the first value by default		
				
			}
		}
		
		Location.selectedIndex = 0;
	}
	
	//status = "initializeLocationComboBox() finish";
	//delete aUniqueLocation;
	
}
///////////////////////////////////////////////////////////////////////////////
//	Function: initializeAcademicLevelComboBox()
//	Purpose: initialize the academic level select box
///////////////////////////////////////////////////////////////////////////////
function initializeAcademicLevelComboBox()
{
	//status = "initializeAcademicLevelComboBox() start";
	
	uniqueLevel=new Array();
	//status = "Initializing values to false";
	
	for(rowNum = 0; rowNum < aLevels.getRowCount(); rowNum++ )
	{
		uniqueLevel[aLevels.acad_level[rowNum]] = false;
		//status = "uniqueLevel[" + aLevels.ACAD_LEVEL[rowNum]+ "] == false. rowNum == " + rowNum;
	}
	
	with (document.SelectLists)
	{	
		//status = "Initializing values to true";
		
		//status = "Location.options[" + Location.selectedIndex + "].value == " + Location.options[Location.selectedIndex].value;
		//alert("The location value selected.");		
		
		//select which values should be in the drop down list
		for(rowNum = 0; rowNum < aClasses.getRowCount(); rowNum++ )
		{
			if(aClasses.location[rowNum] == Location.options[Location.selectedIndex].value)
			{
				uniqueLevel[aClasses.acad_level[rowNum]] = true;
				//status = "uniqueLevel[" + aClasses.acad_level[rowNum] + "] == true.";
			}
		}
		
		//save previously selected subject
		prevSelLevelValue = Level.options[Level.selectedIndex].value;
		//status = "Level.options[Level.selectedIndex].value == " + Level.options[Level.selectedIndex].value;
		//alert("Saving previously selected level.");
		
		//Clear any current options from the select
		Level.options.length = 0;
		
		//For each course record
		for(rowNum = 0; rowNum < aLevels.getRowCount(); rowNum++ )
		{
			//status = "uniqueLevel[" + aLevels.acad_level[rowNum]+ "] == " + 
			//uniqueLevel[aLevels.acad_level[rowNum]];
			//alert("Listing the uniqueLevel associative array.");
			
			if( uniqueLevel[aLevels.acad_level[rowNum]] == true )
			{
				//Create new option
				//alert("Creating a new option.");
				NewOpt = new Option;
				NewOpt.value = aLevels.acad_level[rowNum];
				NewOpt.text = aLevels.acad_level_text[rowNum];
				//Add the new object to the select list
				Level.options[Level.options.length] = NewOpt;
			}
		}
		//select the value previously selected if possible
		for(rowNum = 0; rowNum < Level.options.length; rowNum++)
		{
			//status = Level.options[rowNum].value + " ? " + prevSelLevelValue;
			//alert("Are these the same?");
			
			if(Level.options[rowNum].value == prevSelLevelValue)
			{
				Level.selectedIndex = rowNum;
				//status = "Level.selectedIndex = " + Level.options[rowNum].value ;
				//alert("Subject set to previous level.");
				break;
			}
			
		}
		//Select the first value if not already set
		if(Level.selectedIndex < 0)
		{
			Level.selectedIndex = 0;
			//status = "Level.selectedIndex = " + rowNum;
			//alert("level set to first on list.");
		}
	}
	
	delete uniqueLevel;
	//status = "initializeAcademicLevelComboBox finish";
}
///////////////////////////////////////////////////////////////////////////////
//	Function: initializeSubjectComboBox()
//	Purpose: initialize the subject select box
///////////////////////////////////////////////////////////////////////////////
function initializeSubjectComboBox()
{
	//alert("initializeSubjectComboBox start");
	
	uniqueSubject=new Array();
	//status = "Initializing values to false";
	
	for(rowNum = 0; rowNum < aSubjects.getRowCount(); rowNum++ )
	{
		uniqueSubject[aSubjects.subjects[rowNum]] = false;
		//status = "uniqueSubject[" + aSubjects.sec_subject[rowNum]+ "] == false. rowNum == " + rowNum;
		//alert("Initializing value to false");
	}
	
	with (document.SelectLists)
	{
		//status = "Location.options[" + Location.selectedIndex + "].value == " + Location.options[Location.selectedIndex].value + " " + "Level.options[" + Level.selectedIndex + "].value == " + Level.options[Level.selectedIndex].value;
		//alert("The location and the level values selected.");	
		
		//select which values should be in the drop down list
		for(rowNum = 0; rowNum < aClasses.getRowCount(); rowNum++ )
		{
			if(aClasses.location[rowNum] == Location.options[Location.selectedIndex].value)
			{
				if(aClasses.acad_level[rowNum] == Level.options[Level.selectedIndex].value)
				{
					uniqueSubject[aClasses.subject[rowNum]] = true;
					//status = "uniqueSubject[" + aClasses.sec_subject[rowNum] + "] == true.";
					//alert("uniqueSubject value set to true.");	
				}
			}
		}
		
		//save previously selected subject
		prevSelSubjectValue = Subject.options[Subject.selectedIndex].value;
		//status = "Subject.options[Subject.selectedIndex].value == " + Subject.options[Subject.selectedIndex].value;
		//alert("Saving previously selected subject.");
		//Clear any current options from the select
		Subject.options.length = 0;	
		
		//return 0;
						
		//For each subject record
		for(rowNum = 0; rowNum < aSubjects.getRowCount(); rowNum++)
		{
			//status = "uniqueSubject[" + aSubjects.sec_subject[rowNum]+ "] == " + uniqueSubject[aSubjects.sec_subject[rowNum]];
			//alert("Listing the uniqueSubject associative array.");
			
			if( uniqueSubject[aSubjects.subjects[rowNum]] == true )
			{
				//Create new option
				NewOpt = new Option;
				NewOpt.value = aSubjects.subjects[rowNum];
				NewOpt.text = aSubjects.subjects_mix[rowNum];
				
				//Add the new object to the select list
				Subject.options[Subject.options.length] = NewOpt;
			}
		}
		
		//select the value previously selected if possible
		for(rowNum = 0; rowNum < Subject.options.length; rowNum++)
		{
			if(Subject.options[rowNum].value == prevSelSubjectValue)
			{
				Subject.selectedIndex = rowNum;
				//status = "Subject.selectedIndex = " + Subject.options[rowNum].text ;
				//alert("Subject set to previous subject.");
				break;
			}
			
		}
		//Select the first value if not already set
		if(Subject.selectedIndex < 0)
		{
			Subject.selectedIndex = 0;
			//status = "Subject.selectedIndex = " + rowNum;
			//alert("Subject set to first on list.");
		}
	}
	
	delete uniqueSubject;
	//status = "initializeSubjectComboBox finish";
}
///////////////////////////////////////////////////////////////////////////////
//	Function: initializeTitleCourseNumComboBox()
//	Purpose: initialize the title and course number select boxes
///////////////////////////////////////////////////////////////////////////////
function initializeTitleAndCourseNumComboBox()
{
	//alert("initializeTitleAndCourseNumComboBox start");
	with (document.SelectLists)
	{
		//Clear any current options from the select
		Title.options.length = 0;
		CourseNumber.options.length = 0;
		
		//Create first title option
		NewTitleOpt = new Option;
		NewTitleOpt.value = "";
		NewTitleOpt.text = "----- All Courses -----";
		
		//Create first course number option
		NewCourseNumOpt = new Option;
		NewCourseNumOpt.value = "";
		NewCourseNumOpt.text = "----- All Courses -----";
		
		//Add the new object to the select list
		Title.options[Title.options.length] = NewTitleOpt;
		CourseNumber.options[CourseNumber.options.length] = NewCourseNumOpt;
		
		for(rowNum = 0; rowNum < aClasses.getRowCount(); rowNum++ )
		{
			if(aClasses.location[rowNum] == Location.options[Location.selectedIndex].value)
			{
				if(aClasses.acad_level[rowNum] == Level.options[Level.selectedIndex].value)
				{
					if(aClasses.subject[rowNum] == Subject.options[Subject.selectedIndex].value)
					{
						//alert("Adding title and course number");
							
						//Create title option
						NewTitleOpt = new Option;
						NewTitleOpt.value = aClasses.short_title[rowNum];
						NewTitleOpt.text = aClasses.short_title[rowNum];
						
						//Create course number option
						NewCourseNumOpt = new Option;
						NewCourseNumOpt.value = aClasses.course_no[rowNum];
						NewCourseNumOpt.text = aClasses.course_no[rowNum];
						
						//Add the new object to the select list
						Title.options[Title.options.length] = NewTitleOpt;
						CourseNumber.options[CourseNumber.options.length] = NewCourseNumOpt;
					}					
				}
			}
			//Select the first value by default		
			Title.selectedIndex = 0;
			CourseNumber.selectedIndex = 0;
		}
	}
	
	//status = "initializeTitleAndCourseNumComboBox finish";
}

///////////////////////////////////////////////////////////////////////////////
//	List creation functions
//	
///////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
//	Select functions
//	
///////////////////////////////////////////////////////////////////////////////
locationSelected = "";


function changeDebug()
{	msgchangeDebug = "Reloading the form based on new debug selection.\n";
	msgchangeDebug += "Please wait until the form reloads before choosing another option.\n";
	msgchangeDebug += "Click Ok to continue.";
	//alert(msgchangeDebug);
	//document.forms[0].submit();
	//document.debug.submit();
}


function changeLocation()
{	
	initializeAcademicLevelComboBox();
	initializeSubjectComboBox();
	initializeTitleAndCourseNumComboBox();
}

function changeLevel()
{	
	initializeSubjectComboBox();
	initializeTitleAndCourseNumComboBox();
}


function changeSubject()
{	
	initializeTitleAndCourseNumComboBox();
}


function changeTitle()
{	
	with (document.SelectLists)
	{
		CourseNumber.selectedIndex = Title.selectedIndex;
	}
}


function changeCourseNum()
{	
	with (document.SelectLists)
	{
		Title.selectedIndex = CourseNumber.selectedIndex;
	}
}