﻿/// <reference path="jQuery.IntelliSense.js" />
jQuery.ISEmail = function (email) {
    var strlen = email.length;
    var email_rule = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
    return (strlen >= 6 && strlen <= 40 && email_rule.test(email));
}
$(document).ready(function () {
    $("div.c_mid ul  li").each(function () {

        $(this).hover(function () {
            $(this).toggleClass('current');
        });
    });


});

//添加到收藏夹
var addToFavorite = function (code) {
    $.ajax({
        url: '/Ashx/Favorite.ashx',
        type: 'Post',
        timeout: 8000,
        cache: false,
        data: "ProductCode=" + code,
        error: function () {
            alert('添加失败！');
        },
        success: function (result) {
            if (result.Result) {
                $("#SearchBarFavCount").text(parseInt($("#SearchBarFavCount").text()) + 1);
                alert(result.Message);
            }
            else {
                alert(result.Message);
            }
        }
    });
}



//添加到购物车
var addToCart = function (code, qty, options) {
    $.ajax({
        url: '/Ashx/Cart.ashx',
        type: 'Post',
        timeout: 18000,
        cache: false,
        data: { 'ProductCode': code, 'Qty': qty, 'options': options },
        error: function () {
            alert('添加失败！');
        },
        success: function (result) {
            if (result.Result) {
                if (confirm("添加购物车成功,是否需要跳转到购物车?"))
                    window.location = "/Cart/Index.aspx";
                else
                    addCartCount(qty);
            }
            else {
                alert(result.Message);
            }
        }
    });
}

var addCartCount = function (addcount) {
    $("#SearchBarCartCount").text(parseInt($("#SearchBarCartCount").text()) + parseInt(addcount));
}
var removeCartCount = function (removeCount) {
    $("#SearchBarCartCount").text(parseInt($("#SearchBarCartCount").text()) - removeCount);

}
var setCartCont = function (newCartCount) {
    $("#SearchBarCartCount").text(newCartCount);
}
//发表评论
var addProductComment = function (code, comment) {
    $.ajax({
        url: '/Ashx/Comment.ashx',
        type: 'Post',
        timeout: 8000,
        cache: false,
        data: { 'ProductCode': code, 'Comment': comment },
        error: function () {
            alert('发表评论失败！');
        },
        success: function (result) {
            if (result.Result) {
                alert(result.Message);
            }
            else {
                alert(result.Message);
            }
        }
    });
}

function formatFloat(src, fractionDigts) {
    return (parseInt(src * Math.pow(100, fractionDigts) + 0.5) / Math.pow(100, fractionDigts)).toString();

}


var changeBar = function (type, cartCode, proID) {
    var txtC = $("#" + cartCode);
    var qty = txtC.val();
    var num = parseInt(qty);

    var otxtAmount = $("#h" + cartCode);
    var oamount = otxtAmount.val();


    if (type == '+') {
        num = num + 1;
    }
    if (type == '-') {
        num = num - 1;
    }
    if (type != 'del') {
        type = 'update';
        if (parseInt(oamount) == num)
            return;
    }
    if (type == "del") { num = 0; }
    if (num < 0) {
        alert('您输入的数字已经超出的最小值');
        txtC.val(oamount);
        return;
    }
    if (num == 0) {
        if (!confirm("确定移除该产品么!")) { return; }
    }


    //txtC.focus();
    $.ajax({
        url: '/Ashx/Cart.ashx?rand=' + Math.random(),
        type: 'Post',
        timeout: 18000,
        cache: false,
        data: { 'action': type, 'CartCode': cartCode, 'ProductCode': proID, 'qty': num },
        error: function () {
            alert('处理失败！');
        },
        success: function (result) {

            if (result.Result) {
                // alert(result.Message);
                //更新总价的显示
                txtC.val(num);

                setCartCont(num);

                if (num == 0) {
                    $("#tr" + cartCode).remove();

                }
                hidenCheck();

                $("#subtotle" + cartCode).html(formatFloat(num * parseFloat($("#price" + cartCode).text()), 3));
                $("#osubtotle" + cartCode).val(formatFloat(num * parseFloat($("#oprice" + cartCode).val()), 3));
                $("#subweight" + cartCode).val(formatFloat(num * parseFloat($("#weight" + cartCode).text()), 3));

                var totle = 0.00; var weight = 0.00; var ototle = 0.00;
                $(".subtotle").each(function (i, e) { totle = parseFloat(totle) + parseFloat($(e).text()); });
                $(".osubtotle").each(function (i, e) { ototle = parseFloat(ototle) + parseFloat($(e).val()); });
                $(".subweight").each(function (i, e) { weight = parseFloat(weight) + parseFloat($(e).val()); });
                $("#totle").text(formatFloat(totle, 3));
                $("#weight").text(formatFloat(weight, 3));
                $("#discount").text(formatFloat((ototle - totle), 3));

                window.location = window.location;
            }
            else {
                alert(result.Message);
            }
        }
    });

}



