//////////////////////////////////////////////////////////////////////////////
//changeRoom
//
//
//Desc: Confirms that the user wants to move to another room.
//////////////////////////////////////////////////////////////////////////////
function changeRoom(Room_Name,Room_ID){
	if(confirm("Change to new room - "+Room_Name+"?")){
		location.href='chat.php?Room_ID='+Room_ID;
	}//end if
}//end function
//////////////////////////////////////////////////////////////////////////////
//sendMessage
//
//
//Desc: uses ajax to post a message to a room
//////////////////////////////////////////////////////////////////////////////
function sendMessage(Room_ID){
	//Get the message contents
	var contents = escape(document.getElementById('messageContents').value);
	

	var vars = "function_name=sendMessage&param1="+Room_ID+"&param2="+contents;	
	updateContentArea('sendMessage',vars,'../../includes/php/run.php',"document.getElementById('messageContents').focus();");

//	updateDiv('sendMessage','sendMessage',Room_ID,contents);
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//refreshRooms
//
//
//Desc: Calls ajax to repopulate the room's list with an update
//////////////////////////////////////////////////////////////////////////////
function refreshRooms(Room_ID){
	
	updateDiv('RoomScoller','grabChatRooms',Room_ID);

}//end function
//////////////////////////////////////////////////////////////////////////////
//refreshUserList
//
//
//Desc: Calls ajax to repopulate the room's user list
//////////////////////////////////////////////////////////////////////////////
function refreshUserList(Room_ID){
	
	updateDiv('curUsers','grabChatCurrentUsers',Room_ID);
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//refreshMainChat
//
//
//Desc: calls ajax to update the chat area.
//////////////////////////////////////////////////////////////////////////////
function refreshMainChat(Room_ID){
	
	var vars = "function_name=grabChatLogs&param1="+Room_ID;	
	updateContentArea('mainChat',vars,'../../includes/php/run.php',"scrollToBottom()");		

//	updateDiv('mainChat','grabChatLogs',Room_ID);
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//cleaningCron
//
//
//Desc: calls a php script to tidy up the place
//////////////////////////////////////////////////////////////////////////////
function cleaningCron(Room_ID){
	
	updateDiv('RoomScoller','cleaningCron',Room_ID);
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//handleEnter
//
//
//Desc: detects the key pressed and, if its enter, fires the sendMessage command
//////////////////////////////////////////////////////////////////////////////
function handleEnter(e,Room_ID){
	
	var code = e.keyCode;
	if(code==13){
		sendMessage(Room_ID);
	}//end if
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//scrollToBottom
//
//
//Desc: scrolls the main chat to the bottom 
//////////////////////////////////////////////////////////////////////////////
function scrollToBottom(){

	if(browsing_chat==0){
		var objDiv = document.getElementById("mainChat");
		objDiv.scrollTop = objDiv.scrollHeight;	
	}
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//inviteUsers
//
//
//Desc: gets a list of user names and submits them to ajax for invites.
//////////////////////////////////////////////////////////////////////////////
function inviteUsers(Room_ID){
	
	var invites = document.getElementById('invites').value;
	updateDiv('inviteUsers','inviteUsers',Room_ID,invites);
	
}//end function
//////////////////////////////////////////////////////////////////////////////
//showInvitation
//
//
//Desc: This is called once for each standing chat invitation a given user
//currently has.
//////////////////////////////////////////////////////////////////////////////
function showInvitation(Username,Room_Name,Room_ID){
	
	if(confirm(Username+" has invited you to chat in "+Room_Name+". Do you accept?")){
		openChat(Room_ID);
	}else{
		updateDiv('','denyInvite',Username,Room_ID);
	}//end else

}//end function
//////////////////////////////////////////////////////////////////////////////
//openChat
//
//
//Desc: called by accepting an invitation. Opens a chat window for the new room
//////////////////////////////////////////////////////////////////////////////
function openChat(Room_ID){
	window.open("chat.php?Invite=true&Room_ID="+Room_ID,'Chat','width=710,height=466');
}//end 
//////////////////////////////////////////////////////////////////////////////
//setWhisper
//
//
//Desc: sets the target of the whisper field then sets the focus to the text
//entry for a whisper.
//////////////////////////////////////////////////////////////////////////////
function setWhisper(User_Name){
	document.getElementById('whisperTarget').value=User_Name;
	document.getElementById('whisperText').focus();
}//end function
//////////////////////////////////////////////////////////////////////////////
//sendWhisper
//
//
//Desc: Calls ajax to send a whisper and reload/refocus on the whisper box.
//////////////////////////////////////////////////////////////////////////////
function sendWhisper(Room_ID){

	//Get the message contents
	var contents = document.getElementById('whisperText').value;
	//Get the target
	var target = document.getElementById('whisperTarget').value;

	var vars = "function_name=ajaxWhisper&param1="+Room_ID+"&param2="+target+"&param3="+contents;	
	updateContentArea('whisper',vars,'../../includes/php/run.php',"document.getElementById('whisperText').focus();");

}//end function