var key = "START";

function updateKey(newKey) {
	var key = newKey;
	if (key != "") {
		for ( var j = 0; j < document.links.length; j++) {
			document.links[j].href = document.links[j].href + "&key=" + key;
		}
		document.getElementById('keyField').disabled = true;
		document.getElementById('inputKey').disabled = true;
		document.getElementById('gazetteerFieldA').checked = true;
		document.getElementById('gazetteerFieldB').checked = false;

		document.getElementById('contactLink').href = "contact.html";
		document.getElementById('homeLink').href = "index.html";
		document.getElementById('placesLink').href = "places.html";
		document.getElementById('geocodesLink').href = "geocodes.html";
		document.getElementById('textLink').href = "text.html";
		document.getElementById('registerLink').href = "register";
	} else {
		alert("Please enter your API key first.");
	}
}

function selectedApi() {
	if (document.getElementById('API').value == "nameSearch") {
		$('#nameFieldLabel').show();
		$('#nameField').show();
		$('#featureTypeFieldLabel').hide();
		$('#featureTypeField').hide();
		$('#operatorFieldLabel').hide();
		$('#operatorField').hide();
		$('#minXFieldLabel').hide();
		$('#minXField').hide();
		$('#minYFieldLabel').hide();
		$('#minYField').hide();
		$('#maxXFieldLabel').hide();
		$('#maxXField').hide();
		$('#maxYFieldLabel').hide();
		$('#maxYField').hide();
		$('#identifierFieldLabel').hide();
		$('#identifierField').hide();
		document.getElementById('gazetteerFieldB').checked = true;
		document.getElementById('gazetteerFieldA').checked = false;
	}

	if (document.getElementById('API').value == "nameAndFeatureSearch") {
		$('#nameFieldLabel').show();
		$('#nameField').show();
		$('#featureTypeFieldLabel').show();
		$('#featureTypeField').show();
		$('#operatorFieldLabel').hide();
		$('#operatorField').hide();
		$('#minXFieldLabel').hide();
		$('#minXField').hide();
		$('#minYFieldLabel').hide();
		$('#minYField').hide();
		$('#maxXFieldLabel').hide();
		$('#maxXField').hide();
		$('#maxYFieldLabel').hide();
		$('#maxYField').hide();
		$('#identifierFieldLabel').hide();
		$('#identifierField').hide();
		document.getElementById('gazetteerFieldB').checked = true;
		document.getElementById('gazetteerFieldA').checked = false;
	}

	if (document.getElementById('API').value == "postCodeSearch") {
		$('#nameFieldLabel').show();
		$('#nameField').show();
		$('#featureTypeFieldLabel').hide();
		$('#featureTypeField').hide();
		$('#operatorFieldLabel').hide();
		$('#operatorField').hide();
		$('#minXFieldLabel').hide();
		$('#minXField').hide();
		$('#minYFieldLabel').hide();
		$('#minYField').hide();
		$('#maxXFieldLabel').hide();
		$('#maxXField').hide();
		$('#maxYFieldLabel').hide();
		$('#maxYField').hide();
		$('#identifierFieldLabel').hide();
		$('#identifierField').hide();

		document.getElementById('gazetteerFieldB').checked = false;
		document.getElementById('gazetteerFieldA').checked = true;
	}

	if (document.getElementById('API').value == "footprintLookup") {
		$('#nameFieldLabel').hide();
		$('#nameField').hide();
		$('#featureTypeFieldLabel').hide();
		$('#featureTypeField').hide();
		$('#operatorFieldLabel').hide();
		$('#operatorField').hide();
		$('#minXFieldLabel').hide();
		$('#minXField').hide();
		$('#minYFieldLabel').hide();
		$('#minYField').hide();
		$('#maxXFieldLabel').hide();
		$('#maxXField').hide();
		$('#maxYFieldLabel').hide();
		$('#maxYField').hide();
		$('#identifierFieldLabel').show();
		$('#identifierField').show();
		document.getElementById('gazetteerFieldB').checked = true;
		document.getElementById('gazetteerFieldA').checked = false;
	}

	if (document.getElementById('API').value == "spatialNameSearch") {
		$('#nameFieldLabel').show();
		$('#nameField').show();
		$('#featureTypeFieldLabel').hide();
		$('#featureTypeField').hide();
		$('#operatorFieldLabel').show();
		$('#operatorField').show();
		$('#minXFieldLabel').show();
		$('#minXField').show();
		$('#minYFieldLabel').show();
		$('#minYField').show();
		$('#maxXFieldLabel').show();
		$('#maxXField').show();
		$('#maxYFieldLabel').show();
		$('#maxYField').show();
		$('#identifierFieldLabel').hide();
		$('#identifierField').hide();
		document.getElementById('gazetteerFieldB').checked = true;
		document.getElementById('gazetteerFieldA').checked = false;
	}

	if (document.getElementById('API').value == "spatialFeatureTypeSearch") {
		$('#nameFieldLabel').hide();
		$('#nameField').hide();
		$('#featureTypeFieldLabel').show();
		$('#featureTypeField').show();
		$('#operatorFieldLabel').show();
		$('#operatorField').show();
		$('#minXFieldLabel').show();
		$('#minXField').show();
		$('#minYFieldLabel').show();
		$('#minYField').show();
		$('#maxXFieldLabel').show();
		$('#maxXField').show();
		$('#maxYFieldLabel').show();
		$('#maxYField').show();
		$('#identifierFieldLabel').hide();
		$('#identifierField').hide();
		document.getElementById('gazetteerFieldB').checked = true;
		document.getElementById('gazetteerFieldA').checked = false;
	}

	if (document.getElementById('API').value == "supportedFeatureTypes") {
		$('#nameFieldLabel').hide();
		$('#nameField').hide();
		$('#featureTypeFieldLabel').hide();
		$('#featureTypeField').hide();
		$('#operatorFieldLabel').hide();
		$('#operatorField').hide();
		$('#minXFieldLabel').hide();
		$('#minXField').hide();
		$('#minYFieldLabel').hide();
		$('#minYField').hide();
		$('#maxXFieldLabel').hide();
		$('#maxXField').hide();
		$('#maxYFieldLabel').hide();
		$('#maxYField').hide();
		$('#identifierFieldLabel').hide();
		$('#identifierField').hide();
		document.getElementById('gazetteerFieldB').checked = true;
		document.getElementById('gazetteerFieldA').checked = false;
	}
}

