function checkform (form)
{
  var errorstring = "";
  with (form) {
    if(validate_required(course))
      errorstring += "Course ";
    if(validate_required(country))
      errorstring += "Location ";
    if(validate_required(date))
      errorstring += "Date ";
  }

  if (errorstring) {
    alert( "The following fields are missing: "+errorstring );
    return false ;
  }
  return true ;
}

function validate_required(field)
{
  with (field)
  {
    return (value==null||value=="");
  }
}

function fillCourseBox(cbox,course,program)
{
  toggle = 0;
  p = eval(program);
  cbox.length = 1;
  for (var i = 0; i < p.length - 1; i++)
  {
    if ( p[i] != "" ) {
      try {
        if (eval('Course'+p[i]+'[0]') != '') {
  //      alert(p[i]);
          cbox.length++;
          try {
            cbox.options[cbox.length-1] = new Option( eval('Course'+p[i]+'[0]') + " - " + eval('Course'+p[i]+'[1]'));          
            cbox.options[cbox.length-1].value = p[i];
            if (course == p[i])
              toggle = cbox.length - 1;
          } catch(e) { }
        }
      } catch(e) {}
    }
  }
  if (cbox.selectedIndex != toggle)
    cbox.selectedIndex = toggle;
}

function fillLocationBox(lbox,location,course)
{
  locations = getLocations(course);
  lbox.length = 1;
  toggle = 0;
  for (i = 0; i < locations.length; i++)
  {
    try{
      eval('Loc'+ locations[i] + "[1]");
      lbox.length++;
      lbox.options[lbox.length - 1] = new Option( eval('Loc'+ locations[i] + "[1]"));
      lbox.options[lbox.length - 1].value = locations[i];
      if (locations[i] == location)
	toggle = lbox.length - 1;
    }
    catch (e){};
  }
  if (lbox.selectedIndex != toggle)
    lbox.selectedIndex = toggle;
}

function fillDateBox(dbox, date, location, course)
{
  dbox.length = 1;
  toggle = 0;
  for (var s = 1;s < schedcount; s++)
  {
     
    if (
      (eval("Sched" + s + "[1]") == course)
      &&
      (eval("Sched" + s + "[2]") == location)
    )
    {
      // enough rope to shoot yourself in the foot where the 
      // lake is the deepest
      _date = eval("Sched" + s + "[0]");
      dbox.length++;
      _duration = ((Math.ceil(eval("Course"+course+"[2]"))-1)*24*60*60*1000);
      _year   = _date.substr(0,4);
      _month  = _date.substr(4,2);
      _day    = _date.substr(6,2);
      _start  = new Date(_year,eval(_month-1),_day);
      _end    = new Date(_start.getTime() + _duration);
      _fstart = _start.formatDate("Y/m/d");
      _fend   = _end.formatDate("Y/m/d")
      if (_fstart != _fend) {
        dbox.options[dbox.length - 1] = new Option(_fstart+" - "+_fend);
      } else {
        dbox.options[dbox.length - 1] = new Option(_fstart);      
      }
      dbox.options[dbox.length - 1].value = _date;
      if (_date == date)
        toggle = dbox.length - 1;
    }
  }
  if (dbox.selectedIndex != toggle)
    dbox.selectedIndex = toggle;
}

function getSelection(box)
{
  try
  {
    selection = box.options[box.selectedIndex].value;
    space = selection.indexOf(" ");
    if (space > 0)
    {
      selection = box.options[box.selectedIndex].value.substring(0,space);
    }
    return selection;
  }
  catch (e) {}
}

function getLocations(selection)
{
  i = 0;
  var _locations = new Array();
  var locations  = new Array();

  // Get locations from course descriptions and write them into a sorted array
  // ::inArray() anyone?
  // alert(selection);
  for (var s = 1;s < schedcount; s++)
  {
    if (selection == (eval('Sched'+ s +'[1]')))
    {
      _locations[i] = eval('Sched'+ s +'[2]');
      i++;
    }
  }
  _locations.sort();


  i = 0;
  // Throw out duplicates
  for (var s = 0; s< _locations.length; s++)
  {
    if (_locations[s] != _locations[s-1])
    {
      locations[i] = _locations[s];
      i++;
    }
  }
  return locations;
}

