/* function set_onclick_events_for_changing_plans() {{{
*
* Description:
*  Sets the "onclick" event for:
*   * Each "Change Plan" image;
*   * Confirming upgrading and downgrading the plan.
*   * Denying upgrading and downgrading the plan.
*
* Arguments:
*  None
*
* Returns:
*  Nothing
*
*/
function set_onclick_events_for_changing_plans() {
  // Clicking the "Change Plan" image toggles the corresponding lightbox and upgrade/downgrade confirmation.
  $$('#mail2web-mobile-email-trial-plan a[name="show-lightbox"]')[0].href = 'javascript:confirm_upgrade_to_mobile_email_pro_trial_plan();';
  $$('#mail2web-mobile-email-pro-trial-plan a[name="show-lightbox"]')[0].href = 'javascript:confirm_downgrade_to_mobile_email_trial_plan();';

  // Clicking these links confirms that the user wants to change plans.
  $$('#confirm-plan-upgrade a[name="confirm-plan-upgrade"]')[0].href = 'javascript:upgrade_to_mobile_email_pro_trial_plan();';
  $$('#confirm-plan-downgrade a[name="confirm-plan-downgrade"]')[0].href = 'javascript:downgrade_to_mobile_email_trial_plan();';

  // Clicking these links denies the plan change.
  $$('#confirm-plan-upgrade a[name="deny-plan-upgrade"]')[0].href = 'javascript:deny_plan_change();';
  $$('#confirm-plan-downgrade a[name="deny-plan-downgrade"]')[0].href = 'javascript:deny_plan_change();';
} // }}}

/* function get_chosen_plan() {{{
*
* Description:
*  Returns the ID of the div that describes the plan that's selected.
*
* Arguments:
*  None
*
* Returns:
*  A string representing a DOM ID, or null if no plan is chosen.
*
*/
function get_chosen_plan() {
    // A CSS selector that will determine which radio button is checked.
    var plan_selector = '#ChangePlan input[type="radio"][name="ctl00$SignupProgress1$PlanChangeGroup"]';

    var chosen_plan_radio_button = $$(plan_selector).find(function(element) {return element.checked == true;});

    if (chosen_plan_radio_button == null)
      {chosen_plan = null;}
    else if (chosen_plan_radio_button.id == 'ctl00_SignupProgress1_PlanChangeME')
      {chosen_plan = 'mail2web-mobile-email-trial-plan';}
    else if (chosen_plan_radio_button.id == 'ctl00_SignupProgress1_PlanChangePro')
      {chosen_plan = 'mail2web-mobile-email-pro-trial-plan';}
    else
      {chosen_plan = null;}

    return chosen_plan;
} // }}}

/* function set_plan_to($plan) {{{
 *
 * Description:
 *  Sets the chosen plan to $plan, and then ensures that the plan is displayed.
 *
 * Arguments:
 *  plan        The plan to select. Valid values are:
 *                mail2web-mobile-email-trial
 *                mail2web-mobile-email-pro-trial
 *
 * Returns:
 *  Nothing
 *
 */
function set_plan_to(plan) {
  if (plan == 'mail2web-mobile-email-trial')
    {$('ctl00_SignupProgress1_PlanChangeME').checked = true;}
  if (plan == 'mail2web-mobile-email-pro-trial')
    {$('ctl00_SignupProgress1_PlanChangePro').checked = true;}

    show_chosen_plan();
} // }}}

/* function show_chosen_plan() {{{
  *
  * Description:
  *  If a plan is visible, it is hidden. The chosen plan is then
  *  displayed, and the div that describes it is highlighted.
  *
  * Arguments:
  *  None
  *
  * Returns:
  *  Nothing
  *  
  */
function show_chosen_plan() {
  var visible_plan = $$('#plans div').find(function(element) {return element.style.display != 'none';});

  if (typeof(visible_plan) != 'undefined')
    {$(visible_plan).hide();}

  $(get_chosen_plan()).show();
} // }}}

/* function toggle_lightbox_for(div_id) {{{
  *
  * Description:
  *  Shows or hides the requested lightbox. If the lightbox is visible, it'll be hidden.
  *  If it's hidden, it'll be shown.
  *
  * Arguments:
  *  div_id      The ID of the div that contains the lightbox content.
  *
  * Returns:
  *  Nothing
  *
  */
