// Edit Profile

function account_editprofile_select_birthday_show() {
  // Only open popup when we can
  if($('birthday_busy').value == 1)
    return;
  $('birthday_busy').value = 1;
  
  popup_show('date_select');
}

function account_editprofile_select_birthday_hide() {
  popup_hide('date_select');
  
  $('birthday_popup').focus();
  $('birthday_busy').value = 0;
}

function account_editprofile_select_birthday() {
  var year = parseInt($('popup_date_select_year').value);
  var month = parseInt($('popup_date_select_month').value);
  var day = parseInt($('popup_date_select_day').value);
    
  // Set Value
  $('birthday_datestr').value = util_create_date_num(year, month, day);
  $('birthday_popup').value = util_create_date_str(year, month, day);
  
  account_editprofile_select_birthday_hide();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// User Location Functions

function account_disable_user_location_buttons() {
  $('user_location_add_button').disable();
  $('user_location_finish_button').disable();
}

function account_enable_user_location_buttons() {
  $('user_location_add_button').enable();
  $('user_location_finish_button').enable();
}

function account_add_user_location() {
  var name = $('loc_name').value;
  var addr = $('loc_address').value;
  var zip  = $('loc_zip').value;
	
  account_disable_user_location_buttons();
  $('location_add_busy').show();
  
  new Ajax.Request('ajax.php', {
    parameters: { 
      _action: 'user_location_add', 
      name: name, 
      address: addr, 
      location: zip
    },
    onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      
      if (json_response.ajax_status == 1) {
        // Add New Location
        var new_loc = new Element('div').innerHTML = json_response.html;
        $('user_locs').insert(new_loc);
        
        // Switch default in UI if we need to
        if(json_response.default_id > 0)
          account_switch_default_location(json_response.default_id);
        
        // Clear Empty Message
        $('user_locs_empty').hide();
          
        // Reset Form
        account_enable_user_location_buttons();
        $('location_add_busy').hide();
        $('loc_name').value = '';
        $('loc_address').value = '';
        $('loc_zip').value = '';
			}
			else {
        $('loc_zip').focus();
        alert (json_response.error_msg);
        account_enable_user_location_buttons();
        $('location_add_busy').hide();
      }
    },
    onFailure: function(){ 
      alert('Could not add location');
      account_enable_user_location_buttons();
      $('location_add_busy').hide();
    }
  });
}

function account_remove_user_location(id) {
  var user_location_id = $('user_location_id_'+id).value;

  $('user_location_'+id).setOpacity(0.4);
  account_disable_user_location_buttons();
  
  new Ajax.Request('ajax.php', {
    parameters: { 
      _action: 'user_location_remove', 
      id: user_location_id
    },
    onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      if (json_response.ajax_status == 1) {
        $('user_location_'+id).hide();
        
        // Switch default in UI if we need to
        if(json_response.default_id > 0)
          account_switch_default_location(json_response.default_id);
          
        account_enable_user_location_buttons();
      }
      else {
        alert ('Could not remove location');
        $('user_location_'+id).setOpacity(1);
        account_enable_user_location_buttons();
      }
    },
    onFailure: function(){ 
      alert('Could not remove location')
      $('user_location_'+id).setOpacity(1);
      account_enable_user_location_buttons();
    }
  });
}

function account_set_default_location(id) {
  var user_location_id = $('user_location_id_'+id).value;
  
  account_disable_user_location_buttons();
  
  new Ajax.Request('ajax.php', {
    parameters: { 
      _action: 'user_location_set_default', 
      id: id
    },
    onSuccess: function(response) {
      var json_response = response.responseText.evalJSON();
      if(json_response.ajax_status == 1) {
        // Perform switch in UI
        account_switch_default_location(id);
      
        // Enable buttons
        account_enable_user_location_buttons();
      }
      else {
        alert (json_response.error_msg);
        account_enable_user_location_buttons();
      }
    },
    onFailure: function(){ 
      alert('Could not apply as default location');
      account_enable_user_location_buttons();
    }
  });
}

function account_switch_default_location(id) {
  var old_id = $('default_user_location').value;
  if(old_id > 0 && $('user_location_default_text_'+old_id) != null && $('user_location_default_text_'+id) != null) {
    $('user_location_default_link_'+old_id).show();
    $('user_location_default_text_'+old_id).hide();
    
    $('user_location_default_link_'+id).hide();
    $('user_location_default_text_'+id).show();
    
    $('default_user_location').value = id;
  }
}
