function setFocus(elemId) {
  $(elemId).focus();
}

function $(elemId) {
  return document.getElementById(elemId);
}

function toggleButtonValue(button) {
  // alert(button.value);
  if (button.value == 'available') {
    button.value = 'will be requested'
    button.style.color = 'maroon';
    button.style.fontWeight = 'bold';
    setHiddenField(button, 'true');
  }
  else {
    button.value = 'available'
    button.style.color = 'black';
    button.style.fontWeight = 'normal';
    setHiddenField(button, 'false');
  }
  return false;
}

function toggleButtonValue2(button) {
  // alert(button.value);
  if (button.value == 'available') {
    button.value = 'will be requested'
    button.innerHTML = 'will be requested\n'
    button.style.color = 'maroon';
    button.style.fontWeight = 'bold';
    setHiddenField2(button, 'true');
  }
  else {
    button.value = 'available'
    button.innerHTML = 'available\n'
    button.style.color = 'black';
    button.style.fontWeight = 'normal';
    setHiddenField2(button, 'false');
  }
  return false;
}

function setHiddenField(button, toWhat) {
  var posColon = button.id.lastIndexOf(':');
  var idWithoutLastSegment = button.id.substring(0, posColon);
  var correspondingHiddenField = document.getElementById(idWithoutLastSegment + ':idHidden');
  correspondingHiddenField.value = toWhat;
}

function setHiddenField2(button, toWhat) {
  var posColon = button.parentNode.id.lastIndexOf(':');
  var idWithoutLastSegment = button.parentNode.id.substring(0, posColon);
  var correspondingHiddenField = document.getElementById(idWithoutLastSegment + ':idHidden');
  correspondingHiddenField.value = toWhat;
}

function showAvailableButtonValues() {
  var availList = document.getElementsByTagName('input');
  var buttonString = "";
  for (var button in availList) {
    // alert('button is ' + button);
    if (availList[button].type == 'submit') {
      var id = availList[button].id;
      if (id.lastIndexOf('idAvailableIntervalButton') != -1) {
        buttonString += availList[button].value + "  ";
      }
    }
  }
  alert(buttonString);
  return false;
}

function showHiddenFieldValues() {
  var hiddenList = document.getElementsByTagName('input');
  var hiddenString = "";
  for (var element in hiddenList) {
    if (hiddenList[element].type == 'hidden') {
      var id = hiddenList[element].id;
      if (id.lastIndexOf('idHidden') != -1) {
        var val = hiddenList[element].value;
        hiddenString += "hidden field id is " + id + "\n";
        hiddenString += "hidden field value is " + val + "\n";
      }
    }
  }
  alert(hiddenString);
  return false;
}

function extractFromTime(timeSlotTime) {  // timeSlotTime example: "12:00pm to 01:00pm"
  var time = timeSlotTime.substring(0, 5);
  if (timeSlotTime.substring(5, 7).toLowerCase() == "am")
    return time;
  if (time == "12:00")
    return time;
  var timeInt = time.substring(0, 2);
  var hour24 = parseInt(timeInt) + 12;
  return hour24.toString() + time.substring(2);
}

function extractToTime(timeSlotTime) {  // timeSlotTime example: "12:00pm to 01:00pm"
  var tokens = timeSlotTime.split(' ');
  var time = tokens[2].substring(0, 5);
  if (tokens[2].substring(5, 7).toLowerCase() == "am")
    return time;
  if (time == "12:00")
    return time;
  var timeInt = time.substring(0, 2);
  var hour24 = parseInt(timeInt) + 12;
  return hour24.toString() + time.substring(2);
}

function openProfile(href, width, height) {
  window.open(href, 'profile', 'resizable=yes,toolbar=yes,menubar=yes,status=yes,location=yes,scrollbars=yes,width='+width+',height='+height);
}

function openAdrWindow(href, target, width, height) {
  window.open(href, target, 'resizable=yes,toolbar=yes,menubar=yes,status=yes,location=yes,scrollbars=yes,width='+width+',height='+height);
}

/* the following two functions alternate the background color of the buttons */
function activate(item) {
  item.style.backgroundColor="rgb(250, 247, 201)";
}
function deactivate(item) {
  item.style.backgroundColor="white";
}
