/**
 * @file
 * Form utilities.
 *
 * @author Shannon M. Rause <shannon.rause@creativeflavor.com>
 * @version $Revision: 1.3 $
 * @version $Name:  $
 * @version $Id: Form.js,v 1.3 2006/09/05 18:23:07 smr Exp $
 *
 * Copyright 2005-2006 Creative Flavor, Inc. All rights reserved.
 * 
 * Please see the LICENSE file for specific licensing information.
 * If you did not receive this file or are unable to locate it, 
 * please contact licensing@creativeflavor.com.
 */
/**
 * Form class.
 *
 * @public
 */
function Form()
{
} // Form


/**
 * This creates a dropdown for selecting a month's day.
 *
 * @param name       Name/id of select.
 * @param selected   Day that is to be selected by default.
 *
 * @return Created select control.
 *
 * @public
 */
Form.createDaySelect = function(name,
                                selected)
{
   var select = document.createElement('select');
   select.setAttribute('name',
                       name);
   select.setAttribute('id',
                       name);

   var option;

   for (var i = 1; i < 32; i++)
   {
      option = document.createElement('option');

      if (i < 10)
      {
         i = '0' + i;
      } // if

      option.setAttribute('value',
                          i);

      if (i == selected)
      {
         option.setAttribute('selected',
                             'selected');
      } // if

      option.appendChild(document.createTextNode(i));
      select.appendChild(option);
   } // for

   return (select);
} // createDaySelect


/**
 * This creates a dropdown for selecting a month.
 *
 * @param name       Name/id of select.
 * @param selected   Month that is to be selected by default.
 *
 * @return Created select control.
 *
 * @public
 */
Form.createMonthSelect = function(name,
                                  selected)
{
   var select = document.createElement('select');
   select.setAttribute('name',
                       name);
   select.setAttribute('id',
                       name);
   
   var option;
   var months = {
      '01':'Jan',
      '02':'Feb',
      '03':'Mar',
      '04':'Apr',
      '05':'May',
      '06':'Jun',
      '07':'Jul',
      '08':'Aug',
      '09':'Sep',
      '10':'Oct',
      '11':'Nov',
      '12':'Dec'
   };

   for (var month in months)
   {
      option = document.createElement('option');
      option.setAttribute('value',
                          month);

      if (month == selected)
      {
         option.setAttribute('selected',
                             'selected');
      } // if

      option.appendChild(document.createTextNode(months[month]));
      select.appendChild(option);
   } // for

   return (select);
} // createMonthSelect


/**
 * This creates a dropdown for selecting a year.
 *
 * @param name       Name/id of select.
 * @param start      Starting year.
 * @param end        Ending year.
 * @param selected   Year that is to be selected by default.
 *
 * @return Created select control.
 *
 * @public
 */
Form.createYearSelect = function(name,
                                 start,
                                 end,
                                 selected)
{
   var select = document.createElement('select');
   select.setAttribute('name',
                       name);
   select.setAttribute('id',
                       name);

   for (var i = start; i <= end; i++)
   {
      option = document.createElement('option');
      option.setAttribute('value',
                          i);
         
      if (i == selected)
      {
         option.setAttribute('selected',
                             'selected');
      } // if

      option.appendChild(document.createTextNode(i));
      select.appendChild(option);
   } // for

   return (select);
} // createYearSelect


/**
 * Focuses the first element of a form.
 *
 * @param f Form to focus first element of.  If null, the first form will be
 *          used.
 *
 * @public
 */
Form.focusFirst = function(f)
{
   if (!f)
   {
      if (document.forms.length)
      {
         f = document.forms[0];
      } // if
      else
      {
         return;
      } // else
   } // if

   for (var i = 0; i < f.elements.length; i++)
   {
      if ((f.elements[i].type != 'hidden') &&
          (f.elements[i].type != 'button') &&
          (f.elements[i].type != 'submit') &&
          (!f.elements.disabled))
      {
         f.elements[i].focus();
         return;
      } // if
   } // for
} // focusFirst


/**
 * Submits the current form when the enter key is pressed on the element.
 *
 * @param input   Input field.
 * @param event   Key press event.
 *
 * @return true to propogate event (not submitted), false to not (submitted).
 *
 * @public
 */
Form.submitOnEnter = function(input,
                              event)
{
   var keycode;

   if (window.event)
   {
      keycode = window.event.keyCode;
   } // if
   else if (event)
   {
      keycode = event.which;
   } // else if
   else
   {
      return (true);
   } // else

   if (keycode == 13)
   {
      input.form.submit();
      return (false);
   } // if
   
   return (true);
} // submitOnEnter


/**
 * Initializes a counter for a textarea that is limited in size by JS.
 *
 * @param inputId Id of textarea.
 * @param countId Id of counter.
 * @param limit   Size textarea is limited to.
 * 
 * @public
 */
Form.textareaCountInit = function(textareaId,
                                  countId,
                                  limit)
{
   var ta = document.getElementById(textareaId);
   var elem = document.getElementById(countId);

   if ((ta) &&
       (elem))
   {
      Utils.removeChildren(elem);
      elem.appendChild(document.createTextNode(limit - ta.value.length));
   } // if
} // textareaCountInit


/**
 * Limits textarea to a specific number of characters.
 *
 * @param textarea   Textarea.
 * @param limit      Maximum number of characters.
 * @param id         Id of element (span usually) that contains a text display
 *                      of the number of characters. null (or undefined) for none.
 */
Form.textareaLimit = function(textarea,
                              limit,
                              id)
{
   if (textarea.value.length > limit)
   {
      // too long - trim.
      textarea.value = textarea.value.substring(0,
                                                limit);
   } // if

   if (id)
   {
      var elem = document.getElementById(id);

      if (elem)
      {
         Utils.removeChildren(elem);
         elem.appendChild(document.createTextNode(limit - textarea.value.length));
      } // if
   } // if
} // textareaLimit
