function show_research(form,element) {
  form.action = "?publication_subtype="+element
  form.submit();
}

var current_step = 0;
function next_step() {
  switch (current_step) {
    case 0:
      current_step++;
      break;
    case 1:
      current_step++;
      break;
    case 2:
      current_step++;
      break;
    case 3:
      current_step++;
      build_review()
      break;
  }
  Element.hide('step_'+(current_step-1));	
  Element.show('step_'+current_step);	
  Element.hide('step_'+(current_step-1) + '_content_header');	
  Element.show('step_'+current_step + '_content_header');	
  set_indicator();
}

function previous_step() {
  switch (current_step) {
    case 1:
      current_step--;
      break;
    case 2:
      current_step--;
      break;
    case 3:
      current_step--;
      break;
    case 4:
      current_step--;
      break;
  }
  Element.hide('step_'+(current_step+1));	
  Element.show('step_'+current_step);
  Element.hide('step_'+(current_step+1) + '_content_header');	
  Element.show('step_'+current_step + '_content_header');	
  set_indicator();
}

function TestObj(id) { 
  YAHOO.util.Event.onAvailable(id, this.handleOnAvailable, this);
} 

TestObj.prototype.handleOnAvailable = function(me) { 
//  alert(this.id + " is available"); 
  Element.hide(this.id);
} 
	 
//var obj = new TestObj("step_1"); 
//var obj = new TestObj("step_2"); 
//var obj = new TestObj("step_3"); 
//var obj = new TestObj("step_4"); 

function set_indicator() {
  $('indicator').setStyle({
    backgroundPosition: '0 -'+current_step*30+'px'
  });
}

function add_field(element, total) {
  for (i = 1; i < total; i++) {
    if (Element.getStyle(element+'_'+i, 'display') == "none") {
      Element.show(element+'_'+i);      
      break;
    }
  }
}

function remove_field (element) {
  fields = $(element).select('select', 'input', 'textarea');
  for (i = 0; i < fields.length; i++) {
    Field.clear(fields[i]);
    if (fields[i].className == "other") {
      Element.hide(fields[i]);
    }
  }
  Element.hide(element);
  children = $(element).select('[class="removable"]')
  for (i = 0; i < children.length; i++) {
      Element.hide(children[i]);
  }	
}

function funder_change (funder, index) {
  if ($F(funder) == 'NSF') {
    // then show program row and hide & clear funder_other
	//    Element.show('program_'+index);    
  }
  else {
//    remove_field('program_'+index);    
//    remove_field('program_'+index+'_other');    

  }
  if ($F(funder) == 'Other') {
     //show funder_other and hide & clear program_row
//    remove_field('program_'+index);    
    Element.show('funder_'+index+'_other');
  }
  else {
    remove_field('funder_'+index+'_other');
  }
}

function program_change (program, index) {
  if ($F(program) == 'Other') {
     //show funder_other and hide & clear program_row
    Element.show('program_'+index+'_other');
  } else {
    remove_field('program_'+index+'_other');    
  }
}

function check_box_highlight(element,id) {
  if ($F(element+'['+id+'][name]') != null) {
    $(element+id).setStyle({backgroundColor: '#eef6e7'});
  }
  else {
    $(element+id).setStyle({backgroundColor: '#ffffff'});
  }
}

function checkbox_other_highlight(element) {
 if ($F(element+'[other]') != null) {
    $(element+'_other').setStyle({backgroundColor: '#eef6e7'});
  }
  else {
    $(element+'_other').setStyle({backgroundColor: '#ffffff'});
  }
}

function build_review() {
  // get everyone whose class is review
  review_element_array = $$('div.review');
  // iterate through the array
  for (i = 0; i < review_element_array.length; i++) {
    var current_id = review_element_array[i].identify();
    current = current_id.substring(current_id.indexOf("_") + 1);
    if (current_id.indexOf("_checkboxes") > 0 ) {
      str = "";
      current = current.substring(0, current.indexOf("_checkboxes"));
      checkboxes = $$('input.'+current);
      for (j = 0; j < checkboxes.length; j++) {
        if ($F(checkboxes[j]) != "" && $F(checkboxes[j]) != null ) {
	  str = str + $F(checkboxes[j]) + " ";
        }
      }	
      $(current_id).update(str);      
      Element.show(current_id);
    }
    else {    
      if ($F(current) != "" && $F(current) != null) {
        $(current_id).update($F(current));
        Element.show(current_id);
      }
    }
  }

  optional_element_array = $$('.optional');
  for (i = 0; i < optional_element_array.length; i++) {
    review_elements = optional_element_array[i].select('div.review');
    for (j = 0; j < review_elements.length; j++) {
      if (review_elements[j].getStyle('display') != 'none') {
        Element.show(optional_element_array[i]);
      }
    }
  }
}


function role_change (role, index) {
  if ($F(role) == 'Other') {
     //show funder_other and hide & clear program_row
    Element.show('project_team_role_'+index+'_other');
  } else {
    remove_field('project_team_role_'+index+'_other');    
  }
}


function project_check() {
  title = $F('project_title');	
//  alert(title);
  new Ajax.Updater('project_check', '/project/project_exists/'+title, { 
  method: 'get'});
	
}

function form_test() {
  new Ajax.Updater('form_test', '/project/form_test', { 
  method: 'get'});
	
}

function add_fields(element, total) {
  for (i = 1; i < total; i+=3) {
    if (Element.getStyle(element+'_'+i, 'display') == "none") {
      Element.show(element+'_'+i);      
      Element.show(element+'_'+(i+1));      
      Element.show(element+'_'+(i+2));      
      break;
    }
  }
}
