// toolkit
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function searchable_array(a) {
// http://snook.ca/archives/javascript/testing_for_a_v/
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}
function cancel_event (e) {
  e = e || window.event;
  if (e.preventDefault) {
    e.preventDefault();
  } else {
    e.returnValue = false;
  }
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


function parse_dday() {
  parse_date('dday','tribute_field_id_6');
}
function parse_bday() {
  parse_date('bday','tribute_field_id_5');
}

function parse_date (label,field_id,has_time) {
  month = document.getElementById(label+'_month').value;
  day = document.getElementById(label+'_day').value;
  year = document.getElementById(label+'_year').value;
  time = (has_time) ? document.getElementById(label+'_time').value : '00:00 AM';
  document.getElementById(field_id).value = year+'-'+month+'-'+day+' '+time;
}

function showFileUploadRow (id_of_more_fields) {
  document.getElementById(id_of_more_fields).style.display = 'block';
}

var upload_fields_count = 1;
function addFileUploadRow (self)  {
  if(upload_fields_count >3) return false;

  if(typeof(self) != "object") self = this;
  if(!self.nodeName || self.nodeName != "INPUT") self = this;
  var li = document.createElement("li");
  li.className = "field";

  var p = document.createElement("p");
  li.appendChild(p);
  
  // label and file field
  var label = document.createElement("label");
  label.innerHTML = "and another";
  p.appendChild(label);
  
  var input = document.createElement("input");
  if(self.onchange) input.onchange = addFileUploadRow;
  input.type = "file";
  input.name = self.name;
  p.appendChild(input);
  
  // label and caption field
  var label = document.createElement("label");
  label.innerHTML = "with caption";
  p.appendChild(label);
  
  var input = document.createElement("input");
  input.type = "text";
  input.name = 'field_id_'+(upload_fields_count+17);
  input.className = 'field'
  p.appendChild(input);
  
  
  
  
  if (self.parentNode.parentNode.appendChild(li)) {
    upload_fields_count++;
  }
  return false;
}

function store_initial_sort_name (full_name_field,id_of_sort_name_field) {
  full_name_field.initial_sort_name = document.getElementById(id_of_sort_name_field).value.trim();
}

function set_sort_name (full_name_field,id_of_sort_name_field,id_of_category_menu) {
  if (full_name_field.initial_sort_name != '') {return};
  full_name = full_name_field.value.trim();
  if (full_name == '') {return};
  
  // if the last ' ' is before the start of the string, ignore it and return
  // the full value
  if (full_name.lastIndexOf(' ') < 0) { sort_name = full_name; }
  
  // otherwise, return evrything after the last space
  else {
    sort_name = full_name.substr(full_name.lastIndexOf(' ')).trim();
  }
  
  sort_name_field = document.getElementById(id_of_sort_name_field)
  sort_name_field.value = sort_name;
  if (id_of_category_menu) {update_category_menu(sort_name,id_of_category_menu);};
}

function update_category_menu (sort_name_field,id_of_category_menu) {
  sort_name = (typeof(sort_name_field) == 'object') ? sort_name_field.value.trim() : sort_name_field.trim();
  if (sort_name == '') {return};
  if (sort_name.match(/^mac/i)) {
    sort_initial = 'Mac';
  }
  else if (sort_name.match(/^mc/i)) {
    sort_initial = 'Mc';
  }
  else  {
    sort_initial = sort_name.substr(0,1).toUpperCase();
  }
  
  sort_initial = filter_invalid_sort_initials(sort_initial);
  
  menu = document.getElementById(id_of_category_menu);
  if (!menu) {return};
  
  for (var i = menu.options.length - 1; i >= 0; i--){
    option = menu.options[i];
    option_label = option.text;
    // deselect...
    option.selected = false;
    // ... unless it's selected!
    if (option_label == sort_initial) {option.selected = true;};
  }
}

function filter_invalid_sort_initials (sort_initial) {
  valid_initials = searchable_array(['A','B','C','D','E','F','G','H','I','J','K','L','M','Mac','Mc','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']);
  if (sort_initial in valid_initials) {return sort_initial};
  return 'none';
}


function validate_tribute_submission() {
  form_element = document.getElementById('entryform');
  
  required_fields = ['tribute_title','tribute_field_id_4','tribute_field_id_5','tribute_field_id_6','tribute_field_id_7']
  is_valid = true;
  
  for (var i = required_fields.length - 1; i >= 0; i--){
    if (field_is_blank(required_fields[i])) {
      error_field(required_fields[i]);
      is_valid = false;
    }
  }
  
  return is_valid;

}


function validate_appreciation_submission() {
  form_element = document.getElementById('entryform');
  
  required_fields = ['tribute_field_id_16']
  is_valid = true;
  
  for (var i = required_fields.length - 1; i >= 0; i--){
    if (field_is_blank(required_fields[i])) {
      error_field(required_fields[i]);
      is_valid = false;
    }
  }
  
  tribute_field = document.getElementById('tribute_field_id_16');
  if (tribute_field) {tribute_field_value = tribute_field.value.trim()};
  if (tribute_field_value.indexOf('Tell us about ') == 0) { error_field('tribute_field_id_16'); is_valid = false; };
  
  return is_valid;

}


function field_is_blank (id) {
  el = document.getElementById(id);
  if (el)
    return el.value.trim() == '';
  else
    return true;
}

function error_field (id) {
  el = document.getElementById(id);
  if (el)
    el.parentNode.className += ' error';
}























