//  timefmt1.js
//  Written:  2004-06-16 by James Alarie <jalarie@umich.edu>
//    http://spruce.flint.umich.edu/~jalarie/
//
//  Call TimeFmt1 with a time to be formatted, and format-out code.
//  Valid format-out codes are:
//
//    format code       description                     example
//    ----------------- ------------------------------- ----------------
//    HH:MM:SS am       12-hour, colons, seconds        03:37:41 pm
//    HH:MM am          12-hour, colon, no seconds      03:37 pm
//    HHMMSS am         12-hour, no colons, seconds     033741 pm
//    HHMM am           12-hour, no colons, no seconds  0337 pm
//    HH24:MM:SS        24-hour, colons, seconds        15:37:41
//    HH24:MM           24-hour, colon, no seconds      15:37
//    HH24MMSS          24-hour, no colons, seconds     153741
//    HH24MM            24-hour, no colons, no seconds  1537
//
//  Examples:
//
//    Time_out=TimeFmt1(input_time,'hh24mmss');
//
//  Notes:
//    A null input time causes return of the current time.
//    If the input value contains colon(s), leading zeros are not needed.
//    Spaces are ignored.
//    The "am" or "pm" may be upper-case, lower-case, or mixed.

        function TimeFmt1(ITime,FormatO) {
          TF_Input=ITime;                           // time to format
          TF_FormatO=FormatO;                       // desired output format
          if (!TF_Input) {
            TF_Now=new Date();
            TF_HH=TF_Now.getHours();
            TF_MM=TF_Now.getMinutes();
            TF_SS=TF_Now.getSeconds();
            TF_Input=TF_HH+':'+TF_MM+':'+TF_SS;
          }
          if (!TF_FormatO) { TF_FormatO='HH24MMSS'; }
          TF_Input=TF_Input.toLowerCase();
          TF_Input=TF_Input.replace(/\ /g,'');
          if (TF_Input == 'noon')     { TF_Input='12:00:00 pm'; }
          if (TF_Input == 'midnight') { TF_Input='12:00:00 am'; }
          TF_FormatO=TF_FormatO.toLowerCase();
          
          TF_HH='';
          TF_MM='';
          TF_SS='';
          TF_am='am';
          if ((TF_ix1=TF_Input.indexOf('am')) > -1) {
            TF_Input=TF_Input.substring(0,TF_ix1);
            TF_am='am';
          }
          if ((TF_ix1=TF_Input.indexOf('pm')) > -1) {
            TF_Input=TF_Input.substring(0,TF_ix1);
            TF_am='pm';
          }

// HH:MM:SS: 
          if (TF_Input.match(/^[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}$/)) {
            TF_ix1=TF_Input.indexOf(':');
            TF_HH=TF_Input.substring(0,TF_ix1);
            TF_Input=TF_Input.substring(TF_ix1+1);
            TF_ix1=TF_Input.indexOf(':');
            TF_MM=TF_Input.substring(0,TF_ix1);
            TF_Input=TF_Input.substring(TF_ix1+1);
            TF_SS=TF_Input;
            if ((TF_am == 'am') && (TF_HH == 12)) {
              TF_HH=0;
            }
          }
// HH:MM:
          if (TF_Input.match(/^[0-9]{1,2}\:[0-9]{1,2}$/)) {
            TF_ix1=TF_Input.indexOf(':');
            TF_HH=TF_Input.substring(0,TF_ix1);
            TF_Input=TF_Input.substring(TF_ix1+1);
            TF_MM=TF_Input;
            if ((TF_am == 'am') && (TF_HH == 12)) {
              TF_HH=0;
            }
          }
// HH24:MM:SS:
          if (TF_Input.match(/^[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}$/)) {
            TF_ix1=TF_Input.indexOf(':');
            TF_HH=TF_Input.substring(0,TF_ix1);
            TF_Input=TF_Input.substring(TF_ix1+1);
            TF_ix1=TF_Input.indexOf(':');
            TF_MM=TF_Input.substring(0,TF_ix1);
            TF_Input=TF_Input.substring(TF_ix1+1);
            TF_SS=TF_Input;
          }
// HH24:MM:
          if (TF_Input.match(/^[0-9]{1,2}\:[0-9]{1,2}$/)) {
            TF_ix1=TF_Input.indexOf(':');
            TF_HH=TF_Input.substring(0,TF_ix1);
            TF_Input=TF_Input.substring(TF_ix1+1);
            TF_ix1=TF_Input.indexOf(':');
            TF_MM=TF_Input.substring(0,TF_ix1);
          }
// HH24MMSS:
          if (TF_Input.match(/^[0-9]{6}$/)) {
            TF_HH=TF_Input.substring(0,2);
            TF_MM=TF_Input.substring(2,4);
            TF_SS=TF_Input.substring(4,6);
          }
// HH24MM:
          if (TF_Input.match(/^[0-9]{4}$/)) {
            TF_HH=TF_Input.substring(0,2);
            TF_MM=TF_Input.substring(2,4);
          }
          
          if ((TF_am == 'pm') && (TF_HH != 12)) {
            TF_HH=TF_HH*1+12;
          }
          if (TF_SS == '') { TF_SS=0; }
          TF_SS=TF_HH*3600+TF_MM*60+TF_SS*1;
          TF_HH=Math.floor(TF_SS/3600);
          TF_SS=TF_SS%3600;
          TF_MM=Math.floor(TF_SS/60);
          TF_SS=TF_SS%60;
          if (TF_HH < 10) { TF_HH='0'+TF_HH*1; }
          if (TF_MM < 10) { TF_MM='0'+TF_MM*1; }
          if (TF_SS < 10) { TF_SS='0'+TF_SS*1; }
          
// input complete; ready for output.
          TF_Out='';
          
          if (TF_FormatO == 'hh:mm:ss am') {
            if (TF_HH < 12) {
              TF_am='am';
            } else {
              TF_am='pm';
              TF_HH-=12;
            }
            if (TF_HH == 0) {
              TF_HH=12;
            }
            if (TF_HH < 10) { TF_HH='0'+TF_HH*1; }
            TF_Out=''+TF_HH+':'+TF_MM+':'+TF_SS+' '+TF_am;
          }
          if (TF_FormatO == 'hh:mm am') {
            if (TF_HH < 12) {
              TF_am='am';
            } else {
              TF_am='pm';
              TF_HH-=12;
            }
            if (TF_HH == 0) {
              TF_HH=12;
            }
            if (TF_HH < 10) { TF_HH='0'+TF_HH*1; }
            TF_Out=''+TF_HH+':'+TF_MM+' '+TF_am;
          }
          if (TF_FormatO == 'hhmmss am') {
            if (TF_HH < 12) {
              TF_am='am';
            } else {
              TF_am='pm';
              TF_HH-=12;
            }
            if (TF_HH == 0) {
              TF_HH=12;
            }
            if (TF_HH < 10) { TF_HH='0'+TF_HH*1; }
            TF_Out=''+TF_HH+TF_MM+TF_SS+' '+TF_am;
          }
          if (TF_FormatO == 'hhmm am') {
            if (TF_HH < 12) {
              TF_am='am';
            } else {
              TF_am='pm';
              TF_HH-=12;
            }
            if (TF_HH == 0) {
              TF_HH=12;
            }
            if (TF_HH < 10) { TF_HH='0'+TF_HH*1; }
            TF_Out=''+TF_HH+TF_MM+' '+TF_am;
          }
          if (TF_FormatO == 'hh24:mm:ss') {
            TF_Out=''+TF_HH+':'+TF_MM+':'+TF_SS;
          }
          if (TF_FormatO == 'hh24:mm') {
            TF_Out=''+TF_HH+':'+TF_MM;
          }
          if (TF_FormatO == 'hh24mmss') {
            TF_Out=''+TF_HH+TF_MM+TF_SS;
          }
          if (TF_FormatO == 'hh24mm') {
            TF_Out=''+TF_HH+TF_MM;
          }
          if (TF_Out == '') {
            alert('*** Invalid Format for Output: '+TF_FormatO);
          }

          return TF_Out;
        } // TimeFmt1
