 var contry = Array('Vietnam','Australia','New Zealand','Thailand','Singapore','Cambodia','Myanmar','Philippines','Japan','Taiwan','Macau','United States');
 function build_airports()
    {
            var apts    = new Array();
            var dests;
                dests = new Array('BKK','BNE','CNS','DRW','DPS','OOL','SGN','HNL','MLB','AVV','MEL','KIX','PER','HKT','SIN','MCY','SYD');
                apts[0] = new airport("ADL", "Adelaide", dests, "Australia" );
                dests = new Array('DPS','OOL','SYD');
                apts[1] = new airport("AKL", "Auckland", dests, "New Zealand" );
                dests = new Array('MEL','SYD');
                apts[2] = new airport("BNK", "Ballina Byron", dests, "Australia" );
                dests = new Array('ADL','BNE','CHC','OOL','SGN','HBA','CGK','LST','MLB','AVV','MEL','PER','SIN','SYD');
                apts[3] = new airport("BKK", "Bangkok", dests, "Thailand" );
                dests = new Array('ADL','BKK','CNS','CHC','DRW','DPS','HTI','HIS','SGN','HBA','HNL','LST','MKY','MLB','AVV','NTL','KIX','HKT','ROK','SIN','SYD','TSV','PPP');
                apts[4] = new airport("BNE", "Brisbane", dests, "Australia" );
                dests = new Array('ADL','BNE','CHC','DRW','DPS','OOL','MLB','MEL','KIX','PER','SIN','SYD','NRT');
                apts[5] = new airport("CNS", "Cairns", dests, "Australia" );
                dests = new Array('BKK','BNE','CNS','DPS','OOL','SGN','HBA','HNL','MLB','MEL','KIX','HKT','SYD');
                apts[6] = new airport("CHC", "Christchurch", dests, "New Zealand" );
                dests = new Array('HAN','HPH','SGN');
                apts[7] = new airport("DAD", "Danang", dests, "Vietnam" );
                dests = new Array('ADL','BNE','CNS','DPS','SGN','MLB','MEL','SIN','SYD');
                apts[8] = new airport("DRW", "Darwin", dests, "Australia" );
                dests = new Array('ADL','AKL','BNE','CNS','CHC','DRW','OOL','HBA','LST','MLB','AVV','MEL','PER','SIN','SYD');
                apts[9] = new airport("DPS", "Denpasar (Bali)", dests, "Indonesia" );
                dests = new Array('SYD');
                apts[10] = new airport("HVB", "Fraser Coast", dests, "Australia" );
                dests = new Array('ADL','AKL','BKK','CNS','CHC','DPS','SGN','HBA','MLB','MEL','NTL','KIX','HKT','SYD','NRT');
                apts[11] = new airport("OOL", "Gold Coast", dests, "Australia" );
                dests = new Array('DAD','SGN','CXR');
                apts[12] = new airport("HAN", "Ha Noi", dests, "Vietnam" );
                dests = new Array('SGN');
                apts[13] = new airport("HPH", "Hai Phong", dests, "Vietnam" );
                dests = new Array('BNE','MLB','MEL','SYD');
                apts[14] = new airport("HTI", "Hamilton Island", dests, "Australia" );
                dests = new Array('BNE','MLB','MEL','SYD');
                apts[15] = new airport("HIS", "Hayman Island", dests, "Australia" );
                dests = new Array('ADL','BKK','BNE','DAD','DRW','OOL','HAN','HPH','HBA','HUI','MLB','AVV','MEL','PER','REP','SIN','SYD','VII');
                apts[16] = new airport("SGN", "Ho Chi Minh City", dests, "Vietnam" );
                dests = new Array('BKK','BNE','CNS','CHC','DPS','OOL','SGN','MLB','MEL','KIX','HKT','SYD');
                apts[17] = new airport("HBA", "Hobart", dests, "Australia" );
                dests = new Array('CGK','PER','SIN');
                apts[18] = new airport("HKG", "Hong Kong", dests, "Hong Kong" );
                dests = new Array('ADL','BNE','MEL','PER','SYD');
                apts[19] = new airport("HNL", "Honolulu", dests, "United States" );
                dests = new Array('SGN');
                apts[20] = new airport("HUI", "Hue", dests, "Vietnam" );
                dests = new Array('BKK','HKG','PER','SIN');
                apts[21] = new airport("CGK", "Jakarta", dests, "Indonesia" );
                dests = new Array('PER','SIN');
                apts[22] = new airport("BKI", "Kota-Kinabalu", dests, "Malaysia" );
                dests = new Array('PER','SIN');
                apts[23] = new airport("KUL", "Kuala Lumpur", dests, "Malaysia" );
                dests = new Array('SIN');
                apts[24] = new airport("KCH", "Kuching", dests, "Malaysia" );
                dests = new Array('BKK','BNE','CNS','DPS','OOL','MLB','MEL','SYD');
                apts[25] = new airport("LST", "Launceston", dests, "Australia" );
                dests = new Array('PER','SIN');
                apts[26] = new airport("MFM", "Macau", dests, "Macau" );
                dests = new Array('BNE');
                apts[27] = new airport("MKY", "Mackay", dests, "Australia" );
                dests = new Array('CGK','PER','HKT','SIN');
                apts[28] = new airport("MNL", "Manila", dests, "Philippines" );
                dests = new Array('SIN');
                apts[29] = new airport("MES", "Medan", dests, "Indonesia" );
                dests = new Array('ADL','BNK','BKK','BNE','CNS','CHC','DRW','DPS','OOL','HTI','HIS','SGN','HBA','HNL','LST','NTL','KIX','PER','HKT','SIN','MCY','SYD','NRT','TSV');
                apts[30] = new airport("MLB", "Melbourne (all airports)", dests, "Australia" );
                dests = new Array('ADL','BKK','BNE','DPS','SGN','MKY','KIX','PER','HKT','SYD');
                apts[31] = new airport("AVV", "Melbourne - Avalon", dests, "Australia" );
                dests = new Array('ADL','BNK','BKK','CNS','CHC','DRW','DPS','OOL','HTI','HIS','SGN','HBA','HNL','LST','NTL','KIX','PER','HKT','SIN','MCY','SYD','NRT','TSV');
                apts[32] = new airport("MEL", "Melbourne - Tullamarine", dests, "Australia" );
                dests = new Array('BNE','OOL','MKY','MLB','MEL');
                apts[33] = new airport("NTL", "Newcastle", dests, "Australia" );
                dests = new Array('HAN');
                apts[34] = new airport("CXR", "Nha Trang", dests, "Vietnam" );
                dests = new Array('ADL','BNE','CNS','CHC','OOL','HBA','MLB','AVV','MEL','PER','SYD');
                apts[35] = new airport("KIX", "Osaka (Kansai International)", dests, "Japan" );
                dests = new Array('PER','SIN');
                apts[36] = new airport("PEN", "Penang", dests, "Malaysia" );
                dests = new Array('ADL','BKK','CNS','DPS','SGN','HKG','HNL','CGK','BKI','KUL','MFM','MLB','AVV','MEL','KIX','PEN','HKT','SIN','SIN','TPE','RGN');
                apts[37] = new airport("PER", "Perth", dests, "Australia" );
                dests = new Array('PER','SIN');
                apts[38] = new airport("PNH", "Phnom Penh", dests, "Cambodia" );
                dests = new Array('ADL','BNE','CHC','OOL','HBA','AVV','MEL','PER','SYD');
                apts[39] = new airport("HKT", "Phuket", dests, "Thailand" );
                dests = new Array('BNE');
                apts[40] = new airport("ROK", "Rockhampton", dests, "Australia" );
                dests = new Array('SGN','SIN','TPE');
                apts[41] = new airport("REP", "Siem Reap (Angkor Wat)", dests, "Cambodia" );
                dests = new Array('ADL','BKK','BNE','CNS','DRW','DPS','SGN','HKG','CGK','BKI','KUL','KCH','MFM','MNL','MES','MLB','MEL','PEN','PER','PNH','REP','SUB','SYD','TPE','RGN');
                apts[42] = new airport("SIN", "Singapore", dests, "Singapore" );
                dests = new Array('ADL','MLB','MEL','SYD');
                apts[43] = new airport("MCY", "Sunshine Coast", dests, "Australia" );
                dests = new Array('SIN');
                apts[44] = new airport("SUB", "Surabaya", dests, "Indonesia" );
                dests = new Array('ADL','AKL','BNK','BKK','BNE','CNS','CHC','DRW','DPS','HVB','OOL','HTI','HIS','SGN','HBA','HNL','LST','MLB','AVV','MEL','KIX','HKT','SIN','MCY','NRT','TSV','PPP');
                apts[45] = new airport("SYD", "Sydney", dests, "Australia" );
                dests = new Array('PER','SIN');
                apts[46] = new airport("TPE", "Taipei", dests, "Taiwan" );
                dests = new Array('CNS','OOL','MLB','MEL','SYD');
                apts[47] = new airport("NRT", "Tokyo (Narita International)", dests, "Japan" );
                dests = new Array('BNE','MLB','MEL','SYD');
                apts[48] = new airport("TSV", "Townsville", dests, "Australia" );
                dests = new Array('SGN');
                apts[49] = new airport("VII", "Vinh", dests, "Vietnam" );
                dests = new Array('BNE','SYD');
                apts[50] = new airport("PPP", "Whitsunday Coast", dests, "Australia" );
                dests = new Array('PER','SIN');
                apts[51] = new airport("RGN", "Yangon", dests, "Myanmar" );

         
       return  apts;  
       }
            
	function airport(c, n, d, ca)
	{
		this.code	= c;
		this.name	= n;
		this.dests	= d;
		this.country	= ca;
	}
	
	function getAirport( code )
	{
		var air;
		var Airports = build_airports()
		for (var i=0; i < Airports.length; i++)
		{
			if ( Airports[i].code == code )
			{
				air		= Airports[i];
				break;
			}
		}
		return air;
	}


