﻿
function selectedCountry(country)  //配送的方法
{

    $.getJSON("/OrderInfo/GetIsCodbyCode", { countryCode: country, aa: new Date().getTime() }, function(data) {

        var htmlLi = "";


        for (var i = 0; i < data.length; i++) {//循环添加到list

            if (data.length != 0) {


                htmlLi += "<li><span class=\"span1\">" + data[i].code + "</span><span  class=\"span2\">" + data[i].price + "元</span><span class=\"span1\">" + data[i].arrive_time + "天</span>";



                $("#commonInfo").html(htmlLi + "<li><p><span class=\"red\">提示：</span>该用费只适用于当前商品，购买多个商品以最终下单为准。</p></li>");




            } else {

                $("#commonInfo").html("");
            }
        }
    });
}



function GetDeliveryMethods() {



    var str = "";
    var countrycode = $('#inp_country_code').val(); //取得地区编号
    if (isNaN(countrycode) || countrycode == "") {
        return;
    }
    var paytype = $('#paytype option:selected').val();
    if (isNaN(paytype) || paytype <= 0)
        return;

    $.ajax({
        url: "/OrderInfo/GetDeliveryByRegion",
        type: "post",
        dataType: "json",
        data: "countryCode=" + countrycode + "&paytype=" + paytype + "&money=" + $("#addPrice").text() + "&timeStamp=" + new Date().getTime(),
        success: function(data) {

            $.each(data, function(i, node) {
                str += "<option value=\"" + node.dmId + "\">" + node.deliveryName + '￥' + node.cost + "</option>"

            });


            $('#dcMethod').html(str);


        }
    });

}
///根据地区代码获取是否支持货到付款
function GetCost(countrycode) {
    $('#inp_country_code').val(countrycode);
    $.ajax({
        url: "/OrderInfo/GetIsCODByCountyCode",
        type: "post",
        dataType: "json",
        data: "countryCode=" + countrycode + "&timeStamp=" + new Date().getTime(),
        success: function(data) {


            if (data.returnvalue == 1) {


                if (data.isPor == 1) {

                    document.getElementById("spn_isPor").style.display = "inline";

                    $('#paytype').html("<option value=\"23\">在线支付</option><option value=\"18\">货到付款</option><option value=\"19\">银行转帐</option><option value=\"20\">邮局汇款</option>");



                }
                else {


                    document.getElementById("spn_isPor").style.display = "none";
                    $('#paytype').html("<option value=\"23\">在线支付</option><option value=\"19\">银行转帐</option><option value=\"20\">邮局汇款</option>");


                }
            }
            else {
                document.getElementById("spn_isPor").style.display = "none";
                $('#paytype').html("<option value=\"23\">在线支付</option><option value=\"19\">银行转帐</option><option value=\"20\">邮局汇款</option>");


            }
        }
    });

    GetDeliveryMethods(); //配送方式

}

function commentPage(currentlyPage, marks) {  //评论分页方法

    $.get("/Product/ProductCommentInfo?bb=" + new Date().getTime(), { currentlyPage: currentlyPage, P_id: p_id, marks: marks }, function(data) {
        if ($("#ConsultationIsNull").val() == "") {  //在评论页的点击分页
            $("#commentShow").html(data);
        } else {//首页
            $("#commentShowProudct").html(data);
            $("#commentShow").html(data);
        }


    });

}

function consultationPage(currentlyPage) {  //咨询分页方法

    $.get("/Product/ProductConsultationInfo?aa=" + new Date().getTime(), { currentlyPage: currentlyPage, P_id: p_id }, function(data) {

        if ($("#ConsultationIsNull").val() == "") {

            $("#consultationShow").html(data);
        } else {

            $("#showConsultation").html(data);
            $("#consultationProudct").html(data);

        }


    });

}



function consultationPage1(currentlyPage) {  //咨询选项卡分页方法

    $.get("/Product/ProductConsultationInfo?aa=" + new Date().getTime(), { currentlyPage: currentlyPage, P_id: p_id }, function(data) {

        $("#consultationProudct").html("");

        $("#showConsultation").html(data);



    });

}

