// JavaScript Document
// ***************************************************************************
// * Programmer: Patrick Gunerud
// * Date: 12/21/2002
// *
// * This JavaScript code will control the select boxes on the reserve form
// * to allow them to be dynamically loaded with options based on what they
// * select.
// ***************************************************************************
var day_start = 1;							// Used to address the day of the weeks check boxes and select boxes - what number it starts with
var day_end = 5;							// Used to address the day of the weeks check boxes and select boxes - what number it ends with

var prefix_sel_am_stop = 'selAmFrom';		// The prefixed used on the AM select Location boxes name - ex: name='selTest1' prefix='selTest'
var prefix_sel_am_time = 'selAmTime';		// The prefixed used on the PM select Time boxes name - ex: name='selTest1' prefix='selTest'		
var prefix_chk_am_day = 'chkAm';			// The prefixed used on the AM day check boxes name - ex: name='chkTest1' prefix='chkTest'

var prefix_sel_pm_stop = 'selPmDest';		// The prefixed used on the PM select Location boxes name - ex: name='selTest1' prefix='selTest'
var prefix_sel_pm_time = 'selPmTime';		// The prefixed used on the PM select Time boxes name - ex: name='selTest1' prefix='selTest'
var prefix_chk_pm_day = 'chkPm';			// The prefixed used on the PM day check boxes name - ex: name='chkTest1' prefix='chkTest'	

var reserve_form = null;					// public variable to hold the form object



// This will initalize the from for browsers other than Netscape 4.
// It should be called for the onLoad event of the body tag.
function init_form(form){
	reserve_form = form;	// Set the public form variable
	
	// Only prep the form for browsers other than netscape 4 or older
	if (document.all || document.getElementById) {
		reset_form();
	}
}


// This will set the form to its inital state with all of
// the stop and time select boxes disabled.
function reset_form() {
	var sel_stop_index = 0;
	var sel_time_index = 0;
	
	
	// Disable all of AM select boxes.
	for (var index = day_start; index <= day_end; index++) {
		sel_stop_index = find_element_index(reserve_form, prefix_sel_am_stop + index);
		sel_time_index = find_element_index(reserve_form, prefix_sel_am_time + index);
		
		disable_day(reserve_form.elements[sel_stop_index], reserve_form.elements[sel_time_index]);
	}
	
	// Disable all of PM select boxes.
	for (var index = day_start; index <= day_end; index++) {
		sel_stop_index = find_element_index(reserve_form, prefix_sel_pm_stop + index);
		sel_time_index = find_element_index(reserve_form, prefix_sel_pm_time + index);
		
		disable_day(reserve_form.elements[sel_stop_index], reserve_form.elements[sel_time_index]);
	}
}

// This will write out dummy options in the select boxes to
// allow Netscape 4 to properly show the select boxes and allow
// them to be dynamically modified.
function netscape_select_init(stop_box) {
	var tmp_text = '';
	
	// Be sure to only run this for netscape 4 or older
	if (!document.all || !document.getElementById) {
		if (stop_box){
			tmp_text = '\n<option value="-1">- Select Location -</option>\n';
		}
		else {
			tmp_text = '\n<option value="-1">- Select Time -</option>\n';
		}
		
		// Add dummy options to allow the correct size for netscape
		for (var j = 1; j <= bus_stops.length; j++) {
			tmp_text += '<option value="-1"></option>\n';
		}
		
		// Write out the text that contains the options
		document.writeln(tmp_text);
	}
}


// This will search the elements of the passed form for the passed element.
// This allows addressing the form elements using the index to make changes 
// to the form.
function find_element_index(form, element_name) {
	var tmp_index = -1;
	var tmpobj;
		
	for (i = 0; i < form.length; i++) {
		tmpobj = form.elements[i];
				
		if (tmpobj.name == element_name) {
			tmp_index = i;
			break;
		}
	}
	
	return (tmp_index);
}


// This will add a new option to the passed select box
function add_select_item(sel_box, item_text, item_value, selected) {
	var new_index = sel_box.length++;
		
	sel_box.options[new_index] = new Option();
	sel_box.options[new_index].text = item_text;
	sel_box.options[new_index].value = item_value;
	sel_box.options[new_index].selected = selected;
}


// This will add the items from the passed array to the passed
// select box.  It assumes that arrays will start with index of 1.
function fill_select(sel_box, item_array){
	for (var array_index = 1; array_index < item_array.length; array_index++) {
		add_select_item(sel_box, item_array[array_index], item_array[array_index], false);
	}
}


// This will remove all the items in the passed select box
function clear_select(sel_box){
	for (var sel_index = sel_box.length; sel_index >= 0; sel_index--){
		sel_box.options[sel_index] = null;
	}
}


