// Util.js
// Copyright (C) 2001-2003  Richard K. Schaffer
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

function EventHandler() {};

EventHandler.add = function(element, eventType, listener) {
  if (document.all)
    element.attachEvent('on' + eventType, listener);
  else
    element.addEventListener(eventType, listener, false);
}

EventHandler.remove = function(element, eventType, listener) {
  if (document.all)
    element.detachEvent('on' + eventType, listener);
  else
    element.removeEventListener(eventType, listener, false);
}


function Position(element, fromElement) {
  if (element == window) {
    this.id = '__window__';
  }
  else {
    this.id = element.id;
    if (fromElement) {
      if (fromElement == document)
        this.fromId = '__document__';
      else
        this.fromId = fromElement.id;
    }
  }
    
  this.refresh();
}

Position.prototype.id = '';
Position.prototype.fromId = '';
Position.prototype.top = 0;
Position.prototype.left = 0;
Position.prototype.height = 0;
Position.prototype.width = 0;
Position.prototype.clientHeight = 0;
Position.prototype.clientWidth = 0;
Position.prototype.scrollTop = 0;
Position.prototype.scrollHeight = 0;
Position.prototype.scrollLeft = 0;
Position.prototype.scrollWidth = 0;

Position.prototype.refresh = function() {
  var element, offsetParent, style;
  
  if (this.id != '__window__') {
    element = document.getElementById(this.id);
    this.top = element.offsetTop;
    this.left = element.offsetLeft;
    this.height = element.offsetHeight;
    this.width = element.offsetWidth;
    this.clientHeight = element.clientHeight;
    this.clientWidth = element.clientWidth;
    this.scrollTop = element.scrollTop;
    this.scrollHeight = element.scrollHeight;
    this.scrollLeft = element.scrollLeft;
    this.scrollWidth = element.scrollWidth;

    if (this.fromId != '') {
      offsetParent = element;
      while (true) {
        if (!offsetParent.offsetParent) break;
        if (offsetParent.id && offsetParent.id == this.fromId) break;
        if (this.fromId == '__document__' && offsetParent == document) break;
        offsetParent = offsetParent.offsetParent;
        this.top += offsetParent.offsetTop
        this.left += offsetParent.offsetLeft
      }
    }
  }
  else {
    if (document.all) {
	    this.clientWidth = document.documentElement.clientWidth;
	    this.clientHeight = document.documentElement.clientHeight;
    }
    else {
 	    this.clientWidth = window.innerWidth;
	    this.clientHeight = window.innerHeight;
    }
  }
}

window.position = new Position(window);


function DHTML() {};

DHTML.initialize = function() {
  DHTML.setContentSizeAndPositionStatusBar();
  EventHandler.add(window, 'resize', DHTML.setContentSizeAndPositionStatusBar);
}

DHTML.setContentSizeAndPositionStatusBar = function() {
  var bodyMarginLeft, bodyMarginRight, bodyMarginBottom, contentElement, contentElementPosition,
   contentSpacerElement, contentPaddingBottom, contentPaddingRight, contentHeight, contentWidth,
   statusBarElement, statusBarElementPosition, statusBarHeight;

  contentElement = document.getElementById('content');
  if (!contentElement) {
    document.body.style.overflow = 'auto';
    return;
  }
  
  window.position.refresh();

  bodyMarginLeft = parseInt(document.body.style.marginLeft);
  bodyMarginRight = parseInt(document.body.style.marginRight);
  bodyMarginBottom = parseInt(document.body.style.marginBottom);

  statusBarElement = document.getElementById('status-bar');
  if (statusBarElement) {
    statusBarElementPosition = new Position(statusBarElement);
    statusBarHeight = statusBarElementPosition.height;
  
    statusBarElement.style.top = (window.position.clientHeight - statusBarHeight - bodyMarginBottom) + 'px';
    statusBarElement.style.width = (window.position.clientWidth - bodyMarginLeft - bodyMarginRight) + 'px';
  }
  else {
    statusBarHeight = 0;
  }    

  contentElementPosition = new Position(contentElement);
  contentPaddingRight = parseInt(contentElement.style.paddingRight);
  contentPaddingBottom = parseInt(contentElement.style.paddingBottom);
  
  contentHeight = window.position.clientHeight - contentElementPosition.top - statusBarHeight - bodyMarginBottom - contentPaddingBottom;
  contentWidth = window.position.clientWidth - contentElementPosition.left - bodyMarginRight - contentPaddingRight;

  contentElement.style.height = contentHeight + 'px';
  contentElement.style.width = contentWidth + 'px';

  contentSpacerElement = document.getElementById('content-spacer');
  contentSpacerElement.height = contentHeight + contentPaddingBottom;
}


