--[[

	AddOn: DuelDatabase
	Description: An in-game duel database
	Author: Matthew Cox (matthew.a.cox@gmail.com)
	Copyright: Copyright 2005 by Matthew Cox
	Description: http://www.jroller.com/resources/pawnxing/WoWAddons.html
	Usage Notes: Feel free to use this how you like; however modification with redistribution is strictly prohibited.
	Release Notes: This version is backward-compatible with version 1.1.1.0.

]]

--
-- Constants 
--

DuelFrame_VERSION = "1.3";
DUELDATABASE_SHOW_VERSION = "Version "..DuelFrame_VERSION.."."..Duelist_VERSION

BINDING_HEADER_DUELFRAME = "Duel Database";
BINDING_NAME_TOGGLEDUELFRAME = "Toggle Duel Database Frame";

DuelFrame_TITLE = "Duel Database";
DuelFrame_DUEL = "Duel Details";
DuelFrame_WINS = "Wins: ";
DuelFrame_LOSSES = "Losses: ";
DuelFrame_PCT = "Pct.: ";
DuelFrame_TAB_WINS = "Wins";
DuelFrame_TAB_LOSSES = "Losses";
DuelFrame_COL_OPPONENT = "Opponent";
DuelFrame_COL_GUILD = "Guild";
DuelFrame_COL_LEVEL = "Lvl";
DuelFrame_COL_RACE = "Race";
DuelFrame_COL_CLASS = "Class";
DuelFrame_COL_PCT = "Pct";
DuelFrame_COL_SCORE = "Score";
DuelFrame_SEARCH = "Search";
DuelFrame_DETAILVERSUS = "- VS -";
DuelFrame_DETAILGUILD = "Guild:";
DuelFrame_DETAILLEVEL = "Level:";
DuelFrame_DETAILDIFF = "Diff: ";
DuelFrame_DETAILMULTIPLIER = "Multi: ";
DuelFrame_DETAILRACE = "Race:";
DuelFrame_DETAILCLASS = "Class:";
DuelFrame_DETAILHEALTH = "Health (Max/Duel) % left:";
DuelFrame_DETAILLOCATION = "Location:";
DuelFrame_DETAILOTHER = "Other:";
DuelFrame_DETAILDUELS = "Duels: ";
DuelFrame_DETAILOVERVIEW = "Wins / Losses: ";
DuelFrame_CURRENT_TAB = 1;
DuelFrame_ITEMS_PER_PAGE = 15;
DuelFrame_ITEM_HEIGHT = 19;

DuelFrame_SORT_FIELD_NAME = "name";
DuelFrame_SORT_FIELD_GUILD = "guild";
DuelFrame_SORT_FIELD_LEVEL = "level";
DuelFrame_SORT_FIELD_CLASS = "class";
DuelFrame_SORT_FIELD_RACE = "race";
DuelFrame_SORT_FIELD_RESULT = "result";
DuelFrame_SORT_FIELD_SCORE = "score";
DuelFrame_SORT_FIELDS = {DuelFrame_SORT_FIELD_NAME,DuelFrame_SORT_FIELD_GUILD,DuelFrame_SORT_FIELD_LEVEL,DuelFrame_SORT_FIELD_CLASS,DuelFrame_SORT_FIELD_RACE,DuelFrame_SORT_FIELD_RESULT,DuelFrame_SORT_FIELD_SCORE};
DuelFrame_SORT_ORDER_ASC = "ASC";
DuelFrame_SORT_ORDER_DSC = "DSC";
DuelFrame_CURRENT_SORT_FIELD = DuelFrame_SORT_FIELD_NAME;
DuelFrame_CURRENT_SORT_ORDER = DuelFrame_SORT_ORDER_ASC;

DuelFrame_SEARCH_NAME = nil;
DuelFrame_SEARCH_GUILD = nil;
DuelFrame_SEARCH_MIN_LEVEL = nil;
DuelFrame_SEARCH_MAX_LEVEL = nil;
DuelFrame_SEARCH_RACE = nil;
DuelFrame_SEARCH_CLASS = nil;

DuelFrame_SCORE_BASE = 1000;
DuelFrame_LEVEL_MULTIPLIER = .05;
DuelFrame_HEALTH_MULTIPLIER = .01;

DuelFrame_OPTIONS_RACE = {"Human","Night Elf","Dwarf","Gnome","Orc","Tauren","Troll","Undead"};
DuelFrame_OPTIONS_CLASS = {"Warrior","Paladin","Hunter","Rogue","Priest","Mage","Warlock","Druid","Shaman"};

