// 说明：用 JavaScript 实现网页图片等比例缩放 
function DrawImage(ImgD,Imgwidth)
{
    defaultWidth = Imgwidth;
    var image=new Image(); 
    image.src=ImgD.src;
    if(image.width>0 && image.height>0)
    { 
        if(image.width>defaultWidth)
        {ImgD.width=defaultWidth;}
    }
}
var attachexts = new Array();
var attachwh = new Array();
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
function $1(id) {
	return document.getElementById(id);
}
function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}
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 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 insertAttach(id,type) 
{
    if(type==1)
    {
        var localimgpreview = '';
        var path = $1('pic_' + id).value;
        var extensions = 'jpg,gif,png,jpeg,bmp';
        var ext = path.lastIndexOf('.') == -1 ? '' : path.substr(path.lastIndexOf('.') + 1, path.length).toLowerCase();
        var re = new RegExp("(^|\\s|,)" + ext + "($1|\\s|,)", "ig");
        var localfile = $1('pic_' + id).value.substr($1('pic_' + id).value.replace(/\\/g, '/').lastIndexOf('/') + 1);
        if(path == '') 
        {
            return;
        }
        if(extensions != '' && (re.exec(extensions) == null || ext == '')) 
        {
            alert('对不起，不支持上传此类扩展名的附件。');
            return;
        }
        newnode = $1('attachbodyhidden').firstChild.cloneNode(true);
        tags = findtags(newnode,'input');
        for(i in tags)
        {
            if(tags[i].name == 'localpicid') 
            {
                tags[i].value = id;
            }
        }
        tags = findtags(newnode, 'span');
        for(i in tags) 
        {
            if(tags[i].id == 'localpic[]') 
            {
                tags[i].id = 'localpic_' + id;
            }
        }
        $1('attachbody').appendChild(newnode);
        attachexts[id] = is_ie ? 2 : 1;
        var err = false;
        if(attachexts[id] == 2) 
        {
            $1('img_hidden').alt = id;
            try 
            {
	            $1('img_hidden').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = 'image';
            } 
            catch (e) 
            {err = true;}
            try 
            {
                $1('img_hidden').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = $1('pic_' + id).value;
            } 
            catch (e) 
            {
			    alert('无效的图片文件。');
			    delAttach(id);
			    err = true;		
                return;
            }
            var wh = {'w' : $1('img_hidden').offsetWidth, 'h' : $1('img_hidden').offsetHeight};
            var aid = $1('img_hidden').alt;
            if(wh['w'] >= thumbwidth || wh['h'] >= thumbheight) 
            {
                wh = thumbImg(wh['w'], wh['h']);
            }
            attachwh[id] = wh;
            $1('img_hidden').style.width = wh['w']
            $1('img_hidden').style.height = wh['h'];
            try 
            {
	            $1('img_hidden').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = 'scale';
            }
            catch (e)
            {
            }
            if (err == true)
            {
	            $1('img_hidden').src = $1('pic_' + id).value;
            }
        }
        $1('localpic_' + id).innerHTML = '<a href="###delAttach" onclick="delAttach(' + id + ',1)">[删除]</a> <a href="###insertAttach" title="点击这里将本附件插入帖子内容中当前光标的位置" onclick="insertAttachtext(' + id + ',1, ' + err + ');return false;">[插入]</a> ' +
        (attachexts[id] == 2 ? '<span id="localimgpreview_' + id + '"> <span class="smalltxt">[' + id + ']</span> <a href="###attachment" onclick="insertAttachtext(' + id + ',1, ' + err + ');return false;">' + localfile + '</a></span>' : '<span class="smalltxt">[' + id + ']</span> ' + localfile);
        $1('pic_' + id).style.display = 'none';
        insertAttachtext(id,1, err);
        addPic();
    }
    if(type==2)
    {
        var localimgpreview = '';
        var path = $1('file_' + id).value;
        var extensions = 'zip,rar,jar,sis,sisx,txt,umd';
        var ext = path.lastIndexOf('.') == -1 ? '' : path.substr(path.lastIndexOf('.') + 1, path.length).toLowerCase();
        var re = new RegExp("(^|\\s|,)" + ext + "($1|\\s|,)", "ig");
        var localfile = $1('file_' + id).value.substr($1('file_' + id).value.replace(/\\/g, '/').lastIndexOf('/') + 1);
        if(path == '') 
        {
            return;
        }
        if(extensions != '' && (re.exec(extensions) == null || ext == '')) 
        {
            alert('对不起，不支持上传此类扩展名的附件。');
            return;
        }
        newnode = $1('filebodyhidden').firstChild.cloneNode(true);
        tags = findtags(newnode,'input');
        for(i in tags)
        {
            if(tags[i].name == 'localfileid') 
            {
                tags[i].value = id;
            }
        }
        tags = findtags(newnode, 'span');
        for(i in tags) 
        {
            if(tags[i].id == 'localfile[]') 
            {
                tags[i].id = 'localfile_' + id;
            }
        }
        $1('filebody').appendChild(newnode);
        attachexts[id] = is_ie ? 2 : 1;
        $1('localfile_' + id).innerHTML = '<a href="###delAttach" onclick="delAttach(' + id + ',2)">[删除]</a> <a href="###insertAttach" title="点击这里将本附件插入帖子内容中当前光标的位置" onclick="insertAttachtext(' + id + ',2, ' + err + ');return false;">[插入]</a> ' +
        (attachexts[id] == 2 ? '<span id="localimgpreview_' + id + '"> <span class="smalltxt">[' + id + ']</span> <a href="###attachment" onclick="insertAttachtext(' + id + ',2, ' + err + ');return false;">' + localfile + '</a></span>' : '<span class="smalltxt">[' + id + ']</span> ' + localfile);
        $1('file_' + id).style.display = 'none';
        //insertAttachtext(id,2, err);
        addFile();
    }
}
function insertAttachtext(id,type,iserr) 
{
    if(!attachexts[id]) 
    {
        return;
    }
    if(type == 1) 
    {
        insertCode='<br />[localimg]' + id + '[/localimg]<br />';
        tinyMCE.execCommand("mceInsertContent","txtmessage",insertCode);
    } 
    if(type==2)
    {
        insertCode='<br />[local]' + id + '[/local]<br />';
        tinyMCE.execCommand("mceInsertContent","txtmessage",insertCode);
    }
}
function thumbImg(w, h) 
{
    var x_ratio = thumbwidth / w;
    var y_ratio = thumbheight / h;
    var wh = new Array();
    if((x_ratio * h) < thumbheight) 
    {
        wh['h'] = Math.ceil(x_ratio * h);
        wh['w'] = thumbwidth;
    } 
    else 
    {
        wh['w'] = Math.ceil(y_ratio * w);
        wh['h'] = thumbheight;
    }
    return wh;
}
function addPic() 
{
    if ($1('attachupbody').childNodes.length > 100 - 1)
    {
	    return;
    }
    var id = aid;
    newnode = document.createElement("<input type=\"file\" name=\"postpic\" id='pic_"+id+"' onchange='insertAttach("+id+",1)' />");
    newnode.unselectable = 'on';
    aid++;
    $1('attachupbody').appendChild(newnode);
}
function addFile()
{
    if ($1('filebody').childNodes.length > 100 - 1)
    {
	    return;
    }
    var id = fid;
    newnode = document.createElement("<input type=\"file\" name=\"postfile\" id='file_"+id+"' onchange='insertAttach("+id+",2)' />");
    newnode.unselectable = 'on';
    fid++;
    $1('fileupbody').appendChild(newnode);
}
function delAttach(id,type) 
{
    if(type==1)
    {
        var curattlength = $1('attachbody').childNodes.length;
        $1('attachupbody').removeChild($1('pic_'+id));
        $1('attachbody').removeChild($1('localpic_' + id).parentNode.parentNode);
        //$1('attachbody').innerHTML == '' && addAttach();
        if (curattlength == 100 && $1("pic_" + (aid-1)).value != "")
        {
	        addPic();
        }
	}
	if(type==2)
	{
	    var curattlength = $1('filebody').childNodes.length;
        $1('filebody').removeChild($1('file_' + id).parentNode.parentNode);
        //$1('filebody').innerHTML == '' && addAttach();
        if (curattlength == 100 && $1("file_" + (fid-1)).value != "")
        {
	        addFile();
        }
	}  
}
//多图上传
	
	function show_hideAttach(controlObj)
	{
	    if(document.getElementById(controlObj).style.display=="none")
	    {
	        document.getElementById(controlObj).style.display="";
	    }
	    else
	    {
	        document.getElementById(controlObj).style.display="none";
	    }
	}
	//多图上传
	function AttachDiv(controlObj,objother)
	{
	    if($1(controlObj).style.display=="none")
	    {
	        $1(controlObj).style.display="";
//	        $1(objother).style.display="none";
//	        $1(controlObj).style.Left=document.body.scrollWidth/2;
	    }
	    else
	    {
	        document.getElementById(controlObj).style.display="none";
	    }
	}
	function show_hide(obj,controlObj)
	{
	if(obj.checked)
	{
	    document.getElementById(controlObj).style.display="";
	}
	else
	{
	    document.getElementById(controlObj).style.display="none";
	}
	}
    function InsertAttach(aid,type,Attachpath,flag)
	{
	    var typeimg = "jpg,png,gif,jpeg,bmp,image/jpg,image/jpeg,image/gif,image/png,image/bmp";
	    var insertCode="";
	    if(typeimg.indexOf(type)==-1)
	    {
	        insertCode="[attach]"+aid+"[/attach]<br />";
	    }
	    else
	    {
            insertCode="[attachimg]"+aid+"[/attachimg]<br />";
            
	    }
	    tinyMCE.execCommand("mceInsertContent","txtmessage",insertCode);
	}
