﻿/*
[Discuz!] (C)2001-2007 Comsenz Inc.
This is NOT a freeware, use is subject to license terms

$RCSfile: common.js,v $
$Revision: 1.101 $
$Date: 2007/07/20 14:05:09 $
*/

var lang = new Array();
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

function $(id)
{
    return document.getElementById(id);
}

Array.prototype.push = function(value)
{
    this[this.length] = value;
    return this.length;
}

function checkall(form, prefix, checkall)
{
    var checkall = checkall ? checkall : 'chkall';
    for (var i = 0; i < form.elements.length; i++)
    {
        var e = form.elements[i];
        if (e.name && e.name != checkall && (!prefix || (prefix && e.name.match(prefix))))
        {
            e.checked = form.elements[checkall].checked;
        }
    }
}

function doane(event)
{
    e = event ? event : window.event;
    if (is_ie)
    {
        e.returnValue = false;
        e.cancelBubble = true;
    } else if (e)
    {
        e.stopPropagation();
        e.preventDefault();
    }
}

function fetchCheckbox(cbn)
{
    return $(cbn) && $(cbn).checked == true ? 1 : 0;
}

function getcookie(name)
{
    var cookie_start = document.cookie.indexOf(name);
    var cookie_end = document.cookie.indexOf(";", cookie_start);
    return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}

