/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | WYSIWYG Embedded Form Component                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001 Contact Designs LLC http://www.contactdesigns.com |
// +----------------------------------------------------------------------+
// | All text, source code, and intellectual property contained herein    |
// | are proprietary to Contact Designs. These materials are protected    |
// | by U.S. copyright and may not be used, copied, transmitted, or       |
// | reproduced in whole or in part without the express written           |
// | consent of Contact Designs. Unauthorized use of the text, source     |
// | code, and intellectual property contained herein may violate         |
// | trademark, copyright, civil and criminal statutes, and privacy       |
// | and publicity laws. Contact Designs reserves all rights.             |
// +----------------------------------------------------------------------+
//


var showing_source = false;
var showing_html = true;
var current_editor = null;
var find_in = 0;


function showSource() {
    if (showing_source) {
        alert('You are already in HTML Edit mode.');
        return;
    } else {
        showing_html = false;
        showing_source = true;
    }
    var content = current_editor.document.documentElement.outerHTML;
    current_editor.document.body.innerText = content;
    current_editor.document.body.style.fontFamily = "Courier";
    current_editor.document.body.style.fontSize = "10px";
}


function showHTML() {
    if (showing_html) {
        alert('You are already in Visual Edit mode.');
        return;
    } else {
        showing_source = false;
        showing_html = true;
    }
    var content = current_editor.document.body.innerText;
    current_editor.document.body.innerHTML = content;
    current_editor.document.body.style.fontFamily = "";
    current_editor.document.body.style.fontSize = "";
}


function findInPage(str) {
	if (str == '') {
		alert('Nothing to search for!');
		return;
	}
	var txt, i, found;
	if (str == "")
		return false;
	txt = current_editor.document.body.createTextRange();
	for (i = 0; i <= find_in && (found = txt.findText(str)) != false; i++) {
		txt.moveStart("character", 1);
		txt.moveEnd("textedit");
	}
	if (found) {
		txt.moveStart("character", -1);
		txt.findText(str);
		txt.select();
		txt.scrollIntoView();
		find_in++;
	} else {
		if (find_in > 0) {
			find_in = 0;
			findInPage(str);
		} else {
			alert(str + " was not found on this page.");
		}
	}
}


function execCommand(command, bUserInterface, vValue) {
    var selrange = current_editor.document.selection.createRange();
    switch (command){
        case "New":
            current_editor.location="about:blank";
            break;
        case "SaveAs":
            current_editor.execCommand("SaveAs");
            break;
        case "Print":
            current_editor.execCommand("Print",1,"");
            break;
        case "Cut":
            selrange.execCommand("Cut");
            break;
        case "Copy":
            selrange.execCommand("Copy");
            break;
        case "Paste":
            selrange.execCommand("Paste");
            break;
        case "Delete":
            selrange.execCommand("Delete");
            break;
        case "Undo":
            selrange.execCommand("Undo");
            break;
        case "Redo":
            selrange.execCommand("Redo");
            break;
        case "Outdent":
            selrange.execCommand("Outdent");
            break;
        case "Indent":
            selrange.execCommand("Indent");
            break;
        case "JustifyLeft":
            selrange.execCommand("JustifyLeft");
            break;
        case "JustifyCenter":
            selrange.execCommand("JustifyCenter");
            break;
        case "JustifyRight":
            selrange.execCommand("JustifyRight");
            break;
        case "Bold":
            selrange.execCommand("Bold");
            break;
        case "Italic":
            selrange.execCommand("Italic");
            break;
        case "Underline":
            selrange.execCommand("Underline");
            break;
        case "InsertUnorderedList":
            selrange.execCommand("InsertUnorderedList");
            break;
        case "InsertOrderedList":
            selrange.execCommand("InsertOrderedList");
            break;
        case "CreateLink":
            selrange.execCommand("CreateLink",1,"");
            break;
        case "InsertImage":
            selrange.execCommand("InsertImage",1,"");
            break;
        case "BackColor":
            selrange.execCommand("BackColor", bUserInterface, vValue);
            break;
        case "ForeColor":
            selrange.execCommand("ForeColor", bUserInterface, vValue);
            break; 
        case "RemoveFormat":
            selrange.execCommand("RemoveFormat");
            break;
        case "CreateBookmark":
            var currentName = ''; 
            if (selrange.htmlText.indexOf('<A name=') != -1) { 
                var na = /\<a\sname\=(\w+)\W+/i;
                var fa = na.exec(selrange.htmlText);
                currentName = fa[1]
            } 
            var name = prompt('Please enter the name for the named anchor.', currentName);
            if (name != null && name != '') { 
                selrange.execCommand("CreateBookmark", 0, name);
            } else { 
                alert('No named anchor was added because no name was given.');
            }
            break;
        case "FontName":
            selrange.execCommand("FontName", bUserInterface, vValue);
            break;
        case "FontSize":
            selrange.execCommand("FontSize", bUserInterface, vValue);
            break;
        case "LoadTable":
            selrange.pasteHTML(vValue);
            break;
        case "Find":
            findInPage(vValue);
            break;
        case "ShowHTML":
            showHTML();
            break;
        case "ShowSource":
            showSource();
            break;
        case "EditMode":
            current_editor.document.designMode = vValue;
            break;
    }
}


function newWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}


