
function refreshCommitmentsDisplayed() {
	if ($('#viewByCountry:visible').size() > 0) {
		showCountryList();
	} else {
		showCommitmentList();
	}
}

function submitNewCommitment() {
		$.ajax({
		url: '/apps/com.solertium.gogoego.server.extensions.ppo.commitment_tracker/commit',
		type: 'post',
		cache: false,
		data: $(this).serialize(),
		beforeSend: function(req) {
			$('#loadingMsg').text("Creating commitment...");
			$('#loadingCommitmentResults').show();
			$('#results').hide();
		  },
		error: function(req, error) {
		  	$('#statusBox').removeClass().addClass("error").text('Error creating new commitment');
		  	$('#loadingCommitmentResults').hide();
		  	$('#results').show();
		  },
		success: function(data, status) {
		  	$('#statusBox').removeClass().addClass("success").text('Created new commitment')
		  	refreshCommitmentsDisplayed();
		  	$('#loadingCommitmentResults').hide();
			$('#results').show();
		  }
		});
		return false;
	}

function addCommitmentActions() {
	$('button.more').click(function() {
		$(this).hide().siblings(".moreInfo").show();
	});
	$('button.hide').click(function() {
		$(this).parent(".moreInfo").hide().siblings('button.more').show();
		
	});
	$('button.edit').click(function() {
		$(this).siblings('.commitmentInfo').hide();
		$(this).siblings('.commitmentEditForm').show();
		$(this).hide();
	});
	$('input.update').click(function() {
		$(this).parent('.commitmentEditForm').hide().siblings('.commitmentInfo').show().siblings('button.edit').show();
	});
	$('input.cancel').click(function() {
		document.getElementById($(this).parent('form').attr("id")).reset();
		$(this).parent('form').hide().siblings('.commitmentInfo').show()
		if ($(this).parent('form').hasClass("commitmentNewForm")) {
			$(this).parent('form').siblings('button.addNew').removeAttr('disabled');
		} else if ($(this).parent('form').hasClass("countryEditForm")) {
			$(this).parent('form').siblings('button.addExisting').removeAttr('disabled');
		} else {
			$(this).parent('form').siblings('button.edit').show();
		}
	});
	$('button.addNew').click(function() {
		$(this).siblings('.commitmentInfo').hide();
		$(this).siblings('.commitmentNewForm').show();
		$(this).attr('disabled', 'disabled');
	});
	
	$('button.addExisting').click(function() {
		$(this).siblings('.commitmentInfo').hide();
		$(this).siblings('.countryEditForm').show();
		$(this).attr('disabled', 'disabled');
	});
	
	$('.commitmentNewForm').submit(submitNewCommitment);
	
	$('.commitmentEditForm').submit(function() {
		$.ajax({
		url: '/apps/com.solertium.gogoego.server.extensions.ppo.commitment_tracker/commit',
		type: 'post',
		cache: false,
		data: $(this).serialize(),
		beforeSend: function(req) {
			$('#loadingMsg').text("Updating commitment...");
			$('#loadingCommitmentResults').show();
			$('#results').hide();
		  },
		error: function(req, error) {
		  	$('#statusBox').removeClass().addClass("error").text('Error updating commitment');
		  	$('#loadingCommitmentResults').hide();
		  	$('#results').show();
		  },
		success: function(data, status) {
			$('#statusBox').removeClass().addClass("success").text('Updated commitment');
		  	refreshCommitmentsDisplayed();
		  	$('#loadingCommitmentResults').hide();
			$('#results').show();
		  }
		});
		return false;
	});
	
	$('.countryEditForm').submit(function() {
		$.ajax({
		url: '/apps/com.solertium.gogoego.server.extensions.ppo.commitment_tracker/country',
		type: 'post',
		cache: false,
		data: $(this).serialize(),
		beforeSend: function(req) {
			$('#loadingMsg').text("Updating country...");
			$('#loadingCommitmentResults').show();
			$('#results').hide();
		  },
		error: function(req, error) {
		  	$('#statusBox').removeClass().addClass("error").text('Error updating country');
		  	$('#loadingCommitmentResults').hide();
		  	$('#results').show();
		  },
		success: function(data, status) {
		  	$('#statusBox').removeClass().addClass("success").text('Updated country')
		  	refreshCommitmentsDisplayed();
		  	$('#loadingCommitmentResults').hide();
			$('#results').show();
		  }
		});
		return false;
	});
	
	$('.deleteFromCountry').click(function() {
		$(this).hide().siblings('.commitmentDeleteForm').show();
	});
	
	$('.cancelDeleteFromCountry').click(function () {
		$(this).parents('.commitmentDeleteForm').hide().siblings('.deleteFromCountry').show();
	});
	
	$('.commitmentDeleteForm').submit(function() {
		$.ajax({
		url: '/apps/com.solertium.gogoego.server.extensions.ppo.commitment_tracker/country/delete',
		type: 'post',
		cache: false,
		data: $(this).serialize(),
		beforeSend: function(req) {
			$('#loadingMsg').text("Deleting commitment from country...");
			$('#loadingCommitmentResults').show();
			$('#results').hide();
		  },
		error: function(req, error) {
		  	$('#statusBox').removeClass().addClass("error").text('Error deleting commitment from country');
		  	$('#loadingCommitmentResults').hide();
		  	$('#results').show();
		  },
		success: function(data, status) {
		  	$('#statusBox').removeClass().addClass("success").text('Deleted commitment from country')
		  	refreshCommitmentsDisplayed();
		  	$('#loadingCommitmentResults').hide();
			$('#results').show();
		  }
		});
		return false;
	});
}