function consultationPage2(currentlyPage) {  //咨询选项卡分页方法

    $.get("/Product/ProductConsultationInfo?aa=" + new Date().getTime(), { currentlyPage: currentlyPage, P_id: p_id }, function(data) {
        $("#showConsultation").html("");

        $("#consultationProudct").html(data);




    });

}





function Add_Assembled_PCart(assembled_pId) {  //组合产品 
    var pidSping = assembled_pId.split("|");
    for (var i = 0; i < pidSping.length; i++) {
        var splintPid = pidSping[i];

        var hg_integral = "";
        var totalweight = "";
        $.get("/Product/assembled?cc=" + new Date().getTime(), { pid: splintPid }, function(data) {

            var info1 = data;

            var info = info1.split("|");


            hg_integral = info[0];

            totalweight = info[1];
            AddToCart(splintPid, hg_integral, "false", "", "1", "0", totalweight);

        });

    }
    tocart();

}


//添加到购物车的方法
function AddToCart(p_id, hg_integral, isredirect, focus, barcode, isexp, weight) {

    var quantity = $("#inp2").val();  //获取商品数量

    var hand_in_val = $('#hand_in option:selected').val();
    if (typeof (hand_in_val) != 'undefined' && hand_in_val == '-1') {
        alert('请选择手寸!'); return;
    } if (typeof (hand_in_val) == 'undefined') { hand_in_val = ''; }
    var engravingrings = $('#engravingrings').val(); remarks = typeof (engravingrings) == 'undefined' ? '' : engravingrings;

    UpdateCart(p_id, quantity, hg_integral, url, isredirect, "", carturl, barcode, isexp, loginurl, mainurl, weight, hand_in_val, remarks);


}




function countAdd(add) {//更改数量的方法

    var count = $("#productCount").val();

    var addprice = parseFloat($("#addPrice").html());
    var addprice1 = parseFloat($("#addPrice1").html());


    if (add == 2) {

        count++;
        $("#productCount").val(count);

        $("#addPrice").html(parseFloat((addprice) + parseFloat(price)).toFixed(2));

        $("#addPrice1").html(parseFloat((addprice1) + parseFloat(price)).toFixed(2));
    }
    if (add == 1) {
        count--;
        if (count < 1) {
            $("#productCount").val(1);
        } else {



            $("#productCount").val(count);
            $("#addPrice").html(parseFloat(parseFloat(addprice) - parseFloat(price)).toFixed(2));

            $("#addPrice1").html(parseFloat(parseFloat(addprice1) - parseFloat(price)).toFixed(2));
        }
    }

    if (add == 3) {

        $("#addPrice").html(parseFloat(count * parseFloat(price)).toFixed(2));

        $("#addPrice1").html(parseFloat(count * parseFloat(price)).toFixed(2));
    }

};

function showWhatShopping() {

    $.get("/Product/whatShopping?aa=" + new Date().getTime(), function(data) {
        $("#whatShopping").html(data);
    });

}
function showLeasebackServer() {

    $.get("/Product/leasebackServer?aa=" + new Date().getTime(), function(data) {
        $("#leasebackServer").html(data);
    });

}
function showLeasebackPrivate() {

    $.get("/Product/leasebackPrivate?aa=" + new Date().getTime(), function(data) {
        $("#leasebackPrivate").html(data);
    });

}

function AllClass() {

    $.get("/Product/AllClass?aa=" + new Date().getTime(), function(data) {
        $("#xianshi12").html(data);
    });

}

function consultationSubmit() {  //点击咨询的方法



    var content = $('#comcontent').val();

    if (content.length < 5) {

        alert('咨询的内容不能少于5个字符!');
        return false;

    } else if (content.length > 140) {

        alert("咨询的内容不能多余140个字!");
        return false;

    } else {

        var jsondata = { "p_id": p_id, "content": "" + encodeURIComponent(content) };

        $.ajax({ type: 'POST', beforeSend: function() { },
            url: '/ProductConsultation/Create?timestamp=' + new Date().getTime(),
            data: jsondata, dataType: 'json', error: '', success:
function(data) {

    if (!data.online) {
        if ($("#ConsultationIsNull").val() == "") {

            showUserLogin("Consultation_Id");

        } else {
            if (document.getElementById("consultationShow").style.display == "block") {

                showUserLogin("Consultation_Id");

            } else {
                showUserLogin("celerityBuy");
            }
        }


    } else {

        alert(data.msg);  //返回成功信息
        $('#comcontent').val("");
        consultationPage(1); //重新刷新

    }

}
        }
);
        return true;
    }
    return true;
}









