﻿

function InsertAtCursor(myFieldID, myValue) 
{
    var ftb = FTB_API[myFieldID]
    var data = ftb.GetHtml();
    var browserName = navigator.appName;

    if (browserName == "Netscape") 
    {
        var lastData = data.substring(data.length - 3, 4);
        
        if (lastData == "<br>")
            ftb.SetHtml(data.substring(0, data.length - 4) + myValue);
        else
            ftb.SetHtml(data + myValue);
    }
    else
        ftb.SetHtml(data + myValue);



//    myField = document.getElementById(myFieldID);
//    
//    myField.focus();
//    //IE support
//    if (document.selection) 
//    {
//        sel = document.selection.createRange();
//        sel.text = myValue;
//    }
//    //MOZILLA/NETSCAPE support
//    else if (myField.selectionStart || myField.selectionStart == '0') 
//    {
//        var startPos = myField.selectionStart;
//        var endPos = myField.selectionEnd;
//        myField.value = myField.value.substring(0, startPos)
//                  + myValue
//                  + myField.value.substring(endPos, myField.value.length);
//    } else 
//    {
//        myField.value += myValue;
//    }
}


function openURL()
{
    alert("tester");        
    top.location.href="http://www.google.com/";
}


//This function returns the index of the cursor location in
//the value of the input text element
//It is important to make sure that the sWeirdString variable contains
//a set of characters that will not be encountered normally in your
//text
function getCursorPos(textElement) 
{
     //save off the current value to restore it later,
     var sOldText = textElement.value;

    //create a range object and save off it's text
     var objRange = document.selection.createRange();
     var sOldRange = objRange.text;

    //set this string to a small string that will not normally be encountered
     var sWeirdString = '#%~';

    //insert the weirdstring where the cursor is at
     objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

    //save off the new string with the weirdstring in it
     var sNewText = textElement.value;

    //set the actual text value back to how it was
     objRange.text = sOldRange;

    //look through the new string we saved off and find the location of
    //the weirdstring that was inserted and return that value
     for (i = 0; i <= sNewText.length; i++) 
     {
       var sTemp = sNewText.substring(i, i + sWeirdString.length);
       if (sTemp == sWeirdString) 
       {
         var cursorPos = (i - sOldRange.length);
         return cursorPos;
       }
     }
}

//this function inserts the input string into the textarea
//where the cursor was at
function insertString(myFieldID, stringToInsert) 
{
    myField = document.getElementById(myFieldID);
    if (myField != null) {
        myField.focus();
        var globalCursorPos = getCursorPos(myField);
        var firstPart = myField.value.substring(0, globalCursorPos);
        var secondPart = myField.value.substring(globalCursorPos, myField.value.length);
        myField.value = firstPart + stringToInsert + secondPart;
    }
}

