/*
written by chris wetherell
http://www.massless.org
chris [THE AT SIGN] massless.org

warning: it only works for IE4+/Win and Moz1.1+
feel free to take it for your site
if there are any problems, let chris know.

modified by Patrick Lucas
*/

opt_url = "";
req_img = "";
req_img_caption = "";
opt_caption = "";

function getSelection(txtarea) {
    retVal = "";
    
    if (document.all) {
        retVal = document.selection.createRange().text
    }
    else {
        var selLength = txtarea.textLength;
        var selStart = txtarea.selectionStart;
        var selEnd = txtarea.selectionEnd;
        if (selEnd==1 || selEnd==2) selEnd=selLength;
        retVal = (txtarea.value).substring(selStart, selEnd);
    }
    
    return retVal;
}

function mozWrap(txtarea, lft, rgt) {
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    if (selEnd==1 || selEnd==2) selEnd=selLength;
    var s1 = (txtarea.value).substring(0,selStart);
    var s2 = (txtarea.value).substring(selStart, selEnd)
    var s3 = (txtarea.value).substring(selEnd, selLength);
    txtarea.value = s1 + lft + s2 + rgt + s3;
}

function IEWrap(txtarea, lft, rgt) {  
    strSelection = document.selection.createRange().text;
    document.selection.createRange().text = lft + strSelection + rgt;
}

function wrapSelection( txtarea, lft, rgt ) {
    txtarea.focus();
    before = getCaretPosition( txtarea );
    
    // Old Function
    if (document.all) {IEWrap(txtarea, lft, rgt);}
    else if (document.getElementById) {mozWrap(txtarea, lft, rgt);}
    //
    
    newStart = before.start;
    newEnd = before.end;
    if( before.start == before.end && opt_url != "" ) {
        newStart = before.start + lft.length;
        newEnd = before.start + lft.length + opt_url.length;
    }
    else if( req_img != "" ) {
        newStart = before.end + lft.length + rgt.length;
        newEnd = newStart;
    }
    else if( opt_caption != "" ) {
        newStart = before.end + lft.length + rgt.length;
        newEnd = newStart;
    }
    else if( req_img_caption != "" ) {
        newStart = before.end + lft.length + 6;
        newEnd = newStart;
    }
    else if( before.start == before.end ) {
        newStart = before.start + lft.length;
        newEnd = newStart;
    }
    else {
        newStart = before.end + lft.length + rgt.length;
        newEnd = newStart;
    }
    
    setCaretPosition( txtarea, newStart, newEnd );
    
    before = null;
    
    opt_url = "";
    req_img = "";
    
    return false
}

function wrapSelectionWithLink(txtarea) {
    var selection = getSelection( txtarea );
    opt_url = prompt("Enter URL:","http://");
    if (opt_url != null) {
        lft="[URL=" + opt_url + "]";
        rgt="[/URL]";
        if( selection == "" ) {
            rgt = opt_url + rgt;
        }
        wrapSelection(txtarea, lft, rgt);
    }
    return false;
}	

function wrapSelectionWithImage(txtarea,type) {
    var selection = getSelection( txtarea );
    req_img = selection;
    
    if( type == "LEFT" ) {
        imgBegin = "[IMG]";
    }
    else {
        imgBegin = "[IMG align=" + type + "]";
    }
        
    if( req_img == "" ) {
        req_img = prompt("Enter Image URL:","http://");
        imgBegin = imgBegin + req_img;
        
        if( req_img == null ) { return false; }
    }
    
    wrapSelection(txtarea, imgBegin, "[/IMG]");
    
    return false;
}

function wrapSelectionWithCaption(txtarea,type) {
    var selection = getSelection( txtarea );
    req_img_caption = selection;
    
    imgBegin = "[CAPTION align=" + type + "][IMG]";
        
    if( req_img_caption == "" ) {
        req_img_caption = prompt("Enter Image URL:","http://");
        imgBegin = imgBegin +  req_img_caption;
        
        if( req_img_caption == null ) { return false; }
    }
    
    opt_caption = prompt( "Enter Caption:", "" );
    
    wrapSelection(txtarea, imgBegin, "[/IMG]" + opt_caption + "[/CAPTION]");
    
    return false;
}

function setCaretPosition(oInput,oStart,oEnd) {
    oInput.focus();
    if( oInput.setSelectionRange ) {
        oInput.setSelectionRange(oStart,oEnd);
    } else if( oInput.createTextRange ) {
        var range = oInput.createTextRange();
        range.collapse(true);
        range.moveEnd('character',oEnd);
        range.moveStart('character',oStart);
        range.select();
    }
}


// CaretPosition object
function CaretPosition()
{
    start = 0;
    end = 0;
}

function getCaretPosition(oField)
{
 // Initialize
 var oCaretPos = new CaretPosition();
 
 // IE support
 if(document.selection)
 {
  // Focus on the text box
  oField.focus();

  // This returns us an object containing
  // information about the currently selected text
  var oSel = document.selection.createRange();

  // Find out the length of the selected text
  var selectionLength = oSel.text.length;
  
  // Move to the end of the field
  oSel.moveEnd( 'textedit' );

  // Find the positions
  oCaretPos.start = oField.value.length - oSel.text.length + 1;
  oCaretPos.end = oCaretPos.start + selectionLength;
 }
 // Firefox support
 else if(oField.selectionStart || oField.selectionStart == '0')
 {
  oCaretPos.start = oField.selectionStart;
  oCaretPos.end = oField.selectionEnd;
 }
 
 // Return results
 return (oCaretPos);
}