UIPanelWindows["DuelFrame"] = { area = "left",	pushable = 5 };

--
-- Event Handlers
-- 

function DuelFrame_OnLoad()
	SlashCmdList["DUELDATABASE"] = DuelFrame_Toggle;
		SLASH_DUELDATABASE1 = "/dueldatabase";
		SLASH_DUELDATABASE2 = "/dd";
	
	PanelTemplates_SetNumTabs(this, 2);
	PanelTemplates_SetTab(DuelFrame, 1);
end

--
-- Library
--

function DuelFrame_Toggle(cmd)  
	if DuelFrame:IsVisible() then
		HideUIPanel(DuelFrame);
	else
		ShowUIPanel(DuelFrame);
		if (not Duelist_CurrentPlayer) then
  		Duelist_DatabaseInitialize();
  	end
		DuelFrameTab_OnClick(1);
	end
end

function DuelFrameTab_OnClick(tab)
	if (not tab) then
		tab = this:GetID();
	end
	PanelTemplates_SetTab(DuelFrame, tab);
	PlaySound("igCharacterInfoTab");
	DuelFrame_CURRENT_TAB = tab;
	DuelFrame_Update();
end

function DuelFrame_SortBy(field)   
  DuelFrame_CURRENT_SORT_FIELD = string.gsub(field,"DuelFrame_","");
  
  if (DuelFrame_CURRENT_SORT_FIELD) then
		if (DuelFrame_CURRENT_SORT_FIELD == field) then
			if (DuelFrame_CURRENT_SORT_ORDER == DuelFrame_SORT_ORDER_DSC) then
				DuelFrame_CURRENT_SORT_ORDER = DuelFrame_SORT_ORDER_ASC;
			else 
				DuelFrame_CURRENT_SORT_ORDER = DuelFrame_SORT_ORDER_DSC;
			end			
		else
			DuelFrame_CURRENT_SORT_ORDER = DuelFrame_SORT_ORDER_ASC;
		end
	end

	DuelFrame_Update();
end

function DuelFrame_Update()
	if (not Duelist_CurrentPlayer or Duelist_Player == Duelist_UNKNOWN) then
		Duelist_databaseInitialize()	
	end

	local _wins = 0;
	local _losses = 0;

  if (DuelDatabase and Duelist_CurrentPlayer) then
  	if (DuelFrame_CURRENT_TAB == 1) then
	  	DuelFrame_UpdateButtons(Duelist_CurrentPlayer.opponentsTable,true);				
	  else 
		  DuelFrame_UpdateButtons(Duelist_CurrentPlayer.opponentsTable,false);				  
		end
  end
  
  if (Duelist_CurrentPlayer) then
	  _wins = Duelist_CurrentPlayer.fightsWon;
	  _losses = Duelist_CurrentPlayer.fightsLost;
	  getglobal("DuelFrameWinText"):SetText("Wins: " .. _wins);
	  getglobal("DuelFrameLossText"):SetText("Losses: " .. _losses);
	  
	  if (_wins ~= 0 and _losses ~= 0) then
	  	local _pct = (_wins / (_wins + _losses)) * 100;
	  	_pct = string.sub(_pct,1,5);
	  	getglobal("DuelFramePctText"):SetText("Pct.: " .. _pct .. "%");
	  elseif (_wins ~= 0 and _losses == 0) then
	  	local _pct = (_wins / (_wins + _losses)) * 100;
	  	_pct = string.sub(_pct,1,5);
	  	getglobal("DuelFramePctText"):SetText("Pct.: " .. _pct .. "%");	
	 	elseif (_wins == 0 and _losses ~= 0) then
	  	local _pct = (_wins / (_wins + _losses)) * 100;
	  	_pct = string.sub(_pct,1,5);
	  	getglobal("DuelFramePctText"):SetText("Pct.: " .. _pct .. "%"); 
	  else 
	  	getglobal("DuelFramePctText"):SetText("Pct.: n/a");		
	  end						
	else
		getglobal("DuelFrameWinText"):SetText("Wins: n/a");
	  getglobal("DuelFrameLossText"):SetText("Losses: n/a");
	  getglobal("DuelFramePctText"):SetText("Pct.: n/a");	
	end
end

