﻿var domain = "http://www.cn100.com";
function CreateIframe(src) {
    var iframe = $("<iframe>").attr("src", src).attr("mode", "mode").css("border", "0");
    return $(iframe);
}
(function ($) {
    $.init = { set: function () {
        var thisTable = $("table");
        $(thisTable).attr("cellpadding", "0").attr("cellspacing", "0");
        $(thisTable).find("input:radio,input:checkbox").css("border", "none");
        $("iframe").css("border", "0");
    }
    };
    $(function () {
        $.init.set();
    });
})(jQuery);

String.prototype.trim = function () {//去除字符串空格
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.triml = function () {//去除字符串左空格 
    return this.replace(/(^\s*)/g, "");
}
String.prototype.trimr = function () {//去除字符串右空格
    return this.replace(/(\s*$)/g, "");
}

$("input[num=deci]").live("keyup", function () { //验证只能输入小数点后两位
    if (this.value == ".") {//第一个字符不能是 . 
        this.value = "";
    } else {
        $(this).val(this.value.replace(/[^0-9.]/g, ''));
        if (this.value.split('.').length > 2)//有且只有一个.
            $(this).val(this.value.substring(0, this.value.length - 1));

        if (this.value.indexOf(".") > -1) {//保留两位小数
            if (this.value.split('.')[1].length > 2) {
                var temp = this.value.split('.')[1];
                temp = temp.substring(0, temp.length - 1)
                $(this).val(this.value.split('.')[0] + "." + temp);
            }
        }
        else if (this.value.length > 7) {//最大值7位整数  后面可保留两位小数
            $(this).val(this.value.substring(0, this.value.length - 1));
        }
    }
}); //验证只能输入数字

$("input[num=true]").live("keyup", function () {
    if ($(this).val() == "")
        $(this).val("0");
    else
        $(this).val(this.value.replace(/\D/g, ''));
});     //验证只能输入数字


//计算字符串字节数
String.prototype.byteLen = function mbStringLength() {
    var s = this;
    var totalLength = 0;
    var i;
    var charCode;
    for (i = 0; i < s.length; i++) {
        charCode = s.charCodeAt(i);
        if (charCode < 0x007f) {
            totalLength = totalLength + 1;
        } else if ((0x0080 <= charCode) && (charCode <= 0x07ff)) {
            totalLength += 2;
        } else if ((0x0800 <= charCode) && (charCode <= 0xffff)) {
            totalLength += 3;
        }

    }
    return totalLength;
}
//字符串长度，中文2
String.prototype.len = function () {
    var cArr = this.match(/[^\x00-\xff]/ig);
    return this.length + (cArr == null ? 0 : cArr.length);
}
//截取字符，支持中英文 用法  var a = "test";a.sub(2);
String.prototype.sub = function (n) {
    n += 2;
    var r = /[^\x00-\xff]/g;
    if (this.replace(r, "mm").length <= n) {
        return this;
    }
    n = n - 3;
    var m = Math.floor(n / 2);
    for (var i = m; i < this.length; i++) {
        if (this.substr(0, i).replace(r, "mm").length >= n) {
            return this.substr(0, i);
        }
    }
    return this;
};
$("input[len^='max']").live("keyup", function () {//限定字符串长度，中文2位 如：len="max60" 30个汉字
    $(this).val(this.value.trim());
    var maxlength = parseInt($(this).attr("len").replace("max", ""));
    if ($(this).val().len() > maxlength) {
        $(this).val($(this).val().sub(maxlength));
    }
});
$("table[move=true]").find("tr:gt(0)").live("mouseover", function () { $(this).css({ color: "#ff0011", background: "#E7EEFE" }); }).live("mouseout", function () { $(this).css({ color: "#000000", background: "white" }); });

function CheckInit(url) {
    if (window.location.href.toLocaleLowerCase().indexOf(url.toLocaleLowerCase()) > -1) {
        return true;
    }
    return false;
}


//绘制指定大小的图片
function DrawImage(MyPic, W, H) {
    if (MyPic == null) return;
    var image = new Image();
    image.src = MyPic.src;
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= W / H) {
            if (image.width > W) {
                MyPic.width = W;
                MyPic.height = (image.height * W) / image.width;
            }
            else {
                MyPic.width = image.width;
                MyPic.height = image.height;
            }
        }
        else {
            if (image.height > H) {
                MyPic.height = H;
                MyPic.width = (image.width * H) / image.height;
            }
            else {
                MyPic.width = image.width;
                MyPic.height = image.height;
            }
        }
    }
}
//获取本地上传图片路径
function getPath(obj) {
    obj = obj[0];
    if (obj) {
        if (document.selection && document.selection.createRange && window.navigator.userAgent.indexOf("MSIE") >= 1) {
            obj.select();
            try {
                return document.selection.createRange().text;
            } catch (e) {
                alert(e.message + "若要使用本地预览图片功能，请执行:Internet选项--安全\n去掉启用保护模式前面的钩/安全级别设置最低！");
                return obj.value;
            }
        }
        else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            if (obj.files) {
                return obj.files.item(0).getAsDataURL();
            }
            return obj.value;
        }
        return obj.value;
    }
}

var isUserLogin = false; //是否登录

