// File Name : ddbox.js
// File Type : Java script
// Function  : Dynamic drop down box utility functions
// Date      : 23.JUL.2001
// Author    : Katherine Lee


/* 
 * Change dynamic drop down select lists from data array
 *
 * According to the following logics, a child ddbox can 
 * have max. of 4 parent codes to filter data 
 *
 * Function name        j_chg_ddbox
 * @param               po_target_select_name   (Name of target select list)
 * @param               pas_item_array          (The data array)
 * @param               ps_sel_code             (The code of the selected item)
 * @param               pb_dft_show_all         (Default show all items in the ddbox)
 * @param               po_parent_code1         (The objects of parent codes - 
 * @param               po_parent_code2          parent code that can filter the array)
 * @param               po_parent_code3         
 * @param               po_parent_code4         
 */

function j_chg_ddbox(po_target_select_name, pas_item_array, ps_sel_code, pb_dft_show_all, 
                     po_parent_code1, po_parent_code2, po_parent_code3, po_parent_code4 ) {

  var ji_i, ji_j;
  var ms_parent_code1, ms_parent_code2, ms_parent_code3, ms_parent_code4;
  var mb_show_ind, mb_sel_code_found;
  mb_show_ind = true;      // Show indicator of items in JavaScript array

  ms_parent_code1 = null;
  ms_parent_code2 = null;
  ms_parent_code3 = null;
  ms_parent_code4 = null;

  if (po_parent_code1 != null) {
    if (typeof(po_parent_code1) == 'select-one') {
      ms_parent_code1 = po_parent_code1.options[po_parent_code1.selectedIndex].value;
    } else {
      ms_parent_code1 = po_parent_code1.value;
    }
  }
  if (po_parent_code2 != null) {
    if (typeof(po_parent_code1) == 'select-one') {
      ms_parent_code2 = po_parent_code2.options[po_parent_code2.selectedIndex].value;
    } else {
      ms_parent_code2 = po_parent_code2.value;
    }
  }
  if (po_parent_code3 != null) {
    if (typeof(po_parent_code1) == 'select-one') {
      ms_parent_code3 = po_parent_code3.options[po_parent_code3.selectedIndex].value;
    } else {
      ms_parent_code3 = po_parent_code3.value;
    }
  }
  if (po_parent_code4 != null) {  
    if (typeof(po_parent_code1) == 'select-one') {
      ms_parent_code4 = po_parent_code4.options[po_parent_code4.selectedIndex].value;
    } else {
      ms_parent_code4 = po_parent_code4.value;
    }
  }

// Empty existing items in the target select list
  for (ji_i = po_target_select_name.options.length; ji_i >= 0; ji_i--) {
    po_target_select_name.options[ji_i] = null;
  }

// Fill in target select drop down box
  mb_sel_code_found = false;
  if (pas_item_array != null) {

    for (ji_i = 0, ji_j = 0; ji_i < pas_item_array.length; ji_i++) {
  
      mb_show_ind = true;

      if (pas_item_array[ji_i][2] != null && pas_item_array[ji_i][2] != '') {
        if (ms_parent_code1 != null && ms_parent_code1 != pas_item_array[ji_i][2]) {
          if ((pb_dft_show_all == false || ms_parent_code1 != '')) {
            mb_show_ind = false;
          }
        }
      }
      if (pas_item_array[ji_i][3] != null && pas_item_array[ji_i][3] != '') {
        if (ms_parent_code2 != null && ms_parent_code2 != pas_item_array[ji_i][3]) {
          if ((pb_dft_show_all == false || ms_parent_code2 != '')) {
            mb_show_ind = false;
          }
        }
      }
      if (pas_item_array[ji_i][4] != null && pas_item_array[ji_i][4] != '') {
        if (ms_parent_code3 != null && ms_parent_code3 != pas_item_array[ji_i][4]) {
          if ((pb_dft_show_all == false || ms_parent_code3 != '')) {
            mb_show_ind = false;
          }
        }
      }
      if (pas_item_array[ji_i][5] != null && pas_item_array[ji_i][5] != '') {
        if (ms_parent_code4 != null && ms_parent_code4 != pas_item_array[ji_i][5]) {
          if ((pb_dft_show_all == false || ms_parent_code4 != '')) {
            mb_show_ind = false;
          }
        }
      }


      // Add new options into the select box
      if (mb_show_ind) {

          // Create drop down menu item
          po_target_select_name.options[ji_j] = new Option(pas_item_array[ji_i][0]);

          // Add drop down menu values
          if (pas_item_array[ji_i][1] != null) {
            po_target_select_name.options[ji_j].value = pas_item_array[ji_i][1];
          }

          // Set 'selected' if drop down box value = ps_sel_code
          if (pas_item_array[ji_i][1] == ps_sel_code) {

            // The 'if' condition below is to fix problem in Netscape 6.0
            if (po_target_select_name.options[ji_j].selected == false) {
              po_target_select_name.options[ji_j].selected = true;
            }

            mb_sel_code_found = true;
          }

          // Increment drop down box item counter
          ji_j++;
      }

    }  // end for loop
  } // end if pas_item_array != null

  if (ps_sel_code != null && mb_sel_code_found == false) {
    alert('Code "' + ps_sel_code + '" not found drop down box "' + po_target_select_name.name + '"');
  }

}

