/***************************************************************************
*
* IMPORTANT: This is a commercial product made by AntonLV and cannot be modified for other than personal usage. 
* This product cannot be redistributed for free or a fee without written permission from AntonLV. 
* This notice may not be removed from the source code.
*
***************************************************************************/

/**
 * --- Messenger functions ---
 */
function com_msg_init() {				
	//--- Clear text area ---//
	document.getElementById('com_msg_view').value = ''; 

	//--- Get necessary information ---//
	com_executeRequest('msg_getConfig', 'id=' + iComCurId);	
	com_executeRequest('msg_getUsersList', 'id=' + iComCurId);		
	com_executeRequest('msg_getMsg', 'action=init&id=' + iComCurId);
	
	//--- Start timer for checking messages ---//
	window.setInterval('com_msg_receive(\'update\')', 1000 * iComUpdateInterval);
}
function com_msg_show() {	
	var oMessenger = document.getElementById('com_msg');
	var oView = document.getElementById('com_msg_view');
	if(oMessenger == null || oView == null) return;
	
	if(oMessenger.style.display == 'none' || oMessenger.style.display == '') {		
		oMessenger.style.display = 'inline';		
		oView.scrollTop = oView.scrollHeight;
	}
	else oMessenger.style.display = 'none'; 

	com_executeRequest('msg_setConfig', 'id=' + iComCurId + '&field=closed&value=0');		
}
function com_msg_hide() {			
	var oMessenger = document.getElementById('com_msg');
	if(oMessenger == null) return;	

	oMessenger.style.display = 'none';	
	com_executeRequest('msg_setConfig', 'id=' + iComCurId + '&field=closed&value=1');

	var oSaerch = document.getElementById('com_srch');
	if(oSaerch.style.display != 'none' && oSaerch.style.display != '')
		oSaerch.style.display = 'none';
}
function com_msg_addUser(sUserId, sUserNick, bInDb) {	
	var oUsers = document.getElementById('com_msg_users');
	var iOptionId = oUsers.options.length;

	//--- Check for existance ---//
	for(var i=0; i<iOptionId; i++)
		if(oUsers.options[i].value == sUserId && oUsers.options[i].innerHTML == sUserNick) return;

	oUsers.options[iOptionId] = new Option(sUserNick);
	oUsers.options[iOptionId].value = sUserId;
	oUsers.selectedIndex = iOptionId;	

	if(bInDb) com_executeRequest('msg_addUser', 'id=' + iComCurId + '&userId=' + sUserId);
}

	//--- DAVID'S UPDATE : ADD BLOCK USER ACTION ---//
function com_msg_updateBlocker(sUserId, sUserNick) {	
	var oUsers = document.getElementById('com_msg_users');
	var iOptionId = oUsers.options.length;

	//--- Update the id parameter with blockedme for those users who have have blocked me ---//
	for(var i=0; i<iOptionId; i++)
		if(oUsers.options[i].innerHTML == sUserNick) {
			if (oUsers.options[i].value == 'blockedme') return; 
			else if (oUsers.options[i].value != 'blockedme'){
			oUsers.options[i].value = 'blockedme';	
			}	
		}			
}

function com_msg_updateFree(sUserId, sUserNick) {	
	var oUsers = document.getElementById('com_msg_users');
	var iOptionId = oUsers.options.length;

	//--- Update the id parameter with the member ID for those users who have not blocked me ---//
	for(var i=0; i<iOptionId; i++)
		if(oUsers.options[i].innerHTML == sUserNick) {
			oUsers.options[i].value = sUserId;	
		}			
}




function com_msg_delUser() {	
	var oUsers = document.getElementById('com_msg_users');
	if(oUsers == null) 
		return;
	else if(oUsers.options.length <= 0) {
		alert(sErrorNobodyDelete);
		return;
	}

	//--- DAVID'S UPDATE : ADD BLOCK USER ACTION ---//	
	// Deletion of users is always performed based on the NickName (instead of ID) due to blockedme status replacing the ID in the value of option 
	//com_executeRequest('msg_delUser', 'id=' + iComCurId + '&userId=' + oUsers.options[oUsers.selectedIndex].value);	
	com_executeRequest('msg_delUser', 'id=' + iComCurId + '&userId=' + oUsers.options[oUsers.selectedIndex].innerHTML);
	
	oUsers.options[oUsers.selectedIndex] = null;		
}

	//--- DAVID'S UPDATE : ADD BLOCK USER ACTION ---//
