
// Fields to set the disappear_on_click() texts for the user form.
function disappear_user() {
  disappear_on_click( 'user_firstname' , 'Enter Your First Name');
  disappear_on_click( 'user_lastname'  , 'Enter Your Last Name');
  disappear_on_click( 'user_company'   , 'Enter Your Company\'s Name');
}

// Fields to set the disappear_on_click() texts for the user form (profile page text).
function disappear_user_profile() {
  disappear_on_click( 'user_email'                , 'Enter Your New Email Address');
  disappear_on_click( 'user_email_confirmation'   , 'Confirm Your New Email Address');
  disappear_on_click( 'password_existing_text'    , 'Enter your Existing Password');
  disappear_on_click( 'password_text'             , 'Choose a New Password');
  disappear_on_click( 'password_confirmation_text', 'Confirm Your New Password Choice');    
}

// Fields to set the disappear_on_click() texts for the user form (registration page text).
function disappear_user_reg() {
  disappear_on_click( 'user_email'                , 'Enter Your Email Address');
  disappear_on_click( 'user_email_confirmation'   , 'Confirm Your Email Address');
  disappear_on_click( 'password_text'             , 'Choose a Password');
  disappear_on_click( 'password_confirmation_text', 'Confirm Your Password Choice');
}

// Fields to set the disappear_on_click() texts for the contact form.
function disappear_contact() {
  disappear_on_click( 'user_email2' , 'Enter Your Secondary Email');
  disappear_on_click( 'phone_home'  , 'Enter Your Home Phone Number');
  disappear_on_click( 'phone_work'  , 'Enter Your Work Phone Number');
  disappear_on_click( 'phone_cell'  , 'Enter Your Cell Phone Number');
}

// Fields to set the disappear_on_click() texts for the billing address form.
function disappear_billing_card() {
  disappear_on_click( 'billinginfo_name' , 'Enter the Name on the Credit Card');
  disappear_on_click( 'billinginfo_cc'   , 'Enter Your Card Number');
  disappear_on_click( 'billinginfo_cvv'  , 'Enter CVV');
  
  change_style_on_click( 'billinginfo_accttype');
  change_style_on_click( 'billinginfo_exp_month');
  change_style_on_click( 'billinginfo_exp_year');
  
  initialize_select_style( 'billinginfo_accttype');
  initialize_select_style( 'billinginfo_exp_month');
  initialize_select_style( 'billinginfo_exp_year');
}
    
// Fields to set the disappear_on_click() texts for the billing address form.
function disappear_billing_address() {
  disappear_on_click( 'billinginfo_addressline1' , 'Enter the Billing Street Address');
  disappear_on_click( 'billinginfo_addressline2' , 'Optional: Enter Additional Billing Address Information');
  disappear_on_click( 'billinginfo_city'         , 'Enter the Billing City');

  disappear_on_click( 'billinginfo_zip'          , 'Enter the Billing Zip');
  disappear_on_click( 'billinginfo_ca_zip'       , 'Enter the Billing Postal Code');
  disappear_on_click( 'billinginfo_all_zip'      , 'Enter the Billing Postal Code');
  disappear_on_click( 'billinginfo_all_state'    , 'Enter the Billing Province');

  // Use the top United States and Canada options instead of the lower ones if those countries are selected.
  var country_element = document.getElementById('billinginfo_country');
  if( country_element != null && typeof(country_element) != 'undefined') {
    if( country_element.value == 'US') {
      country_element.selectedIndex = 1;
    } else if( country_element.value == 'CA') {
      country_element.selectedIndex = 2;
    }
  }

  change_style_on_click( 'billinginfo_country');
  change_style_on_click( 'billinginfo_state');
  change_style_on_click( 'billinginfo_ca_state');

  initialize_select_style( 'billinginfo_country');
  initialize_select_style( 'billinginfo_state');
  initialize_select_style( 'billinginfo_ca_state');
}

