function getCookieValue(name){
	var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
$(document).ready(function(){
	options_countries = '';
	options_cities = '';
	options_languages = '';
	
	var sel_country = getCookieValue('country');
	var sel_airport = getCookieValue ('homeAirport');
	var sel_language = getCookieValue('language');
	
	$.each(data, function(i,d){
		if(d.ccode == sel_country){
			options_countries += '<option value="' + d.ccode + '" title="' + d.country[sel_language] + '" SELECTED>' + d.country[sel_language] + '<\/option>';
		}else{
			options_countries += '<option value="' + d.ccode + '" title="' + d.country[sel_language] + '">' + d.country[sel_language] + '<\/option>';
		}
	});
	$('#frm-details-country').html(options_countries);
	$("#frm-details-country").change(function(){
		var index = $(this).get(0).selectedIndex;
		//var d = data[index-1];  // -1 because index 0 is for empty 'Select' option
		var d = data[index];
		var options = '';
		
		$.each(d.airports, function(i,j){
			var sText = j.title[sel_language];
			if (sText == null)
				sText = j.title["en"];

			options += '<option value="' + j.code + '" title="' + sText + '">' + sText + '<\/option>';
			});
		$("#frm-details-city").html(options);
	})
	
	$("#frm-details-country").change(function(){
		var Newindex = $(this).get(0).selectedIndex;
		//var d = data[Newindex-1];  // -1 because index 0 is for empty 'Select' option
		var d = data[Newindex];
		var options = '';
			$.each(d.language, function(i,k){
	                    options += '<option value="' + k.code + '" title="' + allLanguages[k.code] + '">' + allLanguages[k.code] + '<\/option>';
			});
		$("#frm-details-language").html(options);
	})

	var index = $("#frm-details-country").get(0).selectedIndex;
	var dfo = data[index];
	$.each(dfo.airports, function(i,j){
		var sText = j.title[sel_language];
		if (sText == null)
			sText = j.title["en"];

		if (j.code == sel_airport)
            options_cities += '<option value="' + j.code + '" title="' + sText + '" SELECTED>' + sText + '<\/option>';
		else
			options_cities += '<option value="' + j.code + '" title="' + sText + '">' + sText + '<\/option>';
		});
	$("#frm-details-city").html(options_cities);

	$.each(dfo.language, function(i,k){
		if (k.code == sel_language)
            options_languages += '<option value="' + k.code + '" title="' + allLanguages[k.code] + '" SELECTED>' + allLanguages[k.code] + '<\/option>';
		else
            options_languages += '<option value="' + k.code + '" title="' + allLanguages[k.code] + '">' + allLanguages[k.code] + '<\/option>';
		});
	$("#frm-details-language").html(options_languages);
});
