jQuery(window).load(function() {
});
								
jQuery(function($) {
	$("input.StateName").change(function () {
		var StateName = $(this).val();
		var StateID = $(this).parents("tr").attr("ID");
		$('#jquerydebug').html('Updating ' +StateName+ '<br />');
		$.post("UpdateStateName.cfm", {StateID: StateID, StateName: StateName});
	});
	$("input.isState").change(function () {
		var isState = $(this).attr('checked');
		var StateID = $(this).parents("tr").attr("ID");
		$('#jquerydebug').html('Updating ' + isState + '...<br />');
		$.post("UpdateisState.cfm", {StateID: StateID, isState: isState});
	});
	$("input.datepicker").change(function () {
		var Statehood = $(this).val();
		var StateID = $(this).parents("tr").attr("ID");
		$('#jquerydebug').html('Updating ' + Statehood + '...<br />');
		$.post("UpdateStatehood.cfm", {StateID: StateID, Statehood: Statehood});
	});
	$("input.CityName").change(function() {
		alert("Yee haw");
	});
});

jQuery(function($) {
	$("a.delete").click(function() {
		if(confirm("Are you sure?")){
			var StateID = $(this).parents("tr").attr("ID");
			$('#jquerydebug').html('Deleting ' + StateID +'<br />');
			$.post("DeleteState.cfm", {StateID: StateID}); // Todo: error checking
			$(this).parents("tr").remove();
		}
		return false;
	});
});

jQuery(function($) {
	$("a.insert").click(function() {
		var currentRow = $(this).parents("tr:first");
		var newRow = currentRow.clone(true); // Might be easier just to build a new row instead of clone the old one.
		$('#jquerydebug').html('Inserting...<br />');
		newRow.find('input.StateName').val('');
		// newRow.find('input').attr('checkbox');
		newRow.find('input.datepicker').val('');
		$.post("InsertState.cfm", {StateID: 0},function (data) {
			newRow.attr("ID",data);
			currentRow.after(newRow);
		});
		return false;
	});
});

jQuery(function($) {
	$("a.reveal").click(function() {
		var StateID = $(this).parents("tr").attr("ID");

		var $subset = $('#Cities'+StateID);
		if ($subset.is(':hidden')) {
			$('#jquerydebug').html('Revealing '+ StateID + '...<br />');
			$.getJSON("RemoteCity.cfc?method=WhereStateID&returnFormat=json",{StateID:StateID},function(data) {
				var str = '<ul>';
				for (var I=0; I<data.length; I++) {
					str += '<li><input class="CityName" type="text" name="CityID' +data[I][0]+ '" value="' +data[I][1]+ '" /></li>'
				}
				str += '</ul>';
				$('#Cities'+StateID).html(str).slideDown("slow");
			});
		} else {
			$('#jquerydebug').html('Hiding '+ StateID + '...<br />');
			$('#Cities'+StateID).slideUp("slow");
		}
		return false;
	});
});
jQuery(function($) {
	$('.datepicker').datepicker();
});
