newjax.compose = function() {

	return {

		name: 'compose',
		body_: '',
		sig_: '',
		id: 0,
		message_listener: false, 
		counter: 1,
		sending: true,
		
		
		
		select_all: function(form, field) {
			
			var objCheckBoxes = document.forms[form].elements[field];
			
			for (var i = 0; i < objCheckBoxes.length; i++) {
				
				if (!objCheckBoxes[i].checked) objCheckBoxes[i].checked = true;
				else objCheckBoxes[i].checked = false;
				
			}
	
		},
		
		
		send_delayed_message: function(id) {
				

				if (!newjax.compose.sending) return;
				
				
				newjax.update(
					'', 
					newjax.actions_page, 
					{ newjax_action:'send_delayed_message', id:id, counter:newjax.compose.counter++ }, 
					function(){} 
				);
				
				
				setTimeout("newjax.compose.send_delayed_message("+id+")", 1000);
				
		},


		change_format: function(x) {
			

			if (x.value == 'HTML') {
			
				tinyMCE.triggerSave(false,true);
				tinyMCE.execCommand("mceRemoveControl", true, 'body');
		
				tinyMCE.idCounter=0;
				tinyMCE.execCommand('mceAddControl', false, 'body');
		
		
		
		
		
		
			} else {
		
				tinyMCE.triggerSave(false,true);
				tinyMCE.execCommand("mceRemoveControl", true, 'body');
		
					
					
				//var b = $('body').value;
				//b = b.replace(/"\//g, '"');
				//b = b.replace(/\n/g, '"');
				
				//$('body').value = b;
		
		
				return;

			
		


				var data = '&newjax_action=change_format&body='+b;
				newjax.update('', newjax.actions_page, data, function(){} );
		
			}

		},
		
		



		add_attachment: function() {
			mail.attachmentCount++;
			$('attachment_'+mail.attachmentCount).style.display = 'block';
		},
		
		remove_attachment: function(id) {
			if (mail.attachmentCount < 1) return;
			$('attachment_'+id).style.display = 'none';
			mail.attachmentCount--;
		},

		add_sig: function() {
		
			var data = 'action=add_sig'; 
			mail.update('', mail.actions_page, data, function() {});
		
		},

		compose_message: function(to) {

			if (mail.current_page != 'messages') mail.messages.view_messages_tools();

			mail.current_page = 'compose';
		
			var data = 'action=compose'; 
			if (to) data += '&to_='+to;
			 
			mail.update('mailbox_div', mail.actions_page, data, function() {

				mail.compose.body_ = $('body_').value;
				mail.compose.add_sig();

				tinyMCE.idCounter=0;
				tinyMCE.execCommand('mceAddControl', false, 'body_');

			});

			dhtmlHistory.add('compose', '');
		
		},

		remove: function() {
		
						if (!$('body')) return;
		
						tinyMCE.idCounter = 0;

						//tinyMCE.triggerSave(false,true);
						tinyMCE.execCommand("mceRemoveControl", true, 'body');
				
						//tinyMCE.idCounter=0;
						//tinyMCE.execCommand('mceAddControl', false, 'body_');
		

		},

		compose: function() {


				if (typeof(tinyMCE) == 'undefined') return;
				if (!$('body')) return;

				tinyMCE.idCounter = 0;
				tinyMCE.execCommand('mceAddControl', false, 'body');

		},

		load_tiny: function() {


			if (!$('body')) return;


			tinyMCE.init({ 
			
					mode : "none",
					theme : "advanced",
			
			
					plugins : "table,advimage,advlink,emotions,insertdatetime,searchreplace,contextmenu,paste,directionality,noneditable",
					theme_advanced_buttons1_add : "fontselect,fontsizeselect",
					theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,separator,forecolor,backcolor",
					theme_advanced_buttons2_add_before: "cut,copy,paste,separator,search,replace,separator",
					theme_advanced_buttons3_add_before : "tablecontrols,separator",
					theme_advanced_buttons3_add : "emotions,separator,ltr,rtl",
					theme_advanced_toolbar_location : "top",
					theme_advanced_toolbar_align : "left",
					theme_advanced_path_location : "none",
					content_css : "example_full.css",
					plugin_insertdate_dateFormat : "%Y-%m-%d",
					plugin_insertdate_timeFormat : "%H:%M:%S",
					external_link_list_url : "example_link_list.js",
					external_image_list_url : "example_image_list.js",
					theme_advanced_resize_horizontal : true,
					theme_advanced_resizing : true,
					relative_urls : true,
    				convert_urls : false,
					remove_linebreaks : false
					
					
					
			});



		}

	}

}();



if (typeof(tinyMCE) != 'undefined') newjax.compose.load_tiny();


Event.observe(window, 'load', function() {

	newjax.compose.compose();

});


