/* image names */

v1 = 0; // alef
v2 = 1; // bais
v3 = 2; // gimel
v4 = 3; // dalet
v5 = 4; // hay
v6 = 5; // vav
v7 = 6; // zayin
v8 = 7; // khess
v9 = 8; // tess
v10 = 9; // yud
v20 = 10; // kaf
v30 = 11; // lamed
v40 = 12; // mem
v50 = 13; // nun
v60 = 14; // samekh
v70 = 15; // ayin
v80 = 16; // pay
v90 = 17; // tsadi
v100 = 18; // kuf
v200 = 19; // raish
v300 = 20; // shin
v400 = 21; // taf
vtick = 22; // apostrophe
vquote = 23; // quote

var alef = '&#1488;'
var bais = '&#1489;'
var gimel = '&#1490;'
var dalet = '&#1491;'
var hay = '&#1492;'
var vav = '&#1493;'
var zayin = '&#1494;'
var khess = '&#1495;'
var tess = '&#1496;'
var yud = '&#1497;'
var kaf2 = '&#1498;'
var kaf = '&#1499;'
var lamed = '&#1500;'
var mem2 = '&#1501;'
var mem = '&#1502;'
var nun2 = '&#1503;'
var nun = '&#1504;'
var samekh = '&#1505;'
var ayin = '&#1506;'
var fay2 = '&#1507;'
var pay = '&#1508;'
var tsadi2 = '&#1509;'
var tsadi = '&#1510;'
var kuf = '&#1511;'
var raish = '&#1512;'
var shin = '&#1513;'
var taf = '&#1514;'
var apostrophe = '&#1523;'
var quote = '&#1524;'

var valueImages = [
  [alef],
  [bais],
  [gimel],
  [dalet],
  [hay],
  [vav],
  [zayin],
  [khess],
  [tess],
  [yud],
  [kaf],
  [lamed],
  [mem],
  [nun],
  [samekh],
  [ayin],
  [pay],
  [tsadi],
  [kuf],
  [raish],
  [shin],
  [taf],
  [apostrophe],
  [quote]
];

var valueImagesN4 = [
  "v1.jpg", "v2.jpg", "v3.jpg", "v4.jpg", "v5.jpg",
  "v6.jpg", "v7.jpg", "v8.jpg", "v9.jpg",
  "v10.jpg", "v20.jpg", "v30.jpg", "v40.jpg", "v50.jpg",
  "v60.jpg", "v70.jpg", "v80.jpg", "v90.jpg",
  "v100.jpg", "v200.jpg", "v300.jpg", "v400.jpg", "tick.jpg", "quote.jpg"];

var monthImages = [
  [taf+shin+raish+yud], // tishri
  [khess+shin+vav+nun2], // heshvan
  [kaf+samekh+lamed+vav], // kislev
  [tess+bais+taf], // tevet
  [shin+bais+tess], // shevat
  [alef+dalet+raish], // adar
  [bais+alef+dalet+raish], // veadar
  [nun+yud+samekh+nun2], // nisan
  [alef+yud+yud+raish], // iyyar
  [samekh+yud+vav+nun2], // sivan
  [taf+mem+vav+zayin], // tamuz
  [alef+bais], // ab
  [alef+lamed+vav+lamed] // elul
];

var monthImagesN4 = [
  "m1.jpg", "m2.jpg", "m3.jpg", "m4.jpg", "m5.jpg", "m6.jpg", "m7.jpg",
  "m8.jpg", "m9.jpg", "m10.jpg", "m11.jpg", "m12.jpg", "m13.jpg"];



var n4 = (navigator.appName == "Netscape" && navigator.appVersion.charAt(0) <= 4);

var tombstoneBuffer;

function TombstoneBufferClear() {
  tombstoneBuffer = '';
}

function TombstoneBufferDisplay(v, frame) {
  if (n4) {
    if (frame != "") {
      self.frames[frame].document.write("<img src=" + valueImagesN4[v] + ">");
    } else {
      document.write("<img src=" + valueImagesN4[v] + ">");
    }
  } else {
    tombstoneBuffer = valueImages[v] + tombstoneBuffer;
  }
}

