// Some global variables
var globalEntityMessagesArray;


function getNewMessages(pDocumentText)
{
	var pDocument = null;
	try 
	{
		pDocument = eval(pDocumentText);
		if (pDocument == null)
		{
			return;
		}
	} catch (ee) { return; }
	
	if (!top.frames.headerFrame)
	{
		return;
	}
	
	if (!top.frames.headerFrame.loaded)
	{
		setTimeout("getNewMessages('"+pDocumentText+"');", 100);
		return;
	}
	
	if (top.frames.headerFrame.loadedEntityId == -1)
	{
		// User not logged in
		return;
	}
	
	var myRequest = new ajaxObject('');
	myRequest.callback = function(responseText, responseStatus, responseXML) 
	{
		if (responseStatus = 200) 
		{
		  	var status = 999;
		  	var message = '';
		  	
		  	var numNewMessages = 0;
		  	
		  	try { eval(responseText); } catch (e) {}
	  		
		  	// Status=1		Message='OK'
		  	
		  	if (status == 1)
		  	{
		  		if (numNewMessages > 0)
		  		{
		  			var newMessagesDiv = pDocument.getElementById('id_newMessagesDiv');
						if (newMessagesDiv != null)
						{
							newMessagesDiv.innerHTML = "<table border='0' cellpadding='0' cellspacing='0' width='100%'>\n" +
																				 "  <tr>\n" +
																				 "    <td nowrap align='right'><div align='right' class='sblue'><b>"+numNewMessages+"&nbsp;New&nbsp;</b><img src='"+fileUrl+"/images/icons/send.gif' width='15' height='14' border='0'>&nbsp;</div></td>\n" +
																				 "  </tr>\n" +
																				 "</table>";
						}
			  		return;
		  		}
		  	}
		} 
	}
	
	var app = 'app=people';
	var cmd = 'cmd=USERINFO';
	var subcmd = 'subcmd=NUMNEWMESSAGES';

	var sendString = app+'&'+cmd+'&'+subcmd;
	myRequest.update(sendString,'POST');
}


function getMessagesInbox()
{
		// Clear previous errors if there were any.
		var errorDiv = document.getElementById('id_errorDiv');
		if (errorDiv != null)
		{
			errorDiv.innerHTML = "";
		}
		
		var myRequest = new ajaxObject();
		myRequest.callback = function(responseText, responseStatus, responseXML) 
		{
	      if (responseStatus = 200) 
		  	{
				  	var status = -999;
				  	var message;
				  	
				  	try { eval(responseText); } catch (e) {}
				  	
				  	// Status=1		Message='OK'
				  	
				  	if (status == 1)
				  	{
				  		globalEntityMessagesArray = entityMessagesArray;
					  	printInbox();
				  	}
				  	else if (status == -999)
				  	{
				  		top.document.location="../login.html";
				  	}
				  	else
						{
						  	var errorDiv = document.getElementById('id_errorDiv');
								if (errorDiv != null)
								{
									errorDiv.innerHTML = "<div class='alert_noimg'>"+getGenericErrorMessage()+"</div><br>";
								}
					  }
				} 
				else 
				{
				  	var errorDiv = document.getElementById('id_errorDiv');
						if (errorDiv != null)
						{
							errorDiv.innerHTML = "<div class='alert_noimg'>"+getGenericErrorMessage()+"</div><br>";
						}
			  }
	  }
		
		var app = 'app=people';
		var cmd = 'cmd=USERINFO';
		var subcmd = 'subcmd=MESSAGES';
		
		var sendString = app+'&'+cmd+'&'+subcmd;
		myRequest.update(sendString,'POST');
}


function printInbox()
{
	var inboxDiv = document.getElementById('id_MessagesInboxDiv');
	if (inboxDiv != null)
	{
		var inboxHTML = "";
		
		inboxHTML += "<div class='iMainTitle'>Inbox</div>\n";
										
		if (globalEntityMessagesArray.length > 0)
		{
			inboxHTML += "<table border='0' cellspacing='1' cellpadding='3' bgcolor='#FFFFFF' width='100%'>\n" +
	    						 "  <tr>\n" +
			             "    <td bgcolor='#EBF5FF' style='border-top-width:1; border-top-color:rgb(153,204,255); border-top-style:solid;'><div class='slgray'>From</div></td>\n" +
			             "    <td bgcolor='#EBF5FF' style='border-top-width:1; border-top-color:rgb(153,204,255); border-top-style:solid;'><div class='slgray'>Subject</div></td>\n" +
			             "    <td bgcolor='#EBF5FF' style='border-top-width:1; border-top-color:rgb(153,204,255); border-top-style:solid;'><div class='slgray'>Date</div></td>\n" +
									 "  </tr>\n";
			
			var numPrinted = 0;				 
			for (var numM = 0; numM < globalEntityMessagesArray.length; numM++)
			{
				var currMessage = globalEntityMessagesArray[numM];
				if (currMessage != null)
				{				
					if (currMessage[8] != '')
					{
						// This message has been deleted
						continue;	
					}
					
					var backgroundColor = "#f9f9f9";
					var fontUsed = "sgray";
					if(currMessage[7] == '')
					{
						backgroundColor="#FFFFFF";
						fontUsed="bsgray";
					}
					
					var messageDate = getDateFromDBDateString(currMessage[6]);
        	var messageDateString = "";
        
        	if ((messageDate != null) && (messageDate != ''))
        	{
        		messageDateString = month_names[messageDate.getMonth()] + " " + messageDate.getDate() + ", " + messageDate.getFullYear();
        	}
					
					inboxHTML += "  <tr>\n" +
											 "    <td style='border-bottom-width:1; border-bottom-color:rgb(235,245,255); border-bottom-style:solid;' nowrap valign='top'><div class='"+fontUsed+"' style='cursor:pointer;cursor:hand;' onClick='displayMessage("+currMessage[0]+")'>"+currMessage[3]+"</div></td>\n" +
											 "    <td style='border-bottom-width:1; border-bottom-color:rgb(235,245,255); border-bottom-style:solid;' valign='top'><div class='"+fontUsed+"' style='cursor:pointer;cursor:hand;' onClick='displayMessage("+currMessage[0]+")'>"+currMessage[4]+"</div></td>\n" +
											 "    <td style='border-bottom-width:1; border-bottom-color:rgb(235,245,255); border-bottom-style:solid;' nowrap valign='top'><div class='"+fontUsed+"' style='cursor:pointer;cursor:hand;' onClick='displayMessage("+currMessage[0]+")'>"+messageDateString+"</div></td>\n" +
											 "  </tr>\n";
					numPrinted++;
				}
			}
										 
			inboxHTML += "</table>";
		}
		else
		{
			inboxHTML += "<div class='slgray'>There are no messages in your Inbox at the moment.</div>\n";
		}
		if (numPrinted == 0)
		{
			inboxHTML += "<div class='slgray'>There are no messages in your Inbox at the moment.</div>\n";
		}
									 
		inboxDiv.innerHTML = inboxHTML;
	}
}


