﻿// JScript File


var idEdit = '';
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) {
//alert("abc")
	with (document.frames(iframeID).document) {
		//write("<html><head></head><body>" + eval(iframeID) + "</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 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
}

	document.write ('    <map name="designMode">\n');
	document.write ('    <area alt="Bold" href="javascript:fncCommand(\'bold\');void(0);" shape="rect" coords="2, 0, 25, 26">\n');
	document.write ('    <area alt="Italic" href="javascript:fncCommand(\'italic\');void(0);" shape="rect" coords="26, 0, 50, 26">\n');
	document.write ('    <area alt="Underline" href="javascript:fncCommand(\'underline\');void(0);" shape="rect" coords="51, 0, 76, 26">\n');
	document.write ('    <area alt="Heading" href="javascript:fncFormatBlock(\'H1\');void(0);" shape="rect" coords="77, 0, 101, 26">\n');
	document.write ('    <area alt="Left Justify" href="javascript:fncCommand(\'justifyleft\');void(0);" shape="rect" coords="102, 0, 127, 26">\n');
	document.write ('    <area alt="Center" href="javascript:fncCommand(\'justifycenter\');void(0);" shape="rect" coords="128, 0, 151, 26">\n');
	document.write ('    <area alt="Right Justify" href="javascript:fncCommand(\'justifyright\');void(0);" shape="rect" coords="152, 0, 176, 26">\n');
	document.write ('    <area alt="Numbered List" href="javascript:fncCommand(\'insertorderedlist\');void(0);" shape="rect" coords="180, 0, 204, 26">\n');
	document.write ('    <area alt="Bullet List" href="javascript:fncCommand(\'insertunorderedlist\');void(0);" shape="rect" coords="205, 0, 226, 26">\n');
	document.write ('    <area alt="Outdent" href="javascript:fncCommand(\'outdent\');void(0);" shape="rect" coords="227, 0, 250, 26">\n');
	document.write ('    <area alt="Indent" href="javascript:fncCommand(\'indent\');void(0);" shape="rect" coords="251, 0, 273, 26">\n');
	//document.write ('    <area alt="Create Link" href="javascript:fncCreateLink();void(0);" shape="rect" coords="278, 0, 303, 26">\n');
	//document.write ('    <area alt="Switch Modes" href="javascript:fncSwapModes();void(0);" shape="rect" coords="307, 0, 334, 26">\n');
	document.write ('    </map>\n');
