<%if {attachsize}>0%>
	<%if {attachextensions}!=""%>
<script type="text/javascript">
function ShowFormatBytesStr(bytes)
{
	if(bytes > 1073741824)
	{
		document.write((Math.round((bytes/1073741824)*100)/100).toString()+' GB');
	}
	else if(bytes > 1048576)
	{
		document.write((Math.round((bytes/1048576)*100)/100).toString()+' MB');
	}
	else if(bytes > 1024)
	{
		document.write((Math.round((bytes/1024)*100)/100).toString()+' KB');
	}
	else
	{
		document.write(bytes.toString()+' 字节');
	}
}
</script>

<table cellspacing="4" cellpadding="6" border="0">
<tr class="row1">
<td width="300"><img src="templates/{templatepath}/images/attachment.gif" alt="附件"/> 上传附件</td>
<td align="center">阅读权限</td>
<td align="center">描述</td></tr>
<tbody id="attachbodyhidden" style="display:none"><tr class="row1">
<td width="300">
<input type="file" name="postfile" class="absmiddle colorblue" onfocus="this.className='colorfocus';" onblur="this.className='colorblue';" />
<span id="localfile[]"></span>&nbsp;
<input type="hidden" name="localid" />
</td>
<td align="center"><input type="text" name="readperm" value="0" size="1" class="colorblue" onfocus="this.className='colorfocus';" onblur="this.className='colorblue';" /></td>
<td><input type="text" name="attachdesc" size="25" class="colorblue" onfocus="this.className='colorfocus';" onblur="this.className='colorblue';" /></td>
<%if {config.enablealbum}==1 && {caninsertalbum}%>
<td><select name="albums" style="display:none">
<option value="0">请选择相册</option>
<%loop album albumlist%>
<option value="{album[albumid]}">{album[title]}</option>
<%/loop%>
</select></td>
<%/if%>
</tr></tbody>
<tbody id="attachbody"></tbody>
<tr class="altbg1"><td colspan="5">
<div class="hintinfo">单个附件大小: &nbsp;<strong><script type="text/javascript">ShowFormatBytesStr({usergroupinfo.Maxattachsize});</script></strong>&nbsp;&nbsp;今天可上传大小: <strong><script type="text/javascript">ShowFormatBytesStr({attachsize});</script></strong>&nbsp;<br />附件类型: &nbsp;<strong >{attachextensionsnosize}</strong></div>
</td></tr>
</table>

<img id="img_hidden" alt="1" style="position:absolute;top:-100000px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='image');width:400;height:300"></img>
<script type="text/javascript">
var aid = 1;
var thumbwidth = parseInt(400);
var thumbheight = parseInt(300);
var attachexts = new Array();
var attachwh = new Array();

function delAttach(id) 
{
    var curattlength = $('attachbody').childNodes.length;
    $('attachbody').removeChild($('attach_' + id).parentNode.parentNode);
    $('attachbody').innerHTML == '' && addAttach();
    if (curattlength == {config.maxattachments} && $("attach_" + (aid-1)).value != "")
    {
	    addAttach();
    }
	if ($('localimgpreview_' + id + '_menu'))
	{
		document.body.removeChild($('localimgpreview_' + id + '_menu'));
	}    
}

function addAttach() 
{
    if ($('attachbody').childNodes.length > {config.maxattachments} - 1)
    {
	    return;
    }
    newnode = $('attachbodyhidden').firstChild.cloneNode(true);
    var id = aid;
    var tags;
    tags = findtags(newnode, 'input');
    for(i in tags) 
    {
        if(tags[i].name == 'postfile') 
        {
            tags[i].id = 'attach_' + id;
            tags[i].onchange = function() 
            {
	            insertAttach(id);
            };
            tags[i].unselectable = 'on';
        }
        if(tags[i].name == 'localid') 
        {
            tags[i].value = id;
        }
    }
    tags = findtags(newnode, 'span');
    for(i in tags) 
    {
        if(tags[i].id == 'localfile[]') 
        {
            tags[i].id = 'localfile_' + id;
        }
    }
<%if {caninsertalbum}%>
    tags = findtags(newnode, 'select');
    for(i in tags)
    {
        if(tags[i].name == 'albums')
        {
            tags[i].id = 'albums' + id;
        }
    }
<%/if%>
    aid++;
    $('attachbody').appendChild(newnode);
}

addAttach();

