/**
 * @file
 * @author Shannon M. Rause <shannon.rause@creativeflavor.com>
 * @version $Revision: 1.9 $
 * @version $Name: ARTISTICUSA_2011-06-20_2 $
 * @version $Id: Form.js,v 1.9 2008/05/03 01:45:38 smr Exp $
 *
 * These files are copyrighted to Creative Flavor Inc. and are
 * subject to the terms of the applicable Service Agreement.
 * If no service agreement is available you must contact us at
 * legal@creativeflavor.com or 303-379-9450.
 * 
 * These files may be watermarked to ensure traceability.
 */
/**
 * Form class.
 */
function Form()
{
} // Form


/**
 * Clears a form.
 * 
 * @param name Form name to clear. If omitted, it will just do the first one.
 */
Form.clear = function(name)
{
   if ((name == undefined) ||
       (name == null))
   {
      var form = document.forms[0];
   } // if
   else
   {
      var form = document.forms[name];
   } // else

   if (form)
   {
      for (var i = 0; i < form.elements.length; i++)
      {
         if (form.elements[i].tagName == 'INPUT')
         {
            var type = form.elements[i].getAttribute('type');

            if ((type != 'button') &&
                (type != 'hidden') &&
                (type != 'submit'))
            {
               form.elements[i].value = '';
            } // if
         } // if
         else if (form.elements[i].tagName == 'SELECT')
         {
            form.elements[i].selectedIndex = 0;
         } // else if
         else if (form.elements[i].tagName == 'TEXTAREA')
         {
            form.elements[i].value = '';
         } // else if
      } // for
   } // if
} // clear


/**
 * 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.
 */
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.
 */
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.
 */
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 name Name of form to focus first element of.  If null (or not found), the first form
 *             will be used.
 */
Form.focusFirst = function(name)
{
   if (!document.forms.length)
   {
      return;
   } // if

   if ((name) &&
       (document.forms[name]))
   {
      var f = document.forms[name];
   } // if
   else
   {
      var f = document.forms[0];
   } // else

   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


/**
 * Gets the form element for the given element which must be an 
 * input that is in the actual form.
 *
 * @param elem Element to get form for.
 *
 * @return Form element or null if cant find.
 */
Form.getFromElement = function(elem)
{
   for (var i = 0; i < document.forms.length; i++)
   {
      for (var j = 0; j < document.forms[i].elements.length; j++)
      {
         if (document.forms[i].elements[j] == elem)
         {
            return (document.forms[i]);
         } // if
      } // for
   } // for

   return (null);
} // getFromElement


/**
 * 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).
 */
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.
 */
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

