//*****************************************************
//*****************************************************
//**************   R O B S O N   **********************
//*****************************************************
//*****************************************************
Range.prototype.spanText = function(tagName,attribute,vValue){

	textNodes = this.textNodes;
	attribute=attribute.split(",");
	vValue=vValue.split(",");

	if(textNodes.length){
		
		var textContainer = textNodes[0].parentNode;
	
		var elem = documentCreateXHTMLElement(tagName);

		textContainer.insertBefore(elem, textNodes[0]);
		elem.appendChild(textNodes[0]);
		textNodes[0] = elem.firstChild;

		if(textNodes.length>0){
			for(i=1; i<textNodes.length; i++)	{
				if(textContainer!=textNodes[i].parentNode){
					elem.appendChild(textNodes[i].parentNode);
				}else{
					elem.appendChild(textNodes[i]);
				}
			}
		}
		
		for(i=0;i<attribute.length;i++)
			if(attribute[i] && vValue[i]) elem.setAttribute(attribute[i],vValue[i]);
		
		textContainer = elem;
	}

	this.__restoreTextBoundaries();
	keepTxtNodes = null;
	return;

}

//*****************************************************
//*****************************************************
//**************   R O B S O N   **********************
//*****************************************************
//*****************************************************
Range.prototype.removeSpanText = function(tagName,attribute){

	if(!keepTxtNodes)
		textNodes = this.textNodes;
	else
		textNodes = keepTxtNodes;

	if(textNodes.length){
		for(i=0; i<textNodes.length; i++)	{
			var textContainer = textNodes[i].parentNode;
		
			if(textContainer.tagName == tagName){
				if(attribute) textContainer.removeAttribute(attribute);
				textContainer.setAttribute('tyconremovestyle','true'); //Köschattribut setzen
			}
		}
	}

	//alle html-elemente mit gesetztem löschattribut löschen
	if(divId){
		content=document.getElementById('tyep'+divId).innerHTML;
		temp=content;
		content=content.replace(/\r\n/igm,' ');
	
		re=new RegExp('<'+tagName+'[^>]*tyconremovestyle="true"[^>]*>(.*?)</'+tagName+'>','gim');
		content=content.replace(re,"$1");		
		document.getElementById('tyep'+divId).innerHTML=content;
	}
	
	this.__restoreTextBoundaries();
	keepTxtNodes = null;
	return;
}

//*****************************************************
//*****************************************************
//**************   R O B S O N   **********************
//*****************************************************
//*****************************************************
Range.prototype.pasteHTML = function(vValue){

	var elem = document.createElement('tycontent');
	
	elem.setAttribute('tyconremovestyle','true');
	elem.innerHTML=vValue;

	this.insertNode(elem);
	
	if(divId){
		content=document.getElementById('tyep'+divId).innerHTML;
		content=content.replace(/\r\n/igm,' ');
	
		re=new RegExp('<tycontent[^>]*tyconremovestyle="true"[^>]*>(.*?)</tycontent>','gim');
		content=content.replace(re,"$1");		
		document.getElementById('tyep'+divId).innerHTML=content;
	}
	
	this.__restoreTextBoundaries();
	keepTxtNodes = null;
	return;
}

/**
 * "Delete" for selected XHTML represents three behaviors:
 * - if range isn't collapsed then delete contents of the range - treat table contents properly (see code for behavior)
 * - if range is collapsed
 *   - if at start of line then merge line with previous line if there is one and this is appropriate
 *   - otherwise delete character or element before the selected point in the line
 *
 * Note: this is an XHTML compliant deletion. It is driven solely by CSS settings. This works for XHTML selections but
 * it is unlikely to work for semantically rich and restrictive XML. Deletion of an XML document would have to pay 
 * attention to that document's semantics.
 */
Selection.prototype.deleteSelection = function()
{
	// delete in place ...
	if(this.isCollapsed)
	{
		var ip = this.insertionPoint(true);

		if(!ip)
			return false;

		var result = ip.deletePreviousInLine();

		if(!result)
		{
			var line = ip.line;
			ip = line.deleteStructure();
		}

		this.collapse(ip.ipNode, ip.ipOffset);
		return true;
	}

	// delete selection
	var cssr = this.getEditableRange(false);

	if(!cssr)
		return false;

	cssr.extractContentsByCSS();

	this.removeAllRanges();
	this.addRange(cssr.cloneRange());

	return true;
}