$( document ).ready(function() {
	$('#input_usa_state').change(function(){
		var state = $("#input_usa_state option:selected").text();
		if (state != "") {
			$("#stationCntyInput").prop('disabled', false);
			$('#stationCntyInput').selectize({
				maxItems: 1,
				closeAfterSelect: true,
				loadThrottle: 250,
				valueField: 'name',
				labelField: 'name',
				searchField: 'name',
				options: [],
				create: false,
				load: function(query, callback) {
					var state = $("#input_usa_state option:selected").text();
					if (!query || state == "") return callback();
					$.ajax({
						url: base_url+'index.php/qso/get_county',
						type: 'GET',
						dataType: 'json',
						data: {
							query: query,
							state: state,
						},
						error: function() {
							callback();
						},
						success: function(res) {
							callback(res);
						}
					});
				}
			});
		} else {
			$("#stationCntyInput").prop('disabled', true);
			//$('#stationCntyInput')[0].selectize.destroy();
			$("#stationCntyInput").val("");
		}
	});
	$('#sota_ref').selectize({
		maxItems: 1,
		closeAfterSelect: true,
		loadThrottle: 250,
		valueField: 'name',
		labelField: 'name',
		searchField: 'name',
		options: [],
		create: false,
		load: function(query, callback) {
			if (!query || query.length < 3) return callback();  // Only trigger if 3 or more characters are entered
			$.ajax({
				url: base_url+'index.php/qso/get_sota',
				type: 'GET',
				dataType: 'json',
				data: {
					query: query,
				},
				error: function() {
					callback();
				},
				success: function(res) {
					callback(res);
				}
			});
		}
	});
	$('#darc_dok').selectize({
		maxItems: 1,
		closeAfterSelect: true,
		loadThrottle: 250,
		valueField: 'name',
		labelField: 'name',
		searchField: 'name',
		options: [],
		create: false,
		load: function(query, callback) {
			if (!query) return callback();  // Only trigger if at least 1 character is entered
			$.ajax({
				url: base_url+'index.php/qso/get_dok',
				type: 'GET',
				dataType: 'json',
				data: {
					query: query,
				},
				error: function() {
					callback();
				},
				success: function(res) {
					callback(res);
				}
			});
		}
	});
	/*
	  Populate the Satellite Names Field on the QSO Panel
	*/
	$.getJSON(base_url+"assets/json/satellite_data.json", function( data ) {
		// Build the options array
		var items = [];
		$.each( data, function( key, val ) {
			items.push(
				''
			);
		});
		// Add to the datalist
		$('.satellite_names_list').append(items.join( "" ));
	});
});
var selected_sat;
var selected_sat_mode;
$(document).on('change', 'input', function(){
	var optionslist = $('.satellite_modes_list')[0].options;
	var value = $(this).val();
	for (var x=0;x' + key1 + '');
						});
					}
				});
				// Add to the datalist
				$('.satellite_modes_list').append(sat_modes.join( "" ));
			});
		}
	}
});
function changebadge(entityname) {
	if($("#sat_name" ).val() != "") {
		$.getJSON('logbook/jsonlookupdxcc/' + convert_case(entityname) + '/SAT/0/0', function(result)
		{
			$('#callsign_info').removeClass("badge-secondary");
			$('#callsign_info').removeClass("badge-success");
			$('#callsign_info').removeClass("badge-danger");
			$('#callsign_info').attr('title', '');
			if (result.workedBefore)
			{
				$('#callsign_info').addClass("badge-success");
				$('#callsign_info').attr('title', 'DXCC was already worked in the past on this band and mode!');
			}
			else
			{
				$('#callsign_info').addClass("badge-danger");
				$('#callsign_info').attr('title', 'New DXCC, not worked on this band and mode!');
			}
		})
	} else {
		$.getJSON('logbook/jsonlookupdxcc/' + convert_case(entityname) + '/0/' + $("#band").val() +'/' + $("#mode").val(), function(result)
		{
			// Reset CSS values before updating
			$('#callsign_info').removeClass("badge-secondary");
			$('#callsign_info').removeClass("badge-success");
			$('#callsign_info').removeClass("badge-danger");
			$('#callsign_info').attr('title', '');
			if (result.workedBefore)
			{
				$('#callsign_info').addClass("badge-success");
				$('#callsign_info').attr('title', 'DXCC was already worked in the past on this band and mode!');
			}
			else
			{
				$('#callsign_info').addClass("badge-danger");
				$('#callsign_info').attr('title', 'New DXCC, not worked on this band and mode!');
			}
		})
	}
}
/* Function: reset_fields is used to reset the fields on the QSO page */
function reset_fields() {
	$('#locator_info').text("");
	$('#country').val("");
	$('#lotw_info').text("");
	$('#dxcc_id').val("");
	$('#cqz').val("");
	$('#name').val("");
	$('#qth').val("");
	$('#locator').val("");
	$('#iota_ref').val("");
	$("#locator").removeClass("workedGrid");
	$("#locator").removeClass("newGrid");
	$("#callsign").removeClass("workedGrid");
	$("#callsign").removeClass("newGrid");
	$('#callsign_info').removeClass("badge-secondary");
	$('#callsign_info').removeClass("badge-success");
	$('#callsign_info').removeClass("badge-danger");
	$('#qsl_via').val("");
	$('#callsign_info').text("");
	$('#input_usa_state').val("");
	$('#qso-last-table').show();
	$('#partial_view').hide();
	mymap.setView([51.505, -0.09], 13);
	mymap.removeLayer(markers);
	$('.callsign-suggest').hide();
}
$("#callsign").focusout(function() {
	if ($(this).val().length >= 3) {
		/* Find and populate DXCC */
		$('.callsign-suggest').hide();
		if($("#sat_name").val() != ""){
			var sat_type = "SAT";
			var json_band = "0";
			var json_mode = "0";
		} else {
			var sat_type = "0";
			var json_band = $("#band").val();
			var json_mode = $("#mode").val();
		}
		var find_callsign = $(this).val().toUpperCase();
		find_callsign.replace(/\//g, "-");
		// Replace / in a callsign with - to stop urls breaking
		$.getJSON('logbook/json/' + find_callsign.replace(/\//g, "-") + '/' + sat_type + '/' + json_band + '/' + json_mode, function(result)
		{
			//$('#country').val(result); lotw_info
			if(result.dxcc.entity != undefined) {
				$('#country').val(convert_case(result.dxcc.entity));
				$('#callsign_info').text(convert_case(result.dxcc.entity));
				if($("#sat_name" ).val() != "") {
					//logbook/jsonlookupgrid/io77/SAT/0/0
					$.getJSON('logbook/jsonlookupcallsign/' + find_callsign.replace(/\//g, "-") + '/SAT/0/0', function(result)
					{
						// Reset CSS values before updating
						$('#callsign').removeClass("workedGrid");
						$('#callsign').removeClass("newGrid");
						$('#callsign').attr('title', '');
						if (result.workedBefore)
						{
							$('#callsign').addClass("workedGrid");
							$('#callsign').attr('title', 'Callsign was already worked in the past on this band and mode!');
						}
						else
						{
							$('#callsign').addClass("newGrid");
							$('#callsign').attr('title', 'New Callsign!');
						}
					})
				} else {
					$.getJSON('logbook/jsonlookupcallsign/' + find_callsign.replace(/\//g, "-") + '/0/' + $("#band").val() +'/' + $("#mode").val(), function(result)
					{
						// Reset CSS values before updating
						$('#callsign').removeClass("workedGrid");
						$('#callsign').removeClass("newGrid");
						$('#callsign').attr('title', '');
						if (result.workedBefore)
						{
							$('#callsign').addClass("workedGrid");
							$('#callsign').attr('title', 'Callsign was already worked in the past on this band and mode!');
						}
						else
						{
							$('#callsign').addClass("newGrid");
							$('#callsign').attr('title', 'New Callsign!');
						}
					})
				}
				changebadge(result.dxcc.entity);
			}
			if(result.lotw_member == "active") {
				$('#lotw_info').text("LoTW");
			}
			$('#dxcc_id').val(result.dxcc.adif);
			$('#cqz').val(result.dxcc.cqz);
			$('#ituz').val(result.dxcc.ituz);
			var redIcon = L.icon({
				iconUrl: icon_dot_url,
				iconSize:     [18, 18], // size of the icon
			});
			// Set Map to Lat/Long
			markers.clearLayers();
			mymap.setZoom(8);
			if (typeof result.latlng !== "undefined" && result.latlng !== false) {
				var marker = L.marker([result.latlng[0], result.latlng[1]], {icon: redIcon});
				mymap.panTo([result.latlng[0], result.latlng[1]]);
			} else {
				var marker = L.marker([result.dxcc.lat, result.dxcc.long], {icon: redIcon});
				mymap.panTo([result.dxcc.lat, result.dxcc.long]);
			}
			markers.addLayer(marker).addTo(mymap);
			/* Find Locator if the field is empty */
			if($('#locator').val() == "") {
				$('#locator').val(result.callsign_qra);
				$('#locator_info').html(result.bearing);
				if (result.callsign_qra != "")
				{
					if (result.workedBefore)
					{
						$('#locator').addClass("workedGrid");
						$('#locator').attr('title', 'Grid was already worked in the past');
					}
					else
					{
						$('#locator').addClass("newGrid");
						$('#locator').attr('title', 'New grid!');
					}
				}
				else
				{
					$('#locator').removeClass("workedGrid");
					$('#locator').removeClass("newGrid");
					$('#locator').attr('title', '');
				}
			}
			/* Find Operators Name */
			if($('#qsl_via').val() == "") {
				$('#qsl_via').val(result.qsl_manager);
			}
			/* Find Operators Name */
			if($('#name').val() == "") {
				$('#name').val(result.callsign_name);
			}
			if($('#qth').val() == "") {
				$('#qth').val(result.callsign_qth);
			}
			/*
			* Update state with returned value
			*/
			if($("#input_usa_state").val() == "") {
				$("#input_usa_state").val(result.callsign_state).trigger('change');
			}
			if($('#iota_ref').val() == "") {
				$('#iota_ref').val(result.callsign_iota);
			}
			// Hide the last QSO table
			$('#qso-last-table').hide();
			$('#partial_view').show();
			/* display past QSOs */
			$('#partial_view').html(result.partial);
		});
	} else {
		/* Reset fields ... */
		$('#callsign_info').text("");
		$('#locator_info').text("");
		$('#country').val("");
		$('#dxcc_id').val("");
		$('#cqz').val("");
		$('#name').val("");
		$('#qth').val("");
		$('#locator').val("");
		$('#iota_ref').val("");
		$("#locator").removeClass("workedGrid");
		$("#locator").removeClass("newGrid");
		$("#callsign").removeClass("workedGrid");
		$("#callsign").removeClass("newGrid");
		$('#callsign_info').removeClass("badge-secondary");
		$('#callsign_info').removeClass("badge-success");
		$('#callsign_info').removeClass("badge-danger");
		$('#input_usa_state').val("");
	}
})
// Only set the frequency when not set by userdata/PHP.
if ($('#frequency').val() == "")
{
	$.get('qso/band_to_freq/' + $('#band').val() + '/' + $('.mode').val(), function(result) {
		$('#frequency').val(result);
		$('#frequency_rx').val("");
	});
}
/* on mode change */
$('.mode').change(function() {
	$.get('qso/band_to_freq/' + $('#band').val() + '/' + $('.mode').val(), function(result) {
		$('#frequency').val(result);
		$('#frequency_rx').val("");
	});
});
/* Calculate Frequency */
/* on band change */
$('#band').change(function() {
	$.get('qso/band_to_freq/' + $(this).val() + '/' + $('.mode').val(), function(result) {
		$('#frequency').val(result);
		$('#frequency_rx').val("");
	});
});
/* On Key up Calculate Bearing and Distance */
$("#locator").keyup(function(){
	if ($(this).val()) {
		var qra_input = $(this).val();
		var qra_lookup = qra_input.substring(0, 4);
		if(qra_lookup.length >= 4) {
			// Check Log if satname is provided
			if($("#sat_name" ).val() != "") {
				//logbook/jsonlookupgrid/io77/SAT/0/0
				$.getJSON('logbook/jsonlookupgrid/' + qra_lookup.toUpperCase() + '/SAT/0/0', function(result)
				{
					// Reset CSS values before updating
					$('#locator').removeClass("workedGrid");
					$('#locator').removeClass("newGrid");
					$('#locator').attr('title', '');
					if (result.workedBefore)
					{
						$('#locator').addClass("workedGrid");
						$('#locator').attr('title', 'Grid was already worked in the past');
					}
					else
					{
						$('#locator').addClass("newGrid");
						$('#locator').attr('title', 'New grid!');
					}
				})
			} else {
				$.getJSON('logbook/jsonlookupgrid/' + qra_lookup.toUpperCase() + '/0/' + $("#band").val() +'/' + $("#mode").val(), function(result)
				{
					// Reset CSS values before updating
					$('#locator').removeClass("workedGrid");
					$('#locator').removeClass("newGrid");
					$('#locator').attr('title', '');
					if (result.workedBefore)
					{
						$('#locator').addClass("workedGrid");
						$('#locator').attr('title', 'Grid was already worked in the past');
					}
					else
					{
						$('#locator').addClass("newGrid");
						$('#locator').attr('title', 'New grid!');
					}
				})
			}
		}
		if(qra_input.length >= 4 && $(this).val().length > 0) {
			$.getJSON('logbook/qralatlngjson/' + $(this).val(), function(result)
			{
				// Set Map to Lat/Long
				markers.clearLayers();
				if (typeof result !== "undefined") {
					var redIcon = L.icon({
						iconUrl: icon_dot_url,
						iconSize:     [18, 18], // size of the icon
					});
					var marker = L.marker([result[0], result[1]], {icon: redIcon});
					mymap.setZoom(8);
					mymap.panTo([result[0], result[1]]);
				}
				markers.addLayer(marker).addTo(mymap);
			})
			$('#locator_info').load("logbook/searchbearing/" + $(this).val()).fadeIn("slow");
		}
	}
});
// Change report based on mode
$('.mode').change(function(){
	setRst($('.mode') .val());
});
function convert_case(str) {
	var lower = str.toLowerCase();
	return lower.replace(/(^| )(\w)/g, function(x) {
		return x.toUpperCase();
	});
}
$('#dxcc_id').on('change', function() {
	$.getJSON('logbook/jsonentity/' + $(this).val(), function (result) {
		if (result.dxcc.name != undefined) {
			$('#country').val(convert_case(result.dxcc.name));
			$('#cqz').val(convert_case(result.dxcc.cqz));
			$('#callsign_info').removeClass("badge-secondary");
			$('#callsign_info').removeClass("badge-success");
			$('#callsign_info').removeClass("badge-danger");
			$('#callsign_info').attr('title', '');
			$('#callsign_info').text(convert_case(result.dxcc.name));
			changebadge(result.dxcc.name);
			// Set Map to Lat/Long it locator is not empty
			if($('#locator').val() == "") {
				var redIcon = L.icon({
					iconUrl: icon_dot_url,
					iconSize:     [18, 18], // size of the icon
				});
				markers.clearLayers();
				var marker = L.marker([result.dxcc.lat, result.dxcc.long], {icon: redIcon});
				mymap.setZoom(8);
				mymap.panTo([result.dxcc.lat, result.dxcc.long]);
				markers.addLayer(marker).addTo(mymap);
			}
		}
	});
});
//Spacebar moves to the name field when you're entering a callsign
//Similar to contesting ux, good for pileups.
$("#callsign").on("keypress", function(e) {
	if (e.which == 32){
		$("#name").focus();
		return false; //Eliminate space char
	}
});
// On Key up check and suggest callsigns
$("#callsign").keyup(function() {
	if ($(this).val().length >= 3) {
		$('.callsign-suggest').show();
		$.get('lookup/scp/' + $(this).val().toUpperCase(), function(result) {
			$('.callsign-suggestions').text(result);
		});
	}
});