/* GLOBALS */
var effects=Array();
var response_count=0;
var logged_in;
var loggedout_sessions;
var attempted_user_action;
var error_set = true;


/*
BEGIN init (Loaded at page start)
*/
window.addEvent('domready', function(){	
	session_scroll=new Fx.Scroll('sessionsListContainer');
	document.getElementById('search_field').value='';
	document.getElementById('search_string').value='';	
	var lock_var=0;
	refreshPage();
	image1 = new Image();
	image1.src = appURL+'/img/btn_addToSchedule.gif';
	var form = document.getElementById('session_search');
	form.onkeydown = function(e) {
		e = e || window.event;
		if (e.keyCode == 13) {
			document.getElementById('search_string').value=document.getElementById('search_field').value;			
			getSessions();
			return false;
		}
	};
}
);
/* END init */
/**********************************/
/* BEGIN database, AJAX functions */
/**********************************/

function eventChecked(event_id,is_checked) {
	change_via_text_link=false;
	event_checkbox=$('event_checkbox_'+event_id);
	event_status=$('event_status_'+event_id);

	if (event_checkbox.checked!=is_checked) {
		event_checkbox.checked=is_checked;
		change_via_text_link=true;
	}

	track_checkboxes=new Array;
	$$('input.track_checkbox').each (function(track) {if (track.getProperty('event_id')==event_id) { track_checkboxes.push(track)}});
	track_count=track_checkboxes.length;
	if (is_checked) {
		showTracks(event_id,'on');
		track_checkboxes.each(function(track) {track.checked=true;});
		event_status.innerHTML='('+track_count+'/'+track_count+') selected';
	}
	else {
		track_checkboxes.each(function(track) {track.checked=false;});
		event_status.innerHTML='(0/'+track_count+') selected';
		if (!change_via_text_link) {
			showTracks(event_id,'off');
		}
	}
}

Array.prototype.in_array = function (value) {
	for (var i=0; i < this.length; i++) {
		if (this[i] == value) {
			return true;
		}
	}
	return false;
}

function trackChecked(event_id) {
	track_count=0;
	checked_track_count=0;
	$$('input.track_checkbox').each (function(track) {if (track.getProperty('event_id')==event_id) { if (track.checked) checked_track_count++; track_count++;}});
	event_checkbox=$('event_checkbox_'+event_id);
	if (checked_track_count==0) {
		event_checkbox.checked=false;
	}
	else {
		event_checkbox.checked=true;
	}
	$('event_status_'+event_id).innerHTML='('+checked_track_count+'/'+track_count+') selected';
}

//Retrieves the session list data to display to the user.  
//Includes: session_list case from (Schedule Builder Root)/include/data_interface.php
function getSessions() {
	var my_id=++response_count;
	$('sessionsListContainer').innerHTML='<br /><center><img src="'+appURL+'/img/load.gif" alt="Loading"/></center>';
	tracks_array=new Array();
	$$('input.track_checkbox').each (function(track) {if (track.checked) {tracks_array.push(track.value);} });
	formats_array=new Array();
	$$('input.format_checkbox').each (function(format) {if (format.checked) formats_array.push(format.value); });
	var json_send=Json.toString({formats:formats_array,tracks:tracks_array});
	new Json.Remote(appURL+'/include/data_interface.php?action=session_list', {
		method:'post',
		onComplete: function(data) {
			if (my_id==response_count) {
				$('sessionsListContainer').innerHTML=data.session_list;
				$('sessionsInfoContainer').innerHTML=data.session_info;
				appendDebugData(data.debug_data);

			}
		}
	}).send({formats:formats_array,tracks:tracks_array,'search_string':document.getElementById('search_string').value,'search_by':document.getElementById('search_by').value,'pass_id':document.getElementById('for_pass').value,'order_by':document.getElementById('sort_by').value,'filter_by_day':document.getElementById('filter_by_day').value,'filter_by_time':document.getElementById('filter_by_time').value});
}

function appendDebugData(debug_data) {
	$('debug').innerHTML=$('debug').innerHTML+debug_data;
}

function refreshPage() {
	$$('input.eventCheckbox').each(function(el) {trackChecked(el.getProperty('parent_id')); });	
	getSessions();
	updateUserVoteDisplay();
	addCookieToForm();
}

