function replaceSelection( textarea, text, doc) {
	if( textarea.setSelectionRange) {
		selection = textarea.value.substring( textarea.selectionStart, textarea.selectionEnd);
		textarea.value = textarea.value.substring( 0, textarea.selectionStart) +
			text +
			textarea.value.substring( textarea.selectionEnd, textarea.value.length);
	} else if( document.selection) {
		if( doc==null) {
			doc = self.opener.document;
		}
		range = doc.selection.createRange();
		if( range.text && range.parentElement()==textarea) {
			range.text = text;
		}
	}
}

function getSelection( textarea, doc) {
	selection = "";
	if( textarea.setSelectionRange) {
		selection = textarea.value.substring( textarea.selectionStart, textarea.selectionEnd);
	} else if( document.selection) {
		if( doc==null) {
			doc = window.opener.document;
		}
		range = doc.selection.createRange();
		if( range.text && range.parentElement()==textarea) {
			selection = range.text;
		}
	}
	return selection;
}

function insertAtCaret( textElement, text, doc) {
	if( doc==null) {
		doc = window.opener.document;
	}
	textElement.focus();
	if( textElement.createTextRange) {
		textElement.caretPos = doc.selection.createRange().duplicate();
		var caretPos = textElement.caretPos;
		caretPos.text = caretPos.text.charAt( caretPos.text.length-1) == ' ' ? text + ' ' : text;
	} else {
		replaceSelection( textElement, text, doc);
	}
}

function displayLength( component, countField) {
	setCont( "id", countField, null, component.value.length);
	return true;
}

function checkLength( component, max )
{
  if( component.value.length>max )
  {
	  alert( "Ihre Anzeige beinhaltet mehr als 315 Zeichen!\n\nBitte kürzen Sie den Text." );
	  return( false );
  }
  else
	  return( true );
}

function makeList( textarea) {
	selection = getSelection( textarea, document);
	if( selection != '') {
		replaceSelection( textarea, "<liste>\n" + selection + "\n</liste>", document);
	}
}

function formatSelection( textarea, style) {
	selection = getSelection( textarea);
	if( selection.length==0) return;
	styles = getStyles( style);
	if( styles.length==0) return;
	replaceSelection( textarea, '<format stil="' + styles + '">' + selection + '</format>');
	window.close();
}

function getStyles( style) {
	var styles = '';
	for( i=0; i<style.length; i++) {
		if( style[ i].checked) {
			if( styles.length>0) styles += ',';
			styles += style[ i].value;
		}
	}
	return styles;
}

function contentLink( textarea, id, type) {
	selection = getSelection( textarea);
	if( selection != '') {
		replaceSelection( textarea,
			'<link-intern typ="webnix.article" id="' + id + '">' + selection +
			'</link-intern>', self.opener.document);
	}
}

function webLink( textarea, url, target) {
	selection = getSelection( textarea);
	if( selection != '') {
		targetAttr = target.length>0 ? 'target="' + target + '" ' : '';
		replaceSelection( textarea,
			'<link-extern ' + targetAttr + 'url="' + url + '">' + selection +
			'</link-extern>');
	}
}

function insertImage( textarea, image, align) {
	selection = getSelection( textarea);
	alignAttr = align!='' ? ' ausrichtung="' + align + '"' : '';
	insertAtCaret( textarea,
		'<bild datei="' + image + '"' + alignAttr + '>' + selection +
		'</bild>');
}



