﻿function AddFavorite(sURL, sTitle) {
    try
		{ window.external.addFavorite(sURL, sTitle); }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

function SetHome(obj, vrl) {
    try {
        obj.style.behavior = 'url(#default#homepage)';
        obj.setHomePage(vrl);
    }
    catch (e) {
        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', vrl);
        }
    }
}

function getCookie(b) { var a = document.cookie.match(new RegExp("(^| )" + b + "=([^;]*)(;|$)")); if (a != null) { return unescape(a[2]) } return null }
function setTopColor(d) { var b = getCookie("globalquotecolor"); if (b != null && TrimWhiteSpace(b) == "cn") { var a = document.getElementById(d).getElementsByTagName("span"); for (var c = 6; c < a.length; c++) { if (a[c].className == "green_color") { a[c].className = "red_color" } else { if (a[c].className == "red_color") { a[c].className = "green_color" } } } } }
function setColor() {
    var globalquotecolor = getCookie("globalquotecolor");
    if (globalquotecolor != null && TrimWhiteSpace(globalquotecolor) == "cn") {
        var obj = document.getElementById("tblglobalquote");
        var rowEl, cellEl;
        for (var i = 0; i < obj.rows.length; i++) {
            rowEl = obj.rows[i];
            for (var j = 0; j < obj.rows[i].cells.length; j++) {
                cellEl = rowEl.cells[j];
                if (cellEl.className == "green_color")
                    cellEl.className = "red_color";
                else if (cellEl.className == "red_color")
                    cellEl.className = "green_color";
            }
        }
    }
}

function winOpen(url) {


    if (url != '') {
        window.open(url);
    }
}


function getURLTimeStamp() {
    var nowDate = new Date();
    var h = nowDate.getHours();
    var m = nowDate.getMinutes();
    var s = nowDate.getSeconds();
    if (h <= 9) {
        h = "0" + h;
    }
    if (m <= 9) {
        m = "0" + m;
    }
    if (s <= 9) {
        s = "0" + s;
    }
    return h + "" + m + "" + s;
}



var $h = function(obj) {
    return document.getElementsByName(obj)[0];
}
var $ = function(obj) { return document.getElementById(obj); }

String.prototype.trim = function() {
    // 用正则表达式将前后空格
    // 用空字符串替代。
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
function trims(parms) {
    return parms.replace(/(^\s*)|(\s*$)/g, "");
}


function setCode2(id) {
    $("hcode2").value = id;
}

function createHttpRequest() {
    HttpRequest = false;
    if (window.XMLHttpRequest) {
        HttpRequest = new XMLHttpRequest();
        if (HttpRequest.overrideMimeType) {
            HttpRequest.overrideMimeType("text/xml");
        }
    }
    else if (window.ActiveXObject) {
        try {
            HttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }
    return HttpRequest;
}
function $ajax(method, url, content, callback, failecallback) {
    var obj = createHttpRequest();
    obj.onreadystatechange = function() {
        if (obj.readyState == 4)
            if (obj.status == 200) {
            if (callback != null) callback(obj);
        }
        else {
            if (failecallback != null) failecallback(obj);
        }
    }
    obj.open(method, url, true);
    obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    obj.send(content);
}

function changeClass(obj, index, id) {
    obj.id = "over";
    if (cacheLastId != index) {
        if (cacheLastId != null)
            $("infoE").childNodes[cacheLastId].id = "";
        cacheLastId = index; lastId = index;
        setCode2(id);
    }
}
var lastId = -1, cacheLastId = null, isCode4 = false;
function selectLi(e) {
    try {
        var objUL = $("infoE").childNodes;

        if (e.keyCode == 40) {
            if (objUL.length > 0 && objUL.length > lastId) {
                if (!isCode4) { lastId++; isCode4 = true; }
                if (cacheLastId != null) objUL[cacheLastId].id = "";
                objUL[lastId].id = "over";
                cacheLastId = lastId; lastId++; isCode4 = true;
            }
        } else if (e.keyCode == 38) {
            if (cacheLastId != null) objUL[cacheLastId].id = "";
            if (isCode4) { lastId--; isCode4 = false; }
            lastId != 0 ? lastId-- : lastId; cacheLastId = lastId;
            objUL[lastId].id = "over";
        }
    } catch (e) { }
}
function $getXY(e) {
    var l = e.offsetLeft;
    var t = e.offsetTop;
    while (e = e.offsetParent) {
        l += e.offsetLeft;
        t += e.offsetTop;
    }
    return { x: l, y: t };
}
function setText(obj) {
    if (obj != null) {
        if (cache_InputId != null) {
            if (cache_parms == "split") {
                $(cache_InputId).value += trims(obj.innerHTML.replace(/<a>.*/gi, ""));
                $(cache_InputId).value = $(cache_InputId).value.replace(cache_parms_values, "")
                cache_parms_values = "";
                $(cache_InputId).focus();
            }
            else
                $(cache_InputId).value = trims(obj.innerHTML.replace(/<a>.*/gi, ""));
        }
        var objmsg = document.getElementById("rmsg");
        objmsg.style.display = "none";
        $("infoE").innerHTML = "";
        //重置变量
        lastId = -1, cacheLastId = null, isCode4 = false
    }
}
var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
function setText2(e) {
    var keycode; ;
    if (isIE)
        keycode = e.keyCode;
    else
        keycode = e.which;
    if (keycode == 13) {
        setText($("infoE").childNodes[cacheLastId]);
    }
}
var cache_InputId = null;
var cache_parms = null;
var cache_parms_values = "";
function showE(obj, e, inputID, callType, parms) {
    var objmsg = document.getElementById("rmsg");
    var _inputValue;
    cache_InputId = inputID;
    cache_parms = parms;
    if (e.keyCode != 40 && e.keyCode != 38 && e.keyCode != 13) {
        if (parms == "split") {
            var _array = $(inputID).value.split(',');
            if (_array.length == 0)
                _inputValue = $(inputID).value;
            else
                _inputValue = _array[_array.length - 1];
            cache_parms_values = _inputValue;
        }
        else
            _inputValue = $(inputID).value;
        if (_inputValue.length > 0) {

            $ajax("get", "webhandler/ajaxsearch.ashx?code=" + _inputValue, null, function(obj) {
                var text = obj.responseText;
                try {
                    if (text == null || text == "") { objmsg.style.display = "none"; $("infoE").innerHTML = text; } else { objmsg.style.display = ""; $("infoE").innerHTML = text; }
                } catch (e) { alert(e) }
                ////重置变量
                lastId = -1, cacheLastId = null, isCode4 = false
            }, null);

        }
    }
    var xy = $getXY(obj);
    objmsg.style.left = xy.x + "px";
    objmsg.style.top = xy.y + 25 + "px";
}


//msg
function showMSG(obj, call) {
    var objmsg = document.getElementById("msg");
    if (call != null) {
        objmsg.innerHTML = call();
        objmsg.style.display = "";
    }
    var xy = $getXY(obj);
    objmsg.style.left = xy.x + "px";
    objmsg.style.top = xy.y + 25 + "px";
}