function toggleDiv(div_id,show) {
	if (show) {
		$(div_id).setStyle('display','inline');
	}
	else {
		$(div_id).setStyle('display','none');
	}
}

//Looks to see if the user is logged in or not.  If not, then it will log the user in.
//Includes: login_user case from (Schedule Builder Root)/include/data_interface.php
function loginUser() {
	var login_data=new Hash();
	var session_cookie;
	$$('input.login_input').each(function(el) { if (el != undefined) login_data.set(el.name,el.value);});
	document.getElementById('login').value="Please Wait...";
	document.getElementById('login').disabled=true;
	
	//Stores and loads a Hash as a Cookie using JSON format and assigns it to a new variable.
	var session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	
	//Grabs the session data from the cookie and passes it into a new variable.
	var sessions = session_cookie.get('sessions');
	
	//Executes the login_user case in the data_interface.php file and appends the cookie data to the URL.
	new Json.Remote(appURL+'/include/data_interface.php?action=login_user&cookie='+ sessions, {
		method:'post',
		onComplete: function(data) {	
			appendDebugData(data.debug_data);
			if (data.valid_login) {	
				document.getElementById('loginStatus').innerHTML=data.results;
				$('loginContainer').setStyle('display','none');
				$('logoutContainer').setStyle('display','inline');
				session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
				//session_cookie.extend({'user_id':data.user_id,'user_key':data.user_key,'user_email':data.user_email,'sessions':data.sessions, 'user_name':data.user_name, 'pass_type':data.pass_type});
				session_cookie.extend({'user_id':data.user_id,'user_key':data.user_key,'user_email':data.user_email,'sessions':data.sessions, 'user_name':data.user_name});
				addCookieToForm();
				TB_remove();
				refreshPage();
				if (attempted_user_action != undefined) {
					eval(attempted_user_action);
					}
			}
			else {
				if (error_set == true)
				{
					document.getElementById('TB_ajaxContent').innerHTML= document.getElementById('TB_ajaxContent').innerHTML+data.results;	
					error_set = false;
				}
			}
		}

	}).send(login_data.obj);
}

function logoutUser() {
	Cookie.remove('scheduler_session');
}

//Runs when the E-mail button is clicked.  This will generate the popup box
//that the user will use to enter in his/her information.
function showEmailMySchedule() {
	var session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	user_id=session_cookie.get('user_id');
	//Checks to see if the user is logged in.
	if (user_id) {
		//Verifies that the user wants to save their schedule before continuing.
		var answer = confirm ("Attention: By clicking ok, you will save your current schedule.  If do not wish to save your schedule, please click Cancel to continue.");
		if (answer) {
			var session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
			user_id=session_cookie.get('user_id');
			if (user_id) {
				var now = new Date();
				var hour        = now.getHours();
				var minute      = now.getMinutes();
				var seconds      = now.getSeconds();
				if (minute < 10) { minute = '0' + minute}
				if (seconds < 10) { seconds = '0' + seconds}
				var ap = "AM";
				if (hour   > 11) { ap = "PM";        }
				if (hour   > 12) { hour = hour - 12; }
				if (hour   == 0) { hour = 12;        }
				var savedTime = ('Last Saved: ' + hour + ':' + minute + ':' + seconds + ' ' + ap);
				
				//Displays saved results in the appropriate visual container.
				document.getElementById('SaveResultsContainer').innerHTML=savedTime;
			}
		}
		if (($('popoverContainer').getStyle('display'))=='block') {
			$('popoverContainer').setStyle('display','none');
		}
		else {
			$('popoverContainer').setStyle('display','block');
		}
		var now = new Date();
	}
	else {
		showLogin();
	}
}

//Sends out the users schedule from the information provided in the popup Email form (function above).
//Includes: email_schedule case from (Schedule Builder Root)/include/data_interface.php
function emailSchedule(user_id) {
	var session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	user_id=session_cookie.get('user_id');
	mail_data = new Hash();
	$$('input.email_schedule_input').each(function(el) { if (el != undefined) mail_data.set(el.name,el.value);});
	document.getElementById('my_schedule_send').value="Sending...";
	document.getElementById('my_schedule_send').disabled=true;
	var session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	if (user_id) {
		new Json.Remote(appURL+'/include/data_interface.php?action=email_schedule', {
			method:'post',
			onComplete: function(data) {
				appendDebugData(data.debug_data);
				$('email_results').innerHTML=data.results;
				document.getElementById('my_schedule_send').value="Send";
				document.getElementById('my_schedule_send').disabled=false;
			}
		}).send({'mail_data':mail_data.obj,'user_id':user_id,'user_key':session_cookie.get('user_key')});
	}
	else {
		showLogin();
	}
}

