/* Lookup category keyword based on initial characters */
function lookup_keyword(input_string, cat_id)
{
  var box_id = '#suggestions_' + cat_id;
  var list_id = '#auto_suggestions_list_' + cat_id;

  if(input_string.length < 2)
  {
    // Hide the suggestion box.
    $(box_id).hide();
  }
  else
  {
    //alert('You are here, ' + input_string);
    $.post("/includes/ajax/cat_keyword_lookup.php", {q_string: ""+input_string+""}, function(data)
    {
      if(data.length >0)
      {
        $(box_id).show();
        $(list_id).html(data);
      }
      else
      {
        $(list_id).html('');
        $(box_id).hide();
      }
    });
  }
}

/* Add a category keyword to the database */
function add_keyword(cat_id)
{
  var keyword_box = '#keywords_' + cat_id;
  var string_field = '#keyword_name_' + cat_id;
  var suggestions_box = '#suggestions_' + cat_id;
  var keyword_button = '#show_keyword_button_' + cat_id;
  var keyword_toggle_button = '#toggle_keywords_' + cat_id;
  var keyword_count = '#keyword_count_' + cat_id;

//  alert('string_field = ' + string_field);
  var keyword_name = $(string_field).attr("value");
//  alert('Add ' + keyword_name + ' for cat_id ' + cat_id);

  if(keyword_name.length < 2)
  {
    // The keyword is too short
    alert('Keyword must contain at least two characters.');
  }
  else
  {
    $.post("/includes/ajax/add_keyword.php",
    {
      keyword_name: ""+keyword_name+"",
      cat_id: ""+cat_id+""
    },
    function(data)
    {
      if(data.length >0)
      {
        var total_keywords = data.split(/\n/).length - 1;

        // Display the updated keyword list
        $(suggestions_box).fadeOut("fast");
        $(keyword_box).hide();
        $(keyword_box).html(data);
        $(keyword_box).fadeIn("slow");

        // Clear out the Add keyword field
        $(string_field).attr("value", "");

        $(keyword_button).show("slow");
        $(keyword_toggle_button).attr("value", "hide");
        $(keyword_count).html(total_keywords);
      }
      else
      {
        alert('Oops! It appears nothing was added.');
      }
    });
  }
}

/* Get the user count for keywords for a category */
function keyword_user_count(cat_id)
{
  var keyword_box = '#keywords_' + cat_id;
  var keyword_count_button = '#keyword_user_count_' + cat_id;
  var keyword_count = '#keyword_count_' + cat_id;

  $(keyword_box).html('Getting count. Please wait...<br /><img src="/images/ajax_wait_2.gif">');
  $(keyword_box).show("slow");
  $(keyword_count_button).hide("slow");

  $.post("/includes/ajax/get_keyword_user_count.php",
  {
    cat_id: ""+cat_id+""
  },
  function(data)
  {
    if(data.length > 0)
    {
      // Display the updated keyword list
      $(keyword_box).hide("fast");
      $(keyword_box).html(data);
      $(keyword_box).show("fast");
      $(keyword_count_button).show("slow");
    }
    else
    {
      alert('Oops! Could not get the count');
    }
  });
}

/* Lookup category based on initial characters */
function lookup_category(input_string, cat_id)
{
  var box_id = '#suggestions_' + cat_id;
  var list_id = '#auto_suggestions_list_' + cat_id;
  var parent_cat_id = document.getElementById('cat_id').value;

  if(input_string.length < 2)
  {
    // Hide the suggestion box.
    $(box_id).hide();
  }
  else
  {
    //alert('You are here, ' + input_string);
    $.post("/includes/ajax/cat_lookup.php", {q_string: ""+input_string+"", parent_cat_id: ""+parent_cat_id+""}, function(data)
    {
      if(data.length >0)
      {
        $(box_id).show();
        $(list_id).html(data);
      }
      else
      {
        $(list_id).html('');
        $(box_id).hide();
      }
    });
  }
}

