/* note that after editing this you'll [currently] need to run rake js:min for career pro to see its javascript */
function show_programs()
{
	select = $('lead_program_id');
	label = $('campus_label');
	Element.replace('lead_program_id', campus_options);
	if(label!=null) {
		label.innerHTML = campus_label_text;
	}
}
function hide_programs()
{
	label = $('campus_label');
	$('lead_program_id').innerHTML = '<option value="">No programs found.</option>';
  $('lead_program_id').value = '';
	if(label!=null) {
		label.innerHTML = '';
	}
}

function get_popup(school_id, program) {
	params = 'school_id='+ school_id +'&program='+ program;
	new Ajax.Request('/distribution/program_message', {asynchronous:true, evalScripts:true, parameters:params});
}

function fetch_programs(zip,school_id,program_id){
	if(zip==null) return;
	zip = zip.toString();
	if (zip.length == 5){
		params = 'zip=' + zip + '&school_id=' + school_id + '&program_id=' + program_id;
		new Ajax.Request('/distribution/campus_programs', {asynchronous:true, evalScripts:true, parameters:params});
	}
}

function fetch_programs_by_location_and_zip(zip,campus_location_id,school_id){
  if(zip==null || campus_location_id==null || school_id==null) return;
  if(isNaN(parseInt(campus_location_id)) || isNaN(parseInt(school_id))) return;

  params = 'school_id=' + school_id.toString();
  params += '&zip=' + zip.toString();
  params += '&campus_location_id=' + campus_location_id.toString();
  new Ajax.Request('/distribution/campus_programs', {asynchronous:true, evalScripts:true, parameters:params});
}

function highlight_fields_with_errors(fields_with_errors) {
  if (fields_with_errors != null) 
    fields_with_errors.each(function(field) {
      Element.addClassName(field,'error');
    });
}

function add_field_errors(errors,position) {
  if (errors != null) 
    errors.each(function(error) {
      element = $(error[0]);
      if (element != null) {
        html = '<span class="error">' + error[1] + '</span>';
        switch (position) {
        case 'before': new Insertion.Before(element, html); break;
        case 'bottom': new Insertion.Bottom(element, html); break;
        case 'top': new Insertion.Top(element, html); break;
        default : new Insertion.After(element, html); /* after */
        }
      }
    });
}

function clear_field_errors(parent_element) {
  Element.getElementsByClassName(parent_element,"error").each(function(element) {
    Element.remove(element);
  });
}

function hideAll(element,className) {
	element.select('[class='+className+']').each(function(child) {
    Element.hide(child);
  });
}

function changeRadioColor(element_id) {
	$('main').select('[class="radio_text"]').each(function(child) {
    child.style.color='#666';
  });
  Element.next(element_id).style.color='#d10000';
}

function selectSchool(school_id) {
  // highlight
  Element.addClassName('school_'+school_id+'_programs','highlighted');
  Element.show('school_'+school_id+'_deselect');
}

function deselectSchool(school_id) {
  // unhighlight
  Element.removeClassName('school_'+school_id+'_programs','highlighted');
  Element.hide('school_'+school_id+'_deselect');
  // deselect radio buttons
  $('school_'+school_id+'_programs').select('[class="radio"]').each(function(child) {
    child.checked = false;
  });
}

/* clears the military form if they click on the 'no' military radio */
function clear_military_wrap() {
	Element.hide("lead_military_wrap");
	$('lead_military_branch').value = '';
	$('lead_military_status').value = '';
	$('lead_military_relationship').value = '';
}


function getApiDocs(url,school) {
  if (school == '' || school == null) {
    window.location = url;
  } else {
    window.location = url+"?school="+school;
  }
}

function disableBudgetFields(value) {
	['MonthlyBudget','DailyBudget'].each(function(v) {
		if (value == v) {
			$(v+'Fields').select('.'+v+'Field').invoke('enable');
			$(v+'Fields').show();
		} else {
			$(v+'Fields').select('.'+v+'Field').invoke('disable');
			$(v+'Fields').hide();
		}
	});
}

function toggleCollegeCredits(education_level_id) {
  if (education_level_id == '4') {
    Element.show('college_credits_wrap');
  } else {
    Element.hide('college_credits_wrap');
  }
}