
function chat_submit( ) {
	// Add New Chat
	if ( $('new_chat_data').value == '' ) {
		return;
	}
	tmp = Hash.toQueryString({type: "chat", action: "submitChat", user_id: "1", chat_room_id: "1"});
	tmp += '&' + $('chatsubmit').toQueryString();
	$('new_chat_data').value = '';
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onComplete: function(){
			chat_load();
		}
	}).send();
	
	return false;
}

function chat_load( ) {
	// Get New Chats
	clearInterval(chatInterval);
	tstamp = $$('#chatpane input');
	size = tstamp.length;
	if ( tstamp.length > 0 ) {
		tstamp = tstamp[size-1].value;
	} else {
		tstamp = 0;
	}
	tmp = Hash.toQueryString({type: "chat", action: "getChats", chat_room_id: "1", time_stamp: tstamp});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		async: false,
		data: tmp,
		onComplete: function(){
			doScroll = 0;
			if ( ($('chatpane').scrollTop + 450) >= $('chatpane').scrollHeight ) {
				doScroll = 1;
			}
			
			$('chatpane').innerHTML += this.response.text;
			if ( doScroll == 1 ) {
				var myFx = new Fx.Scroll('chatpane').toBottom();
			}
			chatInterval = setInterval('chat_load()',1000);
		}
	}).send();
}

if ( $('new_chat_data') != undefined ) {
	$('new_chat_data').addEvents({
		'keyup': function( event ){
					if (event.key == 'enter') {
						chat_submit();
					}
				}
	});

	var chatInterval = setInterval('chat_load()',1500);
}
/////////////////////////////////

if ( $('memberslogin') != undefined ) {
	$('memberslogin').addEvents({
		'submit': function( ) {
					users_check_login();
					return false;			
				}
	});
}

function users_check_login( ) {
	// Check to see if the user has entered a valid username/password
	tmp = Hash.toQueryString({type: "users", action: "checkLogin"});
	tmp += '&' + $('memberslogin').toQueryString();
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			if ( text == 0 ) {
				$('login_error').innerHTML = "Invalid username and/or password";
			} else {
				$('login_error').innerHTML = "Logging in ...";
				users_login();
			}
		}
	}).send();
}

function users_login( ) {
	// Log in a user
	tmp = Hash.toQueryString({type: "users", action: "login"});
	tmp += '&' + $('memberslogin').toQueryString();
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onComplete: function( ){
			window.location = 'redirect/login.php';
		}
	}).send();
}

////////////////////////////////
function users_get_modifyUserForm( ) {
	// Get a user's information for the user mananger
	id = $('userManager_tab_editUser_content_userList').value;
	if ( id == '' ) {
		return;
	}

	tmp = Hash.toQueryString({type: "users", action: "get_user_modify_form", user_id: id, form_name: 'userManager_tab_edituser_content_modifyUserForm', submit_function: 'users_modifyUserForm_submit()'});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$('userManager_tab_editUser_content_modifyUserForm_container').setStyle('display','block');
			$('userManager_tab_editUser_content_modifyUserForm_container').innerHTML = text;
			users_modifyUserForm_checkAge();
		}
	}).send();
}

function users_modifyUserForm_checkAge( ) {
	prefix = 'userManager_tab_edituser_content_modifyUserForm__';

	if ( $(prefix+'age').value == 'adult' ) {
		$(prefix+'patrol_id').disabled = true;
		$(prefix+'patrol_id').selectedIndex = 1;
		$(prefix+'parent_1').disabled = true;
		$(prefix+'parent_1').selectedIndex = 1;
		$(prefix+'parent_2').disabled = true;
		$(prefix+'parent_2').selectedIndex = 1;
	} else {
		$(prefix+'patrol_id').disabled = false;
		$(prefix+'parent_1').disabled = false;
		$(prefix+'parent_2').disabled = false;
	}
	
	$(prefix+'age').addEvents({
		'change': function( ) {
					users_modifyUserForm_checkAge();
				}
	});
}

function users_modifyUserForm_submit( ) {
	// Save a user
	tmp = Hash.toQueryString({type: "users", action: "save", prefix: 'userManager_tab_edituser_content_modifyUserForm__'});
	tmp += '&' + $('userManager_tab_edituser_content_modifyUserForm').toQueryString();
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onComplete: function( ){
			$('userManager_tab_editUser_content_modifyUserForm_container').setStyle('display','none');
			$('userManager_tab_editUser_content_changesSaved').setStyle('visibility','visible');
			users_get_userList('userManager_tab_editUser_content_userList_container','userManager_tab_editUser_content_userList',false);
			$('userManager_tab_editUser_content_userList').addEvents({
				'change': function( ) {
							users_get_modifyUserForm();
							$('userManager_tab_editUser_content_changesSaved').setStyle('visibility','hidden');
						}
			});
		}
	}).send();	
}

