var leErrorMessage;
var leSubmitForm;
var leButtonClicked;

function leListboxCheck(fld, desc) {
  if ( listboxPicked(fld) == 0 ) { leBadField(fld, "You must select a " + desc + "."); }
}

function leBadField(f, msg) {
  f.style.color = "FFFFFF";
  f.style.backgroundColor = "FF0000";
  if ( leErrorMessage == "" ) {
    f.focus();
    if ( f.type == "text" ) { f.select(); }
    leErrorMessage = msg;
  }
}

function leSelectItem(strRow) {
  var i, j, el, strItem, temp;
  if ( self.leSelectItemExit ) { leSelectItemExit(1, strRow); }
  var tabIndexOffset = 100
  var arrRow = strRow.split(";")
  var maxTabIndex = tabIndexOffset + arrRow.length - 1
  for (i=0; i<document.f.length; i++) {
    el = document.f.elements[i];
    temp = el.id;
    if ( temp.substring(0, 2) == 'le' ) {
      strItem = arrRow[parseInt(temp.substring(2), 10)];
      switch (el.type) {
      case 'text': el.value = strItem; break;
      case 'hidden': el.value = strItem; break;
      case 'select-one': el.value = strItem; break;
      case 'select-multiple' : 
        for (j=0; j<el.length; j++) {
          el.options[j].selected = ( ( ( 1 << ( el.options[j].value - 1 ) ) & strItem ) != 0 );
        }
        break;
      case 'checkbox': el.checked = (strItem == "1") || (strItem == "*"); break;
      default: alert('Unsupported form element type - "' + el.type + '".');
      }
    }
  }
  if ( self.leSelectItemExit ) { leSelectItemExit(2, strRow); }
}

function leValidateInit() {
  leErrorMessage = "";
}

function leValidateEnd() {
  leSubmitForm = ( leErrorMessage == "" );
  if ( leSubmitForm ) {
//    alert('SUBMIT stopped');
//    leSubmitForm = false;
  } else {
    alert(leErrorMessage);
  }
}

function leCallValidateSelection() {
  if ( self.leValidateSelection ) {
    leValidateSelection();
  } else {
    alert('Warning: Javascript function leValidateSelection() is not defined and was not called.');
  }
}

function leCallValidateFormData() {
  if ( self.leValidateFormData ) {
    leValidateFormData();
  } else {
    alert('Warning: Javascript function leValidateFormData() is not defined and was not called.');
  }
}

function clickButtonAdd() {
  leButtonClicked = 'add';
  leValidateInit();
  leCallValidateFormData();
  leValidateEnd();
  return leSubmitForm;
}

function clickButtonMod() {
  leButtonClicked = 'mod';
  leValidateInit();
  leCallValidateSelection();
  if ( leErrorMessage == '' ) { leCallValidateFormData(); }
  leValidateEnd();
  return leSubmitForm;
}

function clickButtonDel() {
  leButtonClicked = 'del';
  leValidateInit();
  leCallValidateSelection();
  leValidateEnd();
  return leSubmitForm;
}

function clickButtonRef() {
  leButtonClicked = 'ref';
  leValidateInit();
  leValidateEnd();
  return leSubmitForm;
}

function clickButtonClr() {
  leButtonClicked = 'clr';
  leSelectItem(leGetClearRow());
  return false;
}
  
