//This library of JavaScript functions was developed by Jessica Myrbo
//laste edited: 19.1.2004


//make_date makes a date (e.g. 2002-11-13) from month, day, and year dropboxes in the_form
function make_date(the_form) {
  the_date = the_form.year.value
    + "-" +  the_form.month.value 
    + "-" + the_form.day.value;

  return the_date;
}



//set_zeroth_to_date assembles  date from the dropboxes and sets the value of sql[date]
//we have to reference the sql[date] field as the zeroth element of the form;
//js can't handle the php $_POST arrays--[] in names trip it up 
function set_zeroth_to_date(the_form) {
  the_form[0].value = make_date(the_form);
} //end verify_it

//set_zeroth_to_dates stores string of comma-separated dates to zeroth element
function set_zeroth_to_dates(the_form) {
  var the_string = "";
  var opt_obj = the_form.dates.options;

  //add all but last value to string with commas
  for (i=0; i < (opt_obj.length)-1; i++) {
    the_string = the_string + opt_obj[i].value; 
    the_string = the_string + ",";
  } //end for i 0 thru # options-1

  //add last value w/o comma
  the_string = the_string + opt_obj[opt_obj.length-1].value;

  the_form[0].value = the_string;
}



function add_date(the_form) {
  the_date = make_date(the_form);

  //add date as an option in the select box
  the_form.dates.options[the_form.dates.options.length] = new Option(the_date,the_date);

  //re-order select box
  sort_select(the_form.dates);
} //end add_date




function sort_select(the_select) {
  var the_options = new Array();
  var opt_obj = new Object();

  for (i=0; i < the_select.options.length; i++) {
    opt_obj = the_select.options[i];	
    the_options[i] = new Option(opt_obj.text, opt_obj.value);
  } //end for i 0 thru # options

  the_options = the_options.sort(sort_by_value); 

  for (i=0; i < the_options.length; i++) {
    the_select.options[i] = new Option(the_options[i].text, the_options[i].value);
  } //end for i 0 through # options

} //end sort_select




//sort_by_value gets called by the sort method to sort the obj by obj.value 
function sort_by_value(a,b) {
 if ((a.value) < (b.value))
   return -1;
 if ((a.value) > (b.value))
   return 1;
 return 0;
} //end sort_by_value




//remove_selected removes selected options from a text box
function remove_selected(the_select) { 

  for (i=0; i < the_select.options.length; i++) { 
    var the_option = the_select.options[i]; 
    if (the_option.selected) 
      the_select.options[i] = null; 
    } //end for i 0 thru # of options

    the_select.selectedIndex = -1; 

} //end remove_selected




//tries to reload the window if Nav4 resized
function MM_reloadPage(init) {  
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}





/* >>>>>Everything the New Window needs<<<<< */


//index names for the new_window function
        var win_name_i = '1';
        var win_url_i = '2';
        var win_width_i = '3';
        var win_height_i = '4';

/*Thus, the winArray format is:
        winArray[0] = 'Array!; to indicate that it's an array, ya hey
        winArray[win_name_i] = the name for the window
        winArray[win_url_i] = the url for the window
        winArray[win_width_i] = the width of thw window
        winArray[win_height_i] = the height of the window */

//generic tiny popup window
var prompt_winArray = new Array(5);
        prompt_winArray[0] = 'Array!';
        prompt_winArray[win_url_i] = '';
        prompt_winArray[win_name_i] = 'tinywin';
        prompt_winArray[win_width_i] = 250;
        prompt_winArray[win_height_i] = 100;



// new_window function: gets passed properties of a new window and opens it
// All windows are scrollable, resizable, and have no toolbars.

function new_window(name_or_winArray,win_url,win_width,win_height) {

  //if any data was passed, the window is not just a generic  
  if (name_or_winArray) {

    //store array values to width, height, name, and probably url
    //test for the 'Array!' 0th entry to see if it's a genuine array (vs. plain string)
    if (name_or_winArray[0]=='Array!')  {
      var win_name = name_or_winArray[win_name_i];

      //allow a passed url to override the array's url
      if (!win_url)
	var win_url = name_or_winArray[win_url_i];
      var win_width = name_or_winArray[win_width_i];
      var win_height = name_or_winArray[win_height_i];
    } //end if name_or_winArray[0] is the "Array!" flag

    //if win_name isn't a predefined array, it's the name   
    else
      var win_name = name_or_winArray;
  } //end if name_or_winArray
  
  //if there's no name_or_winArray
  else
    var win_name = 'littlewin';
  //default window name when passed no arguments
  
  
  //make the window blank if no url was passed
  if (!win_url)
    var win_url = '';
  //default width is 470
  if (!win_width)
    var win_width = 470;
  //make the window square if no height was specified
  if (!win_height)
    var win_height = win_width;

  
  
  //make a string for the third arg of window.open
  var thirdarg = 'scrollbars=yes,resizable=yes,width=' + win_width + ',height=' + win_height;
  
  //open the window
  var window_object = window.open(win_url,win_name,thirdarg);
  
  //ensure orig. window's unchanged... or prevents an alert... or something... when merely opening a pre-existing doc
  if (win_url)
    void('');
  else
    //pass the window's identity back so it can be defined as a variable in the caller who's writing a new doc
    return window_object;
} //end new_window




//new_browser function: opens a new browser with toolbars
function new_browser(win_name,win_url) {

  //make a string for the third arg of window.open
  var thirdarg = 'scrollbars=yes,toolbar=yes,resizable=yes';
  
  //open the window
  var window_object = window.open(win_url,win_name,thirdarg);
  
  //ensure orig. window's unchanged... or prevents an alert... or something... when merely opening a pre-existing doc
  if (win_url)
    void('');
  else
    //pass the window's identity back so it can be defined as a variable in the caller who's writing a new doc
    return window_object;

}