DHTMLCalendar = function() {
}

DHTMLCalendar.days = new Array('S', 'M', 'T', 'W', 'T', 'F', 'S');
DHTMLCalendar.daysInMonths = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
DHTMLCalendar.months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
DHTMLCalendar.calendarField = null;
DHTMLCalendar.hideTimer = null;
DHTMLCalendar.selectedDate = null;
DHTMLCalendar.visibleDate = null;

DHTMLCalendar.showCalendar = function(calendarFieldId, calendarImgId) {
  var calendarPosition, contentElement, contentElementPosition, div, iframe;

  if (document.getElementById('calendar'))
    DHTMLCalendar.hideCalendar();  
  if (DHTMLCalendar.hideTimer != null)
    clearTimeout(DHTMLCalendar.hideTimer);

  DHTMLCalendar.calendarField = document.getElementById(calendarFieldId);
  
  if (DHTMLCalendar.calendarField.value != '' && !isNaN(Date.parse(DHTMLCalendar.calendarField.value))) {
    DHTMLCalendar.selectedDate = new Date(Date.parse(DHTMLCalendar.calendarField.value));
    DHTMLCalendar.visibleDate = DHTMLCalendar.selectedDate;
  }
  else {
    DHTMLCalendar.selectedDate = null;
    DHTMLCalendar.visibleDate = new Date();
  }
  
  calendarPosition = new Position(document.getElementById(calendarImgId), document);
  calendarPosition.left += 27;
  calendarPosition.top += 2;
  
  contentElement = document.getElementById('content');
  
  if (contentElement != null) {
    contentElementPosition = new Position(contentElement);
    calendarPosition.left -= contentElement.scrollLeft;
    calendarPosition.top -= contentElement.scrollTop;
  }
  
  if (document.all) {
    iframe = document.createElement('iframe');
    iframe.id = 'calendar-frame';
    iframe.className = 'calendar-frame';
    iframe.frameBorder = '0px';
    iframe.style.left = calendarPosition.left + 'px';
    iframe.style.top = calendarPosition.top + 'px';
    document.body.appendChild(iframe);  
  }
    
  div = document.createElement('div');
  div.id = 'calendar';
  div.className = 'calendar';
  div.style.left = calendarPosition.left + 'px';
  div.style.top = calendarPosition.top + 'px';
  div.onmouseover = function() { clearTimeout(DHTMLCalendar.hideTimer); }
  div.onmouseout = function() { DHTMLCalendar.hideTimer = setTimeout('DHTMLCalendar.hideCalendar();', 3000); }
  document.body.appendChild(div);
     
  DHTMLCalendar.hideTimer = setTimeout('DHTMLCalendar.hideCalendar();', 3000);
  
  DHTMLCalendar.updateCalendar();
}

DHTMLCalendar.hideCalendar = function() {
  var calendarElement, calendarFrameElement;
  
  calendarElement = document.getElementById('calendar');
  calendarFrameElement = document.getElementById('calendar-frame');
  
  if (calendarElement)
    document.body.removeChild(calendarElement);
  if (document.all && calendarFrameElement)
    document.body.removeChild(calendarFrameElement);
}