function SaveQuickBuy(isLogin) { //快捷购物提交

    try {



        if ($("#productCount").val() == "0") {
            alert("商品数量不能为0");
            return false;
        }

        if ($('#a_province option:selected').attr('innerHTML') == '--省份--') {
            alert("请选择省");
            return false;
        }
        else if ($('#a_city option:selected').attr('innerHTML') == undefined || $('#a_city option:selected').attr('innerHTML') == '--地级市--') {

            alert("请选择所在市");
            return false;
        }
        else if ($('#a_county option:selected').attr('innerHTML') == undefined || $('#a_county option:selected').attr('innerHTML') == '--区、县--') {

            alert("请选择所在地区");
            return false;
        }
        if ($('#receive_address').val() == '') {
            alert("请填写详细地址");
            return false;
        }
        if ($('#receive_user').val() == '') {
            alert("请填写收件人姓名");
            return false;
        }

        if ($('#receive_mobile').val() == '') {
            alert("联系电话必填");
            return false;
        }
        else if (!isMobil($('#receive_mobile').val())) {
            alert("请填写正确的手机号码");
            return false;
        }
        if ($('#dcMethod option:selected').val() == null || $('#dcMethod option:selected').val() == "") {

            alert("请选择配送方式");
            return false;

        }
        if ($('#inp_email').val() != "") {
            if (!isEmail($('#inp_email').val())) {

                alert("你输入的邮箱不正确!");
                return false;
            }

        }


    }
    catch (e) {
        return false;
    }

    var userName = $('#receive_user').val();
    var province = $('#a_province option:selected').val() + "," + $('#a_province option:selected').attr('innerHTML');
    var city = $('#a_city option:selected').val() + "," + $('#a_city option:selected').attr('innerHTML');
    var county = $('#a_county option:selected').val() + "," + $('#a_county option:selected').attr('innerHTML');
    var address = $('#receive_address').val();
    //var mobile = $('#e_mobile').val();
    var phone = $('#receive_mobile').val();
    var zip_code = $('#code').val(); //邮政编码
    var rid = $('#receive_id').val();
    var email = $('#inp_email').val();
    var dcid = $('#dcMethod option:selected').val(); //配送方式
    var paytype = $("#paytype option:selected").val(); //支付方式
    var remark = document.getElementById("remark").value; //订单备注


    var productCount = $("#productCount").val(); //产品数量


    window.location = carturl + "/OrderInfo/CelerityShopping?userName=" + userName + "&province=" + province + "&city=" + city + "&address=" + address + "&zip_code=" + zip_code + "&pid=" + p_id + "&productCount=" + productCount + "&paytype=" + paytype + "&dcid=" + dcid + "&phone=" + phone + "&county=" + county + "&email=" + email + "&remark=" + remark;
  

}


function newAddrGetCost(code) {
    //添加邮政编码

    $("#code").val(code);
}

function isEmail(s) {  //验证邮箱是否正确 
    var patrn = new RegExp("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$");
    if (!patrn.test(s)) { return false }
    return true
}

function isMobil(s) {  //验证是否是手机号

    var patrn = new RegExp("1[0-9]{10}$");
    if (!patrn.test(s)) {

        return false
    }


    return true
}
function webchat_chkkeysend(event) {  //按回车键登陆
    if (event.keyCode == 13) {

        LoginByScript("");

    }

}

