function fill_content_type(elem) {
  new_content_type = "application/octet-stream"; //the default
  if (!elem.value) {return(true)}

  //allowed extensions
  var ext = Array;
  ext['pdf'] = "application/pdf";
  
  //word
  ext['doc'] = "application/msword";
  
  //excel
  ext['xla'] = "application/vnd.ms-excel";
  ext['xlc'] = "application/vnd.ms-excel";
  ext['xlm'] = "application/vnd.ms-excel";
  ext['xls'] = "application/vnd.ms-excel";
  ext['xlt'] = "application/vnd.ms-excel";
  ext['xlw'] = "application/vnd.ms-excel";
  
  found = elem.value.match(/\.([^.]*)$/);
  if (found && found[1]) {
    new_content_type = ext[found[1].toLowerCase()] || new_content_type;
  }
  
  $('content-type').value = new_content_type;
}

// Event handler fired when user clicks to create page with editor
function show_newsletter_editor(event) {
	$$('.nl_mce_spacing').invoke('hide');
	init_page_editor();		// Using same MCE config as pages
	document.stopObserving('lightview:opened', show_newsletter_editor);
}

function hide_newsletter_editor(event) {
	tinyMCE.execCommand('mceRemoveControl', false, 'newsletter_html_content');
	$$('.nl_mce_spacing').invoke('show');
}

function show_page_editor(event) {
	init_page_editor();
	$$('.nl_mce_spacing').invoke('hide');
	document.stopObserving('lightview:opened', show_page_editor);
	// Going to want to hide editor BEFORE lightview closes, so lightview:hidden no workee for that.
	// $$('.lv_Button.lv_Close').invoke('observe','click',hide_page_editor);
	// new theory: destroy all of those divs when lightview hides 'em, which is handled by lightview:hidden in _combined
}

function hide_page_editor(event) {
	tinyMCE.execCommand('mceRemoveControl', false, 'page_html_content');
	$$('.nl_mce_spacing').invoke('show');
}

function show_info_editor(event) {
	$$('.nl_mce_spacing').invoke('hide');
	init_intro_editor();
	document.stopObserving('lightview:opened', show_info_editor);
	$$('.lv_Button.lv_Close').invoke('observe','click',hide_info_editor);
}

function hide_info_editor(event) {
	tinyMCE.execCommand('mceRemoveControl', false, 'info_html_content');
	$$('.nl_mce_spacing').invoke('show');
}

function show_introduction_editor(event) {
	$$('.nl_mce_spacing').invoke('hide');
	init_introduction_editor();
	document.stopObserving('lightview:opened', show_introduction_editor);
	//$$('.lv_Button.lv_Close').invoke('observe','click',hide_page_editor);
}

// For onSubmit handled below (validate_nms_form) - if an element isn't filled in,
// highlights the element and returns true
function flag_missing(elem) {
	missing = false;
	if (elem.type == 'text') { missing = !elem.present(); }
	// TODO: implement for field types other than 'text'
	if (missing) {
		elem.setStyle({'background': '#FFBBBB'});
		elem.focus();
		return true;
	} else {
		return false;
	}
}
// An onSubmit handler which users of our FormMail cgi-bin can use to validate
// before the form is posted.  Call with optional "debug" parameter set to "true"
// for debugging.
function validate_nms_form(form,debug) {
	if (debug === undefined) {
		debug = false;
	}
	try {
		// Array of all "required" elements in the form, by name
		required = form.select('input[name="required"]').first().getValue().split(/,/);
		if (required.length == 0) { return true; }

		missing_elements = form.select('input').findAll( function(elem) {
			if (required.include(elem.readAttribute("name"))) {
				return flag_missing(elem);
			} else {
				return false;
			}
		} );

		if (missing_elements.length > 0) {
			alert("Please fill in all required fields.  Thanks!");
			return false;
		}
	}
	catch(e) {
		if (debug) { alert(e); return false; }
		else { return true; }
	}
}

// Add 
function add_form_validation() {
	$$('form.nms_validate').each( function(f) {
		f.writeAttribute('onSubmit','return validate_nms_form(this);');
	} );
}

document.observe('dom:loaded', function() {add_form_validation();});
