//Author: Stephen McCall
//Date: 6/20/2012
//Upgraded and cleaned up by Subin Fernandez on 20 Mar 2014
//The purpose of this script is to correct the format of the URLs used within the anchor tags surrounding the "Directions to Investor Center" links on the
//HTML pages that are output by the script at the following location: /dfs/personal/src/gen/centers/centers.pl
//These changes were required in order to support the new Branch Locator application launched in March of 2012.
//This script requires jquery-1.7.1.min.js
//GLOBALS
var callBack = "getMapQuestData";
var jsonResponseArray = [];
var numberOfLinks = 0;
var url = "http://www.fidelity.com/branchlocator/directions.shtml?branchId=";
$(document).ready(function(){
$(".seminar-address").each(function(){
numberOfLinks++; /*Count up the number of links on the page*/
});
//Collect all of the parent elements containing the "Directions to Investor Center" links
$(".seminar-address").each(function(){
//var origin = $(this).find(".address").html().split(" "); /*Extract the ZIP code from the address*/
var origin = $(this).find(".address").html().replace(/ +(?= )/g,'').split(" "); /*Extract the ZIP code from the address, after removing any two or more whitespaces*/
origin = jQuery.grep(origin, function(n){return (n);}); /*Clean up empty elements in array*/
for (var i = 0; i < origin.length; i++){
if(origin[i].indexOf(",") != -1){
if(origin[i+1].length == 2){
if(origin[i+2].length == 5 && $.isNumeric(origin[i+2])){
origin = origin[i+2];
break;
}
}
}
}
//origin = origin[origin.length-1];/*Zip code should always appear as the last element*/
var RecordId = $(this).find(".directions-link").data("branchid"); /*Extract the keyId from the current URL used*/
var mapQuestURL = "http://www.fidelity.com/mapquest/search/v2/radius?key=Gmjtd%7Cluua2l07nd%2C85%3Do5-lzbsu&outFormat=json&origin=" + origin + "&hostedData=mqap.37350_BranchProduction&RecordId=" + RecordId + "&radius=10&units=m&callback=" + callBack;
var mapQuestScriptTag = "";
$("head").append(mapQuestScriptTag);
});
});
//This is the callback function defined in the call to the MapQuest API above
getMapQuestData = function (json){
jsonResponseArray.push(json); /*Add each repsonse object received to an array*/
if (jsonResponseArray.length == numberOfLinks){ /*Check to make sure that all of the script tags have returned*/
$(".directions-link").each(function (index){
try{
/*The repsonses from the script calls are not guaranteed to have executed sequentially. Use the RecordId to determine which response object in the jsonResponseArray is the correct one to use for each link*/
RecordId = $(this).data("branchid");
$(this).html("Directions Unavailable"); /*If record found, this text will be changed*/
for(i=0;i