var xmlhttp;
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}
function validate_tag()
{
	if($("#tag_opt").val() == "")
	{
		alert("Mohon tentukan label yang akan digunakan!");
		return false;
	}
	return true;
}
function usr_compose_msg(s)
{
	var url="../user/compose.php";
	window.location = url;
	return false;
}
function usr_add_contact(s)
{
	var url="../user/contact.php?act=add";
	window.location = url;
	return false;
}
function usr_add_group(s)
{
	var url="../user/group.php?act=add";
	window.location = url;
	return false;
}
function usr_mod_inbox(state, next)
{
	if($("input[name='check_item']:checked").length > 0)
	{
		var msg = "";

		if(state == "delete")
			msg = "Apakah anda ingin menghapus pesan ini?";

		$("#dialog-confirm-text").html(msg);
		$("#dialog").dialog("destroy");	
		$("#dialog-confirm").dialog({
			resizable: false,
			height:140,
			modal: true,
			buttons: {
				"Yes": function() {
					$(this).dialog("close");
					usr_mod_inbox_proc(state, next);
				},
				Cancel: function() {
					$(this).dialog("close");
				}
			}
		});
	}
	else
	{
		show_message_box("Silahkan pilih pesan yang akan dimoderasi!");		
	}
}
function usr_mod_inbox_proc(state, next)
{	
	var1 = "";
	$("input[name='check_item']:checked").each(function() { 
		if(var1 == "")
			var1=$(this).val();
		else
			var1=var1+"-"+$(this).val();
	});

	var url="../user/inbox.php?act="+state+"&ids="+var1+"&next="+next;
	window.location = url;
	return false;
}
function usr_mod_outbox(state, next)
{
	if($("input[name='check_item']:checked").length > 0)
	{
		var msg = "";

		if(state == "delete")
			msg = "Apakah anda ingin menghapus pesan ini?";

		$("#dialog-confirm-text").html(msg);
		$("#dialog").dialog("destroy");	
		$("#dialog-confirm").dialog({
			resizable: false,
			height:140,
			modal: true,
			buttons: {
				"Yes": function() {
					$(this).dialog("close");
					usr_mod_outbox_proc(state, next);
				},
				Cancel: function() {
					$(this).dialog("close");
				}
			}
		});
	}
	else
	{
		show_message_box("Silahkan pilih pesan yang akan dimoderasi!");		
	}
}
function usr_mod_outbox_proc(state, next)
{	
	var1 = "";
	$("input[name='check_item']:checked").each(function() { 
		if(var1 == "")
			var1=$(this).val();
		else
			var1=var1+"-"+$(this).val();
	});

	var url="../user/outbox.php?act="+state+"&ids="+var1+"&next="+next;
	window.location = url;
	return false;
}
function usr_mod_sent(state, next)
{
	if($("input[name='check_item']:checked").length > 0)
	{
		var msg = "";

		if(state == "delete")
			msg = "Apakah anda ingin menghapus pesan ini?";

		$("#dialog-confirm-text").html(msg);
		$("#dialog").dialog("destroy");	
		$("#dialog-confirm").dialog({
			resizable: false,
			height:140,
			modal: true,
			buttons: {
				"Yes": function() {
					$(this).dialog("close");
					usr_mod_sent_proc(state, next);
				},
				Cancel: function() {
					$(this).dialog("close");
				}
			}
		});
	}
	else
	{
		show_message_box("Silahkan pilih pesan yang akan dimoderasi!");		
	}
}
function usr_mod_sent_proc(state, next)
{	
	var1 = "";
	$("input[name='check_item']:checked").each(function() { 
		if(var1 == "")
			var1=$(this).val();
		else
			var1=var1+"-"+$(this).val();
	});

	var url="../user/sent.php?act="+state+"&ids="+var1+"&next="+next;
	window.location = url;
	return false;
}
function usr_mod_contact(state, next)
{
	if($("input[name='check_item']:checked").length > 0)
	{
		var msg = "";

		if(state == "delete")
			msg = "Apakah anda ingin menghapus kontak ini?";

		$("#dialog-confirm-text").html(msg);
		$("#dialog").dialog("destroy");	
		$("#dialog-confirm").dialog({
			resizable: false,
			height:140,
			modal: true,
			buttons: {
				"Yes": function() {
					$(this).dialog("close");
					usr_mod_contact_proc(state, next);
				},
				Cancel: function() {
					$(this).dialog("close");
				}
			}
		});
	}
	else
	{
		show_message_box("Silahkan pilih kontak yang akan dimoderasi!");		
	}
}
function usr_mod_contact_proc(state, next)
{	
	var1 = "";
	$("input[name='check_item']:checked").each(function() { 
		if(var1 == "")
			var1=$(this).val();
		else
			var1=var1+"-"+$(this).val();
	});

	var url="../user/contact.php?act="+state+"&ids="+var1+"&next="+next;
	window.location = url;
	return false;
}
function usr_mod_group(state, next)
{
	if($("input[name='check_item']:checked").length > 0)
	{
		var msg = "";

		if(state == "delete")
			msg = "Apakah anda ingin menghapus grup ini?";

		$("#dialog-confirm-text").html(msg);
		$("#dialog").dialog("destroy");	
		$("#dialog-confirm").dialog({
			resizable: false,
			height:140,
			modal: true,
			buttons: {
				"Yes": function() {
					$(this).dialog("close");
					usr_mod_group_proc(state, next);
				},
				Cancel: function() {
					$(this).dialog("close");
				}
			}
		});
	}
	else
	{
		show_message_box("Silahkan pilih grup yang akan dimoderasi!");		
	}
}
function usr_mod_group_proc(state, next)
{	
	var1 = "";
	$("input[name='check_item']:checked").each(function() { 
		if(var1 == "")
			var1=$(this).val();
		else
			var1=var1+"-"+$(this).val();
	});

	var url="../user/group.php?act="+state+"&ids="+var1+"&next="+next;
	window.location = url;
	return false;
}
function ctz_check_all()
{
	if($("input[name='check_all']").attr("checked"))
		$("input[name='check_item']").attr("checked", true);
	else
		$("input[name='check_item']").attr("checked", false);
}
function show_message_box(msg)
{
	$("#dialog-info-text").html(msg);
	$("#dialog").dialog("destroy");	
	$("#dialog-info").dialog({
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
}

function resent(msg_id, s, next)
{	
	var msg = "";

	msg = "Apakah anda ingin mengirimkan ulang pesan ini?";	

	$("#dialog-confirm-text").html(msg);
	$("#dialog").dialog("destroy");	
	$("#dialog-confirm").dialog({
		resizable: false,
		height:140,
		modal: true,
		buttons: {
			"Yes": function() {
				$(this).dialog("close");
				resent_proc(msg_id, s, next);
			},
			Cancel: function() {
				$(this).dialog("close");
			}
		}
	});
}
function resent_proc(msg_id, s, next)
{	
	var1 = "";
	$("input[name='check_item']:checked").each(function() { 
		if(var1 == "")
			var1=$(this).val();
		else
			var1=var1+"-"+$(this).val();
	});

	var url="../user/sent.php?act=resent&id="+msg_id+"&s="+s+"&next="+next;
	window.location = url;

	return false;
}

function get_total_open_messages()
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}

	var url="../../../jquery/ajax.service.php";
	url=url+"?s=1";
	url=url+"&sid="+Math.random();
	
	xmlhttp.onreadystatechange= function(){ get_total_open_messages_proc(); };
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}

