﻿function _fresh(index) {
    var id = $("#proid_" + index).val();
    var date = $("#date_" + index).val();
    var go_price = $("#go_price_" + index).val();
    var ptype = $("#ptype_" + index).val();
    var endtime = new Date(date);
    var nowtime = new Date();
    var leftsecond = parseInt((endtime.getTime() - nowtime.getTime()) / 1000),
        _d = parseInt(leftsecond / 3600 / 24),
        _h = parseInt((leftsecond / 3600) % 24) + parseInt(_d * 24),
        _m = parseInt((leftsecond / 60) % 60),
        _s = parseInt(leftsecond % 60);
    if (nowtime >= endtime) {
        getDate(id, index, 2);
    }
    else {
        if (leftsecond <= 0) {
            leftsecond = 0;
        }
        else {
            if (leftsecond == 3) {
                getRand(id, index, 2, go_price, ptype);
            }
            else {
                getUserName(id, index, 0);
                getPrice(id, index, 1);
            }
            var hour = _h;
            var minn = _m;
            var second = _s;
            if (_h < 10)
                hour = "0" + _h;
            if (_m < 10)
                minn = "0" + _m;
            if (_s < 10)
                second = "0" + _s;
            $("#time_" + index).html(hour + ":" + minn + ":" + second);
            if (second < 10 && hour < 1 && minn < 1) {
                $("#time_" + index).css({ 'color': 'red' });
            }
            else {
                $("#time_" + index).css({ 'color': '#D06F22' });
            }
        }
    }
}

function getRand(id, index, flag, p_price, p_type) {
    $.post("/buy_add.aspx", { id: id, flag: 3, p_price: p_price, p_type: p_type }, function(data) {
        $("#member_" + index).html(getUserName(id, index, 0));
        $("#price_" + index).html(getPrice(id, index, 1));
        $("#date_" + index).html(data);
    });
}

function getUserName(id, index, flag) {
    $.post("/ajax.aspx", { id: id, flag: 0 }, function(data) {
        $("#member_" + index).html(data);
    });
}

function getPrice(id, index, flag) {
    $.post("/ajax.aspx", { id: id, flag: 1 }, function(data) {
        $("#price_" + index).html(data);
    });
}

function getDate(id, index, flag) {
    $.post("/ajax.aspx", { id: id, flag: 2 }, function(data) {
        document.getElementById("date_" + index).value = data;
        var date = data;
        var endtime = new Date(date);
        var nowtime = new Date();
        var leftsecond = parseInt((endtime.getTime() - nowtime.getTime()) / 1000);
        _d = parseInt(leftsecond / 3600 / 24);
        _h = parseInt((leftsecond / 3600) % 24) + parseInt(_d * 24);
        _m = parseInt((leftsecond / 60) % 60);
        _s = parseInt(leftsecond % 60);
        if (nowtime >= endtime) {
            getDate(id, index, 2);
        }
        else {
            if (leftsecond < 0) {
                leftsecond = 0;
            }
            else {
                var hour = _h;
                var min = _m;
                var second = _s;
                var id = $("#proid_" + index).val();
                var date = $("#date_" + index).val();
                var go_price = $("#go_price_" + index).val();
                var ptype = $("#ptype_" + index).val();
                if (leftsecond == 3) {
                    getRand(id, index, 2, go_price, ptype);
                }
                else {
                    getUserName(id, index, 0);
                    getPrice(id, index, 1);
                }
                if (_h < 10)
                    hour = "0" + _h;
                if (_m < 10)
                    min = "0" + _m;
                if (_s < 10)
                    second = "0" + _s;
                $("#time_" + index).html(hour + ":" + min + ":" + second);
                if (second < 4 && minn < 1 && hour < 1) {
                    $("#time_" + index).css({ "color": "red" });
                }
                else {
                    $("#time_" + index).css({ "color": "#D06F22" });
                }
            }
        }
    });
}

function buy_add(id, p_type, buy_type, p_price, index) {
    $.post("/buy_add.aspx", { id: id, p_type: p_type, p_price: p_price, buy_type: buy_type }, function(data) {
        var array = data.toString().split("|");
        alert(array[0]);
        if (array[1] == "login") {
            location.href = '/Login_Reg.aspx';
        }
        else if (array[0] == "保价购买成功！") {
            location.href = '/user/My_OrderList.aspx?ordertype=2';
        }
        else if (array[1] == "error") {

        }
        else {
            document.getElementById("date_" + index).value = array[1];
        }
    });
}
