// JavaScript Document
var editwin = null;


function Help(daLink) {
var helpWnd=window.open(daLink,"help","width=400,height=500,scrollbars=yes,dependent=yes");
}


function UtilBeginScript()
{
return String.fromCharCode(60, 115, 99, 114, 105, 112, 116, 62);
}

function UtilEndScript()
{
return String.fromCharCode(60, 47, 115, 99, 114, 105, 112, 116, 62);
}

function IDGenerator(nextID)
{
this.nextID = nextID;
this.GenerateID = IDGeneratorGenerateID;
}

function IDGeneratorGenerateID()
{
return this.nextID++;
}

var BUTTON_IMAGE_PREFIX = "buttonImage";
var BUTTON_DIV_PREFIX = "buttonDiv";
var BUTTON_PAD1_PREFIX = "buttonPad1";
var BUTTON_PAD2_PREFIX = "buttonPad2";
var buttonMap = new Object();

function Button
(
idGenerator,
caption,
action,
image
)
{
this.idGenerator = idGenerator;
this.caption = caption;
this.action = action;
this.image = image;
this.enabled = true;
this.Instantiate = ButtonInstantiate;
this.Enable = ButtonEnable;
}

function ButtonInstantiate()
{
this.id = this.idGenerator.GenerateID();
buttonMap[this.id] = this;
var html = "";
html += '<div id="';
html += BUTTON_DIV_PREFIX;
html += this.id;
html += '" class="ButtonNormal"';
html += ' onselectstart="ButtonOnSelectStart()"';
html += ' ondragstart="ButtonOnDragStart()"';
html += ' onmousedown="ButtonOnMouseDown(this)"';
html += ' onmouseup="ButtonOnMouseUp(this)"';
html += ' onmouseout="ButtonOnMouseOut(this)"';
html += ' onmouseover="ButtonOnMouseOver(this)"';
html += ' onclick="ButtonOnClick(this)"';
html += ' ondblclick="ButtonOnDblClick(this)"';
html += '>';
html += '<table cellpadding=0 cellspacing=0 border=0><tr><td><img id="';
html += BUTTON_PAD1_PREFIX;
html += this.id;
html += '" width=2 height=2></td><td></td><td></td></tr><tr><td></td><td>';
html += '<img id="';
html += BUTTON_IMAGE_PREFIX;
html += this.id;
html += '" src="';
html += this.image;
html += '" title="';
html += this.caption;
html += '" class="Image"';
html += '>';
html += '</td><td></td></tr><tr><td></td><td></td><td><img id="';
html += BUTTON_PAD2_PREFIX;
html += this.id;
html += '" width=2 height=2></td></tr></table>';
html += '</div>';
document.write(html);
}

function ButtonEnable(enabled)
{
this.enabled = enabled;
if (this.enabled)
{
document.all[BUTTON_DIV_PREFIX + this.id].className = "ButtonNormal";
}
else
{
document.all[BUTTON_DIV_PREFIX + this.id].className = "ButtonDisabled";
}
}

function ButtonOnSelectStart()
{
window.event.returnValue = false;
}

function ButtonOnDragStart()
{
window.event.returnValue = false;
}

function ButtonOnMouseDown(element)
{
if (event.button == 1)
{
var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
var button = buttonMap[id];
if (button.enabled)
{
ButtonPushButton(id);
}
}
}

function ButtonOnMouseUp(element)
{
if (event.button == 1)
{
var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
var button = buttonMap[id];
if (button.enabled)
{
ButtonReleaseButton(id);
}
}
}

function ButtonOnMouseOut(element)
{
var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
var button = buttonMap[id];
if (button.enabled)
{
ButtonReleaseButton(id);
}
}

function ButtonOnMouseOver(element)
{
var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
var button = buttonMap[id];
if (button.enabled)
{
ButtonReleaseButton(id);
document.all[BUTTON_DIV_PREFIX + id].className = "ButtonMouseOver";
}
}

function ButtonOnClick(element)
{
var id = element.id.substring(BUTTON_DIV_PREFIX.length, element.id.length);
var button = buttonMap[id];
if (button.enabled)
{
eval(button.action);
}
}

function ButtonOnDblClick(element)
{
ButtonOnClick(element);
}

function ButtonPushButton(id)
{
document.all[BUTTON_PAD1_PREFIX + id].width = 3;
document.all[BUTTON_PAD1_PREFIX + id].height = 3;
document.all[BUTTON_PAD2_PREFIX + id].width = 1;
document.all[BUTTON_PAD2_PREFIX + id].height = 1;
document.all[BUTTON_DIV_PREFIX + id].className = "ButtonPressed";
}