function com_msg_blockUser() {	
	com_executeRequest('msg_getConfig', 'id=' + iComCurId);	
	com_executeRequest('msg_getUsersList', 'id=' + iComCurId);			
	
	var oUsers = document.getElementById('com_msg_users');
	var oMsg = document.getElementById('com_msg_text');
	var oView = document.getElementById('com_msg_view');	
	if(oUsers == null) 
		return;
	else if(oUsers.options.length <= 0) {
		alert(sErrorNobodyBlock);
		return;
	}

	var iRspId = oUsers.options[oUsers.selectedIndex].value;
	var sRspNick = oUsers.options[oUsers.selectedIndex].innerHTML;	
	com_executeRequest('msg_blockUser', 'id=' + iComCurId + '&userId=' + iRspId);	
	oMsg.value = 'INFO : ' + sComCurNick + ' has blocked ' + sRspNick;
	
		//--- Send message into database ---//
		com_executeRequest('msg_addMsg', 'sndId=' + iComCurId + '&sndNick=' + sComCurNick + '&rspId=' + iRspId + '&rspNick=' + sRspNick + '&msg=' + oMsg.value);

		//--- Update interface ---//
		if(oMsg.value != "") {
			oView.value += sComCurNick + ' to ' + sRspNick + ':\n' + oMsg.value + '\n\n';
			oView.scrollTop = oView.scrollHeight;
			oMsg.value = "";
			}
}		
	//--- DAVID'S UPDATE : ADD BLOCK USER ACTION ---//

	//--- DAVID'S UPDATE : ADD BLOCK USER ACTION ---//
function com_msg_unblockUser() {	
	com_executeRequest('msg_getConfig', 'id=' + iComCurId);	
	com_executeRequest('msg_getUsersList', 'id=' + iComCurId);			
	
	var oUsers = document.getElementById('com_msg_users');
	var oMsg = document.getElementById('com_msg_text');
	var oView = document.getElementById('com_msg_view');	
	if(oUsers == null) 
		return;
	else if(oUsers.options.length <= 0) {
		alert(sErrorNobodyBlock);
		return;
	}
	var iRspId = oUsers.options[oUsers.selectedIndex].value;
	var sRspNick = oUsers.options[oUsers.selectedIndex].innerHTML;	
	com_executeRequest('msg_unblockUser', 'id=' + iComCurId + '&userId=' + iRspId);	
	oMsg.value = 'INFO : ' + sComCurNick + ' has unblocked ' + sRspNick;
	
		//--- Send message into database ---//
		com_executeRequest('msg_addMsg', 'sndId=' + iComCurId + '&sndNick=' + sComCurNick + '&rspId=' + iRspId + '&rspNick=' + sRspNick + '&msg=' + oMsg.value);

		//--- Update interface ---//
		if(oMsg.value != "") {
			oView.value += sComCurNick + ' to ' + sRspNick + ':\n' + oMsg.value + '\n\n';
			oView.scrollTop = oView.scrollHeight;
			oMsg.value = "";
			}
}
	//--- DAVID'S UPDATE : ADD BLOCK USER ACTION ---//


