var infoInterval;
var timerInterval;

var currentTime = new Date();
var tempTime = new Date();

/* Get New Messages */
function loadChatRoom() {
	// if update isnt turned off update chat window
	if (chatRoom != '') {
		PostPage('chatroom_engine.asp?Time='+currentTime+'&FirtLoad=True&Room=' + chatRoom);

		//infoInterval = setInterval(getInformation, 1500);
		timerInterval = setInterval(startTimer, 900);
	} else {
		alert('Gireceginiz Odayi Secmediniz. Tekrar Giris yapin');

		document.location='login.asp';
	}
}

/* Pop Up Window Function */
function openWin(theURL,winName,features) {
  	window.open(theURL,winName,features);
}

/* Timer Function */
function startTimer() {
	currentTime = new Date();

	//window.status = currentTime;

	//Calculate the last loaded time
	var newTime = currentTime.getSeconds() - tempTime.getSeconds();

	//var chatBoxHTML = document.getElementById("chatBox");
	//chatBoxHTML.innerHTML += '<br>' + newTime;

	//If last loaded time is not a positive number update
	if (newTime<0) {
		newTime = new Date();
		newTime = newTime.getSeconds();
	}

	//if (newTime==10 || newTime == 16 || newTime == 22 || newTime == 26) getInformation();

	//If no update has been received in 30 seconds consider users connection failed and warn
	if (newTime>20) {
		getInformation();
	}

	if (newTime>30) {
		timerInterval = clearInterval(timerInterval);
		updateInformation = false;
		disconnected(newTime);
	}
}

/* Disconnected Function */
function disconnected(newTime) {
	updateInformation = false;

	alert('Baglanti zaman asimi oldu.Tekrar giris yapin.\n\nError Code: ' + newTime);

	document.location='login.asp';
}

/* Send Message Function */
function SendMessage() {
	var message = document.getElementById('txtMessage');
	var format = document.getElementById('txtFormat');
	var color = document.getElementById('txtColor');

	var format = format.options[format.selectedIndex].value
	var color = color.options[color.selectedIndex].value

	if (format==null) format = '';
	if (color==null) color = '';

	if (message!=null)
		PostPage('send_message.asp?Time='+currentTime+'&txtMessage=' + message.value + '&txtFormat='+format+'&txtColor='+color);

	return false;
}

/* Set Chatroom */
function changeRoom(strRoom) {
	//var chatBoxHTML = document.getElementById("chatBox");
	//chatBoxHTML.innerHTML = '';

	toggleBox('changingroom');

	chatRoom = strRoom;
}

/* Get New Messages */
function getInformation() {
	infoInterval = clearInterval(infoInterval);

	// if update isnt turned off update chat window
	if (updateInformation)
		PostPage('chatroom_engine.asp?Time='+currentTime+'&Room=' + chatRoom);
}

/* Set Username */
function updateUsername(strNewUsername, blnChangeUsername) {
	// stop chat update
	//updateInformation = false;

	// if username not null, post to username page, or go to default page
	if (newUsername != null) {
		PostPage('javascript_login.asp?txtUsername=' + newUsername + '&newName=' + blnChangeUsername);
	} else {
		document.location="./";
	}
}

/* function used for remote scripting */
function PostPage(strPage) {

	var header = document.getElementsByTagName("head").item(0);
	var scriptTag = document.getElementById('loadScript');

	// if 'loadScript' already exists - remove it
	if (scriptTag) header.removeChild(scriptTag);

	// create a new element by the name of script
	script = document.createElement('script');
	script.src = strPage;
	script.type = 'text/javascript';
	script.id = 'loadScript';
	//script.defer = true;

	// move the element script into the headheader
	header.appendChild(script);
}

/* Scroll Message Window */
function toBottom(scrollValue) {
	document.getElementById("chatBox").scrollTop=document.getElementById("chatBox").scrollHeight;
}

/* Togles Hidden Boxs */
function toggleBox(objDiv) {
	var objBox = document.getElementById(objDiv);

	if (objBox.style.display == "none")
		objBox.style.display = "";
	else
		objBox.style.display = "none";
}

/* Pops Up Window */
function LogoutWindow(w,h) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	window.open('logout.asp?Room=' + chatRoom, 'ASPChatNowLogout', 'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=700,height=500,top='+wint+',left='+winl)
}

/* Plays Message Sound */
function NewMSGSound() {
	soundManager.play('newMSG')
}

/* Insert Text into Message Field */
function insertText(strText) {
 	var messageBox = document.frmAddMessage.txtMessage;

	messageBox.value = strText;
	messageBox.focus();
}

/* clear command */
function clearWindow() {
	var chatBoxHTML = document.getElementById("chatBox");
	chatBoxHTML.innerHTML = '';
}

function submitMessage(strText) {
 	var objForm = document.frmAddMessage;

	objForm.txtMessage.value = strText;
	objForm.txtMessage.focus();
	objForm.submit();
}

function AddSmileyIcon(iconCode) {
 	var messageBox = document.frmAddMessage.txtMessage;

	messageBox.value  += iconCode;
	messageBox.focus();

	toggleBox('emoticons');
}

/* Change Status Option */
function changeStatus(objForm) {
	var userStatus = objForm.options[objForm.selectedIndex].value;

	if (userStatus == 'Custom')
		userStatus = prompt("Durumum ne olsun:", '');

	if (userStatus != null)
		PostPage('status.asp?txtStatus=' + userStatus);
}

/* Changes Sound Volumne */
function changeVolume(objForm) {
	var userVolume = objForm.options[objForm.selectedIndex].value;

	soundManager.setVolume('newMSG',userVolume);
}

/* Kicker User */
function kickUser() {
	alert('Hade Yallah!');

	document.location='login.asp';
}

/* Ban User */
function banUser() {
	alert('Banlandiniz! Hade Yallah');

	document.location='login.asp';
}

function RoomIn(menItem) {
	menItem.style.backgroundColor='#D5D5D5';
}

function RoomOut(menItem) {
	menItem.style.backgroundColor='#FFFFFF';
}