var formattingUsed = false;

window.onload = function() {
	if ( navigator.userAgent.indexOf('MSIE') != -1 ) {
		details.document.designMode = 'On';
	}
	else
	{
		details = document.getElementById('details').contentWindow;
		details.document.designMode = 'On';
		
		// Set frame style, don't attempt for IE
		
		detailsStyle = details.document.getElementsByTagName('body')[0].style;
		detailsStyle.background = '#3E3E3E url(\'images/form.gif\') repeat-x';
		detailsStyle.color = '#DDD';
		detailsStyle.fontFamily = 'Verdana, Arial, sans-serif';
		detailsStyle.fontSize = '11px';
	}
}

function doFormat ( action ) {
	formattingUsed = true;
	details.document.execCommand(action, false, null);
}

function setFormat ( action, value ) {
	formattingUsed = true;
	details.document.execCommand(action, false, value);
}

function createLink () {
	formattingUsed = true;
	details.document.execCommand('createlink', false, prompt('Please enter the URL of the link:', 'http://' ) );
}

function switchDetailHandler () {
	if ( document.forms[1].handleDetails.value == 'richtext' ) {
		document.getElementById('iframeInput').style.display = "none";
		document.getElementById('textareaInput').style.display = "";
		document.forms[1].handleDetails.value = 'standard';
		document.getElementById('switchlink').innerHTML = 'Switch to Advanced View';
	}
	else {
		document.getElementById('iframeInput').style.display = "";
		document.getElementById('textareaInput').style.display = "none";
		document.forms[1].handleDetails.value = 'richtext';
		document.getElementById('switchlink').innerHTML = 'Switch to Standard View';
	}
}

function setColor ( handle ) {
	formattingUsed = true;
	
	setFormat('forecolor', ( handle.value == 'CUSTOM' ) ? prompt('Enter a color or hexadecimal value.') : handle.value );
	
	handle.selectedIndex = 0;
}

function finalizeData () {
	hiddenElement = document.createElement('input');
	hiddenElement.type = 'hidden';
	hiddenElement.name = 'details';
	
	if ( ! formattingUsed ) {
		document.forms[1].handleDetails.value = 'standard';
		
		if ( navigator.userAgent.indexOf('MSIE') != -1 ) {
			hiddenElement.value = details.document.body.innerText;
		}
		else {		
			hiddenElement.value = details.document.body.textContent;
		}
	}
	else
	{
		hiddenElement.value = details.document.body.innerHTML;
	}
	
	document.forms[1].appendChild(hiddenElement);
	document.forms[1].submit();
}