var projectid = 3;  
var submittedyn = 'Y';
//var numRec = countRecordings();

$(function() {
		   
	var firstname = $('#firstname').text();
	var lastname = $('#lastname').text();
	var ageid = $('#displayAge').text();  
	var genderid = $('#displayGender').text();
	var email = $('#email').text();  
	var usertypeid = $('#displayType').text();
	//var questionid = $('#myQuestionsList li:first').val();  
	var filename = $('#filenameFromFlash').text();	
	
	
	//DYNAMICALLY CREATE REGION CBO BASED ON COUNTRY CBO SELECTION
	$('#cboCountry').change(function() {
		var countryid = $('#cboCountry').val();
		$('#cboRegion').removeClass("hidden");
		$('.rowDry').addClass("hidden");
		$('.rowWet').addClass("hidden");
		$('#loadingRegion').removeClass("hidden");
		
		$.ajax({
			url: 'db_cbo_region.php',
			type: 'POST',
			data:	'countryid=' + countryid,
			
			success: function(result) {
				//console.log("success!");
				$('#loadingRegion').addClass("hidden");
				$('#cboRegion option').remove();
				$('#cboRegion').append('<option value="0" selected="selected">Select Region...</option>');
				$('#cboRegion').append(result);
			}
		});
		return false;
	});
	
	//DYNAMICALLY CREATE CITY CBO BASED ON REGION CBO SELECTION
	$('#cboRegion').change(function() {
		var regionid = $('#cboRegion').val();
		$('#drywet').removeClass("hidden").fadeIn(500);
		$('#loadingCity').removeClass("hidden");
		$('#loadingOceanFeature').removeClass("hidden");
		
		$.ajax({
			url: 'db_cbo_city.php',
			type: 'POST',
			data:	'regionid=' + regionid,
			
			success: function(result) {
				//console.log("success!");
				$('#loadingCity').addClass("hidden");
				$('#cboCity option').remove();
				$('#cboCity').append('<option value="0" selected="selected">Select City...</option>');
				$('#cboCity').append(result);
			}
		});
		$.ajax({
			url: 'db_cbo_oceanfeature.php',
			type: 'POST',
			data:	'regionid=' + regionid,
			
			success: function(result) {
				//console.log("success!");
				$('#loadingOceanFeature').addClass("hidden");
				$('#cboOceanFeatureName option').remove();
				$('#cboOceanFeatureName').append('<option value="0" selected="selected">Select Ocean Feature...</option>');
				$('#cboOceanFeatureName').append(result);
			}
		});
		return false;
	});
	
	//DYNAMICALLY CREATE OCEAN FEATURE NAME CBO BASED ON OCEAN FEATURE CBO SELECTION
	$('#cboOceanFeatureType').change(function() {
		var fcode = $('#cboOceanFeatureType').val();
		$('#loadingOceanRegion').removeClass("hidden");
		
		$.ajax({
			url: 'db_cbo_oceanregion.php',
			type: 'POST',
			data:	'fcode=' + fcode,
			
			success: function(result) {
				//console.log("success!");
				$('#loadingOceanRegion').addClass("hidden");
				$('#cboOceanRegionName option').remove();
				$('#cboOceanRegionName').append('<option value="0" selected="selected">Select...</option>');
				$('#cboOceanRegionName').append(result);
			}
		});
		return false;
	});
	
	//MODIFY SELF-PLACEMENT MAP BASED ON CITY SELECTION
	/*$('#geoSearch').change(function() {
		var geonameid = $('#cboCity').val();
		var table = 'cities1000';
		
		$.ajax({
			url: 'db_geoname_latlon.php',
			type: 'POST',
			data:	'geonameid=' + geonameid +
					'&table=' + table,
			
			success: function(result) {
				$('.dmap').removeClass("hidden");
				demoMap.checkResize();
				demoMap.clearOverlays();
				
				point = GLatLng.fromUrlValue(result);
				demoMap.setCenter(point,10, G_NORMAL_MAP);
				markerD = new GMarker(point, {draggable: true}); 
				demoMap.addOverlay(markerD);
				markerD.enableDragging();
				$("#myLatitude").text(markerD.getPoint().lat().toFixed(6));
				$("#myLongitude").text(markerD.getPoint().lng().toFixed(6));
				GEvent.addListener(markerD, "drag", function(){
					//$("#myLocation").text(markerD.getPoint().toUrlValue());
					$("#myLatitude").text(markerD.getPoint().lat().toFixed(6));
					$("#myLongitude").text(markerD.getPoint().lng().toFixed(6));
				});	
				
			}
		});
		return false;
	});*/
	
	//MODIFY SELF-PLACEMENT MAP BASED ON OCEAN FEATURE SELECTION
	$('#cboOceanFeatureName').change(function() {
		var geonameid = $('#cboOceanFeatureName').val();
		var table = 'ocean';
		
		$.ajax({
			url: 'db_geoname_latlon.php',
			type: 'POST',
			data:	'geonameid=' + geonameid +
					'&table=' + table,
			
			success: function(result) {
				$('.dmap').removeClass("hidden");
				demoMap.checkResize();
				demoMap.clearOverlays();
				
				point = GLatLng.fromUrlValue(result);
				demoMap.setCenter(point,10, G_NORMAL_MAP);
				markerD = new GMarker(point, {draggable: true}); 
				demoMap.addOverlay(markerD);
				markerD.enableDragging();
				$("#myLatitude").text(markerD.getPoint().lat().toFixed(6));
				$("#myLongitude").text(markerD.getPoint().lng().toFixed(6));
				GEvent.addListener(markerD, "drag", function(){
					//$("#myLocation").text(markerD.getPoint().toUrlValue());
					$("#myLatitude").text(markerD.getPoint().lat().toFixed(6));
					$("#myLongitude").text(markerD.getPoint().lng().toFixed(6));
				});	
				
			}
		});
		return false;
	});
	
	//MODIFY SELF-PLACEMENT MAP BASED ON OCEAN REGION SELECTION
	$('#cboOceanRegionName').change(function() {
		var geonameid = $('#cboOceanRegionName').val();
		var table = 'ocean';
		
		$.ajax({
			url: 'db_geoname_latlon.php',
			type: 'POST',
			data:	'geonameid=' + geonameid +
					'&table=' + table,
			
			success: function(result) {
				$('.dmap').removeClass("hidden");
				demoMap.checkResize();
				demoMap.clearOverlays();
				
				point = GLatLng.fromUrlValue(result);
				demoMap.setCenter(point,6, G_NORMAL_MAP);
				markerD = new GMarker(point, {draggable: true}); 
				demoMap.addOverlay(markerD);
				markerD.enableDragging();
				$("#myLatitude").text(markerD.getPoint().lat().toFixed(6));
				$("#myLongitude").text(markerD.getPoint().lng().toFixed(6));
				GEvent.addListener(markerD, "drag", function(){
					//$("#myLocation").text(markerD.getPoint().toUrlValue());
					$("#myLatitude").text(markerD.getPoint().lat().toFixed(6));
					$("#myLongitude").text(markerD.getPoint().lng().toFixed(6));
				});	
				
			}
		});
		return false;
	});

	countRecordings();
	//$('#numberRecordings').text(numRec);
	
});