/* Add a sub category to the database */
function add_subcat(cat_id)
{
  var keyword_box = '#keywords_' + cat_id;
  var string_field = '#keyword_name_' + cat_id;
//  alert('string_field = ' + string_field);
  var keyword_name = $(string_field).attr("value");

//  alert('Add ' + keyword_name + ' for cat_id ' + cat_id);

  if(keyword_name.length < 2)
  {
    // The keyword is too short
    alert('Keyword must contain at least two characters.');
  }
  else
  {
    $.post("/includes/ajax/add_keyword.php",
    {
      keyword_name: ""+keyword_name+"",
      cat_id: ""+cat_id+""
    },
    function(data)
    {
      if(data.length >0)
      {
        // Display the updated keyword list
        $(keyword_box).hide();
        $(keyword_box).html(data);
        $(keyword_box).fadeIn("slow");
        // Clear out the Add keyword field
        $(string_field).attr("value", "");
      }
      else
      {
      }
    });
  }
}

/* Publish/Unpublish a category */
function publish_toggle_category(cat_id, current_state)
{
  var box_id = '#publish_' + cat_id;
  var button_id = '#publish_toggle_' + cat_id;

//  var do_it = confirm("Are you sure you want to update this publish status?");
  var do_it = true;

  if(do_it == true)
  {
    $.post("/includes/ajax/cat_publish_toggle.php", {cat_id: ""+cat_id+"", current_state: ""+current_state+""}, function(data)
    {
      if(data == 0)
      {
        $(box_id).html("No");
        $(box_id).attr("class","publish_box hi_red");
        $(button_id).attr("value","Publish");
      }
      else
      {
        $(box_id).html("Yes");
        $(box_id).attr("class","publish_box hi_green");
        $(button_id).attr("value","Un-Publish");
      }
    });
  }
}

/* Copy an existing category under the current one */
function copy_to_curr_cat(existing_cat)
{
  var current_id = document.getElementById('cat_id').value;

  var do_it = confirm("Are you sure you want to copy the category into the current one?");

  if(do_it == true)
  {
    $.post("/includes/ajax/copy_cat.php", {current_id: ""+current_id+"", existing_cat: ""+existing_cat+""}, function(data)
    {
      if(data == 1)
      {
        alert('The category was successfully copied. Reload this page to view update.');
      }
      else
      {
        alert('Could not copy the category: ' + data);
      }
    });
  }
}

/* Run the user categorization script */
function categorize_users()
{
  var box_id = '#categorization_results';
  var do_it = confirm("This operation can take a while.\n\nAre you sure you want to continue?");

  if(do_it == true)
  {
    $(box_id).html('Updating users. Please wait...<br /><img src="/images/ajax_wait_1.gif">');
    $.post("/includes/ajax/categorize_users.php", function(data)
    {
      if(data.length > 0)
      {
        $(box_id).hide();
        $(box_id).html(data);
        $(box_id).fadeIn("slow");
      }
      else
      {
        $(box_id).html("Categorization failed");
      }
    });
  }
  else
  {
    $(box_id).html('');
  }
}

/* Run the category categorization script */
function re_categorize_category(cat_id)
{
  var box_id = '#categorization_results_' + cat_id;
  //var do_it = confirm("This operation can take a while.\n\nAre you sure you want to continue (cat ID " + cat_id + "?");
  var do_it = true;

  if(do_it == true)
  {
    $(box_id).html('Updating Category. Please wait...<br /><img src="/images/ajax_wait_1.gif">');
    $.post("/includes/ajax/re_categorize_category.php", {cat_id: ""+cat_id+""}, function(data)
    {
      if(data.length > 0)
      {
        $(box_id).hide();
        $(box_id).html(data);
        $(box_id).fadeIn("slow");
      }
      else
      {
        $(box_id).html("Categorization failed");
      }
    });
  }
  else
  {
    $(box_id).html('');
  }
}


/* Delete a category keyword */
function delete_keyword(keyword_id, cat_id)
{
  var box_id = '#keywords_' + cat_id;
  var button_id = '#delete_keyword_' + keyword_id;
  var keyword_count = '#keyword_count_' + cat_id;
  var keyword_button = '#show_keyword_button_' + cat_id;

  var do_it = confirm("Are you sure you want to delete this keyword?");

  if(do_it == true)
  {
    $.post("/includes/ajax/delete_keyword.php", {keyword_id: ""+keyword_id+"", cat_id: ""+cat_id+""}, function(data)
    {
      var total_keywords = data.split(/\n/).length - 1;

      $(keyword_count).html(total_keywords);
      if(data == '0')
      {
        alert('Oops! It looks like this keyword has already been deleted. :)');
      }
      else if(total_keywords < 1)
      {
        $(keyword_button).hide("slow");
        $(box_id).html('');
        $(box_id).hide();
      }
      else
      {
        $(box_id).html(data);
      }
    });
  }
}


