﻿
function Clock (hour, minute, second, controlName, objName) {
  this.hour = hour ;
  this.minute = minute ;
  this.second = second ;

  this.controlName = controlName ;
  this.displayObj = null ;  
  this.objName = objName ;
  this.update = Clock_update  ;
  this.start = Clock_start ;
  this.build_hour = Clock_build_hour ;

}

function Clock_update() {
  var timeStr ;
  
  this.second ++ ;

  if (this.second >= 60) {
    this.second = 0 ;
    this.minute ++ ;

    if (this.minute >= 60) {
      this.minute = 0 ;
      this.hour ++ ;

      if (this.hour >= 24) {
        this.hour = 0 ;
      }

    }
  }

  timeStr = this.build_hour (this.hour, this.minute, this.second) ;

  if (!this.displayObj) {
    this.displayObj = getElement (this.controlName) ;
  }
  
  this.displayObj.value = timeStr ;
  
}

function Clock_start() {
  var func = this.objName + ".update()" ;
  setInterval (func, 1000) ;
}

function Clock_build_hour (hour, minute, second) {
  var strTime = decimal_to_string (hour, 2) + ":" +
                decimal_to_string (minute, 2) + ":" +
                decimal_to_string (second, 2) ;

  return (strTime) ;

}