// JavaScript Document
var djURL = "includes/content/dreamjournal/dreamjournallist_server.php";
var rowspan = ""; 
var processingbar = '<div id="dj-processing" align="center" style="vertical-align:middle;"><strong>Gathering Dream List  </strong><img id="processingbar" src="images/processing_bar.gif" /></div>';
var dreamlist_max = 25; 
var limit_cur = 0;
var limit_max = dreamlist_max;
var sortby = "";
var sortorder = "ASC";
var thesearch = "";
var dreamid = "";
var dream_title = "";
var interprequest = false;
var sharename = "";
var sharescope = "";
var shareactive = "";
var newshare = false;
$(document).ready(function()
	{
		IntPageLoad();
		$('#nd-dreamtext').each(function()
			{ 
				var length = $(this).val().length;  
				$(this).parent().find('#charcount').html(length);
				$(this).keyup(function()
					{  
						var new_length = $(this).val().length;  
						//$(this).parent().find('#charcount').html(new_length);  
						$('#charcount').empty();
						$('#charcount').append(new_length);
					});  
			});  
		$('#dialog-newdream').dialog(
			{
				
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:620,
				modal:true,
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Save & Close': function()
						{
							dream_title = $('#nd-dream-title').val();
							var coloration = $('#nd-coloration').val();
							var perspective = $('#nd-perspective').val();
							var anonymous = $('#nd-anonymous').val();
							var dreamtext = $('#nd-dreamtext').val();
							validate = true;
							
							
						validate = validate && FillCheck(dream_title,"You must supply a dream title",$('#nd-dream-title'),$('#nd-error'));		
							validate = validate && FillCheck(dreamtext,"You must supply a dream story",$('#nd-dreamtext'),$('#nd-error'));	
							if (validate) 
							{
								$('#nd-dreamtext').removeClass('ui-state-error');
								$('#nd-dream-title').removeClass('ui-state-error');
								$('#nd-content').hide();
								$('#nd-process').show();
					newDream('<? echo $member_id; ?>',coloration,dream_title,perspective,anonymous,dreamtext)
					
				//	$(this).dialog('close');
							}
							
							
				
						},
						
					'Cancel': function()
						{
						$(this).dialog('close');
		
						}
						
				},
				close:function()
					{
						$('#nd-dreamtext').removeClass('ui-state-error');
						$('#nd-dream-title').removeClass('ui-state-error');	
					}
			});
		$('#dialog-editdream').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:620,
				modal:true,
				position:'top',
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Publicly Share': function()
						{
							CanShareDream();
						},
					'Save & Close': function()
						{
							dream_title = $('#nd-dream-title').val();
							var coloration = $('#nd-coloration').val();
							var perspective = $('#nd-perspective').val();
							var anonymous = $('#nd-anonymous').val();
							var dreamtext = $('#nd-dreamtext').val();
							validate = true;
							
							validate = validate && FillCheck(dream_title,"You must supply a dream title",$('#nd-dream-title'),$('#nd-error'));		
							validate = validate && FillCheck(dreamtext,"You must supply a dream story",$('#nd-dreamtext'),$('#nd-error'));	
							
							if (validate) 
								{
									$('#nd-dreamtext').removeClass('ui-state-error');
									$('#nd-dream-title').removeClass('ui-state-error');
									//$('#fieldset-dream').hide();
									//$('#ed-process-update').show();
									//updateTips("Saving Dream...",$('#ed-process-update h2'));
									
									updateDream("updatedream",dreamid,coloration,dream_title,perspective,anonymous,dreamtext);
								}
						},
					/*
						//TT 2009.07.23
							//once #dialog-confirm opens, textarea becomes disabled. 
							//Turn on once a fix is created
					'Save & Submit for Interpretation': function()
						{
							dream_title = $('#nd-dream-title').val();
							coloration = $('#nd-coloration').val();
							perspective = $('#nd-perspective').val();
							anonymous = $('#nd-anonymous').val();
							dreamtext = $('#nd-dreamtext').val();
							validate = true;
							
							validate = validate && FillCheck(dream_title,"You must supply a dream title",$('#nd-dream-title'),$('#nd-error'));		
							validate = validate && FillCheck(dreamtext,"You must supply a dream story",$('#nd-dreamtext'),$('#nd-error'));	
							
							if (validate) 
								{
									$('#nd-dreamtext').removeClass('ui-state-error');
									$('#nd-dream-title').removeClass('ui-state-error');
									//$('#fieldset-dream').hide();
									//$('#ed-process-update').show();
									updateTips("Saving Dream...",$("#ed-process-update h2"));
									updateDream("simpleupdate",dreamid,coloration,dream_title,perspective,anonymous,dreamtext);
								}
							
						},
					*/
					'Cancel': function()
						{
						$(this).dialog('close');
		
						}
				},
				close: function()
					{
						$('#nd-dreamtext').removeClass('ui-state-error');
						$('#nd-dream-title').removeClass('ui-state-error');
					}
			});
		$('#dialog-sharedream-confirm').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:500,
				modal:true,
				position:'top',
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Confirm': function()
						{
							ShareDreamPublicly();
						},

					'Cancel': function()
						{
						$(this).dialog('close');
		
						}
				},
				close: function()
					{
						$('#nd-dreamtext').removeClass('ui-state-error');
						$('#nd-dream-title').removeClass('ui-state-error');
					}
			});
		$('#dialog-sharedream').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:600,
				modal:true,
				position:'top',
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Submit': function()
						{
							sharename = $('#view-sd-displayname').val();
							sharescope = $('#view-sd-scope').val();
							if(newshare)
								{
									shareactive = $('#view-sd-active').val(); 
								}
							validate = true;
							
							validate = validate && FillCheck(sharename,"You must select a Display Name",$('#view-sd-displayname'),$('#tips-sharedream'));
						   validate = validate && FillCheck(sharescope,"You must select a Share Option",$('#view-sd-scope'),$('#tips-sharedream'));
						   if(newshare)
						   	{
						   		validate = validate && FillCheck(shareactive,"You must select a Share Status",$('#view-sd-active'),$('#tips-sharedream'));
							}
							if(validate)
								{
									if(shareactive == 1)
										{
											updateTips("Are you sure you wish to publicly share this dream? This will enable anyone to view your dream and its interpretation. This will also allow you to place your dream on any social network (Facebook, MySpace, Blog, etc) you wish.",$('#tips-sharedream-confirm'));
										}
									else
										{
											updateTips("Are you sure you wish to quit sharing this dream?",$('#tips-sharedream-confirm'));
										}
									$('#dialog-sharedream-confirm').dialog('open');
									$('#dialog-editdream').dialog('close');
								}
						},
					'Cancel': function()
						{
							$(this).dialog('close');
						}
				},
				close: function()
					{
						$('#nd-dreamtext').removeClass('ui-state-error');
						$('#nd-dream-title').removeClass('ui-state-error');
					}
			});
		$('#dialog-viewingmydream').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:620,
				modal:true,
				position:'top',
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Publicly Share': function()
						{
							CanShareDream();
						},
					'Close': function()
						{
						$(this).dialog('close');
		
						}
				},
				close: function()
					{
						$('#vi-labels').hide();
					}
			});
		$('#dialog-confirm').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:400,
				modal:true,
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Submit': function()
						{
							CheckDreamSubmittals(dreamid);
						},
						
					'Cancel': function()
						{
							$(this).dialog('close');
						}
				},
				close:function()
					{
						updateTips("Are you sure you wish to submit this dream for interpretation?",$("#tips-confirm"));
					}
			});
		$('#dialog-djalert').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:400,
				modal:true,
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					
					'Close': function()
						{
							$(this).dialog('close');
						}
				},
				close:function()
					{
						
					}
			});
		$('#dialog-viewdream').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:600,
				modal:true,
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Close': function()
						{
						$(this).dialog('close');
						}
				}
			});
		$('#dialog-dj-error').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:400,
				modal:true,
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Close': function()
						{
						$(this).dialog('close');
						}
				}
			});
		$('#dialog-comment-reply').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:600,
				modal:true,
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Submit': function()
						{
							var comment_newreply = $('#c-comment-replytext').val();
							validate = true;
							
							validate = validate && FillCheck(comment_newreply,"Please enter a response before submitting a reply",$('#c-comment-replytext'),$('#tips-comment-reply'));

							if(validate)
								{
									SendCommentReply(dreamid,comment_newreply);	
								}
						},
					'Close': function()
						{
						$(this).dialog('close');
						}
				}
			});
		$('#dialog-removeinterp').dialog( 
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:400,
				modal:true,
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Confirm': function()
						{
							RemoveInterp(dreamid)
						},
					'Cancel': function()
						{
						$(this).dialog('close');
						}
				}
			});
		$('#dialog-dj-success').dialog(
			{
				bgiframe:true,
				autoOpen:false,
				resizable:false,
				width:400,
				modal:true,
				closeOnEscape:false,
				overlay:
				{
					backgroundColor: '#000',
					opacity: 0.7
				},
				buttons:
				{
					'Close': function()
						{
						$(this).dialog('close');
						}
				}
			});
		$('#dj-a-alldreams').click(function()
			{ 
				thesearch = "";
				if(dreamer_loggedin == "yes")
					{
						$('#dj-thead-dreamslist').hide();
						$('#dj-thead-interpreteddreamslist').hide();
						$('#dj-thead-commentdreamlist').hide();
						$('#dj-tbody-dreamlist').empty();
						$('#dj-text-listamount').empty();
						$('#dj-text-comment').empty();
						sortby = "";
						sortorder = "ASC";
						limit_cur = 0;
						limit_max = dreamlist_max;
						GetDreamJournalList("alldreams");
					}
				else
					{
						UpdateDJSubmittalsText("notloggedin");	
					}
			})
		.hover(
			function(){ 
				$(this).addClass("int-a-hover");
			},
			function(){ 
				$(this).removeClass("int-a-hover"); 
			}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('#dj-a-unsubmitted').click(function()
			{
				thesearch = "";
				if(dreamer_loggedin == "yes")
					{	
						$('#dj-thead-dreamslist').hide();
						$('#dj-thead-interpreteddreamslist').hide();
						$('#dj-thead-commentdreamlist').hide();
						$('#dj-tbody-dreamlist').empty();
						$('#dj-text-listamount').empty();
						$('#dj-text-comment').empty();
						sortby = "";
						sortorder = "ASC";
						limit_cur = 0;
						limit_max = dreamlist_max;
						GetDreamJournalList("unsubmitted");
				}
				else
					{
						UpdateDJSubmittalsText("notloggedin");	
					}
			})
		.hover(
			function(){ 
				$(this).addClass("int-a-hover");
			},
			function(){ 
				$(this).removeClass("int-a-hover"); 
			}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('#dj-a-submitted').click(function()
			{
				thesearch = "";
				if(dreamer_loggedin == "yes")
					{	
						$('#dj-thead-dreamslist').hide();
						$('#dj-thead-interpreteddreamslist').hide();
						$('#dj-thead-commentdreamlist').hide();
						$('#dj-tbody-dreamlist').empty();
						$('#dj-text-listamount').empty();
						$('#dj-text-comment').empty();
						sortby = "";
						sortorder = "ASC";
						limit_cur = 0;
						limit_max = dreamlist_max;
						GetDreamJournalList("submitted");
				} 
				else
					{
						UpdateDJSubmittalsText("notloggedin");	
					}
			})
		.hover(
			function(){ 
				$(this).addClass("int-a-hover");
			},
			function(){ 
				$(this).removeClass("int-a-hover"); 
			}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('#dj-a-interpreted').click(function()
			{
				thesearch = "";
				if(dreamer_loggedin == "yes")
					{	
						$('#dj-thead-dreamslist').hide();
						$('#dj-thead-interpreteddreamslist').hide();
						$('#dj-thead-commentdreamlist').hide();
						$('#dj-tbody-dreamlist').empty();
						$('#dj-text-listamount').empty();
						$('#dj-text-comment').empty();
						sortby = "";
						sortorder = "ASC";
						limit_cur = 0;
						limit_max = dreamlist_max;
						GetInterpretedDreamJournalList("interpreted");
				} 
				else
					{
						UpdateDJSubmittalsText("notloggedin");	
					}
			})
		.hover(
			function(){ 
				$(this).addClass("int-a-hover");
			},
			function(){ 
				$(this).removeClass("int-a-hover"); 
			}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('#dj-button-newdream').click(function()
			{
			})
		.hover(
				function(){ 
					$(this).addClass("ui-state-hover");
				},
				function(){ 
					$(this).removeClass("ui-state-hover"); 
				}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('#dj-button-search').click(function()
			{
				window.location.hash = "search";
				thesearch = $('#searchbox-field').val();
				if(dreamer_loggedin == "yes")
					{
						$('#dj-thead-dreamslist').hide();
						$('#dj-thead-interpreteddreamslist').hide();
						$('#dj-thead-commentdreamlist').hide();
						$('#dj-tbody-dreamlist').empty();
						$('#dj-text-listamount').empty();
						$('#dj-text-comment').empty();
						sortby = "";
						sortorder = "ASC";
						limit_cur = 0;
						limit_max = dreamlist_max;
						GetDreamJournalList("search");
					}
				else
					{
						UpdateDJSubmittalsText("notloggedin");	
					}
			})
		.hover(
				function(){ 
					$(this).addClass("ui-state-hover");
				},
				function(){ 
					$(this).removeClass("ui-state-hover"); 
				}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('#dj-a-comments').click(function()
			{ 
				thesearch = "";
				if(dreamer_loggedin == "yes")
					{	
						$('#dj-thead-dreamslist').hide();
						$('#dj-thead-interpreteddreamslist').hide();
						$('#dj-thead-commentdreamlist').hide();
						$('#dj-tbody-dreamlist').empty();
						$('#dj-text-listamount').empty();
						$('#dj-text-comment').empty();
						sortby = "";
						sortorder = "ASC";
						limit_cur = 0;
						limit_max = dreamlist_max;
						GetDJCommentsList();
				} 
				else
					{
						UpdateDJSubmittalsText("notloggedin");	
					}
			})
		.hover(
			function(){ 
				$(this).addClass("int-a-hover");
			},
			function(){ 
				$(this).removeClass("int-a-hover"); 
			}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('#dj-button-previous').click(function()
			{
				limit_cur = limit_cur - dreamlist_max;
				limit_max = limit_max - dreamlist_max;
				$('#dj-thead-dreamslist').hide();
				$('#dj-thead-interpreteddreamslist').hide();
				$('#dj-thead-commentdreamlist').hide();
				GetDreamList();
			})
		.hover(
				function(){ 
					$(this).addClass("ui-state-hover");
				},
				function(){ 
					$(this).removeClass("ui-state-hover"); 
				}
			).mousedown(function(){
				$(this).addClass("ui-state-active");  
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('#dj-button-next').click(function()
			{
				limit_cur = limit_cur + dreamlist_max;
				limit_max = limit_max + dreamlist_max;
				$('#dj-thead-dreamslist').hide();
				$('#dj-thead-interpreteddreamslist').hide();
				$('#dj-thead-commentdreamlist').hide();
				GetDreamList();
			})
		.hover(
				function(){ 
					$(this).addClass("ui-state-hover");
				},
				function(){ 
					$(this).removeClass("ui-state-hover"); 
				}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
		$('.th-date').click(function()
			{
				if(sortorder == "ASC")
					{
						sortorder = "DESC";
					}
				else
					{
						sortorder = "ASC";	
					}
				sortby = "date";
				GetDreamList();
			})
		.hover(
				function(){ 
					$(this).addClass("ui-state-hover");
				},
				function(){ 
					$(this).removeClass("ui-state-hover"); 
				}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});
        $('.th-title').click(function()
			{
				if(sortorder == "ASC")
					{
						sortorder = "DESC";
					}
				else
					{
						sortorder = "ASC";	
					}
				sortby = "dream_title";
				GetDreamList();
			})
		.hover(
				function(){ 
					$(this).addClass("ui-state-hover");
				},
				function(){ 
					$(this).removeClass("ui-state-hover"); 
				}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
					$(this).removeClass("ui-state-active");
			});									  
	});
function IntPageLoad()
	{
		//alert("Tyler");
		$('#dj-a-comments').hide();
		$('#ed-process-update').hide();
		$('#c-process-update').hide();
		$('#vi-labels').hide();
		if(dreamer_loggedin == "yes")
			{
				$('#searchbox').show();
				$('#dj-newdream').show();
				//$('#dj-button-previous').hide();
				//$('#dj-button-next').hide();
				GetDreamList();
				UpdateDJSubmittalsText("");
			}	
		else
			{
				$('#dj-thead-commentdreamlist').hide();
				$('#dj-thead-dreamslist').show();
				$('#dj-thead-interpreteddreamslist').hide();
			//	$('#dj-thead-searchdreamlist').hide();
				//$('#dj-thead-commentdreamlist').hide();
				$('#searchbox').hide();
				$('#dj-newdream').hide();
			//	$('#dj-text-level').empty();
			//	$('#dj-text-listamount').empty();
			//	$('#dj-tbody-dreamlist').append('<tr><td colspan="7">'+"You must have an active recurring subscription in order to interpret dreams. <a href='http://dev.interpretmydream.com/members/signup.php'>Click Here </a>to subscribe."+'</td></tr>');
			}
	}
function CanShareDream()
	{
		$.post(djURL,
		   {
				request:"cansharedream",
				dream_id:dreamid
		   },
		function(res)
			{
				var info = $.json.deserialize(res);
				var inforesult = "";
				for(key in info)
					{
						inforesult = info[key].cansharedream;
					}
				if(inforesult)
					{
						if(inforesult == "new")
							{
								$('#view-sd-active').hide();
								$('#title-sd-active').hide();
								shareactive = 1;
								newshare = false;
							}
						else if(inforesult == "edit")
							{
								$('#view-sd-active').show();
								$('#title-sd-active').show();
								shareactive = "";
								newshare = true;
							}
						GetShareDreamInfo();
					}
				else
					{
						$('#dialog-djalert').dialog('open');
						updateTips("This dream is unavailable for sharing",$("#tips-djalert"));	
					}
			});
	}
function GetShareDreamInfo()
	{
		$('#view-sd-dreamtitle').empty();
		$.post(djURL,
			{
				request:"getsharedreaminfo",
				dream_id:dreamid
			},
		function(res)
			{
				var info = $.json.deserialize(res);
				var dream_title = "";
				var display_name = "";
				var display_scope = "";
				var is_active = "";
				for(key in info)
					{
						dream_title = info[key].dream_title;
						display_name = info[key].display_name;
						display_scope = info[key].display_scope;
						is_active = info[key].is_active;
					}
				if(dream_title)
					{
						$('#view-sd-dreamtitle').append(dream_title);
						$('#view-sd-displayname').val(display_name);
						$('#view-sd-scope').val(display_scope);
						$('#view-sd-active').val(is_active);
						$('#dialog-viewingmydream').dialog('close');
						$('#dialog-sharedream').dialog('open');
					}
				else
					{
						$('#dialog-djalert').dialog('open');
						updateTips("Please try again",$("#tips-djalert"));	
					}
			});
	}
function ShareDreamPublicly()
	{
		$.post(djURL,
		   {
				request:"makedreampublic",
				dream_id:dreamid,
				sharename:sharename,
				sharescope:sharescope,
				shareactive:shareactive
				
		   },
		function(res)
			{
				var info = $.json.deserialize(res);
				var inforesult = "";
				for(key in info)
					{
						inforesult = info[key].id;
					}
				if(inforesult)
					{ 
						//this.target = "_blank";
						window.open('/shared/'+inforesult,'_blank');
					}
				else
					{
						$('#dialog-djalert').dialog('open');
						updateTips("Please try again.",$("#tips-djalert"));	
					}
				$('#dialog-sharedream-confirm').dialog('close');
				$('#dialog-sharedream').dialog('close');
				sharename = "";
				sharescope = "";
			});
	}	
function GetDreamJournalList(request) 
	{
		$('#dj-thead-dreamslist').show();
		$('#dj-thead-interpreteddreamslist').hide();
		$('#dj-thead-commentdreamlist').hide();
		$('#dj-tbody-dreamlist').empty();
		$('#dj-tbody-dreamlist').append(processingbar);
		$.post(djURL,
		{
			request:request,
			thesearch:thesearch,
			sortby:sortby,
			sortorder:sortorder,
			limit_cur:limit_cur,
			limit_max:limit_max 
		},
		function(res)
		{
			var dreamlist = $.json.deserialize(res); 
			var dreamlist_result = "";
			for(key in dreamlist)
				{
					//alert(dreamlist[key].date_submitted);
					dreamlist_result += '<tr>';
					dreamlist_result += '<td>' + dreamlist[key].dream_date + '</td>';
					dreamlist_result += '<td>' + dreamlist[key].dream_title + '</td>';
					dreamlist_result += '<td>' + dreamlist[key].dj_status + '</td>';
					dreamlist_result += '<td>' + dreamlist[key].dj_editview + '</td>';
					dreamlist_result += '<td>' + dreamlist[key].dj_interp + '</td>';
					dreamlist_result += '</tr>';
				}
				if(request == "search")
					{
						$('#searchbox-field').val("");
					}
				UpdateDreamListText(request,thesearch);
				$('#dj-tbody-dreamlist').empty();
				$('#dj-tbody-dreamlist').append(dreamlist_result);
				$('#dj-tbody-dreamlist tr:odd').css("background-color", "#e1e7f3");
				$('#dj-tbody-dreamlist tr:even').css("background-color", "#f3f6fa");
		});
	}
function GetInterpretedDreamJournalList(request) 
	{
		$('#dj-thead-dreamslist').hide();
		$('#dj-thead-interpreteddreamslist').show();
		$('#dj-thead-commentdreamlist').hide();
		$('#dj-tbody-dreamlist').empty();
		$('#dj-tbody-dreamlist').append(processingbar);
		$.post(djURL,
		{
			request:request,
			thesearch:thesearch,
			sortby:sortby,
			sortorder:sortorder,
			limit_cur:limit_cur,
			limit_max:limit_max 
		},
		function(res)
		{
			var dreamlist = $.json.deserialize(res); 
			var dreamlist_result = "";
			for(key in dreamlist)
				{
					//alert(dreamlist[key].date_submitted);
					dreamlist_result += '<tr>';
					dreamlist_result += '<td>' + dreamlist[key].dream_date + '</td>';
					dreamlist_result += '<td>' + dreamlist[key].dream_title + '</td>';
					dreamlist_result += '<td>' + dreamlist[key].dj_status + '</td>';
					dreamlist_result += '<td>' + dreamlist[key].dj_interp + '</td>';
					dreamlist_result += '</tr>';
				}
				if(request == "search")
					{
						$('#searchbox-field').val("");
					}
				UpdateDreamListText(request,thesearch);
				$('#dj-tbody-dreamlist').empty();
				$('#dj-tbody-dreamlist').append(dreamlist_result);
				$('#dj-tbody-dreamlist tr:odd').css("background-color", "#e1e7f3");
				$('#dj-tbody-dreamlist tr:even').css("background-color", "#f3f6fa");
		});
	}
function GetDreamList()
	{
		//TT 2009.07.13
			//LIMITS: if on interpret.php page and type in link OR click on link w/in favorites, info will not reload
		var DreamListHash = window.location.hash;
		if(DreamListHash == "#alldreams" || DreamListHash == "")
			{
				rowspan = 5;
				GetDreamJournalList("alldreams");
			}
		else if(DreamListHash == "#unsubmitted")
			{
				rowspan = 5;
				GetDreamJournalList("unsubmitted");
			}
		else if(DreamListHash == "#submitted")
			{
				rowspan = 5;
				GetDreamJournalList("submitted");
			}
		else if(DreamListHash == "#interpreted")
			{
				rowspan = 5;
				GetInterpretedDreamJournalList("interpreted");
			}
		else if(DreamListHash == "#search")
			{
				rowspan = 5;
				GetDreamJournalList("search");
			}
		else if(DreamListHash == "#comments")
			{
				rowspan = 4;
				GetDJCommentsList();
			}
	}
function GetDJCommentsList()
	{
		$('#dj-thead-dreamslist').hide();
		$('#dj-thead-interpreteddreamslist').hide();
		$('#dj-thead-commentdreamlist').show();
		$('#dj-tbody-dreamlist').empty();
		$('#dj-tbody-dreamlist').append(processingbar);
		$.post(djURL,
			{
				request:"getcomments",
				sortby:sortby,
				sortorder:sortorder,
				limit_cur:limit_cur,
				limit_max:limit_max 
			},
		function(res)
			{
					var comment = $.json.deserialize(res);
					var commentlist_result = "";
					for(key in comment)
						{
							commentlist_result += '<tr>';
							commentlist_result += '<td>' + comment[key].dream_date + '</td>';
							commentlist_result += '<td>' + comment[key].dream_title + '</td>';
							commentlist_result += '<td>' + comment[key].dj_status + '</td>';
							commentlist_result += '<td>' + comment[key].dj_action + '</td>';
							commentlist_result += '</tr>';
						}
					
					UpdateDreamListText("comments","");
					$('#dj-tbody-dreamlist').empty();
					if(commentlist_result != "")
						{
							$('#dj-tbody-dreamlist').append(commentlist_result);
							$('#dj-tbody-dreamlist tr:odd').css("background-color", "#e1e7f3");
							$('#dj-tbody-dreamlist tr:even').css("background-color", "#f3f6fa");
						}
			});
	}
function CommentReply(x_dreamid)
	{
		$('#c-dreamtitle').empty();
		$('#c-comment-info').empty();
		$('#c-comment-replytext').val("");
		$.post(djURL,
			{
				request:"commentreply",
				dream_id:x_dreamid
			},
		function(res)
			{
				
				var commentreply = $.json.deserialize(res);
				var commentreply_dreamtitle = "";
				var commentreply_info = "";
				var x_dreamid = "";
				for(key in commentreply)
					{
						commentreply_dreamtitle = commentreply[key].dream_title;
						commentreply_info += '<span>' + commentreply[key].question_timestamp + " " + commentreply[key].commentor + '</span><br />';
						commentreply_info += '<span>' + commentreply[key].question_text + '</span><br /><br />';
						x_dreamid = commentreply[key].dream_id;
					}
				if(commentreply_dreamtitle == "")
					{
						updateTips("There are no open comments at this time",$('#tips-comment-reply'));
					}
				else
					{
						$('#c-dreamtitle').append(commentreply_dreamtitle);
						$('#c-comment-info').append(commentreply_info);
					}
				dreamid = x_dreamid;
				$('#dialog-comment-reply').dialog('open');
			});
	}	
function SendCommentReply(dreamid,comment_newreply)
	{
		$.post(djURL,
			{
				request:"sendcommentreply",
				dream_id:dreamid,
				comment_newreply:comment_newreply
			},
		function(res)
			{
				//alert(res);
				var commentsent = $.json.deserialize(res);
				var commentsent_result = "";
				for(key in commentsent)
					{
						commentsent_result = commentsent[key].replysent;
					}
				if(commentsent_result == "yes")
					{
						updateTips("Your comment was successfully sent",$('#tips-dj-success'));
						$('#dialog-dj-success').dialog('open');
						$('#dialog-comment-reply').dialog('close');
						GetDreamList();
					}
				else
					{
						updateTips("There was a problem submitting your comment. Please try again",$('#tips-int-error'));
						$('#dialog-dj-error').dialog('open');
						$('#int-thead-commentdreamlist').hide();
						$('#int-tbody-dreamlist').empty();
						$('#dj-text-listamount').empty();
						$('#dj-text-comment').empty();
						GetInterpretCommentsList();
					}
			});
	}
function UpdateDJSubmittalsText(hash)
	{
		//alert(hash);
		$('#dj-text-submittals').empty();
		$.post(djURL, 
			{
				request:"updatedjsubmittalstext",
				hash:hash
			},
		function(res)
		{
			var textform = $.json.deserialize(res);
			var text_result = "";
			
			for(key in textform)
				{
					text_result += textform[key].djtext;
				}
			$('#dj-text-submittals').append(text_result).effect("highlight",{},3000);
		});
	}
function UpdateDreamListText(hash,thesearch)
	{
		//alert(hash);
		$('#dj-text-listamount').empty();
		$('#dj-text-comment').empty();
		$('#dj-button-next').hide();
		$('#dj-button-previous').hide();
		$('#dj-a-comments').hide();
		$.post(djURL, 
			{
				request:"updatedreamlisttext",
				hash:hash,
				thesearch:thesearch
			},
		function(res)
		{
			var textform = $.json.deserialize(res);
			var text_result = "";
			var dream_count = "";
			var comment = "";
			for(key in textform)
				{
					text_result += textform[key].dreamtext;
					dream_count += textform[key].dreamcount;
					comment += textform[key].iscomment;
				}
			$('#dj-text-listamount').append('<br />' + text_result).effect("highlight",{},3000);
			if(dreamer_loggedin == "yes")
				{
					if(dream_count > limit_max)
						{
							$('#dj-button-next').show();
						}
					if(limit_cur > 0)
						{
							$('#dj-button-previous').show();
						}
				}
			if(comment != "")
				{
					$('#dj-a-comments').show();
					if(hash != "comments")
						{
							$('#dj-text-comment').append('<br />'+comment).effect("highlight",{},3000);
						}
				}
			if(hash == "comments")
				{
				$('#dj-a-comments').show();	
				}
		});
	}
function RemoveInterpRequest(x_dreamid)
	{
		$('#text-removeinterp').empty();
		dreamid = x_dreamid;
		$('#dialog-removeinterp').dialog('open');
	}
function RemoveInterp(dreamid)
	{
		$.post(djURL,
		{
			request:"removeinterp",
			dream_id:dreamid
		},
		function(res)
		{
			var info = $.json.deserialize(res);
			var info_result = "";
			for(key in info)
				{
					info_result += info[key].removal_success;	
				}
			$('#dialog-removeinterp').dialog('close');
			if(!info_result)
				{
					$('#dialog-djalert').dialog('open');
					updateTips("It is possible that an interpreter has begun interpreting, if the option is still available please try again",$("#tips-djalert"));
				}
			GetDreamList();
		});
	}
function getDream(dream_id, member_id)
	{
		$('#dream_content').hide();
		$('#viewdream-process').show();
		$('#dialog-viewdream').dialog('open');
		$('#dream_name').empty();
		$('#dream_text').empty();
		$('#dream_date').empty();
		//alert(request + param1 + param2 + param3 + param4 + param5 + param6 + eid + login)
		$.post(djURL, 
			{
				request:"view_dream",
				dream_id:dream_id,
				member_id:member_id
			},
		function(xml)
			{
				$(xml).find('dream').each(function()
					{
							var dream_name = $("dream_name",this).text();
								var dream_text = $("dream_text",this).text();
								var dream_date = $("dream_date",this).text();
								
								$('#dream_name').append(dream_name);
								$('#dream_text').append(dream_text);
								$('#dream_date').append(dream_date);
								
								
								$('#viewdream-process').hide();
								$('#dream_content').show();
					});
			});
	}
function newDream(member_id,coloration,dream_title,perspective,anonymous,dreamtext) {
		
		$.post(djURL, 
			{
				request:"create_dream",
				member_id:member_id,
				coloration:coloration,
				dream_title:dream_title,
				perspective:perspective,
				anonymous:anonymous,
				dream_text:dreamtext
				
			},
			function(xml)
			{
				$(xml).find('result').each(function()
					{
							var success = $("success",this).text();
							
							if (success == "yes") {
							//window.location.reload();
							$("#dialog-newdream").dialog('close');
							GetDreamList();
							}
							
					});
			});
	}
function updateDream(request,dreamid,coloration,dream_title,perspective,anonymous,dreamtext)
	{
		$.post(djURL,
			{
				request:request,
				dream_id:dreamid,
				coloration:coloration,
				dream_title:dream_title,
				perspective:perspective,
				anonymous:anonymous,
				dreamtext:dreamtext
			},
		function(res)
			{
				$('#ed-process-update').hide();
				//$('#fieldset-dream').show();
					
				if(request == "simpleupdate")
				{
					$('#dialog-confirm').dialog('open');
				}
				else
				{
					var confirmation = $.json.deserialize(res);
					var confirmation_result = "";
				
					for(key in confirmation)
						{
							confirmation_result = confirmation[key].confirmation;
						}
					
					var tips = "";
					
					$('#c-process-update').hide();
	
					if(confirmation_result == "ok")
						{
							if(request == "updatedream")
								{
									tips = "Your Dream was successfully updated!";
								}
							else if(request == "submitforinterp")
								{
									$('#c-process-update').hide();
									$("#tips-confirm").show();
									$('#dialog-confirm').dialog('close');
									tips = "Dream Submittal Successful!";
									UpdateDJSubmittalsText("");
								}
							$('#dialog-editdream').dialog('close');
						}
					else
						{
							tips = confirmation_result;
							//tips = res;
						}
					$('#dialog-djalert').dialog('open');
					updateTips(tips,$("#tips-djalert"));
					GetDreamList();
				}
		});
			
	}
function CheckDreamSubmittals(dreamid)
	{
		$.post(djURL,
			{
				request:"submittalcheck",
				dream_id:dreamid
			},
		function(res)
			{
				var confirmation = $.json.deserialize(res);
				var confirmation_result = "";
			
				for(key in confirmation)
					{
						confirmation_result = confirmation[key].confirmation;
					}
				
				$('#process-update').hide();
				//$('#fieldset-dream').show();
				if(confirmation_result == "yes")
					{
						if(interprequest)
							{
							$.post(djURL,
								{
									request:"editdream",
									dream_id:dreamid
								},
								function(res)
								{
									var info = $.json.deserialize(res);
										dreamid = "";
										var dream_title = "";
										var coloration = "";
										var perspective = "";
										var anonymous = "";
										var dreamtext = "";
										for(key in info)
											{
												dreamid = info[key].dream_id;
												dream_title = info[key].dream_title;
												coloration = info[key].dream_coloration;
												perspective = info[key].dream_perspective; 
												anonymous = info[key].hidedreamer;
												dreamtext = info[key].dream_text;
											}
										$("#tips-confirm").hide();
										//$('#c-process-update').show();
										//updateTips("Submitting for Interpretation...",$("#c-process-update h2"));
										updateDream("submitforinterp",dreamid,coloration,dream_title,perspective,anonymous,dreamtext);
										interprequest = false;
								});	
							}
						else
							{
								$("#tips-confirm").hide();
								//$('#c-process-update').show();
								//updateTips("Submitting for Interpretation...",$("#c-process-update h2"));
								updateDream("submitforinterp",dreamid,coloration,dream_title,perspective,anonymous,dreamtext);
							}
					}
				else
					{
						updateTips(confirmation_result,$("#tips-confirm"));
					}
			});
			
	}
function EditViewDream(request,x_dreamid) 
	{	
		$('#nd-dream-title').val("");
		$('#nd-coloration').val("");
		$('#nd-perspective').val("");
		$('#nd-anonymous').val("");
		$('#nd-dreamtext').val("");
		$('#view-dreamtitle').empty();
		$('#view-coloration').empty();
		$('#view-perspective').empty();
		$('#view-anonymous').empty();
		$('#view-dreamtext').empty();
		$.post(djURL, 
			{
				request:request,
				dream_id:x_dreamid
			},
			function(res)
			{
				var info = $.json.deserialize(res);
				var xx_dreamid = "";
				var dream_title = "";
				var coloration = "";
				var perspective = "";
				var anonymous = "";
				var dreamtext = "";
				for(key in info)
					{
						xx_dreamid = info[key].dream_id;
						dream_title = info[key].dream_title;
						coloration = info[key].dream_coloration;
						perspective = info[key].dream_perspective; 
						anonymous = info[key].hidedreamer;
						dreamtext = info[key].dream_text;
					}
				NewEditViewShowHide(request);
				dreamid = xx_dreamid;
				if(request == "editdream")
					{
						$('#nd-process').hide();
						$('#nd-dream-title').val(dream_title);
						$('#nd-coloration').val(coloration);
						$('#nd-perspective').val(perspective);
						$('#nd-anonymous').val(anonymous);
						$('#nd-dreamtext').val(dreamtext);
						
						$('#dialog-editdream').dialog('open');
						$('#dialog-editdream').append($('#fieldset-dream'));
					}
				else if(request == "viewdream" || request == "viewinterp")
					{
						$('#nd-process').hide();
						
						$('#view-dreamtitle').append(dream_title);
						$('#view-coloration').append(coloration);
						$('#view-perspective').append(perspective);
						$('#view-anonymous').append(anonymous);
						$('#view-dreamtext').append(dreamtext);
						
						$('#dialog-viewingmydream').append($('#fieldset-dream'));
						if(request == "viewinterp")
							{
								GetInterpInfo(dreamid);
							}
						$('#dialog-viewingmydream').dialog('open');
					}
			});
	}
function startDream(member_id) {
	$('#nd-process').hide();
	
	var dream_title = "";
		$('#nd-dream_text').show_char_limit(50000, { status_element: '#char', status_style: 'chars_typed' });
		//alert('before');
					$("input").empty();
				//$('#nd-coloration').empty();
					//$('#nd-perspective').empty();
						//$('#nd-anonymous').empty();
							$('#nd-dreamtext').empty();
					//		alert('after');
						
	$('#nd-dream-title').val("");
	$('#nd-coloration').val("");
	$('#nd-perspective').val("");
	$('#nd-anonymous').val("");
	$('#nd-dreamtext').val("");
	
	$('#view-dreamtitle').empty();
	$('#view-coloration').empty();
	$('#view-perspective').empty();
	$('#view-anonymous').empty();
	$('#view-dreamtext').empty();
	NewEditViewShowHide("newdream");
	$('#dialog-newdream').dialog('open');
	$('#dialog-newdream').append($('#fieldset-dream'));
	$('#nd-content').show();
}
function NewEditViewShowHide(request)
	{
		if(request == "newdream" || request == "editdream")
			{
				$('#nd-dream-title').show();
				$('#nd-coloration').show();
				$('#nd-perspective').show();
				$('#nd-anonymous').show();
				$('#nd-dreamtext').show();
		
				$('#view-dreamtitle').hide();
				$('#view-coloration').hide();
				$('#view-perspective').hide();
				$('#view-anonymous').hide();
				$('#view-dreamtext').hide();
			}
		else if(request == "viewdream" || request == "viewinterp")
			{
				$('#nd-dream-title').hide();
				$('#nd-coloration').hide();
				$('#nd-perspective').hide();
				$('#nd-anonymous').hide();
				$('#nd-dreamtext').hide();
				
				$('#view-dreamtitle').show();
				$('#view-coloration').show();
				$('#view-perspective').show();
				$('#view-anonymous').show();
				$('#view-dreamtext').show();
			}
	}
function SubmitforInterpRequest(dream_id)
	{
		dreamid = dream_id;
		$.post(djURL,
			{
				request:"submittaltext",
				dream_id:dream_id	
			},
		function(res)
			{
				var info = $.json.deserialize(res);
				var info_response = "";
				for(key in info)
					{
						info_response += info[key].confirmation;
					}
			
				interprequest = true;
				updateTips(info_response,$('#dialog-confirm'));
				$('#dialog-confirm').dialog('open');
		});
	}
function GetInterpInfo(dreamid)
	{
		$.post(djURL,
			{
				request:"getinterpinfo",
				dream_id:dreamid
			},
		function(res)
			{
				var info = $.json.deserialize(res);
				var intdate = "";
				var interpreter = "";
				var inttext = "";
				
				for(key in info)
					{
						intdate = info[key].int_date;
						interpreter = info[key].interpreter;
						inttext = info[key].int_text;
					}
				$('#view-interpdate').empty();
				$('#view-interpreter').empty();
				$('#view-interptext').empty();
				
				$('#view-interpdate').append(intdate);
				$('#view-interpreter').append(interpreter);
				$('#view-interptext').append(inttext);
				
				$('#vi-labels').show();
			});
	}