function LoginByScript(loginSite) {

    var username = $.trim($("#inp_userName").val());
    var password = $.trim($("#inp_passWord").val());

    var Name = new RegExp("^[\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w]+$");
    var pwd = new RegExp("^[0-9a-zA-z_]{6,20}$");

    //用户名为空
    if (username == "" || username == null || username == "邮箱/手机号/昵称") {

        $("#denglutishi").css("color", "red");
        $("#denglutishi").html("请输入Email/手机号/昵称");
        return false;
    } else {
        //判断是Email还是TelPhone
        if (isEmail(username)) {
            //手机登录
            if (!isMobil(username)) {
                if (!Name.test(username)) {

                    $("#denglutishi").css("color", "red");
                    $("#denglutishi").html("你输入的用户名不正确.");
                    return false;
                }
                else {

                    $("#denglutishi").css("color", "#399");
                    $("#denglutishi").html("用户名格式正确");


                }

            } else {
                $("#denglutishi").css("display", "block");
                $("#denglutishi").css("color", "#399");
                $("#denglutishi").html("手机号码格式正确");

            }


        }


        else {
            //邮箱登录
            if (!isEmail(username)) {

                if (!Name.test(username)) {

                    $("#denglutishi").css("color", "red");
                    $("#denglutishi").html("你输入的用户名不正确.");
                    return false;
                }
                else {

                    $("#denglutishi").css("color", "#399");
                    $("#denglutishi").html("用户名格式正确");
                }
            }
            else {


                $("#denglutishi").css("display", "block");
                $("#denglutishi").css("color", "#399");
                $("#denglutishi").html("Email 格式正确");

            }


        }

    }
    if (password == "" || password == null || !pwd.test(password)) {


        $("#tishi").css("color", "red");
        $("#tishi").html("密码输入有误，请输入正确的密码");

        return false;
    }
    else {
        $("#tishi").css("color", "#399");
        $("#tishi").html("密码格式正确");


    }

    $.ajax({
        type: 'POST',
        beforeSend: function() { },
        url: '/OrderInfo/LoginByScript',
        data: "UserName=" + $('#inp_userName').val() + "&autoLogin=" + document.getElementById('isAutoLoginNext').checked +
                 "&Password=" + $('#inp_passWord').val() + "&timeStamp=" + new Date().getTime(),
        dataType: 'json',
        error: '',
        success: function(data, textstate) {
            $('#inp_passWord').val("");
            if (data.returnValue == '1') {
                $("#isNoLogin_Product").val("11");
                ClosePopWin();

            }
            else {
                $("#loginError").css("color", "red");
                $("#loginError").html(data.Msg);
            }

        }
    });
}

function ClosePopWin() //隐藏登陆窗口
{
    document.getElementById("minbg").style.display = "none";
    document.getElementById("login_mask").style.display = "none";
    document.getElementById("denglu2").style.display = "none";

}

function centerObj(o) {//遮照的显示

    $(o).css("left", (document.documentElement.clientWidth / 2 - (parseInt($(o).width()) / 2) + document.documentElement.scrollLeft) + "px");
    $(o).css("top", ((document.documentElement.clientHeight) / 2 - (parseInt($(o).height()) / 2) + document.documentElement.scrollTop) + "px");
}

function onLoginName() {

    var userName = $("#inp_userName").val();

    var Name = new RegExp("^[\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w]+$");


    //用户名为空
    if (userName == "" || userName == null || userName == "邮箱/手机号/昵称") {

        $("#denglutishi").css("color", "red");
        $("#denglutishi").html("请输入Email/手机号/昵称");
        return false;
    } else {
        //判断是Email还是TelPhone
        if (isEmail(userName)) {
            //手机登录
            if (!isMobil(userName)) {
                if (!Name.test(userName)) {

                    $("#denglutishi").css("color", "red");
                    $("#denglutishi").html("你输入的用户名不正确.");
                    return false;
                }
                else {

                    $("#denglutishi").css("color", "#399");
                    $("#denglutishi").html("用户名格式正确");


                }

            } else {
                $("#denglutishi").css("display", "block");
                $("#denglutishi").css("color", "#399");
                $("#denglutishi").html("手机号码格式正确");

            }


        }


        else {
            //邮箱登录
            if (!isEmail(userName)) {

                if (!Name.test(userName)) {

                    $("#denglutishi").css("color", "red");
                    $("#denglutishi").html("你输入的用户名不正确.");
                    return false;
                }
                else {

                    $("#denglutishi").css("color", "#399");
                    $("#denglutishi").html("用户名格式正确");
                }
            }
            else {


                $("#denglutishi").css("display", "block");
                $("#denglutishi").css("color", "#399");
                $("#denglutishi").html("Email 格式正确");

            }


        }





    }

}