function TombstoneBufferFlush(frame) {
  rv = "";
  if (frame == -1) {
    rv = tombstoneBuffer;
  } else if (frame != "") {
    self.frames[frame].document.write(tombstoneBuffer);
  } else {
    document.write(tombstoneBuffer);
  }
  return rv;
}


function TombstoneDay(jDay, frame) {
  TombstoneBufferClear();
  if (jDay == 15 || jDay == 16) {
    // need to special case 15 and 16 which would spell out the name of G-d
    TombstoneBufferDisplay(jDay%9 - 1, frame);
    TombstoneBufferDisplay(v9, frame);
  } else {
    if (jDay % 10) {
      TombstoneBufferDisplay(jDay%10 - 1, frame);
      jDay -= jDay%10;
    }
    jDay /= 10;
    if (jDay % 10) {
      TombstoneBufferDisplay(v10 + jDay%10 - 1, frame);
    }
  }
  return TombstoneBufferFlush(frame);
}

function TombstoneMonth(jMonth, frame) {
  rv = "";
  if (n4) {
    if (frame != "") {
      self.frames[frame].document.write("<img src=" + monthImagesN4[jMonth] + ">");
    } else {
      document.write("<img src=" + monthImagesN4[jMonth] + ">");
    }
  } else {
    if (frame == -1) {
      rv = monthImages[jMonth];
    } else if (frame != "") {
      self.frames[frame].document.write(monthImages[jMonth]);
    } else {
      document.write(monthImages[jMonth]);
    }
  }
  return rv;
}

function TombstoneYear(jYear, frame) {
  var excess = (jYear - (jYear%1000))/1000;
  jYear %= 1000;
  TombstoneBufferClear();
  var tombstoneCount = 0;

  var correction = ((jYear % 100) == 15 || (jYear % 100) == 16);
  // need to special case 15 and 16 which would spell out the name of G-d
 
  if (jYear % 10) {
    if (correction) {
      TombstoneBufferDisplay(jYear%10, frame);
    } else {
      TombstoneBufferDisplay(jYear%10 - 1, frame);
    }
    jYear -= jYear%10;
    tombstoneCount++;
  }
  if (tombstoneCount == 1) {
    TombstoneBufferDisplay(vquote, frame); // quote before leftmost digit
    tombstoneCount++;
  }
  jYear /= 10;
  if (jYear % 10) {
    if (correction) {
      TombstoneBufferDisplay(v10 + jYear%10 - 2, frame);
    } else {
      TombstoneBufferDisplay(v10 + jYear%10 - 1, frame);
    }
    jYear -= jYear%10;
    tombstoneCount++;
  }
  if (tombstoneCount == 1) {
    TombstoneBufferDisplay(vquote, frame); // quote before leftmost digit
    tombstoneCount++;
  }
  jYear /= 10;
  jYear %= 10;
  if (jYear % 4) {
    TombstoneBufferDisplay(v100 + jYear%4 - 1, frame);
    jYear -= jYear%4
    tombstoneCount++;
    if (tombstoneCount == 1) {
      TombstoneBufferDisplay(vquote, frame); // quote before leftmost digit
      tombstoneCount++;
    }
  }
  while (jYear%10) {
    TombstoneBufferDisplay(v400, frame);
    jYear -= 4
    tombstoneCount++;
    if (tombstoneCount == 1) {
      TombstoneBufferDisplay(vquote, frame); // quote before leftmost digit
      tombstoneCount++;
    }
  }

  if (excess) {
    TombstoneBufferDisplay(vtick, frame);
    if (excess % 10) {
      TombstoneBufferDisplay(excess%10 - 1, frame);
      excess -= excess%10
      tombstoneCount++;
      if (tombstoneCount == 1) {
        TombstoneBufferDisplay(vquote, frame); // quote before leftmost digit
        tombstoneCount++;
      }
    }
    excess /= 10;
    if (excess % 10) {
      TombstoneBufferDisplay(v10 + excess%10 - 1, frame);
      tombstoneCount++;
      if (tombstoneCount == 1) {
        TombstoneBufferDisplay(vquote, frame); // quote before leftmost digit
        tombstoneCount++;
      }
    }
  }

  return TombstoneBufferFlush(frame);
}
