// Simmetric TextEdit script
// Copyright 2005 Simon Strijbos


// ** Browser detection

doc = document;
nav = navigator.appVersion.toLowerCase();
isIE = (nav.indexOf("msie") != -1) ? true : false;		// if browsername contains "msie", return true : else return false;
isGecko = (nav.indexOf("gecko") != -1) ? true : false;	// same for "gecko"

// ** Variables for the textbox control

var mode = "html";


// ** Startup - starting the texteditbox

function TextEditInit(TheName)
{
	if (document.all) {
		var textBox = document.getElementById(TheName + "_box");
		textBox.contentEditable = "true";
		FilterCode(TheName);
	}
}


// ** Applying styles

function doStyle(styletype, target, parameter)
{
	if(mode=="html")
	{
		obj = getTextBox(target + "_box");
		obj.focus();
		var range = document.selection.createRange();
		
		if(styletype == "hyperlink")
		{
			obj.focus();
			strUrl = prompt("Voer een internetadres in:", "http://");
			range.execCommand("Unlink", false, null);
			range.execCommand("CreateLink", false, strUrl);
		}
		else if (styletype == "emaillink")
		{			
			obj.focus();
			strUrl = prompt("Voer een e-mailadres in:", "mailto:");
			range.execCommand("Unlink", false, null);
			range.execCommand("CreateLink", false, strUrl + " target=\"_new");
		}
		else
		{
			obj.focus();
			range.execCommand(styletype, false, parameter);
			obj.focus();
		}
		
		FilterCode(target);
	}
}


// ** Switching to Html-mode

function switchView(texName)
{
	textBox = getTextBox(texName + "_box");
	buttonRow = getObject(texName + "_btns");
	//switchbutton = btnId.id;
	
	if(mode=="html") //switchen naar text
	{	
		syncHtmlToText(texName);
		
		mode = "text";
		//switchbutton.innerText = "Terug naar Design-mode";
		buttonRow.style.display = "none";
	}
		
	else //switchen naar html
	{		
		mode = "html";
		//switchbutton.innerText = "Broncode bekijken";
		
		buttonRow.style.display = "block";
		syncTextToHtml(texName);
	}
}


// ** Getting the textbox object depending on the browser

function getTextBox(texName)
{
	if(isIE)
	{
		return eval(texName);
	}
	else
	{
		//return document.getElementById(texName).contentWindow;
		return document.getElementById(texName);
	}
}


// ** Getting an object of the specified ID, browser-specific

function getObject(objName)
{
	if(isIE)
	{
		return document.getElementById(objName);
	}
	else
	{
		return document.getElementById(objName);
	}
}


// ** Syncing the TextEdit with the textarea

function syncHtmlToText(texName)
{
	obj = getTextBox(texName);
	content = obj.innerHTML;
	obj.innerText = content;
}


//** Syncing the textarea with the TextEdit

function syncTextToHtml(texName)
{
	obj = getTextBox(texName);
	content = obj.innerText;
	obj.innerHTML = content;
}


// ** Cleaning unwanted tags from text

function FilterCode(target)
{
	textBox = getObject(target + "_box");
	textHolder = getObject(target);
	html = textBox.innerHTML;
	oldhtml = html;
	
	if (html.indexOf("class=Mso") >= 0) //alleen filteren als er vanuit word geplakt is
	{
		// Remove all SPAN tags
		html = html.replace(/<\/?SPAN[^>]*>/gi, "" );
		// Remove all FONT tags
		html = html.replace(/<\/?FONT[^>]*>/gi, "" );
		// Remove Class attributes
		html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");
		// Remove Style attributes
		html = html.replace(/<(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3");
		// Remove Lang attributes
		html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3");
		// Remove XML elements and declarations
		html = html.replace(/<\\?\?xml[^>]*>/gi, "");
		// Remove Tags with XML namespace declarations: <o:p></o:p>
		html = html.replace(/<\/?\w+:[^>]*>/gi, "");
		// Replace the &nbsp;
		html = html.replace(/\&nbsp\;/g, " " );
		// Transform <P> to <DIV>
		var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi");
		html = html.replace( re, "<div$2</div>" );
		
		if(html != oldhtml)
		{
			textBox.innerHTML = html;
		}
	}
	html = html.replace(/</g, "&lt;");
	textHolder.value = html;
	return false;
}


//Textarea methods
var thetxd;

function SetSafeCode(target)
{
	obj = document.getElementById(target);
	obj.form.onsubmit = SafeCode;
	thetxd = target;
}

function SafeCode(e)
{
	obj = document.getElementById(thetxd);
	obj.value = obj.value.replace(/</g, "&lt;");
}
