var sEditMode;

function ed_init(sContents, sHead)
{
	var sInitialContents;
	
	sInitialContents = "<html><head>" + sHead + "</head><body>" + sContents + "</body></html>";

	EditFrame.document.designMode = "on";
	EditFrame.document.open("text/html","replace");
	EditFrame.document.write(sInitialContents);
	EditFrame.document.close();
	//ed_format('FormatBlock', '<DIV>');
	//EditFrame.document.execCommand('FormatBlock', false, '<DIV>');

	sEditMode = '';

	document.all.EditFrameDiv.style.visibility='visible';

	setFocus();
}

function ed_mode(sMode)
{
	var editFrameBody;
	editFrameBody = EditFrame.document.body;

	if ((sMode == 'HTML') && (sEditMode != 'HTML'))
	{
		editFrameBody.innerText = editFrameBody.innerHTML;
		sEditMode = sMode;
		document.all.buttons.style.visibility='hidden';
		document.all.buttons.style.pixelHeight = 1;
	}
	else if ((sMode == 'Text') && (sEditMode != 'Text'))
	{
		editFrameBody.innerHTML = editFrameBody.innerText;
		sEditMode = sMode;
		document.all.buttons.style.visibility='visible';
		document.all.buttons.style.pixelHeight = 34;
	}
	setFocus();
}

function ed_format(sAttribute, sValue)
{
	var objSelection = this.selection;
	
	if (!objSelection) {
		objSelection = EditFrame.document.selection.createRange();
		objSelection.type = EditFrame.document.selection.type;
	}
	var sType   = objSelection.type;
	var oTarget = (sType == "None" ? EditFrame.document : objSelection);
	var oBlock  = (objSelection.parentElement != null ? ed_GetTag(objSelection.parentElement()) : objSelection.item(0));

	setFocus();

	if (sValue != null) {
		if (sValue == "no style") {
			sAttribute = "nothing";
			if (oTarget.htmlText != null)
				objSelection.pasteHTML(oTarget.text);
			else
				EditFrame.document.body.innerHTML = EditFrame.document.body.innerText;
		}
	}

	switch(sAttribute)
	{
		case "InsertAdjacentHTML":
			objSelection.pasteHTML(sValue);
			break;
/*alert(sValue);
		case "bold": // instead of <strong>
			if (oTarget.htmlText != null)
				objSelection.pasteHTML("<b>" + oTarget.htmlText + "</b>");
			break;
		case "italic": // instead of <em>
			if (oTarget.htmlText != null)
				objSelection.pasteHTML("<i>" + oTarget.htmlText + "</i>");
			break;
*/
		case "CreateLink":
			if (oTarget.htmlText != null)
				oTarget.execCommand("CreateLink", true, sValue);
			else
				alert("You must select the link text first.");
			break;
		case "colour":
			if (oTarget.htmlText != null) {
				if (sValue == "") {
					if (objSelection.parentElement().outerHTML.search(/style=[^<>]*color\s*:/gi) > -1)
						objSelection.parentElement().outerHTML = objSelection.parentElement().outerText;
				}
				else {
					objSelection.pasteHTML("<span style='color: " + sValue + "'>" + objSelection.htmlText + "</span>");
				}
			}
			ed_ToggleColourPicker(null);
			break;
		case "Justify":
			if (oBlock) 
			{
				oBlock.style.textAlign = "";
				if (((oBlock.tagName=="table") || (oBlock.tagName=="img")) && (("left"==oBlock.align) && ("Left"==sValue))) {
					oBlock.align = "";
					break;
				}	
				oBlock.align = sValue;
				if ((oBlock.tagName=="hr") || ((oBlock.tagName=="img") && sValue!="Center")) break;
			}
			sAttribute = sAttribute + sValue;
			sValue="";
		case "nothing":	// Do nothing
		// Fall through
		default:
			oTarget.execCommand(sAttribute, false, sValue);
			break;
	}
	//g_state.RestoreSelection()
	setFocus();
	
	return true;
}

function setFocus() {
	EditFrame.focus();
}

function ed_clear()
{
	EditFrame.document.body.innerHTML = '';
	ed_init();
}

function ed_GetTag(objElement) 
{
	var sTags = "|H1|H2|H3|H4|H5|H6|P|PRE|LI|TD|DIV|BLOCKQUOTE|DT|DD|TABLE|HR|IMG|";

	while ((objElement != null) && (sTags.indexOf("|"+objElement.tagName+"|")==-1))
		objElement = objElement.parentElement;
	return objElement;
}

function ed_submit(oHiddenInput)
{
	var sHTML = "";
	var reHostName = new RegExp("\"" + location.protocol + "//" + location.host, "gi");
	
	if (sEditMode == 'HTML') {
		sHTML = EditFrame.document.body.innerText;
	} else {
		sHTML = EditFrame.document.body.innerHTML;
	}


	sHTML = sHTML.replace(reHostName, "\"");

	oHiddenInput.value = sHTML;

    if (sHTML.length > 102000)
        return confirm("Some web servers have a limit of 102,399 bytes which your content has exceeded.\n\nClick 'Cancel' to reduce your content-length.");
    else
        return true;
}

function ed_ToggleButtonsHeight() {
	if (buttons.style.pixelHeight < 50)
		buttons.style.pixelHeight = 76;
	else
		buttons.style.pixelHeight = 32;

	setFocus();
}

function ed_ToggleColourPicker(oElement) {
  if ((btncolours.style.visibility == 'visible') || (oElement == null)) {
		btncolours.style.visibility = 'hidden';

		setFocus();
	}
	else {
		btncolours.style.posLeft = 370;
    btncolours.style.visibility = 'visible';
	}
}
