// StarFire Almanac Record
function alm1b_record()
{
   this.record_id;
   this.health;
   this.network_id;
   this.channel_num;
   this.lon_deg;
   this.flags;
   this.bit_rate;
   this.time_included;
   this.week;
   this.time;
}

// Main StarFire Almanac Object
function starfirealm1b_obj()
{
   var MsgName = "STARFIREALM1B"
   
   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 + 1);
      
      // 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();
      
      // StarFire Almanac
      this.set_num        = p.fetchU08();   // Set number
      this.alamanc_size   = p.fetchU08();   // Number of entries in set
      this.alm1b = [];
      
      // for (i=0; i < this.almanac_size; i++)
      for (i=0; i<this.alamanc_size; i++)
      {
              this.alm1b[i] = new alm1b_record;
      
	      Byte01 = p.fetchU08();
	      this.alm1b[i].record_id  = (Byte01 >> 4);	      
	      this.alm1b[i].health = (Byte01 >> 3) & 0x01;
	      this.alm1b[i].network_id = (Byte01 >> 1) & 0x03;
	      
	      Byte02 = p.fetchU08();
	      Byte03 = p.fetchU08();
	      this.alm1b[i].channel_num = ((Byte01 << 16) & 0x100) | (Byte02 << 8) | Byte03;

	      // Grab the next byte as a signed 8-bit value
	      Byte04 = p.fetchS08();

              // If it's negative, grab the next 4 bits as signed
	      if(Byte04 == -1)
	      {
		 Byte05_H = p.fetchS04();      
	      }
	      else
	      {
		 Byte05_H = p.fetchU04();
	      }

	      if(Byte04 < 0)
	      {
		 this.alm1b[i].lon_deg = ((Byte04 << 4) & 0xFFF0) | (Byte05_H & 0x0F) - "0xFFFF" - 1;
	      }
	      else
	      {
		 this.alm1b[i].lon_deg = ((Byte04 << 4) & 0xFFF0) | (Byte05_H & 0x0F);
	      }

	      this.alm1b[i].lon_deg = (this.alm1b[i].lon_deg * .1);

	      Byte05_L = p.fetchU04();
	      Byte06_H = p.fetchU04();
	      this.alm1b[i].flags = ((Byte05_L << 4) | (Byte06_H & 0x0F));

	      Byte06_L = p.fetchU04();
	      this.alm1b[i].bit_rate = (Byte06_L >> 1);

	      Byte07         = p.fetchU08();
	      Byte08         = p.fetchU08();
	      Byte09         = p.fetchU08();

	      // Check to see if time is stored
	      this.alm1b[i].time_included = Byte06_L & 0x01;
	      if (this.alm1b[i].time_included == 1)
	      {
		 this.alm1b[i].week = (Byte07 << 2) | (Byte08 >> 6);
		 this.alm1b[i].time= ((Byte08 << 8) & 0xFFFF) | Byte09;
	      }
	      else
	      {
		 this.alm1b[i].week = 0;
		 this.alm1b[i].time = 0;
	      }
      
      }

      return true;
   }
}