var months = new Array('January', 'February', 'March', 'April', 'May', 'June',
	    'July', 'August', 'September', 'October', 'November', 'December');
var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
	    'Friday', 'Saturday');

var names = new Array(0, '1st',  '2nd',  '3rd',  '4th',  '5th',  '6th', '7th',
	 '8th', '9th',  '10th', '11th', '12th', '13th', '14th', '15th', '16th',
	'17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th',
	'26th', '27th', '28th', '29th', '30th', '31st');

/**
 * Toggle showing or hiding extra information
 *
 */

function showCat(elmt) {
  var cat = elmt.parentNode;
  var kittens = cat.childNodes;
  var elmtKids = elmt.childNodes;
  var subCat = null;
  var button = null;

  for (var i = 0; i < kittens.length; i++) {
    if (kittens[i].className == "descr") {
      subCat = kittens[i];
      break;
    }
  }

  for (var i = 0; i < elmtKids.length; i++) {
    if (elmtKids[i].className == "descrbutton") {
      button = elmtKids[i];
      break;
    }
  }

  var scStyle = subCat.style;

  if (scStyle.display == 'block') {
    scStyle.display = 'none';
    button.innerHTML = 'Show Details';
  } else {
    scStyle.display = 'block';
    button.innerHTML = 'Hide Details';
  }
}

function loadCat(id) {
  var c = document.all ? eval("document.all."+id) : document.getElementById(id);
  var kids = c.childNodes;
  for (var i = 0; i < kids.length; i++) {
    if (kids[i].href) {
      c = kids[i];
      break;
    }
  }
  showCat(c);
}

function checkDate(form) {
  var start_date = form.start_date.value;
  var repeat = form.type;
  var basename = "rpt_txt_";

  var tmp_d = Date.parse(start_date);
  if (tmp_d.toString() == 'NaN' && start_date.match(/^\d{4}[-\/]\d{2}[-\/]\d{2}/)) {
    var t = start_date.split(/[-\/]/);
    start_date = t[1]+"/"+t[2]+"/"+t[0];
  }
  var st_d = new Date(Date.parse(start_date));

  if (st_d.toString().match(/invalid/i) || st_d.toString() == 'NaN') {
    repeat.disabled = 1;
    hideRow('recurType');
  } else {
    var d = st_d.getDate();
    var m = months[st_d.getMonth()];
    var y = st_d.getFullYear();
    var weekday = days[st_d.getDay()];
    var nth_day = names[Math.floor((d - 1) / 7) + 1];
    //alert(d+"\n"+m+"\n"+y+"\n"+weekday+"\n"+nth_day);

    var t0 = "This event will repeat every";
    if (repeat.options[repeat.selectedIndex].value == 'monthly') {
      t0 += " month";
    } else {
      t0 += " year in "+m;
    }
    if (form.multiple.checked) t0 += " starting";
    t0 += " on:";

    var t1 = "The "+nth_day+" "+weekday+" of the month.";
    var t2 = "The "+names[d]+" of the month.";

    for (var i = 0; i < 3; i++)
      document.getElementById(basename+i).innerHTML = eval('t'+i);

    repeat.disabled = 0;
    var val = form.type.options[form.type.selectedIndex].value;
    if (val == 'monthly' || val == 'yearly') showRow('recurType');
  }
}

function toggleDate(elmt) {
  toggleRow('endDate');
  toggleDateText();

  var repeat = elmt.form.type
  var sel = repeat.options.selectedIndex;
  if (elmt.checked) {
    repeat.options[1] = null;
    if (sel != 0) repeat.options.selectedIndex = sel-1;
  } else {
    for (var i = repeat.options.length; i > 1; i--) {
      var p = repeat.options[i-1];
      repeat.options[i] = new Option(p.text, p.value);
    }
    repeat.options[1] = new Option('Weekly', 'weekly');
    if (sel != 0) repeat.options.selectedIndex = sel+1;
  }
  var val = repeat.options[repeat.selectedIndex].value;
  if (val == 'monthly' || val == 'yearly') checkDate(elmt.form);
}

function checkRepeat(elmt) {
  var val = elmt.options[elmt.selectedIndex].value;
  if (val == 'monthly' || val == 'yearly') {
    showRow('recurType');
    checkDate(elmt.form);
  } else {
    hideRow('recurType');
  }
}

function changeRowDisplay(id, display) {
  var children = document.getElementById(id).childNodes;
  for (var i = 0; i < children.length; i++) {
    var c = children[i];
    if (c.nodeType == 1) {
      for (var j = 0; j < c.childNodes.length; j++) {
	var c2 = c.childNodes[j];
	if (c2.tagName && c2.tagName.toLowerCase() == 'div') {
	  c2.style.display = display;
	}
      }
    }
  }
}

function showRow(id) {
  changeRowDisplay(id, 'block');
}

function hideRow(id) {
  changeRowDisplay(id, 'none');
}

function getFirstDiv(id) {
  var children = document.getElementById(id).childNodes;
  var testNode = null;
  for (var i = 0; i < children.length; i++) {
    if (testNode) break;
    var c = children[i];
    if (c.nodeType == 1) {
      for (var j = 0; j < c.childNodes.length; j++) {
	var c2 = c.childNodes[j];
	if (c2.tagName && c2.tagName.toLowerCase() == 'div') {
	  testNode = c2;
	  break;
	}
      }
    }
  }
  return testNode;
}

function toggleRow(id) {
  var testNode = getFirstDiv(id);
  if (testNode.style.display == 'block') {
    hideRow(id);
  } else {
    showRow(id);
  }
}

function toggleDateText() {
  var div = getFirstDiv('startDate');
  var txt = div.innerHTML;
  var newTxt = 'First Day of Event:';

  if (txt.match(/^First/)) newTxt = 'Date:';

  div.innerHTML = newTxt;
}

function hideAllRows() {
  for (var i = 0; i < hideArr.length; i++) {
    hideRow(hideArr[i]);
  }
}

function hilightDay(elmt, classname) {
  elmt.className = 'dateContainer '+classname;
  //elmt.style.backgroundColor = bgColor;
}
