function insertLink () {
    if (!document.selection) return;
    var str = document.selection.createRange().text;
    if (!str) return;
    var my_link = prompt('Enter URL (link address):', 'http://');
    //var my_target = confirm('If this URL links to another website, click OK.')
    if (my_link != null) {
      //if (confirm('If this URL links to another website, click OK.')) {
        document.selection.createRange().text = '<a href="' + my_link + '" target="_blank" class="link">' + str + '</a>';
      //}
      //else {
      //  document.selection.createRange().text = '<a href="' + my_link + '" class="link">' + str + '</a>';
      //}
    }
}

function insertMailto () {
    if (!document.selection) return;
    var str = document.selection.createRange().text;
    if (!str) return;
    var my_link = prompt('Enter mailto info in this format - mailto:jon@doe.com', 'mailto:');
    //var my_target = confirm('If this URL links to another website, click OK.')
    /*
    if (my_link != null) {
      if (confirm('If this URL links to another website, click OK.')) {
        document.selection.createRange().text = '<a href="' + my_link + '" target="_blank" class="link">' + str + '</a>';
      }
      else {
        document.selection.createRange().text = '<a href="' + my_link + '" class="link">' + str + '</a>';
      }
    }
    */
    // for email, it doesn't matter if the link is internal or external...
    document.selection.createRange().text = '<a href="' + my_link + '" class="link">' + str + '</a>';
}

function insertImage () {
    //if (!document.selection) return;
    //var str = document.selection.createRange().text;
    //if (!str) return;
    imgSrc = document.pages.ImgSrc1.value;
    if (!imgSrc) return;
    if (document.pages.ImgJustify.value != '') {
      justify = ' align="' + document.pages.ImgJustify.value + '"'
    }
    else {
      justify = ''
    }
    if (document.pages.Thumb1Border.checked) {
      thisclass = ' class="image"';
    }
    else {
      thisclass = ' border="0"';
    }
    h = document.pages.Thumb1Height.value;
    w = document.pages.Thumb1Width.value;
    //document.selection.createRange().text = '<img src="' + imgSrc + '" class="image">'
    imgSrcHTML = '<img src="/imageup/' + imgSrc + '"' + thisclass + justify + ' width="' + w + '" height="' + h + '">'
    insertAtCaret(document.pages.PageContent, imgSrcHTML);
}

/*
function insertInternal () {
    if (!document.selection) return;
    var str = document.selection.createRange().text;
    if (!str) return;
    //var my_link = prompt('Enter URL (link address):', 'http://');
    my_link = document.pages.SubLink[document.pages.SubLink.selectedIndex].value
    if (my_link != null) {
      document.selection.createRange().text = '<a href="' + my_link + '" class="link">' + str + '</a>';
    }
}
*/
function insertInternal () {
    if (!document.selection) return;
    var str = document.selection.createRange().text;
    if (!str) return;
    //var my_link = prompt('Enter URL (link address):', 'http://');
    my_link = '';
    my_target = '';
    my_link = document.pages.SubLink[document.pages.SubLink.selectedIndex].value;
    //alert('1 = ' + my_link);
    if (my_link == '') {
      my_link = '/imageup/' + document.pages.SubPDF[document.pages.SubPDF.selectedIndex].value;
      my_target = ' target="_blank"';
    }
    //alert('2 = ' + my_link);
    if (my_link == '' || my_link == '/imageup/') {
      my_link = '/imageup/' + document.pages.SubImage[document.pages.SubImage.selectedIndex].value;
      my_target = ' target="_blank"';
    }
    //alert('3 = ' + my_link);
    if (my_link != null && my_link != '' && my_link != '/imageup/' && my_link != '/imageup/../../../imageup/SelectImageFile2.png') {
      document.selection.createRange().text = '<a href="' + my_link + '" class="link"' + my_target + '>' + str + '</a>';
    }
    else {
      alert('No item chosen for link!');
    }
    document.pages.SubLink.selectedIndex = 0;
    document.pages.SubImage.selectedIndex = 0;
    document.pages.SubPDF.selectedIndex = 0;
    document.Thumb2.src='/imageup/SelectImageFile2.png';
}