//ADD NEW RECORDING TO DATABASE
function addRecordingToDb() {
	//$('#closeRecDialog').click(function() {
		var firstname = $('#firstname').val();
		var lastname = $('#lastname').val();
		var email = $('#email').val();
		var ageid = $('#cboAge').val();  
		var genderid = $('#cboGender').val();
		var usertypeid = $('#cboType').val();
		var geonameid = $('#locationid').val();
		var latitude = $('#myLatitude').text();
		var longitude = $('#myLongitude').text();
		var filename = $('#filenameFromFlash').text();
		var comment = $('#comment').val();
		var categoryid = $('#myCategoriesList li').attr("value");
		var subcategoryid = $('#mySubCategoriesList li').attr("value");
		var questionid = selectedQuestionid;
		$.ajax({
			url: 'rec_db_add2.php',
			type: 'POST',
			data:	'firstname=' + firstname +
					'&lastname=' + lastname +
					'&ageid=' + ageid +
					'&genderid=' + genderid +
					'&email=' + email +
					'&usertypeid=' + usertypeid +
					'&geonameid=' + geonameid +
					'&latitude=' + latitude +
					'&longitude=' + longitude +
					'&questionid=' + questionid +
					'&filename=' + filename +
					'&projectid=' + projectid +
					'&categoryid=' + categoryid +
					'&subcategoryid=' + subcategoryid +
					'&submittedyn=' + submittedyn +
					'&comment=' + comment,
			
			success: function(result) {
				//console.log("success!");
				countRecordings();
				//$('#responsetext').remove();
				//$('#feedback').append('<p id="responsetext">' + result + '</p>').slideDown(2000);
			}
		});
		return false;
	//});
}

//returns number of recordings in database
function countRecordings() {
		$.ajax({
			url: 'rec_db_count.php',
			type: 'POST',
					
			success: function(result) {
				//console.log("success!");
				$('#numberRecordings').text(result);
			}
		});
		//return false;
}

//send emails to admin and participant
function sendEmail() {
	//$('#closeRecDialog').click(function() {
		var firstname = $('#firstname').val();
		var lastname = $('#lastname').val();
		var email = $('#email').val();
		var ageid = $('#cboAge').val();  
		var genderid = $('#cboGender').val();
		var usertypeid = $('#cboType').val();
		var locationid = $('#cboCity').val();
		var filename = $('#filenameFromFlash').text();
		var comment = $('#comment').val();
		var questionid = selectedQuestionid;
		$.ajax({
			url: "http://oceanvoices.dyndns.org:80/recon/mail.php",
			type: 'POST',
			data:	'firstname=' + firstname +
					'&lastname=' + lastname +
					'&ageid=' + ageid +
					'&genderid=' + genderid +
					'&email=' + email +
					'&usertypeid=' + usertypeid +
					'&locationid=' + locationid +
					'&questionid=' + questionid +
					'&filename=' + filename +
					'&projectid=' + projectid +
					'&submittedyn=' + submittedyn +
					'&comment=' + comment,
			
			success: function(result) {
				//console.log("success!");
			}
		});
		return false;
	//});
}