// Fill default values in the form.
function disappear_on_click( field, text) {
  if( typeof($(field)) != 'undefined') {
    if( $(field).value == null || $(field).value == '' || $(field).className == 'clearField') {
        $(field).value = text;
    } else {
        $(field).className = 'filledField';
        $(field).onfocus = null;
    }
  }
}

function clear_if_not_filled( field) {
  if( typeof($(field)) != 'undefined') {
    if( $(field).className == 'clearField') {
        $(field).value = '';
    }
  }
}

function change_style_on_click( field) {
  if( typeof($(field)) != 'undefined') {
    if( $(field).value == null || $(field).value == '' || $(field).value == '0') {
      $(field).className = 'clearField';
    }
  }
}

function initialize_select_style( field) {
  if( typeof($(field)) != 'undefined') {
     $(field).options[0].className = 'clearField';
     for( var i = 1; i < $(field).options.length; i++) {
         $(field).options[i].className = 'filledField';
     }
  }
}

function clear_all_if_not_filled() {
  clear_if_not_filled( 'user_firstname'            );
  clear_if_not_filled( 'user_lastname'             );
  clear_if_not_filled( 'user_company'              );
  clear_if_not_filled( 'user_email'                );
  clear_if_not_filled( 'user_email_confirmation'   );

  clear_if_not_filled( 'password_text'             );
  clear_if_not_filled( 'password_existing_text'    );
  clear_if_not_filled( 'password_confirmation_text');

  clear_if_not_filled( 'phone_home'                );
  clear_if_not_filled( 'phone_work'                );
  clear_if_not_filled( 'phone_cell'                );

  clear_if_not_filled( 'user_password'             );
  clear_if_not_filled( 'user_password_confirmation');

  clear_if_not_filled( 'billinginfo_name'        );
  clear_if_not_filled( 'billinginfo_cc'          );
  clear_if_not_filled( 'billinginfo_cvv'         );
  clear_if_not_filled( 'billinginfo_addressline1');
  clear_if_not_filled( 'billinginfo_addressline2');
  clear_if_not_filled( 'billinginfo_city'        );
  
  clear_if_not_filled( 'billinginfo_zip'         );
  clear_if_not_filled( 'billinginfo_ca_zip'      );
  clear_if_not_filled( 'billinginfo_all_zip'     );
  clear_if_not_filled( 'billinginfo_all_state'   );
}

// Hide temporal <input> fields (this is needed for password fields, which
// can't show hints, since they would be all masked to "*********").
function hide_temporal_field(element, element_to_show) {
  element.style.display = 'none';
  $(element_to_show).show();
  $(element_to_show).focus();
}

// Function was created to fix Firefox Password Manager feature.
function hide_temporal_password_field(element, element_to_show) {
  hide_temporal_field( element, element_to_show);
  // Firefox's Password Manager keeps filling this field.
  $(element_to_show).value = '';
}

function field_onfocus(element) {
  element.value = '';
  element.className = 'filledField';
}

// Restores the onfocus handler if the field is not empty.
function restore_onfocus( field) {
  if( $(field).value == null || $(field).value == '') {
    // Also restore the original clear field class.
    $(field).className = 'clearField';

    $(field).onfocus = function() {
	  // Restore onfocus handler.
      field_onfocus($(field));
    }
  }
}

// Restore a field to the original value.
function restore_field( object, field) {
  // Build ids.
  var field_name = object + '_' + field;
  var reset_name = 'reset_' + field_name + '_id';
  var error_name = field + 'Error';
  var span_name = 'field_' + field;
 
  // Restore field to original value.
  $(field_name).value = $(reset_name).value;
  
  // If it's not empty, set the class.
  if( $(field_name).value != null && $(field_name).value != '') {
    $(field_name).className = 'filledField';
  }

  // Clear error.  
  var error_obj = $(error_name);
  if( typeof(error_obj) != 'undefined') {
    error_obj.innerHTML = '';
  }
  
  // Restore span class.
  $(span_name).className = 'field';
}