function get_total_open_messages_proc()
{
	if (xmlhttp.readyState==4)
	{
		var data = xmlhttp.responseText;
		if(data!=""){
			$("#title_h").val("("+data+") Inbox");
		}
		else{
			$("#title_h").val("JakSMS: Sistem Error");
		}
	}
}
function open_quick_reply(msg_id)
{
	if($("#qreplyform_"+msg_id).css("display")=="none"){
		$("#qreply_text_"+msg_id).removeAttr("disabled", "disabled");
		$("#qsend_"+msg_id).removeAttr("disabled", "disabled");

		check_character_total(msg_id);

		$("#qreplyform_"+msg_id).show(300);
	}
	else{
		$("#qreplyform_"+msg_id).hide(300);
	}
}
function open_quick_status(msg_id)
{
	if($("#qstatusform_"+msg_id).css("display")=="none")
		$("#qstatusform_"+msg_id).show(300);
	else
		$("#qstatusform_"+msg_id).hide(300);
}
function open_quick_label(msg_id)
{
	if($("#qlabelform_"+msg_id).css("display")=="none")
		$("#qlabelform_"+msg_id).show(300);
	else
		$("#qlabelform_"+msg_id).hide(300);
}
function open_quick_proc(msg_id)
{
	if($("#qprocform_"+msg_id).css("display")=="none")
		$("#qprocform_"+msg_id).show(300);
	else
		$("#qprocform_"+msg_id).hide(300);
}