function toggle_lightbox_for(div_id) {
  var overlay_id = 'lightbox-overlay';

  if ($(div_id).visible()) {
    $(div_id    ).hide();
    $(overlay_id).hide();
  }
  else {
    $(div_id    ).show();
    $(overlay_id).show();
  }
} // }}}

/* function toggle_ChangePlan_lightbox() {{{
*
* Description:
*  Shows or hides the "ChangePlan" lightbox.
*
* Arguments:
*  None
*
* Returns:
*  Nothing
*
*/
function toggle_ChangePlan_lightbox() {
    toggle_lightbox_for('ChangePlan');

    // Set current_plan if it hasn't been set yet.
    // Otherwise, if current_plan doesn't match which radio button is checked, then
    // update current_plan.
    if (typeof(current_plan) == 'undefined')
      {current_plan = get_chosen_plan();}
    else if (current_plan != get_chosen_plan())
      {current_plan = get_chosen_plan();}
} // }}}

/* function set_account_synchronization_to($sync_setting) {{{
*
* Description:
*  Sets the radio button that indicates whether or not the customer
*  wants to "synchronize your account ... using Outlook or Entourage"
*  to "Yes" or "No".
*
* Arguments:
*  sync_setting    Describes how to set the radio buttons. Valid values are:
*                    yes
*                    no
*
* Returns:
*  Nothing
*
*/
function set_account_synchronization_to(sync_setting) {
    if (sync_setting == 'yes')
      {$('ctl00_ContentBody_OutlookYes').checked = true;}
    else if (sync_setting == 'no')
      {$('ctl00_ContentBody_OutlookNo').checked = true;}
} // }}}

/* function confirm_upgrade_to_mobile_email_pro_trial_plan() {{{
*
* Description:
*  If the user has already selected the Mobile Email Pro Trial plan,
*  nothing happens.
*  Otherwise, it toggles the "Change Plan" lightbox, and asks the
*  user to confirm that they want to upgrade to the Mobile Email Pro
*  Trial plan.
*
* Arguments:
*  None
*
* Returns:
*  Nothing
*
*/
function confirm_upgrade_to_mobile_email_pro_trial_plan() {
    // Do nothing if the user has already selected the Mobile Email Pro Trial plan.
    if (get_chosen_plan() == 'mail2web-mobile-email-pro-trial-plan')
      {return;}

    toggle_ChangePlan_lightbox();

    if ($('ctl00_ContentBody_OutlookNo'))
      {set_account_synchronization_to('no');}

    hide_plan_downgrade_text();
    show_plan_upgrade_text();
} // }}}

/* function confirm_downgrade_to_mobile_email_trial_plan() {{{
 *
 * Description:
 *  If the user has already selected the Mobile Email Trial plan,
 *  nothing happens.
 *  Otherwise, it toggles the "Change Plan" lightbox, and asks the
 *  user to confirm that they want to downgrade to the Mobile Email
 *  Trial plan.
 *
 * Arguments:
 *  None
 *
 * Returns:
 *  Nothing
 *
 */
function confirm_downgrade_to_mobile_email_trial_plan() {
  // Do nothing if the user has already selected the Mobile Email Trial plan.
  if (get_chosen_plan() == 'mail2web-mobile-email-trial-plan')
    {return;}

  toggle_ChangePlan_lightbox();

  if ($('ctl00_ContentBody_OutlookYes'))
    {set_account_synchronization_to('yes');}

  hide_plan_upgrade_text();
  show_plan_downgrade_text();
} // }}}

/* function upgrade_to_mobile_email_pro_trial_plan() {{{
*
* Description:
*  Changes the chosen plan to Mobile Email Pro Trial. This involves:
*    * Setting the "Do you want to synchronize ..." radio button to "Yes".
*    * Toggling the "Change Plan" lightbox, thus hiding it.
*    * Setting the chosen plan to the Mobile Email Pro Trial plan.
*
* Arguments:
*  None
*
* Returns:
*  Nothing
*
*/
function upgrade_to_mobile_email_pro_trial_plan() {
  if ($('ctl00_ContentBody_OutlookNo'))
    {set_account_synchronization_to('yes');}

  toggle_ChangePlan_lightbox();
  set_plan_to('mail2web-mobile-email-pro-trial');
  show_notification('upgrade to ME Pro');
} // }}}