function insertAttach(id) 
{
    var localimgpreview = '';
    var path = $('attach_' + id).value;
    var extensions = '{attachextensionsnosize}';
    var ext = path.lastIndexOf('.') == -1 ? '' : path.substr(path.lastIndexOf('.') + 1, path.length).toLowerCase();
    var re = new RegExp("(^|\\s|,)" + ext + "($|\\s|,)", "ig");
    var localfile = $('attach_' + id).value.substr($('attach_' + id).value.replace(/\\/g, '/').lastIndexOf('/') + 1);

    if(path == '') 
    {
        return;
    }
    if(extensions != '' && (re.exec(extensions) == null || ext == '')) 
    {
        alert('对不起，不支持上传此类扩展名的附件。');
        return;
    }
    attachexts[id] = is_ie && in_array(ext, ['gif', 'jpg', 'jpeg', 'png', 'bmp']) ? 2 : 1;

    var err = false;
    if(attachexts[id] == 2) 
    {
        $('img_hidden').alt = id;
        try 
        {
	        $('img_hidden').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = 'image';
        } 
        catch (e) 
        {err = true;}
        try 
        {
            $('img_hidden').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = $('attach_' + id).value;
        } 
        catch (e) 
        {
			alert('无效的图片文件。');
			delAttach(id);

			err = true;		
            
            return;
        }
        var wh = {'w' : $('img_hidden').offsetWidth, 'h' : $('img_hidden').offsetHeight};
        var aid = $('img_hidden').alt;
        if(wh['w'] >= thumbwidth || wh['h'] >= thumbheight) 
        {
            wh = thumbImg(wh['w'], wh['h']);
        }
        attachwh[id] = wh;
        $('img_hidden').style.width = wh['w']
        $('img_hidden').style.height = wh['h'];
        try 
        {
	        $('img_hidden').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = 'scale';
        }
        catch (e)
        {
        }
        if (err == true)
        {
	        $('img_hidden').src = $('attach_' + id).value;
        }
        div = document.createElement('div');
        div.id = 'localimgpreview_' + id + '_menu';
        div.style.display = 'none';
        div.style.marginLeft = '20px';
        div.className = 'popupmenu_popup';
        document.body.appendChild(div);
        div.innerHTML = '<img style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=\'scale\',src=\'' + $('attach_' + id).value+'\');width:'+wh['w']+';height:'+wh['h']+'" src=\'images/common/none.gif\' border="0" aid="attach_'+ aid +'" alt="" />';
    }
    var isimg = in_array(ext, ['gif', 'jpg', 'jpeg', 'png', 'bmp']) ? 2 : 1;
    <%if {caninsertalbum}%>
    if(isimg == 2)
    {
        $('albums' + id).style.display='block';
        $('albums' + id).disabled = tempaccounts;
    }
    <%/if%>
    $('localfile_' + id).innerHTML = '<a href="###delAttach" onclick="delAttach(' + id + ')">[删除]</a> <a href="###insertAttach" title="点击这里将本附件插入帖子内容中当前光标的位置" onclick="insertAttachtext(' + id + ', ' + err + ');return false;">[插入]</a> ' +
    (attachexts[id] == 2 ? '<span id="localimgpreview_' + id + '" onmouseover="showMenu(this.id, 0, 0, 1, 0)"> <span class="smalltxt">[' + id + ']</span> <a href="###attachment" onclick="insertAttachtext(' + id + ', ' + err + ');return false;">' + localfile + '</a></span>' : '<span class="smalltxt">[' + id + ']</span> ' + localfile);
    $('attach_' + id).style.display = 'none';
    if(isimg == 2)
        insertAttachtext(id, err);
    addAttach();
}

function insertAttachtext(id, iserr) 
{
    if(!attachexts[id]) 
    {
        return;
    }
    if(attachexts[id] == 2) 
    {
        bbinsert && wysiwyg && iserr == false ? insertText($('localimgpreview_' + id + '_menu').innerHTML, false) : AddText('[localimg=' + attachwh[id]['w'] + ',' + attachwh[id]['h'] + ']' + id + '[/localimg]');
    } 
    else 
    {
        bbinsert && wysiwyg ? insertText('[local]' + id + '[/local]', false) : AddText('[local]' + id + '[/local]');
    }
}

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;
}
</script>


	<%else%>
		<div class="hintinfo">							
				你没有上传附件的权限.
		</div>
	<%/if%>
<%else%>
	<div class="hintinfo">
		<%if {usergroupinfo.Maxsizeperday}>0 && {usergroupinfo.Maxattachsize}>0%>
			你目前可上传的附件大小为 0 字节.
		<%else%>
			你没有上传附件的权限.
		<%/if%>
	</div>
<%/if%>