/* 
 * Change dynamic drop down select lists from data array
 * By Tim Liu
 *
 * Function name        j_chg_ddbox_by_radio
 * @param               po_target               (Name of target select list)
 * @param               pas_array               (The data array)
 * @param               po_filter               (The filter check box)
 */

function j_chg_ddbox_by_radio(po_target, pas_array, po_filter) {
  var js_filter;
  var mb_show;
  var ji_j;
  js_filter = '';
  ji_j = 0;
  if (po_filter != null) {
    for (var ji_i=0; ji_i<po_filter.length; ji_i++)
      if (po_filter[ji_i].checked)
        js_filter = po_filter[ji_i].value;
  }
  if (po_target.options != null)
    for (var ji_i=po_target.options.length-1; ji_i>=0; ji_i--)
      po_target.options[ji_i] = null;
  if (po_filter != null) {
    if (pas_array != null) {
      for (var ji_i=0; ji_i<pas_array.length; ji_i++) {
        mb_show = true;
        if (pas_array[ji_i][2] != '') {
          if (pas_array[ji_i][2] != js_filter)
            mb_show = false;
        }
        if (mb_show) {
          po_target.options[ji_j] = new Option(pas_array[ji_i][0]);
          if (pas_array[ji_j][1] != null)
            po_target.options[ji_j].value = pas_array[ji_i][1];
          ji_j++;
        }
      }
    }
  }
}

/* 
 * Select option in drop down box
 *
 * Function name        j_select_ddbox
 * @param               po_ddbox_obj     (Drop down box object)
 * @param               ps_value         (Value of the option to be selected)
 */
function j_select_ddbox(po_ddbox_obj, ps_value) {
  for (var ji_i = 0; ji_i < po_ddbox_obj.options.length; ji_i++) {
    if (po_ddbox_obj.options[ji_i].value == ps_value) {
      po_ddbox_obj.options[ji_i].selected = true;
    }
  }
}


/* 
 * Set drop down box object visibility
 *
 * Function name        j_set_obj_visibility
 *
 * @param               po_ddbox_obj     (Drop down box object)
 * @param               ps_show_hide_ind (Show / Hide indicator)
 *                                       ('show' = show ddbox,
 *                                        'hide' = hide ddbox)
 */
function j_set_obj_visibility(po_obj, ps_show_hide_ind) {

  jo_obj = po_obj;
  if (jo_obj.style) {
    jo_obj = jo_obj.style;
    if (ps_show_hide_ind == 'show') {
      ps_show_hide_ind = 'visible';
    } else {
      ps_show_hide_ind = 'hidden';
    }
  }
  jo_obj.visibility = ps_show_hide_ind;
}


/* 
 * Loop for all ddbox in the form and set visibility
 *
 * Function name        j_set_ddbox
 *
 * @param               ps_show_hide_ind (Show / Hide indicator)
 *                                       ('show' = show ddbox,
 *                                        'hide' = hide ddbox)
 */
function j_set_ddbox(ps_show_hide_ind) {

  jo_form = document.forms;
  for (ji_i = 0; ji_i < jo_form.length; ji_i++) {
    jo_element = jo_form[ji_i].elements;
    for (ji_j = 0; ji_j < jo_element.length; ji_j++) {
      if (jo_element[ji_j].type == 'select-one') {
        j_set_obj_visibility(jo_element[ji_j], ps_show_hide_ind);
      }
      if (jo_element[ji_j].type == 'select-multiple') {
        j_set_obj_visibility(jo_element[ji_j], ps_show_hide_ind);
      }
    }
  }
}


/* 
 * Clear all item in ddbox
 *
 * Function name        j_clear_ddbox
 *
 * @param               po_target_select_name   Target drop down box
 * @param               pi_len                  number of char '-' in empty line
 */
function j_clear_ddbox(po_target_select_name, pi_len) {

  var ms_empty_line;
  var ji_i;

  // Empty existing items in the target select list
  for (ji_i = po_target_select_name.options.length; ji_i >= 0; ji_i--) {
    po_target_select_name.options[ji_i] = null;
  }

  if (pi_len != null) {
    ms_empty_line = '';
    for (ji_i = 0; ji_i < pi_len; ji_i++) {
      ms_empty_line = ms_empty_line + '-';
    }
    po_target_select_name.options[0] = new Option(ms_empty_line);
    po_target_select_name.options[0].value = '';
  }

}