function DuelFrame_UpdateButtons(DuelTable,won)
  local _offset = FauxScrollFrame_GetOffset(DuelFrame_DuelScrollFrame);
  
  DuelFrame_FilteredTable = Duelist_findDuels(
  		DuelTable,
  		won,
  		Duelist_Faction,
  		DuelFrame_SEARCH_GUILD,
  		DuelFrame_SEARCH_NAME,
  		DuelFrame_SEARCH_MIN_LEVEL,
			DuelFrame_SEARCH_MAX_LEVEL,
			DuelFrame_SEARCH_RACE,
			DuelFrame_SEARCH_CLASS);  
  
  if (DuelFrame_CURRENT_SORT_FIELD == DuelFrame_SORT_FIELD_RESULT) then
  	if (DuelFrame_CURRENT_TAB == 1) then
	  	if (DuelFrame_CURRENT_SORT_ORDER == DuelFrame_SORT_ORDER_ASC) then
	  		table.sort(DuelFrame_FilteredTable,DuelFrame_sortByResultAsc);
	  	else
	  		table.sort(DuelFrame_FilteredTable,DuelFrame_sortByResultDsc);
	  	end
	  else
	  	if (DuelFrame_CURRENT_SORT_ORDER == DuelFrame_SORT_ORDER_ASC) then
	  		table.sort(DuelFrame_FilteredTable,DuelFrame_sortByResultAsc);
	  	else
	  		table.sort(DuelFrame_FilteredTable,DuelFrame_sortByResultDsc);
	  	end
		end			
  else  
	  if (DuelFrame_CURRENT_TAB == 1) then
	  	if (DuelFrame_CURRENT_SORT_ORDER == DuelFrame_SORT_ORDER_ASC) then
	  		table.sort(DuelFrame_FilteredTable,DuelFrame_sortByAsc);
	  	else
	  		table.sort(DuelFrame_FilteredTable,DuelFrame_sortByDsc);
	  	end
	  else
	  	if (DuelFrame_CURRENT_SORT_ORDER == DuelFrame_SORT_ORDER_ASC) then
	  		table.sort(DuelFrame_FilteredTable,DuelFrame_sortByAsc);
	  	else
	  		table.sort(DuelFrame_FilteredTable,DuelFrame_sortByDsc);
	  	end
		end
	end

	local _buttonpressed = getglobal("DuelFrame_"..DuelFrame_CURRENT_SORT_FIELD);
	
	for i=1,getn(DuelFrame_SORT_FIELDS) do
		if (DuelFrame_SORT_FIELDS[i] ~= field) then 
			local _column = getglobal("DuelFrame_"..DuelFrame_SORT_FIELDS[i]);
			getglobal(_column:GetName().."Arrow"):SetTexCoord(1.0, 0.5625, 0, 0);
		end
	end
	
	if (DuelFrame_CURRENT_SORT_ORDER == DuelFrame_SORT_ORDER_ASC) then
		getglobal(_buttonpressed:GetName().."Arrow"):SetTexCoord(0, 0.5625, 1.0, 0);
	else
		getglobal(_buttonpressed:GetName().."Arrow"):SetTexCoord(0, 0.5625, 0, 1.0);
	end
  
	for i=1,DuelFrame_ITEMS_PER_PAGE do 
	  
  	local _DuelName = "DuelFrame_Duel"..i;
  	
  	if (i+_offset > table.getn(DuelFrame_FilteredTable )) then
  		getglobal(_DuelName.."Opponent"):SetText("");
  		getglobal(_DuelName.."Guild"):SetText("");
  		getglobal(_DuelName.."Level"):SetText("");
	    getglobal(_DuelName.."Race"):SetText("");
	    getglobal(_DuelName.."Class"):SetText("");
	    getglobal(_DuelName.."Result"):SetText("");
	    getglobal(_DuelName.."Score"):SetText("");	
  	else 
  		local _Duel = DuelFrame_FilteredTable [i+_offset];
  		local _name = _Duel.name;
  		local _guild = _Duel.guild;
  		local _level = _Duel.level;
  		local _race = _Duel.race;
  		local _class = _Duel.class;
  		local _result = math.abs(_Duel.percentPlayer - _Duel.percentTarget);
  		local _score = _Duel.score;  		
 
			getglobal(_DuelName.."Opponent"):SetText(_name);
			
			if (_guild ~= nil) then
	  		getglobal(_DuelName.."Guild"):SetText(_guild);
	  	else
	  		getglobal(_DuelName.."Guild"):SetText("");
	    end
	    
	    getglobal(_DuelName.."Level"):SetText(_level);
	    getglobal(_DuelName.."Race"):SetText(_race);
	    getglobal(_DuelName.."Class"):SetText(_class);
	    getglobal(_DuelName.."Result"):SetText(_result.."%");
	    getglobal(_DuelName.."Score"):SetText(_score);  		  	
  	end  	  	
  end
  
  FauxScrollFrame_Update(DuelFrame_DuelScrollFrame, table.getn(DuelFrame_FilteredTable), DuelFrame_ITEMS_PER_PAGE, DuelFrame_ITEM_HEIGHT);
