//前台脚本
function id(idstr) { return document.getElementById(idstr); }
function RefreshVerification() {
    var imgid = "imgVerification", textid = "VerificationCode";
    if (id(imgid).onclick == null) { id(imgid).onclick = function () { RandomVerification(textid, imgid); id(textid).focus(); } }
    RandomVerification(textid, imgid);
}
function RandomVerification(inputid, imgid) { if (inputid == null || imgid == null) { return; } else { id(inputid).value = ""; id(imgid).src = "/Ashx/VerificationCode.ashx?r=" + new Date().getTime(); } }
function formatDate(d, f) {
    var date = bx.utility.date(d);
    if (f == null) {
        return date.getFullYear() + "-" + (parseInt(date.getMonth()) + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
    } else if (f == 1) {
        return date.getFullYear() + "-" + (parseInt(date.getMonth()) + 1) + "-" + date.getDate();
    }
}
//头部
function loadLoginStatus() {
    User.info(function (result) {
        if (result.status == "success") {
            var lgobj = id("LoginStatus");
            if (lgobj != null) {
                lgobj.innerHTML = "您好: " + result.message + " <a href=\"/manage/default\">会员中心</a> | <a href=\"/login/default?cmd=logout\">退出登录</a>";
            }
        }
    })
}
function search() {
    var keyword = id("txtKeywords").value;
    if (keyword != "") {
        document.location.href = encodeURI("/search/?keywords=" + keyword);
    }
}
//内容页
function getIpAddress(ipstr, idstr) {
    GetIPAddress(ipstr, function (result) {
        var obj = document.getElementById(idstr);
        if (obj != null) { obj.innerHTML = result.data.city; }
    });
}
function addCollect(_infoId, _title) {
    User.addCollect({ "infoid": _infoId, "title": encodeURI(_title), "url": document.location.pathname }, function (result) {
        if (result.status == "success") {
            Alert("操作成功");
        } else {
            if (result.error == "nologin") {
                Alert(result.message);
            } else if (result.error == "exists") {
                Alert(result.message);
            }
            else {
                Alert("操作失败,请重试");
            }
        }
    });
};
function submitMessage() {//留言
    var ck = form.check();
    if (ck == "") {
        var formValue = form.getJsonStr();
        if (formValue != "") {
            AddLeaveMessage({ "form": encodeURI(formValue) }, function (result) {
                if (result.status == "success") {
                    alert(result.message);
                    location.reload();
                } else if (result.error == "nologin") {
                    Alert(result.message);
                }
                else {
                    Alert(result.message);
                }
            })
        }
    }
}
//发布页
function releaseInfo() {
    var hrole = document.getElementById("HiddenRule");
    var formrule = eval("[" + hrole.value + "]");
    var categoryid = document.getElementById("HiddenCategoryID");
    form.init(formrule);
    var ck = form.check();
    if (ck == "") {
        var formValue = form.getJsonStr();
        Info.release(categoryid.value, { "form": formValue }, function (result) {
            if (result != null && result.status == "success") {
                Alert("操作成功!");
                if (result.message != "") {
                    document.location.href = result.message;
                }
                else {
                    document.location.href = document.location.href;
                }
            } else if (result != null && result.status == "failure") {
                Alert("发布信息失败!,原因:" + result.message);
            }
            else {
                Alert("发布信息失败!");
            }
        });
        //$.post("/info/release/?cmd=release&id=" + categoryid.value + "&r=", { "form": formValue }, function (data) {

        //}, "json");
    }
    else {
        var errobj = document.getElementById("name_" + ck);
        if (errobj != null) {
            form.tips.error(ck, "\"" + errobj.innerHTML + "\" 未通过验证,请修改后重试");

            //errobj.style.border= "1px red solid";
        }
    }
}
//-------URL状态与菜单----------
//2013-12-18
var UrlStatus = {
    /*
      /目录包括文件名本身
       matchstatus指要参与匹配的对象,里面的值可以包含目录名(不包含/符号),参数(key=value格式),锚点
   */
    domAttributeName: "matchstatus",
    selectedCssName: "select",
    id: function (idstr) { return document.getElementById(idstr); },
    setSelectStatus: function (containerId, v) {
        var childList = this.id(containerId).childNodes; var isSelected = false;
        for (var i = 0, j = childList.length; i < j; i++) {
            if (childList[i].nodeName.indexOf("#") == -1) {
                if (childList[i].getAttribute(this.domAttributeName) != null && childList[i].getAttribute(this.domAttributeName) == v) { childList[i].className = this.selectedCssName; isSelected = true; }
            }
        }
        return isSelected;
    },
    setSelectDefaultStatus: function (containerId, v) {//设置默认选中状态,默认是第一个或等于指定的值
        var childList = this.id(containerId).childNodes;
        for (var i = 0, j = childList.length; i < j; i++) {
            if (childList[i].nodeName.indexOf("#") == -1 && childList[i].getAttribute(this.domAttributeName) != null) {
                if (typeof v != "undefined") {
                    if (childList[i].getAttribute(this.domAttributeName) == v) { childList[i].className = this.selectedCssName; break; }
                } else { childList[i].className = this.selectedCssName; break; }
            }
        }
    },
    changeDomSelectStatus: function (containerId, category) {//category有选择的区分使用
        var isSelected = false;
        if (typeof category == "undefined" || category == 1) {//目录
            var dirList = document.location.pathname.split(/\//gi);
            for (var x = 0; x < dirList.length; x++) {
                if (dirList[x] != "") { if (this.setSelectStatus(containerId, dirList[x]) == true) { isSelected = true; }; }
            }
        }
        if (typeof category == "undefined" || category == 2) {//参数
            var paramList = document.location.search.substring(document.location.search.lastIndexOf('?') + 1).split('&');
            for (var x = 0; x < paramList.length; x++) {
                if (paramList[x] != "") { if (this.setSelectStatus(containerId, paramList[x])) { isSelected = true; }; }
            }
        }
        if (typeof category == "undefined" || category == 3) {//锚点
            if (this.setSelectStatus(containerId, document.location.hash)) { isSelected = true; };
        }
        if (isSelected == false) { this.setSelectDefaultStatus(containerId); }
    }
};
/// <summary>
/// 格式化小数位数
/// </summary>
/// <param name="value">小数值</param>
/// <param name="decimal">位数,默认两位,可选项</param>
/// <param name="defaultValue">为空时默认值,可选项</param>
function DecimalFormat(value, decimal, defaultValue) {
    decimal = decimal == null || decimal == "" ? 2 : parseInt(decimal);
    if (value != null && value != "") { return (new Number(value).toFixed(decimal)); } else { if (defaultValue == null) { return new Number(0).toFixed(decimal); } else { return defaultValue; } }
}
function AddFavorite() {
    var _title = document.title;
    var _url = encodeURI(document.location.href);
    try { window.external.addFavorite(_url, _title); } catch (e) {
        try { window.sidebar.addPanel(_title, _url, ""); } catch (e) { alert("加入收藏失败，请使用Ctrl+D进行添加,或手动在浏览器里进行设置."); }
    }
}
function Alert(_msg, _callback) {
    art.dialog({ content: _msg, lock: true, ok: function () { if (typeof _callback == "function") { _callback(true); } } });
}
function Confirm(_msg, _callback) {
    art.dialog({
        title: '确认消息', content: _msg, lock: true, width: "280px", height: "50px", okVal: "确认", cancelVal: "取消", ok: function () { if (_callback != null) { _callback(true); } },
        cancel: function () { if (_callback != null) { _callback(false); } }
    });
}
function DelConfirm(_callback) { Confirm("确定删除?", _callback); }
function addEvent(obj, type, fn) { if (obj.attachEvent) { obj.attachEvent('on' + type, fn); } else { obj.addEventListener(type, fn, false); } }
//页面加载完成要做的事情
addEvent(window, "load", function () {
    //异步加载js和css文件
    getScript("/plugin/dialog/dialog.js;/plugin/dialog/dialog.css", false, function () {
        GetMessageList(function (result) {//检查是否有系统消息
            if (result.status == "success") {
                if (result.data != null) {
                    var txt = document.getElementById("winpop").innerHTML.replace("@msg", result.data[0].MessageContent);
                    win.slideWindow({ height: "100px", text: txt })
                }
            }
        })
    })
    //
});