function submitButton() {
	var server = 'http://unlock.edina.ac.uk/ws/';
	var api = document.getElementById('API').value + '?'

	var keyParam = '';
	if (document.getElementById('keyField').disabled == true) {
		keyParam = '&key=' + document.getElementById('keyField').value
	}

	var gazetteers = '';
	if (document.getElementById('gazetteerFieldA').checked == true
			&& document.getElementById('gazetteerFieldA').checked == false) {
		gazetteers = gazetteers + '&gazetteer=os';
	}
	if (document.getElementById('gazetteerFieldB').checked == true
			&& document.getElementById('gazetteerFieldA').checked == false) {
		gazetteers = gazetteers + '&gazetteer=geonames';
	}
	if (document.getElementById('gazetteerFieldA').checked == true
			&& document.getElementById('gazetteerFieldB').checked == true) {
		gazetteers = gazetteers + '&gazetteer=os,geonames';
	}

	var format = '&format=' + document.getElementById('formatField').value;
	var nameParam = '&name=' + document.getElementById('nameField').value;
	var postCodeParam = '&postCode=' + document.getElementById('nameField').value;
	var maxRowsParam = '&maxRows=' + document.getElementById('maxRowsField').value;
	var startRowParam = '&startRow=' + document.getElementById('startRowField').value;
	var featureTypeParam = '&featureType=' + document
			.getElementById('featureTypeField').value;
	var operatorParam = '&operator=' + document.getElementById('operatorField').value;
	var identifierParam = '&identifier=' + document
			.getElementById('identifierField').value;
	var minXParam = '&minx=' + document.getElementById('minXField').value;
	var minYParam = '&miny=' + document.getElementById('minYField').value;
	var maxXParam = '&maxx=' + document.getElementById('maxXField').value;
	var maxYParam = '&maxy=' + document.getElementById('maxYField').value;

	if (document.getElementById('API').value == "nameSearch") {
		var url = server + api + format + nameParam + maxRowsParam
				+ startRowParam + gazetteers + keyParam;
		window.open(url);
	}

	if (document.getElementById('API').value == "nameAndFeatureSearch") {
		var url = server + api + format + nameParam + featureTypeParam
				+ maxRowsParam + startRowParam + gazetteers + keyParam;
		window.open(url);
	}

	if (document.getElementById('API').value == "postCodeSearch") {
		var url = server + api + format + postCodeParam + maxRowsParam
				+ startRowParam + gazetteers + keyParam;
		window.open(url);
	}

	if (document.getElementById('API').value == "footprintLookup") {
		var url = server + api + format + identifierParam + gazetteers
				+ keyParam;
		window.open(url);
	}

	if (document.getElementById('API').value == "spatialNameSearch") {
		var url = server + api + format + nameParam + minXParam + minYParam
				+ maxXParam + maxYParam + maxRowsParam + startRowParam
				+ gazetteers + keyParam;
		window.open(url);
	}

	if (document.getElementById('API').value == "spatialFeatureTypeSearch") {
		var url = server + "spatialFeatureSearch?" + format + featureTypeParam
				+ minXParam + minYParam + maxXParam + maxYParam + maxRowsParam
				+ startRowParam + gazetteers + keyParam;
		window.open(url);
	}
	if (document.getElementById('API').value == "supportedFeatureTypes") {
		var url = server + api + format + gazetteers + keyParam;
		window.open(url);
	}
}

