﻿//*************************************************************************
// Rich Text methods
// -----------------
//

function doRichEditCommand(aName, aArg){
	document.execCommand(aName,false, aArg);
//	document.getElementById(editablecanvas).focus()
    CheckMenuState();
} 

function formatBlock(formatStyle)
{
    if(formatStyle=="normal")
        doRichEditCommand('FormatBlock','Normal');
    else
    {
        doRichEditCommand('FormatBlock',formatStyle);
    }
}

var docsel;
function addLink(){

    docsel = document.selection.createRange();
    if(docsel.text=="")
        alert("Please highlight an area of text for the hyperlink.");
    else
    {
        var linkEditor = document.getElementById("ifrInsertLink");
        if(linkEditor==null)
            alert("Cannot find link editor.  Please try again.  If this persists contact support.");
        else
        {
	        var sExistingUrl = "";
	        var parentElem = document.selection.createRange().parentElement();

	        if((parentElem!=null) && ( parentElem.tagName=="A"))
		        sExistingUrl = parentElem.href;	//nameProp;	//getAttribute("href");
            linkEditor.src = "RTF/LinkEditor.aspx?current=" + sExistingUrl;

            var DialogOffsetTop = document.documentElement.scrollTop + 50;
            var DialogOffsetLeft = (document.documentElement.clientWidth - 400)/2;
            document.getElementById("divGrayWindow").style.display="block";
            linkEditor.style.top = DialogOffsetTop + 100;
            linkEditor.style.left = DialogOffsetLeft;

            linkEditor.style.display="block";
        }
    }
}    

function insertLink()
{
    var linkEditor = window.frames["ifrInsertLink"];

    if(linkEditor==null)
        alert("Cannot find link editor.  Please try again.  If this persists contact support.");
    else
    {
        var Content = document.getElementById('Content');
        if(Content!=null)
            Content.focus();

        var target = linkEditor.document.getElementById('txtLink');
        var targetWnd= linkEditor.document.getElementById('chkSeparateWindow');

        var link = "<a href='" + target.value + "'";
        if(targetWnd.checked)
            link += " target='_blank';";
            
        link += ">" + docsel.htmlText + "</a>";

        docsel.pasteHTML(link);

//        doRichEditCommand("createlink",target.value);
    
        cancelLinkEditor();
    }
    
}

function cancelLinkEditor()
{
    parent.document.getElementById("divGrayWindow").style.display="none";

    var linkEditor = parent.document.getElementById("ifrInsertLink");
    
    if(linkEditor!=null)
        linkEditor.style.display="none";
}


function addBookmark(){
	var myBookmark = prompt("Please enter a bookmark name:", "");
    if((myBookmark!="") && (myBookmark!=false))
	{
        var range = document.selection.createRange();
        var BookmarkText = range.text;
        var str = "<a name=\"" + myBookmark +"\">" + BookmarkText + "</a>";

        range.pasteHTML(str);
	}
}

function CheckMenuState()
{
    try
    {
        if((document.getElementById("bold").className=="current") && !document.queryCommandState("bold"))
            document.getElementById("bold").className="";
        else if ((document.getElementById("bold").className=="") && document.queryCommandState("bold"))
            document.getElementById("bold").className="current";
        
        if((document.getElementById("italic").className=="current") && !document.queryCommandState("italic"))
            document.getElementById("italic").className="";
        else if ((document.getElementById("italic").className=="") && document.queryCommandState("italic"))
            document.getElementById("italic").className="current";
        
        if((document.getElementById("underline").className=="current") && !document.queryCommandState("underline"))
            document.getElementById("underline").className="";
        else if ((document.getElementById("underline").className=="") && document.queryCommandState("underline"))
            document.getElementById("underline").className="current";

        if((document.getElementById("leftalign").className=="current") && !document.queryCommandState("justifyleft"))
            document.getElementById("leftalign").className="";
        else if ((document.getElementById("leftalign").className=="") && document.queryCommandState("justifyleft"))
            document.getElementById("leftalign").className="current";

        if((document.getElementById("centeralign").className=="current") && !document.queryCommandState("justifycenter"))
            document.getElementById("centeralign").className="";
        else if ((document.getElementById("centeralign").className=="") && document.queryCommandState("justifycenter"))
            document.getElementById("centeralign").className="current";

        if((document.getElementById("rightalign").className=="current") && !document.queryCommandState("justifyright"))
            document.getElementById("rightalign").className="";
        else if ((document.getElementById("rightalign").className=="") && document.queryCommandState("justifyright"))
            document.getElementById("rightalign").className="current";
//    if((document.getElementById("heading").className=="current") && (document.queryCommandValue("FormatBlock").toLowerCase()=="normal"))
//        document.getElementById("heading").className="";
//    else if ((document.getElementById("heading").className=="") && (document.queryCommandValue("FormatBlock").toLowerCase()!="normal"))
//        document.getElementById("heading").className="current";
    }
    catch(e)
    {
        if(window.menuStateID!=undefined)
            clearInterval(menuStateID);

    }
}
var menuStateID = window.setInterval(CheckMenuState,500);

