﻿


///商品收藏删除
function DelCollect(c_id) {
    art.dialog({
        content: '确定删除?',
        ok: function () {
            var outs = "type=delcollect&c_id=" + c_id;
            $.ajax({
                type: "POST",
                url: "/PostData/Ajaxdata.aspx",
                data: outs,
                beforeSend: function (XMLHttpRequest) {
                    art.dialog({
                        title: 'Loading...'
                    }).lock();
                },
                success: function (msg) {
                    if (msg == "1") {

                        var list = art.dialog.list;
                        for (var i in list) {
                            list[i].close();
                        };
                        art.dialog({
                            content: '删除成功!',
                            ok: function () {
                                reloadproductcollectpage();

                            }
                        }).lock();
                    }
                    else {
                        alert(msg);
                    }
                }
            });
            return false;
        },
        cancelVal: '关闭',
        cancel: true
    });

}


function collectproduct(proid) {//商品以往记录
    var outs = "type=collectproduct&pid=" + proid;
    $.ajax({
        type: "POST",
        url: "/PostData/Ajaxdata.aspx",
        data: outs,
        beforeSend: function (XMLHttpRequest) {
            art.dialog({
                title: 'Loading...'
            }).lock();
        },
        success: function (msg) {
            if (msg = 'True') {
                var list = art.dialog.list;
                for (var i in list) {
                    list[i].close();
                };
                art.dialog({
                    content: '商品收藏成功?',
                    ok: function () {

                        return true;
                    }
                });
            }
            else if (msg = '0')//no session
            {
                var url = '/Product.aspx?pid=' + proid;
                location.href = '/member.aspx?url=' + escape(url);
            }
            //alert(msg);
        }
    });

}


function viewproduct(proid) {//商品以往记录
    var outs = "type=viewproduct&pid=" + proid;
    $.ajax({
        type: "POST",
        url: "/PostData/Ajaxdata.aspx",
        data: outs,
        beforeSend: function (XMLHttpRequest) {

        },
        success: function (msg) {
            //alert(msg);
        }
    });

}


function loginout() { //登出
    var outs = "type=loginout";
    //alert(outs);
    $.ajax({
        type: "POST",
        url: "/PostData/Ajaxdata.aspx",
        data: outs,
        beforeSend: function (XMLHttpRequest) {

        },
        success: function (msg) {
            if (msg == "1") {
                location.href = '/Default.aspx';
            }

        }
    });
}


function payorder(o_id) {//支付订单
    location.href = '/Shoppingpay.aspx?oid=' + o_id;
}
function editorder(str_order_id) {//修改订单
    var s_value = str_order_id.split('*');
    var order_id = s_value[0];
    var o_id = s_value[1];
    location.href = '/Shoppingorder.aspx?type=edit' + '&oid=' + o_id + '&orders=' + order_id;
}
function cancelorder(o_id) {//取消订单
    alert(o_id);
}
function vieworder(o_id) {//浏览订单
    alert(o_id);
}

//订单信息初始化(订单确认页面 Shoppingorder.aspx)
function orderinit(o_id) {
    var outs = "type=initorders&o_id=" + o_id;
    $.ajax({
        type: "POST",
        url: "/PostData/Ajaxdata.aspx",
        data: outs,
        beforeSend: function (XMLHttpRequest) {
            art.dialog({
                title: 'Loading...'
            }).lock();
        },
        success: function (msg) {
            var sp_msg = msg.split('&');
            var province = "";
            var city = "";
            var area = "";
            document.getElementById("op").value = "edit";
            document.getElementById("oid").value = o_id;
            for (var k = 0; k < sp_msg.length; k++) {
                var sp_s = sp_msg[k].split('=');
                if (sp_s[0] == "province") {
                    province = sp_s[1];
                }
                else if (sp_s[0] == "city") {
                    city = sp_s[1];
                }
                else if (sp_s[0] == "area") {
                    area = sp_s[1];
                }
                else if (sp_s[0] == "expressid") {
                    var i = Number(sp_s[1]) - 1;

                    $("input[name=checkexpress]:eq(" + i + ")").attr("checked", 'checked');
                }
                else {
                    document.getElementById(sp_s[0]).value = sp_s[1];
                }

            }
            new PCAS("province", "city", "area", province, city, area);
            GetP_C();
            var list = art.dialog.list;
            for (var i in list) {
                list[i].close();
            };
        }
    });
}

