function force_recurring_changed() {
	var force_recurring = $('subscription_plan_force_recurring').checked;
	$('subscription_plan_charge_after_first_period').disabled = !force_recurring;

	if (!force_recurring) {
		$('subscription_plan_charge_after_first_period').checked = false;
		disable_custom_charge_later_fields(!force_recurring);
	}
}

function force_charge_later() {
	var charge_later = $('subscription_plan_charge_after_first_period').checked;
	disable_custom_charge_later_fields(!charge_later);
}

function disable_custom_charge_later_fields(disable) {
	$('subscription_plan_charge_later_duration_quantity').disabled = $('subscription_plan_charge_later_duration_units').disabled = disable;
	if (disable) {
		$('subscription_plan_charge_later_duration_quantity').value = '';
	}
}

function expiration_label_reflects_plan_type() {
  var selected = $('subscription_plan_plan_type').value;
  switch (selected) {
    case 'free_trial':
      $('expirationLabel').innerHTML = 'Subscriptions expire after ...';
      break;
    case 'metered':
      $('expirationLabel').innerHTML = 'Fees are captured every ...';
      break;
    default:
      $('expirationLabel').innerHTML = 'Subscriptions expire (and will need to be renewed) every ...';
  }
}

function adjust_plan_type_parts() {
  if ($('subscription_plan_plan_type') == null) { 
    return; 
  }

  var selected = $('subscription_plan_plan_type').value;
  $$('[hide_for_plan_type]').each(function(e) {
    var hide_fors = e.getAttribute('hide_for_plan_type').split(', ');
    hide_for = e.getAttribute('hide_for_plan_type');
    (hide_fors.include(selected)) ? e.hide() : e.show();
    });

  $('subscription_plan_amount_label').innerHTML = (selected == 'metered') ? 'Initial Charge' : 'Price';

  expiration_label_reflects_plan_type();
}

function plan_type_changed() {
  adjust_plan_type_parts();

  var selected = $('subscription_plan_plan_type').value;
  if (selected == 'free_trial') {
    $('subscription_plan_amount').value = '0.00';
  }
  if (selected == 'metered') {
    $('duration_fields').show();
  }
}


function hide_regular_plan_options() {
	$('regular_plan_options').hide();
	$('subscription_plan_force_recurring').checked = false;
	$('subscription_plan_charge_after_first_period').checked  = false;
	$('subscription_plan_charge_after_first_period').disabled = true;
	disable_custom_charge_later_fields(true);
}

function togglePasswordChange(){
    $('editPassword').toggle();
		$('change_password_link').toggle();
    $('account_password').focus();
  }

function needs_to_be_renewed_changed() {
	if ($('subscription_plan_needs_to_be_renewed').checked == true)	{
    $('duration_fields').show();
		if ($('subscription_plan_plan_type').value == 'regular') {
			$('regular_plan_options').show();
		}
	}
	else {
    $('duration_fields').hide();
    $('subscription_plan_duration_quantity').value = 0;
		hide_regular_plan_options();
	}
}

function show_site_selector(){
  $('switchMenu').show();
}

function hide_site_selector(){
  $('switchMenu').hide();
}

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

document.observe('dom:loaded', function() {
  $$('.swivel').each(function(e) {
    var trigger = e.select('.trigger').first();
    var content = e.select('.content');

    content.invoke('hide');

    trigger.up = function() {
      return trigger.hasClassName("up");
    };

    trigger.toggle = function() {
      if(trigger.up()) {
        content.invoke('show');
        trigger.removeClassName("up");
        trigger.addClassName("down");
      } else {
        content.invoke('hide');
        trigger.removeClassName("down");
        trigger.addClassName("up");
      }
    };

    trigger.select('a').invoke('observe', "click", function(event) {
      trigger.toggle();
      Event.stop(event);
    });
  });
});

document.observe('dom:loaded', function() {
  $$('.selective_toggle').each(function(e) {
    var toggler = e.select('.toggler').first();
    var options = e.select('.toggle_option');

    toggler.update_content = function() {
      options.invoke('hide');
      e.select('#' + toggler.value).invoke('show');
    };

    toggler.observe('change', function(event) {
      toggler.update_content();
      Event.stop(event);
    });
    
    toggler.update_content();
  });
});
