toandersongmail.com / Whitepages Pro Web - Cybersource - BT/ST/Both

// ==UserScript==
// @name         Whitepages Pro Web - Cybersource - BT/ST/Both
// @namespace    http://pro.whitepages.com/
// @version      0.1
// @description  Implement Deep Links to Whitepages Pro Web Identity Check within Cybersource Decision Manager.
// @author       Trevor Anderson <tanderson@whitepages.com>
// @grant        none
// ==/UserScript==

var currURL = document.url || window.location.href || this.href;
//make sure we are on a Decision Manager screen, on the initial load that contains the contact data (there is a subsequent load identified by the hash at the end of the URL that we don't want).
if((currURL.indexOf('DecisionManagerCaseManagementSearchExecute.do') >= 0 || currURL.indexOf('CaseManagementDetailsLoad') >= 0) && (currURL.indexOf('DecisionManagerCaseManagementSearchExecute.do#') == -1) && (currURL.indexOf('CaseManagementDetailsLoad.do#') == -1))
{
    var wpIP, wpEmail, wpBillName, wpBillStreet, wpBillStreet2, wpBillCity, wpBillState, wpBillZip, wpBillPhone, wpShipName, wpShipStreet, wpShipStreet2, wpShipCity, wpShipState, wpShipZip, wpShipPhone;
    var wpErrors = [];

    //find the order info table and iterate over its rows to find the transaction contact data
    var orderInfoTable = document.getElementById('orderInfoDataTbl');

    //if we failed to find this table, then there has been a site change
    if(!orderInfoTable)
        wpErrors.push('Failed to find the order info table due to website change. The Whitepages script will need to be updated.');
    else
    {
        var orderInfoRows = orderInfoTable.rows;

        for(var i = 0; i < orderInfoRows.length; i++)
        {
            var cols = orderInfoRows[i].cells;
            //if this row contains no data, skip it
            if(cols.length < 2)
                continue;
            //the first column should contain text describing the field on this row
            switch(cols[0].innerHTML)
            {
                case 'IP Address:':
                    //the second column contains links to search the IP in addition to the IP itself, so use RegEx to find the IP.
                    var pattern = /\d+\.\d+\.\d+\.\d+/;
                    wpIP = pattern.exec(cols[1].innerHTML);
                    break;
                case 'Email Address:':
                    //the second column contains an anchor tag whose text is the email.
                    wpEmail = cols[1].childNodes[0].text;
                    break;
            }
        }
        //now identify the billing and shipping data.
        if(document.getElementById("billingName"))
            wpBillName = document.getElementById("billingName").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("billingAddress1"))
            wpBillStreet = document.getElementById("billingAddress1").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("billingAddress2"))
            wpBillStreet2 = document.getElementById("billingAddress2").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("billingCity"))
            wpBillCity = document.getElementById("billingCity").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("billingState"))
            wpBillState = document.getElementById("billingState").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("billingZip"))
            wpBillZip = document.getElementById("billingZip").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("phoneLink"))
            wpBillPhone = document.getElementById("phoneLink").text.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        else{
            var pattern = /\d+/;
            var nodes = document.getElementById("billingHead").childNodes;
            var lastNode = nodes[nodes.length-1];
            wpBillPhone = pattern.exec(lastNode.nodeValue);
        }
        if(document.getElementById("shippingName"))
            wpShipName = document.getElementById("shippingName").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("shippingAddress1"))
            wpShipStreet = document.getElementById("shippingAddress1").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("shippingAddress2"))
            wpShipStreet2 = document.getElementById("shippingAddress2").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("shippingCity"))
            wpShipCity = document.getElementById("shippingCity").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("shippingState"))
            wpShipState = document.getElementById("shippingState").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("shippingZip"))
            wpShipZip = document.getElementById("shippingZip").innerHTML.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        if(document.getElementById("shipPhoneLink"))
            wpShipPhone = document.getElementById("shipPhoneLink").text.trim().replace("&nbsp;"," ").replace(/\s+/g," ");
        else{
            var pattern = /\d{10}\d*/;
            var nodes = document.getElementById("shippingHead");
            var lastNode = nodes.childNodes[nodes.childNodes.length-1];
            wpShipPhone = pattern.exec(lastNode.nodeValue);
        }
    }
    //now we have all the input data, so we can build the Pro Web Identity Check URLs
    billAPIURL = 'https://pro.lookup.whitepages.com/identity_checks?'
    billAPIURL += 'name='+encodeURIComponent(wpBillName)+'&';
    if(wpBillPhone != null)
        billAPIURL += 'phone='+encodeURIComponent(wpBillPhone)+'&';
    billAPIURL += 'address_street_line_1='+encodeURIComponent(wpBillStreet)+'&';
    if(document.getElementById("billingAddress2"))
            billAPIURL += 'address_street_line_2='+encodeURIComponent(wpBillStreet2)+'&';
    billAPIURL += 'address_city='+encodeURIComponent(wpBillCity)+'&';
    billAPIURL += 'address_state_code='+encodeURIComponent(wpBillState)+'&';
    billAPIURL += 'address_postal_code='+encodeURIComponent(wpBillZip)+'&';
    billAPIURL += 'email_address='+encodeURIComponent(wpEmail)+'&';
    billAPIURL += 'ip_address='+encodeURIComponent(wpIP);
    
    shipAPIURL = 'https://pro.lookup.whitepages.com/identity_checks?'
    shipAPIURL += 'name='+encodeURIComponent(wpShipName)+'&';
    if(wpShipPhone != null)
        shipAPIURL += 'phone='+encodeURIComponent(wpShipPhone)+'&';
    if(wpShipStreet != null){
        shipAPIURL += 'address_street_line_1='+encodeURIComponent(wpShipStreet)+'&';
        if(document.getElementById("shippingAddress2"))
                shipAPIURL += 'address_street_line_2='+encodeURIComponent(wpShipStreet2)+'&';
        shipAPIURL += 'address_city='+encodeURIComponent(wpShipCity)+'&';
        shipAPIURL += 'address_state_code='+encodeURIComponent(wpShipState)+'&';
        shipAPIURL += 'address_postal_code='+encodeURIComponent(wpShipZip)+'&';
    }
    shipAPIURL += 'email_address='+encodeURIComponent(wpEmail)+'&';
    shipAPIURL += 'ip_address='+encodeURIComponent(wpIP);
    
    fullURL = 'https://pro.lookup.whitepages.com/identity_checks?'
    fullURL += 'billing_name='+encodeURIComponent(wpBillName)+'&';
    if(wpBillPhone != null)
        fullURL += 'phone='+encodeURIComponent(wpBillPhone)+'&';
    fullURL += 'billing_address_street_line_1='+encodeURIComponent(wpBillStreet)+'&';
    if(document.getElementById("billingAddress2"))
            fullURL += 'billing_address_street_line_2='+encodeURIComponent(wpBillStreet2)+'&';
    fullURL += 'billing_address_city='+encodeURIComponent(wpBillCity)+'&';
    fullURL += 'billing_address_state_code='+encodeURIComponent(wpBillState)+'&';
    fullURL += 'billing_address_postal_code='+encodeURIComponent(wpBillZip)+'&';
    fullURL += 'shipping_name='+encodeURIComponent(wpShipName)+'&';
    if(wpShipStreet != null){
        fullURL += 'shipping_address_street_line_1='+encodeURIComponent(wpShipStreet)+'&';
        if(document.getElementById("shippingAddress2"))
                fullURL += 'shipping_address_street_line_2='+encodeURIComponent(wpShipStreet2)+'&';
        fullURL += 'shipping_address_city='+encodeURIComponent(wpShipCity)+'&';
        fullURL += 'shipping_address_state_code='+encodeURIComponent(wpShipState)+'&';
        fullURL += 'shipping_address_postal_code='+encodeURIComponent(wpShipZip)+'&';
    }
    fullURL += 'email_address='+encodeURIComponent(wpEmail)+'&';
    fullURL += 'ip_address='+encodeURIComponent(wpIP);
    
    //edit the phone links to go to Pro Web instead of whitepages.com
    if(document.getElementById("phoneLink"))
        document.getElementById("phoneLink").href = 'https://pro.lookup.whitepages.com/phones?number='+encodeURIComponent(wpBillPhone);
    if(document.getElementById("shipPhoneLink"))
        document.getElementById("shipPhoneLink").href = 'https://pro.lookup.whitepages.com/phones?number='+encodeURIComponent(wpShipPhone);
    
    //now insert links for these into billing and shipping sections
    
    var a = document.createElement("a");
	var linkText = document.createTextNode("BT-WPP");
	a.href = billAPIURL;
	a.target = "_blank";
	a.appendChild(linkText);
	a.style.font = "bold 14px calibri";
	a.style.color = "#F37320";
	document.getElementById('billingHead').appendChild(a);
    
    var a = document.createElement("a");
	var linkText = document.createTextNode("ST-WPP");
	a.href = shipAPIURL;
	a.target = "_blank";
	a.appendChild(linkText);
	a.style.font = "bold 14px calibri";
	a.style.color = "#F37320";
	document.getElementById('shippingHead').appendChild(a);
	
	var a = document.createElement("a");
	var linkText = document.createTextNode("BT and ST-WPP");
	a.href = fullURL;
	a.target = "_blank";
	a.appendChild(linkText);
	a.style.font = "bold 14px calibri";
	a.style.color = "#F37320";
	//find the table containing the billing and shipping columns
	var billingHeadTd = document.getElementById("billingHead");
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	td.style = "text-align:center;";
	td.style.textAlign='center';
	td.colSpan = 2;
	td.appendChild(a);
	tr.appendChild(td);
	billingHeadTd.parentNode.parentNode.parentNode.appendChild(tr);
}