//获取价格详细信息(订单确认页面 Shoppingorder.aspx)
function GetP_C() {
    $("#input_all_orders").val(getArgs('orders')); //隐藏域
    var x = 0;
    var y = 0;
    $('[name="priceall"]').each(function () {
        x += parseFloat($(this).html());
    });

    $("#priceall_view").html(x.toFixed(2));

    $('[name="countall"]').each(function () {
        if (!isNaN(parseInt($(this).html()))) {

            y += parseInt($(this).html());
        }
    });
    //商品总数量 y
    $("#countall_view").html(y);
    //商品总价 x
    $("#product_postprice").html(x);
    $("#input_product_postprice").val(x); //隐藏域

    GetTotalPrice(x);

    $('[name="checkexpress"]').click(function () {
        GetTotalPrice(x);
    });

}
//获取总价(订单确认页面 Shoppingorder.aspx)
function GetTotalPrice(x) {
    var value = $("input[name='checkexpress']:checked").val();
    var sp_value = value.split('|');
    //获取快递费用 sp_value[1]
    $("#express_postprice").html(sp_value[1]);
    $("#input_express_postprice").val(sp_value[1]); //隐藏域
    //计算总价
    var all = x + parseFloat(sp_value[1]);

    $("#all_postprice").html(all.toFixed(2));
    $("#input_all_postprice").val(all.toFixed(2)); //隐藏域

}


//获取订单ID(购物车页面 account_carlist.aspx)
function goorder(url, hide_checked) {
    var checked = document.getElementById(hide_checked).innerHTML;
    if (checked != "") {
        url += "?type=add&orders=" + checked;
        location.href = url;
    }
    else {//未选择 
        art.dialog({
            content: '未选择购物产品',
            ok: function () {
                return true;
            }
        });
    }
}



//地址选择（单确认页面 Shoppingorder.aspx）
function changeaddress() {
    var address_id = document.getElementById('addressget').value;
    //alert(address_id);
    if (address_id != 0) {
        var outs = "type=addressget&address_id=" + address_id;
        $.ajax({
            type: "POST",
            url: "/PostData/Ajaxdata.aspx",
            data: outs,
            beforeSend: function (XMLHttpRequest) {
                art.dialog({
                    title: 'Loading...'
                }).lock();
            },
            success: function (msg) {
                var sp_msg = msg.split('&');
                var province = "";
                var city = "";
                var area = "";
                for (var k = 0; k < sp_msg.length; k++) {
                    var sp_s = sp_msg[k].split('=');
                    if (sp_s[0] == "province") {
                        province = sp_s[1];
                    }
                    else if (sp_s[0] == "city") {
                        city = sp_s[1];
                    }
                    else if (sp_s[0] == "area") {
                        area = sp_s[1];
                    }
                    else {
                        document.getElementById(sp_s[0]).value = sp_s[1];
                    }

                }
                new PCAS("province", "city", "area", province, city, area);

                var list = art.dialog.list;
                for (var i in list) {
                    list[i].close();
                };
            }
        });
    }

}


function onchecked(tableid, dv_check) {

    var table = "tb" + tableid;
    if (document.getElementById(table).style.background == "#ccc") {
        document.getElementById(table).style.background = "";
        var reps = tableid + ',';
        document.getElementById("hide_checked").innerHTML = document.getElementById("hide_checked").innerHTML.replace(reps, '');
    }
    else {
        document.getElementById(table).style.background = "#ccc";
        var checkedhtml = document.getElementById("hide_checked").innerHTML;
        checkedhtml += tableid + ",";
        document.getElementById("hide_checked").innerHTML = checkedhtml;
    }


}


