﻿$(document).ready(function () {
    RepositionSpan();   //定位消息提示框
});
///<summary>显示消息，该方法主要用于后台通过向客户端注册JS调用</summary>
///<param name="msg">显示的消息文字</param>
///<param name="secon">要显示的毫秒数</param>
function ShowMessage(msg, secon) {
    var obj = document.getElementById("spMessage");
    $(obj).show("fast", function () { $(this).text(msg); $(this).corner("5px"); });
    $(obj).delay(secon);
    $(obj).hide("fast", function () { $(this).text(""); });
}
///<param name="secon">要延迟的毫秒数</param>
function DelayMessage(secon) {
    $("#spMessage").delay(secon);
}
function RepositionSpan() {
    var documentClientWidth = $(document).get(0).documentElement.clientWidth / 2; //居中
    var documentClientHeight = $(document).get(0).documentElement.clientHeight / 2; //居中
    $("#spMessage").css({ "left": documentClientWidth + "px", "top": documentClientHeight + "px" });
}
var HomepageFavorite = {
    //设为首页
    Homepage: function () {
        if (document.all) {
            document.body.style.behavior = 'url(#default#homepage)';
            document.body.setHomePage(window.location.href);

        }
        else if (window.sidebar) {
            if (window.netscape) {
                try {
                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                }
                catch (e) {
                    alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
                    history.go(-1);
                }
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', window.location.href);
        }
    }
    ,

    //加入收藏
    Favorite: function Favorite(sURL, sTitle) {
        try {
            window.external.addFavorite(sURL, sTitle);
        }
        catch (e) {
            try {
                window.sidebar.addPanel(sTitle, sURL, "");
            }
            catch (e) {
                alert("加入收藏失败,请手动添加.");
            }
        }
    }
}
function textLimitCheck(thisArea, maxLength,SpanId) {//根据onkeyup事件计算文本框中的字符个数，限制在500以内
    if (thisArea.value.length > maxLength) {
        alert(maxLength + ' 个字限制. \r超出的将自动去除.');
        thisArea.value = thisArea.value.substring(0, maxLength);
        thisArea.focus();
    }
    /*回写span的值，当前填写文字的数量*/
    $("#" + SpanId).html(thisArea.value.length);
}
//保存Cookie
function cookiesave(n, v, mins, dn, path)
{
    if (n)
    {
        var expires;
        if (mins == -1) {
            expires = "";
        }
        else {
            if (!mins) {
                mins = 60;
            }
            var date = new Date();
            date.setTime(date.getTime() + (mins * 60 * 1000));
            expires = "; expires=" + date.toGMTString();
        }

        if (!path) path = "/";

        if (dn) dn = "domain=" + dn + "; ";
        document.cookie = n + "=" + encodeURIComponent(v) + expires + "; " + dn + "path=" + path;
    }
}
//得到Cookie
function cookieget(n)
{
    var name = n + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++)
    {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(name) == 0) return decodeURIComponent(c.substring(name.length, c.length));
    }
    return "";
}
function setname(cookieName, value) {
    cookiesave(cookieName, value, "", "", ""); //写入Cookie到客户端
}
///清除cookie
function clearCookie(cookieName) {
    cookiesave(cookieName, "", "", "", "");
}
///移除HTML代码，
function RemoveHTML(obj,event)
{
    var argumentsLength = arguments.length;
    if (argumentsLength == 2)
    {
        //KeyUp
        if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40)
        {
            return;
        }
        if (window.event.ctrlKey && event.keyCode == 65)
        {
            return;
        }
    }
    var tempValue = obj.value.replace(/<\/?[^>]+>/g, ""); //去除HTML tag
    tempValue = tempValue.replace(/[ | ]*\n/g, "\n"); //去除行尾空白
    tempValue = tempValue.replace(/\n[\s| | ]*\r/g, "\n"); //去除多余空行
    if (obj.value != tempValue)
    {
        obj.value = tempValue;
    }
}
//去掉空格
String.prototype.Trim = function() { return Trim(this); }
String.prototype.LTrim = function() { return LTrim(this); }
String.prototype.RTrim = function() { return RTrim(this); }
function LTrim(str)
{
    var i;
    for (i = 0; i < str.length; i++)
    {
        if (str.charAt(i) != " " && str.charAt(i) != " ")
            break;
    }
    str = str.substring(i, str.length);
    return str;
}
function RTrim(str)
{
    var i;
    for (i = str.length - 1; i >= 0; i--)
    {
        if (str.charAt(i) != " " && str.charAt(i) != " ")
            break;
    }
    str = str.substring(0, i + 1);
    return str;
}
function Trim(str)
{
    return LTrim(RTrim(str));
}
//扩展数组
Array.prototype.contains = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
}
//设置ckeditor不能为空，解决jquery.validate不能验证的问题
function SetCkeditorNotNull(editorName) {
    CKEDITOR.instances[editorName].on("instanceReady", function () {
        //set keyup event   
        this.document.on("keyup", function () { updateTextArea(editorName); });
        //and paste event   
        this.document.on("paste", function () { updateTextArea(editorName); });
    });
}
function updateTextArea(editorName) {
    CKEDITOR.tools.setTimeout(function () {
        $("#" + editorName).val(CKEDITOR.instances[editorName].getData());
        $("#" + editorName).trigger('keyup');
    }, 0);
}
// 选项卡
function Tab(name)
{
	$(name+" dl dt>a:first").addClass("tabActive");
	$(name+" dl dd ul").not(":first").hide();
	$(name+" dl dt>a").unbind("click").bind("click", function(){
		$(this).siblings("a").removeClass("tabActive").end().addClass("tabActive");
		var index = $(name+" dl dt>a").index( $(this) );
		$(name+" dl dd ul").eq(index).siblings(name+" dl dd ul").hide().end().fadeIn("slow");
   });
}
// 渐变弹出层
var speed = "fast"; //动画速度
//objName引发点击事件对象，theDIV为显示的DIV
function ShowDIV(objName, theDIV, IsClickHide) {
    $(objName).click(function(event) {//绑定事件处理
        event.stopPropagation();
        var offset = $(event.target).offset(); //取消事件冒泡
        $(theDIV).css({ top: offset.top + $(event.target).height() + "px", left: offset.left }); //设置弹出层位置
        $(theDIV).show(speed); //动画显示
    });
    if (IsClickHide) {
        $(document).click(function(event) { $(theDIV).hide(speed) }); //单击空白区域隐藏
        //$(theDIV).click(function(event) { $(theDIV).hide(speed) });//单击弹出层则自身隐藏
    } else {
        $("#BtnSubmit").click(function() { $(theDIV).hide(speed); });
        $("div").not($(theDIV)).click(function(event) { $(theDIV).hide(speed) }); //单击空白区域隐藏
    }
}