function LoadHeader() {

    if ($("input[id^=permissons]").attr("name") == "store" && ($("input[id^=permissons]").val() == "1" || $("input[id^=permissons]").val() == "2") && !CheckInit("/Store/MyStore") && !CheckInit("Store/StoreRatesDetail")) {/*-加载Store编辑页面*/
        $("ul.ulInversNav ").find("a").attr("href", "javascript:;");
        $.get("/Store/AjaxRenovationPage", function (data) {
            isUserLogin = true;
            $("div#SITE-NAV").html($(data));
            $("li.menu-select,div.decorateMenunone").hover(
                 function () {
                     $(".decorateMenunone").show();
                 },
                  function () {
                      $(".decorateMenunone").hide();
                  }
                );
            $(".decorateBox").find("a").filter("a:contains('查看店铺')").attr("href", "/Show/Store/" + $("input[id^=permissons]").attr("store"));
            $(".decorateBox").find("a").filter("a:contains('预览')").attr("href", "/Store/MyStore/" + $("input[id^=permissons]").attr("store"));
        });
    } else {
        if ($("span[name='login']").attr("login") == "True") {
            isUserLogin = true;
        }
        //        $.get("/LogReg/AjaxHeader", function (data) {
        //            $(".headBox").before(data);
        //            if ($(data).find("span.loginInfo").attr("name") == "login") {
        //                isUserLogin = true;
        //            }
        //        });
    }
}

//迷你购物车加载数据
$("li.cart").live("mouseover", function () {
    LoadMiniCartData();
    $(".HeaderShopingCart").show().css("left", $(this).offset().left + "px");
}).live("mouseout", function () {
    $(".HeaderShopingCart").hide();
});

$("div.HeaderShopingCart").live("mouseover", function () {
    LoadMiniCartData();
    $(this).show();
}).live("mouseout", function () {
    $(this).hide();
});

var firstLoad = false;
function LoadMiniCartData() {
    //    if (!firstLoad && CheckInit("/Store/") == false) {
    //        $.get("/Order/AjaxMiniCart", function (data) {
    //            $(".HeaderShopingCart").html(data);
    //            firstLoad = true;
    //        });
    //    }

    if (!firstLoad) {

        if (CheckInit("Order/ShoppingCart")) {
            $(".HeaderShopingCart").remove();
        }
        else {
            $.get("/Order/AjaxMiniCart", function (data) {
                $(".HeaderShopingCart").html(data);
                firstLoad = true;
            });
        }
    }
}

$(".delMiniCart").live("click", function () {
    $.post("/Order/deleteCart", { cart: $(this).attr("id"), operation: 2 }, function (data) {
        $("#proCount").html(data.split("|")[0]);
    })
    $(this).parent().parent().remove();
    if ($(".mini-cart-bd li").html() == null) {
        $(".mini-cart-hd").html("您购物车里还没有任何宝贝。");
    }
});

function LoginOut() {
    $.post("/LogReg/UserLoginOut", { cart: $(this).attr("id"), operation: 2 }, function (data) {
        if (CheckInit("Order/ShoppingCart")) {
            window.location.reload();
        } else {
            window.location.href = "/";
            //            var login = "亲亲，欢迎来中国第一百货！&nbsp;<a href=\"javascript:;\" onclick=\"Login();\">请登录</a>&nbsp;<a href=\"/LogReg/Register\">免费注册</a>";
            //            $("span.loginInfo").html(login);
            //            $("li.favorite").remove();
        }
    });
}

function Login() {
    var url = window.location.href.replace("http://" + location.hostname, "").replace(":88", "");
    url = url.split("/");
    var returnUrl = "";
    for (var i = 0; i < url.length; i++) {
        returnUrl += "%2f" + url[i];
    }
    window.location.href = "http://reg.cn100.com/LogReg/UserLogin?ReturnUrl=" + returnUrl.replace("%2f%2f", "%2f");
}

function $$(id) {
    return document.getElementById(id);
}


//过滤所有js事件和<script>标签
function FilterScript(strHtml) {
    //var myReg1 = /on\w+\s*=\s*['\"].*['\"]/i;
    //    var myReg2 = /on\w+\s*=\s*[^\s>]*/i;
    //    var myReg3 = /<script.*?>.*?<\/script>/i;
    //    var myReg4 = /(javascript|jscript|vbscript|vbs):/i;

    //    //strHtml = strHtml.replace(myReg1, "");
    //    strHtml = strHtml.replace(myReg2, "");
    //    strHtml = strHtml.replace(myReg3, "");
    //    strHtml = strHtml.replace(myReg4, "");
    return strHtml;
}



//获取url参数
function URLParas(paras) {
    var url = location.href;
    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
    var paraObj = {}
    for (i = 0; j = paraString[i]; i++) {
        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    }
    var returnValue = paraObj[paras.toLowerCase()];
    if (typeof (returnValue) == "undefined") {
        return "";
    } else {
        return returnValue;
    }
}

function FilterString(source) {
    var str = /[@#\$%\^&\*]+/g;
    return str.test(source);
}

//几秒后跳转到指定url页面
function TimeRedircet(secs, url) {
    //    var jumpTo = document.getElementById('jumpTo');
    //    jumpTo.innerHTML = secs;
    setTimeout("window.location.href='" + url + "'", secs * 1000);
}

//判断是否为站内链接
function RegCheckLink(url) {
    // var valid = /^http:\/\/([A-Za-z0-9]+\.)?(cn100\.com){1}(\/[A-Za-z0-9]+(\.[A-Za-z0-9]+)?)?$/;
    var valid = /^http:\/\/([A-Za-z0-9]+\.)?(cn100\.com){1}(\S*)?$/;
    if (valid.test(url)) {
        return true;
    }
    else {
        return false;
    }
}

//替换所以内容
function ReplaceAll(str, sptr, sptr1) {

    while (str.indexOf(sptr) > -1) {
        str = str.replace(sptr, sptr1);
    }
    return str;
}


