$(document).ready(function(){
  $('#lnkAddFriends').click(function () { 
      $('#tblFriendList').fadeIn();
    });	
	
  $('#cmdClose').click(function () { 
      $('#tblFriendList').fadeOut();
    });
});

function delMessages(objForm)
{
	var flag = $('input:checked').length;
	if (flag < 1)
	{
		alert("Please select atleast one message to delete.");
		return false;
	}
	else
	{
		if (confirm("Are you sure you want to delete message(s)?"))
		{				
			objForm.actionType.value = "deleteMessage";
			return true;
		}
		else
		{
			return false;
		}
	}
}

function selAllMessages()
{
	var strType = $('#cmdSelectAll').val();		
	if (strType == "Select None")
	{
		$('input:checkbox').attr("checked","");
		$('#cmdSelectAll').val("Select All");
	}
	else
	{
		$('input:checkbox').attr("checked","checked");
		$('#cmdSelectAll').val("Select None");
	}
}

function submitForm(objForm, strID, strSubject, strType)
{
	objForm.hidID.value = strID;
	objForm.hidSubject.value = strSubject;
	if (strSubject != '')
	{
		objForm.action = HTTP_SERVER + DIR_WS_HTTP + "messages/write";
	}
	objForm.actionType.value = strType;
	objForm.submit();
}

function sendMessage(objForm)
{
	if(trim(objForm.hidTo.value) == '')
	{
		alert("Please select a recipient");
		objForm.txtTo.focus();
		return false;
	}
	else if(trim(objForm.txtSubject.value) == '')
	{
		alert("Please enter the subject");
		objForm.txtSubject.focus();
		return false;
	}
	else if(trim(objForm.txtarMessage.value) == '')
	{
		alert("Please enter the message");
		objForm.txtarMessage.focus();
		return false;
	}
	else
	{
		objForm.actionType.value = "sendMessage";
		return true;
	}
}

function addContacts()
{
	var arrCheckBoxes = $('input:checked');
	if (arrCheckBoxes.length < 1)
	{
		alert("Please select user(s)");
	}
	else
	{
		var strToIDs = '';
		var strToNames = '';
		arrCheckBoxes.each( 
			function() {
				(strToIDs != '') ? strToIDs += ',' + this.value : strToIDs += this.value;
				(strToNames != '') ? strToNames += ',' + this.title : strToNames += this.title;
			} 
		);

		$('#hidTo').val(strToIDs);
		$('#txtTo').val(strToNames);

		$('#tblFriendList').fadeOut();
	}
}

function friendReqAction(userID,status)
{
	$.ajax({
		type: "POST",
		url: HTTP_SERVER+DIR_WS_HTTP+"myProfileAjax.php",
		data: "actionType=FRIENDSHIP_ACTION&strUserID="+userID+"&status="+status,
		success: function(msg){
			if (msg == 1)
			{
				var txt = '<img src="../../images/icons/check.png" width="64" height="64" border="0" alt="" align="absmiddle">User added to your friends list.';
				alertBox(txt, false);
			}
			else if (msg == 2)
			{
				var txt = '<img src="../../images/icons/check.png" width="64" height="64" border="0" alt="" align="absmiddle">Friendship request is declined.';
				alertBox(txt, false);
			}
			else if (msg == 0)
			{
				var txt = '<img src="../../images/icons/check.png" width="64" height="64" border="0" alt="" align="absmiddle">Server error!!! Action can not be performed.';
				alertBox(txt, false);
			}
		}
	});
}