var toggleChat = function(div) {
  var rpcUrl = 'modchatxmlrpc.php';
  var args   = 'action=toggleChat';
  var ajax   = new Ajax.Updater(div, rpcUrl,
    {
      method: 'post',
      postBody: args,
      evalScripts: true
    }
  );
}

var handleEnter = function(txtInput, event) {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13) {
    if (txtInput.value == '') {
      alert("You must enter some text before sending.");
    } else {
      send(txtInput.value);
      txtInput.value = '';
    }
  }

  return false;
}

var send = function(message) {
  var rpcUrl = 'modchatxmlrpc.php';
  var args   = 'action=send&message=' + message;
  var ajax   = new Ajax.Request(rpcUrl,
    {
      method: 'post',
      postBody: args,
      onSuccess: chatSuccess
    }
  );
}

var chatSuccess = function(t) {
  if (t.responseText != "") {
    new Insertion.Bottom('modchatoutput', t.responseText);
    var objDiv = $("modchatoutputcontainer");
    objDiv.scrollTop = objDiv.scrollHeight;
  }
}

var receive = function() {
	if($('modchatoutput')) {
		var rpcUrl = 'modchatxmlrpc.php';
		var args   = 'action=receive&returnall=';
		args += ($('modchatoutput').innerHTML) ? 'false' : 'true';

		var ajax   = new Ajax.Request(rpcUrl,
			{
				method: 'post',
				postBody: args,
				onSuccess: chatSuccess
			}
		);
	}
}
	
var updateUsers = function() {
	if($('modchatuserlist')) {
		var rpcUrl = 'modchatxmlrpc.php';
		var args   = 'action=activeUsers';
		var ajax   = new Ajax.Request(rpcUrl,
			{
				method: 'post',
				postBody: args,
				onSuccess: updateUserDiv
			}
		);
	}
}

var updateUserDiv = function(t) {
	if (t.responseText != "") {
		$('modchatuserlist').innerHTML = t.responseText;
	}
}