function users_get_addUserForm( ) {
	tmp = Hash.toQueryString({type: "users", action: "get_user_add_form", form_name: 'userManager_tab_edituser_content_addUserForm', submit_function: 'users_addUserForm_submit()'});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$('userManager_tab_addUser_content_addUserForm_container').innerHTML = text;
		}
	}).send();
}

function users_addUserForm_submit( ) {
	// Save a user
	tmp = Hash.toQueryString({type: "users", action: "add", prefix: 'userManager_tab_edituser_content_addUserForm__'});
	tmp += '&' + $('userManager_tab_edituser_content_addUserForm').toQueryString();
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			users_get_userList('userManager_tab_editUser_content_userList_container','userManager_tab_editUser_content_userList',false);
			$('userManager_tab_editUser_content_userList').value = text;
			$('userManager_tab_editUser_content_modifyUserForm_container').innerHTML = '';
			users_get_modifyUserForm();
			$('userManager_tab_editUser').fireEvent('click');
			users_get_addUserForm();
		}
	}).send();	
}

function users_get_userList( container_name, fld_name, do_async ) {
	tmp = Hash.toQueryString({type: "users", action: "get_user_list", field_name: fld_name});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		async: do_async,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$(container_name).innerHTML = text;
		}
	}).send();
}

/*
function users_load_user_list_delete( ) {
	tmp = Hash.toQueryString({type: "users", action: "get_user_list", field_name: "usermanager_users_delete"});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$('user_list_delete').innerHTML = text;
			$('usermanager_users_delete').addEvents({
				'change': function( ) {
							users_get_delete_user();
							//$('changes_saved').setStyle('visibility','hidden');
						}
			});
		}
	}).send();
}
*/

function users_getChildNames( container_name, fld_name, do_async ) {
	// Get a list of children
	id = $('permission_userID').value;
	if ( id == '' ) {
		return;
	}

	tmp = Hash.toQueryString({type: "users", action: "get_child_list", field_name: fld_name, user_id: id});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		async: do_async,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$(container_name).innerHTML = text;
		}
	}).send();
}

function users_getParentNamesAttend( container_name, fld_name, do_async ) {
	// Get a list of which parents can attend
	tmp = Hash.toQueryString({type: "users", action: "get_parent_list_attend", field_name: fld_name});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		async: do_async,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$(container_name).innerHTML = text;
		}
	}).send();
}

function users_getEventList( container_name, fld_name, do_async ) {
	// Get a list of events
	tmp = Hash.toQueryString({type: "users", action: "get_event_list", field_name: fld_name});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		async: do_async,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$(container_name).innerHTML = text;
		}
	}).send();
}

function users_get_permission_data( ) {
	id = $('permission__childName').value;
	if ( id == '' ) {
		return;
	}

	tmp = Hash.toQueryString({type: "users", action: "get_permission_data", user_id: id});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, ignore3, text ){
			// Set the form data			
			eval(text);
			
			$('permission__phone_1').value = permission_data['emergency_phone_1'];
			$('permission__phone_2').value = permission_data['emergency_phone_2'];
			$('permission__phone_3').value = permission_data['emergency_phone_3'];
			$('permission__insurance_company').value = permission_data['medical_insurance_company'];
			$('permission__insurance_policy').value = permission_data['medical_insurance_policy'];
			$('permission__medication').value = permission_data['medications'];
			$('permission__medication_instructions').value = permission_data['medication_instructions'];
			$('permission__other').value = '';
		}
	}).send();
}

function users_permission_submit( ) {
	if ( $('permission__event_id').value == '' ) {
		$('permission__submitted').innerHTML = 'Please choose a trip';
		return;
	}
	
	if ( $('permission__childName').value == '' ) {
		$('permission__submitted').innerHTML = 'Please a Scout';
		return;
	}
	
	if ( $('permission__parentName').value == '' ) {
		$('permission__submitted').innerHTML = 'Please select which parents are attending';
		return;
	}
	
	// Submit a permission slip
	tmp = Hash.toQueryString({type: "users", action: "submit_permission_slip", prefix: 'permission__'});
	tmp += '&' + $('permission_submitForm').toQueryString();
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onComplete: function( ){
			$('permission__submitted').innerHTML = 'Permission slip submitted';
		}
	}).send();	
}