function displayMessage(messageId)
{
	var inboxDiv = document.getElementById('id_MessagesInboxDiv');
	if (inboxDiv != null)
	{
		var inboxHTML = "";
		
		for (var numM = 0; numM < globalEntityMessagesArray.length; numM++)
		{
			var currMessage = globalEntityMessagesArray[numM];
			if (currMessage != null)
			{		
				if (currMessage[0] == messageId)
				{
					inboxHTML += "<table border='0' cellpadding='2' cellspacing='0'>\n" +
											 "  <tr>\n" +
											 "    <td bgcolor='#EBF5FF' style='border-top-width:1; border-top-color:rgb(153,204,255); border-top-style:solid;' align='right'>\n" +
											 "      <a href='messagesFrame.html' onClick='printInbox();return false;' class='sblue'><u>Inbox</u></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='messagesFrame.html' onClick='deleteMessage("+messageId+");printInbox();return false;' class='sblue'><u>Delete this message</u></a>\n" +
											 "    </td>\n" +
											 "  </tr>\n" +
											 "  <tr>\n" +
											 "    <td>\n" +
											 "      <div class='attributeName'>Subject: <span class='sgray'><b>"+currMessage[4]+"</b></span></div>\n" +
											 "      <table border='0' cellspacing='0' cellpadding='0' height='2'><tr><td height='2' background="+fileUrl+"/images/background/blueGradientDiv.gif'></td></tr></table>\n" +
											 "      <div class='attributeName'>From: <span class='sgray'><b>"+currMessage[3]+"</b></span></div>\n" +
											 "      <table border='0' cellspacing='0' cellpadding='0' height='2'><tr><td height='2' background="+fileUrl+"/images/background/blueGradientDiv.gif'></td></tr></table>\n" +
											 "      <hr size='1' color='#EBF5FF'>\n" +
											 "      <div class='sgray'>"+currMessage[5]+"</div>\n" +
											 "    </td>\n" +
											 "  </tr>\n" +
											 "  <tr>\n" +
											 "    <td bgcolor='#FFFFFF' style='border-bottom-width:1; border-bottom-color:rgb(153,204,255); border-bottom-style:solid;' align='right'>&nbsp;</td>\n" +
											 "  </tr>\n" +
											 "</table>\n";
					
					if (currMessage[7] == '')
					{
						currMessage[7] = 'A moment ago';
						
						var myRequest = new ajaxObject();
					
						var app = 'app=people';
						var cmd = 'cmd=USERINFO';
						var subcmd = 'subcmd=READMESSAGE';
						var messageId = 'messageId='+messageId;
						
						var sendString = app+'&'+cmd+'&'+subcmd+'&'+messageId;
						myRequest.update(sendString,'POST');
					}
					
					break;
				}		
			}
		}
		
		inboxDiv.innerHTML = inboxHTML;
	}
}

function deleteMessage(messageId)
{
	var inboxDiv = document.getElementById('id_MessagesInboxDiv');
	if (inboxDiv != null)
	{		
		for (var numM = 0; numM < globalEntityMessagesArray.length; numM++)
		{
			var currMessage = globalEntityMessagesArray[numM];
			if (currMessage != null)
			{		
				if (currMessage[0] == messageId)
				{
					currMessage[8] = 'A moment ago'; 
					
					var myRequest = new ajaxObject();
					
					var app = 'app=people';
					var cmd = 'cmd=USERINFO';
					var subcmd = 'subcmd=DELETEMESSAGE';
					var messageId = 'messageId='+messageId;
					
					var sendString = app+'&'+cmd+'&'+subcmd+'&'+messageId;
					myRequest.update(sendString,'POST');
					
					break;
				}		
			}
		}
	}
}