/***********************************************************/
/*
/* Context menu
/*
/***********************************************************/
function showContextMenu()
{
    var menu = document.getElementById("ifrContextMenu");
    if(menu!=null)
    {
        var scrollTop = document.documentElement.scrollTop;
        var topPos = event.clientY + scrollTop;
        var clientHeight = document.documentElement.clientHeight;
        
        if((topPos + menu.offsetHeight)>(scrollTop + clientHeight))
            topPos = (scrollTop + clientHeight) - menu.offsetHeight;
        menu.style.top = topPos;// + body.scrollTop;

        menu.style.left = event.clientX;
        menu.style.display = 'block';
    }
}

function hideContextMenu()
{
    var menu = document.getElementById("ifrContextMenu");
    if(menu!=null)
        menu.style.display="none";
}

function insertImage()
{

	hideContextMenu();

    var Content = document.getElementById('Content');
    if(Content!=null)
        Content.focus();
    docsel = document.selection.createRange();
    var menu = window.frames["ifrContextMenu"].document.getElementById('contextMenu');
//    if(menu!=null)
//        menu.style.display = 'none';

    var browseForFile = document.getElementById('ifrUpload');
    if(browseForFile!=null)
    {
        var DialogOffsetTop = document.documentElement.scrollTop + 50;
        var DialogOffsetLeft = (document.documentElement.clientWidth - 400)/2;
        document.getElementById("divGrayWindow").style.display="block";
        browseForFile.style.top = DialogOffsetTop + 100;
        browseForFile.style.left = DialogOffsetLeft;
            
//        browseForFile.style.top = document.documentElement.scrollTop + 100; //event.clientY - 200;
//        browseForFile.style.left = Content.offsetLeft; //event.clientX;
        browseForFile.style.display="block";
    }
}


//*************************************************************************
// Snapshot methods
// -----------------
//

function placeSnapshot() {
	//hideContextMenu();

    //var Content = document.getElementById('Content');
    //if(Content!=null)
    //    Content.focus();
    docsel = document.selection.createRange();
    //var menu = document.getElementById('contextMenu');
    //if(menu!=null)
    //    menu.style.display = 'none';

    var browseForFile = document.getElementById('ifrSnapshot');
    if(browseForFile!=null)
    {
        var DialogOffsetTop = document.documentElement.scrollTop + 50;
        var DialogOffsetLeft = (document.documentElement.clientWidth - 673)/2;
        document.getElementById("divGrayWindow").style.display="block";
        browseForFile.style.top = DialogOffsetTop + 50;
        browseForFile.style.left = DialogOffsetLeft;
        
        browseForFile.style.display="block";
//        document.getElementById("editor").style.display="block";

//        browseForFile.style.top = event.clientY - 200;
//        browseForFile.style.left = event.clientX;
//        browseForFile.style.display = "block";
//        


        var frm = window.frames.ifrSnapshot;
        if(frm && frm.reload)
            frm.reload();
        else
            alert("Unable to reload page");
    }
}

function closeSnapshot()
{
    parent.document.getElementById("divGrayWindow").style.display="none";
    parent.document.getElementById("ifrSnapshot").style.display="none";
//    parent.document.getElementById("editor").style.display="none";
}

function pasteSnapshot(anchor)
{
	hideContextMenu();

    if(docsel!=null)
    {
        var html = "<div style='float:left;border:3px dotted #F5F5F5;padding:3px;'><img src='snapshots/" + anchor.innerHTML + "' /></div>";
        docsel.pasteHTML(html);
    }

//    var uploadPanel = document.getElementById("ifrSnapshot");

//    if(uploadPanel!=null)
//        uploadPanel.style.display="none";

    closeSnapshot();

//    pasteImage("snapshots/" + anchor.innerHTML);
}


function pasteImage(ImageURL)
{
    if(docsel!=null)
    {
        var html = "<div style='float:left;border:3px dotted #F5F5F5;padding:3px;'><img src='" + ImageURL + "' /></div>";
        docsel.pasteHTML(html);
    }

    var uploadPanel = document.getElementById("ifrUpload");

    if(uploadPanel!=null)
        uploadPanel.style.display="none";
}

function doCommand(cmdName)
{
	hideContextMenu();

    var Content = document.getElementById('Content');
    if(Content!=null)
        Content.focus();

    doRichEditCommand(cmdName);
}

function cancelUpload()
{
    parent.document.getElementById("divGrayWindow").style.display="none";

    var uploadPanel = parent.document.getElementById("ifrUpload");
    
    if(uploadPanel!=null)
        uploadPanel.style.display="none";
}



function offerInsert(relPath)
{
    parent.document.getElementById("divGrayWindow").style.display="none";

    var docsel = document.selection.createRange();
    if(docsel!=null)
    {
        if(confirm("Would you like this snapshot inserted into this document?"))
        {
//            var html = "<div style='float:left;border:3px dotted #999999;padding:3px;'><img src='" + relPath + "' /></div>";
            var html = "<img src='" + relPath + "' style='float:left;border:3px dotted #999999;margin:3px;/>";
            docsel.pasteHTML(html);
        }
    }

}


function changeThumb(anchor)
{
    var img = document.getElementById("ssthumb");
    if(img!=null)
    {
        img.src = "snapshots/" + anchor.innerHTML;
        img.style.display = "block";
    }
}