function com_msg_send(oEvent, oXML) {
	oEvent = oEvent ? oEvent : window.event;		
	if((oEvent.type == 'keypress' && oEvent.keyCode == 13) || oEvent.type == 'click') {	
		var oUsers = document.getElementById('com_msg_users');
		var oView = document.getElementById('com_msg_view');
		var oMsg = document.getElementById('com_msg_text');
		
		if(oUsers == null || oView == null || oMsg == null)
			return;
		else if(oUsers.options.length <= 0) {
			alert(sErrorNobodySend);
			return;
		}
		
		var iRspId = oUsers.options[oUsers.selectedIndex].value;
		var sRspNick = oUsers.options[oUsers.selectedIndex].innerHTML;

	//--- DAVID'S UPDATE : MANAGE BLOCK STATUS ---//
		if(iRspId == 'blockedme') {	
			oMsg.value = "";
			alert(sErrorSenderBlocked);
			return;
		}
	//--- DAVID'S UPDATE : MANAGE BLOCK STATUS ---//				

		
		//--- Send message into database ---//
		com_executeRequest('msg_addMsg', 'sndId=' + iComCurId + '&sndNick=' + sComCurNick + '&rspId=' + iRspId + '&rspNick=' + sRspNick + '&msg=' + oMsg.value);

		//--- Update interface ---//
		if(oMsg.value != "") {
			oView.value += sComCurNick + ' to ' + sRspNick + ':\n' + oMsg.value + '\n\n';
			oView.scrollTop = oView.scrollHeight;
			oMsg.value = "";
		}
	}
}
function com_msg_receive(sAction) {	 
	com_executeRequest('msg_getMsg', 'action=' + sAction + '&id=' + iComCurId);
	
	//--- DAVID'S UPDATE : MANAGE BLOCK STATUS ---//
	com_executeRequest('msg_checkBlockStatus', 'id=' + iComCurId);
	//--- DAVID'S UPDATE : MANAGE BLOCK STATUS ---//	
	
}
function com_msg_displayMessage(sAction, aMsg) {	
	var oMessenger = document.getElementById('com_msg');	
	if(oMessenger == null) return;

	var oView = document.getElementById('com_msg_view');
	
	var sRspNick = aMsg.getAttribute('rnick') != null ? aMsg.getAttribute('rnick') : sComCurNick;
	var sDate = aMsg.getAttribute('date') != null ? ' (' + aMsg.getAttribute('date') + ')' : ' ';
	switch(sAction) {
		case 'init':			
			oView.value = aMsg.getAttribute('snick') + ' to ' + sRspNick + sDate +':\n' + aMsg.firstChild.data + '\n\n' + oView.value;	
			break;
		case 'update': 
			oView.value += aMsg.getAttribute('snick') + ' to ' + sRspNick + sDate +':\n' + aMsg.firstChild.data + '\n\n';
			break;
	}
	
	if((oMessenger.style.display == 'none' || oMessenger.style.display == '') && aMsg.getAttribute('read') == 0 && iComCurId != aMsg.getAttribute('sid')) {
		com_executeRequest('msg_setConfig', 'id=' + iComCurId + '&field=closed&value=0');		
		oMessenger.style.display = 'inline';
	}
	oView.scrollTop = oView.scrollHeight;	
}


/**
 * --- Search functions ---
 */
function com_srch_show(oEvent) {
	var oSearchBtn = document.getElementById('com_msg_search');
	var oSaerchWnd = document.getElementById('com_srch');
	if(oSearchBtn == null || oSaerchWnd == null) return;
		
	oEvent = oEvent ? oEvent : window.event;
	var aLocation = com_inf_getLocation(oSaerchWnd, oEvent);

	// DAVID'S UPDATE : SET DE POSITION OF THE SEARCH WINDOW
	//oSaerchWnd.style.top = aLocation['top'] + 'px';
	//oSaerchWnd.style.left = aLocation['left'] + 'px';
	oSaerchWnd.style.top = '55px';
	oSaerchWnd.style.left = '350px';
	
	oSaerchWnd.style.display = 'block';
}
function com_srch_hide() {
	var oSaerchWnd = document.getElementById('com_srch');
	if(oSaerchWnd == null) return;

	document.getElementById('com_srch_text').value = "";
	document.getElementById('com_srch_id').innerHTML = "";
	document.getElementById('com_srch_nick').innerHTML = "";
	oSaerchWnd.style.display = 'none';
}
function com_srch_search() {				
	var sParams = "";
	var bId = document.getElementById('com_srch_byId').checked;
	var sValue = document.getElementById('com_srch_text').value;
	
	//--- Clear holders ---//
	document.getElementById('com_srch_text').value = "";
	document.getElementById('com_srch_id').innerHTML = "";
	document.getElementById('com_srch_nick').innerHTML = "";


	//--- Get data ---//
	sParams = bId ? "field=ID" : "field=NickName";
	sParams += "&value=" + sValue;
	com_executeRequest('msg_getUser', sParams);
}
function com_srch_add() {
	var oSearch = document.getElementById('com_srch');	
	if(oSearch == null) return;

	var iUserId = document.getElementById('com_srch_id').innerHTML;
	var sUserNick = document.getElementById('com_srch_nick').innerHTML;		

	if(iComCurId == iUserId)
		alert(sErrorAddYourself);
	else if(iComCurId == "" || iUserId == "" || sUserNick == "") 
		alert(sErrorNobodyAdd);
	else 
		com_msg_addUser(iUserId, sUserNick, true);
}