function onblurLoginPwd() { //失去焦点

    var userPwd = $("#inp_passWord").val();
    var pwd = new RegExp("^[0-9a-zA-z_]");

    if (userPwd == "" || userPwd == null || !pwd.test(userPwd)) {


        $("#tishi").css("color", "red");
        $("#tishi").html("密码输入有误，请输入正确的密码");

        return false;
    }
    else {
        $("#tishi").css("color", "#399");
        $("#tishi").html("密码格式正确");
    }
}
function onclickLoginPwd() {

    var userPwd = $("#inp_passWord").val();
    $("#tishi").css("color", "red");
    $("#tishi").html("密码，限英文、数字、“_”组成;");
}
function showUserLogin(button) {//显示登陆小窗口
    if (button == "celerityBuy") {


        $("#denglu2").css({ top: $("#" + button).position().top - 50, left: $("#" + button).position().left - 300 });
        document.getElementById("noLoginShopping").style.display = "none";
    } else if (button == "buySubmit") {//快捷购买无登陆弹出窗口
        $("#denglu2").css({ top: $("#" + button).position().top - 120, left: $("#" + button).position().left + 250 });
        document.getElementById("noLoginShopping").style.display = "inline";
    } else {
        $("#denglu2").css({ top: $("#" + button).position().top + 50, left: $("#" + button).position().left + 300 });
        document.getElementById("noLoginShopping").style.display = "none";
    }

    document.getElementById("denglu2").style.display = "inline";
    document.getElementById("minbg").style.display = "inline";
    document.getElementById("login_mask").style.display = "inline";

    document.getElementById("inp_userName").style.display = "inline";

    var h = document.documentElement.scrollHeight;
    if (h < document.documentElement.clientHeight)
        h = document.documentElement.clientHeight;
    $("#login_mask").height(h);
    $("#minbg").offset($("body").position());
    centerObj("#minbg");

}


function CommentSubmit() {  //点击评论的方法

    var content = $('#comcontent').val();
    if (content.length < 5) {
        alert('内容不能少于5个字符!');
        return false;

    } else if (content.length > 140) {

        alert("内容不能多余140个字!");
        return false;

    } else {


        var title = $('#comtitle').val();

        var marksChecked = $('input[name="b"]:checked').val();

        if (content.length >= 5) {
            var jsondata = { "p_id": p_id, "content": "" + encodeURIComponent(content) + "", "rate": marksChecked };
            $.ajax({ type: 'POST', beforeSend: function() { },
                url: '/ProductComment/Create?timestamp=' + new Date().getTime(),
                data: jsondata, dataType: 'json', error: '', success:
function(data) {

    if (!data.online) {

        showUserLogin("Consultation_Id"); //跳出登陆窗口


    } else {

        alert(data.msg);  //返回成功信息
        $('#comcontent').val("");
        commentPage(1, 1);
    }

}

            }
);

        }
        return true;
    }
    return true;
}


function CommentReplySubmit() {  //点击评论回复的方法


    var content = $('#comcontent').val();
    if (content.length > 140) {

        alert('内容不能多余140个字!');
        return false;
    }
    if (content.length >= 5) {
        var jsondata = { "c_id": c_id, "content": "" + encodeURIComponent(content) };
        $.ajax({ type: 'POST', beforeSend: function() { },
            url: '/ProductComment/ConnReply?timestamp=' + new Date().getTime(),
            data: jsondata, dataType: 'json', error: '', success:
function(data) {

    if (!data.online) {


        showUserLogin("Consultation_Id"); //跳出登陆窗口

    } else {

        alert(data.msg);  //返回成功信息
        $('#comcontent').val("");
        location.reload();

    }

}

        }
);

    }


    else {

        alert('内容不能少于5个字符!');

    }


}