DHTMLCalendar.updateCalendar = function() {
  var calendarElement, calendarFrameElement, daysInMonth, i, firstDay, table, tbody, td, tr, month, year;
  
  calendarElement = document.getElementById('calendar');
  calendarFrameElement = document.getElementById('calendar-frame');

  year = DHTMLCalendar.visibleDate.getFullYear();
  month = DHTMLCalendar.visibleDate.getMonth();
  firstDay = new Date(year, month, 1).getDay(); 
  daysInMonth = ((month == 1) && ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))) ? 29 : DHTMLCalendar.daysInMonths[month];
   
  if (calendarElement.hasChildNodes())
    calendarElement.removeChild(calendarElement.firstChild);
  
  table = document.createElement('table');
  table.className = 'calendar-table';
  table.cellSpacing = '0px';
  table.cellPadding = '0px';
  
  tbody = document.createElement('tbody');
  
  tr = document.createElement('tr');  
  
  td = document.createElement('td');
  td.className = 'calendar-month calendar-month-button';
  td.style.borderStyle= 'outset';
  td.onmousedown = function() { this.style.borderStyle= 'inset'; }
  td.onmouseout = td.onmouseup = function() { this.style.borderStyle= 'outset'; }
  td.onclick = function() {
    DHTMLCalendar.visibleDate = new Date(DHTMLCalendar.visibleDate.getFullYear(), DHTMLCalendar.visibleDate.getMonth() - 1, DHTMLCalendar.visibleDate.getDate());
    DHTMLCalendar.updateCalendar();
  }
  td.appendChild(document.createTextNode('<'));
  tr.appendChild(td);
  
  td = document.createElement('td');
  td.colSpan = '5';
  td.className = 'calendar-month';
  td.appendChild(document.createTextNode(DHTMLCalendar.months[DHTMLCalendar.visibleDate.getMonth()] + ' ' + DHTMLCalendar.visibleDate.getFullYear()));
  tr.appendChild(td);
    
  td = document.createElement('td');
  td.className = 'calendar-month calendar-month-button';
  td.style.borderStyle= 'outset';
  td.onmousedown = function() { this.style.borderStyle= 'inset'; }
  td.onmouseout = td.onmouseup = function() { this.style.borderStyle= 'outset'; }
  td.onclick = function() {
    DHTMLCalendar.visibleDate = new Date(DHTMLCalendar.visibleDate.getFullYear(), DHTMLCalendar.visibleDate.getMonth() + 1, DHTMLCalendar.visibleDate.getDate());
    DHTMLCalendar.updateCalendar();
  }
  td.appendChild(document.createTextNode('>'));
  tr.appendChild(td);    
  
  tbody.appendChild(tr);
  
  tr = document.createElement('tr');
  for (i = 0; i < 7; i++) {
    td = document.createElement('td');
    td.className = 'calendar-day';
    td.appendChild(document.createTextNode(DHTMLCalendar.days[i]));
    tr.appendChild(td);
  }
  tbody.appendChild(tr);
  
  tr = document.createElement('tr');
  
  for (i = 0; i < firstDay; i++) {
    td = document.createElement('td');
    td.className = 'calendar-date';
    tr.appendChild(td);
  }
    
  for (i = 0; i < daysInMonth; i++) {
    td = document.createElement('td');
    td.className = 'calendar-date calendar-date-visible';
    
    if (i == new Date().getDate() - 1 && month == new Date().getMonth() && year == new Date().getFullYear())
      td.className += ' calendar-date-today';
    
    if (DHTMLCalendar.selectedDate != null && i == DHTMLCalendar.selectedDate.getDate() - 1 && month == DHTMLCalendar.selectedDate.getMonth() && year == DHTMLCalendar.selectedDate.getFullYear())
      td.className += ' calendar-date-selected';
    
    if (!DHTMLCalendar.calendarField.readOnly) {
      td.onmouseover = function() { this.className += ' calendar-date-hover'; }
      td.onmouseout = function() { this.className = this.className.substring(0, this.className.indexOf(' calendar-date-hover')); }
      
      td.onclick = function() {
        DHTMLCalendar.calendarField.value = (DHTMLCalendar.visibleDate.getMonth() + 1) + '/' + this.firstChild.nodeValue + '/' + DHTMLCalendar.visibleDate.getFullYear();
        DHTMLCalendar.calendarField.onchange();
        DHTMLCalendar.hideCalendar();
      }
    }
  
    td.appendChild(document.createTextNode(i + 1));
    tr.appendChild(td);
  
    if ((i + firstDay + 1) % 7 == 0 && i < daysInMonth - 1) {
      tbody.appendChild(tr);
      tr = document.createElement('tr');
    }
  }
  
  tbody.appendChild(tr);
  
  table.appendChild(tbody);

  calendarElement.appendChild(table);
  
  if (calendarFrameElement)
    calendarFrameElement.style.height = (Math.ceil((firstDay + daysInMonth) / 7) + 2) * 20 + 2;
}

DHTMLStatusBar = function() {};

DHTMLStatusBar.statusBarImageToggleCount = 0;
DHTMLStatusBar.statusBarImageToggleInterval = null;
DHTMLStatusBar.statusBarTimeout = null;

