//
//
function add_task(
	when)
{
	//document.add_task_form.when.value = when;
	document.add_task_form.submit();
}

//
//
function nd_get_days_in_month(
	month,
	year)
{
	if ((month == 1) ||
	 	(month == 3) ||
	 	(month == 5) ||
	 	(month == 7) ||
	 	(month == 8) ||
	 	(month == 10) ||
	 	(month == 12))
	 {
	 	return 31;
	 }
	 if ((month == 4) ||
	 	 (month == 6) ||
	 	 (month == 9) ||
	 	 (month == 11))
	 {
	 	return 30;
	 }
	 if (((year % 4) == 0) &&
	 	 ((year % 100) != 0) ||
	 	 ((year % 400) == 0))
	 {
	 	return 29;
	 }
	 return 28;
}

//
//
function nd_update_days_in_month(
	daysInMonth)
{
	var taskDaySelect = document.getElementById("task_day");
	var currentDayIndex = taskDaySelect.selectedIndex;
	var options = taskDaySelect.options;
	options.length = daysInMonth;
	if (daysInMonth == 28)
	{
		options[28] = null;
		options[29] = null;
		options[30] = null;
	}
	else if (daysInMonth == 29)
	{
		options[28] = new Option("29", "29");
		options[29] = null;
		options[30] = null;
	}
	else if (daysInMonth == 30)
	{
		options[28] = new Option("29", "29");
		options[29] = new Option("30", "30");
		options[30] = null;
	}
	else if (daysInMonth == 31)
	{
		options[28] = new Option("29", "29");
		options[29] = new Option("30", "30");
		options[30] = new Option("31", "31");
	}
	if (currentDayIndex >= daysInMonth)
	{
		currentDayIndex = daysInMonth - 1;
	}
	taskDaySelect.selectedIndex = currentDayIndex;
}

//
//
function task_when_on_change(
	when)
{
	if (when == 5)
	{
		document.getElementById("task_when_date").style.display = 'block';
	}
	else
	{
		document.getElementById("task_when_date").style.display = 'none';
	}
}

//
//
function task_month_on_change(
	month)
{
	var year = document.getElementById("task_year").value;
	var daysInMonth = nd_get_days_in_month(month, year);
	nd_update_days_in_month(daysInMonth);
}

//
//
function task_year_on_change(
	yearIndex)
{
	var year = document.getElementById("task_year").value;
	var month = document.getElementById("task_month").value;
	var daysInMonth = nd_get_days_in_month(month, year);
	nd_update_days_in_month(daysInMonth);
}

//
//
function hide_story(
	story_id)
{
	document.getElementById("story_" + story_id).style.display = 'none';
}

//
//
function show_story(
	story_id)
{
	document.getElementById("story_" + story_id).style.display = 'block';
}

//
//
function set_google_calendar_enabled(
	user_calendar_id)
{
	id = 'google_calendar_check_' + user_calendar_id;
	enabled = document.getElementById(id).checked;
	new Ajax.Request(
				'/api/set_google_calendar_enabled?id=' + user_calendar_id + '&enabled=' + enabled,
				{
				    method:'get',
					onSuccess: function(transport)
									{
										alert(success);
									},
					onFailure: function()
									{
										alert(fail);
									}
				});  
}

//
//
function mark_story_read(
	story_id)
{
	window.last_story_marked_read = story_id;
	new Ajax.Request(
				'/api/mark_story_read/' + story_id,
				{
				    method:'get',
					onSuccess: function(transport)
									{
										hide_story(story_id)
										document.getElementById("undo_mark_read").style.display = 'inline';	
									},
					onFailure: function()
									{
										alert('mark_story_read: something went wrong...') 
									}
				});  
}

//
//
function undo_mark_read()
{
	if (window.last_story_marked_read != undefined)
	{
		new Ajax.Request(
				'/api/mark_story_unread/' + window.last_story_marked_read,
				{
				    method:'get',
					onSuccess: function(transport)
									{
										show_story(window.last_story_marked_read)
										document.getElementById("undo_mark_read").style.display = 'none';	
									},
					onFailure: function()
									{
										alert('mark_story_unread: something went wrong...') 
									}
				});
	}
}

//
//
function flag_story(
	story_id)
{
	new Ajax.Request(
				'/api/flag_story/' + story_id,
				{
				    method:'get',
					onSuccess: function(transport)
									{
										hide_story(story_id)
									},
					onFailure: function()
									{
										alert('flag_story: something went wrong...') 
									}
				});  
}