function thumbImg(obj)
{
    var zw = obj.width;
    var zh = obj.height;
    if (is_ie && zw == 0 && zh == 0)
    {
        var matches
        re = /width=(["']?)(\d+)(\1)/i
        matches = re.exec(obj.outerHTML);
        zw = matches[2];
        re = /height=(["']?)(\d+)(\1)/i
        matches = re.exec(obj.outerHTML);
        zh = matches[2];
    }
    obj.resized = true;
    obj.style.width = zw + 'px';
    obj.style.height = 'auto';
    if (obj.offsetHeight > zh)
    {
        obj.style.height = zh + 'px';
        obj.style.width = 'auto';
    }
    if (is_ie)
    {
        var imgid = 'img_' + Math.random();
        obj.id = imgid;
        setTimeout('try {if ($(\'' + imgid + '\').offsetHeight > ' + zh + ') {$(\'' + imgid + '\').style.height = \'' + zh + 'px\';$(\'' + imgid + '\').style.width = \'auto\';}} catch(e){}', 1000);
    }
}

function in_array(needle, haystack)
{
    if (typeof needle == 'string' || typeof needle == 'number')
    {
        for (var i in haystack)
        {
            if (haystack[i] == needle)
            {
                return true;
            }
        }
    }
    return false;
}

function setcopy(text, alertmsg)
{
    if (is_ie)
    {
        clipboardData.setData('Text', text);
        alert(alertmsg);
    } else if (prompt('Press Ctrl+C Copy to Clipboard', text))
    {
        alert(alertmsg);
    }
}

function isUndefined(variable)
{
    return typeof variable == 'undefined' ? true : false;
}

function mb_strlen(str)
{
    var len = 0;
    for (var i = 0; i < str.length; i++)
    {
        len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? (/*charset == 'utf-8' ? 3 : 2*/3) : 1;
    }
    return len;
}

function setcookie(cookieName, cookieValue, seconds, path, domain, secure)
{
    var expires = new Date();
    expires.setTime(expires.getTime() + seconds);
    document.cookie = escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toLocaleString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

function strlen(str)
{
    return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}

function updatestring(str1, str2, clear)
{
    str2 = '_' + str2 + '_';
    return clear ? str1.replace(str2, '') : (str1.indexOf(str2) == -1 ? str1 + str2 : str1);
}

function toggle_collapse(objname, noimg)
{
    var obj = $(objname);
    obj.style.display = obj.style.display == '' ? 'none' : '';
    if (!noimg)
    {
        var img = $(objname + '_img');
        img.src = img.src.indexOf('_yes.gif') == -1 ? img.src.replace(/_no\.gif/, '_yes\.gif') : img.src.replace(/_yes\.gif/, '_no\.gif')
    }
    var collapsed = getcookie('discuz_collapse');
    collapsed = updatestring(collapsed, objname, !obj.style.display);
    setcookie('discuz_collapse', collapsed, (collapsed ? 86400 * 30 : -(86400 * 30 * 1000)));
}

function trim(str)
{
    return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}

/*
function updateseccode() {
type = seccodedata[2];
var rand = Math.random();
if(type < 2) {
$('seccodeimage').innerHTML = '<img id="seccode" onclick="updateseccode()" width="' + seccodedata[0] + '" height="' + seccodedata[1] + '" src="seccode.php?update=' + rand + '" class="absmiddle" alt="" />';
} else {
$('seccodeimage').innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + seccodedata[0] + '" height="' + seccodedata[1] + '" align="middle">'
+ '<param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="seccode.php?update=' + rand + '" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" />'
+ '<embed src="seccode.php?update=' + rand + '" quality="high" wmode="transparent" bgcolor="#ffffff" width="' + seccodedata[0] + '" height="' + seccodedata[1] + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
}
}

function updatesecqaa() {
var x = new Ajax();
x.get('ajax.php?action=updatesecqaa&inajax=1', function(s) {
$('secquestion').innerHTML = s;
});
}
*/

function _attachEvent(obj, evt, func)
{
    if (obj.addEventListener)
    {
        obj.addEventListener(evt, func, false);
    } else if (obj.attachEvent)
    {
        obj.attachEvent("on" + evt, func);
    }
}
/*Discuz!NT*/
function scrollHiddenDiv(div, scrollwidth)
{
    div.scrollLeft += scrollwidth;
}

function findtags(parentobj, tag)
{
    if (!isUndefined(parentobj.getElementsByTagName))
    {
        return parentobj.getElementsByTagName(tag);
    } else if (parentobj.all && parentobj.all.tags)
    {
        return parentobj.all.tags(tag);
    } else
    {
        return null;
    }
}

function getQueryString(queryname)
{
    var qKeys = {};
    var re = /[?&]([^=]+)(?:=([^&]*))?/g;
    var matchInfo;
    while (matchInfo = re.exec(location.search))
    {
        qKeys[matchInfo[1]] = matchInfo[2];
    }
    return typeof (qKeys[queryname]) == 'undefined' ? '' : qKeys[queryname];
}

function getposition(obj)
{
    var r = new Array();
    r['x'] = obj.offsetLeft;
    r['y'] = obj.offsetTop;
    while (obj = obj.offsetParent)
    {
        r['x'] += obj.offsetLeft;
        r['y'] += obj.offsetTop;
    }
    return r;
}

//显示提示层
function showhintinfo(obj, objleftoffset, objtopoffset, title, info, objheight, showtype, objtopfirefoxoffset)
{

    var p = getposition(obj);

    if ((showtype == null) || (showtype == ""))
    {
        showtype == "up";
    }
    document.getElementById('hintiframe' + showtype).style.height = objheight + "px";
    document.getElementById('hintinfo' + showtype).innerHTML = info;
    document.getElementById('hintdiv' + showtype).style.display = 'block';

    if (objtopfirefoxoffset != null && objtopfirefoxoffset != 0 && !isie())
    {
        document.getElementById('hintdiv' + showtype).style.top = p['y'] + parseInt(objtopfirefoxoffset) + "px";
    }
    else
    {
        if (objtopoffset == 0)
        {
            if (showtype == "up")
            {
                document.getElementById('hintdiv' + showtype).style.top = p['y'] - document.getElementById('hintinfo' + showtype).offsetHeight - 40 + "px";
            }
            else
            {
                document.getElementById('hintdiv' + showtype).style.top = p['y'] + obj.offsetHeight + 5 + "px";
            }
        }
        else
        {
            document.getElementById('hintdiv' + showtype).style.top = p['y'] + objtopoffset + "px";
        }
    }
    document.getElementById('hintdiv' + showtype).style.left = p['x'] + objleftoffset + "px";
}



//隐藏提示层
function hidehintinfo()
{
    document.getElementById('hintdivup').style.display = 'none';
    document.getElementById('hintdivdown').style.display = 'none';
}

//当指定name的复选框选中时，激活相应的按钮
//arguments[0]为指定form，arguments[1]为复选框的name，arguments[2]～arguments[arguments.length - 1]为要激活的按钮
function checkedEnabledButton()
{
    for (var i = 0; i < arguments[0].elements.length; i++)
    {
        var e = arguments[0].elements[i];
        if (e.name == arguments[1] && e.checked)
        {
            for (var j = 2; j < arguments.length; j++)
            {
                document.getElementById(arguments[j]).disabled = false;
            }
            return;
        }
    }
    for (var j = 2; j < arguments.length; j++)
    {
        document.getElementById(arguments[j]).disabled = true;
    }
}

function CheckAll(form)
{
    for (var i = 0; i < form.elements.length; i++)
    {
        var e = form.elements[i];
        if (e.type == "checkbox" && e.name != 'chkall' && e.name != 'deleteMode')
        {
            e.checked = form.chkall.checked;
        }
    }
}

/*输出返回按钮*/
function write_backbutton(url)
{
    var html = "&nbsp;<button type=\"button\" class=\"ManagerButton\" onclick=\"window.location.href = '" + url + "';\"><img src=\"images/back.gif\"/> 返 回 </button>";
    document.write(html);
}

//取消状态栏显示链接地址
function window.onload()
{
    var olink = document.links;
    for (var i = 0; i < olink.length; ++i)
    {
        olink[i].onmouseover = function()
        {
            window.status = '';
            return true;
        }
        olink[i].onfocus = function()
        {
            window.status = '';
            return true;
        }
    }
}
