﻿/**************************************
* Copyright by Spring Studio(http://springstudio.org)
* 网站全局脚本
***************************************/

/**
* 去掉字符串前后空格
*/
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim = function () {
    return this.replace(/(^\s*)/g, "");
}
String.prototype.rtrim = function () {
    return this.replace(/(\s*$)/g, "");
}

/**
* 关键词搜索
*/
function Search(keyword) {
    keyword = keyword.trim();
    if (keyword == "") {
        alert("搜索词不能为空。");
        return;
    }
    if (keyword.indexOf("/") > -1 || keyword.indexOf("\\") > -1 || keyword.indexOf("?") > -1 || keyword.indexOf("*") > -1 || keyword.indexOf("&") > -1 || keyword.indexOf("\"") > -1 || keyword.indexOf(":") > -1) {
        alert("搜索词中不允许有 / \\ ? * & \" : 等特殊字符。");
        return;
    }

    document.location = "/search/" + encodeURIComponent(keyword);
}

/**
* 显示验证码图片
*/
function ShowVerifyImage(imgurl) {
    var objInput = document.getElementById("VerifyCode");
    var objImage = document.getElementById("VerifyImage");
    var objLink = document.getElementById("VerifyLink");

    if (objImage.style.visibility == "hidden") {
        objInput.style.color = "#000000";
        objInput.style.fontSize = "14px";
        objImage.style.visibility = "visible";
        objLink.style.visibility = "visible";
        FlushVerifyImage(imgurl);
    }
}

/**
* 刷新验证码图片
*/
function FlushVerifyImage(imgurl) {
    var objImage = document.getElementById("VerifyImage");
    var objInput = document.getElementById("VerifyCode");

    var numkey = Math.random(); // 产生一个随机数，附加到图片地址，以规避缓存
    numkey = Math.round(numkey * 10000);

    objImage.src = imgurl + "?key=" + numkey;
    objInput.value = "";
    objInput.focus();
}

// 加入收藏夹
function AddFavorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}



// 设为首页
function SetHomepage(sURL) {
    if (document.all) {
        document.body.style.behavior = "url(#default#homepage)";
        document.body.setHomePage(sURL);
    }
    else if (window.sidebar) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
            }
        }
        var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
        prefs.setCharPref("browser.startup.homepage", sURL);
    }
}

function OpenDialog(url, width, height) {
    return window.showModalDialog(url, window, "dialogWidth:" + width + "px;dialogHeight:" + height + "px");
}
