// JavaScript Document

/*

  DER: 5-1-08: Links, images, and info are now updated on a separate page, so they are commented out here

*/

function focusLastItem() {
  document.form_categories['option[' + (YAYBOO_FORM.num_items - 2) + ']'].focus();
}

function YaybooForm()
{
  this.item_array  = new Array('', '', '', '');
  //this.link_array  = new Array('', '', '', '');
  //this.image_array  = new Array('', '', '', '');
  //this.info_array  = new Array('', '', '', '');
  this.num_items   = 4;
  this.html_id     = 'yayboo_items';
  this.links_id    = 'yayboo_links';
  this.min_alert   = 'You must have at least 3 items.';
  this.num_points  = '100';
  this.show_info   = 0;
  this.tabs        = 3;

  this.recordItems = function() {
    for (var i=0; i<(this.num_items - 1); i++) {
      this.item_array[i] = document.form_categories['option[' + i + ']'].value;
      // this.link_array[i] = (this.show_info) ? document.form_categories['link[' + i + ']'].value : this.link_array[i];
      // this.image_array[i] = (this.show_info) ? document.form_categories['image_url[' + i + ']'].value : this.image_array[i];
      // this.info_array[i] = (this.show_info) ? document.form_categories['info[' + i + ']'].value : this.info_array[i];
    }
  };

  this.setShowInfo = function(val) {
    this.show_info = val;
    document.form_categories['show_info_link'].value = val;
  };

  // run this each time an option gets added to update the tabindex of the rest of the form fields
  this.updateTabs = function() {
    var i = 0;
    var obj;
    while (obj = document.getElementById('category' + i)) {
      this.tabs++;
      obj.tabIndex = this.tabs;
      i++;
    }
    if (obj = document.getElementById('tags')) {
      this.tabs++;
      obj.tabIndex = this.tabs;
    }
    if (obj = document.getElementById('source_name')) {
      this.tabs++;
      obj.tabIndex = this.tabs;
    }
    if (obj = document.getElementById('source_url')) {
      this.tabs++;
      obj.tabIndex = this.tabs;
    }
    if (obj = document.getElementById('submit_button')) {
      this.tabs++;
      obj.tabIndex = this.tabs;
    }
  };

  this.addFormItem = function() {
    this.item_array.push('');
    //this.link_array.push('');
    //this.image_array.push('');
    //this.info_array.push('');
    this.num_items = this.item_array.length;
    this.printFormItems();
    setTimeout('focusLastItem()', 300);
  };

  this.addFormItemFilled = function(x_item, x_link, x_image, x_info) {
    this.item_array.push(x_item);
    //this.link_array.push(x_link);
    //this.image_array.push(x_image);
    //this.info_array.push(x_info);
    this.num_items = this.item_array.length;
    this.printFormItems();
  }

  this.removeFormItem = function(index) {
    this.num_items = this.item_array.length;
    if ((this.num_items - 1) <= 3) {
      alert(this.min_alert);
    } else {
      for (var i=0; i<=(this.num_items - 1); i++) {
        if (i == index) {
          this.item_array.splice(index, 1);
          //this.link_array.splice(index, 1);
          //this.image_array.splice(index, 1);
          //this.info_array.splice(index, 1);
        }
      }
      this.num_items = this.item_array.length;
      this.printFormItems();
    }
  };

  this.removeAllFormItems = function() {
    this.item_array = new Array();
    //this.link_array = new Array();
    //this.image_array = new Array();
    //this.info_array = new Array();
    this.num_items = 0;
    this.printFormItems();
  }
  
  //this.printInfoLinks = function() {
  //  var obj = document.getElementById(this.links_id);
  //  if (!obj) { return; }
  //  var output = "";
  //  if (this.show_info) {
  //    output += '<a href="#" onclick="YAYBOO_FORM.recordItems(); YAYBOO_FORM.setShowInfo(0); YAYBOO_FORM.printFormItems(); return false;" id="just_titles" style="text-decoration:underline;">Titles Only</a> &nbsp; | &nbsp; <b class="black">Enable Links, Images, and Explanations</b>';
  //  } else {
  //    output += '<b class="black">Titles Only</b> &nbsp; | &nbsp; <a href="#" onclick="YAYBOO_FORM.recordItems(); YAYBOO_FORM.setShowInfo(1); YAYBOO_FORM.printFormItems(); return false;" id="add_links" style="text-decoration:underline;">Enable Links, Images, and Explanations</a>';
  //  }
  //  obj.innerHTML = output;
  //};

  this.printFormItems = function() {
    var obj = document.getElementById(this.html_id);
    if (!obj) { return; }
    var output = "";
    this.tabs = 3;
    var last_item = 0;
    var item_limit = this.num_items - 1;
    for (var i=0; i<item_limit; i++) {
      output += '<div class="yayboo_item">' + "\n";
      output += '                  <table border="0" cellspacing="0" width="480">' + "\n";
      output += '                    <tr class="middle">' + "\n";
      output += '                      <td width="20"><b class="big">' + (i + 1) + '.</b></td>' + "\n";
      output += '                      <td width="365"><input name="option[' + i + ']" onblur="" tabindex="' + (this.tabs++) + '" id="option[' + i + ']" value="' + this.item_array[i] + '" class="input_yayboo_item" type="text" maxlength="150" /></td>' + "\n";
      output += '                      <td width="25"><a href="#" onclick="YAYBOO_FORM.recordItems(); YAYBOO_FORM.removeFormItem(' + i + '); return false;"><img src="/images/buttons/minus.gif" alt="minus" width="22" height="18" align="absmiddle" class="inline"></a></td>' + "\n";
      output += '                      <td>';
      last_item = (this.tabs > last_item) ? this.tabs : last_item;
      if (i+1 == item_limit) {
        output += '&nbsp;<a href="#" onclick="YAYBOO_FORM.recordItems(); YAYBOO_FORM.addFormItem(); return false;" tabindex="' + (last_item) + '" class="lnk_add"><img src="/images/buttons/plus.gif" alt="plus" width="22" height="18" align="absmiddle" class="inline"> Add</a>';
      }
      output += '</td>' + "\n";
      output += '                    </tr>' + "\n";
      output += '                  </table>' + "\n";
      //if (this.show_info) {
      //  output += '                  <table border="0" cellspacing="0" width="480">' + "\n";
      //  output += '                    <tr>' + "\n";
      //  output += '                      <td width="20">&nbsp;</td>' + "\n";
      //  output += '                      <td width="70"><label class="label_item" for="link[' + i + ']">Website Link <br />' + "\n";
      //  output += '                      <span class="red">(optional)</span></label></td>' + "\n";
      //  output += '                      <td><input name="link[' + i + ']" tabindex="' + (this.tabs++) + '" id="link[' + i + ']" value="' + this.link_array[i] + '" class="input_yayboo_info" type="text" /></td>' + "\n";
      //  output += '                    </tr>' + "\n";
      //  output += '                    <tr>' + "\n";
      //  output += '                      <td width="20">&nbsp;</td>' + "\n";
      //  output += '                      <td width="70"><label class="label_item" for="info[' + i + ']">Explanation <br />' + "\n";
      //  output += '                      <span class="red">(optional)</span></label></td>' + "\n";
      //  output += '                      <td><textarea name="info[' + i + ']" rows="3" class="input_yayboo_info" id="info[' + i + ']" tabindex="' + (this.tabs++) + '">' + this.info_array[i] + '</textarea></td>' + "\n";
      //  output += '                    </tr>' + "\n";
      //  output += '                  </table>' + "\n";
      //}
      output += '                </div>' + "\n";
    }
    obj.innerHTML = output;
    //this.printInfoLinks();
    this.updateTabs();
  };

} // end YayBooForm

function getObj(name) {
  if (document.getElementById) {
    return document.getElementById(name);
  }
  else if (document.all) {
    return document.all[name];
  }
  else if (document.layers) {
    return document.layers[name];
  } else {
    return false;
  }
}

function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

function setCategoryClass(prefix, num, num_total, class_name, class_at) {
  var obj;
  for (var i=1; i<=num_total; i++) {
    if (obj = getObj(prefix + i)) {
      obj.className = class_name;
    }
  }
  if (obj = getObj(prefix + num)) {
    obj.className = class_at;
  }
}

