function toggleId (id) {
  var e = document.getElementById(id);
  if (e.style.display == "none" || e.style.display == "") {
    e.style.display = "block";
  }
  else {
    e.style.display = "none";
  }
}

function toggleIdOn (id) {
	var e = document.getElementById(id);
	e.style.display = "block";
}

function toggleIdOff (id) {
	var e = document.getElementById(id);
	e.style.display = "none";
}

/**
 * Test if a date is of the form 13 Jul 2008 or 7 aug 08.
 * @param {string} date
 * @returns {Boolean} indicates a valid date
 */
var DATE_RE = /^\s*(\d|0[1-9]|[012]\d|3[01])\s*(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\w*\s*((?:20)?[01]\d)\s*$/i;
function testDate(date) {
	return DATE_RE.test(date);
}

/**
 * Ensure a date is of valid format.
 * @param {string} date
 * @returns {Boolean} indicates a valid date
 */ 
function verifyDate(date) {
	if (!date) {
		alert('Date is missing!');
		return false;
	}
	if (testDate(date)) {
		return true;
	}
	alert('Date must be of the form 17 Jul 08 (not ' + date + ')');
	return false;
}

/**
 * Ensure all dates on a form are valid.
 * @param {HTMLElement} form
 * @returns {Boolean} indicates all valid dates
 */ 
function verifyDates(form) {
	var elements = form.elements;
	for(var i = 0, ok = true, elem; ok && (elem = elements[i]); i++) {
		if (!elem.name.match(/date/)) continue;
		ok = verifyDate(elem.value);
	}
	return ok;
}

/**
 * Find a particular option in a <select> and select it.
 * @param {HTMLElement} select
 * @param {string} option the option value we're searching for
 */
function selectOption(select, option) {
	for (var s, i = 0; (s = select.options[i]); i++) {
		if (s.value == option) {
			select.selectedIndex = i;
			select[0].value = option;
			return;
		}
	}
	alert('Could not find ' + option + ' in ' + select.name);
}

/**
 * Find a particular option in a <select>, fix up the button text to be
 * Update for the form handler, and submit.
 * @param {HTMLElement} element the button
 * @param {string} option the option value we're searching for
 */
function selectAndSubmit(element, select_name, option) {
	selectOption(element.form[select_name], option);
	element.value = 'Update';
	element.form.submit();
}
