if (document.getElementById('remaining_auction_time')) {
    refreshAuctionDetails(auctionID);
    refreshAuctionInterval = setInterval('refreshAuctionDetails(' + auctionID + ')', 3000);
}

function add_leading_zero(n) {
    if (n.toString().length < 2) {
        return '0' + n;
    }
    else {
        return n;
    }
}

function format_output(time_left) {
    var days, hours, minutes, seconds;
    days = Math.floor(time_left / 86400);
    temporary_time = time_left - (days * 86400);


    seconds = temporary_time % 60;
    minutes = Math.floor(temporary_time / 60) % 60;
    hours = Math.floor(temporary_time / 3600);

    seconds = add_leading_zero(seconds);
    minutes = add_leading_zero(minutes);
    hours = add_leading_zero(hours);

    return days + ' jours ' + hours + 'h ' + minutes + 'm ' + seconds + 's';
}

function refreshAuctionDetails(iAuctionId) {
    jQuery.ajax({
        type: 'GET',
        url: '/ajax_commands/refresh_auction_details.php',
        data: 'fk_auctions=' + iAuctionId + '&random_number=' + (new Date()).getDate(),
        success: function (msg) {
            json = eval("(" + msg + ")");
            document.getElementById('current_winner').innerHTML = json.winner;
            document.getElementById('actual_bid').innerHTML = json.current_bid;
            remainingSeconds = json.timestamp_end;

            document.getElementById('remaining_auction_time').innerHTML = format_output(remainingSeconds);

            if (remainingSeconds <= 50 && remainingSeconds > 0) {
                document.getElementById('remaining_auction_time').style.backgroundColor = 'yellow';
                document.getElementById('remaining_auction_time').style.color = 'red';
                document.getElementById('remaining_auction_time').style.fontWeight = 'bold';
            }
            else if (remainingSeconds <= 0) {
                document.getElementById('bidding_section').style.display = 'none';
                document.getElementById('remaining_auction_time').innerHTML = "L'enchère est terminée!";
                clearInterval(refreshAuctionInterval);

                jQuery.ajax({
                    type: 'GET',
                    url: '/ajax_commands/set_auction_won.php',
                    data: 'pk_auctions=' + iAuctionId + '&random_number=' + (new Date()).getDate(),
                    success: function (msg) {
                    }
                });
            }
        }
    });
}

function incrementNormalBet() {
    var currentBid = document.getElementById('current_bid');

    if (dollarsOnly == 'yes') {
        currentBid.value = (Math.abs(currentBid.value) + 1).toFixed(2);
    }
    else {
        currentBid.value = (Math.abs(currentBid.value) + .01).toFixed(2);
    }
}

function incrementProxyBid() {
    var proxyBid = document.getElementById('proxy_bid');

    if (proxyBid.value == '') {
        proxyBid.value = document.getElementById('current_bid').value;
    }

    if (dollarsOnly == 'yes') {
        proxyBid.value = (Math.abs(proxyBid.value) + 1).toFixed(2);
    }
    else {
        proxyBid.value = (Math.abs(proxyBid.value) + .10).toFixed(2);
    }
}

function decrementNormalBet() {
    var currentBid = document.getElementById('current_bid');
    var actualBid = Math.abs(document.getElementById('actual_bid').innerHTML);

    if (dollarsOnly == 'yes') {
        if (Math.abs(currentBid.value) > actualBid + 1) {
            currentBid.value = (Math.abs(currentBid.value) - 1).toFixed(2);
        }
    }
    else {
        if (Math.abs(currentBid.value) > actualBid + 0.01) {
            currentBid.value = (Math.abs(currentBid.value) - 0.01).toFixed(2);
        }
    }
}

function decrementProxyBid() {
    var proxyBid = document.getElementById('proxy_bid');
    var actualBid = Math.abs(document.getElementById('actual_bid').innerHTML);

    if (dollarsOnly == 'yes') {
        if (Math.abs(proxyBid.value) > actualBid + 2) {
            proxyBid.value = (Math.abs(proxyBid.value) - 1).toFixed(2);
        }
    }
    else {
        if (Math.abs(proxyBid.value) > actualBid + 0.20) {
            proxyBid.value = (Math.abs(proxyBid.value) - 0.10).toFixed(2);
        }
    }
}

function sendNormalBet() {
    if (username == '') {
        return false;
    }
    sCurrentWinner = document.getElementById('current_winner').innerHTML;

    if (sCurrentWinner != username) {
    	document.getElementById('BtnMiseNormal').style.display = 'none';
    	document.getElementById('BtnMiseProxy').style.display = 'none';
    	
        iBet = Math.abs(document.getElementById('current_bid').value);
        iActualBid = Math.abs(document.getElementById('actual_bid').innerHTML);

        if (iBet > iActualBid) {
            jQuery.ajax({
                type: "POST",
                url: '/ajax_commands/send_auction_bet.php',
                data: 'fk_auctions=' + auctionID + '&bid_amount=' + iBet + '&random_number=' + (new Date()).getDate(),
                success: function (msg) {
                    json = eval("(" + msg + ")");
                    document.getElementById('actual_bid').innerHTML = json.actual_bid;
                    document.getElementById('current_winner').innerHTML = json.current_winner;

                    iActualBid = Math.abs(json.actual_bid);

                    if (dollarsOnly == 'yes') {
                        document.getElementById('current_bid').value = (iActualBid + 1).toFixed(2);
                    }
                    else {
                        document.getElementById('current_bid').value = (iActualBid + 0.01).toFixed(2);
                    }

                    if (json.remaining_time <= 50) {
                        clearInterval(refreshAuctionInterval);
                        remainingSeconds = json.remaining_time;
                        refreshAuctionInterval = setInterval('refreshAuctionDetails(' + auctionID + ')', 1000);
                    }
                    
                    
			    	document.getElementById('BtnMiseNormal').style.display = '';
			    	document.getElementById('BtnMiseProxy').style.display = '';
                }
            });
        }
    }
}