function submitLatlonPostcodeButton() {
	var server = 'http://unlock.edina.ac.uk';

	var latitude = '';
	latitude = document.getElementById('latitudeField').value;

	var longitude = '';
	longitude = document.getElementById('longitudeField').value;

	var postalunits = '';
	if (document.getElementById('postalunitsFieldA').checked) {
		postalunits = 'Postal Units';
	} else
		postalunits = 'Postal Areas';
	var url = server + '/ws/spatialFeatureSearch?featureType='
		+ postalunits + '&operator=within&miny=' + latitude + '&maxy='
		+ latitude + '&minx=' + longitude + '&maxx=' + longitude;

	window.open(url);
}

function submitPostcodeLatlonButton() {
	var server = 'http://unlock.edina.ac.uk';

	var postcode ='';
	var postcodeA = '';
	var postcodeB = '';
	
	postcodeA = document.getElementById('postcodeFieldA').value;
	postcodeB = document.getElementById('postcodeFieldB').value;

	if(postcodeA.length == 3)
		postcodeA = postcodeA + ' ';
	var url = server + '/ws/nameAndFeatureSearch?featureType=postal units&name=' + postcodeA + postcodeB;

	window.open(url);
}

function updateTextGazetteerChoice(gazetteerChoice) {
	if (gazetteerChoice == "xwalk") {
		$('#keyNotice').show();
		$('#apiKeyLabel').show();
		$('#apiKey').show();
	} else {
		$('#keyNotice').hide();
		$('#apiKeyLabel').hide();
		$('#apiKey').hide();
	}
}

function startProcessing() {
	$('#processing-spinner').fadeIn("slow"); // hide the processing spinner
												// initially
	$('#processing-text').fadeIn("slow"); // hide the processing spinner
											// initially
	$.doTimeout(2000, function() {
		$('#processing-text').fadeOut("fast");
		$('#processing-text').html("Extracting place names...")
		$('#processing-text').fadeIn("slow");
	});
	$.doTimeout(2000, function() {
		$('#processing-text').html("Processing...")
		$('#processing-text').fadeIn("slow");
	});
}
