MGCCbEvoNS.already_editing 			= 0;
MGCCbEvoNS.edit_chatid				= 0;
MGCCbEvoNS.edit_previous_content 	= null;
MGCCbEvoNS.edit_content				= null;
MGCCbEvoNS.username 				= null;
MGCCbEvoNS.report_chatid			= 0;

MGCCbEvoNS.delete_chat = function(chatid)
{	
	dojo.xhrPost({
		url : this.jsloc + 'mgc_cb_evo_ajax.php',
		handleAs : 'xml',
		preventCache : true,
		content :
		{
			securitytoken : SECURITYTOKEN,
			action : 'ajax_delete_chat',
			chatid : chatid
		},
        error: function (error) {
            console.error ('Error: ', error);
		},
		load : dojo.hitch(this,'wait_delete_chat')
	});	
	
	return false;
};

MGCCbEvoNS.wait_delete_chat = function(delete_chat_answer_xml)
{
	if(delete_chat_answer_xml)
	{
		xmlresult = delete_chat_answer_xml.getElementsByTagName('answer_result')[0].firstChild.nodeValue;
      	eval(xmlresult);
	}
};

MGCCbEvoNS.edit_chat = function(chatid)
{
	// Only go here if editing not yet pending
	if (this.already_editing == 0)
	{
		this.already_editing = 1;
		
		// Disable all refresh
		clearTimeout(this.refresh_timeout);
		clearTimeout(this.refreshPM_timeout);
		
		// Register chatid
		this.edit_chatid = chatid;
		
		// Register previous content 
		MGCCbEvoNS.edit_previous_content = dojo.byId('cspan_' + this.edit_chatid).innerHTML;
		
		// Ajax query
		dojo.xhrPost({
			url : this.jsloc + 'mgc_cb_evo_ajax.php',
			handleAs : 'xml',
			preventCache : true,
			content :
			{
				securitytoken : SECURITYTOKEN,
				action : 'ajax_edit_getchat',
				chatid : this.edit_chatid
			},
	        error: function (error) {
	            console.error ('Error: ', error);
			},
			load : dojo.hitch(this,'wait_edit_chat_getcontent')
		});
	}
	else
	{
		this.show_dialog(phrase_already_editing,'');
	}
};

MGCCbEvoNS.wait_edit_chat_getcontent = function(edit_chat_getcontent_xml)
{
	if(edit_chat_getcontent_xml)
	{
		edit_content = edit_chat_getcontent_xml.getElementsByTagName('chat_content')[0].firstChild.nodeValue;;

		// Any content ?
		if (edit_content != '')
		{
			this.edit_content = edit_content;
			
			// Limitation of chars
			if (this.char_limit > 0)
			{
				dojo.byId('cspan_' + this.edit_chatid).innerHTML = '<input type="text" id="chatedit" value="' + PHP.htmlspecialchars(this.edit_content) + '" class="bginput" style="width: 75%" maxlength="500" onkeyup="MGCCbEvoNS.check_char_limit(this,1);" /> <input type="text" id="mgc_cb_evo_edit_char_counter" class="primary full textbox" value="' + this.char_limit + '" style="vertical-align: middle;" size="2" readonly="readonly" />';
				MGCCbEvoNS.check_char_limit(dojo.byId('chatedit'),1);
			}
			else
			{
				dojo.byId('cspan_' + this.edit_chatid).innerHTML = '<input type="text" id="chatedit" value="' + PHP.htmlspecialchars(this.edit_content) + '" class="bginput" style="width: 75%" maxlength="500" />';
			}

			dojo.byId('chatedit').focus();
			dojo.byId('chatedit').select();

			dojo.byId('chatedit').onblur = function leave_edit()
			{
   				MGCCbEvoNS.edit_chat_save();
			};

			// Enter key pushed
			dojo.byId('chatedit').onkeypress = function keyDown(e)
			{
				var characterCode;

				if(e && e.which)
				{
					e = e;
					characterCode = e.which;
				}
				else
				{
					e = event;
					characterCode = e.keyCode;
				}

				if(characterCode == 13)
				{
					MGCCbEvoNS.edit_chat_save();
				}
			};
		}
		else
		{
			this.show_dialog(phrase_edit_forbidden,'');
		}
	}
};