/* 
 * Alert user to make parent ddbox selection if parent ddbox was not selected
 *
 * Function name        j_chk_filter_ddbox
 *
 * @param               po_parent   Parent drop down box object
 * @param               ps_msg      Message prompt to user
 */
function j_chk_filter_ddbox(po_parent, ps_msg) {

  if (po_parent.options[po_parent.selectedIndex].value == "") {
      alert(ps_msg);
      po_parent.focus();
  }

}

/* 
 * Move selected items from one list box to another using "^" and "v" direction buttons.
 * It works like database field selection, supports selecting multiple items, and even
 * can sort the moved item into the new list. 
 *
 * Function name        j_move_items
 * @param               po_from_list   (Name of select list to select items from)
 * @param               po_to_list     (Name of select list to move the selected items to)
 */

function j_move_items(po_fr_list, po_to_list) {

  var ma_fr_list     = new Array();
  var ma_to_list     = new Array();

  var ji_i, ji_c, ji_j;
  var js_fr_str = "", js_to_str = "";

  var js_delimiter = "~";

  //determine whether the dummy line exists
  for(ji_i=0; ji_i<po_fr_list.options.length; ji_i++) {
    if (po_fr_list.options[ji_i].value.length == 0) {
      if (js_fr_str.length == 0 )
        js_fr_str = po_fr_list.options[ji_i].text;
    }
  }

  for(ji_i=0; ji_i<po_to_list.options.length ; ji_i++) {
    if(po_to_list.options[ji_i].value.length == 0) {
      if (js_to_str.length == 0 )
        js_to_str = po_to_list.options[ji_i].text;
    }
  }

  //copy from form selection box object into array 
  ji_j = 0;
  for (ji_i = 0; ji_i < po_to_list.options.length; ji_i++) {
    if(po_to_list.options[ji_i].value.length != 0) {
      ma_to_list[ji_j] = po_to_list.options[ji_i].text + js_delimiter + po_to_list.options[ji_i].value;
      ji_j ++;
    }
  }

  var fLength = 0;
  var tLength = ma_to_list.length;

  for(ji_i = 0; ji_i < po_fr_list.options.length; ji_i++) {
    if (po_fr_list.options[ji_i].value.length != 0) {
      if (po_fr_list.options[ji_i].selected && po_fr_list.options[ji_i].value.length != 0) {      
        ma_to_list[tLength] = po_fr_list.options[ji_i].text + js_delimiter + po_fr_list.options[ji_i].value;  
        tLength++;
      } else {    
        ma_fr_list[fLength] = po_fr_list.options[ji_i].text + js_delimiter + po_fr_list.options[ji_i].value;    
        fLength++;
      }
    }
  }

  //sort the selection array
  ma_fr_list.sort();
  ma_to_list.sort();
  po_fr_list.length = 0;
  po_to_list.length = 0;

  //copy from array to selection box object
  if (js_fr_str.length != 0) { 
    var no        = new Option();
    no.value      = "";
    no.text       = js_fr_str;
    po_fr_list[0] = no;
  }
  for(ji_c = 0; ji_c < ma_fr_list.length; ji_c++) {
    var no      = new Option();
    var ji_pos  = ma_fr_list[ji_c].lastIndexOf(js_delimiter);
    no.value    = j_trim(ma_fr_list[ji_c].substring(ji_pos+1, ma_fr_list[ji_c].length));
    no.text 	= j_trim(ma_fr_list[ji_c].substring(0, ma_fr_list[ji_c].length - no.value.length - js_delimiter.length));

    if (js_fr_str.length != 0)
      po_fr_list[ji_c+1] = no;
    else 
      po_fr_list[ji_c]   = no;
  }

  if (js_to_str.length != 0) {
    var no        = new Option();
    no.value      = "";
    no.text       = js_to_str;
    po_to_list[0] = no;        
  }

  for(ji_c = 0; ji_c < ma_to_list.length; ji_c++) {
    var no     = new Option();
    var ji_pos = ma_to_list[ji_c].lastIndexOf(js_delimiter);
    no.value   = j_trim(ma_to_list[ji_c].substring(ji_pos+1, ma_to_list[ji_c].length));
    no.text    = j_trim(ma_to_list[ji_c].substring(0, ma_to_list[ji_c].length - no.value.length - js_delimiter.length));
  
    if (js_to_str.length != 0)
      po_to_list[ji_c+1] = no;
    else
      po_to_list[ji_c]   = no;
  }
}


/* 
 * Select all items in the input list 
 *
 * Function name        j_move_items
 * @param               po_from_list   (Name of select list to select items from)
 * @param               po_to_list     (Name of select list to move the selected items to)
 */

function j_select_all(po_list) {

  var ji_j;

  for (ji_j=0; ji_j < po_list.options.length; ji_j++) {
    po_list.options[ji_j].selected = true;

    if (po_list.options[ji_j].selected && po_list.options[ji_j].value == "") {
        po_list.options[ji_j].selected = false;
    }
  }

}
