﻿<html>

<head>
    <meta charset="utf-8" />
</head>

<body onload="address()">
    <!--This JavaScript code pulls the address out of the 5 To Address fields,
        on the mileage expense form, and places them in a single field to control the Bing Map-->
    <script type="text/javascript">

        function address(){

            //pull values of the 5 to address fields and place them in variables
            var line1 = window.parent.Xrm.Page.getAttribute("btsss_addresstomileage").getValue();
            var line2 = window.parent.Xrm.Page.getAttribute("btsss_address2tomileage").getValue();
            var city = window.parent.Xrm.Page.getAttribute("btsss_addresstocitymileage").getValue();
            var state = window.parent.Xrm.Page.getAttribute("btsss_addresstostatemileage").getValue();
            var zip = window.parent.Xrm.Page.getAttribute("btsss_addresstozipmileage").getValue();

            //check to see if line2 has a value
            if (line2 != null) { //if it has value, add it to the full to address field
                window.parent.Xrm.Page.getAttribute("btsss_facilityaddress").setValue(line1 + ', ' + line2 + ', ' + city + ', ' + state + ', ' + zip);
            }
            else { //if it does not have a value, do not add it to the full to address field
                window.parent.Xrm.Page.getAttribute("btsss_facilityaddress").setValue(line1 + ', ' + city + ', ' + state + ', ' + zip);
            }

            
            //pull values of the 5 from address fields and place them in variables
            var line1from = window.parent.Xrm.Page.getAttribute("btsss_addressfrommileage").getValue();
            var line2from = window.parent.Xrm.Page.getAttribute("btsss_address2frommileage").getValue();
            var cityfrom = window.parent.Xrm.Page.getAttribute("btsss_addressfromcitymileage").getValue();
            var statefrom = window.parent.Xrm.Page.getAttribute("btsss_addressfromstatemileage").getValue();
            var zipfrom = window.parent.Xrm.Page.getAttribute("btsss_addressfromzipmileage").getValue();

            //check to see if line2from has a value
            if (line2from != null) { //if it has value, add it to the full from address field
                window.parent.Xrm.Page.getAttribute("btsss_beneficiaryaddress").setValue(line1from + ', ' + line2from + ', ' + cityfrom + ', ' + statefrom + ', ' + zipfrom);
            }
            else { //if it does not have a value, do not add it to the full from address field
                window.parent.Xrm.Page.getAttribute("btsss_beneficiaryaddress").setValue(line1from + ', ' + cityfrom + ', ' + statefrom + ', ' + zipfrom);
            }

            //saves the form
            window.parent.Xrm.Page.data.entity.save();
        }

    </script>



</body>
</html>