function users_getTripReport( ) {
	id = $('trip_report_tripsMenu').value;
	if ( id == '' ) {
		return;
	}
	
	tmp = Hash.toQueryString({type: "users", action: "get_trip_report", trip_id: id});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$('trip_report_container').innerHTML = text;
		}
	}).send();
}

if ( $('userManager_tab_editUser_content_userList_container') != undefined ) {
	users_get_userList('userManager_tab_editUser_content_userList_container','userManager_tab_editUser_content_userList',false);
	$('userManager_tab_editUser_content_userList').addEvents({
		'change': function( ) {
					users_get_modifyUserForm();
					$('userManager_tab_editUser_content_changesSaved').setStyle('visibility','hidden');
				}
	});
}

if ( $('userManager_tab_addUser_content_addUserForm_container') != undefined ) {
	users_get_addUserForm();
}

if ( $('permission_container') != undefined ) {
	users_getParentNamesAttend('permission_parentList_container','permission__parentName',true);
	users_getEventList('permission_eventList_container','permission__event_id',true);
	users_getChildNames('permission_childList_container','permission__childName',false);
	$('permission__childName').addEvents({
		'change': function( ) {
					users_get_permission_data();
				}
	});
}

if ( $('trip_report_container') != undefined ) {
	users_getEventList('trip_report_eventList_container','trip_report_tripsMenu',false);
	$('trip_report_tripsMenu').addEvents({
		'change': function( ) {
					users_getTripReport();
				}
	});
}

function users_getAttendanceReport( ) {
	id = $('trip_report_tripsMenu').value;
	if ( id == '' ) {
		return;
	}
	
	tmp = Hash.toQueryString({type: "users", action: "get_attendance_report", trip_id: id});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$('attendance_report_container').innerHTML = text;
		}
	}).send();
}

if ( $('attendance_report_container') != undefined ) {
	users_getEventList('attendance_report_eventList_container','trip_report_tripsMenu',false);
	$('trip_report_tripsMenu').addEvents({
		'change': function( ) {
					users_getAttendanceReport();
				}
	});
}
function events_editEvent_toggleAllDay( ) {
	var time_container = new Fx.Slide('time_container');

	if ( $('eventManager_editEvent_allDay').checked ) {
		time_container.slideOut();
	} else {
		time_container.slideIn();
	}
}

function events_editEvent_changeRepeat( ) {
	var repeat_until = new Fx.Slide('repeat_until');

	if ( $('eventManager_editEvent_repeatType').selectedIndex != 0 ) {
		repeat_until.slideIn();
	} else {
		repeat_until.slideOut();
	}
}

function events_addEvent_toggleAllDay( ) {
	var add_start_time_container = new Fx.Slide('add_start_time_container');
	var add_end_time_container = new Fx.Slide('add_end_time_container');

	if ( $('eventManager_tab_addEvent_content_addEventForm__allDay').checked ) {
		add_start_time_container.slideOut();
		add_end_time_container.slideOut();
	} else {
		add_start_time_container.slideIn();
		add_end_time_container.slideIn();
	}
}

function events_addEvent_changeRepeat( ) {
	var add_repeat_until = new Fx.Slide('add_repeat_until');

	if ( $('eventManager_tab_addEvent_content_addEventForm__repeatType').selectedIndex != 0 ) {
		add_repeat_until.slideIn();
	} else {
		add_repeat_until.slideOut();
	}
}

function events_get_modifyEventForm( ) {
	id = $('eventManager_tab_editEvent_content_userList').value;
	if ( id == '' ) {
		return;
	}

	tmp = Hash.toQueryString({type: "calendar", action: "get_event_modify_form", event_id: id, form_name: 'eventManager_tab_editEvent_content_modifyEventForm', submit_function: 'events_modifyEventForm_submit()'});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$('eventManager_tab_editEvent_content_modifyEventForm_container').setStyle('display','block');
			$('eventManager_tab_editEvent_content_modifyEventForm_container').innerHTML = text;
		}
	}).send();
}

