﻿/*
 * functions for public chat
 */

// variables:
var publicChatMsgsTimer;
var lastSaidMsgTime = 0;
var lastReceivedMsgId = -1;
var isFF = navigator.userAgent.indexOf("Firefox") != -1;
var maxMsgsToDisplay = 1000, msgsDisplayed = 0;

// controls:
var txt_TextToSay;
var div_ChatMessages;
var win_bigPublicChat;

/*
 * start and stop the chat
 */
// starts the chat
function StartPublicChat() {
	// hide the loading message
	if (tbl_chatLoading !=null) {
		tbl_chatLoading.style.display = 'none';
		document.getElementById('tbl_chat').style.display = 'block';
	}
	
	txt_TextToSay = document.getElementById('txt_TextToSay');
	div_ChatMessages = document.getElementById('div_ChatMessages');

	txt_TextToSay.focus();
	
	// start the chat
	publicChatMsgsTimer = setTimeout("GetNewMessages()",messagesRefreshInterval*1000);
}

// stops the chat
function StopPublicChat() {
	// stop the chat
	clearTimeout(publicChatMsgsTimer);
}

/*
 * messages
 */
// say something to the room
function SaySomething() {
	// make sure user is logged in
	if (!isLoggedIn) {
		alert('.רק משתמשים רשומים יכולים להשתתף בצ\'אט. כנס עם שם משתמש וסיסמתך, או בצע הרשמה כמנוי');
		txt_TextToSay.value = "";
		return false;
	}
	
	// make sure user is not banned
	if (isBanned) {
		alert('בעקבות שימוש שאינו הולם את תקנון האתר, נחסמת לכתיבה בצ\'אט.');
		txt_TextToSay.value = "";
		return false;
	}	

	// get text
	var text = '';
	
	// strip html tags
	text = txt_TextToSay.value.replace(/(<([^>]+)>)/ig,"");
	
	// validate the text
	if (text == '') return;
	
	// block links?
	if (blockLinks)
		if ( (text.indexOf('http:') > -1 || text.indexOf('www.') > -1 || text.indexOf('co.il') > -1)) {
			alert('אין אפשרות לפרסם קישורים לאתרים חיצוניים');
			return;			
		}
		
	// check if enough time has passed
	var now = (new Date()).getTime();
	if (now - lastSaidMsgTime < timeBetweenPosts * 1000) {
		alert('על מנת למנוע הצפת הצ\'אט, יש להמתין מספר שניות לפני שליחת הודעות ברצף');
		return;
	}
	lastSaidMsgTime = now;
	
	var roomId = ddl_RoomsList.options[ddl_RoomsList.selectedIndex].value;
	AjaxObj.Say(text, roomId);
	
	txt_TextToSay.value = "";
}

// get what the other are saying
function GetNewMessages() {
	var text = txt_TextToSay.value;
	var roomId = ddl_RoomsList.options[ddl_RoomsList.selectedIndex].value;
	var response = AjaxObj.GetNewMessages(roomId, lastReceivedMsgId);
	
	if (response!=null && response.value!=null) {
		lastReceivedMsgId = response.value[1];
		ShowMessages(response.value[0]);
	}
	
	response = null;
	
	// restart the chat timer
	publicChatMsgsTimer = setTimeout("GetNewMessages()",messagesRefreshInterval*1000);
}

// change the room
function ChangeRoom() {
	var roomName = ddl_RoomsList.options[ddl_RoomsList.selectedIndex].text;
	var text = "<span style='color:red;'>הצטרפת לחדר "+roomName+"</span><br/>";
	ShowMessages(text);
	lastReceivedMsgId = 0;
}

// show messages
function ShowMessages(text) {
	if (text==null || text.length == 0)
	    return;

    // to prevent over-expanding client memory usage
	msgsDisplayed++;
	if (msgsDisplayed > maxMsgsToDisplay)
	    location.reload();
	
	div_ChatMessages.innerHTML += text;
	div_ChatMessages.scrollTop = div_ChatMessages.scrollHeight;
}

// on key press for txt_TextToSay
function KeyPress(event) {
	if (event.keyCode==13) {
		SaySomething(); 
		return false;
	}
	
	// limit message length
	if (txt_TextToSay.value.length > maxMessageLength)
		return false;
}

// returns the string stripped of html tags
function GetStrippedString(string) {
	return string.replace(/(<([^>]+)>)/ig,""); 
}

// opens the emoticons window
function PopupEmoticons(appPath) {
	OpenNamePopUp(appPath + 'Web/Popups/Emoticons.aspx','win_emoticons', 250, 200);
}

/*
 * show and hide chat
 */
// shows the small chat
function showSmallChat(whileClosingBig) {
	// if big chat window is open, close it
	if (whileClosingBig==null && win_bigPublicChat!=null && !win_bigPublicChat.closed) {
		win_bigPublicChat.close();
	}
	tbl_topProfile_low.style.display = 'none';
	tbl_topProfile_high.style.display = 'block';
	div_smallChat.style.display = 'block';
	
	// start chat
	StartPublicChat();
	
	// erase cookie
	eraseCookie(chatCookieName);
}

// hides the small chat
function hideSmallChat() {
	tbl_topProfile_low.style.display = 'block';
	tbl_topProfile_high.style.display = 'none';
	div_smallChat.style.display = 'none';
	
	// stop chat
	StopPublicChat();
	
	// save cookie
	createCookie(chatCookieName,"true",365);
}

// show the big chat
function popupBigChat() {
	hideSmallChat();
	win_bigPublicChat = OpenNamePopUp('Web/Popups/BigPublicChat.aspx','win_bigPublicChat', 625, 472);
}

// hide the big chat
function closeBigChat() {
	// stop chat
	StopPublicChat();
	self.close();
}

// hide the big chat
function hideBigChat() {
	closeBigChat();
	opener.showSmallChat(true);
}
