
var colour;
var htmlOn;

//Function to format text in the text box
function FormatText(command, option) {	
	//Colour pallete
	if ((command == 'forecolor') || (command == 'backcolor')) {
		
		parent.command = command;
		buttonElement = document.getElementById(command);
		document.getElementById("message").contentWindow.focus()
		document.getElementById("colourPalette").style.left = getOffsetLeft(buttonElement) + "px";
		document.getElementById("colourPalette").style.top = (getOffsetTop(buttonElement) + buttonElement.offsetHeight) + "px";
		
		if (document.getElementById("colourPalette").style.visibility=="hidden")
			document.getElementById("colourPalette").style.visibility="visible";
		else {
			document.getElementById("colourPalette").style.visibility="hidden";
		}
		
		//get current selected range
		var sel = document.getElementById("message").contentWindow.document.selection; 
		if (sel != null) {
			colour = sel.createRange();
		}
	}	
	//Text Format
	else {
		document.getElementById("message").contentWindow.focus();
	  	document.getElementById("message").contentWindow.document.execCommand(command, false, option);
		document.getElementById("message").contentWindow.focus();
	}
}

//Function to set colour
function setColor(color) {
	//retrieve selected range
	var sel = document.getElementById("message").contentWindow.document.selection; 
	if (sel!=null) {
		var newColour = sel.createRange();
		newColour = colour;
		newColour.select();
	}	
	document.getElementById("message").contentWindow.focus();
	document.getElementById("message").contentWindow.document.execCommand(parent.command, false, color);
	document.getElementById("message").contentWindow.focus();
	document.getElementById("colourPalette").style.visibility="hidden";
}


//Function to add image
function AddImage(imagePath) {
	if(!imagePath){
		imagePath = prompt('Enter the web address of the image', 'http://');			
	}
	if ((imagePath != null) && (imagePath != "")) {
		document.getElementById("message").contentWindow.focus()
		document.getElementById("message").contentWindow.document.execCommand('InsertImage', false, imagePath);
	}
	document.getElementById("message").contentWindow.focus()
}

//Function to switch to HTML view
function HTMLview() {
	
	//WYSIWYG view
	if (htmlOn == true) {
		var html = document.getElementById("message").contentWindow.document.body.innerText;
		document.getElementById("message").contentWindow.document.body.innerHTML = html;
		document.getElementById("ToolBar1").style.visibility="visible";
		document.getElementById("ToolBar2").style.visibility="visible";
		htmlOn = false;
	
	//HTML view
	} else {
		
		var html = document.getElementById("message").contentWindow.document.body.innerHTML;
		document.getElementById("message").contentWindow.document.body.innerText = html;
    		document.getElementById("ToolBar1").style.visibility="hidden";
    		document.getElementById("ToolBar2").style.visibility="hidden";
    		htmlOn = true;
    	}		
}

//Function to clear form
function ResetForm() {
	if (window.confirm('คุณแน่ใจหรือไม่ ว่าต้องการแก้ไขฟอร์มใหม่?')) {
		document.getElementById("message").contentWindow.focus()
	 	document.getElementById("message").contentWindow.document.body.innerHTML = ''; 
	 	return true;
	 } 
	 return false;		
}

//Function to add smiley
function AddSmileyIcon(imagePath){	
	document.getElementById("message").contentWindow.focus();							
	document.getElementById("message").contentWindow.document.execCommand('InsertImage', false, imagePath);
}

//Colour pallete top offset
function getOffsetTop(elm) {
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent){
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetTop;
}

//Colour pallete left offset
function getOffsetLeft(elm) {
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}

//Function to hide colour pallete
function hideColourPallete() {
	document.getElementById("colourPalette").style.visibility="hidden";
}