﻿Number.prototype.formatNumber = function(c, d, t){
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

Number.prototype.leadingZero = function()
{
    if(this < 0)
        return "00";
    else if(this < 10)
        return "0" + this
    else
        return this + "";
}

Number.prototype.formatTimeLeft = function()
{
    if(this < 1)
        return "Closing";
    
    var seconds = this;
    var days = Math.floor(seconds / 86400);
    seconds -= days * 86400;
    var hours = Math.floor(seconds / 3600);
    seconds -= hours * 3600;
    var minutes = Math.floor(seconds / 60);
    seconds -= minutes * 60;
    
    if(days > 1)
        return days + " days " + hours.leadingZero() + ":" + minutes.leadingZero() + ":" + seconds.leadingZero();
    else if(days == 1)
        return "1 day " + hours.leadingZero() + ":" + minutes.leadingZero() + ":" + seconds.leadingZero();
    else
        return hours.leadingZero() + ":" + minutes.leadingZero() + ":" + seconds.leadingZero();
}

function countdown()
{
    var seconds = 0;
    var index = 0;
    var objText = document.getElementById("TimeLeftLabel" + index)
    if (objText != null)
    {
        var objHidden = document.getElementById("TimeLeftValue" + index);
        while(objText != null)
        {
            if (objHidden.innerHTML == "Closed")
            {
                objText.innerHTML = objHidden.innerHTML;
            }
            else
            {
                seconds = objHidden.innerHTML;
                if(seconds > 0)
                {
                    seconds--;
                    objText.innerHTML = seconds.formatTimeLeft();
                    objHidden.innerHTML = seconds;
                }
            }
                
            index++;
            objText = document.getElementById("TimeLeftLabel" + index);
            objHidden = document.getElementById("TimeLeftValue" + index);
        }
        
        setTimeout("countdown();", 1000);
    }
}

function startTimer()
{
    setTimeout("refreshBids();", 5000);
}

function refreshBids_OnComplete(arg)
{
    if (arg != null)
    {
        var oPriceLabel;
        var oReserveLabel;
        var oTimeLeftValue;
        var oBidsLabel;
        var oStatusLabel;
        var oEndTimeLabel;
        
        for (var index = 0; index < arg.length; index++)
        {
            var oListingValue = document.getElementById("ListingValue" + index);
            if (oListingValue.innerHTML == arg[index].ListingID)
            {
                var oPriceLabel = document.getElementById("PriceLabel" + index);
                var oReserveLabel = document.getElementById("ReserveLabel" + index);
                var oTimeLeftValue = document.getElementById("TimeLeftValue" + index);
                var oBidsLabel = document.getElementById("BidsLabel" + index);
                var oStatusLabel = document.getElementById("StatusLabel" + index);
                var oEndTimeLabel = document.getElementById("EndTimeLabel" + index);
                var oBidIncrementLabel = document.getElementById("BidIncrementLabel" + index);
                var oMinimumBidLabel = document.getElementById("MinimumBidLabel" + index);

                oPriceLabel.innerHTML = arg[index].Price.formatNumber(2, '.', ',');
                if (oReserveLabel != null)
                {
                    if (arg[index].ReserveMet)
                    {
                        oReserveLabel.innerHTML = "Reserve met.";
                        oReserveLabel.style["color"] = "#090";
                    }
                    else
                    {
                        oReserveLabel.innerHTML = "Reserve NOT met.";
                        oReserveLabel.style["color"] = "#900";
                    }
                }
                if (arg[index].SecondsRemaining < 0)
                    oTimeLeftValue.innerHTML = "Closed";
                else
                    oTimeLeftValue.innerHTML = arg[index].SecondsRemaining;
                if (oBidsLabel != null)
                    oBidsLabel.innerHTML = arg[index].BidCount;
                if (oStatusLabel != null)
                {
                    switch (arg[index].Status)
                    {
                        case 1:
                            oStatusLabel.innerHTML = "Winning";
                            break;
                        case 2:
                            oStatusLabel.innerHTML = "Partially Winning";
                            break;
                        case 3:
                            oStatusLabel.innerHTML = "Losing";
                            break;
                    }
                }
                if (oEndTimeLabel != null)
                    oEndTimeLabel.innerHTML = arg[index].ClosesOn;
                if (oBidIncrementLabel != null && oMinimumBidLabel != null)
                {
                    if (arg[index].BidCount > 0)
                    {
                        oMinimumBidLabel.innerHTML = (arg[index].Price + parseFloat(oBidIncrementLabel.innerHTML)).formatNumber(2, '.', ',');
                    }
                }
            }
        }
    }
    startTimer();
}

function refreshBids_OnTimeout(arg)
{
    startTimer();
}

function refreshBids_OnError(arg)
{
    startTimer();
}