// Returns true if all sections are closed.
function are_sections_closed() {
  if( $('account_info_modified_id').value == 'NO' &&
      $('contact_info_modified_id').value == 'NO' &&
      $('billing_info_modified_id').value == 'NO' &&
      $('card_info_modified_id'   ).value == 'NO') {
    return true;
  }
  
  return false;
}

function disable_onfocus(element) {
  element.onfocus = null;
}

// Checks that CVV length is not bigger than 4, and
// also do the same as enter_check() afterwards.
function cvv_check(e) {
  var keyCode;

  if( window.event) {
    // IE.
    keyCode = e.keyCode;
  } else if(e.which) {
    // Netscape/Firefox/Opera.
    keyCode = e.which;
  }

  if( $('billinginfo_cvv').value.length == 5) {
	alert( "CVV can't be more than 4 characters.");
    $('billinginfo_cvv').value = $('billinginfo_cvv').value.substring(0,4);
    return false;
  }

  // If the user pressed the ENTER key, submit the form.
  if( keyCode == 13) {
    update_my_info_submit();
    return false;
  }
  
  return true;
}

// Submits the registration/profile form if the ENTER key was pressed.
function enter_check(e) {
  var keyCode;

  if( window.event) {
    // IE.
    keyCode = e.keyCode;
  } else if(e.which) {
    // Netscape/Firefox/Opera.
    keyCode = e.which;
  }

  // If the user pressed the ENTER key, submit the form.
  if( keyCode == 13) {
    update_my_info_submit();
    return false;
  }
  
  return true;
}

function select_onchange(element) {
  if( element.selectedIndex == 0) {
    element.className = 'clearField';
  } else {
    element.className = 'filledField';
  }
}

function reg_edit_link( edit_id, anchor_txt, form_id) {
  $(edit_id).value  = anchor_txt;
  $(form_id).action = $(form_id).action + '#' + anchor_txt;
  document.forms[form_id].submit();
}

// The "[edit]" link.
function profile_edit_link( show_block, hide_block, modified_id) {
  // Hide the read-only block.
  $(hide_block).hide();

  // Show the edit block.
  $(show_block).show();

  // Show block with "Update My Info" button.
  $('div_update_my_info').show();

  // Block was modified.
  $(modified_id).value = 'YES';
}

// The "[never mind]" function, it basically cancels the edit, and set flags to their
// default state (like if no "edit" were clicked at all).
function profile_never_mind_link( show_block, hide_block, modified_id) {
  // Hide the edit block.
  $(show_block).hide();

  // Show the read-only block.
  $(hide_block).show();

  // Block was modified.
  $(modified_id).value = 'NO';
  
  // If there is no open edit block.
  if( are_sections_closed()) {
	// Hide the "Update My Info" button.
    $('div_update_my_info').hide();
    
    // Clear the error heading.
    if( typeof($('heading_errors')) != 'undefined') {
      $('heading_errors').innerHTML = '';
    }
  }
}

// The "never mind" function for the user form (The "Account Info" section).
function profile_user_never_mind() {
  // Hide the user section.
  profile_never_mind_link( 'profile_edit_account', 'profile_view_account', 'account_info_modified_id');
  
  // Restore the change login and change pass sections to their default.
  $('user_email_readonly_section' ).show();
  $('user_pass_readonly_section'  ).show();
  $('user_email_editable_section' ).hide();
  $('user_pass_editable_section_1').hide();
  $('user_pass_editable_section_2').hide();
  $('email_modified_id').value = 'NO';
  $('pass_modified_id' ).value = 'NO';
  
  // Reset fields to their original values.
  restore_field('user', 'firstname'            );
  restore_field('user', 'lastname'             );
  restore_field('user', 'company'              );
  restore_field('user', 'email'                );
  restore_field('user', 'email_confirmation'   );
  restore_field('user', 'password'             );
  restore_field('user', 'password_confirmation');
  restore_field('user', 'password_existing'    );

  // Restore onfocus handlers.
  restore_onfocus('user_firstname'            );
  restore_onfocus('user_lastname'             );
  restore_onfocus('user_company'              );
  restore_onfocus('user_email'                );
  restore_onfocus('user_email_confirmation'   );
  
  // Don't restore focus on these, show the temporal fields again instead.
  // restore_onfocus('user_password'             );
  // restore_onfocus('user_password_confirmation');
  // restore_onfocus('user_password_existing'    );
  $('password_text'             ).show();
  $('password_confirmation_text').show();
  $('password_existing_text'    ).show();

  // Reset the disappear_on_click() texts.
  disappear_user();
  disappear_user_profile();
}