DHTMLStatusBar.hideStatusBar = function() {
  var statusBarElement, statusBarImageElement;
  
  statusBarElement = document.getElementById('status-bar');
  
  if (statusBarElement && statusBarElement.style.visibility == 'visible') {
    statusBarImageElement = document.getElementById('status-bar-image');

    window.clearTimeout(DHTMLStatusBar.statusBarTimeout);
    window.clearInterval(DHTMLStatusBar.statusBarImageToggleInterval);
    DHTMLStatusBar.statusBarImageToggleCount = 0;
  
    statusBarElement.style.visibility = 'hidden';
    statusBarImageElement.style.visibility = 'hidden';
    DHTML.setContentSizeAndPositionStatusBar();
  }
}

DHTMLStatusBar.showStatusBar = function() {
  var statusBarElement;
  
  statusBarElement = document.getElementById('status-bar');
  
  if (statusBarElement && statusBarElement.style.visibility == 'hidden') {
    window.clearTimeout(DHTMLStatusBar.statusBarTimeout);
    window.clearInterval(DHTMLStatusBar.statusBarImageToggleInterval);
    DHTMLStatusBar.statusBarImageToggleCount = 0;
  
    statusBarElement.style.visibility = 'visible';
    DHTMLStatusBar.statusBarTimeout = window.setTimeout(DHTMLStatusBar.hideStatusBar, 10000);
    DHTMLStatusBar.statusBarImageToggleInterval = window.setInterval(DHTMLStatusBar.toggleStatusBarImage, 200);
    DHTML.setContentSizeAndPositionStatusBar();
  }
}

DHTMLStatusBar.toggleStatusBarImage = function() {
  var statusBarImageElement;
  
  statusBarImageElement = document.getElementById('status-bar-image');
  if (statusBarImageElement) {
    if (DHTMLStatusBar.statusBarImageToggleCount % 2 == 0)
      statusBarImageElement.style.visibility = 'hidden';
    else
      statusBarImageElement.style.visibility = 'visible';
    DHTMLStatusBar.statusBarImageToggleCount += 1;
    if (DHTMLStatusBar.statusBarImageToggleCount > 3) window.clearInterval(DHTMLStatusBar.statusBarImageToggleInterval);
  }
  else {
    window.clearInterval(DHTMLStatusBar.statusBarImageToggleInterval);
  }
}


DHTMLReport = function() {
}

DHTMLReport.lastReportDetailRow = null;
DHTMLReport.lastHRefCol = null;
DHTMLReport.lastMouseMoveAt = new Date();
DHTMLReport.toolTipInterval = null;

/*DHTMLReport.checkToolTip = function() {
  var xmlParameters;
  
	if (new Date().getTime() - lastMouseMoveAt.getTime() > 1000) {
    window.clearInterval(reportToolTipInterval);
    xmlParameters = buildXmlParameters(reportToolTipParameters);
    httpCallService(showReportToolTip, reportToolTipURL, reportToolTipMethodName, xmlParameters); 
	}
}*/

DHTMLReport.handleEvent = function(e) {
  var col, cols, href, row, target, tbody, type, i, x, y;
  
  if (window.event) {
    target = window.event.srcElement;
    type = window.event.type;
  }
  else {
    target = e.target;
    type = e.type;
  }

  body = null; row = null; col = null;
  while (target != null && tbody == null) {
    if (target.nodeType == 1 && target.tagName == 'TBODY') tbody = target;  
    if (target.nodeType == 1 && target.tagName == 'TR') row = target;  
    if (target.nodeType == 1 && target.tagName == 'A') col = target;
    target = target.parentNode;  
  }
  
  if (tbody != null && tbody.className == 'report-detail' && row != null && !row.initialized) {
    cols = row.getElementsByTagName('A');
    for (i = 0; i < cols.length; i++) {
      href = reportHRefs[i];
      if (href && reportHRefParameters[i][row.index]) {
        cols[i].href = href(reportHRefParameters[i][row.index]);
        cols[i].style.textDecoration = 'none';
        cols[i].style.cursor = 'pointer';
      }
    }
    row.initialized = true;
  }

  if (DHTMLReport.lastHRefCol != null && col != DHTMLReport.lastHRefCol) DHTMLReport.lastHRefCol.style.textDecoration = 'none';
  if (col != null && col.href) {
    col.style.textDecoration = 'underline';
    DHTMLReport.lastHRefCol = col;
  }
  else {
    DHTMLReport.lastHRefCol = null;
  }
  
  if (DHTMLReport.lastReportDetailRow != null && row != DHTMLReport.lastReportDetailRow) DHTMLReport.lastReportDetailRow.style.backgroundColor = 'transparent';
  if (row != null && row.className && row.className == 'rd') {
    row.style.backgroundColor = '#E8F0E0';
    DHTMLReport.lastReportDetailRow = row;
  }
  else {
    DHTMLReport.lastReportDetailRow = null;
  }
}

