function makeThread(post_id){
	if(isNaN(post_id)||post_id==0){return false;}
	if(document.getElementById('mt_'+post_id)){
		$j.scrollTo('#mt_'+post_id, 'normal');
		return false;
	}
	$j('#mtl' + post_id + ' img').fadeTo('fast', 0.6);
	new Ajax.Request(_f('makethread'),{
		method:'post',
		parameters:'pid='+post_id+'&mode=t',
		onSuccess:function(xh){
			var resp=xh.responseText;
			if(resp.substr(0,5)=='error'){
				alert(_("Error")+':\n'+resp.substring(6));
				$j('#mtl' + post_id + ' img').fadeIn('fast');
				return false;
			}else{
				var data;
				eval('data = ' + resp);
				var content=printForm(data);
				var newItem = document.createElement('div');
				newItem.id='mt_'+post_id;
				newItem.className='makethreaddiv';
				newItem.innerHTML=content;
				newItem.style.display='none';
				$('p'+post_id).appendChild(newItem);
				styleCheckboxes();
				$j('#mt_'+post_id).fadeIn('fast');
				$j.scrollTo('#mt_'+post_id, 'normal');
				$j('#message'+post_id).focus();
				return false;
			}
		}
	});
}
function saveThread(post_id){
	if(isNaN(post_id)||post_id==0){return false;}

	disableButton('sendbutton'+post_id);
	disableButton('cancelbutton'+post_id);

	var title = $F('topic_title'+post_id);
	var text = $F('message'+post_id);
	var cat = $F('newcat'+post_id);
	var tags = '';
	var errors = new Array();
  	if(title.length<2){errors.push(_("Please enter a title."));}
	if(document.getElementById('topic_tags'+post_id)){tags = $F('topic_tags'+post_id);}
	if(text.length<1){ errors.push(_("Please enter a text.")); }

	if(errors.length>0){printErrors(errors);enableButton('sendbutton'+post_id);enableButton('cancelbutton'+post_id);return false;}

	var obm=getCheckBoxValue('optbm'+post_id);
	var oabo=getCheckBoxValue('optabo'+post_id);
	var ohide=getCheckBoxValue('opthide'+post_id);

	var params = {
		pid:post_id,
		title:title,
		tags:tags,
		cid:cat,
		text:text,
		bm:obm,
		abo:oabo,
		hide:ohide,
		mode:'s'
	};
	var h = $H(params);
	new Ajax.Request(_f('makethread'),{
		method:'post',
		parameters:h.toQueryString(),
		onSuccess:function(xh){
			var resp=xh.responseText;
			if(resp.substr(0,5)=='error'){
				alert(_("Error")+':\n'+resp.substring(6));
				enableButton('sendbutton'+post_id);
				enableButton('cancelbutton'+post_id);
				return false;
			}else{
				var newurl=resp;
				location.href=newurl;
			}
		}
	});
}
function closeForm(post_id){
	if(document.getElementById('mt_'+post_id)){
			$j('#mt_'+post_id).fadeOut('fast', removeMe);
			$j.scrollTo('#post' + post_id, 'normal');
			$j('#mtl' + post_id + ' img').fadeTo('fast', 1);
	}
}
function printForm(data){
	var text=data.text;
	var cats=data.cats;
	var tags=data.tags;
	var showtags=data.showtags;
	var pid=data.pid;
	var output = '<div class="replycontainer"><div class="replyleft"><b>Neues Thema</b></div><div class="replyright"><form onsubmit="saveThread('+pid+');return false;" name="postform'+pid+'" id="postform'+pid+'" method="post" action=""><input maxlength="45" class="newtitleform" id="topic_title'+pid+'" value="Titel" tabindex="4" onfocus="if(this.value==\'Titel\')this.value=\'\';" onblur="if(this.value==\'\')this.value=\'Titel\';" type="text"><br/>'+data.formatting+'<div id="divsmiliesmessage'+pid+'" style="display:none;" class="divsmilies"></div><textarea class="replyform" id="message'+pid+'" rows="15" cols="20">'+text+'</textarea><br/><br/><h6>Kategorie</h6><select class="pulldown" id="newcat'+pid+'" onchange="switchTagfield('+pid+');return false;"><option value="#" selected="selected">Kategorie w&auml;hlen...</option>'+cats+'</select>';
	if(showtags==1){
		output+='<div id="tagDIV'+pid+'"><br/><h6>Tags</h6>';
		if(tags!=''){
			output+='<div id="autotags'+pid+'" class="alert">M&ouml;gliche Tags: '+tags+'</div>';
		}
		output+='<input class="tagsform" id="topic_tags'+pid+'" value="" tabindex="6" type="text"><br/><div class="taginfo">(Die Angabe von Tags ist optional. Beispiel: tag1 tag2 "tag drei" tag4)</div></div>';
	}
	output+='<br/><h6>Optionen</h6><div class="newthreadoptions"><label for="opthide'+pid+'"><input id="opthide'+pid+'" name="opthide'+pid+'" value="1" type="checkbox" />Thema verbergen</label><label for="optabo'+pid+'"><input id="optabo'+pid+'" name="optabo'+pid+'" value="1" type="checkbox" />Thema abonnieren</label><label for="optbm'+pid+'"><input id="optbm'+pid+'" name="optbm'+pid+'" value="1" type="checkbox" />Thema bookmarken</label></div><br/><br/><input id="cancelbutton'+pid+'" type="button" class="submit" value="Abbrechen" onclick="closeForm('+data.pid+');" /><input id="sendbutton'+pid+'" type="submit" class="submit" value="Abschicken" /></form></div></div><br/><br/>';
	return output;
}
var old_cat_tags='';
function switchTagfield(post_id){
	var elem=$('newcat'+post_id);
	var cat=elem.options[elem.selectedIndex].value;
	new Ajax.Request(_f('makethread'),{
		method:'post',
		parameters:'mode=f&cid='+cat,
		onSuccess:function(xh){
			var resp=xh.responseText;
			if(resp=='error'){
				alert(_("An error occurred."));
				return false;
			}else{
				eval('resp = ' + resp);
				var ok=resp.ok;
				var tags=resp.tags;
				if(document.getElementById('tagDIV'+post_id)){
					if(ok==1){
						if(document.getElementById('autotags'+post_id)){
							if($('autotags'+post_id).innerHTML!=''){
								Element.show('autotags'+post_id);
							}
						}
						var tt=trim($('topic_tags'+post_id).value.replace(new RegExp(old_cat_tags,"g"), ''));
						if(tt!=''){tags=' '+tags;}
    					$('topic_tags'+post_id).value=tt+tags+' ';
    					old_cat_tags=tags;
						Element.show('topic_tags'+post_id);
						Element.show('tagDIV'+post_id);
					}else{
						Element.hide('tagDIV'+post_id);
						if(document.getElementById('autotags'+post_id)){
							Element.hide('autotags'+post_id);
						}
					}
				}
			}
		}
	});
}