// The "never mind" function for the billing form (The "Billing Info" section).
function profile_billing_never_mind() {
  // Hide the billing section.
  profile_never_mind_link( 'profile_edit_billing', 'profile_view_billing', 'billing_info_modified_id');
  
  // Reset fields to their original values.
  restore_field('billinginfo', 'country'     );
  restore_field('billinginfo', 'addressline1');
  restore_field('billinginfo', 'addressline2');
  restore_field('billinginfo', 'city'        );
  restore_field('billinginfo', 'state'       );
  restore_field('billinginfo', 'zip'         );
  restore_field('billinginfo', 'ca_state'    );
  restore_field('billinginfo', 'ca_zip'      );
  restore_field('billinginfo', 'all_state'   );
  restore_field('billinginfo', 'all_zip'     );

  // Restore onfocus handlers.
  restore_onfocus('billinginfo_addressline1');
  restore_onfocus('billinginfo_addressline2');
  restore_onfocus('billinginfo_city'        );
  restore_onfocus('billinginfo_zip'         );
  restore_onfocus('billinginfo_ca_zip'      );
  restore_onfocus('billinginfo_all_state'   );
  restore_onfocus('billinginfo_all_zip'     );

  // Reset the disappear_on_click() texts.
  disappear_billing_address();
  
  // Set the correct fields (state/zip for US, province/postal code for others).
  country_onchange($('billinginfo_country'));
}

// The "never mind" function for the card form (The "Card Info" section).
function profile_card_never_mind() {
  // Hide the card section.
  profile_never_mind_link( 'profile_edit_card', 'profile_view_card', 'card_info_modified_id');
  
  // Reset fields to their original values.
  restore_field('billinginfo', 'name'     );
  restore_field('billinginfo', 'accttype' );
  restore_field('billinginfo', 'cc'       );
  restore_field('billinginfo', 'cvv'      );
  restore_field('billinginfo', 'exp_month');
  restore_field('billinginfo', 'exp_year' );

  // Restore onfocus handlers.
  restore_onfocus('billinginfo_name');
  restore_onfocus('billinginfo_cc'  );
  restore_onfocus('billinginfo_cvv' );

  // Reset the disappear_on_click() texts.
  disappear_billing_card();
}