/* function downgrade_to_mobile_email_trial_plan() {{{
*
* Description:
*  Changes the chosen plan to Mobile Email Trial. This involves:
*    * Setting the "Do you want to synchronize ..." radio button to "No".
*    * Toggling the "Change Plan" lightbox, thus hiding it.
*    * Setting the chosen plan to the Mobile Email Trial plan.
*
* Arguments:
*  None
*
* Returns:
*  Nothing
*
*/
function downgrade_to_mobile_email_trial_plan() {
  if ($('ctl00_ContentBody_OutlookNo'))
    {set_account_synchronization_to('no');}

  if ($('DivOutlookUpgrade') && ($('DivOutlookUpgrade').visible() == false))
    {Effect.BlindDown('DivOutlookUpgrade', {duration: blind_duration});}

  toggle_ChangePlan_lightbox();
  set_plan_to('mail2web-mobile-email-trial');
  show_notification('downgrade to ME');
} // }}}

/* function hide_plan_downgrade_text() {{{
*
* Description:
*  Hides the text to confirm that the user wants to downgrade their plan.
*
* Arguments:
*  None
*
* Returns:
*  Nothing
*
*/
function hide_plan_downgrade_text() {
  $('confirm-plan-downgrade').hide();
} // }}}

/* function hide_plan_upgrade_text() {{{
*
* Description:
*  Hides the text to confirm that the user wants to upgrade their plan.
*
* Arguments:
*  None
*
* Returns:
*  Nothing
*
*/
function hide_plan_upgrade_text() {
  $('confirm-plan-upgrade').hide();
} // }}}

/* function show_plan_upgrade_text() {{{
*
* Description:
*  Shows the text to confirm that the user wants to upgrade their plan.
*
* Arguments:
*  None
*
* Returns:
*  Nothing
*
*/
function show_plan_upgrade_text() {
  $('confirm-plan-upgrade').show();
} // }}}

/* function show_plan_downgrade_text() {{{
*
* Description:
*  Shows the text to confirm that the user wants to downgrade their plan.
*
* Arguments:
*  None
*
* Returns:
*  Nothing
*
*/
function show_plan_downgrade_text() {
  $('confirm-plan-downgrade').show();
} // }}}

/* function deny_plan_change() {{{
*
* Description:
*  The user does NOT want to change their plan, so:
*    * Toggle the "Change Plan" lightbox, thus hiding it.
*    * Hide the plan-upgrade text.
*    * Hide the plan-downgrade text.
*
* Arguments:
*  None
*
* Returns:
*  Nothing
*
*/
function deny_plan_change() {
  toggle_lightbox_for('ChangePlan');
  hide_plan_upgrade_text();
  hide_plan_downgrade_text();
} // }}}

/* function show_notification(notification) {{{
 *
 * Description:
 *  Hides all notification elements, and shows the desired element, if it exists.
 *
 * Arguments:
 *  notification    The notification element to display.
 *
 * Returns:
 *  Nothing
 *
 */
function show_notification(notification) {
  var dom_ID;
  var notification_containers = {
    'downgrade to ME'   : 'downgradetoME',
    'upgrade to ME Pro' : 'upgradetoMEpro',
    'added BlackBerry'  : 'BlackBerryAdded',
    'removed BlackBerry': 'BlackBerryRemoved'
  }
  var element_to_show = notification_containers[notification];

  Object.values(notification_containers).each(function(dom_ID) {
    if ($(dom_ID) && $(dom_ID).visible())
      {$(dom_ID).hide();}
  });

  if (element_to_show) {
    $(element_to_show).show();
    new Effect.Highlight(element_to_show, {startcolor: '#ff9999', endcolor: '#E4F5B0'});
  }
} // }}}

/* function set_onclick_events_for_onboarding() {{{
 *
 * Description:
 *  Configures the customer onboarding radio buttons to show and hide the div that contains
 *  the customer onboarding form.
 *
 * Arguments:
 *  None
 *
 * Returns:
 *  Nothing
 *
 */
function set_onclick_events_for_onboarding() {
  if (($('ctl00_ContentBody_OnboardingRadioON') == null) || ($('ctl00_ContentBody_OnboardingRadioOff') == null) || ($('divCallBackPanel') == null))
    {return;}

  $('ctl00_ContentBody_OnboardingRadioON' ).onclick = function() {show_if_hidden('divCallBackPanel');}
  $('ctl00_ContentBody_OnboardingRadioOff').onclick = function() {hide_if_visible('divCallBackPanel');}
} // }}}

