// GorTimeDate.js
// routines to calculate and display Gorean Time and Date
// (c) 2004-9 by The Lara Inn Organization
// 2009-03-02 Oryx         Correct showdate's year retrieval

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// There are three functions in the tLi GorTime package:
//  -- digital date display
//  -- digital time display
//  -- analog time display (GorClock.js)
//
// Your implementation may use any, or all.
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// To use the Gor digital time/date functions, your <BODY> tag needs an OnLoad attribute
// invoking PostDigDateTime (Post-Digital-Date-&-Time) and specifying which or both to run.
//
//  PostDigDateTime takes one string argument
//      if it contains "date", the date will be initialized and updated;
//      if it contains "time", the time will be initialized and updated;
//
//  This allows you to show the time OR the date OR both in your HTML
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//  To use the analog display, see instructions in GorClock.js
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

function PostDigDateTime (what) {
    stopclock();
    if (what.toLowerCase().indexOf("date") >= 0) showdate();
    if (what.toLowerCase().indexOf("time") >= 0) showtime();
}

var timerID = null;
var timerRunning = false;
function stopclock () {
    if(timerRunning)
        clearTimeout(timerID);
    timerRunning = false;
}
//-----------------------------------------------------------------

// Julian Date Calculator by Oryx con Lara, http://the-Lara-inn.org
// if you wish to copy this, please leave these credit lines in your copy
mnLen = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);

function dat2Jul(dy, mn) {
    m = mn-1;
    jul = dy*1;   // force dy as a number
    while (m > 0) {
        jul += mnLen[m];
        --m;
    }
    return(jul);
}

// Gorean Date Calculator by Oryx con Lara, http://the-Lara-inn.org
// if you wish to copy this, please leave these credit lines in your copy
gorMonthNames = new Array(
        "En`kara",
        "Hesius",
        "Selnar",
        "En`Var",
        "Fifth",
        "Sixth",
        "Se`Kara",
        "Eighth",
        "Ninth",
        "Se`Var",
        "Eleventh",
        "Twelfth",
        "Waiting Hand"
        );
gorHandNames    = new Array("1st","2nd","3rd","4th","5th","Passage");
gorHandDays     = new Array("5th","1st","2nd","3rd","4th");
gorPassageHands = new Array("1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th");

var gorHandExp, gorMonthExp, gorYearExp;

function doGorDate(day, mon, yer) {
// Gorean Date Calculator by Oryx con Lara, http://the-Lara-inn.org
// if you wish to copy this, please leave these credit lines in your copy
  var gormon,gorday;
  var jul = dat2Jul(day, mon);
/*
  En`kara	4/19	109
  Hesius	5/19	139
  Selnar	6/18	169
  En`Var	7/18	199
  Fifth		8/17	229
  Sixth		9/16	259
  Se`Kara	10/16	289
  Eigth		11/15	319
  Ninth		12/15	349
  Se`Var	1/14	14
  Eleventh 	2/13	44
  Twelfth	3/15	74
  Waiting	3/20	79
*/

  if (   (yer % 4 == 0)   // leap year
      && (jul > 59)      // feb 29 & mar 1
      && (mon == 3)     // ONLY mar
     )
  {
    if (jul <= 78) jul += 1
    else
    if (jul == 79)
    {
        gorHandExp = "*** New Year's Day ***";
        gorMonthExp = "-=#=-";
        doGorYear(0,0,yer);
        return(1)
    }
  }

  if ( jul > 349 )
  {
    gormon = 10;
    gorday = jul - 349;
  }
  else if ( jul > 79 )
  {
    gormon = Math.floor(( jul - 79 ) / 30 + 1) ;
    gorday = ( jul - 79 ) % 30 ;
  }
  else if ( jul <= 74 )
  {
    gormon = ( jul - 14 ) / 30 ;
    gormon = Math.floor( gormon ) + 11;
    gorday = ( jul + 16 ) % 30 ;
  }
  else
  {
    gormon = 13;
    gorday = jul - 74
  }

  if ( gorday == 0 )
  {
    gorday = 30;
    gormon -= 1;
  }
  if (gormon < 13)
  {
    gormonth = gorMonthNames[gormon-1];
    if ( gormonth.substr(gormonth.length-2, 2) == "th" ) gorMonthExp = "the " + gormonth + " Month";
    else  gorMonthExp = "the Month of " + gormonth;
  }

  var gorhday = gorday % 5 + 1 ;
  var gorhand = Math.floor(( gorday - 1 ) / 5) + 1 ;
  gorhandtxt = gorHandNames[gorhand-1];
  var gorhandday = gorHandDays[gorhday-1];

  var gordate = "x";
  if ( gorhandtxt == "Passage" )
  {
    gorHandExp = gorhandday + " of the " + gorPassageHands[gormon-1] + " Passage Hand";
    gorMonthExp = "-=#=-";
  }
  else if (gormon == 13)
  {
    gorHandExp = gorhandday + " of the Waiting Hand";
    gorMonthExp = "-=#=-";
  }
  else
  {
    gorHandExp = gorday + ", " +gorhandday + " of the " + gorhandtxt + " Hand";
    gorMonthExp = "of " + gorMonthExp;
  }

  doGorYear(gorday,gormon,yer);
  return(0);
}