function events_addEventForm_submit( ) {
	// Add an event
	tmp = Hash.toQueryString({type: "calendar", action: "addEvent", prefix: 'eventManager_tab_addEvent_content_addEventForm__'});
	tmp += '&' + $('eventManager_tab_addEvent_content_addEventForm').toQueryString();
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			//users_get_userList('userManager_tab_editUser_content_userList_container','userManager_tab_editUser_content_userList',false);
			//$('userManager_tab_editUser_content_userList').value = text;
			//$('userManager_tab_editUser_content_modifyUserForm_container').innerHTML = '';
			//users_get_modifyUserForm();
			//$('userManager_tab_edituser').fireEvent('click');
			//users_get_addUserForm();
		}
	}).send();	
}

function events_get_addEventForm( ) {
	tmp = Hash.toQueryString({type: "calendar", action: "get_event_add_form", form_name: 'eventManager_tab_addEvent_content_addEventForm', submit_function: 'events_addEventForm_submit()'});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$('eventManager_tab_addEvent_content_addEventForm_container').innerHTML = text;
			$('eventManager_tab_addEvent_content_addEventForm__allDay').addEvents({
				'change': function( ) {
					events_addEvent_toggleAllDay();
				}
			});
			$('eventManager_tab_addEvent_content_addEventForm__repeatType').addEvents({
				'change': function( ) {
					events_addEvent_changeRepeat();
				}
			});
			var add_repeat_until = new Fx.Slide('add_repeat_until');
			add_repeat_until.hide();
		}
	}).send();
}

function events_get_eventList( container_name, fld_name, do_async ) {
	tmp = Hash.toQueryString({type: "calendar", action: "getEventList", field_name: fld_name});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		async: do_async,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$(container_name).innerHTML = text;
		}
	}).send();
}

function events_load_highlights( ) {
	val = $('meetingdate').value;
	tmp = Hash.toQueryString({type: "calendar", action: "getHighlights", meeting_date: val});
	new Request.HTML({
		url: 'include/ajax.php',
		method: 'post',
		evalScripts: true,
		async: true,
		data: tmp,
		onSuccess: function( ignore1, ignore2, text ){
			$('highlights').innerHTML = text;
		}
	}).send();
}

if ( $('eventManager_editEvent') != undefined ) {
	$('eventManager_editEvent_allDay').addEvents({
		'change': function( ) {
					events_editEvent_toggleAllDay();
				}
	});
	
	$('eventManager_editEvent_repeatType').addEvents({
		'change': function( ) {
					events_editEvent_changeRepeat();
				}
	});
	
	var repeat_until = new Fx.Slide('repeat_until');
	repeat_until.hide();
}

if ( $('eventManager_tab_editEvent_content_eventList_container') != undefined ) {
	events_get_eventList('eventManager_tab_editEvent_content_eventList_container','eventManager_tab_editEvent_content_eventList',false);
	$('eventManager_tab_editEvent_content_eventList').addEvents({
		'change': function( ) {
					events_get_modifyEventForm();
					$('eventManager_tab_editEvent_content_changesSaved').setStyle('visibility','hidden');
				}
	});
}

if ( $('eventManager_tab_removeEvent_content_eventList_container') != undefined ) {
	events_get_eventList('eventManager_tab_removeEvent_content_eventList_container','eventManager_tab_removeEvent_content_eventList',false);
	$('eventManager_tab_removeEvent_content_eventList').addEvents({
		'change': function( ) {
					// DELETE EVENT
				}
	});
}

if ( $('eventManager_tab_addEvent_content_addEventForm_container') != undefined ) {
	events_get_addEventForm();
}

if ( $('highlight_container') != undefined ) {
	$('meetingdate').addEvents({
		'change': function( ) {
					events_load_highlights();
				}
	});
	events_load_highlights();
}function switch_tab( tab_name ) {
	// Hide all of the content
	$$('#tab_content_container div.tab_content').each(
		function ( item,index ) {
			item.setStyle('display','none');
		}						 
	);
	
	// Show the content for the current tab
	$(tab_name + '_content').setStyle('display','block');
	
	// Hide all of the titles
	$$('#tab_title_container span.tab_title').each(
		function ( item,index ) {
			item.setStyle('display','none');
		}						 
	);
	
	// Show the title for the current tab
	$(tab_name + '_title').setStyle('display','inline');

	
	// Set the tab styles
	$$('#tabs li').each(
		function ( item, index ) {
			if ( item.id == tab_name ) {
				item.className = 'selected';
			} else {
				item.className = '';
			}
		}
	);
}

if ( $('tab_container') != undefined ) {
	$$('#tabs li').each(
		function ( item, index ) {
			$(item.id).addEvents({
				'click': function( ) {
							switch_tab(item.id);
						}
			});
		}
	);
}
