var oArea, bAreaIsLoaded, oColorSelect, colour;

bAreaIsLoaded = false;
oColorSelect = 0;

function makeAreaIsLoaded(){
	bAreaIsLoaded = true;
}

function _Editor_ClickHandler() {
	if (document.frames(1).event.keyCode==13 && !document.frames(1).event.shiftKey)
	{
		var rng = document.frames(1).document.selection.createRange();
		rng.pasteHTML('<br>');
		rng.select();
		return false;
	}
} 

function editModeOn()
{
	if (navigator.userAgent.match(/msie/i))
	{
		if (document.frames(1).document.readyState != "complete") {
			window.setTimeout("editModeOn()", 100);
		} else {
			document.frames(1).document.designMode = "On";
			document.frames(1).document.onkeypress = _Editor_ClickHandler;
			oArea = document.frames(1).document;
			oArea.innerHTML = "";
			bAreaIsLoaded = true;
		}
	}
	else if (navigator.userAgent.match(/gecko/i))
    {
		oArea = document.getElementById('EditorFrame').contentDocument;
		oArea.designMode = 'On'; 
		oArea.onkeypress = _Editor_ClickHandler;
		bAreaIsLoaded = true;
	}
}

function makeSelectColor(intColorMode)
{
	var buttonId;

	oColorSelect = intColorMode;
	buttonId = document.getElementById("btnFontColor");
	document.all.colourPalette.style.left = getOffsetLeft(buttonId) + "px";
	document.all.colourPalette.style.top = (getOffsetTop(buttonId) + buttonId.offsetHeight) + "px";
	document.all.colourPalette.style.visibility = "visible";

	var sel = frames(1).document.selection;
	if (sel != null) {
		colour = sel.createRange();
	}
}

function makeSelectBGColor()
{
	document.frames(1).document.bgColor = "#eeeeee";
}

function SelectFontColor(strColor)
{
	if (bAreaIsLoaded)
	{
		var sel_area = oArea.selection;
		if ( sel_area != null ) {
			var colourRange = sel_area.createRange();
			colourRange = colour;
			colourRange.select();
		}
		if ( oColorSelect == 0 )
		{
			oArea.execCommand ("ForeColor", false, strColor);
		} else {
			if (navigator.userAgent.match(/msie/i))
    		{
				oArea.execCommand ("BackColor", false, strColor);
			}
   			else if (navigator.userAgent.match(/gecko/i))
    		{
				oArea.execCommand ("hilitecolor", false, strColor);
	 		}
		}
		document.all.colourPalette.style.visibility = "hidden";
	}
}

