// ==UserScript==
// @name           Feedburner Unverified Email Subscriber Tool
// @description    Script to retrieve a list of all the unverified email subscribers for your Feedburner feed, so that you can email them to tell them how to subscribe properly.
// @include        http://feedburner.google.com/fb/a/emailsyndication*
// @version        1.0
// @creator        Daniel Harrison (http://www.powerdosh.com)
// ==/UserScript==


var subHolder, newElement;

// Get the subscriber holder list
subHolder = document.getElementById('subHolder');

// If found, then add a new element to it
if (subHolder)
{
    newElement = document.createElement('div');
    newElement.innerHTML = '<div style="margin: 0px 15px 15px 15px; padding: 10px; border: 2px solid #015FAB; background: #EEE; font-family: Arial,Helvetica,sans-serif; font-size: 10pt;"><a name="uvl"></a>' +
                           '<div style="margin-bottom: 0px; font-weight: bold;">Feedburner Unverified Email Subscriber Tool</div>' +
                           '<div style="margin-bottom: 10px; font-size: 8pt;">By <a href="http://www.powerdosh.com">Daniel Harrison of PowerDosh.com</a></div>' +
                           '<p><span id="unverifiedCountBox"></span>The <b>unverified</b> state means that the subscriber has not clicked on the verification email from Feedburner, and therefore is not getting your updates via email.</p> ' +
                           '<p>This tool extracts the email addresses of unverified users so that you can contact them to try and sign up again.</p>' + 
                           '<p><a id="listhide" href="#uvl">Show Unverified Subscriber List</a></p>' +
                           '<div id="listdiv" style="display: none;">' +
                           '<div style="font-weight: bold;">Unverified Subscriber List (with commas)</div>' +
                           '<textarea rows="20" cols="50" id="listComma" style="margin: 15px; font-size: 8pt; font-family: courier new, courier ;"></textarea>' +
                           '<div style="font-weight: bold;">Unverified Subscriber List (without commas)</div>' +
                           '<textarea rows="20" cols="50" id="listNewline" style="margin: 15px; font-size: 8pt; font-family: courier new, courier ;"></textarea>' +
                           '</div>' +
                           '</div>';

    subHolder.parentNode.insertBefore(newElement, subHolder);
}


// Get all subscribers on the page by table row.
var allSubscribers, aSubRow, aSubEmail, unverifiedCount;
allSubscribers = document.evaluate("//div[@id='subHolder']/table/tbody/tr[@id]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

if (allSubscribers)
{
    // Get a reference to the list of email addresses
    var listNewline = document.getElementById('listNewline');
    var listComma   = document.getElementById('listComma');

    if (listNewline && listComma)
    {
        // Check each row of the subscribers
        unverifiedCount = 0;
        for (var i = 0; i < allSubscribers.snapshotLength; i++)
        {
            aSubRow = allSubscribers.snapshotItem(i);
        
            // Find the ones that are Unverifed
            if (aSubRow.childNodes[5].innerHTML == 'Unverified')
            {
               // Trim all the whitespace surrounding the email address
               var emailAddress = aSubRow.childNodes[1].innerHTML.replace(/^\s+|\s+$/g,"");
               unverifiedCount++;
               
               // Add email address to the lists
               listNewline.innerHTML +=  emailAddress + '\n';
               listComma.innerHTML   +=  emailAddress + ', \n';
            }
        }
    }
}

// Get the number that are unverified.
var unverifiedCountBox = document.getElementById('unverifiedCountBox');
if (unverifiedCountBox) {
   unverifiedCountBox.innerHTML = 'There are currently <b>' + unverifiedCount +'</b> unverified subscribers. ';
}

// Show the list when asked for when the link is clicked on.
function listShowHide()
{
    var unverList = document.getElementById('listdiv');
    var showLink = document.getElementById('listhide');

    // Determine if everything we need exists to show the list
    if (unverList && showLink)
    {
        if (showLink.innerHTML == "Show Unverified Subscriber List") {
            showLink.innerHTML = "Hide Unverified Subscriber List";
            unverList.style.display = 'block';
        }
        else {
            showLink.innerHTML = "Show Unverified Subscriber List";
            unverList.style.display = 'none';
        }
    }
}

// Add event for clicking to show or hide the link
var showLink = document.getElementById('listhide');
showLink.addEventListener("click", listShowHide, true);


