/* =============================================================================
* forms.js
*
* Advanced Form Handling API
*
* Functions included:
*   copy_values
*
* Developed by Jake Kronika <jkronika@imagescape.com>
* For Imaginary Landscape, LLC <www.imagescape.com>
* Copyright (c) 2006
*
* Last updated 2006.03.10
*
* Reuse or modification without permission is prohibited.
* --------------------------------------------------------------------------- */




// call in iscapeJS javascript library method object
var iscapeJS = iscapeJS || {};

// define a new block of methods for form handling
iscapeJS.forms = {

  /* ===========================================================================
  *
  * iscapeJS.forms.copy_values
  *
  * DEPENDENCIES:
  *   utility.js
  *   dom-core.js
  *
  * Duplicate values within a form for fields with identical names following the
  * provided prefixes prefix1 and prefix2
  *
  * parameters:
  *   frm : the form containing fields for value copy
  *   prefix1 : string identifying the prefix attached to fields to copy from
  *   prefix2 : string identifying the prefix attached to fields to copy to
  *   * always of the form 'prefix' for elements of name 'prefixelement'
  *
  * USAGE:
  *
  * call this method in the onclick event handler for a button (or as part of
  * an onclick handler for a checkbox) as:
  *   iscapeJS.forms.copy_values(this.form, 'prefix1', 'prefix2')
  *
  * ------------------------------------------------------------------------- */
  'copy_values' : function(frm, prefix1, prefix2) {
    if (is_valid_string(prefix1)
        && is_valid_string(prefix2)) {
      if (!frm.nodeName) {
        if (is_valid_var_string(frm)) {
          frm = eval(frm);
        } else if (is_valid_string(frm)) {
          var tmp_frm = get_element_by_id(frm);

          if (!tmp_frm) {
            tmp_frm = document.forms[frm];
          }

          if (!tmp_frm) {
            var frms = get_elements_by_tag('form');
            for (var i = 0; i < frms.length; i++) {
              if (get_attribute(frms[i], 'name') == frm) {
                tmp_frm = frms[i];
                break;
              }
            }
          }

          frm = tmp_frm;
        }
      }

      if (frm && frm.nodeName.toLowerCase() == 'form') {
        var elems = get_elements_by_tag('*', frm);

        for (var i = 0; i < elems.length; i++) {
          var ename = get_attribute(elems[i], 'name'),
              name_re = new RegExp('^'+prefix1);

          if (ename
              && ename.match
              && ename.match(name_re)) {
            ename = prefix2 + ename.substr(prefix1.length);

            if (frm[ename]) {
              set_attribute(
                frm[ename], 'value', get_attribute(elems[i], 'value'));
            }
          }
        }
      }
    }

    return frm;
  },

  'text_swap' : function(fld, blur) {
    var val = fld.value,
        ttl = fld.title;

    if (blur && val == '') {
      val = ttl;
    } else if (!blur && val == ttl) {
      val = '';
    }

    fld.value = val;

    return true;
  }
};

// backwards compatibility
var copy_values = iscapeJS.forms.copy_values;