function cargetall() {
    foundElements = getElementByAttribute('tgname', 'carlistcount', document.body);
    foundElement_prices = getElementByAttribute('tgname', 'carlistprice', document.body);
    var count = 0;
    var priceall = 0.00;
    for (var i = 0; i < foundElements.length; i++) {
        var id = foundElements[i].id;
        var price = foundElement_prices[i].id;
        price_value = document.getElementById(price).innerHTML;
        count_value = document.getElementById(id).value;
        priceall += parseFloat(parseFloat(price_value * count_value).toFixed(2));
        count += Number(count_value);

    }
    document.getElementById('count_all').innerHTML = count;
    document.getElementById('price_all').innerHTML = parseFloat(priceall).toFixed(2);

}
function del_all(ajaxpanel, type) {
    art.dialog({
        content: '确定删除?',
        ok: function () {
            car_ajax('0', '0', ajaxpanel, '1', type)
            return false;
        },
        cancelVal: '关闭',
        cancel: true
    });

}
function get_checked() {
    var selectedvalue = document.getElementById("hide_checked").innerHTML;
    //alert(selectedvalue);
    var s_selected = selectedvalue.split(',');
    if (s_selected != "") {
        for (var i = 0; i < s_selected.length - 1; i++) {
            //alert("tb" + s_selected[i]);
            document.getElementById("tb" + s_selected[i]).style.background = "#ccc";
            document.getElementById(s_selected[i]).checked = true;
        }
    }
}

function car_ajax(i, count, ajaxpanel, delall, type) {
   
    var outs = 'cid=' + i + "&count=" + count + "&delall=" + delall + "&type=" + type;
    // alert(outs);
    $.ajax({
        type: "POST",
        url: "/PostData/Caredit.aspx",
        data: outs,
        beforeSend: function (XMLHttpRequest) {
            art.dialog({
                title: 'Loading...'
            }).lock();
            //alert("waiting...");
            //document.getElementById(msgshow).innerHTML = '加载中...';
        },
        success: function (msg) {
          
            if (msg == "1" && type == "1") {

                AjaxPanel(document.getElementById('hide' + ajaxpanel).innerHTML, ajaxpanel, type);
                var list = art.dialog.list;
                for (var i in list) {
                    list[i].close();
                };

            }
            else if (type == "2") {
                // alert(msg);
               
                AjaxPanel(document.getElementById('hide' + ajaxpanel).innerHTML, ajaxpanel, type);

                var list = art.dialog.list;
                for (var i in list) {
                    list[i].close();
                };
            }
        }
    });
}

function caraddone(i, inv, price, ajaxpanel, type) {
    var input = 'car' + i;
    var priceall = 'priceall' + i;
    var allcount = 'count_all';
    var allprice = 'price_all';
    var count = Number(document.getElementById(input).value);
    if (count < Number(inv)) {
        count++;
        document.getElementById(input).value = count;
    }
    else {
        count = inv;
        document.getElementById(input).value = inv;
    }
   
    car_ajax(i, count, ajaxpanel, 0, type); //修改cookies或者修改数据库

}

function AjaxPanel(innerhtml, ajaxpanel, type) {

    var outs = "innerhtmls=" + escape(innerhtml);

    $.ajax({
        type: "POST",
        url: "/PostData/Ajax_panel.aspx",
        data: outs,
        beforeSend: function (XMLHttpRequest) {
            document.getElementById(ajaxpanel).innerHTML = "";
        },
        success: function (msg) {

            document.getElementById(ajaxpanel).innerHTML = msg;
          
            if (type == "1") {//前台购物车 COOKIES
                cargetall();
            }
            else {
                get_checked();
            }
        }
    });
}