function sendProxyBet() {
	document.getElementById('BtnMiseNormal').style.display = 'none';
	document.getElementById('BtnMiseProxy').style.display = 'none';
	
    iBet = Math.abs(document.getElementById('proxy_bid').value);
	
    jQuery.ajax({
        type: 'POST',
        url: '/ajax_commands/send_proxy_bet.php',
        data: 'fk_auctions=' + auctionID + '&maximum_bid=' + iBet + '&random_number=' + (new Date()).getDate(),
        success: function (msg) {
            if (msg == 'OK') {
                document.getElementById('proxy_bid_sent').style.display = '';
                
		    	document.getElementById('BtnMiseNormal').style.display = 'none';
		    	document.getElementById('BtnMiseProxy').style.display = 'none';
            }
        }
    });
}

var javascriptCountdown = function () {
    var time_left = 10;
    var output_element_id = 'remaining_auction_time';
    var keep_counting = 1;
    var no_time_left_message = "L'enchere est terminee!";

    function countdown() {
        if (time_left < 2) {
            keep_counting = 0;
        }

        time_left = time_left - 1;
    }

    function add_leading_zero(n) {
        if (n.toString().length < 2) {
            return '0' + n;
        }
        else {
            return n;
        }
    }

    function format_output() {
        var days, hours, minutes, seconds;
        days = Math.floor(time_left / 86400);
        temporary_time = time_left - (days * 86400);


        seconds = temporary_time % 60;
        minutes = Math.floor(temporary_time / 60) % 60;
        hours = Math.floor(temporary_time / 3600);

        seconds = add_leading_zero(seconds);
        minutes = add_leading_zero(minutes);
        hours = add_leading_zero(hours);

        return days + ' jours ' + hours + 'h ' + minutes + 'm ' + seconds + 's';
    }

    function show_time_left() {
        document.getElementById(output_element_id).innerHTML = format_output();
    }

    function no_time_left() {
        document.getElementById(output_element_id).innerHTML = no_time_left_message;
        jQuery("#bidding_section").hide();
    }

    return {
        count: function () {
            countdown();
            show_time_left();
        },
        timer: function () {
            javascriptCountdown.count();

            if (keep_counting) {
                setTimeout("javascriptCountdown.timer();", 1000);

                if (time_left < 10) {
                    document.getElementById(output_element_id).style.backgroundColor = 'yellow';
                    document.getElementById(output_element_id).style.color = 'red';
                    document.getElementById(output_element_id).style.fontWeight = 'bold';
                }
            } else {
                no_time_left();
            }
        },
        init: function (t, element_id) {
            time_left = t;
            output_element_id = element_id;
            javascriptCountdown.timer();
        }
    };
} ();

if (document.location.href.indexOf('encheres.php') != -1) {
    for (a in remaining_times) {
        setInterval('formatAuctionCountdown(' + a + ')', 1000);
    }
}

function formatAuctionCountdown(iAuctionId) {
    var time_left = remaining_times[iAuctionId];
    var days, hours, minutes, seconds;
    days = Math.floor(time_left / 86400);
    temporary_time = time_left - (days * 86400);


    seconds = temporary_time % 60;
    minutes = Math.floor(temporary_time / 60) % 60;
    hours = Math.floor(temporary_time / 3600);

    if (seconds.toString().length < 2) {
        seconds = '0' + seconds;
    }

    if (minutes.toString().length < 2) {
        minutes = '0' + minutes;
    }

    if (hours.toString().length < 2) {
        hours = '0' + hours;
    }

    remaining_times[iAuctionId]--;

    document.getElementById('remaining_time_' + iAuctionId).innerHTML = days + ' jours ' + hours + 'h ' + minutes + 'm ' + seconds + 's';
}

if (document.getElementById('current_bid')) {
    document.getElementById('current_bid').onfocus = function () {
        jQuery.ajax({
            type: "GET",
            url: '/ajax_commands/get_next_auction_bid.php',
            data: 'pk_auctions=' + auctionID + '&random_number=' + (new Date()).getDate(),
            success: function (msg) {
                document.getElementById('current_bid').value = Math.abs(msg).toFixed(2);
            }
        });
    }

    document.getElementById('current_bid').onchange = function () {
        this.value = this.value.replace(',', '.');

        var actualBid = Math.abs(document.getElementById('actual_bid').innerHTML);

        if (Math.abs(this.value) > actualBid) {
            this.value = Math.abs(this.value).toFixed(2);
        }
        else {
            if (dollarsOnly == 'yes') {
                this.value = (Math.abs(actualBid + 1)).toFixed(2);
            }
            else {
                this.value = (Math.abs(actualBid + 0.01)).toFixed(2);
            }
        }
    }
}

if (document.getElementById('proxy_bid')) {
    document.getElementById('proxy_bid').onchange = function () {
        var actualBid = Math.abs(document.getElementById('actual_bid').innerHTML);

        if (Math.abs(this.value) > actualBid) {
            this.value = Math.abs(this.value).toFixed(2);
        }
        else {
            this.value = (Math.abs(actualBid + 0.10)).toFixed(2);
        }
    }
}
