var iTimeoutID = 0;


function AutoRefresh()
{
	iTimeoutID = window.setTimeout("AutoRefresh()",20000);  
	HasChatRequest();
}

function HasChatRequest()
{
	try
	{
		var response = Safecash.Main.ssl.pages.DispatchSSL.HasChatRequest().value;
		if( response >= 0 && (document.getElementById("divChatPane").style.display == 'none' || document.getElementById("TablePhoneNumber").style.display == "block")  )
		{
			ChatPane(response);	
		}
	}
	catch(e){}
}

function ChatPane(IsOpen)
{
	document.getElementById("divChatPane").style.display = "block";
	
	if (IsOpen==1)
	{
		document.getElementById("TableChatBox").style.display = "block";
		document.getElementById("TablePhoneNumber").style.display = "none";
		document.getElementById("divScrollIntoView").scrollIntoView(true);
		ChatAutoRefresh();
		window.clearTimeout(iTimeoutID);
	}
	else
	{
		document.getElementById("TablePhoneNumber").style.display = "block";
		document.getElementById("TableChatBox").style.display = "none";
	}
	//document.getElementById("txtChatMessage").focus();
}

function setSelRange(selStart, selEnd) 
{  
	var inputEl = document.getElementById("txtChatMessage");
	if (inputEl.setSelectionRange) 
	{   
		inputEl.focus();   
		inputEl.setSelectionRange(selStart, selEnd);  
	} 
	else if (inputEl.createTextRange) 
	{   
		var range = inputEl.createTextRange();   
		range.collapse(true);   
		range.moveEnd('character', selEnd);   
		range.moveStart('character', selStart);   
		range.select();  
	} 
}


/* <CHAT BOX> */
var iChatTimeoutID;

function ChatAutoRefresh()
{
	iChatTimeoutID=window.setTimeout("ChatAutoRefresh()",5000); 
	ChatRefreshMessage();
}

function ChatRefreshMessage()
{
	try
	{
		var Result=new Array(3);
		Result=Safecash.Main.ssl.controls.UC_ChatBox.getMessage().value;
		
		var chatBox = document.getElementById("MyControl_UC_ChatBox1_divChatHistory");
		var KeyPressStatus = document.getElementById("spanKeyPressStatus");
		
		
		if (Result[0] != '0' )
		{
			chatBox.innerHTML = chatBox.innerHTML + Result[0];
			document.getElementById("divScrollIntoView").scrollIntoView(true);
		}
			
		if (Result[1] == '0' )
			KeyPressStatus.innerHTML = '';
		else
			KeyPressStatus.innerHTML = '<span class=SC_LabelPinkBold8px>'+Result[1]+'</span> mesaj yazıyor...';
			
		if (Result[2] != "True" )
		{
			window.clearTimeout(iChatTimeoutID);
			KeyPressStatus.innerHTML = 'Karşı taraf oturumu kapattı.';
		}
		
		//document.getElementById("divScrollIntoView").scrollIntoView(true);	
	}
	catch(e){}
}

function alternativeScrollIntoView(parentDiv,elementIntoDiv )
{
	var principal =parentDiv;
	principal.scrollTop = 0;
	var rects = principal.getClientRects()[0];
	var topFinal = rects.top;
	var bottomFinal = rects.bottom;
	var bottomActual = elementIntoDiv.getClientRects()[0].bottom;
	
	if (bottomActual == 0)
	{
		return;
	}
	while(bottomActual>bottomFinal||bottomActual<topFinal)
	{
		var direction="down";
		if(bottomActual<topFinal) direction="up";
		principal.doScroll(direction);
		bottomActual=elementIntoDiv.getClientRects()[0].bottom;
	}
}
		
function sendMessage()
{
	try
	{
		var _message = document.getElementById("txtChatMessage").value.replace(/[\r\n]/g, "");
		Safecash.Main.ssl.controls.UC_ChatBox.sendMessage(_message);
		
		var name = document.getElementById("MyControl_UC_ChatBox1_txtNameForChat").value;
		var message = document.getElementById("txtChatMessage").value;
		var chatBox = document.getElementById("MyControl_UC_ChatBox1_divChatHistory");
		var messageToShow = "<span class=SC_LabelGreen>"+ name +":</span><span class=SC_LabelThin>"+ message +"</span><br>" ;
		
		//Clear message box
		document.getElementById("txtChatMessage").value = '';
		//document.getElementById("txtChatMessage").blur();
		//document.getElementById("txtChatMessage").focus();
		setSelRange(0,0);
		
		chatBox.innerHTML= chatBox.innerHTML + messageToShow;
		document.getElementById("divScrollIntoView").scrollIntoView(true);
	}
	catch(e){}
}
	
function ChatCloseSession()
{
	Safecash.Main.ssl.controls.UC_ChatBox.closeSession();
	document.getElementById("divChatPane").style.display = "none";
	window.clearTimeout(iChatTimeoutID);
	AutoRefresh();
}
			
function sendKeyPressStatus( status )
{
	try
	{	
		if ( status == 0 || document.getElementById("txtChatMessage").value.length == 0 )
			Safecash.Main.ssl.controls.UC_ChatBox.chatKeyPressDecision("0");
		else if (status == 1)
			Safecash.Main.ssl.controls.UC_ChatBox.chatKeyPressDecision("1");
	}
	catch(e){}
}
/* </CHAT BOX> */

/* <ON KEY DOWN> */
function fnOnKeyDown()
{
	try
	{
		if (document.getElementById("divChatPane").style.display == "block" )
		{
			switch (event.keyCode)
			{
				case 13:
					sendMessage();
					setSelRange(0,0);
					break;
			}
		}
	}
	catch(e){}
}
/* </ON KEY DOWN> */