function doGorYear(gday,gmon,yer) {
// Gorean Date Calculator by Oryx con Lara, http://the-Lara-inn.org
// if you wish to copy this, please leave these credit lines in your copy
  var goryear = 149 - 98 + yer*1;
  if (yer > 1900) goryear -= 1900;
  if (   (gmon > 10)
      || (   (gmon == 10)
          && (gday >= 17)
         )
     ) --goryear;
  gorYearExp = "10," + goryear + " Contasta Ar";
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// showdate() - formats and displays the current, running date in common & Gorean formats
//
//      showdate requires 2 fields:
//          one, LocalDateText, has text day/month/year properties,
//          and GORDateText, with 3 fields, date_0, date_1, and year
//          ... each of which has a a text value property (usually form/input type="text")
//
//      It does NOT need to know the name of the parents of these fields, and in fact
//          they can be two different objects. Go for it!
//
//      showdate DOES require the following in the OnLoad attribute of the <BODY> tag:
//
//          onLoad="PostDigDateTime('date')"
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

function showdate () {
// Gorean Date Calculator by Oryx con Lara, http://the-Lara-inn.org
// if you wish to copy this, please leave these credit lines in your copy
    var now = new Date();

    var day = now.getDate();
    var mon = now.getMonth()+1;
    var yer = now.getFullYear();

    datObj = findObj('LocalDateText');
    if (datObj != null)
    {
        datObj.day.value = day;
        datObj.month.value = mon;
        datObj.year.value = yer;
    }
    showGorDate(day,mon,yer);
}

function showGorDate(day,mon,yer) {
// Gorean Date Calculator by Oryx con Lara, http://the-Lara-inn.org
// if you wish to copy this, please leave these credit lines in your copy
    doGorDate(day,mon,yer);
    datObj = findObj('GORDateText');
    if (datObj != null)
    {
        datObj.date_0.value = gorHandExp;
        datObj.date_0.size  = gorHandExp.length+1;
        datObj.date_1.value = gorMonthExp;
        datObj.date_1.size  = gorMonthExp.length+1;
        datObj.year.value   = gorYearExp;
        datObj.year.size    = gorYearExp.length+1;
    }
}
var forDay=0;
var forMon=1;
function isLeap(year) {
    return  (   (year % 100 != 0 )
             && ( year % 4 == 0 )
            )  
         || (year % 400 == 0 )
}
function validateDate(which,day,mon,yer) {
// Gorean Date Calculator by Oryx con Lara, http://the-Lara-inn.org
// if you wish to copy this, please leave these credit lines in your copy
    datObj = findObj('LocalDateText');
    if (datObj == null)
        return false;
        
    if (mon > 12)
    {
        datObj.mon.value = 1;
        datObj.year.value = ++yer;
    }

    else
    if (mon < 1)
    {
        datObj.month.value = 12;
        datObj.year.value = --yer;
    }

    else
    if ( day < 1 )
    {
        if ( mon == 1 )
        {
            datObj.month.value = 12;
            datObj.year.value = --yer;
        }
        else
            datObj.month.value = --mon;
        datObj.day.value = mnLen[mon];
        if (   ( mon == 2 )
            && isLeap(yer)
           )
        {
          datObj.day.value = 29;
        }
    }

    else
    if (   (day > mnLen[mon])
        && (   ( mon != 2 )
            || ( day >= 30 )
            || (!isLeap(yer))
           )
        )
    {
        if (which == forDay)
        {
            datObj.day.value = 1;
            if ( mon == 12 )
            {
                datObj.month.value = 1;
                datObj.year.value = ++yer;
            }
            else
                datObj.month.value = ++mon;
        }
        else
        {
            datObj.day.value = mnLen[mon];
            if (   ( mon == 2 )
                && isLeap(yer)
               )
            {
              datObj.day.value = 29;
            }
        }
    }
}


//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-=-=-=YOU ARE ENTERING THE DIMENSION OF TIME-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

function TGorTime() {
    this.Ahn = 0;
    this.Ehn = 0;
    this.Ihn = 0
}
GorTime = new TGorTime;
function getGorTime (time) {
// Gorean Time Calculator by Oryx con Lara
// 20 * 72 = 1440 min = 24 hrs
      var hours = time.getHours();
      var minutes = time.getMinutes();
      var seconds = time.getSeconds();
      var milli = time.getMilliseconds();
      var MinPastMid = hours * 60 + minutes + ( (seconds + milli / 1000)/ 60 );
      var ahn = Math.floor(MinPastMid / 72);
      var ehn = Math.floor((MinPastMid / 72 - ahn) * 40);
      var ihn = Math.floor((((MinPastMid / 72 - ahn) * 40) - ehn) * 80);
      GorTime.Ahn = ahn;
      GorTime.Ehn = ehn;
      GorTime.Ihn = ihn;
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// showtime() - formats and displays the current, running time in three formats
//
//      showtime requires 3 fields than have a text value property (usually
//      form/input type="text") named (coincidentally enough)
//
//          LocalTimeText, UTCTimeText, and GORTimeText
//
//      It does NOT need to know the name of the parents of these fields, and in fact
//          they can be three different objects. Go for it!
//
//      showtime DOES require the following in the OnLoad attribute of the <BODY> tag:
//
//          onLoad="PostDigDateTime('time')"
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

function showtime () {
      var now = new Date();
      var hours = now.getHours();
      var minutes = now.getMinutes();
      var seconds = now.getSeconds();
      var milli = now.getMilliseconds();
   // var timeValue = "" + ((hours >12) ? hours -12 :hours)
      var timeValue = ((hours <10) ? "0" : "") + hours

      timeValue += ((minutes < 10) ? ":0" : ":") + minutes
      timeValue += ((seconds < 10) ? ":0" : ":") + seconds
   // timeValue += (hours >= 12) ? " P.M." : " A.M."
      timObj = findObj('LocalTimeText');
      if (timObj != null)
        timObj.value = timeValue;
      else
        alert("No LocalTimeText :(");

      getGorTime(now);
      ahn = GorTime.Ahn;
      ehn = GorTime.Ehn;
      ihn = GorTime.Ihn;
      if (ahn < 10) ahn = "0" + ahn;
      if (ehn < 10) ehn = "0" + ehn;
      if (ihn < 10) ihn = "0" + ihn;
      var gortimeString = ahn + " Ahn, " + ehn + " Ehn, " + ihn + " Ihn";
      timObj = findObj('GorTimeText');
      if (timObj != null)
        timObj.value = gortimeString;
      else
        alert("No GorTimeText :(");

// UTC Time
      hours = now.getUTCHours();
      minutes = now.getUTCMinutes();
      seconds = now.getUTCSeconds();
      timeValue = ((hours <10) ? "0" : "") + hours;

      timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
      timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
      timObj = findObj('UTCTimeText');
      if (timObj != null)
        timObj.value = timeValue;
      else
        alert("No UTCTimeText :(");
  
      timerID = setTimeout("showtime()",300);
      timerRunning = true;
}
// --------------- END OF GorTimeDate.js