function cardelone(i, inv, price, ajaxpanel, type) {
    var input = 'car' + i;
    var priceall = 'priceall' + i;
    var allcount = 'count_all';
    var allprice = 'price_all';
    var count = Number(document.getElementById(input).value);
    if (count > 0) {
        count--;
    }
    car_ajax(i, count, ajaxpanel, 0, type);
}
function caronchange(i, inv, price, ajaxpanel, a_count, type) {

    var input = 'car' + i;
    var priceall = 'priceall' + i;
    var allcount = 'count_all';
    var allprice = 'price_all';

    var current = Number(document.getElementById(input).value);

    var count = current;
    if (count > inv) {

        count = inv;

    }
    else
        if (count < 0) {
            count = 0;
        }
    if (a_count == 0) {
        count = 0;
    }
    if (count == 0) {
        art.dialog({
            content: '确定删除?',
            ok: function () {
                car_ajax(i, count, ajaxpanel, 0, type);
                return false;
            },
            cancelVal: '关闭',
            cancel: true
        });
    }
    else {
        car_ajax(i, count, ajaxpanel, 0, type);
    }
}

function addtoCart(good_num, good_color, proid, msgshow) {
    var outs = "proid=" + getArgs(proid);
    outs += "&good_num=" + document.getElementById(good_num).value;
    outs += "&good_color=" + document.getElementById(good_color).innerHTML;
    //alert(outs);

    $.ajax({
        type: "POST",
        url: "/PostData/AddtoCart.aspx",
        data: outs,
        beforeSend: function (XMLHttpRequest) {
            open_cart();
            document.getElementById(msgshow).innerHTML = '加载中...';
        },
        success: function (msg) {
            if (msg != '0') {

                var smsg = msg.split('|');
                var oups = '<h3>宝贝已成功添加到购物车！</h3><div>购物车共有<span id=goods_num>' + smsg[0] + '</span>种商品';
                oups += '合计：<span id=goods_sprice>￥' + smsg[1] + '</span></div>';
                oups += '<br>';
                oups += '<input type=image src=/Images/Front/icon/btn_ctu.gif onclick=javascript:close_cart(); />';
                oups += '<input type=image src=/Images/Front/icon/btn_pay.gif onclick=javascript:goshoppinglist(); />';
            }
            else {
                var oups = 'Wrong';
                oups += '合计：<span id=goods_sprice></span></div>';
            }
            document.getElementById(msgshow).innerHTML = oups;
        }
    });
}
function goshoppinglist() {
    var outs = "type=goorder";
    $.ajax({
        type: "POST",
        url: "/PostData/Ajaxdata.aspx",
        data: outs,
        beforeSend: function (XMLHttpRequest) {

        },
        success: function (msg) {
            location.href = '/account_carlist.aspx';

        }
    });


}
function close_cart() {
    document.getElementById('cartSureBox').style.display = "none";
}

function open_cart() {
    document.getElementById('cartSureBox').style.display = 'block';
}

function getArgs(strParame) {
    var args = new Object();
    var query = location.search.substring(1); // Get query string
    var pairs = query.split("&"); // Break at ampersand

    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('='); // Look for "name=value"
        if (pos == -1) continue; // If not found, skip
        var argname = pairs[i].substring(0, pos); // Extract the name
        var value = pairs[i].substring(pos + 1); // Extract the value
        value = value; // Decode it, if needed
        args[argname] = value; // Store as a property
    }
    return args[strParame]; // Return the object
}

function getElementByAttribute(aAttribute, aValue, abody) {
    var Elements = [],
            sfunc = 'return Element.' + aAttribute + '=="' + aValue + '"?true:false;';
    SearchElement(!!abody ? abody : document.body);
    return Elements;
    function ElementVerifier(ele) {
        Element = ele;
        if (Element.nodeName == '#text') return false;
        return eval('Element.' + aAttribute + '=="' + aValue + '"?true:false;');
    }
    function SearchElement(ele) {
        if (!ele) return;
        if (ElementVerifier(ele)) Elements.push(ele);
        SearchElement(ele.firstChild);
        SearchElement(ele.nextSibling);
    }

}

