var colour;
var htmlOn;
var textAreaName = 'message';
//initialise RTE editor
function initialiseWebWizRTE(){
	var textArea = document.getElementById(textAreaName);
	var editor = document.getElementById('WebWizRTE').contentWindow.document;
	function initIframe(){
		editor.addEventListener('keypress', editorEvents, true);
		editor.addEventListener('mousedown', editorEvents, true);
		document.addEventListener('mousedown', hideIframes, true);
		editor.designMode = 'on';
	}
	setTimeout(initIframe, 300);
	
	//resetting the form
	textArea.form.onreset = function(){
		if (window.confirm('Advarsel: Alle formular data mistes!!')){
	 		editor.body.innerHTML = '';
	 		return true;
	 	}
	return false;
	}
}
//Create RTE toolbar
function WebWizRTEtoolbar(formName){
	document.writeln('');
	document.writeln('');
	document.writeln('');
	document.writeln('
');
}
//Function to format text in the text box
function FormatText(command, option){	document.getElementById('WebWizRTE').contentWindow.document.execCommand("useCSS", false, option);
	var editor = document.getElementById('WebWizRTE');
	//Show iframes
	if ((command == 'forecolor') || (command == 'backcolor') || (command == 'hilitecolor') || (command == 'fontname') || (command == 'formatblock') || (command == 'fontsize')){
		parent.command = command;
		buttonElement = document.getElementById(command);
		switch (command){
			case 'fontname': iframeWin = 'fontSelect'; break;
			case 'formatblock': iframeWin = 'formatFont'; break;
			case 'fontsize': iframeWin = 'textSize'; break;
			default: iframeWin = 'colourPalette';
		}
		editor.contentWindow.focus()
		document.getElementById(iframeWin).style.left = getOffsetLeft(buttonElement) + 'px';
		document.getElementById(iframeWin).style.top = (getOffsetTop(buttonElement) + buttonElement.offsetHeight) + 'px';
		if (document.getElementById(iframeWin).style.visibility=='visible'){
			hideIframes();
		}else{
			hideIframes();
			document.getElementById(iframeWin).style.visibility='visible';
		}
		var selectedRange = editor.contentWindow.document.selection;
		if (selectedRange != null){
			range = selectedRange.createRange();
		}
	}
	//Paste for AppleWebKit (Safari & Chrome)
	else if ((navigator.userAgent.indexOf('AppleWebKit') > 0) & (command == 'paste')){
	
		alert('Dine browser-indstillinger tillader ikke editor to invoke \'' + command + '\' operationer. \nBenyt venligst Tastatur-genveje \(Windows brugere: Ctrl + v, Mac brugere: Apple + v\)')
	}
	
	//Cut, copy, paste for Gecko
	else if ((command == 'cut') || (command == 'copy') || (command == 'paste')){
		try{
	  		editor.contentWindow.focus()
	  		editor.contentWindow.document.execCommand(command, false, option);
		}catch(exception){
			switch(command){
				case 'cut': keyboard = 'x'; break;
				case 'copy': keyboard = 'c'; break;
				case 'paste': keyboard = 'v'; break;
			}
			alert('Dine browser-indstillinger tillader ikke editor to invoke \'' + command + '\' operationer. \nBenyt venligst Tastatur-genveje \(Windows brugere: Ctrl + ' + keyboard + ', Mac brugere: Apple + ' + keyboard + '\)')
		}
	}
	else{
	  	editor.contentWindow.focus();
	  	editor.contentWindow.document.execCommand(command, false, option);
	}
	  	editor.contentWindow.focus()
}
//Function to initialise commands
function initialiseCommand(selection){
	var editor = document.getElementById('WebWizRTE')
	editor.contentWindow.document.execCommand(parent.command, false, selection);
	editor.contentWindow.focus();
	hideIframes();
}
//Iframe top offset
function getOffsetTop(elm){
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent){
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetTop;
}
//Iframe left offset
function getOffsetLeft(elm){
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent){
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}
//Function to hide iframes
function hideIframes(){
	if (document.getElementById('colourPalette').style.visibility=='visible'){document.getElementById('colourPalette').style.visibility='hidden';}
	if (document.getElementById('fontSelect').style.visibility=='visible'){document.getElementById('fontSelect').style.visibility='hidden';}
	if (document.getElementById('textSize').style.visibility=='visible'){document.getElementById('textSize').style.visibility='hidden';}
}
//Function to perform spell check
function checkspell(){
	if (confirm('\'SpellBound 0.7.0+\' spelling checker skal være installeret for at benytte denne feature. \nClick OK for at komme til \'SpellBound\' download page.')){
		window.open('http://spellbound.sourceforge.net/install','DownLoad', '');
	}
}
//Run Editor Events
function editorEvents(evt){
	var keyCode = evt.keyCode ? evt.keyCode : evt.charCode;
	var keyCodeChar = String.fromCharCode(keyCode).toLowerCase();
  	//Keyboard shortcuts
  	if (evt.type=='keypress' && evt.ctrlKey){
  		var kbShortcut;
  		switch (keyCodeChar){
			case 'b': kbShortcut = 'bold'; break;
			case 'i': kbShortcut = 'italic'; break;
			case 'u': kbShortcut = 'underline'; break;
			case 's': kbShortcut = 'strikethrough'; break;
			case 'i': kbShortcut = 'italic'; break;
		}
		if (kbShortcut){
			FormatText(kbShortcut, '');
			evt.preventDefault();
			evt.stopPropagation();
		}
	}
	hideIframes();
	return true;
}
//Function to add emoticon
function AddEmoticon(iconItem){
	editor = document.getElementById('WebWizRTE');
	img = editor.contentWindow.document.createElement('img');
	img.setAttribute('src', iconItem.id);
	img.setAttribute('border', '0');
	img.setAttribute('alt', iconItem.title);
	img.setAttribute('align', 'absmiddle');
	try{
		insertElementPosition(editor.contentWindow, img);
	}catch(exception){
		alert('Fejl ved indsætning af objekt i nuværende lokation.');
		editor.contentWindow.focus();
	}
	editor.contentWindow.focus();
}