DHTMLReport.initialize = function() {
  var bodies, bodyCount, col, cols, href, row, rows, i, j, k, r;

  bodyCount = 0; r = 0;
  bodies = document.getElementById('content').getElementsByTagName('TBODY');
  for (i = 0; i < bodies.length; i++) {
    if (bodies[i].className == 'report-detail') {
      bodyCount += 1;
      EventHandler.add(bodies[i], 'mousemove', DHTMLReport.handleEvent);
      rows = bodies[i].rows;
      for (j = 0; j < rows.length; j++) {
        row = rows[j];
        if (row.className == 'rd') {
          row.index = r;
          cols = row.getElementsByTagName('A');
          for (k = 0; k < cols.length; k++) {
            col = cols[k];
            col.index = k;
            href = reportHRefs[k];
            if (href && !(reportHRefParameters[k][r])) href = null;
            if (href) col.style.color = '#000066';
          }
          r += 1;
        }
      }
    }
  }
  
  if (bodyCount == 0) return;
  
  EventHandler.add(document, 'mousemove', DHTMLReport.handleEvent);
  if (document.getElementById('report-header')) {
    EventHandler.add(window, 'resize', DHTMLReport.positionHeader);
    EventHandler.add(document.getElementById('content'), 'scroll', DHTMLReport.positionHeader);
    DHTMLReport.positionHeader();
  }
}

DHTMLReport.positionHeader = function() {
  var contentElement, contentElementPosition, headerElement;
  
  contentElement = document.getElementById('content');
  contentElementPosition = new Position(contentElement);
  
  headerElement = document.getElementById('report-header');
  headerElement.style.top = contentElementPosition.top + 'px';
  headerElement.style.left = (contentElementPosition.left - contentElementPosition.scrollLeft) + 'px';
  headerElement.style.clip = 'rect(auto ' + (contentElementPosition.clientWidth + contentElementPosition.scrollLeft) + 'px auto ' + contentElementPosition.scrollLeft + 'px)';
  if (headerElement.style.visibility == 'hidden') headerElement.style.visibility = 'visible';
}


function setFocus(form, element) {
  for (i = 0; i < document.forms[form].elements.length; i++) {
    if (document.forms[form].elements[i].name == element) {
      document.forms[form].elements[i].focus();
      if (document.forms[form].elements[i].type == 'text') document.forms[form].elements[i].select();
      break;
    }
  }
}
  
function isEmpty(v) {
  return (v == null) || (v.length == 0) || isWhitespace(v);
}