// This will clear all entries in the select box then add the
// directions back into the select box then disable it.
// The stop_box is a boolean variable identifying it as a stop
// select box or not.
function reset_select_box(sel_box, stop_box) {
	sel_box.disabled = false;
	clear_select(sel_box);
	
	// Readd the directions for either the stop select or time select
	if (stop_box) {
		add_select_item(sel_box, '- Select Location -', '-1', true);
	}
	else {
		add_select_item(sel_box, '- Select Time -', '-1', true);
	}
	
	sel_box.disabled = true;
}


// This will enable and fill the passed location select box
function enable_day(sel_stop_box) {
	// Only enable the location select box so they have to select a location first
	reset_select_box(sel_stop_box, true);
	sel_stop_box.disabled = false;
	
	// Fill the from select box using the bus_stops array
	fill_select(sel_stop_box, bus_stops);
}


// This will clear out the entries for the location and time select boxes
// then disable them.
function disable_day(sel_stop_box, sel_time_box) {
	reset_select_box(sel_stop_box, true);
	reset_select_box(sel_time_box, false);
}


// This will either enable the AM location select box or disable it based on
// the check box for the given day.  This should be called on the check box
// click event.
function change_am_day(day_index){
	var chk_index = find_element_index(reserve_form, prefix_chk_am_day + day_index);
	var sel_stop_index = find_element_index(reserve_form, prefix_sel_am_stop + day_index);
	var sel_time_index = find_element_index(reserve_form, prefix_sel_am_time + day_index);
	
	if (reserve_form.elements[chk_index].checked == true) {
		enable_day(reserve_form.elements[sel_stop_index]);
	}
	else {
		disable_day(reserve_form.elements[sel_stop_index], reserve_form.elements[sel_time_index]);
	}
}


// This will change the contents of the AM time select box for the given day.
// The contents will be filled depending on what stop was selected from the
// list.  This should be called from the location select box change event.
function change_am_time(day_index){
	var sel_stop_index = find_element_index(reserve_form, prefix_sel_am_stop + day_index);
	var sel_time_index = find_element_index(reserve_form, prefix_sel_am_time + day_index);
	var stop_index = reserve_form.elements[sel_stop_index].selectedIndex;
	
	// Fill the time select box based on the stop
	if ((stop_index != 0) && (reserve_form.elements[sel_stop_index].value != null)) {	
		reset_select_box(reserve_form.elements[sel_time_index], false);
		reserve_form.elements[sel_time_index].disabled = false;
		
		// Fill the Time select box using the correct row from am_depart_times
		fill_select(reserve_form.elements[sel_time_index], am_depart_times[stop_index]);
	}
	else {	// Unknown stop was selected, just reset the time select box
		reset_select_box(reserve_form.elements[sel_time_index], false);
		reserve_form.elements[sel_stop_index].selectedIndex = 0;
	}
}


// This will either enable the PM location select box or disable it based on
// the check box for the given day.  This should be called on the check box
// click event.
function change_pm_day(day_index){
	var chk_index = find_element_index(reserve_form, prefix_chk_pm_day + day_index);
	var sel_stop_index = find_element_index(reserve_form, prefix_sel_pm_stop + day_index);
	var sel_time_index = find_element_index(reserve_form, prefix_sel_pm_time + day_index);
	
	if (reserve_form.elements[chk_index].checked == true) {
		enable_day(reserve_form.elements[sel_stop_index]);
	}
	else {
		disable_day(reserve_form.elements[sel_stop_index], reserve_form.elements[sel_time_index]);
	}
}


// This will change the contents of the PM time select box for the given day.
// The contents will be filled depending on what stop was selected from the
// list.  This should be called from the location select box change event.
function change_pm_time(day_index){
	var sel_stop_index = find_element_index(reserve_form, prefix_sel_pm_stop + day_index);
	var sel_time_index = find_element_index(reserve_form, prefix_sel_pm_time + day_index);
	var stop_index = reserve_form.elements[sel_stop_index].selectedIndex;
	
	// Fill the time select box based on the stop
	if ((stop_index != 0) && (reserve_form.elements[sel_stop_index].value != null)) {	
		reset_select_box(reserve_form.elements[sel_time_index], false);
		reserve_form.elements[sel_time_index].disabled = false;
		
		// Fill the Time select box using the correct row from pm_depart_times
		fill_select(reserve_form.elements[sel_time_index], pm_depart_times[stop_index]);
	}
	else {	// Unknown stop was selected, just reset the time select box
		reset_select_box(reserve_form.elements[sel_time_index], false);
		reserve_form.elements[sel_stop_index].selectedIndex = 0;
	}
}

// returns the array number of the selected radio button or -1 if no button is selected
function getSelectedRadio(buttonGroup)
{
   if (buttonGroup[0])
   { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) { return i }
      }
   } 
   else 
   {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
}

// returns the value of the selected radio button or "" if no button is selected
function getSelectedRadioValue(buttonGroup)
{
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) { return ""; }
   else 
   {
      if (buttonGroup[i]) 
	  { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } 
	  else 
	  { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
}