﻿// JScript File

    var lat = 0;
    var lon = 0;
	
	function SetAirport(obin){


	    //alert(obin.substring(29, 30))
	    
	    
	    test = obin.indexOf("<br>",34)

	    if (obin.substring(29, 30) == "<") { // us iata
	        document.getElementById("AirPortName").value = obin.substring(33, test)
	        document.getElementById("chooseIata").value = obin.substring(26, 29)

	    }
	    else { //icbo
	        document.getElementById("AirPortName").value = obin.substring(34, test)
	        document.getElementById("chooseIata").value = obin.substring(26, 30)
	    }
	    
	
	}
	
	// Create our jet marker icon
	try{
        
        
        var jeticon = new GIcon();
        jeticon.image = "http://globalcharterjets.com/images/jeticon.png";
        jeticon.shadow = "http://globalcharterjets.com/images/shadow.png";
        jeticon.iconSize = new GSize(20, 20);
        jeticon.shadowSize = new GSize(22, 20);
        jeticon.iconAnchor = new GPoint(6, 20);
        jeticon.infoWindowAnchor = new GPoint(5, 1);

	    // Create our city marker icon
        var cityicon = new GIcon();
        cityicon.image = "http://globalcharterjets.com/images/city.png";
        cityicon.shadow = "http://globalcharterjets.com/images/shadow.png";
        cityicon.iconSize = new GSize(20, 20);
        cityicon.shadowSize = new GSize(22, 20);
        cityicon.iconAnchor = new GPoint(6, 20);
        cityicon.infoWindowAnchor = new GPoint(5, 1);
    }
	catch(err)
    {
        //Handle errors here
        var jeticon = null;
    }
    
    
	function createMarker(point, info) {  
	
		var marker = new GMarker(point, jeticon);  
		GEvent.addListener(marker, "click", 
		    function() 
		    {
		        marker.openInfoWindowHtml(info); 
		        SetAirport(info)
    		    	
		    }
		)
		return marker;
		
	};	
		
	function createCityMarker(point, info) {  
	
		var marker = new GMarker(point, cityicon);  
		GEvent.addListener(marker, "click", 
		    function() 
		    {
		        marker.openInfoWindowHtml(info); 
		        
    		    	
		    }
		)	
	
		return marker;
		
	};
		


//function MyApplication()
// {  
//     this.counter = 0;  
//     this.map = new GMap2(document.getElementById("map"));  
//     this.map.setCenter(new GLatLng(37.4419, -122.1419), 13);  
//     GEvent.bind(this.map, "click", this, this.onMapClick);
// }
// 
// MyApplication.prototype.onMapClick = function()
// {  
//     this.counter++;  
//     alert("You have clicked the map " + this.counter + " " +        
//        (this.counter == 1 ?"time" : "times"));
// }
//    
// var application = new MyApplication();