function ButtonReleaseButton(id)
{
document.all[BUTTON_PAD1_PREFIX + id].width = 2;
document.all[BUTTON_PAD1_PREFIX + id].height = 2;
document.all[BUTTON_PAD2_PREFIX + id].width = 2;
document.all[BUTTON_PAD2_PREFIX + id].height = 2;
document.all[BUTTON_DIV_PREFIX + id].className = "ButtonNormal";
}

var EDITOR_COMPOSITION_PREFIX = "editorComposition";
var EDITOR_PARAGRAPH_PREFIX = "editorParagraph";
var EDITOR_LIST_AND_INDENT_PREFIX = "editorListAndIndent";
var EDITOR_TOP_TOOLBAR_PREFIX = "editorTopToolbar";
var EDITOR_BOTTOM_TOOLBAR_PREFIX = "editorBottomToolbar";
var EDITOR_SMILEY_BUTTON_PREFIX = "editorSmileyButton";
var EDITOR_IMAGE_CHOOSER_PREFIX = "editorImageChooser";
var editorMap = new Array();

var editorIDGenerator = null;

function Editor(idGenerator)
{
this.idGenerator = idGenerator;
this.textMode = false;
this.brief = false;
this.instantiated = false;
this.Instantiate = EditorInstantiate;
this.GetText = EditorGetText;
this.SetText = EditorSetText;
this.GetHTML = EditorGetHTML;
this.SetHTML = EditorSetHTML;
this.GetBrief = EditorGetBrief;
this.SetBrief = EditorSetBrief;
}


function EditorInstantiate(text_height,format_bar,path_img,in_text)
{
if (this.instantiated) {
return;
}
this.id = this.idGenerator.GenerateID();
editorMap[this.id] = this;
editorIDGenerator = this.idGenerator;

var html = "";
var str_html = "";
var str_htmledit = "";

	str_htmledit += "<tr>";
	str_htmledit += "<td>";
	str_htmledit += "<iframe style=\"border:0px solid\"  id=\"" + EDITOR_COMPOSITION_PREFIX + this.id + "\" width=\"100%\" height=\"" + text_height + "\">";
	str_htmledit += "</iframe>";
	str_htmledit += "</td>";
	str_htmledit += "</tr>";


if(format_bar!=0){

	html += "<tr>";
	html += "<td id=\"" + EDITOR_BOTTOM_TOOLBAR_PREFIX + this.id + "\" class=\"Toolbar\">";
	html += "<table cellpaddin=\"0\" cellspacing=\"0\" border=\"0\">";
	html += "<tr>";
	html += "<td>";
	html += "<div class=\"Space\"></div>";
	html += "</td>";
	html += "<td>";
	html += "<div class=\"Swatch\"></div>";
	html += "</td>";
	html += "<td>";
	html += UtilBeginScript();
	html += "var boldButton = new Button(";
	html += "editorIDGenerator,";
	html += "\"Ch&#7919; &#273;&#7853;m\",";
	html += "\"EditorOnBold(" + this.id + ")\",";
	html += "\"" + path_img +"/bold.gif\"";
	html += ");";
	html += "boldButton.Instantiate();";
	html += UtilEndScript();
	html += "</td>";
	html += "<td>";
	html += UtilBeginScript();
	html += "var italicButton = new Button(";
	html += "editorIDGenerator,";
	html += "\"Ch&#7919; nghi&#234;ng\",";
	html += "\"EditorOnItalic(" + this.id + ")\",";
	html += "\"" + path_img +"/italic.gif\"";
	html += ");";
	html += "italicButton.Instantiate();";
	html += UtilEndScript();
	html += "</td>";
	html += "<td>";
	html += UtilBeginScript();
	html += "var underlineButton = new Button(";
	html += "editorIDGenerator,";
	html += "\"G&#7841;ch ch&#226;n\",";
	html += "\"EditorOnUnderline(" + this.id + ")\",";
	html += "\"" + path_img +"/uline.gif\"";
	html += ");";
	html += "underlineButton.Instantiate();";
	html += UtilEndScript();
	html += "</td>";
	html += "<td>";
	html += "<div class=\"Divider\"></div>";
	html += "</td>";
	html += "<td>";
	html += UtilBeginScript();
	html += "var foregroundColorButton = new Button(";
	html += "editorIDGenerator,";
	html += "\"M&#7847;u ch&#7919;\",";
	html += "\"EditorOnForegroundColor(" + this.id + ",'"+path_img+"')\",";
	html += "\"" + path_img +"/tpaint.gif\"";
	html += ");";
	html += "foregroundColorButton.Instantiate();";
	html += UtilEndScript();
	html += "</td>";
	html += "<td>";
	html += "<div class=\"Divider\"></div>";
	html += "</td>";
	html += "<td>";
	html += "<div class=\"Divider\"></div>";
	html += "</td>";

	html += "</tr>";
	html += "</table>";
	html += "</td>";
	html += "</tr>";
}
	str_html += "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">";
	switch(format_bar){
		case 0 :		
			str_html += str_htmledit; 			
			break;
		case 1 :
			str_html += html+ str_htmledit ; 				
			break;
		case 2 :
			str_html += str_htmledit + html; 		
			break;
	}
	str_html += "</table>";

document.write(str_html);
html = in_text;
html += '<body style="font-family: Verdana; font-size: 10pt">';
html += '</body>';
var ifrm = eval(EDITOR_COMPOSITION_PREFIX + this.id);
ifrm.document.open();
ifrm.document.write(html);
//ifrm.document.write($rs["gthieu"]);
ifrm.document.close();
ifrm.document.designMode = "on";
//ifrm.document.body.innerHTML = 'sdjfsndfawejrawerawjerj';
//ifrm.document.onclick = new Function("EditorOnClick(" + this.id + ")");
editwin = ifrm;

editorIDGenerator = null;
this.instantiated = true;
}
function  EditorGetText()
{
	return editwin.document.body.innerText;
}

