// inserts a link by prompting the user for a url
// bbcode modified to add textarea, link_type, prompt_text argument, instead of hardcode
//function insertlink()
/***************************************************************/
function insertlink(textarea, link_type, prompt_text)
{
    // our textfield
    //var textarea = document.getElementById("The_TextArea");

    // our link
    var url = prompt("Please enter the "+ link_type, prompt_text);
    var link = "%%" + url + "%%" + url + "%%";

    if(!textarea.setSelectionRange)
    {
        // get selected text
        var selected = document.selection.createRange().text; 

        if(selected.length <= 0)
        { 
            // no text was selected so add the link to the end
            textarea.value += link;
        }
        else
        {
            // replace the selection with the link
            document.selection.createRange().text = "%%" + url + "%%" + selected + "%%";   
        }
    }
    else
    {
        // the text before the selection
        var pretext = textarea.value.substring(0, textarea.selectionStart);

        // the text after the selection
        var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);

        // update the text field
        textarea.value = pretext + "%%" + url + "%%" + selected + "%%" + posttext;
    }

    // set the focus on the text field
    textarea.focus();
}
/***************************************************************/
// bbcode modified to add textarea argument, instead of hardcode
// insert: bold, italic
/***************************************************************/
function insertcode(textarea, tag, desc)
{
    // our textfield
    // var textarea = document.getElementById("The_TextArea");

    // our open tag
    // var open = "[" + tag + ">";
	var open = "<" + tag + ">";
	
    // our close tag
    // var close = "[/" + tag + ">";
	var close = "</" + tag + ">";
	
    if(!textarea.setSelectionRange)
    {
        var selected = document.selection.createRange().text; 
        if(selected.length <= 0)
        { 
            // no text was selected so prompt the user for some text
            textarea.value += open + prompt("Please enter the text you'd like to " + desc, "") + close;
        }
        else
        {
            // put the code around the selected text
            document.selection.createRange().text = open + selected + close; 
        }

    }
    else
    {
        // the text before the selection
        var pretext = textarea.value.substring(0, textarea.selectionStart);
        
        // the selected text with tags before and after
        var codetext = open + textarea.value.substring(textarea.selectionStart, textarea.selectionEnd) + close;

        // the text after the selection
        var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
        
        // check if there was a selection
        if(codetext == open + close)
        {
            //prompt the user
            codetext = open + prompt("Please enter the text you'd like to " + desc, "") + close;
        }

        // update the text field
        textarea.value = pretext + codetext + posttext;
    }

    // set the focus on the text field
    textarea.focus();
}
/***************************************************************/
/***************************************************************
Insert string onButtonClick at cursor position in textarea 
and not at the end of string

Please Use the InsertText Function:
  The First argument is the textbox/textarea control: 
  document.[formName].[textareaName]
  e.g. document.paste_pt_form.pasted_pt 
  the next is the Text to be inserted...
****************************************************************/
function InsertText(input, insTexte)
{

startTag = '';
endTag = '';


     if (input.createTextRange)
     {
      var text;
      input.focus(input.caretPos);
      input.caretPos = document.selection.createRange().duplicate();
      if(input.caretPos.text.length>0)
      {
       input.caretPos.text = startTag + input.caretPos.text + endTag;
      }
      else
      {
       input.caretPos.text = startTag + " " + insTexte + " " + endTag;
      }
     }
     else input.value += startTag + insTexte + endTag;
}
/****************************************************************/
/****************************************************************/
// Legacy functions
// some functions: img, hyperlink may still be used
/****************************************************************/
function findObj(n, d)
{ 
	var p,i,x;  
	if(!d) d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) 
	{
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document); return x;
}

function showHideLayers() 
{
	var i,p,v,obj,args=showHideLayers.arguments;
  	for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) 
	{ 
		v=args[i+2];
    	if (obj.style) 
		{ 
			obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; 
		}
    	obj.visibility=v; 
	}
}

function doCopyValue() 
{
    document.forms[0].content.value = "" + editor.document.body.innerHTML + "";
}

function trimIt(trimStr)
{    
	var i = trimStr.length;
	while (trimStr.charAt(0) == " ")
            {          
		trimStr = trimStr.substring(1,i) 
		i = i-1;
	}
	while (trimStr.charAt(trimStr.length-1) == " ")
	{
		trimStr = trimStr.substring( 0,trimStr.length-1) 
	}
	return trimStr;

}

