// class messagebox 
function messagebox(){
	this.to_id = 0;
	this.msg_on = msg_on;
	this.resp = 0;
	this.advertid = 0;	
	this.show_msgdialog=show_msgdialog;
	this.post_msg=post_msg;
	this.dialog_info=dialog_info;
}
function show_msgdialog(id, advertid, resp){
	if (this.msg_on != 'Y') {
		document.location = 'memberships.php?msg=no_messages';
		return 0;
	}
	if (id == 0)
		return 0;
	this.to_id = id;
	this.resp=0;
	this.advertid = 0;	

	if (resp)
		this.resp=resp;
	if (advertid)
		this.advertid=advertid;


	$("#messagebox_load").hide();
	$("#messagebox_info").hide();
	$("#messagebox_body").show();
	$('#messagebox_info_text').text('');
	$('#messagebox_msg').val("");

	if (this.to_id ==  loginid){
		$("#messagebox_body").hide();
		this.dialog_info(msg_no_write_youself, 'E');
	}

    $("#messagebox_dialog").dialog();
    $("#messagebox_dialog").dialog('open');
}
function dialog_info(msg, type){
	style= "ui-state-highlight ui-corner-all";
	if (type){
		if (type == 'E')
			style = "ui-state-error ui-corner-all";
	}
	$('#messagebox_info').attr('class',style);
	$('#messagebox_info_text').text(msg);
	$("#messagebox_info").show();
}

function post_msg(){
	msg = $('#messagebox_msg').val();
	if (msg == '')
		return 0;
	$("#messagebox_load").show();
	$("#messagebox_body").hide();
	this_class = this; 
	$.post("messages.php",{mode:'postmsg', to_id: this.to_id, message: msg, advertid: this.advertid, resp: this.resp}, 
		function(res){
			$("#messagebox_load").hide();
			if (res>0){
				this_class.dialog_info(lbl_message_sent);
//				if (this_class.resp > 0 && localBrowser == 'MSIE') {
//					window.location =  window.location.href + '?select_id='+res;
//				}

				if (accountpage == 'Y'){
					if (this_class.resp > 0) {
						if (localBrowser != 'MSIE')
							box_I.build_message_table('I');
					}

					if (localBrowser != 'MSIE'){
						box_O.build_message_table('O');
					}
					if (localBrowser == 'MSIE'){
						window.location.reload();
					}
				}


			}else{
				ms = lbl_error_sent_msg;
				if (res == 'none_member')
					ms = lbl_none_member;
				else if(res == 'this_msg_already_resp')
					ms = lbl_this_msg_already_resp;
				else if(res == 'no_handbag_messages'){
					ms = lbl_no_handbag_messages;
					document.location = 'memberships.php?msg=no_messages';
				}

				this_class.dialog_info(ms, 'E');
			}
		}
	);
}
//end class messagebox

boxpostmsg=new messagebox();
