/**
 * Declare a constructor function to which we can add real functions.
 * @constructor
 */
function DWRUtilExt() { }
////////////////////////////////////////////////////////////////////////////////
/** These two functions DWRUtilExt.addOption and DWRUtilExt.removeOption
 *  are similar to the DWRUtil.addOptions and DWRUtil.removeAllOptions.
 *  DWRUtilExt.addOption appends items(LI) to the the UL or OL.
 *  DWRUtilExt.removeOption will remove items(LI) from the UL or OL
 *  Currently DWRUtilExt.removeOption is called from DWRUtilExt.addOption if
 *  textprop value is "".  The removeOption will remove the item(LI) from the
 *  UL or OL if it currently exists. Each (LI) is identified by the (styleid + "id")).
 *  For example for the beltFPM field which has a styleid="bFpm".  The id for that
 *  error message in the list is 'bFpmid'
 * <LI id='bFpmid'>error message</LI>
 *
 */


/**
 * Add options to a Collection(ArrayList) of LabelValueBeans.
 * DWRUtilExt.addOption has 1 modes:
 * <p><b>Array of objects, with differing option text and value</b><br/>
 * DWRUtil.addOptions(selectid, array, valueprop, textprop) creates an option
 * for each object in the array, with the value of the option set to the
 * id property of the object, and the option text set to the textprop
 * property.
 * This mode works with ul and ol lists.  All parameters are required.
 * </p>
 * @param ele The id of the list element or the HTML element itself
 * @param data An collection  of data items to populate the list
 * @param valuerev  represents the styleid of the object on the page
 * @param textprop represents the display text
 * @return false if the error collection is empty and true if not.
 */
DWRUtilExt.addOption = function(ele, data, valuerev, textprop) {
  var orig = ele;
  ele = $(ele);
  var text;
  var value;

  if (ele == null) {
    alert("addOption() can't find an element with id: " + orig + ".");
    return;
  }

   var useLi = DWRUtil._isHTMLElement(ele, ["ul", "ol"]);

  if (!valuerev || !textprop) {
    alert("key and label names need to be defined");
    return;
  }

  if (!useLi) {
    alert("fillList() can only be used with select elements. Attempt to use: " + DWRUtil._detailedTypeOf(ele));
    return;
  }

  // Bail if we have no data
  if (data == null) {
    return;
  }

  // Loop through the data that we do have
  for (var i = 0; i < data.length; i++) {
    id = data[i][valuerev] + "id";
    text = data[i][textprop];
    // delete existing line if it exist
    if ($(id) != null) {
        document.getElementById(id).removeNode(true);
    }

    // only append error messages if one exist
    if(text != ""){

      li = document.createElement("li");
      //map id
      li.id= id;
      li.innerHTML = "" + text;
      ele.appendChild(li);

    }
  }

  return $(ele).hasChildNodes();
}

/**
 * Remove an option from a ul or ol list (specified by id)
 * @param ele The id of the list element or the HTML element itself
 * @param id list item to be removed
 */
DWRUtilExt.removeOption = function(ele, id) {
  var orig = ele;
  id = id + "id";//all id will have "id" concatenated to the end to represent error message field
  ele = $(ele);
  if (ele == null) {
    alert("removeOptions() can't find an element with id: " + orig + ".");
    return;
  }

  var useLi = DWRUtil._isHTMLElement(ele, ["ul", "ol"]);

  if (!useLi) {
    alert("removeOption() can only be used with ol and ul elements. Attempt to use: " + DWRUtil._detailedTypeOf(ele));
    return;
  }

  document.getElementById(id).removeNode(true);
}