// The "never mind" function for the contact form (The "Contact Info" section).
function profile_contact_never_mind() {
  // Hide the contact section.
  profile_never_mind_link( 'profile_edit_contact', 'profile_view_contact', 'contact_info_modified_id');
  
  // Reset fields to their original values.
  if( $('reset_phone_primary_radio_h_id').value == 'Y') {
    $('phone_primary_radio_h').checked = 'checked';
  } else {
    $('phone_primary_radio_h').checked = '';
  }
  
  if( $('reset_phone_primary_radio_w_id').value == 'Y') {
    $('phone_primary_radio_w').checked = 'checked';
  } else {
    $('phone_primary_radio_w').checked = '';
  }
  
  if( $('reset_phone_primary_radio_c_id').value == 'Y') {
    $('phone_primary_radio_c').checked = 'checked';
  } else {
    $('phone_primary_radio_c').checked = '';
  }
  
  restore_field('phone', 'home');
  restore_field('phone', 'work');
  restore_field('phone', 'cell');
  
  if( $('reset_user_email_primary_radio_1_id').value == 'Y') {
    $('email_primary_radio_1').checked = 'checked';
  } else {
    $('email_primary_radio_1').checked = '';
  }

  if( $('reset_user_email_primary_radio_2_id').value == 'Y') {
    $('email_primary_radio_2').checked = 'checked';
  } else {
    $('email_primary_radio_2').checked = '';
  }
  
  restore_field('user', 'email2');

  if( $('reset_preference_survey_id2').value == 'Y') {
    $('preference_survey_id').checked = 'checked';
  } else {
    $('preference_survey_id').checked = '';
  }

  if( $('reset_preference_weekly_id2').value == 'Y') {
    $('preference_weekly_id').checked = 'checked';
  } else {
    $('preference_weekly_id').checked = '';
  }

  // Restore onfocus handlers.
  restore_onfocus('phone_home');
  restore_onfocus('phone_work');
  restore_onfocus('phone_cell');
  restore_onfocus('user_email2');

  // Reset the disappear_on_click() texts.
  disappear_contact();
}

function profile_change_login_email() {
  // Hide readonly email section.
  $('user_email_readonly_section').hide();
  
  // Show login email edit section.
  $('user_email_editable_section').show();
  
  // Always show existing password section.
  $('user_pass_editable_section_2').show();
  
  // Flag that the email section was modified.
  $('email_modified_id').value = 'YES';
}

function profile_change_password() {
  // Hide readonly password section.
  $('user_pass_readonly_section').hide();
  
  // Show password edit section.
  $('user_pass_editable_section_1').show();
  
  // Always show existing password section.
  $('user_pass_editable_section_2').show();
  
  // Flag that the password section was modified.
  $('pass_modified_id').value = 'YES';
}

// Things to do when the "Update My Info" button is pressed.
function update_my_info_submit() {
  $('transparentWait').style.display  = 'none';
  $('updateMyInfoWait').style.display = '';
  
  if( $('password_text').style.display != 'none') {
    // Clear the password field if it was not modified
    // (because of Firefox Password Manager feature which keeps filling it).
    $('user_password').value = '';
  }

  clear_all_if_not_filled();
  
  document.forms['profileUpdateForm'].submit();
}

function country_show_world_rows() {
  $('tr_state_all_1').show();
  $('tr_state_all_2').show();
  $('tr_state_all_4').show();
}

function country_hide_world_rows() {
  $('tr_state_all_1').hide();
  $('tr_state_all_2').hide();
  $('tr_state_all_4').hide();
}

function country_show_canada_rows() {
  $('tr_state_ca_1').show();
  $('tr_state_ca_2').show();
  $('tr_state_ca_4').show();
}

function country_hide_canada_rows() {
  $('tr_state_ca_1').hide();
  $('tr_state_ca_2').hide();
  $('tr_state_ca_4').hide();
}

function country_show_us_rows() {
  $('tr_state_us_1').show();
  $('tr_state_us_2').show();
}

function country_hide_us_rows() {
  $('tr_state_us_1').hide();
  $('tr_state_us_2').hide();
}

function country_onchange( country_select) {
  if( country_select.value == 'US' || country_select.value == '1' || country_select.value == '0') {
    country_show_us_rows();
    country_hide_canada_rows();
    country_hide_world_rows();
  } else
  if( country_select.value == 'CA') {
    country_hide_us_rows();
    country_show_canada_rows();
    country_hide_world_rows();;
  } else {
    country_hide_us_rows();
    country_hide_canada_rows();
    country_show_world_rows();
  }

  // If the user selected the bar, set the index to zero.  
  if( country_select.value == '1') {
     country_select.value = 0;
  }
  
  select_onchange( country_select);
}

function clear_promocode_error() {
  $('promo_tr_title').className = 'formField';
  $('promo_tr_content').className = 'formField';
  if( $('promo_inline_error') != null) {
    $('promo_inline_error').style.display='none';
  }
}