//Gets the users saved session list and displays it in the My Schedule container.
//Includes: get_user_sessions case from (Schedule Builder Root)/include/data_interface.php
function getUserSessions(user_id) {
	var session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	var user_key= session_cookie.get('user_key');
	var user_session_data=new Hash();
	new Json.Remote(appURL+'/include/data_interface.php?action=get_user_sessions', {
		method:'post',
		onComplete: function(data) {
			appendDebugData(data.debug_data);
			if ($data.permission_denied) {
				return false;
			}
			else {
				user_session_data.set('sessions',data.user_sessions);
				user_session_data.set('user_name',data.user_name);
				return user_session_data;
			}
		}
	}).send({'user_id':user_id,'user_key':user_key});
}

//When the "Print Now" button is clicked, the user is directed to a new page to print out his/her user schedule.
//Includes: print_schedule case from (Schedule Builder Root)/include/data_interface.php
function printSchedule(new_window,user_id) {
	var session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	user_id=session_cookie.get('user_id');
	if (user_id) {
		var answer = confirm ("Attention: By clicking ok, you will save your current schedule.  If do not wish to save your schedule, please click Cancel to continue.");
			if (answer) {
			var session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
			user_id=session_cookie.get('user_id');
			//Checks to see if the user is logged in and has a valid user ID.
			if (user_id) {
			var now = new Date();
			var hour        = now.getHours();
			var minute      = now.getMinutes();
			var seconds      = now.getSeconds();
			if (minute < 10) { minute = '0' + minute}
			if (seconds < 10) { seconds = '0' + seconds}
			var ap = "AM";
			if (hour   > 11) { ap = "PM";        }
			if (hour   > 12) { hour = hour - 12; }
			if (hour   == 0) { hour = 12;        }
			var savedTime = ('Last Saved: ' + hour + ':' + minute + ':' + seconds + ' ' + ap);
			document.getElementById('SaveResultsContainer').innerHTML=savedTime;
			}
		}
		print_sessions=session_cookie.get('sessions');
		user_name=session_cookie.get('user_name');
		new Json.Remote(
		appURL+'/include/data_interface.php?action=print_schedule', {
			method: 'post',
			onComplete: function(data) {
				appendDebugData(data.debug_data);
				print_page=new_window.document;
				print_page.write(data.user_schedule_print);
				print_page.close(); }
		}
		).send({'user_id':user_id,'user_key':session_cookie.get('user_key')});
	}
	else {
		print_sessions=session_cookie.get('sessions');
		user_name=session_cookie.get('user_name');
		new Json.Remote(
		appURL+'/include/data_interface.php?action=print_schedule', {
			method: 'post',
			onComplete: function(data) {
				appendDebugData(data.debug_data);
				print_page=new_window.document;
				print_page.write(data.user_schedule_print);
				print_page.close(); }
		}
	
		).send({'user_name':user_name,'sessions':print_sessions});
	}
	
}

//When the "Add to my Profile" button is clicked, the user is first ask to save his/her sessions and then is prompted to add his/her profile.
//Includes: export_schedule case from (Schedule Builder Root)/include/data_interface.php
function exportHTML(user_id) {
	var session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	user_id=session_cookie.get('user_id');
	if (user_id) {
		var answer = confirm ("Attention: By clicking ok, the current schedule you have created will be saved to your profile and override any existing items you have placed in MY CONFERENCE SCHEDULE on your profile. If you have existing content in that field that you would like to save, please select cancel. You may elect to save a copy and add it back in to your PROFILE SETTINGS later.");
		//If ok, then the user's session schedule will be added to their profile in phpfox.
		if (answer) {
			new Json.Remote(
				appURL+'/include/data_interface.php?action=export_schedule', {
					method: 'post',
					onComplete: function(data) {
						appendDebugData(data.debug_data);
						if (data.results) {
							var now = new Date();
							var hour        = now.getHours();
							var minute      = now.getMinutes();
							var seconds      = now.getSeconds();
							if (minute < 10) { minute = '0' + minute}
							if (seconds < 10) { seconds = '0' + seconds}
							var ap = "AM";
							if (hour   > 11) { ap = "PM";        }
							if (hour   > 12) { hour = hour - 12; }
							if (hour   == 0) { hour = 12;        }
							var savedTime = ('Profile Last Updated: ' + hour + ':' + minute + ':' + seconds + ' ' + ap);
							document.getElementById('SaveResultsContainer').innerHTML=savedTime;
						}
						else {
							document.getElementById('SaveResultsContainer').innerHTML='Updated Failed';
						}
					}
				}
			).send({'user_id':user_id,'user_key':session_cookie.get('user_key')});
		}
		else {
			document.getElementById('SaveResultsContainer').innerHTML="Profile not updated";
		}	
	}
	else {
		showLogin();
	}
}