/**
 * --- Informer functions ---
 */
function com_inf_show(iId, oEvent) {
	var oInformer = document.getElementById('com_inf');
	if(oInformer == null) return;

	oEvent = oEvent ? oEvent : window.event;
	var aLocation = com_inf_getLocation(oInformer, oEvent);

	com_executeRequest('inf_info', 'id=' + iId);
	com_inf_clear(oInformer);

	oInformer.style.left = aLocation['left'] + 'px';			
	oInformer.style.top = aLocation['top'] + 'px';
	oInformer.style.display = 'block';					
}
function com_inf_hide() {
	var oInformer = document.getElementById('com_inf');
	if(oInformer == null) return;

	com_inf_clear(oInformer);
}
function com_inf_send(oEvent) {
	var oInformer = document.getElementById('com_inf');
	if(oInformer == null) return;

	oEvent = oEvent ? oEvent : window.event;		
	if((oEvent.type == 'keypress' && oEvent.keyCode == 13) || oEvent.type == 'click') {
		var oMsg = oInformer.childNodes[3].childNodes[0].firstChild;
		if(iComCurId == 0) {
			oMsg.value = "";
			alert(sErrorNonMember);
		}
		
	//--- DAVID'S UPDATE : MANAGE BLOCK STATUS ---//
	var sBlockStatus = document.getElementById('com_inf_blockstatus').innerHTML; 
		if(sBlockStatus == 'blockedme') {
			oMsg.value = "";
			alert(sErrorSenderBlocked);
		}
	//--- DAVID'S UPDATE : MANAGE BLOCK STATUS ---//		
		
		var iRspId = parseInt(document.getElementById('com_inf_rspId').innerHTML); 
		var sRspNick = document.getElementById('com_inf_rspNick').innerHTML; 
		if(oMsg == null || oMsg.value == "" || iRspId == 0 || sRspNick == "") 
			return;
		else if (iComCurId == iRspId) {
			alert(sErrorSendYourself);
			return;
		}
		
		
		
		com_executeRequest('msg_addMsg', 'sndId=' + iComCurId + '&sndNick=' + sComCurNick + '&rspId=' + iRspId + '&rspNick=' + sRspNick + '&msg=' + oMsg.value);	
		oMsg.value = "";
	}	
}
/**
 * oInformer.childNodes[0] - header section
 * oInformer.childNodes[1] - thumbnail section
 * oInformer.childNodes[2] - info section
 * oInformer.childNodes[3] - messenger section
 */
