﻿


function countdown(obj) {
    this.obj = obj;
    this.Div = "clock";
    this.BackColor = "white";
    this.ForeColor = "black";
    this.CurrentDate = "12/31/2020 5:00:00 AM"; //rramaiah: pass in current date (down to the second) so that it can be set by server
    this.TargetDate = "12/31/2020 5:00 AM";
    this.DisplayFormat = "%%D%% Days, %%H%%:%%M%%:%%S%%";
    this.CountActive = true;

    this.DisplayStr;

    this.Calcage = cd_Calcage;
    this.CountBack = cd_CountBack;
    this.Setup = cd_Setup;
}

function cd_Calcage(secs, num1, num2, compact) //rramaiah: "compact" means do not pad with zeros
{
    s = ((Math.floor(secs / num1)) % num2).toString();
    if (!compact) {
        if (s.length < 2) s = "0" + s;
    }
    return (s);
}
function cd_CountBack(secs) {
    //iwo: Stop the countdown at zero and print Boutique Closed
    if (secs <= 0) {
        this.CountActive = false;
        this.DisplayFormat = "Boutique Closed";
    }

    var days = this.Calcage(secs, 86400, 100000, true); //rramaiah: do not pad days with zeros
    this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g, days);
    if (days == 1) { //rramaiah: if days is 1, replace "Days" with "Day"
        this.DisplayStr = this.DisplayStr.replace(/Days/g, 'Day');
    }
    this.DisplayStr = this.DisplayStr.replace(/%%H%%/g, this.Calcage(secs, 3600, 24));
    this.DisplayStr = this.DisplayStr.replace(/%%M%%/g, this.Calcage(secs, 60, 60));
    this.DisplayStr = this.DisplayStr.replace(/%%S%%/g, this.Calcage(secs, 1, 60));

    //rramaiah: added error handling since countdown is used in modal cart, which is destroyed upon closure
    var tmpDiv = document.getElementById(this.Div);
    if (tmpDiv) tmpDiv.innerHTML = this.DisplayStr;
    if (this.CountActive) setTimeout("if (" + this.obj + ") " + this.obj + ".CountBack(" + (secs - 1) + ")", 990);
}
function cd_Setup() {
    var dthen = new Date(this.TargetDate);
    var dnow = new Date(this.CurrentDate); //rramaiah: see above
    ddiff = new Date(dthen - dnow);
    gsecs = Math.floor(ddiff.valueOf() / 1000);
    this.CountBack(gsecs);
}