<HTML><HEAD><TITLE></TITLE>
<META charset=utf-8></HEAD>
<BODY style="MARGIN: 0px; WIDTH: 100%; HEIGHT: 100%" onload="createMap(); FindLocation();">﻿ ﻿
<SCRIPT type=text/javascript src="https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&amp;s=1"></SCRIPT>

<SCRIPT type=text/javascript>
    var map = null;
    var pos = null;
    var controls = parent.Xrm.Page.ui.controls;
    var lat = controls.get("address1_latitude").getAttribute().getValue();
    var lng = controls.get("address1_longitude").getAttribute().getValue();
    var fldname = window.location.search.substr(6);
    if (fldname == "") fldname = "name";
    var acctname = controls.get(fldname).getAttribute().getValue();


    function FindLocation() {
        if (acctname == null) {
            return;
        }
        else if (lat == null || lng == null) {
            var street = controls.get("address1_line1").getAttribute().getValue();
            var city = controls.get("address1_city").getAttribute().getValue();
            var state = controls.get("address1_stateorprovince").getAttribute().getValue();
            var zip = controls.get("address1_postalcode").getAttribute().getValue();
            var address = street + ", " + city + ", " + state + " " + zip;
            query = address;
        } else {
            query = lat + ", " + lng;
        }
        map.getCredentials(CallSearchService);
    }

    function CallSearchService(credentials) {
        var searchRequest = 'https://dev.virtualearth.net/REST/v1/Locations/' + query + '?output=json&jsonp=SearchServiceCallback&key=' + credentials;
        var mapscript = document.createElement('script');
        mapscript.type = 'text/javascript';
        mapscript.src = searchRequest;
        document.getElementById('mapDiv').appendChild(mapscript);
    }
    function SearchServiceCallback(result) {
        var output = document.getElementById("output");
        if (output) {
            while (output.hasChildNodes()) {
                output.removeChild(output.lastChild);
            }
        }
        if (result && result.resourceSets && result.resourceSets.length > 0 && result.resourceSets[0].resources && result.resourceSets[0].resources.length > 0) {
            var bbox = result.resourceSets[0].resources[0].bbox;
            var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(bbox[0], bbox[1]), new Microsoft.Maps.Location(bbox[2], bbox[3]));
            map.setView({ zoom: 15, center: new Microsoft.Maps.Location(result.resourceSets[0].resources[0].point.coordinates[0], result.resourceSets[0].resources[0].point.coordinates[1]) });
            var location = new Microsoft.Maps.Location(result.resourceSets[0].resources[0].point.coordinates[0], result.resourceSets[0].resources[0].point.coordinates[1]);
            controls.get("address1_latitude").getAttribute().setValue(result.resourceSets[0].resources[0].point.coordinates[0]);
            controls.get("address1_longitude").getAttribute().setValue(result.resourceSets[0].resources[0].point.coordinates[1]);
            var pushpin = new Microsoft.Maps.Pushpin(location);
            map.entities.push(pushpin);
        } else {
            if (typeof (response) == 'undefined' || response == null) {
                //alert("Invalid credentials or no response");
            }
            else {
                if (typeof (response) != 'undefined' && response && result && result.errorDetails) {
                    resultsHeader.innerHTML = "Message :" + response.errorDetails[0];
                }
                //alert("No results for the query");
            }
        }
    }

    function createMap() {
        //**************** PLEASE NOTE: This credential key is not to be used for any purpose other than CRM demonstrations demployed with the CRM Demobuilder utility. If you wish to re-use this functionality elsewhere, please sign up for your own key at https://www.bingmapsportal.com/

        map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),
                           { credentials: "AjMKWvJyfkpwpLfU15YkH2Vz_TV5lg7jd6cT-LbYzPr3FszezhkRPlFWTWG6lA6B",
                               center: new Microsoft.Maps.Location(45.5, -122.5),
                               mapTypeId: Microsoft.Maps.MapTypeId.aerial,
                               zoom: 15
                           });
    }
    </SCRIPT>
 
<DIV style="POSITION: relative; WIDTH: 100%; HEIGHT: 100%; TOP: -20px" id=mapDiv></DIV>
<SCRIPT type=text/javascript src="https://ecn.dev.virtualearth.net/mapcontrol/v7.0/js/bin/7.0.2011110595516.00/en-us/veapicore.js"></SCRIPT>

<SCRIPT type=text/javascript src="https://ecn.dev.virtualearth.net/mapcontrol/v7.0/7.0.20120925131809.93/js/en-us/veapicore.js"></SCRIPT>

<SCRIPT type=text/javascript src="https://ecn.dev.virtualearth.net/mapcontrol/v7.0/7.0.20121012100453.93/js/en-us/veapicore.js"></SCRIPT>

<SCRIPT type=text/javascript src="https://ecn.dev.virtualearth.net/mapcontrol/v7.0/7.0.20121012100453.93/js/en-us/veapicore.js"></SCRIPT>
</BODY></HTML>