function update_msg_process(msg_id, status, status_name, obj)
{
	$("#td_proc_"+msg_id).css("background-color", "");
	$("#td_proc_"+msg_id).html("<img style=\"border:0px\" src=\"../media/images/loading_2.gif\"/>");

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}

	var url="../../../jquery/ajax.service.php";
	url=url+"?s=5&par1="+msg_id+"&par2="+status;
	url=url+"&sid="+Math.random();
	
	xmlhttp.onreadystatechange= function(){ update_msg_process_proc(msg_id, status, status_name, obj); };
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);		
}
function update_msg_process_proc(msg_id, status, status_name, obj)
{
	if (xmlhttp.readyState==4)
	{
		var data = xmlhttp.responseText;
		if(data == 1){
			if(status == 1)
				$("#td_proc_"+msg_id).css("background-color", "#FF999B");
			else if(status == 2)
				$("#td_proc_"+msg_id).css("background-color", "#99D3FF");
			else
				$("#td_proc_"+msg_id).css("background-color", "#A3FF99");

			$("#td_proc_"+msg_id).html(status_name);

			$(".qproc_"+msg_id).each(function() { 
				$(this).attr("class", "button6 qproc_"+msg_id);
			});

			$(obj).attr("class", "button5 qproc_"+msg_id);
		}
		else{
			$("#td_proc_"+msg_id).html("Sistem error..");
		}
	}
}


function update_msg_status(msg_id, status, status_name, obj)
{
	$("#td_status_"+msg_id).css("background-color", "");
	$("#td_status_"+msg_id).html("<img style=\"border:0px\" src=\"../media/images/loading_2.gif\"/>");

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}

	var url="../../../jquery/ajax.service.php";
	url=url+"?s=2&par1="+msg_id+"&par2="+status;
	url=url+"&sid="+Math.random();
	
	xmlhttp.onreadystatechange= function(){ update_msg_status_proc(msg_id, status, status_name, obj); };
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);		
}
function update_msg_status_proc(msg_id, status, status_name, obj)
{
	if (xmlhttp.readyState==4)
	{
		var data = xmlhttp.responseText;
		if(data == 1){
			if(status == 1)
				$("#td_status_"+msg_id).css("background-color", "#FF999B");
			else if(status == 2)
				$("#td_status_"+msg_id).css("background-color", "#99D3FF");
			else
				$("#td_status_"+msg_id).css("background-color", "#A3FF99");

			$("#td_status_"+msg_id).html(status_name);

			$(".qstatus_"+msg_id).each(function() { 
				$(this).attr("class", "button6 qstatus_"+msg_id);
			});

			$(obj).attr("class", "button5 qstatus_"+msg_id);
		}
		else{
			$("#td_status_"+msg_id).html("Sistem error..");
		}
	}
}