function getOffsetTop(obj) {
	var mOffsetTop = obj.offsetTop;
	var mOffsetParent = obj.offsetParent;

	while( mOffsetParent )
	{
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetTop;
}

function getOffsetLeft(obj) {
	var mOffsetLeft = obj.offsetLeft;
	var mOffsetParent = obj.offsetParent;

	while( mOffsetParent )
	{
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}

function makeCopy() {
	if (bAreaIsLoaded) {
		if (navigator.userAgent.match(/msie/i)) {
			oArea.execCommand ("Copy", false, null);
		}
	}
}

function makeCut(){
	if (bAreaIsLoaded) {
		if (navigator.userAgent.match(/msie/i)) {
			oArea.execCommand ("Cut", false, null);
		}
	}
}

function makePaste(){
	if (bAreaIsLoaded){
		if (navigator.userAgent.match(/msie/i)) {
			oArea.execCommand ("Paste", false, null);
		}
	}
}

function makeJustifyLeft(){
	if (bAreaIsLoaded) {
		oArea.execCommand ("JustifyLeft", false, null);
		oArea.body.focus();
	}
}

function makeJustifyCenter(){
	if (bAreaIsLoaded) {
		oArea.execCommand ("JustifyCenter", false, null);
		oArea.body.focus();
	}
}

function makeJustifyRight(){
	if (bAreaIsLoaded) {
		oArea.execCommand ("JustifyRight", false, null);
		oArea.body.focus();
	}
}

function makeBlockJustify(){
	if (bAreaIsLoaded) {
		oArea.execCommand ("JustifyFull", false, null);
		oArea.body.focus();
	}
}

function makeInsertLink(){
	if (bAreaIsLoaded) {
		if (navigator.userAgent.match(/msie/i))
		{
			oArea.execCommand ("CreateLink", true, null);
		}
		else if (navigator.userAgent.match(/gecko/i))
		{
			window.open("linkcreator.html", "ha_fullscreen", 
				"toolbar=no,menubar=no,personalbar=no,width=250,height=150,left=100,top=100" + 
				"scrollbars=no,resizable=no,modal=yes,status=no");
		}
	}
}

function makeRemoveLink(){
	if (bAreaIsLoaded) {
		oArea.execCommand ("Unlink", false, null);
		oArea.body.focus();
	}
}

function makeHorizontalRule(){
	if (bAreaIsLoaded) {
		oArea.execCommand ("InsertHorizontalRule", false, null);
		oArea.body.focus();
	}
}

function makeUndo(){
	if (bAreaIsLoaded) {
		oArea.execCommand ("Undo", false, null);
		oArea.body.focus();
	}
}

function makeRedo(){
	if (bAreaIsLoaded) {
		oArea.execCommand ("Redo", false, null);
		oArea.body.focus();
	}		
}

function makePrint(){
	if (bAreaIsLoaded) {
		if (navigator.userAgent.match(/msie/i))
		{
			oArea.execCommand ("Print")
		}
		else if (navigator.userAgent.match(/gecko/i))
		{
			document.getElementById('EditorFrame').contentWindow.print();
		}
	}
}

function makeUnderln(){
	if (bAreaIsLoaded) {
		oArea.execCommand ("Underline", false, null);
		oArea.body.focus();
	}
}

function makeBold() {
	if (bAreaIsLoaded) {
		oArea.execCommand ("Bold", false, null);
		oArea.body.focus();
	}
}

function makeItalic() {
	if (bAreaIsLoaded) {
		oArea.execCommand ("Italic", false, null);
		oArea.body.focus();
	}
}

function changeFontFace() {
	if (bAreaIsLoaded) {
		oArea.execCommand ("FontName", false, document.getElementById("fontFace").value);
		oArea.body.focus();
	}
}

function changeFontSize() {
	if (bAreaIsLoaded) {
		oArea.execCommand ("FontSize", false, document.getElementById("fontSize").value);
		oArea.body.focus();
	}
}

function makeOrderedList() {
	if (bAreaIsLoaded) {
		oArea.execCommand ("InsertOrderedList", false, null);
	}
}

function makeUnorderedList() {
	if (bAreaIsLoaded) {
		oArea.execCommand ("InsertUnorderedList", false, null);
	}
}

function SelPlainHMTL(sel)
{
	if( sel == 0 )
	{
		if(bAreaIsLoaded)
		{
			if( oArea + "" != "undefined" )
			{
				if( confirm(JsWarningChangeFromHTMLToPlain) )
				{
					SetHtmlResults();
					document.message_form.switch_plain_html.value = sel;
					CheckNewForm(2);
				}
			} else {
				alert(JsAlertPageLoading);
			}
		}
	} else {
		document.message_form.switch_plain_html.value = sel;
		boolFormReady = true;
		CheckNewForm(2);
	}
}

function SelPlainHMTL2(sel)
{
	document.message_form.switch_plain_html.value = sel;
	CheckSignatureForm(0);
}

function SetHtmlResults()
{
	var ss;

	boolFormReady = false;
	if (bAreaIsLoaded)
	{
		if( oArea + "" != "undefined" )
		{
	 		if (navigator.userAgent.match(/msie/i))
    		{
					ss = oArea.body.innerText;
					if( ss.length == 0 )
					{
						document.message_form.message.value = "";
					} else {
						document.message_form.message.value = oArea.body.innerHTML;
					}
					document.message_form.switch_plain_html.value = 1;
					boolFormReady = true;
			}
   			else if (navigator.userAgent.match(/gecko/i))
    		{
					ss = document.getElementById('EditorFrame').contentDocument.body.innerHTML;
					if( ss.length == 0 )
					{
						document.message_form.message.value = "";
					} else {
						document.message_form.message.value = document.getElementById('EditorFrame').contentDocument.body.innerHTML;
					}
					document.message_form.switch_plain_html.value = 1;
					boolFormReady = true;
	 		}
		} else {
			alert(JsAlertPageLoading);
		}
	}
}

function SetDefaults()
{
}

