// StarFire Almanac Record
function licContent()
{
   this.netAuth;
   this.status;
   this.issueTime;
   this.issueDate;
   this.startDate;
   this.endDate;
   this.regSel;
   this.dayLeft;
}

var julian_days =
[
   [0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334],
   [0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335]
];

var julian_days_per_month =
[
   [31,  28,  31,  30, 31, 30, 31, 31, 30, 31, 30, 31],
   [31,  29,  31,  30, 31, 30, 31, 31, 30, 31, 30, 31]
];
  
var julian1999 = 2451179.5; //the julian number for 1/1/1999

function leapYear(year)
{
   if( year%400 == 0 )
      return true;
   if( (year%4 == 0) && (year%100 != 0) )
      return true;
   return false;
}

function getDateStr(days, seconds)
{
   if(days < 0)
      return "";
      
   var year = 1999, str;
   days++; //since we are starting from the 1st of Jan, inc the difference by 1
   
   while((!leapYear(year) && days>365) || days>366)   /* more than a year left */
   {
      days -= (leapYear(year)) ? 366 : 365;
      year++;
   }
   
   var idx = (leapYear(year)) ? 1 : 0;
   
   var found = false;
   for(month=1; month<12; month++)
   {
      if(days <= julian_days[idx][month])
      {
         day = days - julian_days[idx][month-1];
         found = true;
         break;
      }
   }
   if(!found)
      day = days - julian_days[idx][11];
   
   str = month + '/' + day + '/' + year;
   if(seconds != null)
   {
      hr = Math.floor(seconds/3600)%24;
      min = Math.floor(seconds/60)%60;
      sec = seconds%60;
       str += ' ' + hr + ':' + min + ':' + sec;
   }

   return str;
}


// Main StarFire Almanac Object
function sflicenseb_obj()
{
   var MsgName = "[SFLICENSEB]"
   
   this.update = function(input)
   {
      var start = input.indexOf(MsgName);

      //don't start parsing if this is not the right message
      if(start < 0)
      {
         return false;
      }
      this.name = MsgName;

      //init the parser
      var p = new parser(input, start + MsgName.length);
      
      // get the starfirealm1b info
      this.length     = p.fetchU16();
      this.week       = p.fetchU16();
      this.TOWms      = p.fetchU32();
      this.TOW        = this.TOWms/1000;
      this.ver        = p.fetchU04();
      this.conf       = p.fetchU04();
      
      // SFLicense Info
      this.numLic    = p.fetchU08();
      this.reserved  = p.fetchU08();
      this.serial    = p.fetchU32();
      this.lic = []
      for(i=0; i<this.numLic; i++)
      {
         this.lic[i] = new licContent();
         netAuth   = p.fetchU08();
         switch(netAuth & 0x03)
         {
            case 0:  this.lic[i].netAuth = "All"; break;
            case 1:  this.lic[i].netAuth = "Net 1"; break;
            case 2:  this.lic[i].netAuth = "Net 2"; break;
            default: this.lic[i].netAuth = "n/a"; break;
         }
         this.lic[i].status    = p.fetchU08();
         this.lic[i].precise = (this.lic[i].status & 0x01) ? "Precise" : "Good";
         this.lic[i].elapsed = (this.lic[i].status & 0x02) ? "Elapsed Time" : "Calendar";
         this.lic[i].active  = (this.lic[i].status & 0x04) ? "Inactive" : "Active";
         
         var tmp = p.fetchU32();
         var issueSec = (tmp&0x1ffff);
         var issueDay = (tmp>>17);
         var startDay = p.fetchU16();
         var endDay   = p.fetchU16();
         var issueDate = getDateStr(issueDay, issueSec).split(' ');
         
         this.lic[i].issueDate = issueDate[0];
         this.lic[i].issueTime = issueDate[1];
         this.lic[i].startDate = getDateStr(startDay);
         this.lic[i].endDate   = getDateStr(endDay);
         regSel    = p.fetchU16();
         if(regSel & 0x4000)
            this.lic[i].region = "Land Only";
         else if(regSel & 0x8000)
            this.lic[i].region = "World Wide";
         else
            this.lic[i].region = "";
         
         this.lic[i].daysLicensed = p.fetchU16();
         this.lic[i].daysLeft     = p.fetchU16();
      }
      
      if (this.numLic >= 2)
      {
         this.primary = 0;//(this.lic[0].status & 0x80) >> 7;
         this.backup = 1;//(this.primary) ? 0 : 1;
      }
      else if (this.numLic >= 1)
      { 
         this.primary = 0;
         this.backup = -1;
      }
      else
      {
         this.primary = -1;
         this.backup = -1;
      }

      return true;
   }
}