function colorMe( elem, sColor ){
	$(elem).style.color = sColor;
}

function hist_LoadInbox(){
	hist_CheckAdd('hist_LoadInbox', 'inbox', 'hist_LoadInbox');
	mess_LoadInbox();
}
function mess_LoadInbox() {
	aj_MarkLoadingBlack('loginDetails', 'Loading...');
	new Ajax.Updater('loginDetails', curPath+'handler.php', {
					 parameters: {'event':'viewinbox'} } );	
	
}

function hist_textNewInboxMsg( forid ){
	
	if (!curUser.isLoggedIn()){
		alert("You cannot send a message if you have not logged in.");
		return;
	}
	/*
	var cObj = new Object();
	cObj.link_clicked = 'text_new_inbox';
	hist_CheckAdd('hist_textReplyInboxMsg', 'new_private_post', cObj );
	*/
	mess_textNewInboxMsg( forid );
}

function mess_textNewInboxMsg( forid ){
	
	if (!curUser.isLoggedIn()){
		alert('You must be logged in to send a message.');
		return;
	}

	var divN = 'newMessageInputDiv';
	var formN = 'newMessageInputForm';
	
	if (forid){
		divN = divN+"_"+forid;
		formN = "newMailMessageForm_"+forid;
		var params = Form.serializeElements( $(formN).getElements() );
		params  = params+"&event=postinboxnew&sub=text&type=search&to_id="+forid;
	} else {
		var msg = $F('newmessagetext');
		var toID = $F('to_user_id');
		var subj = $F('newmessagesubj');
		var params = "event=postinboxnew&sub=text&msg="+msg+"&subject="+subj+"&to_id="+toID;
		
	}
	
	if (subj == ""){
		alert('Please enter a subject line.');
		return;
	}
	if (msg == ""){
		alert("Please enter a message text.");
		return;
	}
	aj_MarkLoadingBlack(divN, 'Sending...');
	new Ajax.Updater(divN, curPath+'handler.php', {parameters:params});
}

function hist_textReplyVlogMsg( mId ){
	var cObj = new Object();
	cObj.mid = mId; 
	cObj.link_clicked = 'text_reply_vlog';
	hist_CheckAdd('hist_textReplyVlogMsg', 'reply', cObj );
	mess_textReplyInboxMsg( mId );
}

function mess_textReplyVlogMsg( mId ){
	aj_MarkLoading('replylogMsg_'+mId, 'Loading...');
	new Ajax.Updater('replyVlogMsg'+mId, curPath+'handler.php', {parameters: {'event':'postvlogreply', 'mid':mId}, evalScript: true});
}

function hist_textReplyInboxMsg( mId ){
	var cObj = new Object();
	cObj.mid = mId; 
	cObj.link_clicked = 'text_reply_inbox';
	hist_CheckAdd('hist_textReplyInboxMsg', 'reply', cObj );
	mess_textReplyInboxMsg( mId );
}

function mess_textReplyInboxMsg( mId ){
	aj_MarkLoadingBlack('replyInboxMsg', 'Loading...');
	new Ajax.Updater('replyInboxMsg', curPath+'handler.php', {parameters: {'event':'forminboxreply', 'mid':mId}, evalScript: true});
}

function mess_postReplyInboxMsg(){
	var mid = $F('messageId');
	var bodyMsg = $F('replyBodyText');
	aj_MarkLoadingBlack('loginDetails', 'Saving video message...');
	new Ajax.Updater("loginDetails", curPath+"handler.php", {
		 parameters: { 'event':'postinboxreply', 'sub':'text', 'bodyMsg':bodyMsg, 'mid':mid } , evalScripts: true});
	
}

function hist_displayMsg( mId ){
	var cObj = new Object();
	cObj.mid = mId;
	cObj.link_clicked = 'display_message';
	hist_CheckAdd('hist_displayMsg','message'+mId, cObj);
	mess_displayMsg( mId );
}

