// File used by j_timestamp.xsl and tlx.tlx.timestamp/gem.xml

// Date/time templates are made up of any mix of these tokens,
// with optional interspersed text:
var tokens = {
    y2: '',       // 2 digits year
    y4: '',       // 4 digits year
    m1: '',       // 1 or 2 digits month ordinal
    m2: '',       // 2 digits month ordinal
    m3: '',       // 3 letters month name
    mn: '',       // spelled out month name
    d1: '',       // 1 or 2 digits day ordinal
    d2: '',       // 2 digits day ordinal
    d3: '',       // 3 letters day name
    dn: '',       // spelled out day name
    h12: '',      // hours from 01 to 12
    h24: '',      // hours from 00 to 23
    mm: '',       // minutes from 00 to 59
    ampm: '',     // AM or PM
    nodate: '',	  // no date
    notime: ''	  // no time
}; 


function Stamp (msecs, stampTemplate, monthList, dayList) {
   // msecs:   value of the instant in millisecs
   // stampTemplate:   date/time template, such as "y4/m2/d2" or "dn d1 mn y4 h24:mm"
   // monthList: names of the months in a given language
   // dayList: names of the days in a given language

   var when = (msecs == null) ? new Date() : new Date(msecs);
   var template = (stampTemplate == null) ? "" : stampTemplate;
   var monthNames = (monthList == null) ? "" : monthList.split("|");
   var dayNames = (dayList == null) ? "" : dayList.split("|");

   tokens['y4'] = when.getFullYear();            // 1970..2xxx
   tokens['y2'] = new String(tokens['y4']).substr(2,2);

   tokens['m1'] = 1 + when.getMonth();             // 1..12
   tokens['m2'] = (tokens['m1'] < 10) ? "0" + tokens['m1'] : tokens['m1'];

   tokens['mn'] = monthNames[tokens['m1'] - 1];
   tokens['m3'] = new String(tokens['mn']).substr(0,3);
   
   tokens['d1'] = when.getDate();                  // 1..31
   tokens['d2'] = (tokens['d1'] < 10) ? "0" + tokens['d1'] : tokens['d1'];

   tokens['dn'] = dayNames[when.getDay()];           // [0..6] = Sun..Sat
   tokens['d3'] = new String(tokens['dn']).substr(0,3);

   tokens['nodate'] = "";
   tokens['notime'] = "";

   var minutes = when.getMinutes();              // 0..59
   if (minutes < 10) { minutes = "0" + minutes; }
   tokens['mm'] = minutes;

   var hours24 = when.getHours();               // 0..23
//
   ampmBlock: {
      if (hours24 == 0) { 
         tokens['h24'] = '00';
         tokens['h12'] = '12';
         tokens['ampm'] = 'AM';
         break ampmBlock;
      }
      if (hours24  < 10) { 
         tokens['h24'] = '0' + hours24;
         tokens['h12'] = '0' + hours24;
         tokens['ampm'] = 'AM';
         break ampmBlock;
      }
      if (hours24  < 12) {
         tokens['h24'] = hours24;
         tokens['h12'] = hours24;
         tokens['ampm'] = 'AM';
         break ampmBlock;
      }
      if (hours24 == 12) {
         tokens['h24'] = hours24;
         tokens['h12'] = hours24;
         tokens['ampm'] = 'PM';
         break ampmBlock;
      }
      if (hours24  < 24) {
         tokens['h24'] = hours24;
         tokens['h12'] = hours24 - 12;
         tokens['ampm'] = 'PM';
         break ampmBlock;
      }
   }

//
   var mapped = "";
   var x = 0;
   topLoop: while (x < template.length) {
      for (token in tokens) {
         if (template.substr(x, token.length) == token) {
               mapped += tokens[token];
               x += token.length;
               continue topLoop;
         }
      }
      mapped += template.charAt(x);
      x++;
   }
   return mapped;
}