//获取产品库存
var getInv = function (code, options) {
    var pinv = 0;
    $.ajax({
        url: '/Ashx/Inv.ashx',
        type: 'Post',
        timeout: 8000,
        async: false,
        cache: false,
        data: { 'ProductCode': code, 'options': options },
        error: function () {
            alert('获取产品库存失败！');
            return 0;
        },
        success: function (result) {
            if (result.Result) {
                pinv = result.Data;
            }
            else {
                alert(result.Message);
                return 0;
            }
        }
    });
    return pinv;
}


function setAmountStatus(obj, x) {

    var liobj = $(obj).parent().parent();

    if (x > 0)
        liobj.addClass("active");
    else
        liobj.removeClass("active");

    return checkGift(liobj, x);
}

function checkGift(liobj, x) {

    var ototal = $('#obalance').val();

    if (ototal == undefined)
        return true;
    var lis = $(".gift_select li").filter('.active');

    lis.each(function (index, lia) {
        var code = $(this).attr('id');
        var price = $('#m' + code).val();


        var qty = $('#pamount' + code).val();
        if (null != liobj && $(liobj).attr('id') == code)
            qty = x;
        ototal = formatFloat(parseFloat(ototal), 3) - formatFloat(parseFloat(price * qty), 3);
        ototal = formatFloat(ototal, 3);
    });

    if (ototal >= 0) {
        $('#balance').html(ototal);
        return true;
    }
    else {
        alert("您的可兑换赠品金额超出！");

        if (null != liobj) {
            if (x > 1) {
                var code = $(liobj).attr('id');

            }
            else
                liobj.removeClass("active");
        }
        return false;
    }
}

var setAmount = {
    min: 0,
    max: 999,
    reg: function (x) {
        return new RegExp("^[0-9]\\d*$").test(x);
    },
    amount: function (obj, mode) {
        var x = $(obj).val();
        if (this.reg(x)) {
            if (mode) {
                x++;
            } else {
                x--;
            }
        } else {
            alert("请输入正确的数量！");
            $(obj).val(0);
            $(obj).focus();
        }
        return x;
    },
    reduce: function (obj) {
        var x = this.amount(obj, false);
        if (x >= this.min) {
            if (setAmountStatus(obj, x))
                $(obj).val(x);
        } else {
            alert("商品数量最少为" + this.min);
            $(obj).val(0);
            $(obj).focus();
        }
    },
    add: function (obj) {
        var x = this.amount(obj, true);
        if (x <= this.max) {
            if (setAmountStatus(obj, x))
                $(obj).val(x);
        } else {
            alert("商品数量最多为" + this.max);
            $(obj).val(999);
            $(obj).focus();
        }
    },
    modify: function (obj) {
        var x = $(obj).val();
        if (x < this.min || x > this.max || !this.reg(x)) {
            alert("请输入正确的数量！");
            $(obj).val(0);
            $(obj).focus();
        }
        else {
            if (setAmountStatus(obj, x))
                return true;
            else {
                $(obj).val(0);
                return false;
            }
        }
    }
}



function contains(string, substr, isIgnoreCase) {
    if (isIgnoreCase) {
        string = string.toLowerCase();
        substr = substr.toLowerCase();
    }
    var startChar = substr.substring(0, 1);
    var strLen = substr.length;
    for (var j = 0; j < string.length - strLen + 1; j++) {
        if (string.charAt(j) == startChar)//如果匹配起始字符,开始查找
        {
            if (string.substring(j, j + strLen) == substr)//如果从j开始的字符与str匹配，那ok
            {
                return true;
            }
        }
    }
    return false;
}
 


 
