﻿/// <reference path="VeJavaScriptIntellisenseHelper.js" />
    var map = null;
    var GroundLayer = null;
    var AirLayer = null;
    var shapeMSN03;var shapeMSN14;var shapeMSN18;var shapeMSN21;var shapeMSN32;var shapeMSN36;
    var shapeOSH09;var shapeOSH18;var shapeOSH27;var shapeOSH36L;var shapeOSH36R;
    var shapeMSN03a;var shapeMSN14a;var shapeMSN18a;var shapeMSN21a;var shapeMSN32a;var shapeMSN36a;
    var shapeOSH09a;var shapeOSH18a;var shapeOSH27a;var shapeOSH36La;var shapeOSH36Ra;
    var RouteGroundColor= new VEColor(255,255,0,.75);
    var RouteAirColor= new VEColor(255,0,0,1);
    var RouteWidth=5;
    var EnrouteAltitude = 500;
    var PatternAltitude = 100;
    var SurfaceAltitude = 0;
    var currentroute = new Array();
    var routewaypoints = new Array();
    var currentstep = -1;
    var flyzoomlevel=16;
    var initialzoomlevel=13;
    var flypitchlevel=-20;
    var initialpitchlevel=-90;
    var pitchtarget = initialpitchlevel;
    var headingtarget = -1.0;
    var timerID = null;
    var timerinterval=2500;  //2.5 seconds
    var simulationmode=false;
    var testmode=false;

    
    
    var EARLY       = new VELatLong(43.83140,-88.59390,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var EARLY_a     = new VELatLong(43.83140,-88.59390,EnrouteAltitude,VEAltitudeMode.RelativeToGround);
    var K87Y        = new VELatLong(43.10000,-89.18330,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var K87Y_a      = new VELatLong(43.10000,-89.18330,EnrouteAltitude,VEAltitudeMode.RelativeToGround);
    var LATE        = new VELatLong(43.90820,-88.51250,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var LATE_a      = new VELatLong(43.90820,-88.51250,EnrouteAltitude,VEAltitudeMode.RelativeToGround);
    var MERGE       = new VELatLong(43.38330,-89.06670,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MERGE_a     = new VELatLong(43.38330,-89.06670,EnrouteAltitude,VEAltitudeMode.RelativeToGround);
    var MSN03_S     = new VELatLong(43.13830,-89.33800,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN03_R     = new VELatLong(43.14430,-89.33350,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN03_T     = new VELatLong(43.16690,-89.31660,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN03_Ta    = new VELatLong(43.16690,-89.31660,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var MSN14_S     = new VELatLong(43.14390,-89.33650,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN14_R     = new VELatLong(43.13930,-89.33050,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN14_T     = new VELatLong(43.10000,-89.27800,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN14_Ta    = new VELatLong(43.10000,-89.27800,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var MSN18_S     = new VELatLong(43.14340,-89.34140,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN18_R     = new VELatLong(43.13810,-89.34160,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN18_T     = new VELatLong(43.10000,-89.34330,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN18_Ta    = new VELatLong(43.10000,-89.34330,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var MSN21_S     = new VELatLong(43.14670,-89.33170,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN21_R     = new VELatLong(43.14050,-89.33640,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN21_T     = new VELatLong(43.10000,-89.36700,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN21_Ta    = new VELatLong(43.10000,-89.36700,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var MSN32_S     = new VELatLong(43.13930,-89.33050,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN32_R     = new VELatLong(43.14390,-89.33640,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN32_T     = new VELatLong(43.16020,-89.35760,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN32_Ta    = new VELatLong(43.16020,-89.35760,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var MSN36_S     = new VELatLong(43.13810,-89.34160,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN36_R     = new VELatLong(43.14340,-89.34130,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN36_T     = new VELatLong(43.17220,-89.34000,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var MSN36_Ta    = new VELatLong(43.17220,-89.34000,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var OSH09_GD    = new VELatLong(43.87230,-88.59390,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH09_GDa   = new VELatLong(43.87230,-88.59390,EnrouteAltitude,VEAltitudeMode.RelativeToGround);
    var OSH09_F     = new VELatLong(43.99188,-88.59270,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH09_Fa    = new VELatLong(43.99188,-88.59270,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var OSH09_TD    = new VELatLong(43.99158,-88.56620,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH18_LB    = new VELatLong(43.98870,-88.51250,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH18_LBa   = new VELatLong(43.98870,-88.51250,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var OSH18_F     = new VELatLong(43.98870,-88.55637,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH18_Fa    = new VELatLong(43.98870,-88.55637,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var OSH18_TD    = new VELatLong(43.97890,-88.55652,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH27_F     = new VELatLong(43.99090,-88.51250,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH27_Fa    = new VELatLong(43.99090,-88.51250,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var OSH27_TD    = new VELatLong(43.99150,-88.55430,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH36L_F    = new VELatLong(43.95630,-88.55680,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH36L_Fa   = new VELatLong(43.95630,-88.55680,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var OSH36L_TD   = new VELatLong(43.97420,-88.55656,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH36R_F    = new VELatLong(43.95630,-88.55480,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var OSH36R_Fa   = new VELatLong(43.95630,-88.55480,PatternAltitude,VEAltitudeMode.RelativeToGround);
    var OSH36R_TD   = new VELatLong(43.97420,-88.55449,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var TURNN       = new VELatLong(43.86670,-88.55650,SurfaceAltitude,VEAltitudeMode.RelativeToGround);
    var TURNN_a     = new VELatLong(43.86670,-88.55650,EnrouteAltitude,VEAltitudeMode.RelativeToGround);
    
    var pMSN03    = [MSN03_S,MSN03_R,MSN03_T,MERGE];
    var pMSN03a   = [MERGE_a,MSN03_Ta,MSN03_R,MSN03_S]; //Reverse order in case of Polygon
    var pMSN03n   = ['MSN 03 Start','MSN 03 Rotate','MSN 03 Initial Turn','MERGE'];
    var pMSN14    = [MSN14_S,MSN14_R,MSN14_T,K87Y,MERGE];
    var pMSN14a   = [MERGE_a,K87Y_a,MSN14_Ta,MSN14_R,MSN14_S];
    var pMSN14n   = ['MSN14 Start','MSN 14 Rotate','MSN 14 Initial Turn','87Y','MERGE'];
    var pMSN18    = [MSN18_S,MSN18_R,MSN18_T,K87Y,MERGE];
    var pMSN18a   = [MERGE_a,K87Y_a,MSN18_Ta,MSN18_R,MSN18_S];
    var pMSN18n   = ['MSN18 Start','MSN 18 Rotate','MSN 18 Initial Turn','87Y','MERGE'];
    var pMSN21    = [MSN21_S,MSN21_R,MSN21_T,K87Y,MERGE];
    var pMSN21a   = [MERGE_a,K87Y_a,MSN21_Ta,MSN21_R,MSN21_S];
    var pMSN21n   = ['MSN21 Start','MSN 21 Rotate','MSN 21 Initial Turn','87Y','MERGE'];
    var pMSN32    = [MSN32_S,MSN32_R,MSN32_T,MERGE];
    var pMSN32a   = [MERGE_a,MSN32_Ta,MSN32_R,MSN32_S];
    var pMSN32n   = ['MSN32 Start','MSN 32 Rotate','MSN 32 Initial Turn','MERGE'];
    var pMSN36    = [MSN36_S,MSN36_R,MSN36_T,MERGE];
    var pMSN36a   = [MERGE_a,MSN36_Ta,MSN36_R,MSN36_S];
    var pMSN36n   = ['MSN36 Start','MSN 36 Rotate','MSN 36 Initial Turn','MERGE'];
    var pOSH09    = [MERGE,EARLY,OSH09_GD,OSH09_F,OSH09_TD];
    var pOSH09a   = [OSH09_TD,OSH09_Fa,OSH09_GDa,EARLY_a,MERGE_a];
    var pOSH09n   = ['MERGE','EARLY','OSH 09 Gear Down','OSH 09 Final','OSH 09 Touch Down'];
    var pOSH18    = [MERGE,LATE,OSH18_LB,OSH18_F,OSH18_TD];
    var pOSH18a   = [OSH18_TD,OSH18_Fa,OSH18_LBa,LATE_a,MERGE_a];
    var pOSH18n   = ['MERGE','LATE','OSH 18 Left Base','OSH 18 Final','OSH 18 Touch Down'];
    var pOSH27    = [MERGE,LATE,OSH27_F,OSH27_TD];
    var pOSH27a   = [OSH27_TD,OSH27_Fa,LATE_a,MERGE_a];
    var pOSH27n   = ['MERGE','LATE','OSH 27 Final','OSH 27 Touch Down'];
    var pOSH36L   = [MERGE,TURNN,OSH36L_F,OSH36L_TD];
    var pOSH36La  = [OSH36L_TD,OSH36L_Fa,TURNN_a,MERGE_a];
    var pOSH36Ln  = ['MERGE','TURNN','OSH 36L Final','OSH 36L Touch Down'];
    var pOSH36R   = [MERGE,TURNN,OSH36R_F,OSH36R_TD];
    var pOSH36Ra  = [OSH36R_TD,OSH36R_Fa,TURNN_a,MERGE_a];
    var pOSH36Rn  = ['MERGE','TURNN','OSH 36R Final','OSH 36R Touch Down'];
    
    var waypoints = [['MSN03',pMSN03n],['MSN14',pMSN14n],['MSN18',pMSN18n],['MSN21',pMSN21n],['MSN32',pMSN32n],['MSN36',pMSN36n],['OSH09',pOSH09n],['OSH18',pOSH18n],['OSH27',pOSH27n],['OSH36L',pOSH36Ln],['OSH36R',pOSH36Rn]];
    
    var currentheading= 360; //theForm.tbHeading.value;
    var currentaltitude = SurfaceAltitude;
    var currentposition = MSN03_S;
    var currentzoom = initialzoomlevel;
    var currentpitch=initialpitchlevel;
    
    function setcurrentmapview(){map.SetMapView(new VEMapViewSpecification(currentposition,currentzoom, currentaltitude, currentpitch, Math.min(currentheading, 359.999999)));}
    
    function pageUnload(){if(map != null) map.Dispose();}
    
    function pageLoad(){
        map = new VEMap('myMap');
        map.onLoadMap=maploaded;
        var x = new VEMapOptions;
        x.EnableBirdseye=false;
        x.EnableDashboardLabels = true;
        //theForm.getElementById("MSVE_obliqueNotification").style.visibility = "hidden";
        currentheading=theForm.tbHeading.value;
        currentzoom=initialzoomlevel;
        map.LoadMap(currentposition,currentzoom,VEMapStyle.Hybrid, false, VEMapMode.Mode3D,true,0,x);
        ShowRoadsChanged(); //will set the map to the proper default
        simulationmode=false;
    }
    
    function maploaded(){
        timerID=setInterval("orientate()",timerinterval);   //let map settle before final position
        map.AttachEvent("onendpan",panended);
        GroundLayer=new VEShapeLayer();
        GroundLayer.SetTitle("GroundLayer");
        GroundLayer.SetDescription("Route paths at ground level");
        map.AddShapeLayer(GroundLayer);
        AirLayer=new VEShapeLayer();
        AirLayer.SetTitle("AirLayer");
        AirLayer.SetDescription("Route paths with altitude profiles");
        map.AddShapeLayer(AirLayer);
        AddDepartures();
        AddArrivals();
        ClearDeparture();
        ClearArrival();
        ShowDepartureMSN03();
        ShowArrivalOSH36L();
        ShowAltitudeChanged();  //will set the red line to proper value
    }

    function orientate() {
        clearInterval(timerID);     //dispose of timer
//        map.SetCenterAndZoom(MSN03_S,initialzoomlevel);
        pitchtarget = initialpitchlevel;//implemented by panended event
        headingtarget = theForm.tbHeading.value;         //implemented by panended event
        if (map.GetMapMode()==VEMapMode.Mode3D)
            {setcurrentmapview();}
        else
            {theForm.cbShowAltitude.checked=false;ShowAltitudeChanged();}   
//            map.SetHeading(Math.min(headingtarget, 359.999999));
//            map.SetPitch(pitchtarget);
//        }
    }

    function panended(){
       if ((headingtarget >= 0) || (pitchtarget <=0)) theForm.tbHeading.value=headingtarget.toString();
       if (map.GetMapMode()==VEMapMode.Mode3D){   
          if (headingtarget >= 0) currentheading= headingtarget;//map.SetHeading(Math.min(headingtarget,359.999999));
          if (pitchtarget <= 0) currentpitch=pitchtarget; //map.SetPitch (pitchtarget);
          if (testmode) 
              {
              theForm.tbMapPitch.value = map.GetPitch();
              theForm.tbMapZoom.value = map.GetZoomLevel();
              var c= map.GetCenter();
              theForm.tbMapLat.value= c.Latitude;
              theForm.tbMapLon.value= c.Longitude;
              theForm.tbMapHeading.value=map.GetHeading();
              theForm.tbMapAltitude.value=map.GetAltitude();
              theForm.tbCurrentPitch.value=currentpitch;
              theForm.tbCurrentAltitude.value=currentaltitude;
              theForm.tbCurrentLat.value=currentposition.Latitude;
              theForm.tbCurrentLon.value=currentposition.Longitude;
              theForm.tbCurrentHeading.value=Math.min(currentheading, 359.999999);
              theForm.tbCurrentZoom.value=currentzoom;
              var map3D = map.vemapcontrol.Get3DControl();
              var lat, lon, alt, pitch, heading, zoom;
              zoom='not returned';
              var data = map3D.GetCameraViewpoint();
              eval(data);
              theForm.tbMCPitch.value = pitch;
              theForm.tbMCZoom.value = zoom;
              theForm.tbMCLat.value= lat;
              theForm.tbMCLon.value= lon;
              theForm.tbMCHeading.value=heading;
              theForm.tbMCAltitude.value=alt;
              }
          if (simulationmode==true) setcurrentmapview();
       }
      pitchtarget=1;
      headingtarget =-1;
      }

    function InitializeSimulation(){
        // Map must be in 3D or changing heading will cause a stack overflow for some unknown reason
        
//        if (map.GetMapMode()==VEMapMode.Mode2D) {alert ("To use the Caravan Simulator, you must be in 3D mode.");map.SetMapMode(VEMapMode.Mode3D);}//first try and set mode to 3D
        if (map.GetMapMode()==VEMapMode.Mode2D){  //if still in 2D, ask to load 3D version
            var alertbox = confirm("You must have the FREE 3D version of Microsoft Virtual Earth loaded to use this feature. If you click OK, you will be redirected" +
             "to a page where you can load the 3D version. " + 
             "\n\nYou will have to reload this web page after the 3D version is loaded." + 
             "\n\nIt is cool, so I hope you load it." +
             "\n\nSorry, but at this time it works only with Internet Explorer and Firefox and does not work under Mac OS.  Hopefully Microsoft will release a compatable version for the Mac by next year.");
            if (alertbox) location="http://maps.live.com/Help/VE3DInstall/Default.aspx?action=install&mkt=en-us";
            return;
        }  
        var shape=null;
        var points;
        var i=0;var j=0;var k=0;
        currentroute = new Array();
        routewaypoints = new Array();
        for (i=0;i<theForm.rbDeparture.length;i++){
            if(theForm.rbDeparture[i].checked==true){   
                for (j=0;j<GroundLayer.GetShapeCount();j++) {
                    shape=GroundLayer.GetShapeByIndex(j);
                    if (shape.GetTitle()==theForm.rbDeparture[i].value){
                        points=shape.GetPoints();
                        for (k=0;k<points.length-1;k++){ //skip the last point as it is the same as the arrival first point
                            currentroute.push(points[k]);
                        }
                    }    
                } 
                for (j=0;j<waypoints.length;j++) {
                    if (waypoints[j][0]==theForm.rbDeparture[i].value){
                        for (k=0;k<waypoints[j][1].length-1;k++){ //skip the last point as it is the same as the arrival first point
                            routewaypoints.push(waypoints[j][1][k]);
                        }
                    }
                }
            }
         }  
        for (i=0;i<theForm.rbArrival.length;i++){
            if(theForm.rbArrival[i].checked==true){   
                for (j=0;j<GroundLayer.GetShapeCount();j++) {
                    shape=GroundLayer.GetShapeByIndex(j);
                    if (shape.GetTitle()==theForm.rbArrival[i].value){
                        points=shape.GetPoints();
                        for (k=0;k<points.length;k++){
                            currentroute.push(points[k]);
                        }
                    }    
                } 
                for (j=0;j<waypoints.length;j++) {
                    if (waypoints[j][0]==theForm.rbArrival[i].value){
                        for (k=0;k<waypoints[j][1].length;k++){
                            routewaypoints.push(waypoints[j][1][k]);
                        }
                    }
                }
            }
         }  
        simulationmode=true;
        currentstep=0;
        SetNewHeading(currentroute,currentstep);
        pitchtarget=flypitchlevel;
        currentposition=currentroute[currentstep];
        currentzoom=flyzoomlevel;
        setcurrentmapview();
//        map.SetCenterAndZoom(currentroute[currentstep],flyzoomlevel);
    }
    
    function FlyToNextPoint(){
        if (currentroute.length>(currentstep+1)) {
            simulationmode=true;
            currentstep = currentstep+1;
            SetNewHeading(currentroute,currentstep);
            map.PanToLatLong(currentroute[currentstep]);
            currentposition=currentroute[currentstep];
    }   }
    
    function SuspendSimulation(){
        simulationmode=false;
        }
    
    function SetNewHeading(route,routestep){
        var target =-1;
        if (route.length>(routestep+1)) {
            target=ComputeHeading(route[routestep].Latitude, route[routestep].Longitude,route[(routestep + 1)].Latitude, route[(routestep + 1)].Longitude);
            headingtarget=target;
//            map.SetHeading(Math.min(theForm.tbHeading.value, 359.999999));
            theForm.tbCurrentWaypoint.value=routewaypoints[routestep];
            theForm.tbNextWaypoint.value=routewaypoints[routestep+1];
        }else {
            headingtarget= theForm.tbHeading.value;
//            map.SetHeading(Math.min(headingtarget, 359.999999));  //keep things aligned onblur the last leg
            theForm.tbCurrentWaypoint.value=routewaypoints[routestep];
            theForm.tbNextWaypoint.value='Airport';
            simulationmode=false;
        }
        currentheading=Math.min(theForm.tbHeading.value, 359.999999);
        setcurrentmapview();
        return (target);
    }
    
    function ComputeHeading(lat1, lon1, lat2, lon2){
         var lat1r = DegtoRad(lat1);
         var lat2r = DegtoRad(lat2);
         var dLon = DegtoRad(lon2-lon1);
         var dLat = lat2r-lat1r;
         if ((dLon == 0) && (dLat == 0)) return (-1);
         var y = Math.sin(dLon) * Math.cos(lat2r);
         var x = Math.cos(lat1r)*Math.sin(lat2r) - Math.sin(lat1r)*Math.cos(lat2r)*Math.cos(dLon);
         var brng = Math.round(10 * ((RadtoDeg(Math.atan2(y, x))+360)%360))/10;
         if (brng==0) brng=360;
         return (brng);
    }    
    function DegtoRad(x){return x*Math.PI/180;}
    function RadtoDeg(x){return x*180/Math.PI;}

    function ClearDeparture () {shapeMSN03.Hide();shapeMSN14.Hide();shapeMSN18.Hide();shapeMSN21.Hide();shapeMSN32.Hide();shapeMSN36.Hide();
                                shapeMSN03a.Hide();shapeMSN14a.Hide();shapeMSN18a.Hide();shapeMSN21a.Hide();shapeMSN32a.Hide();shapeMSN36a.Hide();}   
    function ClearArrival (){shapeOSH09.Hide();shapeOSH18.Hide();shapeOSH27.Hide();shapeOSH36L.Hide();shapeOSH36R.Hide();
                             shapeOSH09a.Hide();shapeOSH18a.Hide();shapeOSH27a.Hide();shapeOSH36La.Hide();shapeOSH36Ra.Hide();}   
    function ShowAltitudeChanged() {if(theForm.cbShowAltitude.checked==true) AirLayer.Show(); else AirLayer.Hide();}
    function ShowRoadsChanged() {if(theForm.cbShowRoads.checked==true) map.SetMapStyle(VEMapStyle.Hybrid); else map.SetMapStyle(VEMapStyle.Aerial);}
    function ShowArrivalOSH09(){    ClearArrival();     shapeOSH09.Show();  shapeOSH09a.Show();}
    function ShowArrivalOSH18(){    ClearArrival();     shapeOSH18.Show();  shapeOSH18a.Show();}
    function ShowArrivalOSH27(){    ClearArrival();     shapeOSH27.Show();  shapeOSH27a.Show();}
    function ShowArrivalOSH36L(){   ClearArrival();     shapeOSH36L.Show(); shapeOSH36La.Show();}
    function ShowArrivalOSH36R(){   ClearArrival();     shapeOSH36R.Show(); shapeOSH36Ra.Show();}
    function ShowDepartureMSN03(){  ClearDeparture();   shapeMSN03.Show();  shapeMSN03a.Show();}
    function ShowDepartureMSN14(){  ClearDeparture();   shapeMSN14.Show();  shapeMSN14a.Show();}
    function ShowDepartureMSN18(){  ClearDeparture();   shapeMSN18.Show();  shapeMSN18a.Show();}
    function ShowDepartureMSN21(){  ClearDeparture();   shapeMSN21.Show();  shapeMSN21a.Show();}
    function ShowDepartureMSN32(){  ClearDeparture();   shapeMSN32.Show();  shapeMSN32a.Show();}
    function ShowDepartureMSN36(){  ClearDeparture();   shapeMSN36.Show();  shapeMSN36a.Show();}

    function CreateShape(shapetype, shapepoints, title, description, linewidth, linecolor) {
        shape=new VEShape(shapetype, shapepoints);
        shape.SetTitle(title);
        shape.SetDescription(description);
        shape.SetLineWidth(linewidth);
        shape.SetLineColor(linecolor);
        return(shape);
    };                

    function AddDepartures() {
        shapeMSN03 = CreateShape(VEShapeType.Polyline, pMSN03,'MSN03', 'MSN 03 Departure', RouteWidth, RouteGroundColor);
        shapeMSN14 = CreateShape(VEShapeType.Polyline, pMSN14,'MSN14', 'MSN 14 Departure', RouteWidth, RouteGroundColor);
        shapeMSN18 = CreateShape(VEShapeType.Polyline, pMSN18,'MSN18', 'MSN 18 Departure', RouteWidth, RouteGroundColor);
        shapeMSN21 = CreateShape(VEShapeType.Polyline, pMSN21,'MSN21', 'MSN 21 Departure', RouteWidth, RouteGroundColor);
        shapeMSN32 = CreateShape(VEShapeType.Polyline, pMSN32,'MSN32', 'MSN 32 Departure', RouteWidth, RouteGroundColor);
        shapeMSN36 = CreateShape(VEShapeType.Polyline, pMSN36,'MSN36', 'MSN 36 Departure', RouteWidth, RouteGroundColor);
        shapeMSN03a = CreateShape(VEShapeType.Polyline, pMSN03a,'MSN03', 'MSN 03 Departure at Altitude', RouteWidth, RouteAirColor);
        shapeMSN14a = CreateShape(VEShapeType.Polyline, pMSN14a,'MSN14', 'MSN 14 Departure at Altitude', RouteWidth, RouteAirColor);
        shapeMSN18a = CreateShape(VEShapeType.Polyline, pMSN18a,'MSN18', 'MSN 18 Departure at Altitude', RouteWidth, RouteAirColor);
        shapeMSN21a = CreateShape(VEShapeType.Polyline, pMSN21a,'MSN21', 'MSN 21 Departure at Altitude', RouteWidth, RouteAirColor);
        shapeMSN32a = CreateShape(VEShapeType.Polyline, pMSN32a,'MSN32', 'MSN 32 Departure at Altitude', RouteWidth, RouteAirColor);
        shapeMSN36a = CreateShape(VEShapeType.Polyline, pMSN36a,'MSN36', 'MSN 36 Departure at Altitude', RouteWidth, RouteAirColor);

        GroundLayer.AddShape(shapeMSN03);AirLayer.AddShape(shapeMSN03a);
        GroundLayer.AddShape(shapeMSN14);AirLayer.AddShape(shapeMSN14a);
        GroundLayer.AddShape(shapeMSN18);AirLayer.AddShape(shapeMSN18a);
        GroundLayer.AddShape(shapeMSN21);AirLayer.AddShape(shapeMSN21a);
        GroundLayer.AddShape(shapeMSN32);AirLayer.AddShape(shapeMSN32a);
        GroundLayer.AddShape(shapeMSN36);AirLayer.AddShape(shapeMSN36a);
    }
    function AddArrivals(){
        shapeOSH09  = CreateShape(VEShapeType.Polyline, pOSH09, 'OSH09',  'OSH 09 Arrival',  RouteWidth, RouteGroundColor);
        shapeOSH18  = CreateShape(VEShapeType.Polyline, pOSH18, 'OSH18',  'OSH 18 Arrival',  RouteWidth, RouteGroundColor);
        shapeOSH27  = CreateShape(VEShapeType.Polyline, pOSH27, 'OSH27',  'OSH 27 Arrival',  RouteWidth, RouteGroundColor);
        shapeOSH36L = CreateShape(VEShapeType.Polyline, pOSH36L,'OSH36L', 'OSH 36L Arrival', RouteWidth, RouteGroundColor);
        shapeOSH36R = CreateShape(VEShapeType.Polyline, pOSH36R,'OSH36R', 'OSH 36R Arrival', RouteWidth, RouteGroundColor);
        shapeOSH09a  = CreateShape(VEShapeType.Polyline, pOSH09a, 'OSH09a',  'OSH 09 Arrival at Altitude',  RouteWidth, RouteAirColor);
        shapeOSH18a  = CreateShape(VEShapeType.Polyline, pOSH18a, 'OSH18a',  'OSH 18 Arrival at Altitude',  RouteWidth, RouteAirColor);
        shapeOSH27a  = CreateShape(VEShapeType.Polyline, pOSH27a, 'OSH27a',  'OSH 27 Arrival at Altitude',  RouteWidth, RouteAirColor);
        shapeOSH36La = CreateShape(VEShapeType.Polyline, pOSH36La,'OSH36La', 'OSH 36L Arrival at Altitude', RouteWidth, RouteAirColor);
        shapeOSH36Ra = CreateShape(VEShapeType.Polyline, pOSH36Ra,'OSH36Ra', 'OSH 36R Arrival at Altitude', RouteWidth, RouteAirColor);

        GroundLayer.AddShape(shapeOSH09);AirLayer.AddShape(shapeOSH09a);
        GroundLayer.AddShape(shapeOSH18);AirLayer.AddShape(shapeOSH18a);
        GroundLayer.AddShape(shapeOSH27); AirLayer.AddShape(shapeOSH27a);
        GroundLayer.AddShape(shapeOSH36L); AirLayer.AddShape(shapeOSH36La);
        GroundLayer.AddShape(shapeOSH36R);AirLayer.AddShape(shapeOSH36Ra);
   }
