// SubCategory table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var subcategory = '\
';

// Category data table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var category = '\
Agricultural Fairs:Agricultural Fairs|\
Arts & Crafts:Arts & Crafts|\
Auto:Auto|\
Boating & Fishing:Boating & Fishing|\
Conservation:Conservation|\
Cultural Clubs/Festivals:Cultural Clubs/Festivals|\
Holiday:Holiday|\
Fairs & Festivals:Fairs & Festivals|\
Flea Markets:Flea Markets|\
Gardener`s Market:Gardener`s Market|\
Historical:Historical|\
Humanities:Humanities|\
Libraries & Literature:Libraries & Literature|\
Museums:Museums|\
Parks & Recreation:Parks & Recreation|\
Sports:Sports|\ ';
function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the category selected with the category from the category list
function populateCategory(defaultCategory) {
  var categoryLineArray = category.split('|');  // Split into lines
  var selObj = document.getElementById('categorySelect');
  selObj.options[0] = new Option('Select Category','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < categoryLineArray.length; loop++) {
    lineArray = categoryLineArray[loop].split(':');
    categoryCode  = TrimString(lineArray[0]);
    categoryName  = TrimString(lineArray[1]);
    if ( categoryCode != '' ) {
      selObj.options[loop + 1] = new Option(categoryName, categoryCode);
    }
    if ( defaultCategory == categoryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}
// Populates the category selected with the category from the category list
function populateCategory2(defaultCategory) {
  var categoryLineArray = category.split('|');  // Split into lines
  var selObj = document.getElementById('categorySelect2');
  selObj.options[0] = new Option('Select Category','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < categoryLineArray.length; loop++) {
    lineArray = categoryLineArray[loop].split(':');
    categoryCode  = TrimString(lineArray[0]);
    categoryName  = TrimString(lineArray[1]);
    if ( categoryCode != '' ) {
      selObj.options[loop + 1] = new Option(categoryName, categoryCode);
    }
    if ( defaultCategory == categoryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}
function populateSubCategory(defaultSubCategory) {
  var selObj = document.getElementById('subcategorySelect');
  var foundSubCategory = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    selObj.options[0] = new Option('Select SubCategory','');
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with subcategories from the selected category
  var subcategoryLineArray = subcategory.split("|");  // Split into lines
  var optionCntr = 1;
  for (var loop = 0; loop < subcategoryLineArray.length; loop++) {
    lineArray = subcategoryLineArray[loop].split(":");
    categoryCode  = TrimString(lineArray[0]);
    subcategoryCode    = TrimString(lineArray[1]);
    subcategoryName    = TrimString(lineArray[2]);
  if (document.getElementById('categorySelect').value == categoryCode && categoryCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('subcategorySelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","subcategory");
        inputSel.setAttribute("id",'subcategorySelect');
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('subcategorySelect');
        selObj.options[0] = new Option('Select SubCategory','');
        selObj.selectedIndex = 0;
      }
      if ( subcategoryCode != '' ) {
        selObj.options[optionCntr] = new Option(subcategoryName, subcategoryCode);
      }
      // See if it's selected from a previous post
      if ( subcategoryCode == postSubCategory && categoryCode == postCategory ) {
        selObj.selectedIndex = optionCntr;
      }
      foundSubCategory = true;
      optionCntr++
    }
    if ( defaultSubCategory == subcategoryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
  // If the category has no subcategories, change the select to a text box
  if ( ! foundSubCategory ) {
    parentObj = document.getElementById('subcategorySelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", 'subcategorySelect');
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", "subcategory");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postSubCategory);
    parentObj.appendChild(inputEl) ;
  }
}


function initCategory(category,subcategory) {
  populateCategory(category);
}
function initCategory2(category) {
  populateCategory2(category);
}

