/*16-12-26 ma 控制滚动条滚动到指定控件*/
jQuery.GetXaddress = function (e) {/*获取X坐标*/
    var t = e.offsetTop;
    while (e = e.offsetParent) {
        t += e.offsetTop;
    }
    return t;
}
jQuery.LeversScroll = function (window, e) {/*控制滚动条滚动到指定控件*/
    var bodyH = document.body.clientHeight;
    var x = $.GetXaddress(e);
    if (x - document.body.scrollTop < bodyH && x - document.body.scrollTop > 0) return;
    if (bodyH < x) {
        window.scroll(0, x - bodyH / 2 );
    } else {
        window.scroll(0, 0);
    }
}
jQuery.GetYaddress = function (e) {/*获取Y坐标*/
    var t = e.offsetLeft;
    while (e = e.offsetParent) {
        t += e.offsetLeft;
    }
    return t;
}

/*数字字符串截取 num数字字符串 num2保留小数点后几位*/
jQuery.NumberBzQz = function (num, num2) {
    var m = "";
    if (num.indexOf('.') > 0) {
        m = num.substring(0, num.indexOf('.'));//截取小数点之前   0001000123
        m = m.replace(/\b(0+)/gi, "");
        var n = num.substring(num.indexOf('.') + 1, num.indexOf('.') + 1 + num2);
        if (n.length < 1) {
            n = "00";
        }
        if (m.length < 1) {
            m = "0" +"."+ n;
        } else {

            m = m +"."+ n;
        }
    } else {
        m = num.replace(/\b(0+)/gi, "");
        if (m.length < 1) {
            m = "0";
        }
    }
    return m;
}

/*日期比较验证 yyyy-M-d*/
jQuery.DateComparison = function (date1, date2) {
    var bool = true;
    if ((new Date(date1) - new Date(date2))>0) bool = false;
    return bool;
}

/*时间比较 hh-mm*/
jQuery.TimeComparison = function (time1, time2) {
    var t1 = time1.substring(0,2);
    var t2 = time2.substring(0, 2);
    if (t1 > t2) {
        return false;
    }
    else if (t1 == t2) {
        t1 = time1.substring(3,5);
        t2 = time2.substring(3, 5);
        if (t1 > t2) {
            return false;
        }
        else {
            return true;
        }
    } else {
        return true;
    }
}
/*星期验证*/
jQuery.WeekComparison = function (sdate, edate, days) {
    var weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    var w1 = new Date(sdate).getDay();
    var w2 = new Date(edate).getDay();
    var xcdays = (new Date(edate) - new Date(sdate)) / 24 / 60 / 60 / 1000;
    if (xcdays >= 7) {
        return true;
    } else if (xcdays < 0) {
        return false;
    }
    else {
        if (w1 > w2) {
            for (n in days) {
                var z = 0;
                for (var i = w1; i <= 7; i++) {
                    if (days[n] == weeks[i]) {
                        z++;
                    }
                }
                for (var i = 0; i <= w2; i++) {
                    if (days[n] == weeks[i]) {
                        z++;
                    }
                }
                if (z == 0) {
                    return false;
                }
            }
            return true;
        } else if (w1 == w2) {
            if (weeks[w1]==days) {
                return true;
            } else {
                return false;
            }
        } else {
            for (n in days) {
                var z = 0;
                for (var i = w1; i <= w2; i++) {
                    if (days[n] == weeks[i]) {
                        z++;
                    }
                }
                if (z == 0) {
                    return false;
                }
            }
            return true;
        }
    }
}

/*图片上传前预览*/
jQuery.ImgYulan = function (file, id) {
    if (!/image\/\w+/.test(file.type)) {
        layer.msg("请选择图片文件", { icon: 2, time: 2000 });
        return false;
    } if (file.size > 300 * 1024) {
        layer.msg("图片不能大于300KB", { icon: 2, time: 2000 });
        return false;
    }
    var img;
    console.log(document.getElementById("fileImg"));
    console.log(file);
    console.log("file-size=" + file.size);
    var reader = new FileReader();
    reader.onloadstart = function (e) {
        console.log("开始读取....");
    }
    reader.onprogress = function (e) {
        console.log("正在读取中....");
    }
    reader.onabort = function (e) {
        console.log("中断读取....");
    }
    reader.onerror = function (e) {
        console.log("读取异常....");
    }
    reader.onload = function (e) {
        console.log("成功读取....");
        var img = document.getElementById(id);
        img.src = e.target.result;
        img.width = 150;

        img.title = file.name;
    }
    reader.readAsDataURL(file);
    return true;
}