MGCCbEvoNS.edit_chat_save = function()
{
	if(this.already_editing)
	{		
		dojo.xhrPost({
			url : this.jsloc + 'mgc_cb_evo_ajax.php',
			handleAs : 'xml',
			preventCache : true,
			content :
			{
				securitytoken : SECURITYTOKEN,
				action : 'ajax_save_edit',
				chatid : this.edit_chatid,
				chat : PHP.urlencode(dojo.byId('chatedit').value)
			},
	        error: function (error) {
	            console.error ('Error: ', error);
			},
			load : dojo.hitch(this,'wait_edit_chat_save')
		});
	}
};

MGCCbEvoNS.wait_edit_chat_save = function(edit_chat_save_answer_xml)
{
	if(edit_chat_save_answer_xml)
	{
		edit_result = edit_chat_save_answer_xml.getElementsByTagName('edit_result')[0].firstChild.nodeValue;
		
		this.already_editing = 0;

		// Finish treatment		
		if (edit_result == 1)
		{	
			// Execute corresponding refresh
			if (this.pm_tabs_on && this.pm_active_tab != 0)
			{
				this.chatbox_pm_refresh();
			}
			else
			{
				this.chatbox_refresh('forced');
			}
		}
		else
		{
			// Error management
			dojo.byId('cspan_' + this.edit_chatid).innerHTML = this.edit_previous_content;
			eval(edit_result);
		}
	}
};

MGCCbEvoNS.set_name = function(username)
{
	if (username != null && PHP.trim(username) != "")
   	{
   		this.username = username;
   		this.send_chat();
	}
	else
	{
		this.show_dialog(phrase_error_name,'');
	}
};

MGCCbEvoNS.send_chat = function()
{
	// If ask name active and no name entered yet, ask for one before sending the chat
	if (this.ask_anonymous_name && this.username == null)
	{
		this.show_promptdialog(phrase_enter_name,"",'MGCCbEvoNS.set_name');
		return false;
	}

	// If trying to send prompt, clear prompt and do nothing
   	if (this.input_prompt_msg)
   	{
    	this.remove_chat_prompt();
      	return false;
   	}

	// If inactive mode is active in the chatbox, reset timeout
	if (this.inactive_mode_on)
	{
		clearTimeout(this.inactive_timeout);
   		this.inactive_timeout = setTimeout("activate_idle_chatbox()", this.inactive_mode_delay); // TODO
	}

	if (this.already_sending)
	{
		this.show_dialog(phrase_error_chat_in_process,'');
	}
	else
	{
		this.already_sending = 1;
		
		// PM in tabs chat
		if (this.pm_tabs_on && MGCCbEvoNS.pm_active_tab != 0) // TODO
		{
			chat = '/pm uid ' + MGCCbEvoNS.pm_active_tab + ' ' + dojo.byId('mgc_cb_evo_input').value;
		}
		else
		{
			chat = dojo.byId('mgc_cb_evo_input').value;
		}

		// Send the chat with the anonymous name if any
		if (this.ask_anonymous_name)
		{
			this.send_chat_post(chat,this.username);
		}
		else
		{
			this.send_chat_post(chat,'');
 		}
   }
   return false;
};

MGCCbEvoNS.send_chat_post = function(chat,username)
{
	if (chat != '')
	{
		dojo.xhrPost({
			url : this.jsloc + 'mgc_cb_evo_ajax.php',
			handleAs : 'xml',
			preventCache : true,
			content :
			{
				securitytoken : SECURITYTOKEN,
				action : 'ajax_chat',
				channel_id : this.current_chanid,
				chat_name : username,
				chat : PHP.urlencode(chat),
				b: this.ubbcode_b,
				i : this.ubbcode_i,
				u : this.ubbcode_u,
				font : this.ubbcode_font,
				color : this.ubbcode_color,
				size : this.ubbcode_size
			},
	        error: function (error) {
	            console.error ('Error: ', error);
			},
			load : dojo.hitch(this,'wait_send_chat_post')
		});		
	}
};