function trackSaveButton() {
	new Json.Remote(appURL+'/include/data_interface.php?action=track_save_button', {
		method:'post',
		onComplete: function(data) {;}
	}).send({'data':'hello'});
}


//When the "Save" button is clicked, the user is prompted to save their schedule.
//Includes: No includes.
function saveMySchedule(track) {
	var session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	user_id=session_cookie.get('user_id');
	//Checks to see if the user is logged in.
	if (user_id) {
		var now = new Date();
		var hour        = now.getHours();
		var minute      = now.getMinutes();
		var seconds      = now.getSeconds();
		if (minute < 10) { minute = '0' + minute}
		if (seconds < 10) { seconds = '0' + seconds}
		var ap = "AM";
		if (hour   > 11) { ap = "PM";        }
		if (hour   > 12) { hour = hour - 12; }
		if (hour   == 0) { hour = 12;        }
		var savedTime = ('Last Saved: ' + hour + ':' + minute + ':' + seconds + ' ' + ap);
		document.getElementById('SaveResultsContainer').innerHTML=savedTime;
		if(track) {
			trackSaveButton();
		}	
	}
	else {
		showLogin();
	}
}

//If the user clicks on a 
function getSessionDescription(session_id) {
	var loadingColor = new Fx.Style('session_'+session_id+'_div', 'background-color', {duration: 500});
	var doneColor = new Fx.Style('session_'+session_id+'_div', 'background-color', {duration: 500});
	if ($('session_description_'+session_id)) {
		$('session_description_'+session_id).remove();
	}
	else {
		var elm = new Element('div');
		elm.setProperties({id:'session_description_'+session_id});
		elm.injectInside('session_'+session_id+'_div');
		new Json.Remote(appURL+'/include/data_interface.php?action=session_description',
		{
			method:'post',
			onComplete: function(data) {
				$('session_description_'+session_id).innerHTML=data.session_description;
				if ($('saved_'+session_id+'_session')) {
					$('session_'+session_id+'_cart_action').setProperties({
					'class':'cart_delete',
					src:appURL+'/img/delete.gif',
					title:"Remove From My Schedule"});
				}
				appendDebugData(data.debug_data);
			}
		}).send({'session_id':session_id});
	}
}

function colorCycle(element) {
	var myColorFx = new Fx.Color(element, 'background-color', {duration: 10000});
	myColorFx.start('#54A1CF', '#000000');
}
function shareMyProfile(share) {
	if (share) {
		$('userRegistrationExtendedContainer').setStyle('display','block');
	}
	else {
		$('userRegistrationExtendedContainer').setStyle('display','none');
	}
}

function updateUserVoteDisplay() {
	var saved_sessions_array=new Array();
	session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	if (session_cookie.get('user_key') != undefined) {
		new Json.Remote(appURL+'/include/data_interface.php?action=update_user_vote_list',
		{	method: 'post',
		onComplete: function(data) {
			$('userVoteListContainer').innerHTML=data.user_vote_list;
			appendDebugData(data.debug_data);
		}}).send({'user_id':session_cookie.get('user_id'),'user_key':session_cookie.get('user_key')});
	}
}

