// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function add_border (side) {
	elements = $$('.add_' + side + '_border_on_expand');
	elements.each (
		function(item){
			item.addClassName(side + '_border_column');
		}
	);
}

function add_to_print (element_ids) {
	element_ids.each(
		function(item) {
			$(item).removeClassName('no_print')
		}
	);
}

function close_modal() {
	$$('.popup_container').each(function(item){item.hide();});
  $('fadeout').hide();
  if ($('form_close_button') != undefined) {
    $('form_close_button').remove();
  }
}

function collapse_right_pane() {
	$('split_screen_form').show();
	$$('.expand_to_full').each (
		function(item){
			item.removeClassName('grid_12');
			item.addClassName('grid_8');
		}
	);
}

function hide_expanded_content() {
	elements_to_hide = $$('.expand_only');
	elements_to_show = $$('.hide_on_expand');

	hide_element_collection(elements_to_hide);
	show_element_collection(elements_to_show);
	ensure_never_print();
	
	$$('.hide_on_collapse').each (
		function(item) {
			item.hide();
			item.removeClassName('hide_on_collapse');
		}
	)
}

function disable_peer_coaches() {
	all_peer_coaches = $$('input[type="checkbox"][id^="session[program_participant_ids][]"]');

	if ($F('session_taught_by_coach_coordinator_true') == 'true') {
		all_peer_coaches.each(function(item){item.disable();item.checked = false;});
		$('peer_coach_selection_row').hide();
	} else {
		all_peer_coaches.each(function(item){item.enable();});
		$('peer_coach_selection_row').show();
	}
}

function ensure_never_print() {
	$$('.never_print').each(
		function(item){
			if (!item.hasClassName('no_print')) {
				item.addClassName('no_print');
			}
		}
	);	
}

function expand_right_pane() {
	$('split_screen_form').hide();
	$$('.expand_to_full').each (
		function(item){
			item.removeClassName('grid_8');
			item.addClassName('grid_12');
		}
	);
}

function show_expanded_content() {
	$$('.hide_on_expand').each (
		function(item){
			if (!item.visible()) {
				item.addClassName('hide_on_collapse');
			}
		}
	);
	
	elements_to_hide = $$('.hide_on_expand');
	elements_to_show = $$('.expand_only');
	
	hide_element_collection(elements_to_hide);
	show_element_collection(elements_to_show);
	ensure_never_print();
}

function force_expanded() {
	if (!$('split_screen_form').visible()) {
		$('split_screen_display').removeClassName('grid_8');
		$('split_screen_display').addClassName('grid_12');
	}
}

function hide_element_collection(element_collection) {
	element_collection.each(
		function(item){
			item.hide();
		}
	);
}

function remove_border (side) {
	elements = $$('.add_' + side + '_border_on_expand');
	elements.each (
		function(item){
			item.removeClassName(side + '_border_column');
		}
	);
}

function remove_from_print (element_ids) {
	element_ids.each(
		function(item) {
			$(item).addClassName('no_print')
		}
	);	
}

function show_element_collection(element_collection) {
	element_collection.each(
		function(item){
			item.show();
		}
	);
}


function hide_section (section_name) {
	elements_to_show = $$('.' + section_name + '_hidden');
	elements_to_hide = $$('.' + section_name + '_shown');
	hide_element_collection(elements_to_hide);
	show_element_collection(elements_to_show);	
}

function show_section (section_name) {
	elements_to_show = $$('.' + section_name + '_shown');
	elements_to_hide = $$('.' + section_name + '_hidden');
	hide_element_collection(elements_to_hide);
	show_element_collection(elements_to_show);	
}

function show_working() {
	$('fadeout').show();
	$('working_icon').show();	
}

function hide_working() {
	$('fadeout').hide();
	$('working_icon').hide();
}

Element.addMethods({
	visibility_hidden: function(element) {
		element = $(element);
		element.setStyle({visibility: 'hidden'});
		return element;
	},
	
	visibility_visible: function(element) {
		element = $(element);
		element.setStyle({visibility: 'visible'});
		return element;
	}
});

function update_forecast_counts(quarter) {
  var total_enrollments = 0;
  var total_placements = 0;
  var total_exits = 0;
  $$('.planned_enrollments').each(function(item){total_enrollments += parseInt(item.value)});
  $$('.planned_placements').each(function(item){total_placements += parseInt(item.value)});
  $$('.planned_exits').each(function(item){total_exits += parseInt(item.value)});
  payrolls.keys().each(
    function(item){
      p = payrolls.get(item);
      total_enrollments += p.enrollment_count;
      total_placements += p.placement_count;
      total_exits += p.exit_count;
    }
  );
  $('enrollments_totals').update(total_enrollments);
  $('placements_totals').update(total_placements);
  $('exits_totals').update(total_exits);
  quarterly_enrollments = quarters.get(quarter).enrollment_count;
  quarterly_placements = quarters.get(quarter).placement_count;
  quarterly_exits = quarters.get(quarter).exit_count;
  $$('.planned_enrollments_q' + quarter).each(function(item){quarterly_enrollments += parseInt(item.value)});
  $$('.planned_placements_q' + quarter).each(function(item){quarterly_placements += parseInt(item.value)});
  $$('.planned_exits_q' + quarter).each(function(item){quarterly_exits += parseInt(item.value)});
  $('quarterly_enrollments_' + quarter).update(quarterly_enrollments);
  $('quarterly_placements_' + quarter).update(quarterly_placements);
  $('quarterly_exits_' + quarter).update(quarterly_exits);
}

function remove_run_out_row_style() {
	$$('.run_out_row').each(function(item){
		item.removeClassName('run_out_row');
	});
}

function highlight_updated_row(payroll_id) {
	var payroll_row = $('payroll_row_' + payroll_id);

	payroll_row.highlight({
		beforeStart: set_background_to_transparent(payroll_row),
		startcolor: '#99cc99',
		endcolor: '#ffffff'
	});
	
	// Why not just use the restore color param in the highlight method?
	// Because IE breaks using dynamically determined restorecolor values.
	restore_background_color(payroll_row);
}

function restore_background_color(payroll_row) {
	var restore_color = determine_restore_color(payroll_row);
	payroll_row.setStyle({backgroundColor: restore_color});
}

function determine_restore_color(payroll_row) {	
	var restore_color
	if (payroll_row.hasClassName('run_out_row')) {
		restore_color = '#ff5555';
	} else {
		if (payroll_row.hasClassName('even_row')) {
			restore_color = '#ccccff';
		} else {
			restore_color = 'transparent';
		}
	}
	return restore_color;
}

function set_background_to_transparent(payroll_row) {
	payroll_row.setStyle({backgroundColor: 'transparent'});
}

function hide_collection_toggle_element(collection_name,element_name) {
	elements_to_hide = $$('.' + collection_name);
	elements_to_hide.each(
		function(item){
			if (item == $(element_name)) {
				Element.toggle(element_name);				
			} else {
				item.hide();
			}
		}
	);
}

function navigate_to_url(url) {
	if (url != 'null') {
		window.location.href = url;
	}
}

function print_casenotes() {
	elements_to_hide = $$('.assessment_form_print');
	elements_to_show = $$('.casenote_print');
	hide_element_collection(elements_to_hide);
	show_element_collection(elements_to_show);
	window.print();
}

function print_assessment() {
	elements_to_hide = $$('.casenote_print');
	elements_to_show = $$('.assessment_form_print');
	hide_element_collection(elements_to_hide);
	show_element_collection(elements_to_show);
	window.print();
}