MGCCbEvoNS.wait_send_chat_post = function(send_chat_post_answer_xml)
{
	if(send_chat_post_answer_xml)
	{
      	// Clear timeout
      	clearTimeout(this.refresh_timeout);	
      	clearTimeout(this.refreshPM_timeout);
	
		this.already_sending = 0;
		
		// Get xml answer content
		send_chat_result = send_chat_post_answer_xml.getElementsByTagName('sendchat_result')[0].firstChild.nodeValue;

      	// Clear chat input
		this.clear_chat_input();
		
		// Give focus back to chat input
		this.focus_chat_input();
		
		// Reset chat counter if active
		if (this.char_limit)
		{
			this.check_char_limit(document.getElementById('mgc_cb_evo_input'),0);
		}
		
      	// Execute ajax call return function
      	if (this.pm_tabs_on && this.pm_active_tab != 0 && send_chat_result == "MGCCbEvoNS.chatbox_refresh('forced')")
      	{ 
      		this.chatbox_pm_refresh();
      	}
      	else
      	{
      		eval(send_chat_result);
		}
   }
};

MGCCbEvoNS.sticky_chat = function(chatid,status)
{
	dojo.xhrPost({
		url : this.jsloc + 'mgc_cb_evo_ajax.php',
		handleAs : 'xml',
		preventCache : true,
		content :
		{
			securitytoken : SECURITYTOKEN,
			action : 'ajax_sticky_chat',
			chatid : chatid,
			status : status
		},
        error: function (error) {
            console.error ('Error: ', error);
		},
		load : dojo.hitch(this,'wait_sticky_chat')
	});
};

MGCCbEvoNS.wait_sticky_chat = function(sticky_chat_answer_xml)
{
	if(sticky_chat_answer_xml)
	{	
		stiky_chat_result = sticky_chat_answer_xml.getElementsByTagName('stiky_result')[0].firstChild.nodeValue;
		eval(stiky_chat_result);
	}
};

MGCCbEvoNS.report_chat = function(reason)
{
	dojo.xhrPost({
		url : this.jsloc + 'mgc_cb_evo_ajax.php',
		handleAs : 'xml',
		preventCache : true,
		content :
		{
			securitytoken : SECURITYTOKEN,
			action : 'ajax_report_chat',
			chatid : this.report_chatid,
			reason : reason
		},
        error: function (error) {
            console.error ('Error: ', error);
		},
		load : dojo.hitch(this,'wait_report_chat_post')
	});
};

MGCCbEvoNS.wait_report_chat_post = function(report_chat_answer_xml)
{
	if(report_chat_answer_xml)
	{
		xmlresult = report_chat_answer_xml.getElementsByTagName('answer_result')[0].firstChild.nodeValue;
      	eval(xmlresult);
	}
};