function addCookieToForm() {
	var sessions; 
	session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	if (session_cookie.get('user_key')) {
		//$('loginStatus').innerHTML="Logged in as "+session_cookie.get('user_name')+"<br />Pass Type:"+session_cookie.get('pass_type');
		$('loginStatus').innerHTML="Logged in as "+session_cookie.get('user_name');
		$('loginContainer').setStyle('display','none');
		$('logoutContainer').setStyle('display','inline');
		new Json.Remote(appURL+'/include/data_interface.php?action=get_user_sessions',{ method: 'post',
		onComplete:function(data) {
			if (data.error != undefined) {
				alert(data.error);
			}
			if (data.sessions != undefined) {
				data.sessions.each( function(id) {
					var saved_session_input= new Element('input',{'type':'hidden','value':id, 'name':'saved_session_'+id, 'id':'saved_session_'+id,'class':'saved_user_session'});
					$('user_sessions').appendChild(saved_session_input);
				});
			}
			appendDebugData(data.debug_data);
			updateUserScheduleDisplay();}
		}
		).send({'user_id':session_cookie.get('user_id'),'user_key':session_cookie.get('user_key')});
	}
	else if (session_cookie.get('sessions') != undefined) {
		sessions=session_cookie.get('sessions');
		if (sessions != undefined) {
			sessions.each( function(id) {
				var saved_session_input= new Element('input',{'type':'hidden','value':id, 'name':'saved_session_'+id, 'id':'saved_session_'+id,'class':'saved_user_session'});
				$('user_sessions').appendChild(saved_session_input);
			}
			);
		}
		updateUserScheduleDisplay();
	}
}

function clearSessions() {
	session_cookie= new Hash.Cookie('scheduler_session',{duration:false});
	session_cookie.set('sessions',new Array());
	var answer = confirm ("Attention: By clicking ok, the current schedule will be erased.  Please note that when erased, the sessions cannot be restored.");
	if (answer) 
	{
		$$('input.saved_user_session').each(function (el) {el.remove()});
		$$('img.cart_delete').each(function(el) {
		var session_id = el.id.substring(8, el.id.length-12);
		$('session_'+session_id+'_add_text').setHTML(' Add this session to your schedule.');
		$('session_'+session_id+'_add_text').setStyle('color', '#79B370');
		el.setProperties({'class':'',src:appURL+'/img/btn_addToSchedule.gif',title:"Add To My Schedule"});});

		if (session_cookie.get('user_key') != undefined) 
		{
			new Json.Remote(appURL+'/include/data_interface.php?action=clear_sessions',
			{ 
				method: 'post',
				onComplete:function(data) 
				{
					if (data.error != undefined) 
					{
						alert(data.error);
					}
					appendDebugData(data.debug_data);
				}
			}
			).send({'user_id':session_cookie.get('user_id'),'user_key':session_cookie.get('user_key')});
		}
		else
		{
			session_cookie.set("");
		}
		
	}
	updateUserScheduleDisplay();
}

function addVote(session_id,rating) {
	$('userVoteListContainer').setProperty('class','transON');
	session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
		if (session_cookie.get('user_key') != undefined) {
			if ($('session_rating_'+session_id)) {
				$('session_rating_'+session_id).setStyles({'width':rating*20+'%','background-position':'left bottom'});
			}
			new Json.Remote(appURL+'/include/data_interface.php?action=add_vote',{ method: 'post',
			onComplete:function(data) {
				if (data.error != undefined) {
					alert(data.error);
				}
				appendDebugData(data.debug_data);
				updateUserVoteDisplay();
			$('userVoteListContainer').setProperty('class','transOFF');}
				
			}
			).send({'user_id':session_cookie.get('user_id'),'user_key':session_cookie.get('user_key'),'session_id':session_id,'rating':rating});
		}
		else {
			showLogin('addVote('+session_id+','+rating+');');
		}
}

function showLogin(action) {
	session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	if (session_cookie.get('user_key') == undefined) {
		TB_show('You must login in order to build a schedule.', appURL+'#TB_inline?inlineId=userLogin&height=273&width=358', null);
		if (action != undefined) {
			attempted_user_action=action;
		}
	}
}

function removeVote(session_id) {
	session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
		if (session_cookie.get('user_key') != undefined) {
			if ($('session_rating_'+session_id)) {
				$('session_rating_'+session_id).setStyles({'width':'0%','background-position':'left top'});
			}
			new Json.Remote(appURL+'/include/data_interface.php?action=remove_vote',{ method: 'post',
			onComplete:function(data) {
				if (data.error != undefined) {
					alert(data.error);
				}
				appendDebugData(data.debug_data);
				updateUserVoteDisplay();}
			}
			).send({'user_id':session_cookie.get('user_id'),'user_key':session_cookie.get('user_key'),'session_id':session_id});
		}
}

