// File Name : form.js
// File Type : Java script
// Function  : html form utility functions
// Date      : 02.NOV.2001
// Author    : Joseph Wong

/*
 * Clear all html fields with name is pas_clr_flds
 *
 * @param po_form       html form object
 * @param pas_clr_flds  name of html fields to be cleared
 */
function j_clr_form_flds(po_form, pas_clr_flds) {

  for (ji_i = 0; ji_i < pas_clr_flds.length; ji_i++) {

    jo_obj = po_form.elements[pas_clr_flds[ji_i]];

    if (typeof(jo_obj) == 'undefined') {
      continue;
    }

    // get field type
    if (typeof(jo_obj.length) == 'undefined') {
      ji_length = 1;
      js_type   = jo_obj.type;
    } else {
      if (typeof(jo_obj[0].type) == 'undefined') {
        ji_length = 1;
        js_type   = jo_obj.type;
      } else {
        ji_length = jo_obj.length;
        js_type   = jo_obj[0].type;
      }
    }

    // clear fields for text box, check box, radio button, ddbox, list box, hidden fields
    for (ji_j = 0; ji_j < ji_length; ji_j++) {

      if (ji_length == 1) {
        jo_tmp_obj = jo_obj;
      } else {
        jo_tmp_obj = jo_obj[ji_j];
      }

      if ((js_type == 'text') || (js_type == 'textarea') || (js_type == 'password')) {

        jo_tmp_obj.value = '';

      } else if (js_type == 'checkbox') {

        jo_tmp_obj.checked = false;

      } else if (js_type == 'radio') {

        jo_tmp_obj.checked = false;

      } else if (js_type == 'select-one') {

        for (ji_k = 0; ji_k < jo_tmp_obj.options.length; ji_k++) {
          if (jo_tmp_obj.options[ji_k].value == '') {
            jo_tmp_obj.options[ji_k].selected = true;
          }
        }
      } else if (js_type == 'select-multiple') {

        for (ji_k = 0; ji_k < jo_tmp_obj.options.length; ji_k++) {
          jo_tmp_obj.options[ji_k].selected = false;
        }

      } else if (js_type == 'hidden') {

        jo_tmp_obj.value = '';
      }
    }
  }
}

/*
 * blur a form object and remove selection on the field, it is needed to 
 * remove selection since if it's selected, it's not possible to leave the field
 * by pressing 'tab'
 *
 * @param po_obj
 */
function j_blur(po_obj) {

  po_obj.blur();
  if (typeof(document.selection) != 'undefined') {
    document.selection.empty();
  }
  window.focus();
} 

/*
 * enable/disable html field with name = ps_fld_name
 *
 * @param  po_form     html form object
 * @param  ps_fld_name name of html field to be disabled
 * @param  pb_disable  true=disable field, false=enable field
 */
function j_set_disable(po_form, ps_fld_name, pb_disable) {

  jo_obj = po_form.elements[ps_fld_name];

  if (typeof(jo_obj) == 'undefined') {
    return;
  }

  // check if multiple field
  if (typeof(jo_obj.length) == 'undefined') {
    ji_length = 1;
  } else {
    ji_length = jo_obj.length;
  }

  // enable/disable fields
  if (ji_length == 1) {
    jo_obj.disabled = pb_disable;
  } else {
    for (ji_i = 0; ji_i < ji_length; ji_i++) {
      jo_obj[ji_i].disabled = pb_disable;
    }
  }

}