function  EditorSetText(text)
{
text = text.replace(/\n/g, "<br>");
editwin.document.body.innerHTML = text;
}

function  EditorGetHTML()
{
if (this.textMode) {
return editwin.document.body.innerText;
}
EditorCleanHTML(this.id);
EditorCleanHTML(this.id);
return editwin.document.body.innerHTML;
}

function  EditorSetHTML(html)
{
if (this.textMode) {
editwin.document.body.innerText = html;
}
else {
editwin.document.body.innerHTML = html;
}
}

function EditorGetBrief()
{
return this.brief;
}

function EditorSetBrief(brief)
{
this.brief = brief;
var display = this.brief ? "none" : "inline";
if (this.instantiated) {
eval(EDITOR_PARAGRAPH_PREFIX + this.id).style.display = display;
eval(EDITOR_LIST_AND_INDENT_PREFIX + this.id).style.display = display;
}
}

function EditorOnCut(id)
{
EditorFormat(id, "cut");
}

function EditorOnCopy(id)
{
EditorFormat(id, "copy");
}

function EditorOnUndo(id)
{
EditorFormat(id, "undo");
}

function EditorOnRedo(id)
{
EditorFormat(id, "redo");
}

function EditorOnSave(id)
{
EditorFormat(id, "SaveAs", "vietunidoc.html");
}

/*function EditorOnOpen(id)
{
//EditorFormat(id, "Open");
alert('Câ`n server-side scripting. Chu+a có thò+i gian... :-)');
editwin.focus();
}*/

/*function EditorOnOpenURL(id)
{
alert('Câ`n server-side scripting. Chu+a có thò+i gian... :-)');
editwin.focus();
}*/

function EditorOnPaste(id)
{
EditorFormat(id, "paste");
}

function EditorOnBold(id)
{
EditorFormat(id, "bold");
}

function EditorOnItalic(id)
{
EditorFormat(id, "italic");
}

function EditorOnUnderline(id)
{
EditorFormat(id, "underline");
}

function EditorOnForegroundColor(id,path_color)
{
if (!EditorValidateMode(id)) {
return;
}
var color = showModalDialog(path_color+"/ColorSelect.htm", "", "font-family:Verdana;font-size:12;dialogWidth:29em;dialogHeight:31em");
if (color) {
EditorFormat(id, "forecolor", color);
}
else {

editwin.focus();
}
}

function EditorOnBackgroundColor(id,path_color)
{
if (!EditorValidateMode(id)) {
return;
}
var color = showModalDialog(path_color+"/ColorSelect.htm", "", "font-family:Verdana;font-size:12;dialogWidth:	29em;dialogHeight:31em");
if (color) {
var doc = editwin;
if (doc.document.selection.type == "None") {
  editwin.document.body.style.backgroundColor = color;
}
EditorFormat(id, "backcolor", color);
}
else {
editwin.focus();
}
}