end

function DuelFrame_LoadDuel()
  local _offset = FauxScrollFrame_GetOffset(DuelFrame_DuelScrollFrame);
	if (DuelFrame_FilteredTable [this:GetID() + _offset]) then
		local _duel = DuelFrame_FilteredTable [this:GetID() + _offset];
		getglobal("DuelFrameDetailsPlayerText"):SetText(Duelist_CurrentPlayer.name);
		getglobal("DuelFrameDetailsOpponentText"):SetText(_duel.name);
		getglobal("DuelFrameDetailsGuildPlayerText"):SetText(_duel.pGuild);
		getglobal("DuelFrameDetailsGuildOpponentText"):SetText(_duel.guild);
		getglobal("DuelFrameDetailsLevelPlayerText"):SetText(_duel.pLevel);
		getglobal("DuelFrameDetailsLevelOpponentText"):SetText(_duel.level);
		getglobal("DuelFrameDetailsRacePlayerText"):SetText(Duelist_CurrentPlayer.race);
		getglobal("DuelFrameDetailsRaceOpponentText"):SetText(_duel.race);
		getglobal("DuelFrameDetailsClassPlayerText"):SetText(Duelist_CurrentPlayer.class);
		getglobal("DuelFrameDetailsClassOpponentText"):SetText(_duel.class);
		getglobal("DuelFrameDetailsHealthPlayerText"):SetText(_duel.pMaxHealth.."/".._duel.pHealth.." ".._duel.percentPlayer.."%");
		getglobal("DuelFrameDetailsHealthOpponentText"):SetText(_duel.maxHealth.."/".._duel.health.." ".._duel.percentTarget.."%");
		getglobal("DuelFrameDetailsMainLocationText"):SetText(_duel.location);
		if (_duel.location ~= _duel.sub) then
			getglobal("DuelFrameDetailsSubLocationText"):SetText(_duel.sub);
		else
			getglobal("DuelFrameDetailsSubLocationText"):SetText("");
		end
		getglobal("DuelFrameDetailsLevelDiffValueText"):SetText(_duel.levelDiff);
		getglobal("DuelFrameDetailsLevelMultiValue"):SetText(_duel.levelMulti);
		getglobal("DuelFrameDetailsHealthDiffValue"):SetText(_duel.healthDiff);
		getglobal("DuelFrameDetailsHealthMultiValue"):SetText(_duel.healthMulti);
		getglobal("DuelFrameVersionText"):SetText("Score: " .._duel.score);
		
		local _opponent = Duelist_getOpponent(_duel.faction,_duel.guild,_duel.name);
		
		if (_opponent) then
			local _duelswon = _opponent.won;
			local _duelsloss = _opponent.loss;
				
			getglobal("DuelFrameDetailsDuelsValue"):SetText(_duelswon + _duelsloss);
			getglobal("DuelFrameDetailsDuelsWonValue"):SetText(_duelswon);
			getglobal("DuelFrameDetailsDuelsLossValue"):SetText(_duelsloss);
		else 
			getglobal("DuelFrameDetailsDuelsWonValue"):SetText("");
			getglobal("DuelFrameDetailsDuelsLossValue"):SetText("");
			getglobal("DuelFrameVersionText"):SetText(DUELDATABASE_SHOW_VERSION);
		end
	else
		getglobal("DuelFrameDetailsPlayerText"):SetText("");
		getglobal("DuelFrameDetailsOpponentText"):SetText("");
		getglobal("DuelFrameDetailsGuildPlayerText"):SetText("");
		getglobal("DuelFrameDetailsGuildOpponentText"):SetText("");
		getglobal("DuelFrameDetailsLevelPlayerText"):SetText("");
		getglobal("DuelFrameDetailsLevelOpponentText"):SetText("");
		getglobal("DuelFrameDetailsRacePlayerText"):SetText("");
		getglobal("DuelFrameDetailsRaceOpponentText"):SetText("");
		getglobal("DuelFrameDetailsClassPlayerText"):SetText("");
		getglobal("DuelFrameDetailsClassOpponentText"):SetText("");
		getglobal("DuelFrameDetailsHealthPlayerText"):SetText("");
		getglobal("DuelFrameDetailsHealthOpponentText"):SetText("");
		getglobal("DuelFrameDetailsMainLocationText"):SetText("");
		getglobal("DuelFrameDetailsSubLocationText"):SetText("");
		getglobal("DuelFrameDetailsLevelDiffValueText"):SetText("");
		getglobal("DuelFrameDetailsLevelMultiValue"):SetText("");
		getglobal("DuelFrameDetailsHealthDiffValue"):SetText("");
		getglobal("DuelFrameDetailsHealthMultiValue"):SetText("");
		getglobal("DuelFrameDetailsDuelsValue"):SetText("");
		getglobal("DuelFrameDetailsDuelsWonValue"):SetText("");
		getglobal("DuelFrameDetailsDuelsLossValue"):SetText("");
		getglobal("DuelFrameVersionText"):SetText(DUELDATABASE_SHOW_VERSION);
	end
