﻿// JScript File

        //*******************************************************************
        //googlemaps API interfacing code
        
        //Global Variables
        var map = null;
        var geocoder = null;
        var CircumPoints = [];
        var center = null;
        var radDist = null;
        var markerArray = [];
        var centerLat = null;
        var centerLong = null;
        var curr_zoom = null;
        var curr_center = null;
        var addressCount = 0;
        var callbackResponseCount=0;
        var polygon;
        var blueIcon = new GIcon(G_DEFAULT_ICON); 
        var greenIcon = new GIcon(G_DEFAULT_ICON); 
        blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png"; 
        greenIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png"; 

        // Set up GMarkerOptions object 
        markerOptionsBlue = {icon:blueIcon};
        markerOptionsGreen = {icon:greenIcon};
        
        //Initialize 
        function initialize(){
        //debugger;
            if (GBrowserIsCompatible()) {
                map = new GMap2($("map"));
                map.setCenter(new GLatLng(38,-98),4);
                map.enableScrollWheelZoom();
                map.addControl(new GLargeMapControl());
                geocoder = new GClientGeocoder();
            }
        } 
        
        //Show Address in Map
        function showZipList(zipList,boundPoints){ 
            initialize();
            CircumPoints= [];
            markerArray = [];
            //debugger;
            
            //showShadow(event);
            
            var zArray = new Array();
            zArray = zipList.split("|");
            var bPointsArray = new Array();
            bPointsArray = boundPoints.split(",");
            
            //Create the Boundary
            var p=0;                                                           
            var minLat = bPointsArray[0];
            var maxLat = bPointsArray[1];
            var minLong = bPointsArray[2];
            var maxLong = bPointsArray[3];
            var latMid = (parseFloat(minLat) + parseFloat(maxLat))/2;
            var longMid= (parseFloat(minLong) + parseFloat(maxLong))/2;
            
            // Set center's lat and long
            centerLat = $('centerLat').value;
            centerLong = $('centerLong').value;
            createGPoint(centerLong,centerLat,'center');
            radDist = maxLat - centerLat; //Radius Diatance
            
            //set Center Point
            var Oy = parseFloat(centerLat);
            var Ox = parseFloat(centerLong)
            center = new GLatLng(Oy,Ox);
            
            //Create the points on Circumference 
            getCircumferencePoints(centerLat,centerLong,radDist);
            polygon = new GPolygon(CircumPoints,"#f33f00", 1, 1, "#ff0000", 0.2); 
            map.removeOverlay(polygon);
            map.addOverlay(polygon); 
            
            callbackResponseCount=0;
            addressCount = zArray.length;
            //Map the points that fall within the circle 
            //and change the marker color to blue for points
            //that fall outside the circle but within bounding square
            for(var i=0;i<=((zArray.length-1));i++)
            {
                var zipAddress = new Array();
                zipAddress = zArray[i].split(";");
                var zPoint = zipAddress[1];
                var clinicDesc = zipAddress[0];
                showAddress(zPoint,clinicDesc);
            }
            renderResultCount();
            showresultbox();
        }
        
        //get address and plot marker using geocoder
        function showAddress(address,desc){
            if (geocoder) {
                geocoder.getLatLng(
                address,
                function(point) {
                    if (!point) {
                        //alert(address + " not found");
                        callbackResponseCount++;
                    } else {
                        callbackResponseCount++;
                        var bWithinRdius = CheckPosition(center,point);
                        if (bWithinRdius==true)
                        {
                            var marker;
                            marker = createInfoMarker(point, desc);
                            markerArray.push(marker);
                            map.addOverlay(marker);
                        }
                        else
                        {
                            //alert('outside circle');
                            var marker;
                            marker = createBoundryMarker(point, desc, blueIcon);
                            markerArray.push(marker);
                            map.addOverlay(marker);
                        }
                        
                        if(callbackResponseCount>=addressCount)
                        {
                            if (timeout)
                            {
                                clearTimeout(timeout);    
                            }
                            hideShadow();
                        }
                        
                    }
                }
                );
            }
        }
        
        //CreateMarker
        function createInfoMarker(point, desc) {
            var marker = new GMarker(point, {title:desc});
            GEvent.addListener(marker, "click",
            function() {
                marker.openInfoWindowHtml(desc);
            }
            );
            return marker;
        }
        
        //Create Boundary/Center Marker
        function createBoundryMarker(point, desc,colour){
           // var markerOptionsString = 'markerOptions' + colour;
            var marker = new GMarker(point, {title:desc,icon:colour});//, eval(markerOptionsString));
            GEvent.addListener(marker, "click",
            function() {
                marker.openInfoWindowHtml(desc);
            }
            );
            return marker;
        }
        
        //Create Points
        function createGPoint(lat,lon,desc){
            var point = new GPoint(lat, lon);
            address = desc;
            var marker;
            if (desc=='center')
            {
                marker = createBoundryMarker(point, address,'Green');
                var zoomLevel;
                var r = $('radius').value;
                if(r<=5)
                {
                    zoomLevel = 11;
                }
                else if(r>=5 && r<=10)
                {
                    zoomLevel = 10;
                }
                else if(r>=10 && r<=30)
                {
                    zoomLevel = 9;
                }
                else if(r>=30 && r<=50)
                {
                    zoomLevel = 8;
                }
                curr_zoom = zoomLevel;
                map.setCenter(marker.getLatLng(), zoomLevel);
                
            }
            else
            {
                marker = createBoundryMarker(point, address,'Blue');
                map.addOverlay(marker);
            }
            
        }
      
        //Check if point inside bounds, falls within circumference
        function CheckPosition(center,point){
            var Cx = parseFloat(center.lng());
            var Cy = parseFloat(center.lat());
            var Px = parseFloat(point.x);
            var Py = parseFloat(point.y);
            var x = Cx - Px;
            var y = Cy - Py;
            //Check radius using Pythogros Theorem
            var dist = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
            if(dist<=radDist)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        //Get Circumference Points
        function getCircumferencePoints(centerLat,centerLong,radDist){
            var b1 = parseFloat(centerLat);
            var a1 = parseFloat(centerLong) + radDist;
            CircumPoints.push(new GLatLng(b1,a1));
            var r = parseFloat(radDist);
            var angle = 1;
            for (var i=angle;i<360;i=i+angle)
            {
                var theta = convertToRadian(i);
                var y = r * Math.sin(theta);
                var x = r * Math.sin(theta) * Math.tan((theta/2));
                var Px = a1-x;
                var Py = b1+y;
                CircumPoints.push(new GLatLng(Py,Px)) ;
            }
            CircumPoints.push(new GLatLng(b1,a1));
        }
        
        //Convert degree to radian
        function convertToRadian(theta){
            var radTheta = theta * (Math.PI/180);
            return radTheta;
        }
        
        //show Marker on click
        function showMarker(clinic){
            for(var i=0;i<markerArray.length;i++)
            {
                var title = markerArray[i].getTitle();
                if(title == clinic)
                {
                    map.setCenter(markerArray[i].getLatLng(),16);
                    markerArray[i].openInfoWindowHtml(clinic);
                    break;
                }
                else
                {
                    title = null;
                }
            }
            if(title == null)
            {
                alert('Could not Locate the Clinic on Map! Please click the MapLocation link.')
            }
        }
        
        //Remove all overlays in the map
        function removeOverlays(){
            if(markerArray.length>0)
            {//debugger;
                for (var i=0;i<markerArray.length;i++)
                {
                   map.removeOverlay(markerArray[i]);
                }
                if(polygon!=null)
                {
                    map.removeOverlay(polygon);
                }
            }
             map.setCenter(new GLatLng(38,-98),4);
        }
        
        //*****************************************************************************
        //utility functions
        function $(id){
            return document.getElementById(id);
        }
        
        //toggleSearch
        function toggleSearchbox(){
            
            if(searchbox.style.display=='block')
            {
               searchbox.style.display = 'none';
               resultbox.style.display = 'block';
            }
            else
            {
               searchbox.style.display = 'block';
               resultbox.style.display = 'none';
            }
        }
        
        //show search box and hide result box
        function showsearchbox(){
            $('searchbox').style.display = 'block';
            $('searchresults-box').style.display = 'none';
            hideLegendA();
            hideToggleMapImg();
        }
        
        //show result box and hide search box
        function showresultbox(){
            $('searchbox').style.display = 'none';
            $('searchresults-box').style.display = 'block';
            showLegendA();
            showToggleMapImg();
        }
        
        var ie6 = ( (navigator.userAgent.indexOf('MSIE 6') > -1) ? true : false );
        var timeout;
        //show shadow
        function showShadow(){
	        var popup = $('popup');
	        var shadow = $('shadow');
	        shadow.style.display='block';
	        var distanceDropdown = $('radius');
	        if (ie6) {
	            if(distanceDropdown != null)
	            {
		            distanceDropdown.style.display = 'none';
		        }
	        }
	        popup.style.display = 'block';
	        timeout = setTimeout("TimedShadowHide()",30000);
        }

        //hide shadow
        function hideShadow(){
	        var popup = $('popup');
	        var distanceDropdown = $('radius');
    	    var shadow = $('shadow');
	        shadow.style.display='none';
	        popup.style.display = 'none';
	        if (ie6) {
	            if(distanceDropdown != null)
	            {
		            distanceDropdown.style.display = 'inline';
		        }
	        }
        }
        
        //render result count
        function renderResultCount(){
            $('rcount').innerHTML = addressCount;
            $('rtotal').innerHTML = addressCount;
        }
        
        //show icon box
        function showiconbox(){
            $('icon-legend').style.display = 'block';
        }
        
        //hide icon box
        function hideiconbox(){
            $('icon-legend').style.display = 'none';
        }
        
        //hide message label on click of search if it is visible
        function hideMessage(){
            var m = $('lblMessage');
            if (m)
            {
                if(m.style.display = 'block')
                {
                    m.style.display = 'none';
                }
            }
        }
        
        //Hide Shadow if the Map Load takes more than 20 sec
        function TimedShadowHide(){
            if($('shadow').style.display == 'block')
            {
                hideShadow();
                alert('Could not load the map completely!');
            }
        }
        
        
        
        //ToggleMapWidth
        function ToggleMapWidth(){
            curr_zoom = map.getZoom();
            curr_center = map.getCenter();
            if ($('content-left').style.display == 'block' || $('content-left').style.display == '')
            {
                $('content-left').style.display = 'none';
                $('content-left').style.width = '0px';
                $('content-right').style.width = '918px';
                //$('map-box').style.width = '818px';
                $('map').style.width = '880px';
                //$('icon-legend').style.width = '822px';
                map.checkResize();
                map.setZoom(curr_zoom);
                $('imgToggleMap').src = '/clinicfinder/images/right-arrow.jpg';
                $('imgToggleMap').alt = 'Diminish Map';
                //animateTimer(418);
            }
            else if ($('content-left').style.display == 'none')
            {
                $('content-left').style.display = 'block';
                $('content-left').style.width = '350px';
                $('content-right').style.width = '500px';
                //$('map-box').style.width = '500px';
                $('map').style.width = '480px';
                //$('icon-legend').style.width = '484px';
                map.checkResize();
                map.setZoom(curr_zoom);
                $('imgToggleMap').src = '/clinicfinder/images/left-arrow.jpg';
                $('imgToggleMap').alt = 'Enlarge Map';
            }
                    map.panTo(curr_center);
        }
        
        //showDarkBorder on Map Container
        function showDarkBorder(){
            //debugger;
            //alert($('map').style.borderColor);
            //$('map').style.borderColor = '#0e71ce';
            $('map').style.borderColor = '#dd5000';
        }
        
        //showLightBorder on Map Container
        function showLightBorder(){
            //debugger;
            $('map').style.borderColor = '#ffffff';
        }
        
        //Show Arrow
        function showToggleMapImg(){
            $('imgToggleMap').style.display = 'block';
        }
        
        //Show Arrow
        function hideToggleMapImg(){
            $('imgToggleMap').style.display = 'none';
        }
        
        //Show Legend Link
        function showLegendA(){
            $('Legend').style.display = 'block';
        }
        
        //Hide Legend Link
        function hideLegendA(){
            $('Legend').style.display = 'none';
        }
        
        //Show Legend Modal Popup
        function showLegend(){
            var popup = $('icon-legend');
	        var shadow = $('shadow');
	        shadow.style.display='block';
	        var distanceDropdown = $('radius');
	        if (ie6) {
	            if(distanceDropdown != null)
	            {
		            distanceDropdown.style.display = 'none';
		        }
	        }
	        popup.style.display = 'block';
        }
        
        //Hide Legend Popup
        function hideLegend(){
            var popup = $('icon-legend');
	        var distanceDropdown = $('radius');
    	    var shadow = $('shadow');
	        shadow.style.display='none';
	        popup.style.display = 'none';
	        if (ie6) {
	            if(distanceDropdown != null)
	            {
		            distanceDropdown.style.display = 'inline';
		        }
	        }
        }

//*****************************************************************************
//az Common: FollowUrl
        function FollowUrl(iLanguage, sURL, bReturn, sWndName, sOptions){
	        var sPrompt;
	        switch (iLanguage)
	        {
		        case 1: // iGlobalEng
			        sPrompt = "This link will take you to a global section of this site maintained by AstraZeneca.\nIt may contain information that does not fall within the indications and guidelines of United States regulatory agencies.\n\nClick Cancel to return or OK to continue."
			        break;
		        case 2: // iThirdPartyEng
			        sPrompt = "This link will take you to a site maintained by a third party who is solely responsible for its contents.\n\nAstraZeneca provides this link as a service to web site visitors. AstraZeneca is not responsible for the Privacy Policy of any third party web sites. We encourage you to read the privacy policy of every web site you visit.\n\nClick Cancel to return or OK to continue."
			        break;
		        case 3: // iGlobalSpan
			        sPrompt = "Este enlace lo llevará a una sección mundial de este sitio mantenida por AstraZeneca.\nEste sitio puede contener información que no entra dentro de las indicaciones y guías de las agencias reglamentarias de Estados Unidos.\n\nHaga clic en Cancelar para regresar o en OK para continuar."
			        break;
		        case 4: //iThirdPartySpan
			        sPrompt = "Este enlace lo llevará a un sitio que es mantenido por terceros, que son los únicos responsables de su contenido.\n\nAstraZeneca proporciona este enlace como un servicio a los visitantes a su sitio web. AstraZeneca no es responsable de la Política de Privacidad de sitios web de terceros. Por favor, lea la política de privacidad de cada sitio web que visite.\n\nHaga clic en Cancelar para regresar o en OK para continuar."
			        break;
		        case 5: // ARIMIDEX
			        sPrompt = "This link will take you to a product Web site where you will find information about a hormonal treatment for breast cancer.\n\nBreast cancer is a malignant tumor that grows in one or both of the breasts. Breast cancer usually develops in the ducts or lobules of the breast. Even after initial treatment for breast cancer, it's possible for breast cancer to come back--or recur--either at the same site as the original tumor or somewhere else in the body. The risk of recurrence is highest in the first 5 years following initial diagnosis, peaking within the first 3 years."
			        break;
		        case 6: // FASLODEX
			        sPrompt = "This link will take you to a product Web site where you will find information about a hormonal treatment for hormone receptor-positive metastatic breast cancer in postmenopausal women whose disease has returned or progressed following antiestrogen therapy.\n\nDoctors use the term \"stage\" to refer to the extent of cancer within the body. Breast cancer staging takes into account the size and location of the primary breast tumor any regional lymph node involvement near the breast and underarm, and any metastatic disease. In stage IV, also known as metastatic cancer, the disease has spread from the breast to other parts of the body such as the bone, liver, lungs, or brain."
			        break;
		        case 7: // iAbraxaneEng
			        sPrompt = "This link will take you to a product Web site where you will find information about a chemotherapy option for metastatic breast cancer.\n\n This site is maintained by Abraxis BioScience who is solely responsible for its contents. Abraxis and AstraZeneca are not responsible for the Privacy Policy of any third party web sites, should you choose to exit an Abraxis or AstraZeneca Web site. We encourage you to read the privacy policy of every web site you visit. Click Cancel to return or OK to continue."
			        break;
	        }
	        if (sWndName == null) sWndName = "opener";
	        if (sOptions == null) sOptions = "";
	        if(! confirm(sPrompt))
		        if (bReturn) return false;
		        else return;
            var externalWin = window.open(sURL, null, sOptions);
	        if (externalWin.opener == null)
                externalWin.opener = window;
	            externalWin.opener.name = sWndName;
	        if (bReturn) return true;
        }