//dojo.require("dijit.form.DateTextBox");

var tmp_item_obj;
function ClearSchedule(id)
{
	dojo.byId('sched_date_'+id).value = '';
	dojo.byId('sched_name_'+id).value = '';
	dojo.byId('sched_location_name_'+id).value = '';
	dojo.byId('sched_address_'+id).value = '';
	dojo.byId('sched_city_'+id).value = '';
	dojo.byId('sched_state_'+id).value = '';
	dojo.byId('sched_zip_'+id).value = '';
	dojo.byId('sched_map_link_'+id).value = '';
	dojo.byId('sched_small_img_filename_'+id).value = '';
	dojo.byId('sched_large_img_filename_'+id).value = '';
	dojo.byId('sched_note_'+id).value = '';
}
function CopyScheduleAsNew(id)
{
	dojo.xhrPost({
		url:'home.php?home_ajax=1&',
		content:{'param':'GetScheduleItemAdminView', 'id':id},
		load: function(data, ioargs)
		{
			div_node = document.createElement('div');
			div_node.id = 'schedule_item_tmp';
			div_node.innerHTML = 'fdsaf';
			div_node.style.display = 'none';
			document.getElementsByTagName('body')[0].appendChild(div_node);
			document.getElementById('schedule_item_tmp').innerHTML = data;
			MoveDataToNew(id);
			DeleteScheduleItem('tmp', true);
			
		}
		});

}

function MoveDataToNew(id)
{
	var date = dojo.byId('sched_date_'+id).value;
	var name = dojo.byId('sched_name_'+id).value;
	var location_name = dojo.byId('sched_location_name_'+id).value;
	var address = dojo.byId('sched_address_'+id).value;
	var city = dojo.byId('sched_city_'+id).value;
	var state = dojo.byId('sched_state_'+id).value;
	var zip = dojo.byId('sched_zip_'+id).value;
	var map_link = dojo.byId('sched_map_link_'+id).value;
	var small_img_filename = dojo.byId('sched_small_img_filename_'+id).value;
	var large_img_filename = dojo.byId('sched_large_img_filename_'+id).value;
	var note = dojo.byId('sched_note_'+id).value;
	
	dojo.byId('sched_date_new').value = date;
	dojo.byId('sched_name_new').value = name;
	dojo.byId('sched_location_name_new').value = location_name;
	dojo.byId('sched_address_new').value = address;
	dojo.byId('sched_city_new').value = city;
	dojo.byId('sched_state_new').value = state;
	dojo.byId('sched_zip_new').value = zip;
	dojo.byId('sched_map_link_new').value = map_link;
	dojo.byId('sched_small_img_filename_new').value = small_img_filename;
	dojo.byId('sched_large_img_filename_new').value = large_img_filename;
	dojo.byId('sched_note_new').value = note;
}

function RefreshScheduleAreaByDateRangeInput(override_min_date, override_max_date)
{
	var min_month = ''+dojo.byId('min_date_month').value;
	var min_day = ''+dojo.byId('min_date_day').value;
	var min_year = ''+dojo.byId('min_date_year').value;
	var max_month = ''+dojo.byId('max_date_month').value;
	var max_day = ''+dojo.byId('max_date_day').value;
	var max_year = ''+dojo.byId('max_date_year').value;
	
	var min_date = min_year+min_month+min_day;
	var max_date = max_year+max_month+max_day;
	
	RefreshScheduleArea(0, min_date, max_date);
}

function RefreshScheduleArea(goto_id, min_date, max_date)
{
	dojo.xhrPost({
		url:'home.php?home_ajax=1&',
		content:{'param':'RefreshScheduleArea', 'min_date':(min_date?min_date:0), 'max_date':(max_date?max_date:0)},
		load: function(data, ioargs)
		{
			document.getElementById('psched_area').innerHTML = data;
			
			if(goto_id)
			{
				SetScheduleItemViewToNormal(goto_id);
			}
		}
		});
}

function SetScheduleItemViewToAdmin(id)
{
	dojo.xhrPost({
		url:'home.php?home_ajax=1&',
		content:{'param':'GetScheduleItemAdminView', 'id':id},
		load: function(data, ioargs)
		{
			document.getElementById('schedule_item_'+id).innerHTML = data;
		}
		});
}

function SetScheduleItemViewToNormal(id)
{
	dojo.xhrPost({
		url:'home.php?home_ajax=1&',
		content:{'param':'GetScheduleItemNormalView', 'id':id},
		load: function(data, ioargs)
		{
			window.location = '#schedule_item_'+id;
						
			var item_obj = document.getElementById('schedule_item_'+id);
			//window.scrollTo(0, item_obj.offsetTop);
			item_obj.innerHTML = data;
			item_obj.style.backgroundColor = '#a0ffa0';
			var item_anim = dojo.animateProperty({
				node: item_obj,
				duration: 3000,
				delay: 10,
				properties: {'backgroundColor':{end: '#ffffff'}}
			});
			item_anim.play();
		}
		});
}

function SaveScheduleItem(id)
{
	var date = dojo.byId('sched_date_'+id).value;
	var name = dojo.byId('sched_name_'+id).value;
	var location_name = dojo.byId('sched_location_name_'+id).value;
	var address = dojo.byId('sched_address_'+id).value;
	var city = dojo.byId('sched_city_'+id).value;
	var state = dojo.byId('sched_state_'+id).value;
	var zip = dojo.byId('sched_zip_'+id).value;
	var map_link = dojo.byId('sched_map_link_'+id).value;
	var small_img_filename = dojo.byId('sched_small_img_filename_'+id).value;
	var large_img_filename = dojo.byId('sched_large_img_filename_'+id).value;
	var note = dojo.byId('sched_note_'+id).value;
	
	AddUpdateScheduleItem(SaveScheduleItemCallback, id,date,name,location_name,address,city,state,zip,map_link,small_img_filename,large_img_filename,note);
	//alert(date+'\r\n'+name+'\r\n'+location_name+'\r\n'+address+'\r\n'+city+'\r\n'+state+'\r\n'+zip+'\r\n'+map_link+'\r\n'+small_img_filename+'\r\n'+large_img_filename);

}
function SaveScheduleItemCallback(data)
{
	if(data.is_new)
	{
		RefreshScheduleArea(data.id);
		ClearSchedule('new');
	}
	else
		SetScheduleItemViewToNormal(data.id);
}

function DeleteScheduleItem(id, is_force_delete)
{
	if(is_force_delete || confirm('Are you sure you want to delete this schedule item?'))
	{
		dojo.xhrPost({
			url:'home.php?home_ajax=1&',
			content:{'param':'DeleteScheduleItem', 'id':id},
			load: function(data, ioargs)
			{
				document.getElementById('schedule_item_'+id).parentNode.removeChild(document.getElementById('schedule_item_'+id));
			}
			});
	}
}
