var UbiCSCallback = {
	
	success : function(o) {
		//alert(o.getAllResponseHeaders);
	},
	
	failure : function(o) {
		alert('Sorry, a problem has occurred.');
	}

};

var UbiCS = {
	
	init : function() {
		if(td = document.getElementById('chat_form')) {
			UbiEvent.addEvent(td, 'submit', UbiCS.handleSubmit, false);
		}
	},

	sendMessage : function(message) {
		//var formObject = document.getElementById('chat_form');
		var id = document.getElementById('chat_id').value;
		var u = 'index.html';
		var pd = 'm=chatreceiver&chat_input='+message+'&id='+id;
		//YAHOO.util.Connect.setForm(formObject);
		var cObj = YAHOO.util.Connect.asyncRequest('POST', u, UbiCSCallback, pd);
	},
	
	cancelBubbling : function(e) {
		if(window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if(e && e.stopPropagation && e.preventDefault) {
			e.stopPropagation();
			e.preventDefault();
		}
	},

	handleSubmit : function(e) {
		UbiCS.cancelBubbling(e);
		if(document.getElementById('chat_input').nodeName.toLowerCase() == 'input') document.getElementById('chat_input').value = '';
		else {
			var iframes = YAHOO.util.Dom.getElementsByClassName('mceEditorIframe', 'iframe');
			for(i = 0; i < iframes.length; i++) {
				if(iframes[i].contentDocument) {
					UbiCS.sendMessage(document.getElementById('chat_input').value);
					iframes[i].contentDocument.body.innerHTML = '';
				}
				else {
					UbiCS.sendMessage(iframes[i].contentWindow.document.body.innerHTML);
					iframes[i].contentWindow.document.body.innerHTML = '';
				}
			}
		}
	}
	
};

UbiEvent.addEvent(window, 'load', UbiCS.init, false);