﻿<!DOCTYPE html>
<html>
    <head>
        <title>Patient Alerts for both Tablet and Web Clients</title>
        <style>
            html, body {
              padding-top: 0;
              padding-right: 0;
              padding-bottom: 0;
              padding-left: 0;
              margin: 0;
              font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
              font-weight: 100;
            }
            img {
                margin-left: 3px;
                margin-right: 3px;
                float: left;
                vertical-align: middle;
                border: none;
            }
            a {
                padding-top: 0;
                padding-right: 0;
                padding-bottom: 0;
                padding-left: 0;
            }
        </style>
        <script src="scripts/jquery.min.js"> </script>
        <script src="scripts/json2.js" type="text/javascript"></script>
        <script src="scripts/HTMLSharedUtilities.js" type="text/javascript"></script>
        <script src="../../ClientGlobalContext.js.aspx" type="text/javascript"></script>
        <script src="scripts/alertcommon.js"></script>
    </head>
    <body>﻿     
        <div id="alertdiv"></div>
        <div id="msgDiv"></div>
        <script type="text/javascript">
			var Xrm;
			if (typeof window.opener !== "undefined" && window.opener != null) {
				Xrm = window.opener.Xrm;
			} else {
				Xrm = window.parent.Xrm;
			}
			
			//declare variables to hold branch and rank image names and display text
			var branch, branchdisplay, rank, rankdisplay;

			try {
				//determine the branch - to get the right images, we need Army/Navy/Air Force/Marines/NOAA/Public Health Service/Coast Guard
				branch = Xrm.Page.getAttribute("bah_branch_text").getValue();
				branchdisplay = branch.toUpperCase(); //set text to display in hovers
				
				//branch images are based on branch name with spaces removed - army/navy/airforce/noaa/publichealthservice/coastguard
				branch = branch.replace(/ /g,''); //remove all spaces so we can get right branch image
			} catch (e) {
				branch = null;
			}

			try {
				//determine the rank - to get the right images, we need just text labels (no paygrades) like:
				//Master Sergeant/Petty Officer First Class/Private/Private First Class/Private, One/Private, Two/Rear Admiral/Rear Admiral (lower half)/Sergeant/Sergeant Major/etc.
				rank = Xrm.Page.getAttribute("bah_rank_text").getValue();
				rankdisplay = rank.toUpperCase(); //set text to display in hovers
				
				//rank images are rank name with spaces, commas, parentheses stripped out - mastersergeant.png/rearadmirallowerhalf.png/sergeantmajorofthemarinecorps.png/etc.
				rank = rank.replace(/ /g,'').replace(/,/g,'').replace(/\(|\)/g, ''); //remove spaces, commas, parentheses so we can get right rank image
			} catch (e) {
				rank = null;
			}
			
			
            function getQueryVariable(variable) {
                //This function is used to get a query variable bassed by the CRM IFRAME
                var query = window.location.search.substring(1);
                var vars = query.split("&");
                for (var i = 0; i < vars.length; i++) {
                    var pair = vars[i].split("=");
                    if (pair[0] == variable) {
                        return pair[1];
                    }
                }
                //Xrm.Utility.alertDialog('Patient Alert Resource Page Load Failure: A required Query Variable: ' + variable + ' is missing!');
                return "";
            }

            try {
                var context = GetGlobalContext();
                var serverUrl = context.getClientUrl();
                var userid = context.getUserId();
                var formFactor = 1;         // context.client.getFormFactor();
                //parse the querystring to get the objectid in the data parameter
                parseDataParams();

                // Is this a tablet
                var isTablet = getParamVal("tablet");
                if (typeof isTablet !== "undefined" && isTablet === "true") {
                    formFactor = 2;
                }
                
				var imageHeight = 72;
				var imageWidth = 72;
				var imgpath = "images/";

                // Get ID for Parent
                var crmentityid = getQueryVariable("id");
                if (crmentityid != null && crmentityid !== "") {
                    crmentityid = crmentityid.replace("%7b", "");
                    crmentityid = crmentityid.replace("%7d", "");
                } else {
                    // if not in context, check parent
                    crmentityid = Xrm.Page.data.entity.getId();
                }

                // Remove any brackets
                crmentityid = crmentityid.replace("{", "");
                crmentityid = crmentityid.replace("}", "");

                // Entity Related Values
                msgDiv.innerText = "";
                
            }
            catch (err) {
                //Display Error....
                msgDiv.innerText += "Load Script Error: " + err.message;
            }

            $(document).ready(function () {
                var showHide = false;
                // Show Icons Logic
                var divHTML = '';
				var branchimage = '';

                if (branch) {
					branchimage = imgpath + 'branch_' + branch + '.png';
					divHTML += addImagetoHTMLtoFit(branchimage, imageWidth, imageHeight, branchdisplay);
					if(rank){
						var rankimg = '';
						var rankimg = imgpath + branch + '/' + rank + '.png';
						if(rankimg){
							//debugger;
							divHTML += addImagetoHTMLtoFit(rankimg, null, imageHeight, rankdisplay);
						}
					}
				}
				$('#alertdiv').html(divHTML);
            });
			
			function addImagetoHTMLtoFit(sourceName, maxWidth, maxHeight, spanText) {
				var newHTML = '';
				if(maxWidth){
					newHTML = "<img src='" + sourceName + "' style='visibility:visible; margin-bottom: 0px; margin-top: 0px; max-width:" + maxWidth + "px;' title='" + spanText + "'>";
				}
				else {
					newHTML = "<img src='" + sourceName + "' style='visibility:visible; margin-bottom: 0px; margin-top: 0px; max-height:" + maxHeight + "px;' title='" + spanText + "'>";
				}
				return newHTML;
			}
        </script> 
    </body>
</html> 