function EditorOnAlignLeft(id)
{
EditorFormat(id, "justifyleft");
}

function EditorOnCenter(id)
{
EditorFormat(id, "justifycenter");
}

function EditorOnAlignRight(id)
{
EditorFormat(id, "justifyright");
}

function EditorOnNumberedList(id)
{
EditorFormat(id, "insertOrderedList");
}

function EditorOnBullettedList(id)
{
EditorFormat(id, "insertUnorderedList");
}

function EditorOnDecreaseIndent(id)
{
EditorFormat(id, "outdent");
}

function EditorOnIncreaseIndent(id)
{
EditorFormat(id, "indent");
}

function EditorOnRTEHelp(id)
{
alert('coming soon...');
}

function EditorOnCreateHyperlink(id)
{
    if (!EditorValidateMode(id)) {
        return;
    }
    editwin.focus();
    EditorFormat(id, "CreateLink");
}

function EditorOnCreateImg(id)
{
    if (!EditorValidateMode(id)) {
        return;
    }
    editwin.focus();
    EditorFormat(id, "InsertImage");
}

function EditorOnInsert(id, select)
{
    if (!EditorValidateMode(id)) {
        return;
    }
    editwin.focus();
    EditorFormat(id, select[select.selectedIndex].value);
    select.selectedIndex = 0;
}

function EditorOnParagraph(id, select)
{
EditorFormat(id, "formatBlock", select[select.selectedIndex].value);
select.selectedIndex = 0;
}

function EditorOnFont(id, select)
{
EditorFormat(id, "fontname", select[select.selectedIndex].value);
select.selectedIndex = 0;
}

function EditorOnSize(id, select)
{
EditorFormat(id, "fontsize", select[select.selectedIndex].value);
select.selectedIndex = 0;
}

function EditorOnCharset(id, select)
{
editwin.document.charset = select[select.selectedIndex].value;
editwin.focus();
}

function EditorOnViewHTMLSource(id, textMode)
{
var editor = editorMap[id];
editor.textMode = textMode;
if (editor.textMode) {
EditorCleanHTML(id);
EditorCleanHTML(id);
editwin.document.body.innerText = editwin.document.body.innerHTML;
}
else {
editwin.document.body.innerHTML = editwin.document.body.innerText;
}
editwin.focus();
}

function EditorOnLoad(id)
{
editwin.document.body.value = "";
}

function EditorValidateMode(id)
{
var editor = editorMap[id];
if (!editor.textMode) {
return true;
}
alert("Ha~y ta('t o^ \"Xem Html-Code\" dde^? co' the^ du`ng thanh co^ng cu.");
editwin.focus();
return false;
}

function EditorFormat(id, what, opt)
{
if (!EditorValidateMode(id)) {
return;
}
if (opt == "removeFormat") {
what = opt;
opt = null;
}
if (opt == null) {
editwin.document.execCommand(what, true);
}
else {
editwin.document.execCommand(what, "", opt);
}
editwin.focus();
}

function EditorCleanHTML(id)
{
var fonts = editwin.document.body.all.tags("FONT");
for (var i = fonts.length - 1; i >= 0; i--) {
var font = fonts[i];
if (font.style.backgroundColor == "#ffffff") {
font.outerHTML = font.innerHTML;
}
}
}

function EditorGetElement(tagName, start)
{
while (start && start.tagName != tagName) {
start = start.parentElement;
}
return start;
}

function Switch() {
if (editor.GetText() != "" && editor.GetText() != editor.GetHTML()) {
conf = confirm("Chi? da^~n: Tác vu. này se~ làm mâ't hê't ddi.nh da.ng. Thu+.c hiê.n?");
if (!conf) return;
  }
  document.Compose.Body.value = editor.GetText();
document.Compose.action = document.Compose.action + "&SWITCH=1";
  document.Compose.submit();
}


function SetVals() {
document.Compose.Body.value = editor.GetHTML();
}


var remote=null;
function rs(n,u,w,h,x) {
  args="width="+w+",height="+h+",resizable=yes,scrollbars=yes,status=0";
  remote=window.open(u,n,args);
  if (remote != null) {
if (remote.opener == null)
  remote.opener = self;
  }
  if (x == 1) { return remote; }
}


var sigAttMap = [false];

function OnFromAddrChange()
{
var i = document.Compose.FromAddr.selectedIndex;
if (i >= 0 && i < sigAttMap.length) {
document.all.SA.checked = sigAttMap[i];
}
}