function showCountryList() {
		var selection = $("#countryName option:selected").val();
		if (selection != "") {
			$('#loadingMsg').text("Loading countries...");
			$('#loadingCommitmentResults').show();
		
			$('#commitmentTracker #results').empty();
			$.ajax({
			  type: "GET",
			  url: "/resources/countryCommitments.html", 
			  data: "id=" + selection,
			  dataType: "html",
			  cache: false,
			  error: function(req, error){
			  	$('#loadingCommitmentResults').hide();
			  	$('#commitmentTracker #results').empty().append('<p>Unable to get information on commitments for this country.</p>');
			  },
			  success: function(data, status){
				$('#loadingCommitmentResults').hide();
				$('#commitmentTracker #results').empty().append(data);
				addCommitmentActions();
			  }
			});
		}
	}
	
function showCommitmentList() {
		var selection = $("#commitmentName option:selected").val();
		if (selection != "") {
			$('#loadingMsg').text("Loading commitments...");
			$('#loadingCommitmentResults').show();
		
			$('#commitmentTracker #results').empty();
			$.ajax({
			  type: "GET",
			  url: "/resources/commitmentInfo.html", 
			  data: "id=" + selection,
			  dataType: "html",
			  cache: false,
			  error: function(req, error){
			  	$('#loadingCommitmentResults').hide();
			  	$('#commitmentTracker #results').empty().append('<p>Unable to get information on this commitment.</p>');
			  },
			  success: function(data, status){
				$('#loadingCommitmentResults').hide();
				$('#commitmentTracker #results').empty().append(data);
				addCommitmentActions();
			  }
			});
		}
	}

function refreshUserTable(type) {
	resultId = '#' + type + 'Listing';
	$(resultId).empty().append('<img src="/images/socialicons/loadingCircle.gif" alt="Refreshing..." width="42" height="42"/>Refreshing list...');
	$.ajax({
	  type: "GET",
	  url: "/resources/commitUserMgmtTable.html", 
	  data: "type=" + type,
	  dataType: "html",
	  cache: false,
	  error: function(req, error){
	  	$(resultId).empty().append('<p>Unable to get list.</p>');
	  },
	  success: function(data, status){
		$(resultId).empty().append(data);
	  }
	});
}