function doMakeBold() 
{
    // Get a text range for the selection
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("Bold")
    tr.select()
    doCopyValue();
}

function doCopy() 
{
    // Get a text range for the selection
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("Copy", false)
    doCopyValue();
}

function doPaste() 
{
    // Get a text range for the selection
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("Paste", false)
    doCopyValue();
}

function doInsertUL() 
{
    // Get a text range for the selection
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("InsertUnorderedList", false)
    doCopyValue();
}

function doIndent() 
{
    // Get a text range for the selection
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("Indent", false)
    doCopyValue();
}

function doUnIndent() 
{
    // Get a text range for the selection
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("Outdent", false)
    doCopyValue();
}

function doMakeItalic() 
{
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("Italic")
    tr.select()
    doCopyValue();    
}

function doFontName(what)
{
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("FontName", false, what)
    tr.select()
    doCopyValue();    
}

function doFontSize(what)
{
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("FontSize", false, what)
    tr.select()
    doCopyValue();    
}


function doInsertLink()
{
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("CreateLink")
    tr.select()
    doCopyValue();    
}

function doUnLink()
{
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("UnLink")
    tr.select()
    doCopyValue();    
}

function doImage(img)
{
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("InsertImage", 0, img)
    tr.select()
    doCopyValue();    
}

function doJustify(where)
{
    var tr = frames.editor.document.selection.createRange()
    if (where == 'Left') tr.execCommand("JustifyLeft")
    else if (where == 'Right') tr.execCommand("JustifyRight")
    else if (where == 'Center') tr.execCommand("JustifyCenter")
    tr.select()
    doCopyValue();  
}

function doHorizRule()
{
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("InsertHorizontalRule")
    tr.select()
    doCopyValue();    
}


function doFieldSet()
{
    var tr = frames.editor.document.selection.createRange()
    tr.execCommand("InsertFieldset")
    tr.select()
    doCopyValue();    
}

function doTable()
{
    var tr = frames.editor.document.selection.createRange()
    tr.pasteHTML("<table border='1'><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>")
    tr.select()
    doCopyValue();    
}

function doRenderHTML() 
{
	editor.document.body.innerHTML=document.forms[0].content.value;
}

/*********************************************************************
						insert expressions
*********************************************************************/
tags = new Array();

//insert Salutation, First name, Last name, etc.
function doText(text)
{
    var tr = frames.editor.document.selection.createRange()
    tr.pasteHTML("%"+text+"%")
    tr.select()
    doCopyValue();    
}

//insert URL
function doLink(link_type, prompt_text) {
	//prompt
	url = prompt("Enter " + link_type, prompt_text);
	
	if ((url != null) && (url != "")){
			text = prompt("Enter the text to be displayed for the " + link_type, "");
			
			if((text != null) && (text != "")){
				display_text = text;
			}else{
				display_text = url;
			}
			var tr = frames.editor.document.selection.createRange()
			// insert %%xxxx%%xxxx%%  style markup
    		tr.pasteHTML("%%"+url+"%%"+display_text+"%%")
    		tr.select()
    		doCopyValue(); 
		}
}

//file link
function doFileLink(file) {
// insert  $$file#file ID#displayed text$$  style markup
	file_id = file;
	//prompt
	text = prompt("Enter the text to be displayed for the link","");
		if ((file_id != null) && (file_id != "")){
			if((text != null) && (text != "")){
				display_text = text;
			}else{
				display_text = file_id;
			}
			var tr = frames.editor.document.selection.createRange()
    		tr.pasteHTML("$$file#"+file_id+"#"+display_text+"$$")
    		tr.select()
    		doCopyValue(); 
		}
}
/*********************************************************************/
function doInsertText(insert_text) {
// insert %xxxx% style markup
	paste_pt_form.pasted_pt.value += "%"+insert_text+"% ";
	paste_pt_form.pasted_pt.focus();
}

function doInsertLink(url, display_text) {
// insert >xxxx xxxx style markup
	paste_pt_form.pasted_pt.value += ">" + display_text + "\n" + url + " \n";
	paste_pt_form.pasted_pt.focus();
}

function doInsertLinkHTML(url, display_text) {
// insert %%xxxx%%xxxx%% style markup
	paste_pt_form.pasted_pt.value += "%%" + url + "%%" + display_text + "%% \n";
	paste_pt_form.pasted_pt.focus();
}

//jump menu
function MM_jumpMenu(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