function write_dest()
{
				var fromList    = document.getElementById("FromSeleted");
        var toList      = document.getElementById( "Toselected" );
        var orig        = fromList.options[ fromList.selectedIndex ].value;
 		    while( toList.childNodes.length )
				{
					toList.removeChild( toList.childNodes[0] );
				}
        toList.options[0]       = new Option("Chọn sân bay");
        toList.options[0].value = "???"
        var Airports = build_airports();
        if (orig=="???") alert('Chọn sân bay');
        else{
        var air     = getAirport( orig );
       for (l = 0; l < contry.length ;l++)
        {
        	var opt_group = document.createElement( 'optgroup' );
					opt_group.label = contry[l];
					var check = false;
             for (var i = 0; i < air.dests.length; i++)
            {
            	  
                dest_air        = getAirport( air.dests[i] );
                if ( dest_air.country !=contry[l])
                {
                	continue;
                	
                }
                check =true;
                var opt_str = dest_air.name;
      					var dest_option = document.createElement( 'option' );
      					dest_option.innerHTML = opt_str;
      					dest_option.value = dest_air.code;
      					opt_group.appendChild( dest_option );
           	}
           	if (check==true) 
           	toList.appendChild( opt_group );       
          }
        }
}




function load_config()
{
		var fromList    = document.getElementById("FromSeleted");
        fromList.length=1;
        fromList.options[0]       = new Option("Chọn sân bay");
        fromList.options[0].value = "???"
        var Airports = build_airports();
        for (l = 0; l < contry.length ;l++)
        {
        	var opt_group = document.createElement( 'optgroup' );
					opt_group.label = contry[l];
            for (var i = 0; i < Airports.length; i++)
            {
            if ( Airports[i].country != contry[l])
                {
                	continue;
                }
                var opt_str = Airports[i].name;
      					var dest_option = document.createElement( 'option' );
      					dest_option.innerHTML = opt_str;
      					dest_option.value = Airports[i].code;
      					opt_group.appendChild( dest_option );   
             
            }
           fromList.appendChild( opt_group ); 
          }  
    }