﻿var jsalarm = {
    padfield: function(f) {
        return (f < 10) ? "0" + f : f
    },
    showcurrenttime: function() {
        var dateobj = new Date()
        var ct = this.padfield(dateobj.getHours()) + ":" + this.padfield(dateobj.getMinutes()) + ":" + this.padfield(dateobj.getSeconds())
        this.ctref.innerHTML = ct
        this.ctref.setAttribute("title", ct)
        if (typeof this.hourwake != "undefined") { //if alarm is set
            if (this.ctref.title == (this.hourwake + ":" + this.minutewake + ":" + this.secondwake)) {
                clearInterval(jsalarm.timer)
                window.location = document.getElementById("musicloc").value
            }
        }
    },
    init: function() {
        var dateobj = new Date()
        this.ctref = document.getElementById("jsalarm_ct")
        var selections = document.getElementsByTagName("select")
        this.hourselect = selections[0]
        this.minuteselect = selections[1]
        this.secondselect = selections[2]
        
        jsalarm.showcurrenttime()
        jsalarm.timer = setInterval(function() { jsalarm.showcurrenttime() }, 1000)
    },
    setalarm: function() {
        this.hourwake = this.hourselect.options[this.hourselect.selectedIndex].value
        this.minutewake = this.minuteselect.options[this.minuteselect.selectedIndex].value
        this.secondwake = this.secondselect.options[this.secondselect.selectedIndex].value
        this.hourselect.disabled = true
        this.minuteselect.disabled = true
        this.secondselect.disabled = true
    }
}

