// ==UserScript==
// @name           Newsletter Tool
// @namespace      http://www.scottklement.com/scripts/newslettertool
// @include        http://www.pentontech.com/ibmcontent/index.cfm*
// ==/UserScript==

GM_addStyle('div#sckpreview { background-color: #F3F1e9; padding:15px 0 20px 15px;'
            + 'width:760px; min-height:150px; overflow:hidden;}');
GM_addStyle('div#sckpreview h1.title{ color:#714307; margin:2px 0; padding:0; '
            + 'font-weight:bold; }');
GM_addStyle('div#sckpreview h1.title a{ color:#714307; }');
GM_addStyle('div#sckpreview h3,div#sckpreview h3,div#sckpreview h4{ font-weight:bold; '
            + 'color:#714307; margin-top:10px; }');
GM_addStyle('div#sckpreview ul{ margin:5px 5px 15px 10px; padding:0; '+
            + 'list-style-type:none; }');
GM_addStyle('div#sckpreview ul li{ background:url(http://systeminetwork.com/' 
            + 'sites/all/themes/sin/images/listBullet.gif) no-repeat 3px 4px; '
            + 'padding:0 0 0 1.5em; list-style-type:none; margin:3px 0;}');
GM_addStyle('div#sckpreview pre, table{ width:740px; padding:5px 3px; ' 
            + 'background:#fbfaf5; overflow-x:auto; overflow-y:auto;}');
GM_addStyle('div#sckpreview sck-author-label { display:inline; font-weight:bold; }');

widenTextBoxes();

var articleText = document.getElementById('articleText');
if (articleText != null) {
   insertButtons();
} else {
   articleText = document.getElementById('issueEditorText');
   if (articleText != null) {
       insertButtons2();
   }
}


// --------------------------------------------------------------
//  widenTextBoxes():  Make the editing boxes in the tool wider
//                     so you can see more content at once.
// --------------------------------------------------------------

function widenTextBoxes() {

   var allElems, curElem;
   allElems = document.evaluate(
       "//select[@size='5']",
       document,
       null,
       XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
       null);
   for (var i = 0; i < allElems.snapshotLength; i++) {
       curElem = allElems.snapshotItem(i);
       curElem.style.width="325px";
   }

   allElems = document.evaluate(
       "//textarea[@cols='100']",
       document,
       null,
       XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
       null);
   for (var i = 0; i < allElems.snapshotLength; i++) {
       curElem = allElems.snapshotItem(i);
       curElem.cols="150";
       curElem.style.width="750px";
   }
}


// --------------------------------------------------------------
//   Insert the PREVIEW and FIX QUOTES buttons, replacing the
//   broken preview button.
// --------------------------------------------------------------

function insertButtons() {

   var allLinks, thisLink, button;
   allLinks = document.evaluate(
       '//a[@href]',
       document,
       null,
       XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
       null);

   for (var i = 0; i < allLinks.snapshotLength; i++) {
       thisLink = allLinks.snapshotItem(i);
       if (thisLink.href.indexOf('override=1') != -1) {
            thisLink.href="#";
            thisLink.innerHTML='<form id="sckform" method="post" action="#">' +
                          '<input type="button" id="sck2" value="Preview">'+
                          '<input type="button" id="sck4" value="Fix Quotes">'+
                          '</form>';
       }
   }

   button = document.getElementById("sck2");
   button.addEventListener('click', inlinePreview, true);
   button = document.getElementById("sck4");
   button.addEventListener('click', fixQuotes, true);
}

function insertButtons2() {

    var buttonDiv = document.createElement('div');
    buttonDiv.id = "buttonDiv";
    buttonDiv.innerHTML='<form id="sckform" method="post" action="#">' +
                        '<input type="button" id="sck2" value="Preview">'+
                        '</form>';
   var leftColumn = document.getElementById('leftColumn');
   leftColumn.insertBefore(buttonDiv, leftColumn.firstChild);

   button = document.getElementById("sck2");
   button.addEventListener('click', inlinePreview, true);
}


// --------------------------------------------------------------
//   This is called when the PREVIEW button is clicked.		
//   It inserts a preview of the article at the top of the
//   browser window.
// --------------------------------------------------------------

function inlinePreview() {
   var articleText = document.getElementById('articleText');
   var articleTitle = null;
   var authorInfo = "";

   if (articleText != null) {
      articleTitle = document.getElementById('articleTitle');
      authorInfo = getAuthorInfo();
   } else {
      articleTitle = document.createElement('input');
      articleTitle.value = "From the Editor";
      articleText = document.getElementById('issueEditorText');
   }

   var previewBox = document.getElementById("previewBox");
   if (previewBox == null) {
       previewBox = document.createElement('div');
       previewBox.id = "previewBox";
   }
   previewBox.innerHTML = '<h2 class="contenthead">Preview (Scroll Down to Edit Article)</h2>'
                        + '<div id="sckpreview">'
                        + '<h1 class="title">' + articleTitle.value + '</h1>'
                        + authorInfo + '<br>'
                        + articleText.value;
                        + '</div>';
   var leftColumn = document.getElementById('leftColumn');
   leftColumn.insertBefore(previewBox, leftColumn.firstChild);
}


// --------------------------------------------------------------
//   This is called when the FIX QUOTES button is clicked.		
//
//   It searches the article text for non-ascii quote marks and 
//   symbols, and replaces them with pure ASCII equivalents.
// --------------------------------------------------------------

function fixQuotes() {
   var replacements, regex, key, textnodes, node, s;
   var articleText = document.getElementById('articleText');
   if (articleText == null) {
       articleText = document.getElementById('issueEditorText');
   }
   articleText.focus();
   articleText.disabled=1;

   replacements = {
       "\xa0": " ",
       "\xa9": "(c)",
       "\xae": "(r)",
       "\xb7": "*",
       "\u2018": "'",
       "\u2019": "'",
       "\u201c": '"',
       "\u201d": '"',
       "\u2026": "...",
       "\u2002": " ",
       "\u2003": " ",
       "\u2009": " ",
       "\u2013": "-",
       "\u2014": "&#151;",
       "\u2122": "(tm)"};

   regex = {};

   for (key in replacements) {
       regex[key] = new RegExp(key, 'g'); 
   }

   for (key in replacements) {
      articleText.value = articleText.value.replace(regex[key], replacements[key]);
   }
   articleText.disabled=0;
   articleText.focus();
}


// --------------------------------------------------------------
//   This retrieves the author names from the Article Author(s)
//   box and builds an HTML string to be used as a "byline".
//    (called by inlinePreview routine.)
// --------------------------------------------------------------

function getAuthorInfo() {
   var authorList, allAuthors, authorBox;

   allAuthors = document.evaluate(
       "//select[@name='artAuthorIDs']",
       document,
       null,
       XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
       null);

   authorList="";
   vis="visible";

   for (var i = 0; i < allAuthors.snapshotLength; i++) {
       authorBox = allAuthors.snapshotItem(i);
       for (var x=0; x<authorBox.length; x++) {
          var authorName = authorBox.options[x].text;
          var authorSplit = authorName.search(",");
          if ( authorSplit > 0 ) {
             authorName = authorName.slice(authorSplit+2)
                        + " "
                        + authorName.slice(0, authorSplit);
          }
          authorList = authorList + '<div style="font-weight: bold; display:inline; visibility:' + vis + ';">By: '
                                  + '<a href="#" style="visibility:visible;">' + authorName + '</a></div><br>';
          vis="hidden";
       }
   }
   return authorList;
}