end

function DuelFrameRaceDropDown_OnLoad() 
	UIDropDownMenu_Initialize(this, DuelFrameRaceDropDown_Initialize);
end

function DuelFrameRaceDropDown_OnClick() 
	UIDropDownMenu_Initialize(this, DuelFrameRaceDropDown_Initialize);
end

function DuelFrameRaceDropDown_Initialize()
	info = {};
	info.text = "All";
	info.value = 0;
	info.func = DuelFrameRaceDropDown_OnClick;
	UIDropDownMenu_AddButton(info);
	
	for i=1,getn(DuelFrame_OPTIONS_RACE) do
		info = {};
		info.text = DuelFrame_OPTIONS_RACE[i];
		info.value = i;
		info.func = DuelFrameRaceDropDown_OnClick;
		UIDropDownMenu_AddButton(info);
	end
end

function DuelFrameRaceDropDown_OnClick()
	UIDropDownMenu_SetSelectedValue(DuelFrameRaceDropDown, this.value);
end

function DuelFrameClassDropDown_OnLoad() 
	UIDropDownMenu_Initialize(this, DuelFrameClassDropDown_Initialize);
end

function DuelFrameClassDropDown_OnClick() 
	UIDropDownMenu_Initialize(this, DuelFrameClassDropDown_Initialize);
end

function DuelFrameClassDropDown_Initialize()
	info = {};
	info.text = "All";
	info.value = 0;
	info.func = DuelFrameClassDropDown_OnClick;
	UIDropDownMenu_AddButton(info);
	
	for i=1,getn(DuelFrame_OPTIONS_CLASS) do
		info = {};
		info.text = DuelFrame_OPTIONS_CLASS[i];
		info.value = i;
		info.func = DuelFrameClassDropDown_OnClick;
		UIDropDownMenu_AddButton(info);
	end
end

function DuelFrameClassDropDown_OnClick()
	UIDropDownMenu_SetSelectedValue(DuelFrameClassDropDown, this.value);
end