function clearVotes() {
	session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
		if (session_cookie.get('user_key') != undefined) {
			new Json.Remote(appURL+'/include/data_interface.php?action=clear_votes',{ method: 'post',
			onComplete:function(data) {
				if (data.error != undefined) {
					alert(data.error);
				}
				$$('li.current-rating').each(function (el) {
				el.setStyles({'width':'0%','background-position':'left top'});
				});
				
				appendDebugData(data.debug_data);
				updateUserVoteDisplay();}
			}
			).send({'user_id':session_cookie.get('user_id'),'user_key':session_cookie.get('user_key')});
		}
}

function addToMySchedule(id)	{
	
		if (!$('saved_session_'+id)) {
			var saved_session_input= new Element('input',{'type':'hidden','value':id, 'name':'saved_session_'+id, 'id':'saved_session_'+id,'class':'saved_user_session'});
			$('user_sessions').appendChild(saved_session_input);
			if ($('session_'+id+'_cart_action')) {
				$('session_'+id+'_cart_action').setProperties({'class':'cart_delete',src:appURL+'/img/btn_removeFromSchedule.gif',title:"Remove From My Schedule"});
				//$('session_'+id+'_add_text').setHTML(' Remove this session from your schedule.');
				//$('session_'+id+'_add_text').setStyle('color', 'red');
			}
			session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
			if (session_cookie.get('user_key') != undefined) {
				new Json.Remote(appURL+'/include/data_interface.php?action=add_session',{ method: 'post',
				onComplete:function(data) {
					if (data.error != undefined) {
						alert(data.error);
					}
					appendDebugData(data.debug_data);
					updateUserScheduleDisplay();
					saveMySchedule();
					}
				}
				).send({'user_id':session_cookie.get('user_id'),'user_key':session_cookie.get('user_key'),'session_id':id});
			}
			else {
				updateUserScheduleDisplay();
			}
		}
		else {
			$('saved_session_'+id).remove();
			if ($('session_'+id+'_cart_action')) {
				$('session_'+id+'_cart_action').setProperties({'class':'',src:appURL+'/img/btn_addToSchedule.gif',title:"Add To My Schedule"});
				//$('session_'+id+'_add_text').setHTML(' Add this session to your schedule.');
				//$('session_'+id+'_add_text').setStyle('color', '#79B370');
			}
			session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
			if (session_cookie.get('user_key') != undefined) {
				new Json.Remote(appURL+'/include/data_interface.php?action=remove_session',{ method: 'post',
				onComplete:function(data) {
					if (data.error != undefined) {
						alert(data.error);
					}
					appendDebugData(data.debug_data);
					updateUserScheduleDisplay();
					saveMySchedule();
				}
	
				}
	
				).send({'user_id':session_cookie.get('user_id'),'user_key':session_cookie.get('user_key'),'session_id':id});
			}
			else {
				updateUserScheduleDisplay();
			}
		}
}

function updateUserScheduleDisplay() {
	var saved_sessions_array=new Array();
	session_cookie=new Hash.Cookie('scheduler_session',{duration:false});
	if (session_cookie.get('user_key') != undefined) {
		new Json.Remote(appURL+'/include/data_interface.php?action=update_user_schedule_list',
		{	method: 'post',
		onComplete: function(data) {
			$('userScheduleListContainer').innerHTML=data.user_schedule_list;
			$('userSchedulePassesContainer').innerHTML=data.user_schedule_pass_list;
			appendDebugData(data.debug_data);
		}}).send({'user_id':session_cookie.get('user_id'),'user_key':session_cookie.get('user_key')});
	}
	else {
		$$('input.saved_user_session').each (function(saved_session) {saved_sessions_array.push(saved_session.value); });
		session_cookie.extend({'sessions':saved_sessions_array});
		new Json.Remote(appURL+'/include/data_interface.php?action=update_user_schedule_list',
		{	method: 'post',
		onComplete: function(data) {
			$('userScheduleListContainer').innerHTML=data.user_schedule_list;
			$('userSchedulePassesContainer').innerHTML=data.user_schedule_pass_list;
			appendDebugData(data.debug_data);
		}}).send({'sessions':saved_sessions_array});
	}
}

