<!--
//Used in the Good Shepherd's Studio (http://www.goodshepherdstudio.org)

//////////////////////////////////////////////////////////////////////////
//Open the appointment box popup to allow the user to select times for a
//particular appointment date
function addAppointment() { appointmentpopup("AppointmentBox.php"); }

//////////////////////////////////////////////////////////////////////////
//Remove an appointment from the "Your Appointment Box" box
function removeAppointment(appointmentBoxObj, removeButtonObj)
  {
  //is an appointment selected?
  if(appointmentBoxObj.selectedIndex == -1) return;

  //delete selected appointment
  deleteSelectedValue(appointmentBoxObj);

  //disable remove button if appropriate
  if(appointmentBoxObj.options.length == 0)
    removeButtonObj.disabled = true;
  }

//////////////////////////////////////////////////////////////////////////
//Delete the selected value from the options list of the given select box
function deleteSelectedValue(appointmentBoxObj)
  {
  //create temporary swap-target arrays
  var valueArray = new Array();
  var textArray = new Array();

  //copy the to-be-remaining appointment list from the appointment box to
  //the swap-target arrays
  var j = 0;
  for(var i=0; i < appointmentBoxObj.options.length; i++)
    {
    //do not save the selected value & text (since we are trying to remove it)
    if(i != appointmentBoxObj.options.selectedIndex)
      {
      //save this value & text
      valueArray[j] = appointmentBoxObj.options[i].value;
      textArray[j] = appointmentBoxObj.options[i].text;
      j++;
      }
    }

  //delete the selected appointment by reloading the appointment box with the
  //swap-target arrrays
  appointmentBoxObj.options.length--;
  for(var i=0; i < appointmentBoxObj.options.length; i++)
    {
    appointmentBoxObj.options[i].value = valueArray[i];
    appointmentBoxObj.options[i].text = textArray[i];
    }
  }

//////////////////////////////////////////////////////////////////////////
//Is the argument month+day+year+time appointment in the appointment box
//already?
function inAppointmentBox(appointmentBoxObj, month, day, year, time)
  {
  //go through all the appointments in the appointment box
  for(var i=0; i < appointmentBoxObj.options.length; i++)
    {
    //get the appointment string
    var appointStr = appointmentBoxObj.options[i].value;

    //extract the month, day and year from the appointment string
    var halfIndex1 = appointStr.indexOf("/");
    var halfIndex2 = appointStr.indexOf("/", halfIndex1+1);
    var halfIndex3 = appointStr.indexOf(" ");
    var inmonth = appointStr.substring(0, halfIndex1);
    var inday   = appointStr.substring(halfIndex1+1, halfIndex2);
    var inyear  = appointStr.substring(halfIndex2+1, halfIndex3);

    //is this date currently selected?  If so, check the time, otherwise I don't care
    if((inmonth == month) && (inday == day) && (inyear == year))
      {
      //get the time string from the appointment string
      var timeStr = appointStr.substr(appointStr.indexOf(" from ") + 6);

      //do the times match?  If so, this appointment is in the appointment box
      if(timeStr == time)
        return true;
      }
    }

  //the appointment was not found in the appointment box
  return false;
  }

//////////////////////////////////////////////////////////////////////////
//Reload the "pick a date" action page in the popup window with the selected
//year
function GoYear(url) { window.top.location.href = url.options[url.selectedIndex].value; }

//////////////////////////////////////////////////////////////////////////
//The user has clicked the "Ok" button in the appointment box popup.  So,
//add the selected appointment times (if any) to the "Your Appointment Box"
//form control and then close the popup window.
function DoOk(appointmentform, month, day, year)
{
  //ASSUMPTIONS: (1) the name of the parent form is "bookform", (2) the name of the "Your
  //Appointment Box" form object is "appointments" and (3) the name of the remove button
  //form object is "removeButton"

  //get the form objects from both forms
  if(window.self.opener) window.close();
  yourAppointmentBoxObj = window.self.opener.document.bookform.appointments;
  removeButtonObj = window.self.opener.document.bookform.removeButton;
  availableTimesObj = appointmentform.timeslots;

  //go through all the selected times and add each to the "Your Appointment Box" list
  for(var i=0; i<availableTimesObj.options.length; i++)
  {
    //was this time selected by the user?
    if(!availableTimesObj.options[i].selected)
      continue;

    //don't add a duplicate appointment
    var time = availableTimesObj.options[i].value;
    if(!inAppointmentBox(yourAppointmentBoxObj, month, day, year, time))
    {
      //add a new appointment slot to the "Your Appointment Box"
      var oldOptionLength = yourAppointmentBoxObj.options.length;
      yourAppointmentBoxObj.options.length = oldOptionLength + 1;

      //add the appointment to the "Your Appointment Box"
      yourAppointmentBoxObj.options[yourAppointmentBoxObj.options.length - 1].value = (month+"/"+day+"/"+year+" from "+time);
      yourAppointmentBoxObj.options[yourAppointmentBoxObj.options.length - 1].text = (month+"/"+day+"/"+year+" from "+time);

      //enable the remove appointment button
      removeButtonObj.disabled = false;
    }
  }

  //close the popup window
  window.close();
}
//-->