$(document).ready(function(){
	$('#commitmentTracker #viewBy option[value=""]').attr("selected", "selected");
	$('#commitmentTracker #viewBy').change(function() {
		var selection = $("#viewBy option:selected").val();
		if (selection == "name") {
			$('#commitmentTracker #viewByName').show();
			$('#commitmentTracker #viewByCountry').hide();
			$('#commitmentTracker #results').empty();
		} else if (selection == "country") {
			$('#commitmentTracker #viewByCountry').show();
			$('#commitmentTracker #viewByName').hide();
			$('#commitmentTracker #results').empty();
		}
	});

	$('#commitmentTracker #commitmentName option[value=""]').attr("selected", "selected");
	$('#commitmentTracker #commitmentName').change(showCommitmentList);
	
	$('#commitmentTracker #countryName option[value=""]').attr("selected", "selected");
	$('#commitmentTracker #countryName').change(showCountryList);
	
	$('#addadminForm').submit(function() {
		$.ajax({
		url: '/apps/com.solertium.gogoego.server.extensions.ppo.commitment_tracker/admin/addUser',
		type: 'post',
		cache: false,
		data: $(this).serialize(),
		beforeSend: function(req) {
			$('#loadingadminAddResults').show();
		  },
		error: function(req, error) {
		  	$('#statusBox').removeClass().addClass("error").text('Error adding new administrator');
		  	$('#loadingadminAddResults').hide();
		  },
		success: function(data, status) {
		  	$('#statusBox').removeClass().addClass("success").text('Added new administrator')
		  	refreshUserTable("admin");
		  	$('#addadminForm input[name="admin"]').val("");
		  	$('#loadingadminAddResults').hide();
		  }
		});
		return false;
	});
	
	$('#adduserForm').submit(function() {
		$.ajax({
		url: '/apps/com.solertium.gogoego.server.extensions.ppo.commitment_tracker/admin/addUser',
		type: 'post',
		cache: false,
		data: $(this).serialize(),
		beforeSend: function(req) {
			$('#loadinguserAddResults').show();
		  },
		error: function(req, error) {
		  	$('#statusBox').removeClass().addClass("error").text('Error adding new editor');
		  	$('#loadinguserAddResults').hide();
		  },
		success: function(data, status) {
		  	$('#statusBox').removeClass().addClass("success").text('Added new editor')
		  	refreshUserTable("user");
		  	$('#adduserForm input[name="user"]').val("");
		  	$('#loadinguserAddResults').hide();
		  }
		});
		return false;
	});
	
	$('#deleteuserForm').submit(function() {
		$.ajax({
		url: '/apps/com.solertium.gogoego.server.extensions.ppo.commitment_tracker/admin/deleteUser',
		type: 'post',
		cache: false,
		data: $(this).serialize(),
		beforeSend: function(req) {
			$('#loadinguserDeleteResults').show();
		  },
		error: function(req, error) {
		  	$('#statusBox').removeClass().addClass("error").text('Error deleting editors');
		  	$('#loadinguserDeleteResults').hide();
		  },
		success: function(data, status) {
		  	$('#statusBox').removeClass().addClass("success").text('Deleted specified editors');
		  	refreshUserTable("user");
		  	$('#loadinguserDeleteResults').hide();
		  }
		});
		return false;
	});
	
	$('#deleteadminForm').submit(function() {
		$.ajax({
		url: '/apps/com.solertium.gogoego.server.extensions.ppo.commitment_tracker/admin/deleteUser',
		type: 'post',
		cache: false,
		data: $(this).serialize(),
		beforeSend: function(req) {
			$('#loadingadminDeleteResults').show();
		  },
		error: function(req, error) {
		  	$('#statusBox').removeClass().addClass("error").text('Error deleting administrators');
		  	$('#loadingadminDeleteResults').hide();
		  },
		success: function(data, status) {
		  	$('#statusBox').removeClass().addClass("success").text('Deleted specified administrators');
		  	refreshUserTable("admin");
		  	$('#loadingadminDeleteResults').hide();
		  }
		});
		return false;
	});
	
	$('#newCommitment button.addNewCommitment').click(function() {
		$(this).hide();
		$(this).siblings('.commitmentNewForm').show();
	});
	
	$('#newCommitment .commitmentNewForm input.cancel').click(function() {
		$(this).parents('form').hide().siblings('.addNewCommitment').show();
	});
	
	$('#newCommitment .commitmentNewForm').submit(submitNewCommitment);
	
});