function getUserProfileForm() {
	var user_profile_form= new Hash()
	$$('input.user_registration_input').each(function(el) {
		if (el != undefined) {
			if (el.type=='checkbox') {
				if (el.checked) user_profile_form.set(el.name,el.value);
			}
			else if (el.type=='radio') {
				if  (el.selected) user_profile_form.set(el.name,el.value);
			}
			else {
				if  (el.value!='') user_profile_form.set(el.name,el.value);
			}
		}
	});
	return user_profile_form;
}

function saveUser()	{
	var user_data = getUserProfileForm();
	var session_cookie= new Hash.Cookie('scheduler_session',{duration:false});
	if (session_cookie.get('user_key')) {
		new Json.Remote(
		appURL+'/include/data_interface.php?action=create_user',
		{
			method:'post',
			onComplete: function(data) {
				$('userRegistrationResults').innerHTML=data.result;
				$("TB_ajaxContent").innerHTML=$('userLogin').innerHTML; 
				appendDebugData(data.debug_data);
			}
		}).send({'user_data':user_data.obj,'user_id':session_cookie.get('user_id'),'user_key':session_cookie.get('user_key')});
	}
	else {
		var sessions= session_cookie.get('sessions');
		new Json.Remote(
		appURL+'/include/data_interface.php?action=create_user',
		{
			method:'post',
			onComplete: function(data) {
				if (data.user_created) {
					$("TB_ajaxContent").innerHTML=data.result; 
				}
				else {
					$('userRegistrationResults').innerHTML=data.result;
					$("TB_ajaxContent").innerHTML=$('userLogin').innerHTML; 
				}
				appendDebugData(data.debug_data);
			}
		}).send({'user_data':user_data.obj,'sessions':sessions,'user_id':'','user_key':''});
	}
}

/*************************/
/* BEGIN Util FUNCTIONS  */
/*************************/

function checkSession(id) {
	$('session_'+id).checked=true;
}

function showTracks(id,show_track) {
	if ($('tracks_'+id)) {

		if (!effects['tracks_slider_'+id]) {
			child_div=document.getElementById('tracks_'+id);
			if (document.defaultView && document.defaultView.getComputedStyle(child_div, null)==null) {
				$('tracks_'+id).setStyle('display','block');
			}
			effects['tracks_slider_'+id]= new Fx.Slide('tracks_'+id,{duration:500});
			effects['tracks_slider_'+id].hide();
			if ($('tracks_'+id).getStyle('display')=='none') $('tracks_'+id).setStyle('display','block');
		}
		if (show_track=="on") {

			effects['tracks_slider_'+id].slideIn();
		}
		else if (show_track=="off") {
			effects['tracks_slider_'+id].slideOut();
		}
		else {
			effects['tracks_slider_'+id].toggle();
		}
	}
}


function validateEmailSchedule() {
	var form=$('email_schedule');
	if (form.my_schedule_email.value==undefined || form.my_schedule_email.value=='') {
		$('email_results').innerHTML="Please enter an email address.";
		return;
	}
}

/*
BEGIN label overlay js
*/
function initOverLabels () {
	if (!document.getElementById) return;

	var labels, id, field;

	// Set focus and blur handlers to hide and show
	// LABELs with 'overlabel' class names.
	labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++) {

		if (labels[i].className == 'overlabel') {

			// Skip labels that do not have a named association
			// with another field.
			id = labels[i].htmlFor || labels[i].getAttribute('for');
			if (!id || !(field = document.getElementById(id))) {
				continue;
			}

			// Change the applied class to hover the label
			// over the form field.
			labels[i].className = 'overlabel-apply';

			// Hide any fields having an initial value.
			if (field.value !== '') {
				hideLabel(field.getAttribute('id'), true);
			}

			// Set handlers to show and hide labels.
			field.onfocus = function () {
				hideLabel(this.getAttribute('id'), true);
			};
			field.onblur = function () {
				if (this.value === '') {
					hideLabel(this.getAttribute('id'), false);
				}
			};

			// Handle clicks to LABEL elements (for Safari).
			labels[i].onclick = function () {
				var id, field;
				id = this.getAttribute('for');
				if (id && (field = document.getElementById(id))) {
					field.focus();
				}
			};

		}
	}
};
function hideLabel (field_id, hide) {
	var field_for;
	var labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++) {
		field_for = labels[i].htmlFor || labels[i].getAttribute('for');
		if (field_for == field_id) {
			labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
			return true;
		}
	}
}
/*
End label overlay js
*/
