function getXMLHttpObjectPreview() {
	try { 
	 xmlhttp = new XMLHttpRequest(); 
	} 
	catch (error) { 
	  try { 
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	  } catch (error) {
		return false; 
	  } 
	}
}
//Informationen senden
function sendDataPreview(text, htmlallowed, layout, clan_id, area) {

	if(text != "") {
		document.getElementById('preview').style.display = 'inline';
	} else {
		document.getElementById('preview').style.display = 'none';
	}
	
	//& Zeichen umwandeln, später zurück umwandeln
	text = text.replace(/&/g, "amp-kmu-amp");
	text = text.replace(/\+/g, "amp-plus-amp");
	
	//Objekt erzeugen
	if(xmlhhtp = getXMLHttpObjectPreview() == false) {
		//alert("Fehler beim Anlegen des XMLHttpObject");
		return;
	}
	//Daten senden & Zeitstempel time anhängen. Daten werden sonst nur einmal gesendet (bei Änderung)
	d = new Date();
	time = d.getTime();
	xmlhttp.open("POST", "modules/includes/ajax_convertCode.inc.php");
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	var requestvars = 'text='+encodeURI(text)+'&htmlallowed='+htmlallowed+'&layout='+layout+'&clan_id='+clan_id+'&area='+area; 
	xmlhttp.send(requestvars);
	//Eventhandler
	xmlhttp.onreadystatechange = handleHttpStatePreview;
} 
//Wert von readyState prüfen und Funktion parseData aufrufen, sofern alles ok ist
function handleHttpStatePreview() {
if(xmlhttp.readyState == 4) {
	if(xmlhttp.status == 200) {
		parseDataPreview();
	}
	else {
		//alert("Fehler beim Abrufen der XML Daten");
	}
}
}
//Empfangene Daten abrufen und prüfen
function parseDataPreview() {
	var xmlDocument = xmlhttp.responseText;
	//Kaufmännisches & wieder zurückumwandeln
	xmlDocument = xmlDocument.replace(/amp-kmu-amp/g, "&");
	xmlDocument = xmlDocument.replace(/amp-plus-amp/g, "\+");
	xmlDocument = xmlDocument.replace(/codeheader_forum'>Quote/g, "codeheader_forum'>"+quote_language_name);
	document.getElementById('preview_text').innerHTML = xmlDocument;
	//window.scrollTo(0,100000);
	
	//Workaround für Google Chrome Browser. 
	//Div Ausblenden und kurz danach wieder einblenden, damit es an der richtigen Stelle angezeigt wird
	//document.getElementById('DivCommands1').style.display = 'none'; 
	//window.setTimeout("document.getElementById('DivCommands1').style.display = 'inline';", 10);
}

var slides = 0; //Variable hat den Wert 1, sofern das Scrollspoiler gerade ein-/ausgeblendet wird //Öffnet / schließt das Suchformular
function showSpoiler(post,feld) {
	if(slides != 1) {
		if(document.getElementById('scrollspoiler_'+post+'_'+feld).style.display == 'none') {
			slides = 1;
			new Effect.SlideDown('scrollspoiler_'+post+'_'+feld);	
			window.setTimeout("slides = 0;", 600);
		} else {
			slides = 1;
			new Effect.SlideUp('scrollspoiler_'+post+'_'+feld);
			window.setTimeout("slides = 0;", 600);
		}
	}
}  

//Globale Variable Cursorposition
var cursorposstart = 0;
var cursorposend = 0;
function addSmiley(aTag, eTag) {
	document.getElementById('smileys1').style.display = 'none';
	document.getElementById('smileys2').style.display = 'none';
	document.getElementById('postformular').style.display = 'inline';
	input.focus();	
	/* für neuere auf Gecko basierende Browser (FF)*/
	if(typeof input.selectionStart != 'undefined') {
		//gemerkte Cursorposition wieder an die entspr. Stelle im Textfeld setzen
		input.selectionStart = cursorposstart;
		input.selectionEnd = cursorposend;
	}
	//Code einfügen
	insertTags(aTag, eTag, 'mid');
}
function showSmileys() {
	if(document.getElementById('smileys1').style.display == 'none' && document.getElementById('smileys2').style.display == 'none') {
		input.focus();		
		/* für neuere auf Gecko basierende Browser (FF)*/
		if(typeof input.selectionStart != 'undefined') {
			//Cursorposition merken
			cursorposstart = input.selectionStart;
			cursorposend = input.selectionEnd;
		}
		document.getElementById('postformular').style.display = 'none';
		document.getElementById('smileys1').style.display = 'inline';
		document.getElementById('smileys2').style.display = 'none';
	} else {
		document.getElementById('smileys1').style.display = 'none';
		document.getElementById('smileys2').style.display = 'none';
		document.getElementById('postformular').style.display = 'inline';
	}
}
function showMoreCommands(wert) {
	/* für neuere auf Gecko basierende Browser (FF)*/
	input.focus();    
	if(typeof input.selectionStart != 'undefined') {
		//Cursorposition merken
		cursorposstart = input.selectionStart;
		cursorposend = input.selectionEnd;	
	}
		
	if(wert == 1) {
		new Effect.Appear('DivCommands1');
		document.getElementById('DivCommands2').style.display = 'none';
		//document.getElementById('DivCommands1').style.display = 'inline';
	} 
	if(wert == 2) {
		new Effect.Appear('DivCommands2');		
		document.getElementById('DivCommands1').style.display = 'none';
		//document.getElementById('DivCommands2').style.display = 'inline';	
	}
	
	if(typeof input.selectionStart != 'undefined') {
		//gemerkte Cursorposition wieder an die entspr. Stelle im Textfeld setzen
		input.selectionStart = cursorposstart;
		input.selectionEnd = cursorposend;	
	}
}

function show_smiles1() {
	document.getElementById('smileys1').style.display = 'inline';
	document.getElementById('smileys2').style.display = 'none';
}

function show_smiles2() {
	document.getElementById('smileys1').style.display = 'none';
	document.getElementById('smileys2').style.display = 'inline';
}

function insertHyperlink() {
	var linkurl = prompt(prompt_text_url,"http://");
	if(linkurl != "" && linkurl != null) {
		var linkname = prompt(prompt_text_link, "Link");
		if(linkname != "" && linkname != null) {
			//Tag einfügen
			insertTags('[url='+linkurl+']'+linkname,'[/url]', 'end');		
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function insertPicture() {
	var pictureurl = prompt(prompt_text_img_url,"http://");
	if(pictureurl != "" && pictureurl != null) {
		
		var picturetitle = prompt(prompt_text_img_title,"");
		if(picturetitle != "" && picturetitle != null) {
			insertTags('[img='+picturetitle+']'+pictureurl,'[/img]', 'end');
		} else {
			insertTags('[img]'+pictureurl,'[/img]', 'end');
		}
	} else {
		return false;
	}
}

function insertMail() {
	var mailaddress = prompt(prompt_text_mail,"");
	if(mailaddress != "" && mailaddress != null) {
		insertTags('[mail]'+mailaddress,'[/mail]', 'end');		
	} else {
		return false;
	}
}

//curPos ist 'end' oder 'mid'
function insertTags(aTag, eTag, curPos) {
  input.focus();
  /* für Internet Explorer */
  if(typeof document.selection != 'undefined') {
	/* Einfügen des Formatierungscodes */
	var range = document.selection.createRange();
	var insText = range.text;
	range.text = aTag + insText + eTag;
	/* Anpassen der Cursorposition */
	range = document.selection.createRange();
	/* Ende-Position des Cursors */
	if (insText.length == 0) {
	  //Cursor ans Ende setzen
	  if(curPos == 'mid') {
		  /* Cursor in die Tag-Mitte */
		  range.move('character', -eTag.length);
	  }
	} else {
	  range.moveStart('character', aTag.length + insText.length + eTag.length);    
	}
	range.select();
  }
  /* für neuere auf Gecko basierende Browser */
  else if(typeof input.selectionStart != 'undefined')
  {
	/* Einfügen des Formatierungscodes */
	var start = input.selectionStart;
	var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
	/* Anpassen der Cursorposition */
	var pos;
	if (insText.length == 0) {
	  pos = start + aTag.length;
	  //Cursor nur in die Mitte setzen, wenn keines dieser Tags
	  if(curPos == 'end') {      
		//Cursorposition ans Ende setzen
		pos = pos + eTag.length;
	  }
	} else {
	  pos = start + aTag.length + insText.length + eTag.length;
	}
	input.selectionStart = pos;
	input.selectionEnd = pos;
  }
  /* für die übrigen Browser */
  else
  {
	/* Abfrage der Einfügeposition */
	var pos;
	var re = new RegExp('^[0-9]{0,3}$');
	while(!re.test(pos)) {
	  pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
	}
	if(pos > input.value.length) {
	  pos = input.value.length;
	}
	/* Einfügen des Formatierungscodes */
	var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
	input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
  }
}
function changeNickname(text) {
	document.getElementById('preview_nickname').innerHTML = text;
}  
