var idEdit = 'F1';
var backgroundColor = "#d4d0c8";

function fncCommand(strCommand, strOption) {

	if (isNormal(idEdit)) {
		if (strOption == 'removeFormat') {
			strCommand = strOption;
			strOption = null;
		}
		if (strOption == null)
			document.frames(idEdit).document.execCommand(strCommand);
		else
			document.frames(idEdit).document.execCommand(strCommand,'',strOption);
			document.frames(idEdit).focus();
	} else {
		alert('Please switch to Normal Mode.')
	}
	document.frames(idEdit).focus()
}

function fncFormatBlock(sTag) {
	if (isNormal(idEdit)) {
		var objLink = fnGetElement(sTag, document.frames(idEdit).document.selection.createRange().parentElement())
		if (objLink != null) {
			fncCommand('formatblock','<p>');
		} else {
			fncCommand('formatblock','<' + sTag + '>');
		}
	} else {
		alert('Please switch to normal mode.')
	}
}
					
function fncCreateLink() {
	if (document.frames(idEdit).document.selection.type == "Text") {
		if (isNormal(idEdit)) {
			var objLink = fnGetElement("A", document.frames(idEdit).document.selection.createRange().parentElement())
			var strLink = prompt("Where do you want to link to?", objLink ? makeRel(objLink.href) : "http:\/\/")
			if ((strLink != null) && (strLink != "http://")) fncCommand("CreateLink", strLink)
		} else {
			alert('Please switch to Normal Mode.')
		}
	} else {
		document.frames(idEdit).focus()
	}
}
					
function fnGetElement(strTag, objStart) {
	while ((objStart != null) && (objStart.tagName != strTag))
		objStart = objStart.parentElement;
	return objStart;
}

function fncInit(iframeID) {

	with (document.frames(iframeID).document) {
		write("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"designmode.css\"></head><body>" + "</body></html>");

		designMode='On'
	}
}

function fncSwapModes() {
	with (document.frames(idEdit).document) {
		if (isNormal(idEdit)) {
			body.innerText = makeRel(body.innerHTML)
			body.style.backgroundColor = backgroundColor.toLowerCase( )
		} else {
			body.innerHTML = body.innerText
			body.style.backgroundColor = "white"
		}
	}
	document.frames(idEdit).focus()
}

function onClick(op, opstr) {

	setFocus('F1');
	if (opstr == null)
		fncCommand(op)
	else 
		fncCommand(op,opstr)
	void(0);
}

function fncPreview(strSize) {
	if (idEdit != '') {
		document.all("idPreview").style.fontSize = strSize
		if (isNormal(idEdit)) {
			document.all("idPreview").innerHTML = document.frames(idEdit).document.body.innerHTML;
		} else {
			document.all("idPreview").innerHTML = document.frames(idEdit).document.body.innerText;
		}
	}
}

function saveToForm(formField, iframeID) {
	if (isNormal(iframeID)) {
		eval(formField + " = makeRel(document.frames('" + iframeID + "').document.body.innerHTML);")
	} else {
		eval(formField + " = makeRel(document.frames('" + iframeID + "').document.body.innerText);")
	}
}

function isNormal(iframeID) {
	if (document.frames(iframeID).document.body.style.backgroundColor == backgroundColor.toLowerCase( )) return false;
	return true;
}

function setFocus(iframeID) {
	idEdit = iframeID
}

function makeRel(sContent) {
	var loc = new String (document.location);
	var re = new RegExp(loc.substr(0, loc.lastIndexOf('/')+1), 'gi');
	var stemp = sContent.replace(re, '')
	re = loc.match(/http?:\/\/([^/]+)/gi)
	stemp = stemp.replace(re, '')
	stemp = stemp.replace(/ dir=ltr style=\"MARGIN-RIGHT: 0px\"/, '')
	return stemp
}