function populateAreaOptions(elemId, areaTop, selected)
{
  var elem = $(elemId);
  var selectedIndex = 0;
  elem.options.length = 0;
  if (!flatAreasMatch[areaTop]) {
    elem.options[0] = new Option("N/A", "0");
    return;
  }
  elem.options[0] = new Option("Please Select", "0");
  if (selected == "0") { selectedIndex = 0; }
  var index = 1;
  $H(flatAreasMatch[areaTop]).each(function(pair) {
      elem.options[index] = new Option(pair[1].match_name, pair[0]);
      if (selected == pair[0]) { selectedIndex = index; }
      index += 1;
  });
  elem.selectedIndex = selectedIndex;
}

function setArea(area_top, area)
{
  populateAreaOptions('area', area_top, area);
  /* setAreaTop(area_top); */
  setAreaDescription();
}

/*
function setAreaTop(value)
{
  var elem = document.theForm.area_top;
  if (elem) {
    elem.value = value;
  }
}

function areaTopChanged()
{
  var elem = document.theForm.area_top;
  if (elem) {
    var topId = elem.value;
    populateAreaOptions('area', topId, "0");
    return true;
  }
  return false;
}
*/

function areaChanged()
{
  setAreaDescription();
}

function setAreaDescription()
{
  var a = document.theForm.area_top;
  var b = document.theForm.area;
  var c = document.theForm.area_desc;
  if (a && b && c) {
    var topId = a.value;
    var areaId = b.value;
    var t = flatAreasMatch[topId];
    if (!t) { c.value = ''; return; }
    var ad = t[areaId];
    c.value = ad ? ad.description : '';
  }
}

var hs = []; // hideShow objects
function searchStart()
{
  ['venue_table', 'vendor_table'].each(function(n) {
    hs[n] = $(n);
  });
}

function hideShow()
{
  var search = document.theForm.search;
  var searchVal = 0;
  $A(search).each(function(r) {
    if (r.checked) { searchVal = parseInt(r.value); return; }
  });
  var venue = ((searchVal & 0x1) == 0x1);
  var vendor = ((searchVal & 0x2) == 0x2); 
  //console.log("hideShow(): searchVal="+searchVal+" venue="+venue+" vendor="+vendor);

  if (venue) {
    hs.venue_table.show();
  }
  else {
    hs.venue_table.hide();
  }
  if (vendor) {
    hs.vendor_table.show();
  }
  else {
    hs.vendor_table.hide();
  }
}


function updateOtherDays()
{
  var dre = /0*(\d+)-0*(\d+)-(\d+)/;
  for (var i = 0; i < 3; i++) {
    var dstr = document.theForm['event_date['+i+']'].value;

    var match = dre.exec(dstr);
    if (!match) { continue; }
    var y = parseInt(match[3]);
    if (y < 0) { y = 0; }
    if (y < 100) { y += 2000; }
    var dateObj = new Date(y, parseInt(match[1]) - 1, parseInt(match[2])); 
   
    if (dateObj.getDay() == 6)
    {
      if (document.theForm['other_days[6]'].checked)
      {
        var dt = new Date(dateObj.getTime() - 60000 * 60 * 24);
        setDate(3, dt.getFullYear(), dt.getMonth(), dt.getDate());
      }
      else {
        clearDate(3);
      }

      if (document.theForm['other_days[1]'].checked)
      {
        var dt = new Date(dateObj.getTime() + 60000 * 60 * 24);
        setDate(4, dt.getFullYear(), dt.getMonth(), dt.getDate());
      }
      else {
        clearDate(4);
      }
      return;
    }
  }
  clearDate(3); clearDate(4);
}

function setDate(n, y, m, d)
{
  m += 1;
  m = ((''+m).length == 1) ? ('0'+m) : (''+m);
  d = ((''+d).length == 1) ? ('0'+d) : (''+d);
  var dstr = m+'-'+d+'-'+y;
  document.theForm['event_date['+n+']'].value = dstr;
}

function clearDate(n)
{
  document.theForm['event_date['+n+']'].value = '';
}