/* Exclude a user from a category */
function exclude_cat_user(user_id, cat_id)
{
  var do_it = confirm("Are you sure you want to exclude this user?");
  var table_id = '#listing_' + user_id;

  if(do_it == true)
  {
    $.post("/includes/ajax/exclude_cat_user.php", {user_id: ""+user_id+"", cat_id: ""+cat_id+""}, function(data)
    {
      if(data == "1")
      {
//        alert('User has been excluded successfully');
        $(table_id).hide("slow");
      }
      else
      {
        alert('Oops! User could not be excluded: ' + data);
      }
    });
  }
}


/* Remove a multiple-parent sub-category */
function remove_subcat(subcat_id, cat_id)
{
  var row = '#cat_row_' + subcat_id;
  var do_it = confirm("Are you sure you want to remove the category?");

  if(do_it == true)
  {
    $.post("/includes/ajax/remove_subcat.php", {subcat_id: ""+subcat_id+"", cat_id: ""+cat_id+""},
    function(data)
    {
      if(data == 1)
      {
        $(row).hide();
        //alert('The category was successfully removed.');
      }
      else
      {
        alert('Oops! Could not remove the category: ' + data);
      }
    });
  }
}

/* Delete a category */
function delete_category(cat_id)
{
  var row = '#cat_row_' + cat_id;
  var do_it = confirm("WARNING! You are about to delete this category. This action cannot be undone. Are you sure you want to delete the category?");

  if(do_it == true)
  {
    $.post("/includes/ajax/delete_category.php", {cat_id: ""+cat_id+""},
    function(data)
    {
      if(data == 1)
      {
        $(row).hide();
        //alert('The category was successfully deleted.');
      }
      else
      {
        alert('Oops! Could not delete the category: ' + data);
      }
    });
  }
}

/* Rename a category */
function edit_cat_name(cat_id, cat_name)
{
  var span_id = '#cat_name_' + cat_id;
  var save_button_id = '#save_new_cat_name_' + cat_id;
  var cancel_button_id = '#cancel_new_cat_name_' + cat_id;
  var edit_cat_icon = '#edit_cat_icon_' + cat_id;
  var new_cat_name = '#new_cat_name_' + cat_id;

  $(span_id).hide();
  $(edit_cat_icon).hide();
  $(new_cat_name).show().focus();
  $(save_button_id).show();
  $(cancel_button_id).show();

  $(save_button_id).click(function()
  {
    var new_name = $(new_cat_name).attr("value");
    var do_it = confirm("Are you sure you want to rename " + cat_name + " to " + new_name + "?");

    if(do_it == true)
    {
      $.post("/includes/ajax/rename_category.php", {cat_id: ""+cat_id+"", new_name: ""+new_name+""}, function(data)
      {
        $(span_id).html(data);
        $(new_cat_name).attr("value",data);

        $(span_id).show();
        $(edit_cat_icon).show();
        $(new_cat_name).hide();
        $(save_button_id).hide();
        $(cancel_button_id).hide();
        $("#suggestions_" + cat_id + "_rename").hide();
      });
    }
    else
    {
      $(span_id).show();
      $(edit_cat_icon).show();
      $(new_cat_name).hide();
      $(save_button_id).hide();
      $(cancel_button_id).hide();
      $(new_cat_name).attr("value",cat_name);
      $("#suggestions_" + cat_id + "_rename").hide();
    }
  });

  $(cancel_button_id).click(function()
  {
    $(span_id).show();
    $(edit_cat_icon).show();
    $(new_cat_name).hide();
    $(save_button_id).hide();
    $(cancel_button_id).hide();
    $(new_cat_name).attr("value",cat_name);
    $("#suggestions_" + cat_id + "_rename").hide();
  });

}


/* Pull the admin stats table */
function get_admin_stats()
{
  $.get("/includes/ajax/get_admin_stats.php",
  function(data)
  {
    $('#admin_stats_wait').hide();
    if(data)
    {
      $('#admin_stats').html(data);
      $('#admin_stats').show();
    }
    else
    {
      $('#admin_stats').html("Error pulling stats");
    }
  });
}