MGCCbEvoNS.create_chatrow = function(xmlnode,specialchat)
{
	if (this.small_version)
	{
		var newchat = xmlnode.getElementsByTagName('chatbit')[0].firstChild.nodeValue;
		var ndiv = document.createElement('div');
		ndiv.id = "chatrow_" + xmlnode.getElementsByTagName('chatid')[0].firstChild.nodeValue;
		ndiv.innerHTML = "<div style='padding: 2px;'>" + newchat + "</div>";
		nchatrow = ndiv;
	}
	else
	{
		var chatid	= xmlnode.getElementsByTagName('chatid')[0].firstChild.nodeValue;
	
		/* Creation of the row */
		var nchatrow = document.createElement('tr');
		nchatrow.setAttribute('id','chatrow_' + chatid);
		
		/* Creation of the Menu column */
		if ((this.notifs_display > 1) || (specialchat == 0))
		{
			if (xmlnode.getElementsByTagName('col_menu')[0].firstChild != null)
			{
				var menu_innerHTML = xmlnode.getElementsByTagName('col_menu')[0].firstChild.nodeValue; 
			}
			else
			{
				var menu_innerHTML = '';
			}
		
			dojo.create(
				"td",
				{
					width: '0',
					id: 'chat_' + chatid,
					align: this.left,
					valign: this.chat_valign,
					innerHTML: menu_innerHTML
				},
				nchatrow
			);	
		}
		else
		{
			dojo.create(
				"td",
				{
					width: "0",
					id: 'chat_' + chatid,
					align: this.left,
					valign: this.chat_valign,
					innerHTML: ''
				},
				nchatrow
			);
		}
	
		/* Creation of the sticky column */
		if (xmlnode.getElementsByTagName('col_sticky')[0].firstChild != null)
		{
			var sticky_innerHTML = xmlnode.getElementsByTagName('col_sticky')[0].firstChild.nodeValue; 
		}
		else
		{
			var sticky_innerHTML = '';
		}
	
		dojo.create(
			"td",
			{
				width: '0',
				align: this.left,
				valign: this.chat_valign,
				innerHTML: sticky_innerHTML
			},
			nchatrow
		);
		
		/* Creation of the atusername column */
		if(this.activate_atusername_icon)
		{
			if (xmlnode.getElementsByTagName('col_atusername')[0].firstChild != null)
			{
				var atusername_innerHTML = xmlnode.getElementsByTagName('col_atusername')[0].firstChild.nodeValue; 
			}
			else
			{
				var atusername_innerHTML = '';
			}
		
			dojo.create(
				"td",
				{
					width: '0',
					align: this.left,
					valign: this.chat_valign,
					innerHTML: atusername_innerHTML
				},
				nchatrow
			);
		}
	
		/* Creation of the avatar column if any */
		if (this.avatar_on)
		{
			dojo.create(
				"td",
				{
					width: '0',
					align: this.left,
					valign: this.chat_valign,
					innerHTML: xmlnode.getElementsByTagName('col_avatar')[0].firstChild.nodeValue
				},
				nchatrow
			);
		}
	
		/* Manage the username/date/chat columns depending of the wished alignment */
		if (this.align_chats && ((specialchat == 0) || !(this.notifs_display > 1 && (specialchat == 1) && this.br_after_username)))
		{
			/* Creation of the date column */
			dojo.create(
				"td",
				{
					width: '0',
					align: this.left,
					valign: this.chat_valign,
					noWrap: 'nowrap',
					style: {
						whiteSpace: 'nowrap'
					},
					innerHTML: xmlnode.getElementsByTagName('col_date')[0].firstChild.nodeValue
				},
				nchatrow
			);
	
			/* Creation of the username column */
			if (xmlnode.getElementsByTagName('col_uname')[0].firstChild != null)
			{
				uname_innerHTML = xmlnode.getElementsByTagName('col_uname')[0].firstChild.nodeValue;
			}
			else
			{
				uname_innerHTML = '';
			}
			
			dojo.create(
				"td",
				{
					width: '0',
					align: this.left,
					valign: this.chat_valign,
					noWrap: 'nowrap',
					style: {
						whiteSpace: 'nowrap'
					},
					innerHTML: uname_innerHTML
				},
				nchatrow
			);
	
			/* Creation of the chat column */
			dojo.create(
				"td",
				{
					width: '100%',
					align: this.left,
					valign: this.chat_valign,
					innerHTML: xmlnode.getElementsByTagName('col_chat')[0].firstChild.nodeValue
				},
				nchatrow
			);
		}
		else
		{
			/* Creation of the date column */
			dojo.create(
				"td",
				{
					width: '0',
					align: this.left,
					valign: this.chat_valign,
					noWrap: 'nowrap',
					style: {
						whiteSpace: 'nowrap'
					},
					innerHTML: xmlnode.getElementsByTagName('col_date')[0].firstChild.nodeValue
				},
				nchatrow
			);
			
			/* Creation of the username + chat column */
			if ((this.notifs_display > 1) && (specialchat == 1) && this.br_after_username)
			{
				/* specialchat add br */
				chat_innerHTML = xmlnode.getElementsByTagName('col_uname')[0].firstChild.nodeValue + '<br />' + xmlnode.getElementsByTagName('col_chat')[0].firstChild.nodeValue;
			}
			else
			{
				/* Normal chat or specialchats no br : no align */
				if (xmlnode.getElementsByTagName('col_uname')[0].firstChild != null)
				{
					chat_innerHTML = xmlnode.getElementsByTagName('col_uname')[0].firstChild.nodeValue + xmlnode.getElementsByTagName('col_chat')[0].firstChild.nodeValue;
				}
				else
				{
					chat_innerHTML = xmlnode.getElementsByTagName('col_chat')[0].firstChild.nodeValue;
				}
			}		
			
			dojo.create(
				"td",
				{
					width: '100%',
					align: this.left,
					valign: this.chat_valign,
					innerHTML: chat_innerHTML
				},
				nchatrow
			);
		}
	}
	
	return nchatrow;
};
