function AdminUsersGridModify( uid ) {
	window.open( 'edit.php?mode=modify&uid='+uid , '_blank' , AdminUsersGridJsParams );
}

function AdminUsersGridSelectUser( user_id , callback , callback_param ) {
	eval( "window.opener."+callback+"('"+callback_param+"','"+user_id+"');" );
	window.close();
}

function AdminUsersGridAction( n , action , params ) {
	if ($("#"+FNTHSuperGrid_ToolbarId( n , action )).hasClass( "dsb" )) return;
	switch (action) {
	case "new":
		window.open( 'edit.php?mode=new' , '_blank' , AdminUsersGridJsParams );break;
	case "modify":
		AdminUsersGridModify( $("#"+n).data( "selecteds" )[0] , AdminUsersGridJsParams );
		break;
	case "delete":
		if (!window.confirm( aJSLangDefs.userspage_confirm_Delete_users )) return;
		BGAction( n , action );
		break;
	case "filter":
		$("#frm_AdminUsersGrid").append( "<input type=\"hidden\" name=\"task\" value=\"filter\" />" );
		document.frm_AdminUsersGrid.submit();
		break;
	case "ack":
		if (!window.confirm( aJSLangDefs.userspage_confirm_Admin_acknowledge )) return;
		BGAction( n , action );
		break;
	case "merge_user":
		BGAction( n , action , params );
		break;
	}
}

function AdminUsersGridSelectionChanged( tblid , chk , id ) {
	BGSelectionChanged( tblid , chk , id );
	AdminUsersChanged( tblid  );
}

function AdminUsersChanged( tblid ) {
	BGSelectionChangedDefaultAMD( tblid );
	var t = $("#"+tblid );
	var a = t.data( "selecteds" );
	if (a.length>0) {
		$("#"+tblid+"_tbrbutton_ack").removeClass( "dsb" );
	} else {
		$("#"+tblid+"_tbrbutton_ack").addClass( "dsb" );
	}
}

function AdminUsersGridSelectAll( tblid , val ) {
	BGSelectAll( tblid , val );
	AdminUsersChanged( tblid );
}
		
function AdminUsersGridSelectRealMergeUser( gridid ) {
	var d = $("#dlg_merge_user").data( "datas" );
	$.ajax(
		{
			type: "POST",
			cache: false,
			data: (
				{
					"inajax" : 1,
					"ajaxevent" : "table_event",
					"tbl" : gridid,
					"action" : "merge_user",
					"source_user_id" : d.source_user_id , 
					"target_user_id" : d.target_user_id 
				}
			),
			success: function(ret) {
				eval( ret );
			}
		}
	);
}

function AdminUsersGridMergeUserDialog( gridid , source_user_id ) {
	var d = { };
	d["inajax"] = 1;
	d["tbl"]=gridid;
	d["ajaxevent"]="dlg_merge_user";
	d["source_user_id"]=source_user_id;

	$("#dlg_merge_user").data( "datas" , d );

	var btns = new Object();
	btns[aJSLangDefs.Merge]=function() {
		AdminUsersGridSelectRealMergeUser( gridid );
	}

	btns[aJSLangDefs.Cancel]=function() {
		$("#dlg_merge_user").dialog("close");
		$("#dlg_merge_user").dialog("destroy");
	}


	AdminUsersGridOpenPopup( gridid );

	$("#dlg_merge_user").dialog(
		{
			title: aJSLangDefs.Merge_user,
			width: 400,
			height: 200,
			buttons: btns
		}
	);
}

function AdminUsersGridOpenPopup( id ) {
	var a = $.getUrlVars();
	a["selector"]=1;
	a["scb"]="AdminUsersGridMergeUserSelected";
	a["scbparam"]=id;
	var url = $.joinUrl( "/admin/users/" , a );
	window.open( url , '_blank', 'width=800,height=600,scrollbars=yes' );
}

function AdminUsersGridMergeUserSelected( id , target_user_id ) {
	var d = $("#dlg_merge_user").data( "datas" );
	d.target_user_id = target_user_id;
	$("#dlg_merge_user").data( "datas" , d );
	
	$.ajax(
		{
			type: "POST",
			cache: false,
			data: (
				{
					"inajax" : 1,
					"ajaxevent" : "table_event",
					"tbl" : id,
					"action" : "dlg_merge_user",
					"source_user_id" : d.source_user_id , 
					"target_user_id" : d.target_user_id 
				}
			),
			success: function(ret) {
				req = eval( ret );
			}
		}
	);
}
