function add_tags_from_text_field () {
  add_tags(get_tags_from_text_field($('tags_string_to_add')))
}

function get_existing_tags () {
  // Get an array of the existing tags from the hidden field
  var existing_tags_string = $F('post_tags_string');
  var existing_tags = new Array();
    
  if (existing_tags_string != '') {
    existing_tags = existing_tags_string.split(/[,;]\s*/);
  }
  
  return existing_tags;
}

function get_tags_from_text_field (element) {
  // Get an array of the new tags
  var new_tags_string = element.value;
  var new_tags = new_tags_string.split(/\s*[,;]\s*/);
  
  // Remove empty tags
  var i = 0;
  while (i < new_tags.length) {
    if (new_tags[i].match(/^\s*$/)) {
      new_tags.splice(i, 1);
    }
    else {
      i++;
    }
  }
  
  // Clear tag input
  element.clear();
  
  return new_tags;
}

function update_tags (updated_tags) {
  $('post_tags_string').value = updated_tags.join(', ');
  
  var updated_tags_html_fragments = new Array();
  
  for (var i = 0; i < updated_tags.length; i++) {
    tag_html_fragment =
      '<a href="#" onclick="remove_tags([\'' +
      updated_tags[i] +
      '\']);return false;">' +
      updated_tags[i] +
      '</a>';
    updated_tags_html_fragments.push(tag_html_fragment);
  }
  var displayedTags = $('displayed_tags'); 
  displayedTags.innerHTML = updated_tags_html_fragments.join(', ');
  tagCount = updated_tags_html_fragments.length;
  if(tagCount == 0) {
    $('sticky_word_countdown_text').innerHTML = "You need at least 3.";
  } else if(tagCount == 1) {
    $('sticky_word_countdown_text').innerHTML = "A good start. You need at least 2 more.";
  } else if(tagCount == 2) {
    $('sticky_word_countdown_text').innerHTML = "One more and you're all set! (Feel free to add more, though)";
  } else if(tagCount >= 3) {
    $('sticky_word_countdown').hide();
  }
  if (tagCount < 3 && $('sticky_word_countdown').style.display == "none") {
    $('sticky_word_countdown').show();
  }
  if(tagCount > 0 && displayedTags.style.display == "none") {
    displayedTags.show();
  }
  if(tagCount == 0 && displayedTags.style.display != "none") {
    displayedTags.hide();
  }
}

function add_tags (tags) {
  var existing_tags = get_existing_tags();
  update_tags(existing_tags.concat(tags).uniq());
}

function remove_tags (tags) {
  var existing_tags = get_existing_tags();
  update_tags(existing_tags.without(tags));
}

function showPage(){
  init_search_field();
}

function editPage(){
  init_search_field();
}

function newPage(){
  init_search_field();
}

function listPage(){
  init_search_field();
}

function init_search_field() {
  default_search_val = 1;
}

function clear_default_value(obj) {
  if(default_search_val == 1) {
    obj.value = '';
    default_search_val = 0;
  }
}

function post_moderate_toggle(link, obj) {
  edit_pane = $(obj);
  if(edit_pane.style.display == "none") {
    link.innerHTML = "stop moderating";
    Effect.BlindDown(edit_pane,{duration:.35});
  } else {
    link.innerHTML = "moderate";
    Effect.BlindUp(edit_pane,{duration:.35});
  }
}

function validate_reply() {
  reply_body = $F('reply_body');
  reply_body_noquotes = reply_body.replace(/\[quote=['"]?([^'"\]\s]+)['"]?\]([^\[]+)(\[\/quote\]|\Z)/g,'');
  reply_body_noquotes = reply_body_noquotes.replace(/^\s+/,'').replace(/\s+$/,'');
  if(reply_body_noquotes.length == 0) {
    alert('Your reply is empty. (Quoting other replies doesn\'t count! :)');
    return false;
  }
  else if (reply_body.length > 2500) {
    alert('Your reply is too long.  It can be a maximum of 2500 characters.');
    return false;
  }
  else {
    return true;
  }
}

function init_editable_countdown(id, target_time, now) {
             
  diff = Math.round(target_time - now);
  local_date = new Date();
  local_target_time = local_date.getTime() + (diff*1000);
  render_editable_countdown(id, local_target_time);
}
  
  
function render_editable_countdown(id, local_target_time) {
  
  now = new Date();
  current_time = now.getTime();
  
  time_left = Math.round((local_target_time - current_time) / 1000);
  
  if(time_left < 0) {
    time_left = 0;
  }  
    
  minutes = Math.floor(time_left / 60);
  time_left %= 60;
  seconds = time_left;
  
  //Plural suffix correction
  mps = 's'; sps = 's';
  if(minutes == 1) mps ='';
  if(seconds == 1) sps ='';
    
  countdown_div_name = "editable_time_remaining_"+id;
  countdown_div = $(countdown_div_name);
  
  if(minutes == 0 && seconds == 0 ) {
    countdown_div.innerHTML = '';
    if(id == "post") {
      edit_pane_name = "post_edit_options";
    } else {
      edit_pane_name = "reply_mods_"+id;
      Element.hide('reply_'+id+'_body_field');
      Element.show('reply_'+id+'_body');
      Element.hide('reply_'+id+'_submit_tag');
      Element.hide('reply_'+id+'_cancel_button');
      Element.show('reply_'+id+'_edit_button');
    }
    edit_pane = $(edit_pane_name);
    Effect.BlindUp(edit_pane);
  } else {
    countdown_div.innerHTML = "Editable for another ";
    if(minutes > 0) {
      countdown_div.innerHTML += minutes + ' minute' + mps + ' and ';
    }
    countdown_div.innerHTML += seconds + ' second' + sps;
    if(id == "post") {
      setTimeout('render_editable_countdown("post",' + local_target_time + ');', 1000);
    } else {
    setTimeout('render_editable_countdown(' + id + ',' + local_target_time + ');', 1000);
    }
  }
}

function quote_reply(id) {
  username = $("reply_"+id+"_author_name").value;
  field = "reply_"+id+"_body_field";
  quote = (($(field).value.replace(/\[quote=['"]?([^'"\]\s]+)['"]?\]([^\[]+)(\[\/quote\]|\Z)/g,'')).replace(/^\s+/,'')).replace(/\s+$/,'');
  output = "[quote="+username+"]"+quote+"[/quote]";
  reply_body = $('reply_body');
  if(reply_body.present()) {
    newline = "\n";  
  } else {
    newline = "";
  }
  reply_body.value += newline+output+"\n";
}
