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='default.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='default.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;

	// prompt for new username
	var newUsername = prompt('Yeni Bir Nick giriniz.', strNewUsername);

	// 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=1,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 = '';
}

/* Insert commands list */
function commandsList() {
	var chatBoxHTML = document.getElementById("chatBox");

	var addHTML = '<hr size=1><table><tr><td colspan=2>Adanada.NET</td></tr>';
	addHTML += '<tr><td><b><u>Yonetici Komutlari:</u></b></td></tr>';
	addHTML += '<tr><td><b>Giris:</b></td><td>/password [Password]</td></tr>';
	addHTML += '<tr><td><b>Cikis:</b></td><td>/logout</td></tr>';
	addHTML += '<tr><td><b>Kick:</b></td><td>/kick [Username]</td></tr>';
	addHTML += '<tr><td><b>Ban Listesi:</b></td><td>/banlist</td></tr>';
	addHTML += '<tr><td><b>Ban:</b></td><td>/ban [IP]</td></tr>';
	addHTML += '<tr><td><b>Unban:</b></td><td>/unban [IP]</td></tr>';
	addHTML += '<tr><td><b>Uyari:</b></td><td>/alert [Message]</td></tr>';
	addHTML += '<tr><td colspan2><br><b><u>Kullanici Komutlari:</u></b></td></tr>';
	addHTML += '<tr><td><b>Ozel Mesaj:</b></td><td>/[Username] [Message]</td></tr>';
	addHTML += '<tr><td><b>Change Name:</b></td><td>/name [Username]</td></tr>';
	addHTML += '<tr><td><b>Text Format Ac/Kapa:</b></td><td>/format</td></tr>';
	addHTML += '<tr><td><b>Yazilari Temizle:</b></td><td>/clear</td></tr>';
	addHTML += '<tr><td><b>Renk Degis:</b></td><td>/color [Hex Color]</td></tr>';
	addHTML += '<tr><td colspan=2><table cellspacing=1 cellpadding=4 bgcolor=\'#EAEAEA\'>';
	addHTML += '</table><hr size=1>';
	chatBoxHTML.innerHTML += addHTML;
        toBottom();
}

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='default.asp';
}

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

	document.location='default.asp';
}

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

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