<hr class="line" size="0">
<table cellspacing="0" cellpadding="6" border="0">
<tr class="row1">
<td width="200"><img src="images/attachicons/common.gif" border="0" alt="" /> {lang attachment_new}</td>
<!--{if $allowsetattachperm}--><td align="center">{lang readperm}</td><!--{/if}-->
<!--{if $maxprice}--><td align="center">{lang price}</td><!--{/if}-->
<td align="center">{lang description}</td></tr>
<tbody id="attachbodyhidden" style="display:none"><tr class="row1">
<td width="250">
<input type="file" name="attach[]" class="absmiddle">
<span id="localfile[]"></span>&nbsp;
<input type="hidden" name="localid[]">
</td>
<!--{if $allowsetattachperm}--><td align="center"><input type="text" name="attachperm[]" value="0" size="1"></td><!--{/if}-->
<!--{if $maxprice}--><td align="center"><input type="text" name="attachprice[]" value="0" size="1">{$extcredits[$creditstrans][unit]}</td><!--{/if}-->
<td><input type="text" name="attachdesc[]" size="25"></td>
</tr></tbody>
<tbody id="attachbody"></tbody>
<tr class="altbg1"><td colspan="5">
	{lang attachment_size}: <!--{if $maxattachsize_kb}-->{lang lower_than} $maxattachsize_kb kb <!--{else}-->{lang size_no_limit}<!--{/if}--><br>
	<!--{if $attachextensions}-->{lang attachment_allow_exts}: $attachextensions<br><!--{/if}-->
	<!--{if $maxprice}-->{lang price}: {lang post_price_comment}{$extcredits[$creditstrans][title]}<!--{if $maxincperthread}-->{lang post_price_income_comment}<!--{/if}--><!--{if $maxchargespan}-->{lang post_price_charge_comment}<!--{/if}--><br><!--{/if}-->
</td></tr>
</table>
<div id="img_hidden" alt="1" style="position:absolute;top:-100000px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='image');width:$thumbwidth;height:$thumbheight"></div>
<script type="text/javascript">
var aid = 1;
var thumbwidth = parseInt($thumbwidth);
var thumbheight = parseInt($thumbheight);
var attachexts = new Array();
var attachwh = new Array();

function delAttach(id) {
	$('attachbody').removeChild($('attach_' + id).parentNode.parentNode);
	$('attachbody').innerHTML == '' && addAttach();
	$('localimgpreview_' + id + '_menu') ? document.body.removeChild($('localimgpreview_' + id + '_menu')) : null;
}

function addAttach() {
	newnode = $('attachbodyhidden').firstChild.cloneNode(true);
	var id = aid;
	var tags;
	tags = findtags(newnode, 'input');
	for(i in tags) {
		if(tags[i].name == 'attach[]') {
			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;
		}
	}
	aid++;
	$('attachbody').appendChild(newnode);
}
addAttach();

function insertAttach(id) {
	var localimgpreview = '';
	var path = $('attach_' + id).value;
	var extensions = '$attachextensions';
	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('{lang post_attachment_ext_notallowed}');
		return;
	}
	attachexts[id] = is_ie && in_array(ext, ['gif', 'jpg', 'png', 'bmp']) && typeof supe == 'undefined' ? 2 : 1;

	if(attachexts[id] == 2) {
		$('img_hidden').alt = id;
		$('img_hidden').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = 'image';
		try {
			$('img_hidden').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = $('attach_' + id).value;
		} catch (e) {
			alert('{lang post_attachment_img_invalid}');
			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'];
		$('img_hidden').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = 'scale';
		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="" />';
	}

	$('localfile_' + id).innerHTML = '<a href="###delAttach" onclick="delAttach(' + id + ')">[{lang post_attachment_deletelink}]</a> <a href="###insertAttach" title="{lang post_attachment_insert}" onclick="insertAttachtext(' + id + ');return false;">[{lang post_attachment_insertlink}]</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 + ');return false;">' + localfile + '</a></span>' : '<span class="smalltxt">[' + id + ']</span> ' + localfile);
	$('attach_' + id).style.display = 'none';
	addAttach();
}

function insertAttachtext(id) {
	if(!attachexts[id]) {
		return;
	}
	if(attachexts[id] == 2) {
		bbinsert && wysiwyg ? 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>