/* Pull the admin stats table */
function approve_cat_suggestion(cat_id, parent_id)
{
  alert('Approving cat ID: ' + cat_id + ' Parent ID: ' + parent_id);
  $.post("/includes/ajax/approve_cat_suggestion.php", {cat_id: ""+cat_id+"", parent_id: ""+parent_id+""},
  function(data)
  {
    if(data == '1')
    {
      $('#approve_button_' + cat_id + '_' + parent_id).attr("value", "approved").attr("disabled", true);
    }
    else
    {
      alert('Could not approve the category. Contact a developer to debug this: ' + data);
    }
  });
}


/* Edit service values */
function update_service(service_id)
{
  var service_name = $('#name_' + service_id).attr("value");
  var url = $('#url_' + service_id).attr("value");
  var service_icon = $('#service_icon_' + service_id).attr("value");
  var user_url = $('#user_url_' + service_id).attr("value");
  var tooltip = $('#tooltip_' + service_id).attr("value");

  $.post("/includes/ajax/update_service.php", {id: ""+service_id+"", service_name: ""+service_name+"", url: ""+url+"", service_icon: ""+service_icon+"", user_url: ""+user_url+"", tooltip: ""+tooltip+""},
  function(data)
  {
    if(data != '0')
    {
        alert('The service was successfully updated: ' + data);
    }
    else
    {
      alert('Oops! Could not update the service: ' + data);
    }
  });
}


/* Add a new service */
function add_service()
{
  var service_name = $('#name_new').attr("value");
  var url = $('#url_new').attr("value");
  var service_icon = $('#service_icon_new').attr("value");
  var user_url = $('#user_url_new').attr("value");
  var tooltip = $('#tooltip_new').attr("value");

  $.post("/includes/ajax/add_service.php", {service_name: ""+service_name+"", url: ""+url+"", service_icon: ""+service_icon+"", user_url: ""+user_url+"", tooltip: ""+tooltip+""},
  function(data)
  {
    if(data != '0')
    {
        alert('The service was successfully added: ' + data);
    }
    else
    {
      alert('Oops! Could not add the service: ' + data);
    }
  });
}


/* Add a geo_city_search_tag */
function add_geo_cities_tag(city_id)
{
  var tag = $('#tag_id_' + city_id).html();

  //alert('Adding tag for city id ' + city_id + '. Vaue is: ' + tag);

  $.post("/includes/ajax/add_geo_cities_tag.php", {city_id: ""+city_id+"", tag: ""+tag+""},
  function(data)
  {
    if(data != '0')
    {
        //alert('The tag was successfully added: ' + data);
        $('#tag_id_' + city_id).html('Done!');
    }
    else
    {
      alert('Oops! Could not add the tag: ' + data);
    }
  });
}



/* Mark an email as validated in the db */
function validate_email(user_id)
{
  $('#email_validated_date').html('<img src="/images/ajax_wait_16px.gif" />');
  $.post("/includes/ajax/set_email_validated.php", {user_id: ""+user_id+""},
  function(data)
  {
    if(data != '0')
    {
        $('#email_validated_date').html(data);
    }
    else
    {
      alert('Oops! Could not validate the email.');
      $('#email_validated_date').html('0');
    }
  });
}


/* Restore a user account */
function restore_account(user_id)
{
  var do_it = confirm('Are you sure you want to retore this account?');

  if(do_it == true)
  {
    $.post("/includes/ajax/restore_user_account.php", {user_id: ""+user_id+""}, function(data)
    {
      if(data == "1")
      {
        alert('Account has been successfully restored. It might take several minutes for the profile to be added back into search results. The page will now be reloaded');
        window.location.reload();
      }
      else
      {
        alert('Oops! An unknown error has occured: ' + data);
      }
    });
  }
}


/* Disable a user account */
function disable_account_adm(user_id)
{
  var do_it = confirm('ATTENTION! Deleting this account will remove it from all Twellow listings. Are you sure you want to continue?');

  if(do_it == true)
  {
    $.post("/includes/ajax/disable_user_account.php", {user_id: ""+user_id+""}, function(data)
    {
      if(data == "1")
      {
        alert('The account has been successfully disabled. The page will now be reloaded');
        window.location.reload();
      }
      else if(data == "2")
      {
        alert('Oops! You do not have permission to disable this account.');
      }
      else
      {
        alert('Oops! An unknown error has occured: ' + data);
      }
    });
  }
}