/*多图片上传前预览*/
jQuery.DuoImgsYulan = function (file, id) {
    for (var i = 0; i < 3; i++) {
        if (!/image\/\w+/.test(file[i].type)) {
            layer.msg("请选择图片文件", { icon: 2, time: 2000 });
            return false;
        }
        if (file[i].size > 300 * 1024) {
            layer.msg("图片不能大于300KB", { icon: 2, time: 2000 });
            continue;
        }
        var img;
        console.log(document.getElementById("fileImg"));
        console.log(file[i]);
        console.log("file-size=" + file[i].size);
        var reader = new FileReader();
        reader.onloadstart = function (e) {
            console.log("开始读取....");
        }
        reader.onprogress = function (e) {
            console.log("正在读取中....");
        }
        reader.onabort = function (e) {
            console.log("中断读取....");
        }
        reader.onerror = function (e) {
            console.log("读取异常....");
        }
        reader.onload = function (e) {
            console.log("成功读取....");
            //var img = document.getElementById(id);
            //img.src = e.target.result;
            //img.width = 150;
            //img.title = file[i].name;
            var div = document.createElement("div");//外层 div
            div.setAttribute("style", "position:relative;width:inherit;height:inherit;float:left;z-index:2;width:150px;margin-left:8px;margin-right:8px;");
            var del = document.createElement("div"); //删除按钮div
            del.setAttribute("style", "position: absolute; bottom: 0px; right: 0px; z-index: 99; width: 30px; height:30px;border-radius:50%;")
            var delicon = document.createElement("img");
            delicon.setAttribute("src", "../icon/删除.png");
            delicon.setAttribute("title", "删除");
            delicon.setAttribute("style", "cursor:pointer;");
            del.onclick = function () {
                this.parentNode.parentNode.removeChild(this.parentElement);
                ClearfirtsImg();
            };
            del.appendChild(delicon);
            div.appendChild(del);
            var imgs = document.createElement("img");//上传的图片
            imgs.setAttribute("name", "loadimgs");
            imgs.setAttribute("src", e.target.result);
            imgs.setAttribute("width", 150);
            if (document.getElementById(id).childNodes.length>2) {
                document.getElementById(id).removeChild(document.getElementById(id).firstChild);
            }
            div.appendChild(imgs)
            document.getElementById(id).appendChild(div);
        
          
            //imgdiv += '<img  src="' + e.target.result + '"  width="150px" /> <div id="delicon" style="position: absolute; bottom: 0px; right: 0px; z-index: 99; width: 30px; height:30px;border-radius:50%;" onclick="ClearlogoImg()"><img src="~/icon/删除.png" title="删除" style="cursor:pointer;" /></div>'
            //alert(imgdiv)
        }
        reader.readAsDataURL(file[i]);
    }
}

/*本地图片预览    id  图片预览层 父元素ID  ajaxurl删除时候请求Ajax路径  imgsrc 图片路径  imgname 图片名称(带后缀名)*/
jQuery.BDimgYuLan = function (id,ajaxurl,imgsrc,imgname) {
    var div = document.createElement("div");//外层 div
    div.setAttribute("style", "position:relative;width:inherit;height:inherit;float:left;z-index:2;width:150px;margin-left:8px;margin-right:8px;");
    var del = document.createElement("div"); //删除按钮div
    del.setAttribute("style", "position: absolute; bottom: 0px; right: 0px; z-index: 99; width: 30px; height:30px;border-radius:50%;")
    var delicon = document.createElement("img");//删除图标
    delicon.setAttribute("src", "../icon/删除.png");
    delicon.setAttribute("title", "删除");
    delicon.setAttribute("style", "cursor:pointer;");
    del.onclick = function () { //删除图标点击事件
        this.parentNode.parentNode.removeChild(this.parentElement);
        //layer.confirm('确定删除已经上传图片?', { icon: 3, title: '提示' }, function (index) {
            $.ajax({
                url: ajaxurl,
                data: {
                    imgname: imgname
                },
                type: "post",
                datatype: "text",
                success: function (data) {
                    if (data == "err") {
                        layer.msg("删除失败", { icon: 2, time: 2000 });
                    } if (data == "ok") {
                        layer.msg("删除成功", { icon: 1, time: 2000 });
                        GetFirstImgs();
                    }
                }, error: function () {
                    layer.msg("删除失败", { icon: 2, time: 2000 });
                }
            })
        //    layer.close(index);
        //});
    };
    del.appendChild(delicon);
    div.appendChild(del);
    var imgs = document.createElement("img");//上传的图片
    imgs.setAttribute("src", imgsrc);
    imgs.setAttribute("width", 150);
    imgs.onerror = function () {
        this.parentNode.parentNode.removeChild(this.parentElement);
        return;
    }
    if (document.getElementById(id).childNodes.length > 2) {
        document.getElementById(id).removeChild(document.getElementById(id).firstChild);
    }
    div.appendChild(imgs)
    document.getElementById(id).appendChild(div);
}

jQuery.GetToday = function () {
    var myDate = new Date();
    return myDate.getFullYear() + "-" + ((myDate.getMonth() + 1) < 10 ? "0" + (myDate.getMonth() + 1) : (myDate.getMonth() + 1)) + "-" + ((myDate.getDate()) < 10 ? "0" + (myDate.getDate()) : (myDate.getDate()));
    //return myDate.getFullYear() + "-" +(myDate.getMonth() + 1)+ "-" + myDate.getDate();
}
