oobayly / ValuationAnywhereAutoVRM

// ==UserScript==
// @name       ValuationAnywhereAutoVRM
// @namespace  http://www.valuationanywhere.co.uk
// @version    0.5.2
// @description  [OBSOLETE] Auto populates the Valuation Anywhere lookup form with the VRM and miles, and more...
// @match      http://example.com
// @copyright  2014 - A subscriber who wanted more.
// @require    http://code.jquery.com/jquery-latest.js
// ==/UserScript==

$(document).ready(function() {
/*    var main = function() {
        var path = window.location.pathname.substr(1);
        
        var params = null;
        if (window.location.hash) {
            var hash = decodeURIComponent(window.location.hash.substr(1));
            try {
                params = JSON.parse(hash);
            } catch (e) {
                console.log("Couldn't parse '" + hash + "': " + e);
            }
        }
        
        if (!params) {
            params = {};

            // Parse the URI and extract all the QueryString name-value pairs
            querystring = window.location.search;
            querystring = querystring.substring(querystring.indexOf('?')+1).split('&');
            var pair, d = decodeURIComponent;
            // march and parse
            for (var i = querystring.length - 1; i >= 0; i--) {
                pair = querystring[i].split('=');
                params[d(pair[0])] = d(pair[1]);
            }
        }
        
        if (path === "" || path === "Default.aspx") {
            if (params.capID) {
                capIDLookup(params.capID, params.miles, params.month, params.year);
            } else if (params.vrm || params.miles) {
                vrmLookup(params.vrm, params.miles);
            }
        } else if ((/^Valuation.aspx/).test(path)) {
            addCapIDLink();
            injectCopyValuation();
        }
        
        // We already pay for the damn service, why do we need an advert taking up half the page.
        $("#blackbookplus-upgrade").remove();
    };
    
    var addCapIDLink = function() {
        // Get the mileage
        var miles = $("#your-mileage").val().replace(/[^0-9]/g, "");
        
        // Information is now stored in the summary table
        var items = $(".summary-container .list-table li");

       	// Registration date
        var dateField = items.find("span:contains('Registration Date')");
        var regDate = new Date(dateField.next().text());
        var month = regDate.getMonth() + 1;
        var year = regDate.toLocaleString("en-GB", { year: "numeric" });
        
        // Derivative description - search the manufacturer-panel for something that looks like a CAP ID
        var idContainer = null;
        var id, derivative;
        for (var item of $(".manufacturer-panel .col-car-details li").toArray()) {
            var match = $(item).text().match(/^\[([0-9]+)\] (.+)/);
            if (match) {
                idContainer = $(item);
                id = match[1];
                derivative = match[2];
                break;
            }
        }
        
        if (!id)
            return;
        
        idContainer.empty();
        $("<a>", {
            href: "/#" + JSON.stringify({
                capID: id,
                miles: miles,
                month: month,
                year: year
            }),
            title: "Use this to find another a similar vehicle.",
            target: "_blank",
        }).text("[" + id + "]").appendTo(idContainer);
        $("<span>").text(" " + derivative).appendTo(idContainer);
    };
    
    var ZeroClipboard_copy = function(e) {
        try {
            var data = {};
            
            var monthly = $("#blackbookTplWrapper .table-va-values");
            var live = $("#blackbookLiveTplWrapper .table-va-values");
            
            data.CAPClean = monthly.find(".clean").text().replace(/[^0-9]/g, "");
            data.CAPAverage = monthly.find(".average").text().replace(/[^0-9]/g, "");
            data.CAPBelow = monthly.find(".below").text().replace(/[^0-9]/g, "");
            data.CAPLiveClean = live.find(".clean").text().replace(/[^0-9]/g, "");
            data.CAPLiveAverage = live.find(".average").text().replace(/[^0-9]/g, "");
            data.CAPLiveBelow = live.find(".below").text().replace(/[^0-9]/g, "");

            data.CAPMileage = $("#your-mileage").val();
            data.CAPDate = $("#your-date").val();

            // Information is now stored in the summary table
            var items = $(".summary-container .list-table li");
            var field;

            // VED
            field = items.find("span:contains('Vehicle Excise Duty')");
            data.VED = field.next().text();

            // ID
            field = items.find("span:contains('CAP ID')");
            data.CAPID = field.next().text();

            // Vehicle description
            field = items.find("span:contains('Manufacturer')");
            data.CAPNotes = field.next().text();
            field = items.find("span:contains('Model')");
            data.CAPNotes += " " + field.next().text();
            field = items.find("span:contains('Trim')");
            data.CAPNotes += " " + field.next().text();
            var text = "";
            for (var key in data) {
                text += key + ":" + data[key] + "\n";
            }

            e.clipboardData.setData("text/plain", text);
        
        } catch (err) {
            alert("Data couldn't be copied:\n" + err);
        }
    };
    
    var injectCopyValuation = function() {
        var button = $("<button>", {
            "class": "copy_button btn btn-sm cap-btn-green",
        });
        button.text("Copy Valuation Values");
        button.on("click", function(e) {return false;});
        $("#fn_ShowPDFDialog").after(button);
        button.before(document.createTextNode(" "));
        
        ZeroClipboard.config( { swfPath: "//cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.1.6/ZeroClipboard.swf" } );
        var client = new ZeroClipboard($(".page-functions.Options .copy_button"));
        client.on("ready", function(e) {
        	client.on("copy", ZeroClipboard_copy);
        });
    };
      
    var capIDLookup = function(id, miles, month, year) {
        $("#vlBrowse a")[0].click();
        
        $("#capIDcont input").val(id);
        $("#vmMMDMile input").val(miles);
        
        var sel = $("#vlReg select");
        sel.first().val(month);
        sel.last().val(year);
        
        window.setTimeout(function() {
        	$("#SearchCAPid a")[0].click();
        }, 250);
    };
  
    var vrmLookup = function(vrm, miles) {
        // Round miles up to the next 1000
        miles = 1000 * Math.ceil(miles / 1000, 0);
        
        if (vrm)
            $("#tbVRM").val(vrm);
        if (miles)
            $("#tbMiles").val(miles);

        // Only execute the search if we have both the VRM and the Mileage
        if (vrm && miles) {
            $(".fn_VRMLookup")[0].click();
        }
    };

    if (document.title.indexOf("UltraDNS") != -1) {
        document.title = "Redirecting...";
        window.location = window.location.toString().replace(/(www.)?valuationanywhere.co.uk/, "valuationanywhere.cap.co.uk");
        return;
    } else {
        $.getScript("https://cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.1.6/ZeroClipboard.js", function(response) {
            main();
        });
    }*/
});