
/**
 * Toggle display of the given div.
 */
function toggleDisplay(divId) {
  var targetDiv = document.getElementById(divId);
  if (targetDiv) {
    if ((targetDiv.style.display == 'none') || (targetDiv.style.display == '')) {
      targetDiv.style.display = 'block';
    } else {
      targetDiv.style.display = 'none';
    }
  }
}

/**
 * Remove childDiv from parentDiv.
 */
function removeElement(parentDiv, childDiv) {
  if (childDiv == parentDiv) {
    // alert("(dbg) The parent div cannot be removed.");
  } else if (document.getElementById(childDiv)) {     
    var child = document.getElementById(childDiv);
    var parent = document.getElementById(parentDiv);
    parent.removeChild(child);
  } else {
    // alert("(dbg) Child div has already been removed or does not exist.");
    return false;
  }
}

/**
 * Remove all children nodes from parent
 */
function removeChildren(parent) {
  if (parent && parent.hasChildNodes && parent.removeChild) {
    while (parent.hasChildNodes()) {
      parent.removeChild(parent.firstChild);
    }
  }
}

/**
 * Create a div element.
 */
function createDiv(opt_text, opt_className) {
  var el = document.createElement("div");
  if (opt_text) {
    el.innerHTML = opt_text;
  }
  if (opt_className) { el.className = opt_className; }
  return el;
}

/**
 * Apply a method helper.
 */
function methodClosure(object, method, opt_argArray) {
  return function() {
    return method.apply(object, opt_argArray);
  }
}

/**
 * Create a link (anchor) element.
 */
function createLink(href, opt_text, opt_target, opt_className, opt_divwrap) {
  var el = document.createElement("a");
  el.href = href;
  if (opt_text) {
    el.innerHTML = opt_text;
  }
  if (opt_className) {
    el.className = opt_className;
  }
  if (opt_target) {
    el.target = opt_target;
  }
  if (opt_divwrap) {
    var div = this.createDiv(null, opt_className);
    div.appendChild(el);
    el = div;
  }
  return el;
}

/**
 * Return the array index of the selected radio button (or -1 if no button is selected).
 */
function getSelectedRadio(buttonGroup) {
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
}

/**
 * Return the value of the selected radio button (or "" if no button is selected).
 */
function getSelectedRadioValue(buttonGroup) {
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
}

/**
 * Run through all the check boxes and return an array of all the ones
 * that are selected (their position numbers). If no boxes are checked,
 * returned array will be empty (length will be zero).
 */
function getSelectedCheckbox(buttonGroup) {
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
}

/**
 * Return an array of values selected in the check box group. if no boxes
 * are checked, returned array will be empty (length will be zero).
 */
function getSelectedCheckboxValue(buttonGroup) {
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
}

/**
 * Trim leading and trailing spaces from a text field.
 */
function trim(item) {
  var tmp = "";
  var item_length = item.value.length;
  var item_length_minus_1 = item.value.length - 1;
  for (index=0; index < item_length; index++) {
    if (item.value.charAt(index) != ' ') {
      tmp += item.value.charAt(index);
    } else {
      if (tmp.length > 0) {
        if (item.value.charAt(index+1) != ' ' && index != item_length_minus_1) {
          tmp += item.value.charAt(index);
        }
      }
    }
  }
  item.value = tmp;
}

/**
 * Return the label associated with the given element
 * (or false, if none exists).
 */
function getLabel(iElem){
  if (/label/i.test(iElem.parentElement.tagName))
    return iElem.parentElement;

  var labels = document.getElementsByTagName("label"),i;
  for (i=0; i<labels.length; i++) {
    if (labels[i].htmlFor == iElem.id)
      return labels[i];
  }

  return false
}

/**
 * Highlight the label associated with fieldId, if
 * (1) there IS a label associated with fieldId
 * (2) fieldId is showing as invalid
 */
function hightlightIfInvalid(fieldId) {
  var lbl = document.getLabel(fieldId);

  if (lbl) {
    alert("Highlighting label "+lbl.innerHTML);
    // Do nothing if client validation is not active
    if (typeof(Page_Validators) == "undefined")  return;
    // Change color of the label
    lbl.style.color = document.getElementById(fieldId).isvalid ? "Black" : "Red";
  }
}

/**
 * Return the number of days in the given month.
 * lnMonth is a month number (1 - 12)
 * lnYear is a year number (eg. 1970)
 */
function daysInMonth(lnMonth, lnYear) {
  var dt1, cmn1, cmn2, dtt, lflag, dycnt, lmn;
  lmn = lnMonth-1;
  dt1 = new Date(lnYear, lmn, 1);
  cmn1 = dt1.getMonth();
  dtt = dt1.getTime()+2332800000;
  lflag = true;
  dycnt = 28;
  while (lflag) {
    dtt = dtt + 86400000;
    dt1.setTime(dtt);
    cmn2 = dt1.getMonth();
    if (cmn1 != cmn2) {
      lflag = false;
    } else {
      dycnt = dycnt + 1;
    }
  }
  if (dycnt > 31) { dycnt = 31; }
  return dycnt;
}

/**
 * Sets the number of days in the selection list, based on the month
 * (and year, if available) currently selected.
 * The objRef parameter refers to the generated date object (before having
 * the suffix appended).
 */
function setDays(objRef) {
  var dobj = eval(objRef + "_d");
  var mobj = eval(objRef + "_m");
  var yobj = eval(objRef + "_y");
  var hobj = eval(objRef + "_str");
  var monthdays = daysInMonth(mobj.options[mobj.selectedIndex].value, yobj.options[yobj.selectedIndex].value);
  var selectdays = dobj.length;

  var curdy = dobj.options[dobj.selectedIndex].value;
  if (curdy.length==1) { curdy = "0"+curdy; }

  var curmn = mobj.options[mobj.selectedIndex].value;
  if (curmn.length==1) { curmn = "0"+curmn; }

  var curyr = yobj.options[yobj.selectedIndex].value;
  if (selectdays > monthdays) {
    for (var dlp=selectdays; dlp > monthdays; dlp--) {
      dobj.options[dlp-1] = null;
    }
  } else if (monthdays > selectdays) {
    for (var dlp=selectdays; dlp < monthdays; dlp++) {
      dobj.options[dlp] = new Option(dlp+1, dlp+1);
    }
  }
  if (curdy > monthdays) {
    dobj.options[monthdays-1].selected = true;
    curdy = monthdays;
  }
  hobj.value = curdy+curmn+curyr;
}


