function dodelete(id, recordType) {
  if (confirm('Are you sure you want to delete this ' + recordType + ' entry?')) {
	  if (window.location.href.match(/\?/))
		  window.location.href = window.location.href + '&delete=' + id;
	  else
		  window.location.href = window.location.href + '?delete=' + id;
  }
}



function y2k(number) {
  return (number < 1000) ? number + 1900 : number; }

function HowOld(month,day,year) {
  var today = new Date();
  var thisYear = y2k(today.getYear());
  var thisMonth = today.getMonth()+1;
  var thisDay = today.getDate();

  var yearsold = thisYear - year, monthsold = 0;
  var daysold = 0, string = '';
  if (thisMonth >= month) monthsold = thisMonth - month;
  else { yearsold--; monthsold = thisMonth + 12 - month; }
  if (thisDay >= day)daysold = thisDay - day;
  else {
    if (monthsold > 0) monthsold--;
    else { yearsold--; monthsold+=11; }
    daysold = thisDay + 31 - day; }
  if (yearsold < 0) return '';
  if ((yearsold == 0) && (monthsold == 0) && (daysold == 0))
    return '<p>The date specified is in the future.</p>';
  if (yearsold > 0) {
    string = yearsold + ' year';
    if (yearsold > 1) string += 's';
    if (monthsold > 0 && daysold > 0) string += ', ';
    else if (monthsold <= 0 && daysold <= 0) string += '';
    else {string += ' and '} }
  if (yearsold < 3 && monthsold > 0) {
    string += monthsold + ' month';
    if (monthsold > 1) string += 's';
    if (daysold > 0) string += ' and '; }
  if (yearsold == 0 && monthsold < 3 && daysold > 0) {
    string += daysold + ' day';
    if (daysold > 1) string += 's'; }
  return string; }


// ### This should be made into a class, to hide these variables

var $images_to_rotate;
var $image_interval;
var $image_element;
var $image_index;

function image_rotation($element, $image_paths, $interval) {
	 $images_to_rotate = $image_paths;
	 $image_element = $element;
	 $image_interval = $interval;

	 $image_index = Math.floor(Math.random()*$images_to_rotate.length);
	 setTimeout("image_rotation_next_image()", 10);
 }

 function image_rotation_next_image() {
	 ++$image_index;
	 if ($image_index >= $images_to_rotate.length)
		 $image_index = 0;
	 document.getElementById($image_element).src = $images_to_rotate[$image_index];
	 setTimeout("image_rotation_next_image()", $image_interval);
 }

function set_date_control(control_name, value) {
	var date_control = $$('input[name="' + control_name + '"]')[0];
	var today = new Date();
	var month_lengths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
	switch(value) {
	case 'today':
		date_control.value = (today.getMonth()+1) + '/' + today.getDate() + '/' + (1900 + today.getYear());
		break;
	case 'endofmonth':
		date_control.value = (today.getMonth()+1) + '/' + month_lengths[today.getMonth()] + '/' + (1900 + today.getYear());
		break;
	case 'firstofmonth':
		date_control.value = (today.getMonth()+1) + '/' + month_lengths[today.getMonth()] + '/' + (1900 + today.getYear());
		break;
	default:
		date_control.value = value;
	}
}

// Gray buttons on admin edit pages call this function. It sets the value of the "preview"
// hidden control from the input parameter, then submits the form.  The input parameter is
// a URL.  The edit page will save the form's data to the database, then open a popup to display 
// the URL specified by the "preview" control.
function submit_and_test(page) {
	$preview_control = $$('input[name="preview"]')[0];
	$preview_control.value = page;
	$preview_control.form.submit();
	return false;
 }

bottom_margin_increment = 20;

// Functions for the printable version of the class flyer (and newsletter, soon).
// These allow setting of page breaks at appropriate points, hiding of sections,
// and showing/hiding the flyer's first-page footer.

// Handler for click on the green "+" button displayed after each flyer section.
function page_break_after() {
	// The ID of the button is xxx_break_after_button, where xxx is unique to the class section.
	// The div that "page-break-after: always" is applied to is named xxx_break_after_div.
	var elem = $(this.id.replace(/button$/, '') + 'div');

	if (elem.getStyle('pageBreakAfter') == 'always') {
		elem.setStyle({ pageBreakAfter : null });
		this.innerHTML = "/";
	}
	else {
		elem.setStyle({ pageBreakAfter : 'always'});
		this.innerHTML = " &mdash;&mdash;&mdash;&mdash; ";
	}
}

// Handler for click on the blue "-" button displayed after each flyer section.
function no_page_break_after() {
	// The ID of the button is xxx_nobreak_after_button, where xxx is unique to the class section.
	// The div that "page-break-after: never" is applied to has id xxx_nobreak_after_div.
	var elem = $(this.id.replace(/button$/, '') + 'div');
	elem.setStyle({ pageBreakAfter : 'never'});
}

// onclick handler for black "X" pseudo-button for hiding sections
function hide() {
	// The ID will be xxx_hide_button.
	// The elements that "display: none" is applied to have class xxx_hide_class.
	var class_to_hide = this.id.replace(/button$/, 'class');

	// If pseudo-button has 'x' (and not '!') in it, sections need to be hidden.
	var hide = (this.innerHTML == 'x');

	if (hide) {
		//  Hide all the elements with the class xxx_hide_class.
		$$("." + class_to_hide).each(function(el){ el.setStyle({ display: 'none' }); });

		// Change the pseudo-button from black "X" to red "!";
		this.innerHTML = "!";
		this.setStyle({ backgroundColor: 'red' });
	}
	else {
		//  Show all the elements with the class xxx_hide_class.
		$$("." + class_to_hide).each(function(el){ el.setStyle({ display: '' }); });

		// Change the pseudo-button back from red "!" to black "X"
		this.innerHTML = "x";
		this.setStyle({ backgroundColor: 'black' });
	}

}

function  popup(name, width, height) {
	if (!width)
		width = 700;
	if (!height)
		height = 600;
	var w = window.open('popup.php?popup=' + name, 'cupboard_popup', 'resizable=yes,toolbar=no,scrollbars=yes,width=' + width + ',height=' + height);
	w.focus();
	self.blur();
}

