// calendar.js
// This code is the main engine for the Noblesse Oblige KoL calendar.
// You are welcome to take this code and use it for your own purposes,
// as long as you ask first and keep an attribution notice.
//  -- by Ragnok (#49653)

// Known bugs:
// 1) Images could use a bit of improvement.
// 2) Holidays don't appear on their real days.

var monthnames = new Array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var shortmonthnames = new Array("Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan");
var monthdays = new Array("", 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var kolmonthnames = new Array("", "Jarlsuary", "Frankruary", "Starch", "April", "Martinus", "Bill", "Bor", "Petember", "Carlvember", "Porktober", "Boozember", "Dougtember");

var nholidays = 12;
var holiday_date = new Array(1, 12, 19, 26, 34, 43, 52, 60, 70, 80, 87, 92);
var holiday_short_name = new Array("jarls", "val", "ssp", "oyster",
				   "dia", "sun", "dd", "arbor", "labor",
				   "halloween", "feast", "yuletide");
var holiday_name = new Array("Festival of Jarlsberg",
			     "Valentine\'s Day", 	
			     "St. Sneaky Pete\'s Day",
			     "Oyster Egg day",
			     "El Dia De Los Muertos Borrachos",
			     "Generic Summer Holiday",
			     "Dependence Day",
			     "Arrrrbor Day",
			     "Lab&oacute;r Day",
			     "Halloween",
			     "Feast of Boris",
			     "Yuletide");

// When these holidays first appeared in the Kingdom.

var holiday_start_month = new Array(10,  6,   10,  7,   11,  3,   6,   3,   4,   11,  2,   1);
var holiday_start_day   = new Array(26,  25,  6,   9,   28,  12,  7,   29,  8,   17,  28,  25);
var holiday_start_year  = new Array(2007,2005,2005,2005,2007,2008,2007,2008,2008,2004,2005,2008);

var musicon;
var mysicon;
var moxicon;
var nmusicons=5;
var nmysicons=5;
var nmoxicons=5;

function change(i)
{
  if (i==1) {
    curfirstkolday += curdim;
    if (curfirstkolday > 96) curfirstkolday -= 96;

    curmonth++;
    if (curmonth > 12) {
      curmonth = 1;
      curyear++;
    }
    
    curfirstdow = curnextdow;
    curdim = monthdays[curmonth];
    if (curmonth == 2 && curyear % 4 == 0 && (curyear % 100 != 0 || curyear % 400 == 0))
      curdim = 29;
    curnextdow = (curfirstdow + curdim) % 7;
  }
  else if (i==-1) {
    curmonth--;
    if (curmonth < 1) {
      curmonth = 12;
      curyear--;
    }
    curdim = monthdays[curmonth];
    if (curmonth == 2 && curyear % 4 == 0 && (curyear % 100 != 0 || curyear % 400 == 0))
      curdim = 29;
    curnextdow = curfirstdow;
    curfirstdow = (curnextdow - curdim + 70) % 7;

    curfirstkolday -= curdim;
    if (curfirstkolday < 1) curfirstkolday += 96;
  }
  else if (i!=0) {
    alert("Can only change by one month at a time, sorry!");
    return;
  }  

  var prevdim;
  if (curmonth == 1) prevdim = monthdays[12];
  else prevdim = monthdays[curmonth-1];
  if (curmonth == 3 && curyear % 4 == 0 && (curyear % 100 != 0 || curyear % 400 == 0))
    prevdim = 29;
  
  var newhead = "<b>" + monthnames[curmonth] + " " + curyear + "</b>";
  var head = document.getElementById("month");
  head.innerHTML = newhead;
  var prevmonth = document.getElementById("prevmonth");
  prevmonth.innerHTML = "<a href=\"javascript:change(-1)\">&lt;&lt;<br>" + shortmonthnames[curmonth-1] + "</a>";
  var nextmonth = document.getElementById("nextmonth");
  nextmonth.innerHTML = "<a href=\"javascript:change(1)\">&gt;&gt;<br>" + shortmonthnames[curmonth+1] + "</a>";

  for (var i=1; i<=42; i++) {
    var entry = document.getElementById("c"+i);
    var thiskolday, thisday, thismonth, thisyear;
    if (i<=curfirstdow) {
      // Carryover from previous month.
      
      thiskolday = curfirstkolday - (curfirstdow - i + 1);
      thisday = prevdim - (curfirstdow - i);
      thismonth = curmonth - 1;
      thisyear = curyear;
      if (thismonth == 0) {
        thismonth = 12;
        thisyear--;
      }
      entry.style.backgroundColor = "gray";
      entry.style.color = "white";
      entry.style.fontWeight = "normal";
    } 
    else if (i > (curdim + curfirstdow)) {
      // Preview of next month.
      
      thiskolday = curfirstkolday + (i - curfirstdow - 1);
      thisday = i - (curdim + curfirstdow);
      thismonth = curmonth + 1;
      thisyear = curyear;
      if (thismonth == 13) {
        thismonth = 1;
	thisyear++;
      }
      entry.style.backgroundColor = "gray";
      entry.style.color = "white";
      entry.style.fontWeight = "normal";
    }
    else {
      // Current month.
      
      thiskolday = curfirstkolday + (i - curfirstdow - 1);
      thisday = i - curfirstdow;
      thismonth = curmonth;
      thisyear = curyear;
      entry.style.backgroundColor = "white";
      entry.style.color = "black";
      entry.style.fontWeight = "normal";
    }

    if (thisday == nowday && thismonth == nowmonth && thisyear == nowyear) {
      entry.style.backgroundColor = "black";
      entry.style.color = "white";
      entry.style.fontWeight = "bold";
    }

    if (thiskolday < 1) thiskolday += 96;
    if (thiskolday > 96) thiskolday -= 96;

    entry.onclick = new Function('show_detail(' + thiskolday + ',' + thismonth + ',' + thisday + ',' + thisyear + ');');
    
    entry.innerHTML = thisday;

    entry.style.backgroundImage = 'none';

    var already_used = 0;
    for (var j=0; j<nholidays; j++) {
      if (thiskolday == holiday_date[j] &&
	  (thisyear > holiday_start_year[j] ||	
	   (thisyear == holiday_start_year[j] && thismonth > holiday_start_month[j]) ||
	   (thisyear == holiday_start_year[j] && thismonth == holiday_start_month[j] && thisday >= holiday_start_day[j]))) {
	entry.style.backgroundImage = 'url(cal-' + holiday_short_name[j] + '.gif)';
	already_used = 1;
      }
    }
    if (thisday == 25 && thismonth == 6 && thisyear == 2007) {
	entry.style.backgroundImage = 'url(cal-ns13.gif)';
	already_used = 1;
    }
    if (thiskolday % 16 == 0 || thiskolday % 16 == 1) {
      if (already_used == 1)
	entry.innerHTML += '<br><img src=\"cal-mox-' + moxicon + '.gif\">';
      else
	entry.style.backgroundImage = 'url(cal-mox-' + moxicon + '.gif)';
    }
    if (thiskolday % 16 == 9 || thiskolday % 16 == 10) {
      if (already_used == 1)
	entry.innerHTML += '<br><img src=\"cal-mus-' + musicon + '.gif\">';
      else
	entry.style.backgroundImage = 'url(cal-mus-' + musicon + '.gif)';
    }
    if (thiskolday % 16 == 5 || thiskolday % 16 == 13) {
      if (already_used == 1)
	entry.innerHTML += '<br><img src=\"cal-mys-' + mysicon + '.gif\">';
      else
	entry.style.backgroundImage = 'url(cal-mys-' + mysicon + '.gif)';
    } 
  } // day loop
}

function show_detail(thiskolday, thismonth, thisday, thisyear) {
  // This might get called with an argument > 96 from setup(), so fix
  // it if necessary.
  if (thiskolday > 96) thiskolday -= 96;
    
  var output = document.getElementById("output");
  
  var thisname = monthnames[thismonth] + " " + thisday + ", " + thisyear;

  var thiskoldom = (thiskolday-1) % 8 + 1;
  var thiskolmonth = Math.floor((thiskolday-1)/8) + 1;

  var rcycleday = (thiskolday-1) % 8;
  var rfullness = rcycleday;
  if (rfullness > 4) rfullness = 8 - rfullness;
  var gcycleday = Math.floor(((thiskolday-1) % 16)/2);
  var gfullness = gcycleday;
  if (gfullness > 4) gfullness = 8 - gfullness;
  var totfullness = rfullness + gfullness;
  var gdarkness = 4 - gfullness;
  
  var hcycleday = -1;

  var outstring = "<b><font size=\"+2\">" + thisname + "</font></b><p>";
  outstring += "Kingdom date: <b>" +
  kolmonthnames[thiskolmonth] + " " + thiskoldom;

  for (var i=0; i<nholidays; i++) {
    if (thiskolday == holiday_date[i] &&
	(thisyear > holiday_start_year[i] ||	
	 (thisyear == holiday_start_year[i] && thismonth > holiday_start_month[i]) ||
	 (thisyear == holiday_start_year[i] && thismonth == holiday_start_month[i] && thisday >= holiday_start_day[i]))) {
      outstring += " (" + holiday_name[i] + "!)";
    }
  }

  // Moons
  
  outstring += "</b><h3>Moons</h3>";
  
  // Old-style, before Grimace catastrophe

  if (thisyear < 2006 || (thisyear == 2006 && thismonth < 6) ||
      (thisyear == 2006 && thismonth == 6 && thisday <= 2)) {
    outstring += "Ronald: <img src=";
    outstring += "\"moon" + (rcycleday+1) + ".gif\">";
    outstring += "&nbsp;Grimace: <img src=";
    outstring += "\"moon" + (gcycleday+1);

    outstring += ".gif\">";
    if (rcycleday == gcycleday)
      outstring += " <b>Moxie day!</b>";
    if (rfullness == 4)
      outstring += " <b>Mysticality day!</b>";
    if (gfullness == 4)
      outstring += " <b>Muscle day!</b>";

  }
  else {

    // Post-catastrophe

    // This is an educated guess based on the 11-day cycle theory.
    // I pinched it from Dentarthurdent on the wiki, who apparently
    // got it from kublai on the HCO forums.
    
    // We count from a "zero date" chosen before the catastrophe.
    
    var refdate = new Date(2006, 4, 28);
    var mydate = new Date(thisyear, thismonth-1, thisday);

    // This computation doesn't always produce an integer due to DST
    // issues. I think rounding it should be sufficient for
    // our purposes, but I'll check again to see if it remains
    // a problem later. Hopefully not.
    
    var daysref = (mydate.getTime() - refdate.getTime())/(3600*24*1000);
    
    var hcycleday = (Math.round(daysref) * 2) % 11;

    outstring += "<table cellpadding=0><tr><td valign=center>Ronald &rarr;</td>";

    if (hcycleday == 8) { // Left of R.
      // Here we hypothesize that the color of the moon is determined
      // by the left side of R.

      if (rcycleday >= 1 && rcycleday <= 4) {
	outstring += "<td width=20 align=right><img src=\"minimoon.gif\"></td>";
	totfullness++;
      }
      else {
	outstring += "<td width=20 align=right><img src=\"minimoon2.gif\"></td>";
	gdarkness++;
      }
    }

    outstring += "<td align=center valign=center><img src=\"moon";
    outstring += (rcycleday+1);
    if (hcycleday == 9) { // Over left side of R
      outstring += "a";
      if (rcycleday >= 1 && rcycleday <= 4) {
	totfullness--;
	gdarkness++;
      }
      else
	totfullness++;
    } 
      
    if (hcycleday == 10) { // Over right side of R
      outstring += "b";
      if (rcycleday >= 4) {
	totfullness--;
	gdarkness++;
      }
      else
	totfullness++;
    }
    outstring += ".gif\"></td>";

    if (hcycleday == 0) {	
      // Dead center. In this case the moon appears to be determined
      // by BOTH the near side of Grimace and Ronald.
      var tot = 0;
      if (rcycleday >= 4 && rcycleday <= 7) tot++;
      if (gcycleday >= 1 && gcycleday <= 4) tot++;

      if (tot == 0) {
	outstring += "<td width=20 align=center><img src=\"minimoon2.gif\"></td>";
	gdarkness++;
      }
      else
	outstring += "<td width=20 align=center><img src=\"minimoon.gif\"></td>";
      totfullness += tot;
    }
    else if (hcycleday == 5) {
      // Closer to the Grimace side. Should be determined by the left
      // side of Grimace.
      if (gcycleday >= 1 && gcycleday <= 4) {
	outstring += "<td width=20 align=right><img src=\"minimoon.gif\"></td>";
	totfullness++;
      }
      else {
	outstring += "<td width=20 align=right><img src=\"minimoon2.gif\"></td>";
	gdarkness++;
      }
    }
    else if (hcycleday == 6) {
      // Closer to the Ronald side. Here we hypothesize that it is determined
      // by the right side of R.
      if (rcycleday >= 4 && rcycleday <= 7) {
	outstring += "<td width=20 align=left><img src=\"minimoon.gif\"></td>";
	totfullness++;
      }
      else {
	outstring += "<td width=20 align=left><img src=\"minimoon2.gif\"></td>";
	gdarkness++;
      }
    }
    else {
      outstring += "<td width=20 align=center>&nbsp;</td>";
    }
    
    outstring += "<td align=center valign=center><img src=\"moon";
    outstring += (gcycleday+1);
    if (hcycleday == 1) { // Over left side of G
      outstring += "a";
      if (gcycleday >= 1 && gcycleday <= 4) {
	totfullness--;
	gdarkness++;
      }
      else {
	totfullness++;
	gdarkness--;
      }
    }
    if (hcycleday == 2) { // Over right side of G
      outstring += "b";
      if (gcycleday >= 4) {
	totfullness--;
	gdarkness++;
      }
      else {
	totfullness++;
	gdarkness--;
      }
    }
    
    outstring += ".gif\"></td>";

    if (hcycleday == 3) { // Right side of G. Check G's fullness.
      if (gcycleday >= 4 && gcycleday <= 7) {
	outstring += "<td width=20 align=left><img src=\"minimoon.gif\"></td>";
	totfullness++;
      }
      else {
	outstring += "<td width=20 align=left><img src=\"minimoon2.gif\"></td>";
	gdarkness++;
      }
    }

    outstring += "<td valign=center>&larr; Grimace</td>";

    if (rcycleday == gcycleday)
      outstring += " <td><b>&nbsp;Moxie day!</b></td>";
    if (rfullness == 4)
      outstring += " <td><b>&nbsp;Mysticality day!</b></td>";
    if (gfullness == 4)
      outstring += " <td><b>&nbsp;Muscle day!</b></td>";

    outstring += "</tr></table>";
  }

  outstring += "<ul><li><a href=\"wereseal.html\">Wereseal</a>: ";

  // Pre-NS-13 formula
  if (thisyear < 2007 || (thisyear == 2007 && thismonth < 6) ||
      (thisyear == 2007 && thismonth == 6 && thisday < 25)) {
    if (totfullness >= 4) outstring += "+";
    outstring += (totfullness - 4)*25 + "%";
  }
  else {
    outstring += "+"+(10+Math.round(2000*Math.sqrt(totfullness))/100) + "%";
  }

  outstring += "</li><li>Grue: ";
  if (totfullness <= 5) outstring += "will fight";
  else outstring += "won't fight";
  outstring += "</li><li>Baio: +" + (totfullness*10) + "%";
  outstring += "</li><li>Jekyllin: +" + (9-totfullness) + " all stats, +" +
  (15 + 5*totfullness) + "% items";
  if (hcycleday > -1) {
    outstring += "</li><li>Grimacite items: +" + (10*gdarkness);
    outstring += "</li><li>Depleted Grimacite: +" + (5*gdarkness) + "%, +" + gdarkness + " adv/fights";
  }
  outstring += "</li></ul>";

  var tailstring = "";
  
  // Daily data

  if (thisyear > nowyear ||
      (thisyear == nowyear && thismonth > nowmonth) ||
      (thisyear == nowyear && thismonth == nowmonth && thisday > nowday)) {
    
    // The future.

    tailstring = "<p><i>This date is in the future -- no Daily Dungeon, etc. information is available.</i>";
  }
  else if (thisyear < 2006 || (thisyear == 2006 && thismonth < 4) ||
	   (thisyear == 2006 && thismonth == 4 && thisday < 25)) {
    
    // 04-25-2006 is when our data starts, so any time before this has
    // no data.
    
    tailstring = "<p><i>This date is before our data collection begins -- no Daily Dungeon, etc. information is available.</i>";
  }
  else {
    // Now, the trickiest part of all -- the case when we do have data.
    // This code is courtesy of boutell.com --
    // http://www.boutell.com/newfaq/creating/include.html
    
    var req = false;
    // For Safari, Firefox, and other non-MS browsers
    if (window.XMLHttpRequest) {
      try {
	req = new XMLHttpRequest();
      } catch (e) {
	req = false;
      }
    } else if (window.ActiveXObject) {
      // For Internet Explorer on Windows
      try {
	req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
	try {
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
	  req = false;
	}
      }
    }
    
    if (req) {
      //var url = "http://www.noblesse-oblige.org/ragnok/daily_" + thisyear
      var url = "./daily_" + thisyear
	+ ((thismonth < 10) ? "0" : "") + thismonth
	+ ((thisday < 10) ? "0" : "") + thisday + ".html";
      
      // Synchronous request, wait till we have it all
      req.open('GET', url, false);
      req.send(null);
      outstring += req.responseText;
    } else {
      tailstring = "<p><i>Sorry, your browser doesn't support the necessary JavaScript to retrieve the Daily Dungeon, etc. data.</i>";
    }
  } // daily data if-then

  // Stat days
  
  outstring += "<h3>Upcoming Stat Days</h3>";
  if (gcycleday == 4)
    outstring += "Muscle day today!<br>";
  else if (gcycleday < 4) {
    var nextmus = ((4-gcycleday)*2+(1-rcycleday%2)-1);
    if (nextmus == 1)
      outstring += "Next muscle day: tomorrow<br>";
    else
      outstring += "Next muscle day: " + nextmus + " days<br>";
  }
  else
    outstring += "Next muscle day: " + ((12-gcycleday)*2+(1-rcycleday%2)-1) + " days<br>";
  if (rcycleday == 4)
    outstring += "Mysticality day today!<br>";
  else {
    var nextmys = 4-rcycleday;
    if (nextmys < 0) nextmys += 8;
    if (nextmys==1)
      outstring += "Next mysticality day: tomorrow<br>";
    else
      outstring += "Next mysticality day: " + nextmys + " days<br>";
  }
  if (rcycleday == gcycleday)
    outstring += "Moxie day today!<br>";
  else {
    var nextmox = 7-rcycleday;
    if (gcycleday < 4) nextmox += 8;
    if (nextmox==1)
      outstring += "Next moxie day: tomorrow<br>";
    else
      outstring += "Next moxie day: " + nextmox + " days<br>";
  }

  // Holidays
  
  outstring += "<h3>Upcoming Holidays</h3>";

  var i_start = 0;
  for (var i=nholidays-1; i>=0; i--) {
    if (thiskolday <= holiday_date[i])
      i_start = i;
  }

  var holidays_found = 0;
  var i=i_start;
  do {
    // For this, I use Javascript's internal date functions.
    // Don't ask me why I don't do that for the regular calendar.
    // Something like false laziness.
    
    var daysuntil = holiday_date[i] - thiskolday;
    if (daysuntil < 0) daysuntil += 96;
    var mydate = new Date(thisyear, thismonth-1, thisday+daysuntil);

    // Don't print out a line unless the next scheduled appearance is
    // on or after the first appearance of the holiday.

    if (mydate.getFullYear() > holiday_start_year[i] ||
	(mydate.getFullYear() == holiday_start_year[i] && mydate.getMonth()+1 > holiday_start_month[i]) ||
	(mydate.getFullYear() == holiday_start_year[i] && mydate.getMonth()+1 == holiday_start_month[i] &&
	 mydate.getDate() >= holiday_start_month[i])) {

      holidays_found++;

      if (daysuntil == 0)
	outstring += holiday_name[i] + " today!";
      else if (daysuntil == 1)
	outstring += "Next " + holiday_name[i] + " tomorrow";
      else {
	outstring += "Next " + holiday_name[i] + ": " + daysuntil + " days ("
	  + monthnames[mydate.getMonth()+1] + " " + mydate.getDate() + ")";
      }

      // Also check to see if this holiday has actually happened yet.
      // If not, we're only guessing.
      if (nowyear < holiday_start_year[i] ||
	  (nowyear == holiday_start_year[i] && nowmonth < holiday_start_month[i]) ||
	  (nowyear == holiday_start_year[i] && nowmonth == holiday_start_month[i] &&
	   nowday <= holiday_start_day[i])) {
	outstring += " (<i>projected</i>)";
      }
      outstring += "<br>";
      
    } // we printed something
    
    i = (i+1) % nholidays;
  } while (i != i_start);

  if (holidays_found == 0) outstring += "<i>No holidays in the upcoming KoL year.</i>";
  
  // Birthdays
  
  outstring += "<h3>Player Birthdays</h3>";

  var bday = (thismonth-1) * 31 + thisday - 1;

  if (birthdays[bday] == "") {
    outstring += "No one has yet reported a birthday for this day.";
  }
  else {
    var pbday = birthdays[bday].split(",");
    for (var i=0; i<pbday.length; i++) {
      outstring += pbday[i]+"<br>";
    }
  }
  outstring += "<p><a href=\"birthday_about.html\">About the Birthday List</a>";

  outstring += tailstring;
  output.innerHTML = outstring;
  outstring = "";
}

function change_icons(i,j,k) {
  musicon = i;
  mysicon = j;
  moxicon = k;
  var exp_date = new Date(nowyear+1, nowmonth, nowday);
  document.cookie = 'ICONSETTINGS='+i+','+j+','+k+';expires='+exp_date.toGMTString();
  change(0);
}

function customize_icons() {
  var output = document.getElementById("iconarea");

  var outstring = '<form><input type="button" value="Save Changes" onClick="save_custom()"> <input type="button" value="Discard Changes" onClick="discard_custom()"><p><table width="95%"><tr><th>Mus day icon</th><th>Mys day icon</th><th>Mox day icon</th></tr>';
  
  outstring += '<tr><td class="monthname">'
  for (var i=0; i<nmusicons; i++) {
    outstring += '<input type="radio" name="mus_icon" id="mus'+i+'" value="' + i + '"> <img src="cal-mus-' + i + '.gif"><br>\n';
  }
  outstring += '</td><td class="monthname">';
  for (var i=0; i<nmysicons; i++) {
    outstring += '<input type="radio" name="mys_icon" id="mys'+i+'" value="' + i + '"> <img src="cal-mys-' + i + '.gif"><br>\n';
  }
  outstring += '</td><td class="monthname">';
  for (var i=0; i<nmoxicons; i++) {
    outstring += '<input type="radio" name="mox_icon" id="mox'+i+'" value="' + i + '"> <img src="cal-mox-' + i + '.gif"><br>\n';
  }
  outstring += "</td></tr></table></form>";

  output.innerHTML = outstring;

  document.getElementById('mus'+musicon).checked = true;
  document.getElementById('mys'+mysicon).checked = true;
  document.getElementById('mox'+moxicon).checked = true;
  document.getElementById("customize").style.display = "none";
}

function save_custom() {
  // There *has* to be a better way to do this than go through each
  // radio button one-by-one and see if it's checked.
  // Unfortunately I don't know what that way is, so we do it
  // the long way. Blah.

  var found = false;
  var i = 0;
  while (!found && i < nmusicons) {
    if (document.getElementById('mus'+i).checked == true) {
      found = true;
      musicon = i;
    }
    i++;
  }

  found = false;
  i = 0;
  while (!found && i < nmysicons) {
    if (document.getElementById('mys'+i).checked == true) {
      found = true;
      mysicon = i;
    }
    i++;
  }

  found = false;
  i = 0;
  while (!found && i < nmoxicons) {
    if (document.getElementById('mox'+i).checked == true) {
      found = true;
      moxicon = i;
    }
    i++;
  }

  document.getElementById("iconarea").innerHTML = "";
  if (musicon == 0 && mysicon == 0 && moxicon == 0) {
    document.getElementById("textradio").checked = true;
  }
  else {
    document.getElementById("iconradio").checked = true;
  }
  document.cookie = 'ICONSETTINGS='+musicon+','+mysicon+','+moxicon;
  document.getElementById("customize").style.display = "";
  change(0);
}

function discard_custom() {
  document.getElementById("iconarea").innerHTML = "";
  document.getElementById("customize").style.display = "";
}

// Pilfered from Netscape's JavaScript reference
function getCookie(name) {
  var search = name + "=";
  if (document.cookie.length > 0) { // if there are any cookies
    offset = document.cookie.indexOf(search);
    if (offset != -1) { // if cookie exists 
      offset += search.length;
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1)
        end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end));
    } 
  }
}

function setup() {
  var mycook = getCookie("ICONSETTINGS");
  if (mycook) {
    var a = new Array(3);
    a = mycook.split(",");
    musicon = a[0];
    mysicon = a[1];
    moxicon = a[2];
  }
  else {
    // No cookie, default to default icons
    musicon = 1;
    mysicon = 1;
    moxicon = 1;
  }

  if (musicon == 0 && mysicon == 0 && moxicon == 0) {
    document.getElementById("textradio").checked = true;
  }
  else {
    document.getElementById("iconradio").checked = true;
  }
  change(0);
  show_detail((nowday-1+curfirstkolday), nowmonth, nowday, nowyear)
}