//		GEvent.addDomListener(marker, "click", 
//		function() 
//		    {
//    		    
//		        //document.getElementById("chooseIata").value = info
//		        alert(info)
//		        if (document.getElementById("chooseIata")!=null){
//		            document.getElementById("chooseIata").value = info
//    		    
//		        } 
//    		    	
//		    }
//		) 






	//for small map 	
	function GetMapMagSm(dist){
    
		dint = 0
		
		if (dist>4000){
			dint = 1
		}
		
		if (dist<4000){
			dint = 2
		}
		
		
		if (dist<3000){
			dint = 3
		}
		
		
		if (dist<1500){
			dint = 4
		}
		if (dist<1000){
			dint = 5
		}
		if (dist<500){
			dint = 6
		}
		if (dist<300){
			dint = 7
		}
    
		return dint;
    
    }
    
    //for big map
    function GetMapMag(dist){
    
		dint = 0
		
		if (dist>4000){
			dint = 2
		}
		
		if (dist<4000){
			dint = 3
		}
		
		
		if (dist<3000){
			dint = 4
		}
		
		
		if (dist<1500){
			dint = 5
		}
		if (dist<1000){
			dint = 6
		}
		if (dist<500){
			dint = 7
		}
		if (dist<300){
			dint = 8
		}
    
		return dint;
    
    }
    
	// generate map of route
	function GenRoute() 
	{

	    // clear trip type display
	    if (document.getElementById("tripType") != null) {
	        document.getElementById("tripType").innerHTML = "";
	    }
      //cycle ++
      
      //alert(cityArray.length)
      //alert(retOrMulti)
    
      // theoretical centre of map
        lat = 0
        lon = 0
        latmax = 0
        latmin = 1000
        lonmax = 0
        lonmax = 1000
      
      
      // it is a simple one way or return
      if (cityArray.length==0){

          if (document.getElementById("depLatLon") == null) {
              return;

          }
      	    
      	    
      	    lat1 = GetLatLon(document.getElementById("depLatLon"),0)
            lon1 = GetLatLon(document.getElementById("depLatLon"),1)
            lat2 = GetLatLon(document.getElementById("arrLatLon"),0)
            lon2 = GetLatLon(document.getElementById("arrLatLon"),1)

            lat = (lat1+lat2)/2
            lon = (lon1+lon2)/2
            
            // need to check this ... it is not working for one way.
            //SetCenter()
            
            tripDistance = llDistance(lat1, lon1, lat2, lon2, 'M') 
          
      
          //document.getElementById("pos").value = cycle
          
          if (typeof(GBrowserIsCompatible)!="undefined"){
          
              if (GBrowserIsCompatible()) {
                var map = new GMap2(document.getElementById("routemap"));
                
                //alert(lat + " : "  + lon + " : " + tripDistance)

                map.setCenter(new GLatLng(lat, lon), GetMapMag(tripDistance));

                map.addControl(new GSmallMapControl());

                document.getElementById("tripLength").innerHTML = tripDistance

                if (retOrMulti == "return") {
                    document.getElementById("tripType").innerHTML = tripDistance = " each way";
                }
                
             
                var polyline = new GPolyline([
                    new GLatLng(lat1, lon1),       
                    new GLatLng( lat2, lon2)]
                    
                    , "#ff0033", 2);
                    map.addOverlay(polyline);
              }
          }
      }
      else{  // muti leg
      
     
        maxDist = 0
        tripDistance = 0
        // calculate the trip distance
         for (var i = 1; i < cityArray.length; i++) 
        {  
            lat1 = GetLatLonArray(cityArray[i-1],0)
            lon1 = GetLatLonArray(cityArray[i-1],1)
            lat2 = GetLatLonArray(cityArray[i],0)
            lon2 = GetLatLonArray(cityArray[i],1)
     
            thisTrip = llDistance(lat1, lon1, lat2, lon2, 'M') 
            if (maxDist<thisTrip){
                maxDist=thisTrip
            }
         
            tripDistance += thisTrip
            
           //SetCenter()

       }

       SetCenter()
        
        
        document.getElementById("tripLength").innerHTML = tripDistance
        
        // plot the points on the map
        var points = [];
        for (var i = 0; i < cityArray.length; i++) 
        {  
            lat1 = GetLatLonArray(cityArray[i],0)
            lon1 = GetLatLonArray(cityArray[i],1)
   
            points.push(new GLatLng(lat1, lon1));
        }
        
        
//        lat = (latmax + latmin)/2
//        lon = (lonmax + lonmin)/2
        
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("routemap"));
            
            //alert(lat + " : "  + lon + " : " + tripDistance)

            map.setCenter(new GLatLng(lat, lon), GetMapMag(maxDist));

            map.addControl(new GSmallMapControl());

            var polyline = new GPolyline(points, "#ff0033", 2)
            map.addOverlay(polyline);
            

          }

      }
     
      if (document.getElementById("ctl00_ContentPlaceHolder1_showQuote")!=null){
      
        if (document.getElementById("ctl00_ContentPlaceHolder1_showQuote").value == 1) {
          ToggleTrips(document.getElementById("showtrips"));
        }
      
      
      }
      
      
      //window.scrollTo(900, 500)
      //document.getElementById("pos").value = cycle
    }
    
    
    function SetCenter(){

        latsum = 0;
        lonsum = 0;
        

        for (var i = 0; i < cityArray.length; i++) {
            
            curlat = GetLatLonArray(cityArray[i], 0)
            curlon = GetLatLonArray(cityArray[i], 1)

            latsum += curlat;
            lonsum += curlon;

        }

        lat = latsum / cityArray.length
        lon = lonsum / cityArray.length    

//        if (region == "US") {

//            // set the center
//            if (lat1 > 0) {
//                if (lat1 > latmax) {
//                    latmax = lat1
//                }
//            }
//            else {
//                if (lat1 < latmax) {
//                    latmax = lat1
//                }
//            }
//            if (lat1 > 0) {
//                if (lat1 < latmin) {
//                    latmin = lat1
//                }
//            }
//            else {
//                if (lat1 > latmin) {
//                    latmin = lat1
//                }
//            }
//            if (lon1 > 0) {
//                if (lon1 > lonmax) {
//                    lonmax = lon1
//                }
//            }
//            else {
//                if (lon1 < lonmax) {
//                    lonmax = lon1
//                }
//            }
//            if (lat1 > 0) {
//                if (lon1 < lonmin) {
//                    lonmin = lon1
//                }
//            }
//            else {
//                if (lon1 > lonmin) {
//                    lonmin = lon1
//                }
//            }

//        }
//        else { //not negative in lon

//            // set the center
//            if (lat1 > 0) {
//                if (lat1 > latmax) {
//                    latmax = lat1
//                }
//            }
//            else {
//                if (lat1 < latmax) {
//                    latmax = lat1
//                }
//            }
//            if (lat1 > 0) {
//                if (lat1 < latmin) {
//                    latmin = lat1
//                }
//            }
//            else {
//                if (lat1 > latmin) {
//                    latmin = lat1
//                }
//            }
//            if (lon1 > 0) {
//                if (lon1 > lonmax) {
//                    lonmax = lon1
//                }
//            }
//            else {
//                if (lon1 < lonmax) {
//                    lonmax = lon1
//                }
//            }
//            if (lat1 > 0) {
//                if (lon1 < lonmin) {
//                    lonmin = lon1
//                }
//            }
//            else {
//                if (lon1 > lonmin) {
//                    lonmin = lon1
//                }
//            }



//        }   
      
    }
   
    function GetLatLonArray(strin,swch){
	
		llAr = strin.split("|")
		return parseFloat(llAr[swch]) 
	
	}
   
    // get nearby airports
	function GetAirports(){
	
	
	    
	    qString = "&citycor=" + AirportLatLon
	    callService(ShowAirports,"GetNearbyAirports",qString)
	
	
	
//		if (cityArray.length==0){
//		
//		    
//		    if (depAirCode.length==0){
//		        qString = "&citycor=" + document.getElementById("depLatLon").value
//		        callService(ShowAirports,"GetNearbyAirports",qString)
//		    }
//		    else{
//		        qString = "&citycor=" + document.getElementById("arrLatLon").value
//		        callService(ShowAirports,"GetNearbyAirports",qString)
//		    }
//		}
//		else{
//		
//		    qString = "&citycor=" + cityArray[cityArray.length-1]
//		    callService(ShowAirports,"GetNearbyAirports",qString)
//		
//		
//		}
	
	}
    
  
	// show departure airports
	function ShowAirports(result){

		airportAr = result.split("$")
		
		
		if (GBrowserIsCompatible()) {
		
		    //AirportMap = document.getElementById("airports")
		
			var airports = new GMap2(AirportMap);
			
			
			
			
			lat1 = GetLatLonArray(AirportLatLon,0)
            lon1 = GetLatLonArray(AirportLatLon,1)
			
			
			
//			if (cityArray.length > 0){
//			    lat1 = GetLatLonArray(cityArray[0],0)
//                lon1 = GetLatLonArray(cityArray[0],1)
//			}
//			else{
//			    if (depAirCode.length==0){
//			        lat1 = GetLatLonArray(document.getElementById("depLatLon").value,0)
//                    lon1 = GetLatLonArray(document.getElementById("depLatLon").value,1)
//                }
//                else{
//                    lat1 = GetLatLonArray(document.getElementById("arrLatLon").value,0)
//                    lon1 = GetLatLonArray(document.getElementById("arrLatLon").value,1)
//                }
//			
//			}
			
			airports.setCenter(new GLatLng(lat1, lon1), 9);
			
			
			// put in the city
			var point = new GLatLng(lat1, lon1)
			info= "<span style='color:Black'>" +  AirportCity + "<br></span>"
			airports.addOverlay(createCityMarker(point, info));
			
			
			//airports.addControl(new GLargeMapControl());
			// 0		1			2			3			4			5
			//ap_IATA - ap_name - ap_city - ap_latdec - ap_londec - ProxDistance - 
			
			for(y=0;y<airportAr.length-1;y++)
			{
				thisApAr = airportAr[y].split("|") 
				var point = new GLatLng(thisApAr[3], thisApAr[4])
				info= "<span style='color:Black'>" +  thisApAr[0] + "<br>" +  thisApAr[1] + "<br>" + thisApAr[2] + "<br>" + Math.round(thisApAr[5]) + " miles from " + AirportCity + "<br></span>"
				
				//var marker = new GMarker(point)");
				airports.addOverlay(createMarker(point, info));

			}
		}
	}
	

	