// ==UserScript==
// @name                Cancel transaction
// @version             1.3
// @date                2010-04-14
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Adds links to cancel a transaction to the sold orders and view transaction pages, and pre-populate the report transaction form with the right ID
// @include             http://www.etsy.com/your/listings/sold*
// @include             http://www.etsy.com/report_transaction.php*
// @include             http://www.etsy.com/view_transaction.php*
// ==/UserScript==

/*
We add links to each transaction on the "sold orders" page, and at the top of the "view transaction" page that link to the "report transaction" page, and pass it a transaction_id parameter.

The "report transaction" page doesn't actually care about what parameters are passed to it. This script picks up the transaction ID and fills in the transaction box, and sets the "I'm a seller" radio button.
*/

// Fire up the right function for the page we're on
if ( window.location.pathname.match( 'your/listings/sold' ) ) {
    decorateOrders();
} else if ( window.location.pathname.match( 'view_transaction.php' ) ) {
    decorateViewTransaction();
} else if ( window.location.pathname.match( 'report_transaction.php' ) ) {
    decorateReportTransaction();
}

function decorateOrders () {
    var orders = getElementsByClassName( 'title-info' );
    for ( var ord = 0; ord < orders.length; ord++ ) {
        var order = orders[ ord ];
        var link = order.getElementsByTagName( 'a' )[ 0 ].href;
        var transaction_id = link.match( /transaction_id=(\d+)/ )[ 1 ];
        var div = document.createElement( 'div' );
        div.innerHTML = '&bull; <a href="/report_transaction.php?transaction_id=' + transaction_id + '">cancel transaction</a>';
        order.appendChild( div );
    }
}

function decorateViewTransaction () {
    // Extract the transaction ID parameter
    var match = window.location.search.match( /transaction_id=(\d+)/ );
    if ( ! match ) return;
    var transaction = match[ 1 ];
    // Seek the cell that contains the "you sold this item" text
    // which means that it's our transaction and we should be able
    // to cancel it
    var tds = document.getElementsByTagName( 'td' );
    for ( var t = 0; t < tds.length; t++ ) {
        var cell = tds[ t ];
        if ( cell.firstChild && cell.firstChild.data && cell.firstChild.data.indexOf( 'You sold this item' ) > -1 ) {
            // Found our cell - add a link
            var links = cell.getElementsByTagName( 'a' );
            var last = links[ links.length - 1 ];
            last.parentNode.innerHTML += ' | <a href="/report_transaction.php?transaction_id=' + transaction + '">cancel transaction</a>';
            break;
        }
    }
}

function decorateReportTransaction () {
    // Extract the transaction ID parameter
    var match = window.location.search.match( /transaction_id=(\d+)/ );
    if ( ! match ) return;
    var transaction = match[ 1 ];
    // Find the "transaction id" box and fill it
    var inputs = document.getElementsByTagName( 'input' );
    for ( var i = 0; i < inputs.length; i++ ) {
        if ( inputs[ i ].name && inputs[ i ].name == 'transaction_id' ) {
            inputs[ i ].value = transaction;
        } else if ( inputs[ i ].type == 'radio' && inputs[ i ].value == 'seller' ) {
            // Greasemonkey causes problems with accessing more than one form
            // element with the same name, so we find the "am a seller" radio
            // button the hard way.
            inputs[ i ].checked = true;
        }
    }
}

function getElementsByClassName ( class, node ) {
    if ( node == null ) node = document;
    if ( node.getElementsByClassName ) {
        return node.getElementsByClassName( class );
    } else {
        var classElements = new Array();
        var els = node.getElementsByTagName( '*' );
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+class+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
            if ( pattern.test(els[i].className) ) {
                classElements[j] = els[i];
                j++;
            }
        }
        return classElements;
    }
}


