function check_party(party_id) {
    
    remaining_lawyers = $$('.lawyer_object');
    
    if (remaining_lawyers.length == 0)
    {   
        grey_out_matter()
    }
    else
    {
        reactivate_matter()
    }
    
    remaining_rows = $('party_lawyer_table_'+party_id).rows.length;    
    if (remaining_rows == 0)
    {
        grey_out_party('partycell_'+party_id,'partycellheader_'+party_id);
    }
    else
    {
        reactivate_party('partycell_'+party_id,'partycellheader_'+party_id);
    }        
}

function add_lawyer_id(lawyer_id) {
    $$("input.lawyer_ids_including_judge").each(function(e) {
        lawyer_ids = $A(e.value.split(','));
        lawyer_ids = lawyer_ids.concat(lawyer_id);
        e.value = lawyer_ids.join(',');
    });
}

function delete_lawyer_id(lawyer_id) {
    $$("input.lawyer_ids_including_judge").each(function(e) {
        lawyer_ids = $A(e.value.split(','));
        lawyer_ids = lawyer_ids.without(lawyer_id);
        e.value = lawyer_ids.join(',');
    });
}

function grey_out_party(element_name, element_header_name) {
    $(element_name).style.color = '#cccccc';    
    $(element_header_name).style.textDecoration = 'line-through';
    $(element_header_name).style.background = '#cccccc';    
}

function reactivate_party(element_name, element_header_name) {    
    $(element_name).style.color = '';    
    $(element_header_name).style.textDecoration = '';
    $(element_header_name).style.background = '';    
}

function grey_out_matter()
{
    el = $('matter_area');
    el.style.color = '#cccccc';
    el.style.textDecoration = 'line-through';
}
function reactivate_matter()
{
    el = $('matter_area');
    el.style.color = '#000';
    el.style.textDecoration = 'none';
}


function matter_not_yet_concluded_click() {
  var chx = $("matter_not_yet_concluded");
  if (chx && chx.checked) {
      $("matter_disposition_datefield").disabled = true;
  } else {
      $("matter_disposition_datefield").disabled = false;
      $("matter_disposition_datefield").focus();
  } 
}

function trap_enter_and_submit_search_subform(event, subform_type, subform_id) {    
  key_code = event.keyCode
  
  if (key_code == Event.KEY_RETURN)
  {          
    submit_search_subform(subform_type, subform_id)
    return false;
  }
}

function reset_search_subform(subform_id) {
    slide_up_if_shown(subform_id + '_search_results');
    Element.hide(subform_id + '_search_buttons');
    Element.hide(subform_id + '_search_control');
    $(subform_id + '_lastname_error').innerHTML = "";
}



function close_search_subform(subform_id) {
    reset_search_subform(subform_id);
    Element.show(subform_id + '_search_text');
}

function close_search_results(subform_id, party_id, allow_additional_selection)
{
    reset_search_subform(subform_id)
    if (party_id != "") {
        check_party('' + party_id)
    }
    if (allow_additional_selection)
    {
        Element.show(subform_id + '_search_text');
    }
    
}

function submit_search_subform(subform_type,subform_id) {  
    clear_all_default_text('input.default');
    $(subform_id + '_search_results').hide();
    
    if (validate_not_blank( subform_id + '_search_control', 
                            subform_type + '[lastname]', 
                            subform_id + '_lastname_error', 
                            "Please enter a last name"))
    {
        submit_search_form_helper(  '/case/find_' + subform_type,
                                  subform_id + '_search_control',
                                  subform_id + '_search_buttons',
                                  subform_id + '_searching', 
                                  subform_id + '_search_results')
    }
    restore_all_default_text('input');
}

function set_max_height(results_div, max_height)
{
  // fake max-height on IE
  $(results_div).setStyle({
    height: "auto"
  });
 
  results_height = $(results_div).getHeight() >= max_height? 
    max_height + "px" : $(results_div).getHeight() + 15 + "px" ;
    
  $(results_div).setStyle({
    height: results_height
  });
}

function submit_search_form_helper(action, parameter_div, buttons_div, intermediate_div, results_div) {
      
  Element.show(intermediate_div);
  $(buttons_div).hide();
  
  // we need to collect all the lawyers/judges who are part of the matter in the browser
  // and send them to the server, so the server knows to disable the HTTP link for those lawyesr
  lawyers = $$('.lawyer_object').concat($$('.judge_object'))
  str = Form.serialize(parameter_div)+"&";
  for (i=0; i<lawyers.length;i++)
  {
      str += Form.serialize(lawyers[i]);
      if (i < lawyers.length - 1)          
        str += "&";    
  }    
  
  new Ajax.Updater( results_div, 
    action, 
    { asynchronous:true, 
      evalScripts:true, 
      onComplete:function(request)
      {
        Element.hide(intermediate_div);
        $(buttons_div).show();
        set_max_height(results_div, 300);
        slide_down_if_hidden(results_div);
      }, 
      parameters:str
    }); 
      
  
  return false;
}

function slide_up_if_shown(div)
{
    if ($(div).visible())
    {
        new Effect.SlideUp(div,{duration:0.5});
    }
}

function slide_down_if_hidden(div)
{
    if (!$(div).visible())
    {
        new Effect.SlideDown(div,{duration:0.5});
    }
}

function validate_not_blank(search_control_root, form_field, error_field, message)
{
    $(error_field).innerHTML = '';
    if ($F($(search_control_root).down('input[id="' + form_field + '"]')).strip() == "")
    {
        $(error_field).innerHTML = message;
        return false;
    }
    return true;
}

function show_update_dispute(lawyer_id)
{    
    if (last_selected)
    {
        $("lawyer_action_"+last_selected).style.display = 'none';            
    }    
    
    if (lawyer_id != "")
    {
        last_selected = lawyer_id;
        $("lawyer_action_"+lawyer_id).style.display = 'block';
    }
    else
    {
        last_selected = null;            
    }

}
function update_dispute_not_in_list()
{    
    $('update_dispute_lawyer_id').value = ''; 
    show_update_dispute($('update_dispute_lawyer_id').value)
    Element.show('new_lawyer_search_control');
    Element.show('new_lawyer_search_buttons');
    Element.hide('new_lawyer_search_text');
}
function submit_add_opposition(form)
{        
    form.add_party.value = '1';
    form.submit();
}