function isWhitespace(v) {
  var i, c;
  for (i = 0; i < v.length; i++) {
    c = v.charAt(i);
    if (!((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'))) return false;
  }
  return true;
}

function completeDate(v, rollyear) {
  var delim, s, y;
  if (isEmpty(v)) return '';
  if (v.indexOf('/') != -1) {
    s = v.split('/');
    delim = '/';
  }
  else if (v.indexOf('-') != -1) {
    s = v.split('-');
    delim = '-';
  }
  else if (v.length == 3) {
    s = new Array();
    s[0] = v.substr(0, 1);
    s[1] = v.substr(2, 2);
    s[2] = null;
    delim = '/';
  }
  else if (v.length == 4) {
    s = new Array();
    s[0] = v.substr(0, 2);
    s[1] = v.substr(2, 2);
    s[2] = null;
    delim = '/';
  }
  else if (v.length == 5) {
    s = new Array();
    s[0] = v.substr(0, 1);
    s[1] = v.substr(1, 2);
    s[2] = v.substr(3, 2);
    delim = '/';
  }
  else if (v.length == 6) {
    s = new Array();
    s[0] = v.substr(0, 2);
    s[1] = v.substr(2, 2);
    s[2] = v.substr(4, 2);
    delim = '/';
  }
  else if (v.length == 7) {
    s = new Array();
    s[0] = v.substr(0, 1);
    s[1] = v.substr(1, 2);
    s[2] = v.substr(3, 4);
    delim = '/';
  }
  else if (v.length == 8) {
    s = new Array();
    s[0] = v.substr(0, 2);
    s[1] = v.substr(2, 2);
    s[2] = v.substr(4, 4);
    delim = '/';
  }
  if (isEmpty(s[2])) {d = new Date(); s[2] = d.getFullYear()}
  else if (s[2].length == 2) {if (s[2] < ((rollyear == null) ? '50' : rollyear)) {s[2] = '20'+s[2]} else {s[2] = '19'+s[2]}};
  return s[0]+delim+s[1]+delim+s[2];
}

function formatDate(v) {
  var d;
  if (isEmpty(v)) return '';
  d = new Date(v);
  return (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear();
}
  
function validateDate(form, element) {
  var n, v;
  v = document.forms[form].elements[element].value;
  if (!isEmpty(v)) {
    n = Date.parse(v);
    if (isNaN(n)) {alert('You must enter a valid date (mon/day/year).'); setFocus(form, element); return false;}
  }
  return true;
}
  
function formatNumber(v, decimals) {
  var d, f, i, n, s;
  if (isEmpty(v)) return '';
  s = new String(new Number(v.replace(/,/g, '')));
  s = s.split('.');
  n = 0; d = '';
  for (i = s[0].length - 1; i >= (s[0].charAt(0) != '-' ? 0 : 1); i--) {
    if (n == 3) {
      d = ',' + d;
      n = 0;
    }
    d = s[0].charAt(i) + d;
    n++;
  }
  s[0] = (s[0].charAt(0) == '-' ? '-' : '') + d;
  if (decimals > 0) {
    if (s.length == 2 && s[1] > 0) {
      s[1] = Math.round(('0.' + s[1]) * Math.pow(10, decimals));
      s[1] = new String(s[1]);
      for (i = s[1].length + 1; i <= decimals; i++) s[1] = '0' + s[1];      
    }
    else {
      s[1] = ''; for (i = 1; i <= decimals; i++) s[1] = s[1] + '0';
    }
    f = s[0] + '.' + s[1];
  }
  else {
    f = s[0];
  }
  return f;
}

function validateNumber(form, element) {
  var n, v;
  v = document.forms[form].elements[element].value;
  if (!isEmpty(v)) {
    n = new Number(v.replace(/,/g, ''));
    if (isNaN(n)) {alert('You must enter a valid number.'); setFocus(form, element); return false;}
  }
  return true;
}

function multiSelectSetSelected(form, element) {;
  var selectedOptions, i;
  selected = document.forms[form].elements['Selected ' + element];
  if (!selected) selected = document.forms[form].elements['Selected.20' + element];
  selectedOptions = '';
  for (i = 0; i < selected.options.length; i++) {
    if (selectedOptions != '') selectedOptions += ';';
    selectedOptions += selected.options[i].value;
  }
  document.forms[form].elements[element].value = selectedOptions;
}

function multiSelectAdd(form, element) {
  var available, selected, i;
  available = document.forms[form].elements['Available ' + element];
  if (!available) available = document.forms[form].elements['Available.20' + element];
  selected = document.forms[form].elements['Selected ' + element];
  if (!selected) selected = document.forms[form].elements['Selected.20' + element];
  if (isEmpty(available.options[available.selectedIndex].value))
    return;
  else {
    for (i = 0; i < selected.options.length; i++) {
      if (selected.options[i].value == available.options[available.selectedIndex].value) return;
    }
  }
  selected.options[selected.options.length] = new Option(available.options[available.selectedIndex].text, available.options[available.selectedIndex].value, false, false);
  selected.selectedIndex = selected.options.length - 1;
  multiSelectSetSelected(form, element)
  selected.focus();
}

function multiSelectRemove(form, element) {
  var selected, selectedIndex;
  selected = document.forms[form].elements['Selected ' + element];
  if (!selected) selected = document.forms[form].elements['Selected.20' + element];
  selectedIndex = selected.selectedIndex;
  if (selectedIndex == -1) return;
  selected.options[selectedIndex] = null;
  if (selected.options.length > selectedIndex)
    selected.selectedIndex = selectedIndex;
  else
    selected.selectedIndex = selected.options.length - 1;
  multiSelectSetSelected(form, element)
  selected.focus();
}