<!--

////////////////////////////////////////////////////////
// Copyright 2003, Timothy James Forsythe, all rights reserved.
// Permission granted to use, copy, modify, and distribute so long as
// the above copyright and this permission statement are retained in all
// copies.  THERE IS NO WARRANTY - USE AT YOUR OWN RISK.
//
// based on the algorithms of ?
////////////////////////////////////////////////////////


function cvts(n) 
{
    // converts number 1 - 4999 to a subtractive Roman numeral ...
    return (cvt1000(n) + cvt100s(n) + cvt10s(n) + cvt1s(n))
}

function cvt1000(n) 
{
    // converts thousands digit to a Roman numeral ...
    return ('MMMM'.substr(0, (Math.floor(n / 1000))));
}

function cvt100s(h) 
{
    // converts hundreds digit to a subtractive Roman numeral ...
    var m = '', h = Math.floor((h % 1000) / 100);
    if (h == 9) {m = 'CM'}
     else if (h > 4) {m = 'DCCC'.substr(0, h - 4)}
      else if (h == 4) {m = 'CD'}
       else {m = 'CCC'.substr(0, h)};
    return m;
}

function cvt10s(t) 
{
    // converts tens digit to a subtractive Roman numeral ...
    var m = '', t = Math.floor((t % 100) / 10);
    if (t == 9) {m = 'XC'}
     else if (t > 4) {m = 'LXXX'.substr(0, t - 4)}
      else if (t == 4) {m = 'XL'}
       else {m = 'XXX'.substr(0, t)};
    return m;
}

function cvt1s(u) 
{
    // converts units digit to a subtractive Roman numeral ...
    var m = '', u = u % 10;
    if (u == 9) {m = 'IX'}
     else if (u > 4) {m = 'VIII'.substr(0, u - 4)}
      else if (u == 4) {m = 'IV'}
       else {m = 'III'.substr(0, u)};     
    return m;
}

function RomanDate(JD, JM, NON, ID, PKAL) 
{
    var Rmth = new Array('IAN', 'FEB', 'MART', 'APR', 'MAI', 'IVN', 'IVL', 'AVG', 'SEP', 'OCT', 'NOV', 'DEC');
   
    // returns string with date in Roman format (except for leap February) ...
    var rd;
    if (JD == 1) {rd = 'KAL. ' + Rmth[JM - 1]}
     else if (JD < NON - 1) {rd = 'A.D. ' + cvts(NON + 1 - JD) + ' NON. ' + Rmth[JM - 1]}
      else if (JD == NON - 1) {rd = 'PRID. NON. ' + Rmth[JM - 1]}
       else if (JD == NON) {rd = 'NON. ' + Rmth[JM - 1]}
        else if (JD < ID - 1) {rd = 'A.D. ' + cvts(ID + 1 - JD) + ' ID. ' + Rmth[JM - 1]}
         else if (JD == ID - 1) {rd = 'PRID. ID. ' + Rmth[JM - 1]}
          else if (JD == ID) {rd = 'ID. ' + Rmth[JM - 1]}
           else if (JD < PKAL) {rd = 'A.D. ' + cvts(PKAL + 2 - JD) + ' KAL. ' + Rmth[JM % 12]}
            else {rd = 'PRID. KAL. ' + Rmth[JM % 12]};
    
    return rd;
}

function RomanDateLeapFeb(JD) 
{
    // returns string with date in Roman format for leap February ...
    var rd;
    if (JD == 1) {rd = 'KAL. FEB'}
     else if (JD < 4) {rd = 'A.D. ' + cvts(6 - JD) + ' NON. FEB'}
      else if (JD == 4) {rd = 'PRID. NON. FEB'}
       else if (JD == 5) {rd = 'NON. FEB'}
        else if (JD < 12) {rd = 'A.D. ' + cvts(14 - JD) + ' ID. FEB'}
         else if (JD == 12) {rd = 'PRID. ID. FEB'}
          else if (JD == 13) {rd = 'ID. FEB'}
           else if (JD < 25) {rd = 'A.D. ' + cvts(30 - JD) + ' KAL. MART'}
            else if (JD == 25) {rd = 'BIS VI KAL. MART'}
             else if (JD < 29) {rd = 'A.D. ' + cvts(31 - JD) + ' KAL. MART'}
              else {rd = 'PRID. KAL. MART'};
              
    return rd;
}

function RoDate(ldstr,ld,lmstr,ly,lstr,rdstr,rd,ry,rstr)
{
  this.ldstr  = ldstr;
  this.ld     = ld;
  this.ldmstr = lmstr;
  this.ly     = ly;
  this.lstr   = lstr;

  this.rdstr  = rdstr;
  this.rd     = rd;
  this.ry     = ry;
  this.rstr   = rstr;
}

function JDNJulianToRoman(jdn,JD,JM,JY)
{
    var Rday = new Array('Dies Solis', 'Dies Lun\xC6', 'Dies Martis', 'Dies Mercurii', 'Dies Iovis', 'Dies Veneris', 'Dies Saturni');
    var Lday = new Array('Dies Dominica', 'Dies Lun\xE6', 'Dies Martis', 'Dies Mercurii', 'Dies Iovis', 'Dies Veneris', 'Dies Saturni');
    var Lmth = new Array('Januarius', 'Februarius', 'Martius', 'Aprilis', 'Maius', 'Junius', 'Julius', 'Augustus', 'September', 'October', 'Novembris', 'December');
    
    var date = new RoDate(0,0,0,0,0,0,0,0,0);
    
    var day = (jdn + 1) % 7;
    var rd;
    var RY = JY + 753; 
      
    if (JM == 12 && JD > 13) 
    {
      RY++;
    }
     
    if (JM == 3 || JM == 5 || JM == 7 || JM == 10) 
    {
      rd = RomanDate(JD, JM, 7, 15, 31);
    }
    else if (JM == 1 || JM == 8 || JM == 12) 
    {
      rd = RomanDate(JD, JM, 5, 13, 31);
    }
    else if (JM == 4 || JM == 6 || JM == 9 || JM == 11) 
    {
      rd = RomanDate(JD, JM, 5, 13, 30);
    }
    else if (JY % 4 == 0) 
    {
      rd = RomanDateLeapFeb(JD);
    }
    else 
    {
      rd = RomanDate(JD, JM, 5, 13, 28);
    }

    date.ldstr = Lday[day];
    date.ld    = cvts(JD).toLowerCase();
    date.lmstr = Lmth[JM - 1];
    date.ly    = cvts(JY);
    date.lstr  = date.ldstr + ' ' + date.ld + ' ' + date.lmstr + ' ' + date.ly;
    
    date.rdstr = Rday[day];
    date.rd    = rd;
    date.ry    = cvts(RY) + ' A.U.C.';
    date.rstr  = Rday[day] + ' ' + rd + '. ' + cvts(RY) + ' A.U.C.';
    
    return date;
}

// -->
