// ==UserScript==
// @name           Hollow City XP Props
// @namespace      http://www.steegness.com/greasemonkey/XP_Props/
// @description    Allows automated submission of XP nomination
// @include        http://*invisionfree.com/The_Hollow_City/index.php?*showtopic=*
// ==/UserScript==

//Edit this to be your name, username, or however you'd like to be known.
var USER_NAME = 'Your name/handle here'

//DO NOT EDIT BELOW THIS LINE
//This is the XPath information for where the username of the post lives.
username_xpath_start = '/html/body/center/table/tbody/tr/td[2]/div[5]/table['
username_xpath_end = ']/tbody/tr[1]/td[1]/span[1]/a[1]'
//This is the XPath information for where the post link, post date, and post time live.
postdate_xpath_start = '/html/body/center/table/tbody/tr/td[2]/div[5]/table['
postdate_xpath_end = ']/tbody/tr[1]/td[2]/div[1]/span[1]'

//This is what the postlink looks like.  We'll tack a number in the middle and on the end when we make the post.
postlink_start = 'http://s13.invisionfree.com/The_Hollow_City/index.php?showtopic='
postlink_end = '&view=findpost&p='

//The title of the thread.  It will be the passed to the props for ease of use.
thread_title = document.title.slice(19)

//This is the topic number
topic_number = location.search.match(/showtopic=\d+/)[0].slice(10)

function get_dt_info(innerstring, dta) 
{
    var dtr
    var dts
    //example: <b><a title="Show the link to this post" onclick="link_to_post(3510305); return false;" style="text-decoration: underline;">Posted:</a></b> Apr 21 2006, 11:39 AM
    dtr = innerstring.slice(innerstring.indexOf('</b>')+5)
    if (dta == 'dt') {
        return dtr
    } else {
        dts = dtr.split(', ')
        if (dta == 'd') {
            return dts[0]
        } else {
            return dts[1]
        }
    }
}

function get_post_number(innerstring)
{
    var rex
    //example: <b><a title="Show the link to this post" onclick="link_to_post(3510305); return false;" style="text-decoration: underline;">Posted:</a></b> Apr 21 2006, 11:39 AM
    rex = /link_to_post\(\d+\)/
    postjs = innerstring.match(rex)[0]
    postno = postjs.slice(postjs.indexOf('(')+1, postjs.indexOf(')'))
    return postno
}

var mailtolink
var postlink
var d
var t

//Main routine
for (i=1;i<=1000;i++) {

    username_node = document.evaluate(username_xpath_start + i + username_xpath_end, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
    postdate_node = document.evaluate(postdate_xpath_start + i + postdate_xpath_end, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
    
    if (postdate_node) {
        user_to_prop = username_node.innerHTML
        post_number = get_post_number(postdate_node.innerHTML)
        postlink = location.protocol + '//' + location.hostname + location.pathname
        
        mailLink = document.createElement('a')
        mailLink.innerHTML = get_dt_info(postdate_node.innerHTML, 'd')
        mailtolink = 'mailto:' + 'hollow' + 'city' + 'xp@gma' + 'il.com'
        mailtolink = mailtolink + '?' + encodeURI('subject=HC XP Props to ' + user_to_prop + ' from ' + USER_NAME)
        mailtolink = mailtolink + encodeURI('&body=I, ' + USER_NAME + ', would like to recognize ' + user_to_prop + ' for an excellently roleplayed post.  ')
        mailtolink = mailtolink + encodeURI('That post can be found here: ')
        mailtolink = mailtolink + encodeURI(postlink) + encodeURIComponent('?')
        mailtolink = mailtolink + encodeURIComponent('showtopic=' + topic_number + '&view=findpost&p=' + post_number)
        mailtolink = mailtolink + encodeURI('\n\nThank You,\n' + USER_NAME)
        mailLink.href = mailtolink
        mailLink.style.color = 'red'
        
        t = get_dt_info(postdate_node.innerHTML, 't')
        postdate_node.innerHTML = postdate_node.innerHTML.slice(0, postdate_node.innerHTML.indexOf('</b>')+5)
        postdate_node.insertBefore(mailLink, null)
        postdate_node.innerHTML = postdate_node.innerHTML + ', ' + t
    } else {
        break
    }
}
        
        
        
        
         
