/**
 * Client chat object.
 */
function ClientChat(name, cid) {
	this.initialize(name, cid);
}

ClientChat.inherits(Chat);

ClientChat.method("doProcess",
	function (x) {
		if (x.type === "new user reply") {
			this.connected = true;
			this.setStatus("Connected.");
			if (x.text) {
				this.logSysMessage(x.text);
			}
		}
		else if (x.type === "disconnect") {
			this.processDisconnect(x);
		}
		else if (x.type === "typing") {
			var typingstatus = x.properties.typingStatus;
			if (typingstatus === "typing") {
				this.setTyping(x.from);
			}
			else if (typingstatus === "stopped") {
				this.setStoppedTyping(x.from);
			}
			else {
				this.clearTyping(x.from);
			}
		}
		else if (x.type === "string") {
			this.clearTyping(x.from);
			this.logMessage(x.from, x.text);
		}
		else if (x.type === "system string") {
			this.logSysMessage(x.text);
		}
		else if (x.type === "agent connect") {
			this.agentConnected = true;
			this.logSysMessage(x.from + " connected.");
		}
	});
	
ClientChat.method("initDisplayChat",
	function () {
		var main = document.getElementById("chatmain");
		var newdiv = document.createElement("div");
		newdiv.setAttribute("id", "chat" + this.callid + "." + this.rand);
		newdiv.setAttribute("class", "chatinstance");
		newdiv.setAttribute("className", "chatinstance");
		newdiv.setAttribute("style", "display: none;");
		newdiv.innerHTML = '<p class="chatstatus">' +
		'<span id="chatstatushead' + this.callid + "." + this.rand + '" class="chatstatushead">Status: </span>' +
		'<span id="chatstatus' + this.callid + "." + this.rand + '" class="chatstatus">Loading...</span>' +
		'</p>' +
		'<div id="chatcontainer' + this.callid + "." + this.rand + '" class="chatcontainer">' +
			'<div id="chatlog' + this.callid + "." + this.rand + '" class="chatlog"></div>' +
			'<div id="chattypingstatus' + this.callid + "." + this.rand + '" class="chattypingstatus"></div>' +
		'</div>' +
		'<div id="debuglog' + this.callid + "." + this.rand + '" class="debuglog" style="display: none;"></div>' +
		'<div class="clearboth"></div>' +
		'<textarea id="chatarea' + this.callid + "." + this.rand + '" class="chatarea chatareaunfocus" rows="3" cols="50" onkeydown="getChat(' + this.callid + ').beforeTypeChatarea();" onkeypress="return getChat(' + this.callid + ').typeChatarea(event);" onkeyup="getChat(' + this.callid + ').afterTypeChatarea();" onfocus="getChat(' + this.callid + ').focusChatarea();" onblur="getChat(' + this.callid + ').unfocusChatarea();"></textarea>' +
		'<div id="chatbuttons' + this.callid + "." + this.rand + '" class="chatbuttons">' +
		'<button id="chatsend' + this.callid + "." + this.rand + '" class="chatsend" onclick="getChat(' + this.callid + ').sendMessage();">&nbsp;</button>' +
		//'<button id="chatdiscon' + this.callid + "." + this.rand + '" class="chatdiscon" onclick="getChat(' + this.callid + ').stopChat();">Disconnect</button>' +
		'</div>';
		main.appendChild(newdiv);

		this.shown = false;
		this.newMessage = false;
	});
	
ClientChat.method("updateShowButton",
	function () {});

/**
 * For client, this is the only one.
 */
function getChat(callid) {
	return chats[0];
}

/**
 * Called on load.
 */
function startChat() {
    var qs = document.location.href.substring(document.location.href.indexOf("?") + 1);
    if (qs.length > 0) {
        qs = qs.split("&");
        for (var param in qs) {
            param = qs[param].split("=");
            if (param[0] == "sID") document.getElementById("HT_SessionID").value = param[1];
            if (param[0] == "vID") document.getElementById("HT_VisitorID").value = param[1];
            if (param[0] == "pro") document.getElementById("HT_PROChat").value = param[1];
        }
    }
}


/**
 * Called on unload.
 */
function stopChat() {
	for (var i in chats) {
		chats[i].stopChat();
	}
}

function addFormElement(form, type, name, value) {
	input = document.createElement("input");
	input.type = type;
	input.name = name;
	input.value = value;
	form.appendChild(input);
}

/**
 * Called on form submit.
 */
function login() {
	var loginname = document.getElementById("chatloginname");
	loginname.value = loginname.value.replace(/^\s+|\s+$/g, "");
	loginname.value = loginname.value.substring(0, 16);

	VisitorName = loginname.value;
    	VisitorEmail = document.getElementById("Email").value;
  	ChatType = document.getElementById("SalesChat").checked ? "Sales" : "Customer Service";
    	transScript = document.getElementById("transScript").checked ? "true":"false";

    	document.getElementById("HT_Referrer").value = document.referrer;
    	document.getElementById("HT_IPAddress").value = IPAddress;
    	document.getElementById("HT_ReVisitID").value = RevisitID;
    	document.getElementById("HT_EnteredOn").value = EnteredOn;

	if (loginname.value.length > 0) {
		var form = document.getElementById("chatform");
		form.action = serverAddr + "?random=" + generateRandomNumber();

		addFormElement(form, "hidden", "display_page", displayPage);
		addFormElement(form, "hidden", "page_servlet_location", agentPage);

		document.getElementById("chatframediv").innerHTML = '<iframe id="chatframe" name="chatframe" style="border: 0px none; width: 0px; height: 0px;"></iframe>';

		document.getElementById("chatlogin").style.display = "none";

		var chat = new ClientChat(loginname.value, 0);
		chats[0] = chat;
		chat.startChat();
		chat.show();
		return true;
	}
	return false;
}


function deleteAllCookies() {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}

function EndChat() {
    if (confirm('Are you sure, you want to close this chat window?')) {
        stopChat();
        deleteAllCookies();
        setTimeout(function() { window.close(); }, 1000);
    }
    return false;
}