//Tabs
function tabChange(e, sindex, delay) {
    if (!e) return;
    if (!delay) { var thisdelay = window.setTimeout(function () { tabChange(e, sindex, thisdelay); }, 300); e.onmouseout = function (e) { window.clearTimeout(thisdelay); }; return; }
    if (e.onmouseout != null) e.onmouseout = null;
    var es = e.parentNode.childNodes; var cindex = 0;
    for (var i = 0; i < es.length; i++) {
        if (es[i].nodeType != 1) continue;
        if (es[i] == e) { cindex = i; if (es[i].className.indexOf('_hover') < 0) es[i].className = es[i].className + '_hover'; }
        else es[i].className = es[i].className.replace('_hover', '');
    }
    if (sindex) { cindex -= sindex; if (cindex < 0) cindex = 0; }
    var c = siblingNext(e.parentNode); if (!c || !c.childNodes || c.childNodes.length <= 0) return;
    var vcount = -1;
    for (var i = 0; i < c.childNodes.length; i++) {
        if (c.childNodes[i].nodeType != 1) continue; vcount++;

        c.childNodes[i].style.display = (vcount == cindex ? '' : 'none');
    }
}

function siblingPrevious(ele) {
    if (ele == null || typeof ele.previousSibling == 'undefined') return null;
    while (ele.previousSibling != null && ele.previousSibling.nodeType != 1) ele = ele.previousSibling;
    return ele.previousSibling;
}
function siblingNext(ele) {
    if (ele == null || typeof ele.nextSibling == 'undefined') return null;
    while (ele.nextSibling != null && ele.nextSibling.nodeType != 1) ele = ele.nextSibling;
    return ele.nextSibling;
}

//加入收藏
function AddFavorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

//获取Query String 
function getArgs(strParame) {
    var args = new Object();
    var query = location.search.substring(1); // Get query string
    var pairs = query.split("&"); // Break at ampersand

    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('='); // Look for "name=value"
        if (pos == -1) continue; // If not found, skip
        var argname = pairs[i].substring(0, pos); // Extract the name
        var value = pairs[i].substring(pos + 1); // Extract the value
        value = value; // Decode it, if needed
        args[argname] = value; // Store as a property
    }
    return args[strParame]; // Return the object
}

//获取分割值
function getsplit(str, i) {
    var s_str = str.split('&|&');
    return s_str[i];
}

//转义
function filter(str) {
    str = str.replace(/&gt;/g, '>');
    str = str.replace(/&lt;/g, '<');
    str = str.replace(/&amp;/g, '&');
    str = str.replace(/&quot;/g, '"');
    str = str.replace(/&#58;/g, ':');
    str = str.replace(/&#39;/g, '\'');
    return str;
}


function get_rate(rate) {
    rate = rate.toString();
    var s;
    var g;
    $("#g").show();
    if (rate.length >= 3) {
        s = 10;
        g = 0;
        $("#g").hide();
    } else if (rate == "0") {
        s = 0;
        g = 0;
    } else {
        s = rate.substr(0, 1);
        g = rate.substr(1, 1);
    }
    $("#s").text(s);
    $("#g").text("." + g);
    $(".big_rate_up").animate({ width: (parseInt(s) + parseInt(g) / 10) * 14, height: 26 }, 1000);
    $(".big_rate_bak b").each(function () {
        $(this).mouseover(function () {
            $(".big_rate_up").width($(this).attr("rate") * 14);
            $("#s").text($(this).attr("rate"));
            $("#g").text("");
        }).click(function () {
            $("#f").text($(this).attr("rate"));
            $("#my_rate").show();
        })
    })
    $(".big_rate_bak").mouseout(function () {
        $("#s").text(s);
        $("#g").text("." + g);
        $(".big_rate_up").width((parseInt(s) + parseInt(g) / 10) * 14);
    })
}

function up_rate(rate) {
    $(".big_rate_up").width("0");
    get_rate(rate);
}