function DuelFrame_Search()
  DuelFrame_SEARCH_NAME = getglobal("DuelFrameOpponentName"):GetText();
  DuelFrame_SEARCH_GUILD = getglobal("DuelFrameGuild"):GetText();
  DuelFrame_SEARCH_MIN_LEVEL = getglobal("DuelFrameMinLevel"):GetText();
  DuelFrame_SEARCH_MAX_LEVEL = getglobal("DuelFrameMaxLevel"):GetText();
	DuelFrame_SEARCH_RACE = UIDropDownMenu_GetSelectedValue(DuelFrameRaceDropDown);
	DuelFrame_SEARCH_CLASS = UIDropDownMenu_GetSelectedValue(DuelFrameClassDropDown);
	
	if (DuelFrame_SEARCH_MIN_LEVEL == "" or DuelFrame_SEARCH_MAX_LEVEL == "") then
		DuelFrame_SEARCH_MIN_LEVEL = nil;
		DuelFrame_SEARCH_MAX_LEVEL = nil;
	else
		DuelFrame_SEARCH_MIN_LEVEL = tonumber(DuelFrame_SEARCH_MIN_LEVEL);
		DuelFrame_SEARCH_MAX_LEVEL = tonumber(DuelFrame_SEARCH_MAX_LEVEL);	
	
		if ((DuelFrame_SEARCH_MIN_LEVEL == 0 or DuelFrame_SEARCH_MAX_LEVEL == 0) or (DuelFrame_SEARCH_MIN_LEVEL > 60 or DuelFrame_SEARCH_MAX_LEVEL > 60)) then
			DuelFrame_SEARCH_MIN_LEVEL = nil;
			DuelFrame_SEARCH_MAX_LEVEL = nil;
		elseif (DuelFrame_SEARCH_MAX_LEVEL < DuelFrame_SEARCH_MIN_LEVEL) then
			DuelFrame_SEARCH_MIN_LEVEL = nil;
			DuelFrame_SEARCH_MAX_LEVEL = nil;
	  end
	end
	
	if (DuelFrame_SEARCH_MIN_LEVEL == nil) then
		getglobal("DuelFrameMinLevel"):SetText("");
  	getglobal("DuelFrameMaxLevel"):SetText("");	
	end
	
	if (DuelFrame_SEARCH_NAME == "") then
		DuelFrame_SEARCH_NAME = nil;
	end	
	
	if (DuelFrame_SEARCH_GUILD == "") then
		DuelFrame_SEARCH_GUILD = nil;
	end
	
	if (DuelFrame_SEARCH_RACE ~= nil) then
		if (DuelFrame_SEARCH_RACE == 0) then
			DuelFrame_SEARCH_RACE = nil;
		else 
			DuelFrame_SEARCH_RACE = DuelFrame_OPTIONS_RACE[DuelFrame_SEARCH_RACE];
		end
	end
	
	if (DuelFrame_SEARCH_CLASS ~= nil) then
		if (DuelFrame_SEARCH_CLASS == 0) then
			DuelFrame_SEARCH_CLASS = nil;
		else 
			DuelFrame_SEARCH_CLASS = DuelFrame_OPTIONS_CLASS[DuelFrame_SEARCH_CLASS];
		end
	end
	
	DuelFrame_Update();
	
end

DuelFrame_sortByAsc = function(arg1,arg2)
  if (arg1 == nil or arg2 == nil) then
  	return true;
	elseif (arg1[DuelFrame_CURRENT_SORT_FIELD] == nil
					and arg2[DuelFrame_CURRENT_SORT_FIELD] ~= nil) then
		return true;
	elseif (arg1[DuelFrame_CURRENT_SORT_FIELD] ~= nil
					and arg2[DuelFrame_CURRENT_SORT_FIELD] == nil) then
		return false;
	elseif (arg1[DuelFrame_CURRENT_SORT_FIELD] == nil
					and arg2[DuelFrame_CURRENT_SORT_FIELD] == nil) then
		return;
	end
	
  if (string.lower(arg1[DuelFrame_CURRENT_SORT_FIELD]) < string.lower(arg2[DuelFrame_CURRENT_SORT_FIELD])) then
  	return true;
  else
  	return false;
  end
end

DuelFrame_sortByDsc = function(arg1,arg2)
  if (arg1 == nil or arg2 == nil) then
  	return true; 
	elseif (arg1[DuelFrame_CURRENT_SORT_FIELD] == nil
					and arg2[DuelFrame_CURRENT_SORT_FIELD] ~= nil) then
		return false;
	elseif (arg1[DuelFrame_CURRENT_SORT_FIELD] ~= nil
					and arg2[DuelFrame_CURRENT_SORT_FIELD] == nil) then
		return true;
	elseif (arg1[DuelFrame_CURRENT_SORT_FIELD] == nil
					and arg2[DuelFrame_CURRENT_SORT_FIELD] == nil) then
		return;
	end

  if (string.lower(arg1[DuelFrame_CURRENT_SORT_FIELD]) > string.lower(arg2[DuelFrame_CURRENT_SORT_FIELD])) then
  	return true;
  else
  	return false;
  end
end

DuelFrame_sortByResultAsc = function(arg1,arg2)
  local _percentArg1 = math.abs(arg1.percentPlayer - arg1.percentTarget);
  local _percentArg2 = math.abs(arg2.percentPlayer - arg2.percentTarget);
	if (_percentArg1 < _percentArg2) then
		return true;
	else
		return false;
	end
end

DuelFrame_sortByResultDsc = function(arg1,arg2)
  local _percentArg1 = math.abs(arg1.percentPlayer - arg1.percentTarget);
  local _percentArg2 = math.abs(arg2.percentPlayer - arg2.percentTarget);
	if (_percentArg1 > _percentArg2) then
		return true;
	else
		return false;
	end
end