function update_msg_label(msg_id, tag, tag_name, obj)
{
	$("#td_label_"+msg_id).html("<img style=\"border:0px\" src=\"../media/images/loading_2.gif\"/>");

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}

	var url="../../../jquery/ajax.service.php";
	url=url+"?s=3&par1="+msg_id+"&par2="+tag;
	url=url+"&sid="+Math.random();
	
	xmlhttp.onreadystatechange= function(){ update_msg_label_proc(msg_id, tag, tag_name, obj); };
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);		
}
function update_msg_label_proc(msg_id, tag, tag_name, obj)
{
	if (xmlhttp.readyState==4)
	{
		var data = xmlhttp.responseText;
		if(data == 1){
			$("#td_label_"+msg_id).html(tag_name);

			$(".qlabel_"+msg_id).each(function() { 
				$(this).attr("class", "button6 qlabel_"+msg_id);
			});

			$(obj).attr("class", "button5 qlabel_"+msg_id);
		}
		else{
			$("#td_label_"+msg_id).html("Sistem error..");
		}
	}
}


function quick_reply_msg(dest_number, msg_id)
{
	if($("#qreply_text_"+msg_id).val()==""){
		$("#msg_validate_"+msg_id).html("Pesan tidak boleh kosong");
		return false;
	}
	else{
		if($("#qreply_text_"+msg_id).val().length>150){
			$("#msg_validate_"+msg_id).html("Pesan tidak boleh lebih dari 150 karakter");
			$("#qsend_"+msg_id).attr("disabled", "disabled");
			return false;
		}
		else{
			$("#msg_validate_"+msg_id).html("");
			$("#qsend_"+msg_id).removeAttr("disabled", "disabled");
		}
	}

	$("#loading_"+msg_id).html("<img style=\"border:0px\" src=\"../media/images/loading_2.gif\"/>");

	var msg_text = $("#qreply_text_"+msg_id).val();

	$("#qreply_text_"+msg_id).attr("disabled", "disabled");
	$("#qsend_"+msg_id).attr("disabled", "disabled");

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
	  alert ("Browser does not support HTTP Request");
	  return;
	}

	var url="../../../jquery/ajax.service.php";

	var data="s_=4&par1="+$.URLEncode(dest_number)+"&par2="+$.URLEncode(msg_text)+"&par3="+msg_id;
	
	xmlhttp.onreadystatechange= function(){ quick_reply_msg_proc(dest_number, msg_id); };
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");          
	xmlhttp.send(data);	
}
function quick_reply_msg_proc(dest_number, msg_id)
{
	if (xmlhttp.readyState==4)
	{
		var data = xmlhttp.responseText;
		if(data == 1){
			$("#qreplyform_"+msg_id).hide();			
			$("#qreplystatus_"+msg_id).html("Pesan terkirim");
			$("#qreplymsg_"+msg_id).css("background-color", "#A3FF99");
			$("#qreplymsg_"+msg_id).show(200);
			setTimeout("$('#qreplymsg_'+msg_id).hide(200)", 3000);
		}
		else{
			$("#qreplystatus_"+msg_id).html("Pesan tidak terkirim");
			$("#qreplymsg_"+msg_id).css("background-color", "#FF999B");
			$("#qreplymsg_"+msg_id).show(200);
			$("#qreplymsg_"+msg_id).hide(200);
		}
	}
}
function bind_msg_tpl_view(textbox)
{
	$("#"+textbox).val($("#msg_tpl").val());
	check_character_total('message_1', 'total_char_1', 'total_msg_1');
}
function bind_msg_tpl_inbox_list(msg_id, obj)
{
	$("#qreply_text_"+msg_id).val($(obj).val());
	check_character_total(msg_id);
}
