﻿/*
		This is used specifically by the Chatting module
*/

function SetScrollPosition() {
	var div = document.getElementById('divMessages');
	if(div != null)
		div.scrollTop = 100000000000;
} // SetScrollPosition - Function

function SetToEnd(txtMessage) {
	if(txtMessage.createTextRange) {
		var fieldRange = txtMessage.createTextRange();
		fieldRange.moveStart('character', txtMessage.value.length);
		fieldRange.collapse();
		fieldRange.select();
	} // if we created a TextRange
} // SetToEnd - Function

function ReplaceChars(txtMessage) {
	var txt = txtMessage.value;
	var out = "<"; // replace this
	var add = ""; // with this
	var temp = "" + txt; // temporary holder

	while(temp.indexOf(out) > -1) {
		pos = temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
	} // while stepping through the temporary string

	txtMessage.value = temp;
} // ReplaceChars - Function

function LogOutUser(result, context) {
	// don't do anything here
} // LogOutUser - Function

function LogMeOut() {
	// Use this as follows to automatically Log out the user if they Close the page.
	// <body ... onunload="LogMeOut()">
	LogOutUserCallBack();
} // LogMeOut - Function

function StartChat(ClientPath, UsersID, CSRID) {
	window.open(ClientPath + "CustomerChatPopup.aspx?UserID=" + UsersID + "&CSRID=" + CSRID, UsersID, 'height=240,width=500,location=no,toolbar=no,scrollbars=yes,status=no,menubar=no,resizable=yes', true);
} // StartChat - Function


//
// Asynch Postback via JavaScript functions
//

function GetChatFromServer() {
	if(chatUserID == undefined) {
		alert("Can't determine who the logged in user is!");
		return false;
	} else {
		var html = PartnerShipWeb.GetChat(chatUserID, chatRunAsCSR, GetChatFromServer_Complete, GetChatFromServer_TimeOut, GetChatFromServer_Error);
		return true;
	} // if we have the variable set
} // GetChatFromServer - Function

function GetChatFromServer_Complete(arg) {
	document.getElementById("divMessages").innerHTML = arg;
	SetScrollPosition();
}

function GetChatFromServer_TimeOut(arg) {
	// alert("There was a Timeout getting data from PartnerShipWeb Service: \n" + arg);
}

function GetChatFromServer_Error(arg) {
	alert("There was an Error getting data from the PartnerShipWeb Service: \n" + arg);
}

// Find this in the OnKeyPressed event on the EndUserChat markup
function ChatKeyPressed() {
	if(event.keyCode == 13) {
		// cancel the default submit
		event.returnValue = false;
		event.cancel = true;
		// submit the form by programmatically clicking the specified button
		document.getElementById(chatSendButtonID).click(); // "btnSend"
	} // if the Enter key was pressed
} // ChatKeyPressed - Function

function AppendToChat() {
	var result = PartnerShipWeb.AppendToChat(
			document.getElementById("txtNewMessage").value, chatRunAsCSR, chatUserID, chatCSRID,
			AppendToChat_Complete, AppendToChat_TimeOut, AppendToChat_Error);
	SetScrollPosition();
	return false; // This is so the Server side will not attempt to click
} // AppendToChat - Function

function AppendToChat_Complete(arg) {
	document.getElementById("txtNewMessage").value = "";
	GetChatFromServer();
}

function AppendToChat_TimeOut(arg) {
	// alert("There was a Timeout when attempting to append a message in the PartnerShipWeb Service: \n" + arg);
}

function AppendToChat_Error(arg) {
	alert("There was an Error when attempting to append a message in the PartnerShipWeb Service: \n" + arg);
}

function DisableChatMessageInput(arg) {
    document.getElementById("txtNewMessage").disabled = arg;
}
