function bulkdelete(messageDiv){
	if(confirm('Are you sure you want to delete all the checked messages?')){
		var postVal = "idlist=";
		field = document.msgform.list
		if(field.length==undefined){
		//only 1 checked
			postVal += field.value;
		}else{
			for (i = 0; i < field.length; i++){
				if(field[i].checked){
					postVal += field[i].value + ",";
				}
			}
		}
		postVal += "&bulkdelete=true";
		makePOSTRequest('/ajax/message/deleteMany',postVal);
	  	http_request.onreadystatechange = function() {//Call a function when the state changes.
			if(http_request.readyState == 4 && http_request.status == 200) {
				document.getElementById(messageDiv).innerHTML = http_request.responseText;
				doPollInbox();
			}
		}
	}
}

function bulkdeletesent(){
	if(confirm('Are you sure you want to delete all the checked messages?')){
		var postVal = "idlist=";
		field = document.msgform.list
		if(field.length==undefined){
		//only 1 checked
			postVal += field.value;
		}else{
			for (i = 0; i < field.length; i++){
				if(field[i].checked){
					postVal += field[i].value + ",";
				}
			}
		}
		postVal += "&bulkdelete=true";
		makePOSTRequest('/ajax/message/deleteSent',postVal);
	  	http_request.onreadystatechange = function() {//Call a function when the state changes.
			if(http_request.readyState == 4 && http_request.status == 200) {
				document.getElementById(messageDiv).innerHTML = http_request.responseText;
				doPollInbox();
			}
		}
	}
}

function bulkread(messageDiv){
	
	var postVal = "idlist=";
	field = document.msgform.list;
	if(field.length==undefined){
	//only 1 checked
		postVal += field.value;
	}else{
		for (i = 0; i < field.length; i++){
			if(field[i].checked){
				postVal += field[i].value + ",";
			}
		}
	}
	
	postVal += "&bulk=true";
	makePOSTRequest('/ajax/message/markread',postVal);
  	http_request.onreadystatechange = function() {//Call a function when the state changes.
		if(http_request.readyState == 4 && http_request.status == 200) {
			document.getElementById(messageDiv).innerHTML = http_request.responseText;
			doPollInbox();
		}
	}
}

function bulkunread(messageDiv){

		var postVal = "idlist=";
		field = document.msgform.list
		if(field.length==undefined){
		//only 1 checked
			postVal += field.value;
		}else{
			for (i = 0; i < field.length; i++){
				if(field[i].checked){
					postVal += field[i].value + ",";
				}
			}
		}
		postVal += "&bulk=true";
		makePOSTRequest('/ajax/message/markunread',postVal);
	  	http_request.onreadystatechange = function() {//Call a function when the state changes.
			if(http_request.readyState == 4 && http_request.status == 200) {
				document.getElementById(messageDiv).innerHTML = http_request.responseText;
				doPollInbox();
			}
	}
}


function doPollInbox() {
/*	ajaxPostCommand('/ajax_message_inbox_count', '', 
		function(reply){
			document.getElementById('msgcnt1').innerHTML=reply;
			document.getElementById('msgcnt2').innerHTML=reply;
		}
	)*/
	return pollInbox();
}

function pollInbox(poller){
  if($$('.poll_inbox').length > 0){
	  new Ajax.Request('/ajax/message/inbox_count', {
	  method: 'get',
	  onSuccess:
	  	function(transport){
				var els = $$('.poll_inbox');
				for (var i = 0; i<els.length; i++){
					els[i].innerHTML=transport.responseText;
				}
			},
		onFailure:function(transport){alert('fail');}
		});
  }
}
// new PeriodicalExecuter(pollInbox, 90); this should be loaded by event window.load

Event.observe(window, 'load', 
	function() {
		if( $$('.poll_inbox').length > 0 ){
			pollInbox();
			new PeriodicalExecuter(pollInbox, 90); 
		}
	}
);

function checkForBack(e, txtObj){
	//fired onkeydown for compose
	//var txtObj = $('to');
	var keycode;
    if (window.event){
    	keycode = window.event.keyCode;
    }else if (e){
    	keycode = e.which;
    }else{
    	return true;
    }
    if(keycode==8 && txtObj.value.length==0){
    	delist_user('last');
    }else{
    	return true;
    }
}

function delist_user(id, field){
	if(field==undefined){
		field='to_ids';
	}
	if(id='last'){
		//get last value
		var ids = $(field).value.split(';')
		id = ids[ids.length-2]
	}
	if(id){
		$(field).value = (';'+$('to_ids').value).replace(';'+id+';', ';').slice(1);
		$('to_'+id).remove();
		$('to').focus();
	}
};