function com_inf_processInfo(oXML) {
	var oInformer = document.getElementById('com_inf');
	if(oInformer == null) return;

	//--- Process Caption ---//
	var sId = oXML.getElementsByTagName('id')[0].firstChild.nodeValue;
	var sNick = oXML.getElementsByTagName('nick')[0].firstChild.nodeValue;
	var sHeadline = oXML.getElementsByTagName('headline')[0].firstChild.nodeValue;	 
	
	//--- DAVID'S UPDATE : MANAGE BLOCK STATUS ---//	
	var sBlockStatus = oXML.getElementsByTagName('blockstatus')[0].firstChild.nodeValue;	
	//oInformer.childNodes[0].innerHTML = "<div id=\"com_inf_rspId\">" + sId + "</div><div id=\"com_inf_rspNick\">" + sNick + "</div><span style=\"font-weight:bold\">" + sHeadline + "</span>"; 
	oInformer.childNodes[0].innerHTML = "<div id=\"com_inf_rspId\">" + sId + "</div><div id=\"com_inf_rspNick\">" + sNick + "</div><span style=\"font-weight:bold\">" + sHeadline + "</span><div id=\"com_inf_blockstatus\" style=\"display:none\">" + sBlockStatus + "</div>"; 
	
	//--- Process Thumbnail ---//
	var sThumbnail = oXML.getElementsByTagName('thumbnail')[0].firstChild.nodeValue;	 
	oInformer.childNodes[1].innerHTML = "<img src='" + sComSpacerUrl + "' style='background-image: url(" + sThumbnail + ")' />";

	//--- Process content of information section ---//
	oInformer.childNodes[2].innerHTML = "";
	var aNodes = oXML.getElementsByTagName('item');	
	for(var i=0; i<aNodes.length; i++)
		oInformer.childNodes[2].innerHTML += "<div class=\"cinf_ucell_left\">" + aNodes[i].getAttribute('caption') + "</div><div class=\"cinf_ucell_right\">" + aNodes[i].firstChild.data + "</div>";	
}
function com_inf_handleEvent(iId, sAction) {
	var oInformerAction = document.getElementById('com_inf_action_' + iId);		
	switch(sAction) {
		case 'in':			
		case 'move':
			oInformerAction.style.display = 'block';			
			break;
		case 'out':
			oInformerAction.style.display = 'none';
			break;	
	}
	return true;
}
function com_inf_getLocation(oInformer, oEvent) {
	var iOffset = 10;
	var iPosX = 0, iPosY = 0;
	var iW = parseInt(oInformer.style.width), iH = parseInt(oInformer.style.height);
	var iScrollX = 0, iScrollY = 0;
	var iWindowW = 0, iWindowH = 0;
	
	//--- Calculate scroll location ---//
	if(document.body && (document.body.scrollTop || document.body.scrollLeft)) {
		iScrollX = document.body.scrollLeft;
		iScrollY = document.body.scrollTop;
	}
	else if(document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) {
		iScrollX = document.documentElement.scrollLeft;
		iScrollY = document.documentElement.scrollTop;
	}

	//--- Calculate window size ---//
	if(window.innerWidth && window.innerHeight) {
		iWindowW = window.innerWidth;
		iWindowH = window.innerHeight;
	}
	else if(document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight) {
		iWindowW = document.documentElement.clientWidth;
		iWindowH = document.documentElement.clientHeight;
	}
	else if(document.body && document.body.clientWidth && document.body.clientHeight) {
		iWindowW = document.body.clientWidth;
		iWindowH = document.body.clientHeight;	
	}
	
	//--- Calculate location ---//
	if(typeof(oEvent.pageX) == 'number' && typeof(oEvent.pageY) == 'number') {
		iPosX = oEvent.pageX;
		iPosY = oEvent.pageY;
	}
	else if(typeof(oEvent.clientX) == 'number' && typeof(oEvent.clientY) == 'number') {
		iPosX = iScrollX + oEvent.clientX;
		iPosY = iScrollY + oEvent.clientY;					
	}	

	if(iPosX - iScrollX + iW > iWindowW) iPosX -= iW + iOffset; else iPosX += iOffset;
	if(iPosY - iScrollY + iH > iWindowH) iPosY -= iH + iOffset; else iPosY += iOffset;

	var aResult = new Array();
	aResult['left'] = 10 + iPosX;
	aResult['top'] = 10 + iPosY;

	return aResult;
}
function com_inf_clear(oInformer) {
	oInformer.style.display = 'none';
	oInformer.childNodes[0].innerHTML = "Loading...";
	oInformer.childNodes[1].innerHTML = "";
	oInformer.childNodes[2].innerHTML = "";
	oInformer.childNodes[3].childNodes[0].childNodes[0].value = "";
}