function mess_displayMsg( mId ){
	aj_MarkLoadingBlack('loginDetails', 'Loading...');
	new Ajax.Updater('loginDetails', curPath+'handler.php', {parameters: {'event':'showmailmsg', 'mid':mId}, evalScripts: true});
}
function hist_displaySentMsg( mId ){
	var cObj = new Object();
	cObj.mid = mId;
	cObj.link_clicked = 'display_sent_message';
	hist_CheckAdd('hist_displaySentMsg','message'+mId, cObj);
	mess_displaySentMsg( mId );
}

function mess_displaySentMsg( mId ){
	aj_MarkLoadingBlack('loginDetails', 'Loading...');
	new Ajax.Updater('loginDetails', curPath+'handler.php', {parameters: {'event':'showsentmailmsg', 'mid':mId}, evalScripts: true});
}

function hist_LoadFolder( folderName ){
	var cObj = new Object();
	cObj.folderName = folderName;
	cObj.link_clicked = 'load_folder';
	hist_CheckAdd('hist_LoadFolder', folderName, cObj );
	mess_LoadFolder( folderName );
}
function mess_LoadFolder( folderName ){
	if (folderName == 'none'){
		return;
	}
	if (folderName == -1){
		folderName = $F('viewFolderOption');
	}
	aj_MarkLoadingBlack('loginDetails', 'Loading...');
	new Ajax.Updater('loginDetails', curPath+'handler.php', {parameters: {'event':'viewfolder', 'forFolder':folderName}} );
}

function hist_LoadSent(){
	hist_CheckAdd('hist_LoadSent','sent', 'hist_LoadSent' );
	mess_LoadSent();
}
function mess_LoadSent(){

	aj_MarkLoadingBlack('loginDetails', 'Loading...');
	new Ajax.Updater('loginDetails', curPath+'handler.php', {
					 parameters: {'event':'viewsent'} } );	
}

function mess_togAllChecked( cbox ){
	var val = false;
	var i = 0;
	var chk = Form.getInputs('inboxForm', 'checkbox', 'selectMsg');

	if ($(cbox).checked == true ){
		val = true;
	}
	
	for (i = 0; i <  chk.length; i++){
		chk[i].checked = val;
	}
	mess_togAllSentChecked( cbox );
}

function mess_togAllSentChecked( cbox ){
	var val = false;
	var i = 0;
	var chk = Form.getInputs('inboxForm', 'checkbox', 'selectSentMsg');

	if ($(cbox).checked == true ){
		val = true;
	}
	
	for (i = 0; i <  chk.length; i++){
		chk[i].checked = val;
	}
}

function mess_notify( msg ){
	$('inboxmessagealerts').innerHTML = msg;
}

function mess_moveChecked( to, from ){
	if (!to){
		to = 'inbox';
	}
	if (!from){
		from = 'inbox';
	}
	
	var chk = Form.getInputs('inboxForm', 'checkbox', 'selectMsg');
	var chkS = Form.getInputs('inboxForm', 'checkbox', 'selectSentMsg');

	// received messages
	var i = 0;
	var idstr = "";
	for (i = 0; i < chk.length; i++){
		if (chk[i].checked == true){
			idstr = idstr+", "+chk[i].value;
		}
	}

	// sent messages
	var i = 0;
	var idstrS = "";
	for (i = 0; i < chkS.length; i++){
		if (chkS[i].checked == true){
			idstrS = idstrS+", "+chkS[i].value;
		}
	}	
	
	
	if (idstr == "" && idstrS == ""){
		mess_notify("You did not select any messages to move.");
		return;
	}

	aj_MarkLoadingBlack('loginDetails', 'Saving...');
	new Ajax.Updater('loginDetails', curPath+'handler.php', {parameters: {'event':'moveMessages', 'sids':idstrS, 'mids':idstr, 'toFolder':to, 'fromFolder':from} });
}

function mess_moveSingle( mid, to, from ){
	if (!from)
		from = 'inbox';
		
	aj_MarkLoadingBlack('loginDetails', 'Saving...');
	new Ajax.Updater('loginDetails', curPath+'handler.php', {parameters: {'event':'moveMessages', 'sids':mid, 'mids':mid, 'toFolder':to, 'fromFolder':from}});
}