/* JavaScript functions to support /circuit/advance_search.jsp */

   /*-------------------------------------------------------------
   *  function:	ValidateDate()
   *  input:	DateString: Date string in the format "99-99-9999"
   *
   *  purpose:	Verify the date is entered correctly, display alert
   *			message if it isn't correct.
   *----------------------------------------------------------------*/

function ValidateDate( DateString )
{
  var ErrMessage = ""

  if ( DateString.length == 10 )
  {
    if ( (DateString.charAt(2) == '-' &&
          DateString.charAt(5) == '-') ||
         (DateString.charAt(2) == '/' &&
          DateString.charAt(5) == '/'))
    {
      Month = parseInt( DateString.substring(0,2),10 )
      Day   = parseInt( DateString.substring(3,6),10 )
      Year  = parseInt( DateString.substring(6,10),10 )

      if ( Year > 1899 && Year < 2051 )
      {
        if ( Day > 0  )
        {
          if ( Month == 4 || Month == 6 || Month == 9 || Month == 11 )
          {
            if ( Day > 30 )
            {
              ErrMessage = "Invalid day of month."
            }
          } 
          else if ( Month == 1 || Month == 3 || Month == 5 || Month == 7 || 
                    Month == 8 || Month == 10 || Month ==  12 )
          {
            if ( Day > 31 )
            {
              ErrMessage = "Invalid day of month."	
            }
          }
          else if ( Month == 2 )
          {
            if ( Day > 28 )
            {
              ErrMessage = "Invalid day of month."
            }
          }
          else
          {
            ErrMessage = "Invalid month."
          }
        }
        else
        {
          ErrMessage = "Invalid day of month."
        }
      }
      else
      {
        ErrMessage = "Invalid year."
      }
    }
    else
    {
      ErrMessage = "Invalid format."
    }
  }
  else
  {
    ErrMessage = "Invalid format."
  }

  /* some error condition */

  if ( ErrMessage != "" )
  {
    alert( DateString + " is not a valid date. Re: " + ErrMessage )
    return false
  }

  return true

} /* end of ValidateDate() function */

   /*----------------------------------------------------------
   * 	function:	ValidateFormsInput()
   *-----------------------------------------------------------*/

function ValidateFormsInput()
{
/*----------------------------------------------------
* Make sure they entered criteria in a least one field 
*------------------------------------------------------*/

  if ( document.FormsForm.FormNumber.options[0].selected &&
       document.FormsForm.beg_date.value.length == 0 &&
       document.FormsForm.end_date.value.length == 0 &&
       document.FormsForm.StatuteCite.options[0].selected &&
       document.FormsForm.Category.options[0].selected &&
       document.FormsForm.FormName.options[0].selected &&
       document.FormsForm.Language.options[0].selected &&
       document.FormsForm.Format.options[0].selected)
  {
    alert( "You have not entered any search criteria" )
    return false
  }

/*----------------------------------------------------------------------------
* Validate date fields.
*----------------------------------------------------------------------------*/
  if ( document.FormsForm.beg_date.value.length != 0 )
  {	
    if ( !ValidateDate( document.FormsForm.beg_date.value ) )	
    {
      return false
    }

    if ( document.FormsForm.end_date.value.length == 0 )
    {
      /* copy begin date to end date */

      document.FormsForm.end_date.value = document.FormsForm.beg_date.value
    }
    else if ( !ValidateDate( document.FormsForm.end_date.value ) )
    {
      return false
    }
  }	
  else
  {
    /*---------------------------------------------
    * Cannot enter an end date without a begin date 
    *----------------------------------------------*/

    if ( document.FormsForm.end_date.value.length != 0 )
    {
      alert( "You must enter a beginning date in order to search on release date." )

      return false
    }
  }

  return true

} /* end of function */