//层拖动
var posX;
var posY;      
var fdiv;
function downdiv(e,obj)
{
	fdiv=$1(obj);
	if(!e)e=window.event
	posX = e.clientX - parseInt(fdiv.style.left);
    posY = e.clientY - parseInt(fdiv.style.top);
	document.onmousemove = mousemove; 
}
document.onmouseup = function()
{
    document.onmousemove = null;
}
function mousemove(ev)
{
    if(ev==null) ev = window.event;//如果是IE
    fdiv.style.left = (ev.clientX - posX) + "px";
    fdiv.style.top = (ev.clientY - posY) + "px";
}

/*从showtopic页面中移植过来的*/
function show(obj)
{
var item=document.getElementById("dope");
var htmltitle=document.getElementById("txtTitle");
var topictitle=document.getElementById("lblTitle");
if (obj=='admin')
{
item.style.display="block";
}
else
{
item.style.display="none";
if (htmltitle!=null && topictitle!=null)
{
htmltitle.value="回复："+topictitle.innerHTML;
}
}
}
function getTitle(postsid,obj,users)
{
var htmltitle=document.getElementById("txtTitle");
var topictitle=document.getElementById("lblTitle");
var item=document.getElementById("hdpostsid");
if (obj!="" && users!="" && htmltitle!=null)
{
form1.txtContent.focus();
htmltitle.value="回复："+obj+"F "+users+" 的帖子";
}
else if (htmltitle!=null && topictitle!=null)
{
form1.txtContent.focus();
htmltitle.value="回复："+topictitle.innerText;
}
if (postsid!=0 && item!=null)
{
item.value=postsid;
}
htmltitle.readOnly=true;
}
function copyToClipBoard(str){ 
var clipBoardContent=""; 
clipBoardContent=str; 
window.clipboardData.setData("Text",clipBoardContent); 
}
function CtrlEnt()
{
if(event.ctrlKey && window.event.keyCode==13)
{
document.form1.btnSend.click();
}
}
