#include "SearchObjForm.h"
#include "ViewForm.h"
//#include "DotNetString.h"

using namespace ObjDataEdit;
//--------------------------------------------------------------------------------------------------
void SearchObjForm::NPCSearch_Click( )
{
    bool    IsActive = false;
    String* Str="";

    if( CB_NpcRace->SelectedIndex  > 0 )
    {
        Str = String::Format( "WHERE Race ={0}" , __box(CB_NpcRace->SelectedIndex-1) );
        IsActive = true;
    }

    if( CB_NpcJob->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and Vocation = {1}" , Str , __box(CB_NpcJob->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE Vocation ={0}" , __box(CB_NpcJob->SelectedIndex-1) );
        IsActive = true;
    }

    if( CB_NpcSex->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and Sex = {1}" , Str , __box(CB_NpcSex->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE Sex ={0}" , __box(CB_NpcSex->SelectedIndex-1) );
        IsActive = true;
    }


    if( CB_NpcSex->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and Sex = {1}" , Str , __box(CB_NpcSex->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE Sex ={0}" , __box(CB_NpcSex->SelectedIndex-1) );
        IsActive = true;
    }

    if( CB_NpcZone->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and ( Zone1 = {1} or Zone2 = {1} or Zone3 = {1} )" , Str , __box(CB_NpcZone->SelectedIndex) );
        else
            Str = String::Format( "WHERE ( Zone1 = {0} or Zone2 = {0} or Zone3 = {0} )" , __box(CB_NpcZone->SelectedIndex) );
        IsActive = true;
    }

    int LV = DotNetStr::StringToInt( CB_NpcLV1->Text );
    int RangeLV = DotNetStr::StringToInt( CB_NpcLV2->Text );
    if( DotNetStr::StringToInt( CB_NpcLV1->Text ) != 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and ( Level >= {1} and Level <= {2} ) " , Str , __box( LV ) , __box( LV+RangeLV ) );
        else
            Str = String::Format( "WHERE ( Level >= {0} and Level <= {1} )" , __box( LV ) , __box( LV+RangeLV ) );
        IsActive = true;        
    }

    SetObjList( Str , "NPCObjectDB" );
 
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::SetObjList( String* Str , String* DB )
{

	int Flag = 0;
	for( int i = CList_Language->Items->Count -1  ; i >= 0 ; i -- )
	{
		Flag *= 2;
		if( CList_Language->GetItemChecked( i ) == true )
			Flag++;
	}

	if( Flag != 0 )
	{
		if( Str->Length  != 0 )
			Str = String::Format( "{0} and ( ({1}.OutputLanguage & {2}) != 0 )" , Str , DB , __box(Flag) );
		else
			Str = String::Format( "WHERE ( ({0}.OutputLanguage & {1}) != 0 )" , DB , __box(Flag) );

	}

	/*
	if( ED_Name->Text->Length != 0  )
	{
		if( Str->Length  != 0 )
			Str = String::Format( "{0} and ( StringDB.Content LIKE '%{1}%') " , Str , ED_Name->Text );
		else
			Str = String::Format( "WHERE ( StringDB.Content LIKE '%{0}%')" , ED_Name->Text );
	}*/


    if( ED_Name->Text->Length != 0  )
    {
        if( Str->Length  != 0 )
            //Str = String::Format( "{0} and ( StringDB.Content LIKE '%{1}%') " , Str , ED_Name->Text );
			Str = String::Format( "{0} and ( s.Content LIKE '%{1}%') " , Str , ED_Name->Text );
        else
            //Str = String::Format( "WHERE ( StringDB.Content LIKE '%{0}%')" , ED_Name->Text );
			Str = String::Format( "WHERE ( s.Content LIKE '%{0}%')" , ED_Name->Text );
    }

	if( ED_DataGroup->Text->Length != 0  )
	{
		if( Str->Length  != 0 )
			Str = String::Format( "{0} and ( DataGroup LIKE '%{1}%') " , Str , ED_DataGroup->Text );
		else
			Str = String::Format( "WHERE ( DataGroup LIKE '%{0}%')" , ED_DataGroup->Text );
	}

    if( ED_Account->Text->Length != 0  )
    {
        if( Str->Length  != 0 )
			Str = String::Format( "{0} and ( LastModifyAccount LIKE '%{1}%') " , Str , ED_Account->Text );
        else
            Str = String::Format( "WHERE ( LastModifyAccount LIKE '%{0}%')" , ED_Account->Text );
    }

	if( Str->Length == 0 )
		//Str = String::Format( "WHERE {0}.GUID = StringDB.OrgObjID AND StringDB.KeyStr LIKE 'Sys%[_]name'" , DB );
		Str = "WHERE s.KeyStr LIKE 'Sys%[_]name'";
	else
		//Str = String::Format( "{1} AND {0}.GUID = StringDB.OrgObjID AND StringDB.KeyStr LIKE 'Sys%[_]name'" , DB , Str );
		Str = String::Format( "{1} AND s.KeyStr LIKE 'Sys%[_]name'" , DB , Str );


    if( ListB_Sort->Items->Count != 0 )
    {
        Str = String::Format( "{0} Order By {1} " , Str , ListB_Sort->Items->Item[0] );
        for( int i = 1 ; i < ListB_Sort->Items->Count ; i++ )
        {
            Str = String::Format( "{0} , {1} " , Str , ListB_Sort->Items->Item[i] );
        }        

        if( CB_SortDirect->Checked == true )
        {
            Str = String::Format( "{0} DESC " , Str );
        }
    }
	String* CMD = String::Format( "SELECT n.GUID, s.Content, n.ImageID from {0} AS n inner join StringDB AS s on n.GUID = s.OrgObjID {1}" , DB , Str );
	//String* CMD = String::Format( "SELECT {0}.GUID, StringDB.Content, {0}.ImageID FROM {0} , StringDB {1} " , DB , Str );

    _SqlCmd = Str;

    int     i;
    SqlDataReader* myReader;
    sqlCommand->CommandText = CMD ;
    myReader = sqlCommand->ExecuteReader();

    _ObjList->clear();
    ListB_NameList->Items->Clear();
    while( myReader->Read())
    {
        DB_GameObjDbIndexStruct Temp;
        Temp.GUID = myReader->GetInt32( 0 );
        String* Str =  myReader->GetString( 1 ) ;
        Temp.ImageID        = myReader->GetInt32( 2 );

        strcpy( Temp.Name , DotNetStr::StringToChar( Str ).c_str() );

        _ObjList->push_back( Temp );
    }
    myReader->Close();

    ListB_NameList->BeginUpdate();

    for ( i = 0 ; i < (int)_ObjList->size() ; i++ )
    {
        String* Str = (*_ObjList)[i].Name;
        ListB_NameList->Items->Add( String::Format( "{0} {1}" 
            , __box( (*_ObjList)[i].GUID ) , Str ) );
    }
    ListB_NameList->EndUpdate();

    ListB_NameList_SelectedIndexChanged( NULL , NULL );
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadNPCForm()
{
    System::Object* AddItem[] = new System::Object*[1];
    AddItem[0] = (String*)"---";

    CB_NpcRace->Items->Clear();
    CB_NpcRace->Items->AddRange(AddItem);
    for( unsigned int i = 0 ; i < Global::RaceName.size() ; i++ )
    {
        String* Str =  Global::RaceName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CB_NpcRace->Items->AddRange(AddItem);

    }

    CB_NpcJob->Items->Clear();
    CB_NpcJob->Items->AddRange(AddItem);
    for( unsigned int i = 0 ; i < Global::JobName.size() ; i++ )
    {
        String* Str =  Global::JobName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CB_NpcJob->Items->AddRange(AddItem);

    }

    CB_NpcSex->Items->Clear();
    CB_NpcSex->Items->AddRange(AddItem);

    for( unsigned int i = 0 ; i < Global::SexName.size() ; i++ )
    {
        String* Str =  Global::SexName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CB_NpcSex->Items->AddRange(AddItem);
    }    



    CB_NpcZone->Items->Clear();
    for( unsigned int i = 0 ; i < Global::ZoneName.size() ; i++ )
    {
        String* Str =  Global::ZoneName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CB_NpcZone->Items->AddRange(AddItem);
    }
}
//--------------------------------------------------------------------------------------------------

Void SearchObjForm::timer1_Tick(System::Object *  sender, System::EventArgs *  e)
{
    if( _OnChange == false )
        return;
    _OnChange = false;

    switch( _ActionForm )
    {
    case EM_SearchFormTypeENUM_NPC:
        NPCSearch_Click( );
        break;
    case EM_SearchFormTypeENUM_Item:
        ItemSearch_Click( );
        break;
    case EM_SearchFormTypeENUM_Weapon:
        WeaponSearch_Click();
        break;
    case EM_SearchFormTypeENUM_Armor:
        ArmorSearch_Click();
        break;
    case EM_SearchFormTypeENUM_Magic:
        MagicSearch_Click();
        break;
    case EM_SearchFormTypeENUM_Image:
        ImageSearch_Click();
        break;
    case EM_SearchFormTypeENUM_Attribute:
        AttributeSearch_Click();
        break;
	case EM_SearchFormTypeENUM_QuestObj:
		QuestObjSearch_Click(); 
		break;
	case EM_SearchFormTypeENUM_Quest:
		QuestSearch_Click();
		break;
    case EM_SearchFormTypeENUM_LearnMagic:
        LearnMagic_Click();
        break;
    case EM_SearchFormTypeENUM_BodyObj:
        BodyObj_Click();
        break;
    case EM_SearchFormTypeENUM_Player:
        Player_Click();        
        break;
    case EM_SearchFormTypeENUM_KeyItem:
        KeyItem_Click();
        break;
    case EM_SearchFormTypeENUM_Rune:
        Rune_Click();
        break;
    case EM_SearchFormTypeENUM_Title:
        Title_Click();
        break;
    case EM_SearchFormTypeENUM_Flag:
        Flag_Click();
        break;
    case EM_SearchFormTypeENUM_Recipe:
        Recipe_Click();
        break;
    case EM_SearchFormTypeENUM_Mine:
        Mine_Click();
        break;
    case EM_SearchFormTypeENUM_Shop:
        Shop_Click();
        break;
	case EM_SearchFormTypeENUM_Suit:
		Suit_Click();
		break;
    case EM_SearchFormTypeENUM_SrvScript:
        SrvScript_Click();
        break;
	case EM_SearchFormTypeENUM_CliScript:
		CliScript_Click();
		break;

	case EM_SearchFormTypeENUM_Treasure:
		Treasure_Click();
		break;
	case EM_SearchFormTypeENUM_MagicCollect:
		MagicCollect_Click();
		break;

	case EM_SearchFormTypeENUM_EqRefineAbility:
		EqRefineAbility_Click();
		break;

	case EM_SearchFormTypeENUM_Zone:
		Zone_Click();
		break;

	case EM_SearchFormTypeENUM_CreateRole:
		CreateRole_Click();
		break;

	case EM_SearchFormTypeENUM_Card:
		Card_Click();
		break;
	case EM_SearchFormTypeENUM_PE:
		PE_Click();
		break;
    default:
        break;
    }
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadItemForm( )
{
    CLB_ItemRaceLimit->Items->Clear();
    for( unsigned int i = 0 ; i < Global::RaceName.size() ; i++ )
    {
        String* Str =  Global::RaceName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CLB_ItemRaceLimit->Items->AddRange(AddItem);

    }

    CLB_ItemVocLimit->Items->Clear();
    for( unsigned int i = 0 ; i < Global::JobName.size() ; i++ )
    {
        String* Str =  Global::JobName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CLB_ItemVocLimit->Items->AddRange(AddItem);

    }

    CLB_ItemSexLimit->Items->Clear();
    for( unsigned int i = 0 ; i < Global::SexName.size() ; i++ )
    {
        String* Str =  Global::SexName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CLB_ItemSexLimit->Items->AddRange(AddItem);
    }    

}
//--------------------------------------------------------------------------------------------------
void SearchObjForm::ItemSearch_Click( )
{   
    bool    IsActive = false;
    String* Str="";

    if( CB_ItemRare->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and Rare = {1}" , Str , __box(CB_ItemRare->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE Rare ={0}" , __box(CB_ItemRare->SelectedIndex-1) );
        IsActive = true;
    }

    if( CB_ItemType->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and ItemType = {1}" , Str , __box(CB_ItemType->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE ItemType ={0}" , __box(CB_ItemType->SelectedIndex-1) );
        IsActive = true;
    }

    if( IsActive  == true )
    {
        Str = String::Format( "{0} and ( LimitRace&{1} = {1} ) and ( LimitVoc&{2} = {2} ) " 
                , Str 
                , __box( GetCheckListBoxValue( CLB_ItemRaceLimit ) )
                , __box( GetCheckListBoxValue( CLB_ItemVocLimit  ) )    );
        Str = String::Format( "{0} and ( LimitSex&{1} = {1} ) " 
                , Str                 
                , __box( GetCheckListBoxValue( CLB_ItemSexLimit  ) )    );
    }
    else
    {
        Str = String::Format( "WHERE ( LimitRace&{0} = {0} ) and ( LimitVoc&{1} = {1} ) and ( LimitSex&{2} = {2} )" 
                , __box( GetCheckListBoxValue( CLB_ItemRaceLimit ) )
                , __box( GetCheckListBoxValue( CLB_ItemVocLimit  ) )
                , __box( GetCheckListBoxValue( CLB_ItemSexLimit  ) )    );
    }

    SetObjList( Str , "ItemObjectDB" );

}
//--------------------------------------------------------------------------------------------------
int    SearchObjForm::GetCheckListBoxValue( CheckedListBox* CBox )
{
    int Flag = 0;
    for( int i = CBox->Items->Count -1  ; i >= 0 ; i -- )
    {
        Flag *= 2;
        if( CBox->GetItemChecked( i ) == true )
            Flag++;
    }

    return Flag;
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadWeaponForm( )
{
    CL_WeaponRaceLimit->Items->Clear();
    for( unsigned int i = 0 ; i < Global::RaceName.size() ; i++ )
    {
        String* Str =  Global::RaceName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CL_WeaponRaceLimit->Items->AddRange(AddItem);

    }

    CL_WeaponVocLimit->Items->Clear();
    for( unsigned int i = 0 ; i < Global::JobName.size() ; i++ )
    {
        String* Str =  Global::JobName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CL_WeaponVocLimit->Items->AddRange(AddItem);

    }

    CL_WeaponSexLimit->Items->Clear();
    for( unsigned int i = 0 ; i < Global::SexName.size() ; i++ )
    {
        String* Str =  Global::SexName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CL_WeaponSexLimit->Items->AddRange(AddItem);
    } 
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::WeaponSearch_Click( )
{
    bool    IsActive = false;
    String* Str="";

    if( CB_WeaponRare->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and Rare = {1}" , Str , __box(CB_WeaponRare->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE Rare ={0}" , __box(CB_WeaponRare->SelectedIndex-1) );
        IsActive = true;
    }

    if( CB_WeaponType->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and WeaponType = {1}" , Str , __box(CB_WeaponType->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE WeaponType ={0}" , __box(CB_WeaponType->SelectedIndex-1) );
        IsActive = true;
    }

    if( CB_WeaponPos->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and WeaponPos = {1}" , Str , __box(CB_WeaponPos->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE WeaponPos ={0}" , __box(CB_WeaponPos->SelectedIndex-1) );
        IsActive = true;
    }

    if( IsActive  == true )
    {
        Str = String::Format( "{0} and ( LimitRace&{1} = {1} ) and ( LimitVoc&{2} = {2} ) " 
            , Str 
            , __box( GetCheckListBoxValue( CL_WeaponRaceLimit ) )
            , __box( GetCheckListBoxValue( CL_WeaponVocLimit  ) )    );
        Str = String::Format( "{0} and ( LimitSex&{1} = {1} ) " 
            , Str                 
            , __box( GetCheckListBoxValue( CL_WeaponSexLimit  ) )    );
	}
	else
	{
		Str = String::Format( "WHERE ( LimitRace&{0} = {0} ) and ( LimitVoc&{1} = {1} ) and ( LimitSex&{2} = {2} )" 
			, __box( GetCheckListBoxValue( CL_WeaponRaceLimit ) )
			, __box( GetCheckListBoxValue( CL_WeaponVocLimit  ) )
			, __box( GetCheckListBoxValue( CL_WeaponSexLimit  ) )    );
	}

    SetObjList( Str ,"WeaponObjectDB" );
}

//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadArmorForm( )
{
    CL_ArmorRaceLimit->Items->Clear();
    for( unsigned int i = 0 ; i < Global::RaceName.size() ; i++ )
    {
        String* Str =  Global::RaceName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CL_ArmorRaceLimit->Items->AddRange(AddItem);

    }

    CL_ArmorVocLimit->Items->Clear();
    for( unsigned int i = 0 ; i < Global::JobName.size() ; i++ )
    {
        String* Str =  Global::JobName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CL_ArmorVocLimit->Items->AddRange(AddItem);

    }

    CL_ArmorSexLimit->Items->Clear();
    for( unsigned int i = 0 ; i < Global::SexName.size() ; i++ )
    {
        String* Str =  Global::SexName[i].c_str();
        System::Object* AddItem[] = new System::Object*[1];
        AddItem[0] = Str;
        CL_ArmorSexLimit->Items->AddRange(AddItem);
    } 
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::ArmorSearch_Click()
{
    bool    IsActive = false;
    String* Str="";

    if( CB_ArmorRare->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and Rare = {1}" , Str , __box(CB_ArmorRare->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE Rare ={0}" , __box(CB_ArmorRare->SelectedIndex-1) );
        IsActive = true;
    }

    if( CB_ArmorType->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and ArmorType = {1}" , Str , __box(CB_ArmorType->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE ArmorType ={0}" , __box(CB_ArmorType->SelectedIndex-1) );
        IsActive = true;
    }

    if( CB_ArmorPos->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and ArmorPos = {1}" , Str , __box(CB_ArmorPos->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE ArmorPos ={0}" , __box(CB_ArmorPos->SelectedIndex-1) );
        IsActive = true;
    }

    if( IsActive  == true )
    {
        Str = String::Format( "{0} and ( LimitRace&{1} = {1} ) and ( LimitVoc&{2} = {2} ) " 
            , Str 
            , __box( GetCheckListBoxValue( CL_ArmorRaceLimit ) )
            , __box( GetCheckListBoxValue( CL_ArmorVocLimit  ) )    );
        Str = String::Format( "{0} and ( LimitSex&{1} = {1} ) " 
            , Str                 
            , __box( GetCheckListBoxValue( CL_ArmorSexLimit  ) )    );
    }
    else
    {
        Str = String::Format( "WHERE ( LimitRace&{0} = {0} ) and ( LimitVoc&{1} = {1} ) and ( LimitSex&{2} = {2} )" 
            , __box( GetCheckListBoxValue( CL_ArmorRaceLimit ) )
            , __box( GetCheckListBoxValue( CL_ArmorVocLimit  ) )
            , __box( GetCheckListBoxValue( CL_ArmorSexLimit  ) )    );
    }


    SetObjList( Str , "ArmorObjectDB" );

}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::ShowDialogBox( SearchFormTypeENUM Type )
{
    if( tabControl1->TabPages->Count > Type )
         tabControl1->SelectedTab = tabControl1->TabPages->Item[ Type ];

    _RequestFormType = Type;
    _IsAccept = false;
    ShowDialog();
}
void    SearchObjForm::Init() 
{ 
    g_SearchObjForm = new SearchObjForm;
};
int SearchObjForm::SelectID()
{
    int SelectID = ListB_NameList->SelectedIndex;
    if( (unsigned)SelectID >= _ObjList->size() )
        return 0;
    return (*_ObjList)[SelectID].GUID;
}

String* SearchObjForm::SqlCmd( )
{
    if( _ActionForm != _RequestFormType )
    {
        MessageBox::Show( "要求命令有問題" , "錯誤" );
        return "";
    }
    return _SqlCmd;
}

bool    SearchObjForm::IsAccept( )
{
    return _IsAccept;
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadAttributeForm( )
{

}

void    SearchObjForm::AttributeSearch_Click( )
{
    bool    IsActive = false;
    String* Str="";

    if( CB_AttrType->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and n.Type = {1}" , Str , __box(CB_AttrType->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE n.Type ={0}" , __box(CB_AttrType->SelectedIndex-1) );
        IsActive = true;
    }
/*
    if( ED_Name->Text->Length != 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and ( Name LIKE '%{1}%') " , Str , ED_Name->Text );
        else
            Str = String::Format( "WHERE ( Name LIKE '%{0}%')" , ED_Name->Text );
        IsActive = true;
    }
*/
    int LV = DotNetStr::StringToInt( CB_AttrInheritLV->Text );
    int Range = DotNetStr::StringToInt( CB_AttrInheritLV_Range->Text );
    if( LV != 0 || Range != 0 )
    {
        if( Range == 0 )
            Range = 100;
        if( IsActive  == true )
        {
            Str = String::Format( "{0} and ( InheritLV + InheritLV_Range  >= {1} ) and InheritLV <= {2}" 
                , Str  , __box( LV ) , __box( LV+Range ) );
        }
        else
        {
            Str = String::Format( "WHERE ( InheritLV + InheritLV_Range  >= {0} ) and InheritLV <= {1}" 
                 , __box( LV ) , __box( LV+Range ) );
        }
        IsActive  = true ;
    }

    if( IsActive  == true )
    {
        Str = String::Format( "{0} and ( AddPos&{1} = {1} )" 
            , Str             
            , __box( GetCheckListBoxValue( CLB_AttrPos  ) )    );

    }
    else
    {
        Str = String::Format( "WHERE ( AddPos&{0} = {0} )" 
            , __box( GetCheckListBoxValue( CLB_AttrPos ) )  );
    }
    
    SetObjList( Str , "AddPowerObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadImageForm( )
{

}

void    SearchObjForm::ImageSearch_Click( )
{
    bool    IsActive = false;
    String* Str="";

    if( CB_ImageType->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and ImageType = {1}" , Str , __box(CB_ImageType->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE ImageType ={0}" , __box(CB_ImageType->SelectedIndex-1) );
        IsActive = true;
    }

    SetObjList( Str ,"ImageObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadMagicForm( )
{

}

void    SearchObjForm::MagicSearch_Click( )
{
    bool    IsActive = false;
    String* Str="";

    if( CB_MagicTargetType->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and TargetType = {1}" , Str , __box(CB_MagicTargetType->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE TargetType ={0}" , __box(CB_MagicTargetType->SelectedIndex-1) );
        IsActive = true;
    }

    if( CB_MagicType->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and MagicFunc = {1}" , Str , __box(CB_MagicType->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE MagicFunc ={0}" , __box(CB_MagicType->SelectedIndex-1) );
        IsActive = true;
    }

    if( CB_MagicRangeType->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and RangeType = {1}" , Str , __box(CB_MagicRangeType->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE RangeType ={0}" , __box(CB_MagicRangeType->SelectedIndex-1) );
        IsActive = true;
    }

    SetObjList( Str , "MagicObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadCliScript()
{

}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadSrvScript()
{

}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadMagicCollect()
{

}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadCard()
{

}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadTreasure()
{

}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::SrvScript_Click()
{
    bool    IsActive = false;
    String* Str="";

    if( CB_ScriptType->SelectedIndex  > 0 )
    {
        if( IsActive  == true )
            Str = String::Format( "{0} and n.Type = {1}" , Str , __box(CB_ScriptType->SelectedIndex-1) );
        else
            Str = String::Format( "WHERE n.Type ={0}" , __box(CB_ScriptType->SelectedIndex-1) );
        IsActive = true;
    }
  
    SetObjList( Str ,"ScriptObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::CliScript_Click()
{
	bool    IsActive = false;
	String* Str="";

	if( CB_ScriptType->SelectedIndex  > 0 )
	{
		if( IsActive  == true )
			Str = String::Format( "{0} and Type = {1}" , Str , __box(CB_ScriptType->SelectedIndex-1) );
		else
			Str = String::Format( "WHERE Type ={0}" , __box(CB_ScriptType->SelectedIndex-1) );
		IsActive = true;
	}

	SetObjList( Str , "CliScriptObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::Treasure_Click()
{
	bool    IsActive = false;
	String* Str="";

	SetObjList( Str , "TreasureObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::EqRefineAbility_Click()
{
	bool    IsActive = false;
	String* Str="";

	SetObjList( Str , "EqRefineAbilityObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::Zone_Click()
{
	bool    IsActive = false;
	String* Str="";

	SetObjList( Str , "ZoneObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::CreateRole_Click()
{
	bool    IsActive = false;
	String* Str="";

	SetObjList( Str , "CreateRoleObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::Card_Click()
{
	bool    IsActive = false;
	String* Str="";

	SetObjList( Str , "CardObjectDB" );
}

void	SearchObjForm::PE_Click()
{
	bool    IsActive = false;
	String* Str="";

	SetObjList( Str , "PEObjectDB" );
}

//--------------------------------------------------------------------------------------------------
void    SearchObjForm::MagicCollect_Click()
{
	bool    IsActive = false;
	String* Str="";

	SetObjList( Str , "MagicCollectObjectDB" );
}
//--------------------------------------------------------------------------------------------------
System::Void SearchObjForm::ListB_NameList_SelectedIndexChanged(System::Object *  sender, System::EventArgs *  e)
{
    if( (unsigned)ListB_NameList->SelectedIndex >= _ObjList->size() )
    {
        ViewForm::g_ViewForm->SetObjInfo( 0 );
        return;
    }

    int ImageID = (*_ObjList)[ ListB_NameList->SelectedIndex ].ImageID;

    ViewForm::g_ViewForm->SetObjInfo( ImageID );
}
//--------------------------------------------------------------------------------------------------
void	SearchObjForm::QuestObjSearch_Click()
{
	bool    IsActive = false;
	String* Str="";

	if( combo_QuestObjType->SelectedIndex > 0 )
	{
		if( IsActive  == true )
			Str = String::Format( "{0} and iQuestMode = {1}" , Str , __box( combo_QuestObjType->SelectedIndex-1 ) );
		else
			Str = String::Format( "WHERE iQuestMode = {0}" , __box( combo_QuestObjType->SelectedIndex-1 ) );
		IsActive = true;
	}

	SetObjList( Str , "QuestNPCObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void	SearchObjForm::QuestSearch_Click()
{
	bool    IsActive = false;
	String* Str="";

	SetObjList( Str , "QuestDetailObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void	SearchObjForm::OnLoadQuestObj()
{

}
//--------------------------------------------------------------------------------------------------
void	SearchObjForm::OnLoadQuest()
{

}
//--------------------------------------------------------------------------------------------------
void	SearchObjForm::OnLoadSuit()
{

}

void    SearchObjForm::Suit_Click()
{
	String* Str = "";

	SetObjList( Str , "SuitObjectDB" );
}
//--------------------------------------------------------------------------------------------------
void SearchObjForm::SetViewFormInfo( SearchFormTypeStruct& Info )
{
	ProcDBName = (String*)Info.DataBaseName;

    tabControl1->Controls->Clear();
    for( int i = 0 ; i < _PageList->Count ; i++ )
    {
        TabPage* Page = ( __try_cast<TabPage*> (_PageList->Item[i]  ) );
        String* Tag = static_cast<String*>( Page->Tag );
        SearchFormTypeENUM FormID = (SearchFormTypeENUM) DotNetStr::StringToInt( Tag );

        if( Info.TypeArr[ FormID ] == true )
        {
            tabControl1->Controls->Add( Page );
        }
        else
        {
            //tabControl2->Controls->Add( Page );
        }
    }
}
//--------------------------------------------------------------------------------------------------
void SearchObjForm::SetSortInfo( )
{
    String* Tag = static_cast<String*>(tabControl1->SelectedTab->Tag);
    _ActionForm = (SearchFormTypeENUM) DotNetStr::StringToInt( Tag );

    _OnChange   = true;
    ListB_AllSort->Items->Clear();
    ListB_Sort->Items->Clear();
    ListB_AllSort->Items->Add( (String*)"StringDB.Content" );
	ListB_AllSort->Items->Add( (String*)"DataGroup" );
    switch( _ActionForm )
    {
    case EM_SearchFormTypeENUM_NPC:
        ListB_AllSort->Items->Add( (String*)"Race" );
        ListB_AllSort->Items->Add( (String*)"Vocation" );
        ListB_AllSort->Items->Add( (String*)"Sex" );
        ListB_AllSort->Items->Add( (String*)"Level" );
        break;
    case EM_SearchFormTypeENUM_Item:
        ListB_AllSort->Items->Add( (String*)"ItemType" );
        ListB_AllSort->Items->Add( (String*)"Rare" );
        ListB_AllSort->Items->Add( (String*)"MaxWorldCount" );
        ListB_AllSort->Items->Add( (String*)"GenerateRate" );
        ListB_AllSort->Items->Add( (String*)"Cost" );
        ListB_AllSort->Items->Add( (String*)"DisappearRate" );
        ListB_AllSort->Items->Add( (String*)"SrcRecipe" );

        ListB_AllSort->Items->Add( (String*)"LimitLV" );
        ListB_AllSort->Items->Add( (String*)"LimitStr" );
        ListB_AllSort->Items->Add( (String*)"LimitSta" );
        ListB_AllSort->Items->Add( (String*)"LimitInt" );
        ListB_AllSort->Items->Add( (String*)"LimitMnd" );
        ListB_AllSort->Items->Add( (String*)"LimitAgi" );
        break;
    case EM_SearchFormTypeENUM_Weapon:
        ListB_AllSort->Items->Add( (String*)"ItemType" );
        ListB_AllSort->Items->Add( (String*)"Rare" );
        ListB_AllSort->Items->Add( (String*)"MaxWorldCount" );
        ListB_AllSort->Items->Add( (String*)"GenerateRate" );
        ListB_AllSort->Items->Add( (String*)"Cost" );
        ListB_AllSort->Items->Add( (String*)"DisappearRate" );
        ListB_AllSort->Items->Add( (String*)"SrcRecipe" );

        ListB_AllSort->Items->Add( (String*)"WeaponPos" );
        ListB_AllSort->Items->Add( (String*)"WeaponType" );
        ListB_AllSort->Items->Add( (String*)"HoleBase" );
        ListB_AllSort->Items->Add( (String*)"HoleRange" );
        ListB_AllSort->Items->Add( (String*)"AddPowerOn" );
        ListB_AllSort->Items->Add( (String*)"Durable" );

        ListB_AllSort->Items->Add( (String*)"LimitLV" );
        ListB_AllSort->Items->Add( (String*)"LimitStr" );
        ListB_AllSort->Items->Add( (String*)"LimitSta" );
        ListB_AllSort->Items->Add( (String*)"LimitInt" );
        ListB_AllSort->Items->Add( (String*)"LimitMnd" );
        ListB_AllSort->Items->Add( (String*)"LimitAgi" );

        ListB_AllSort->Items->Add( (String*)"ATK" );
        ListB_AllSort->Items->Add( (String*)"Def" );
        ListB_AllSort->Items->Add( (String*)"MDef" );
        ListB_AllSort->Items->Add( (String*)"DMG" );
        ListB_AllSort->Items->Add( (String*)"MPow" );
        ListB_AllSort->Items->Add( (String*)"MAtk" );
        ListB_AllSort->Items->Add( (String*)"STR" );
        ListB_AllSort->Items->Add( (String*)"STA" );
        ListB_AllSort->Items->Add( (String*)"INT" );
        ListB_AllSort->Items->Add( (String*)"MND" );
        ListB_AllSort->Items->Add( (String*)"AGI" );
        ListB_AllSort->Items->Add( (String*)"AtkSpeed" );

        ListB_AllSort->Items->Add( (String*)"MoveSpeed" );
        ListB_AllSort->Items->Add( (String*)"HitRate" );
        ListB_AllSort->Items->Add( (String*)"DodgeRate" );
        ListB_AllSort->Items->Add( (String*)"ParryRate" );
        ListB_AllSort->Items->Add( (String*)"StateHiteRate" );
        ListB_AllSort->Items->Add( (String*)"CritHitRate" );
        ListB_AllSort->Items->Add( (String*)"MaxHP" );
        ListB_AllSort->Items->Add( (String*)"MaxMP" );
        ListB_AllSort->Items->Add( (String*)"MaxSP" );
        ListB_AllSort->Items->Add( (String*)"ReHP" );
        ListB_AllSort->Items->Add( (String*)"ReMP" );
        ListB_AllSort->Items->Add( (String*)"ReSP" );

        ListB_AllSort->Items->Add( (String*)"Earth" );
        ListB_AllSort->Items->Add( (String*)"Water" );
        ListB_AllSort->Items->Add( (String*)"Fire" );
        ListB_AllSort->Items->Add( (String*)"Wind" );
        ListB_AllSort->Items->Add( (String*)"Light" );
        ListB_AllSort->Items->Add( (String*)"Darkness" );

        break;
    case EM_SearchFormTypeENUM_Armor:
        ListB_AllSort->Items->Add( (String*)"ItemType" );
        ListB_AllSort->Items->Add( (String*)"Rare" );
        ListB_AllSort->Items->Add( (String*)"MaxWorldCount" );
        ListB_AllSort->Items->Add( (String*)"GenerateRate" );
        ListB_AllSort->Items->Add( (String*)"Cost" );
        ListB_AllSort->Items->Add( (String*)"DisappearRate" );
        ListB_AllSort->Items->Add( (String*)"SrcRecipe" );

        ListB_AllSort->Items->Add( (String*)"ArmorPos" );
        ListB_AllSort->Items->Add( (String*)"ArmorType" );
        ListB_AllSort->Items->Add( (String*)"HoleBase" );
        ListB_AllSort->Items->Add( (String*)"HoleRange" );
        ListB_AllSort->Items->Add( (String*)"AddPowerOn" );
        ListB_AllSort->Items->Add( (String*)"Durable" );

        ListB_AllSort->Items->Add( (String*)"LimitLV" );
        ListB_AllSort->Items->Add( (String*)"LimitStr" );
        ListB_AllSort->Items->Add( (String*)"LimitSta" );
        ListB_AllSort->Items->Add( (String*)"LimitInt" );
        ListB_AllSort->Items->Add( (String*)"LimitMnd" );
        ListB_AllSort->Items->Add( (String*)"LimitAgi" );

        ListB_AllSort->Items->Add( (String*)"ATK" );
        ListB_AllSort->Items->Add( (String*)"Def" );
        ListB_AllSort->Items->Add( (String*)"MDef" );
        ListB_AllSort->Items->Add( (String*)"DMG" );
        ListB_AllSort->Items->Add( (String*)"MPow" );
        ListB_AllSort->Items->Add( (String*)"MAtk" );
        ListB_AllSort->Items->Add( (String*)"STR" );
        ListB_AllSort->Items->Add( (String*)"STA" );
        ListB_AllSort->Items->Add( (String*)"INT" );
        ListB_AllSort->Items->Add( (String*)"MND" );
        ListB_AllSort->Items->Add( (String*)"AGI" );
        ListB_AllSort->Items->Add( (String*)"AtkSpeed" );

        ListB_AllSort->Items->Add( (String*)"MoveSpeed" );
        ListB_AllSort->Items->Add( (String*)"HitRate" );
        ListB_AllSort->Items->Add( (String*)"DodgeRate" );
        ListB_AllSort->Items->Add( (String*)"ParryRate" );
        ListB_AllSort->Items->Add( (String*)"StateHiteRate" );
        ListB_AllSort->Items->Add( (String*)"CritHitRate" );
        ListB_AllSort->Items->Add( (String*)"MaxHP" );
        ListB_AllSort->Items->Add( (String*)"MaxMP" );
        ListB_AllSort->Items->Add( (String*)"MaxSP" );
        ListB_AllSort->Items->Add( (String*)"ReHP" );
        ListB_AllSort->Items->Add( (String*)"ReMP" );
        ListB_AllSort->Items->Add( (String*)"ReSP" );

        ListB_AllSort->Items->Add( (String*)"Earth" );
        ListB_AllSort->Items->Add( (String*)"Water" );
        ListB_AllSort->Items->Add( (String*)"Fire" );
        ListB_AllSort->Items->Add( (String*)"Wind" );
        ListB_AllSort->Items->Add( (String*)"Light" );
        ListB_AllSort->Items->Add( (String*)"Darkness" );
        break;
    case EM_SearchFormTypeENUM_Magic:
        break;
    case EM_SearchFormTypeENUM_Image :
        ListB_AllSort->Items->Add( (String*)"ImageType" );
        break;
    case EM_SearchFormTypeENUM_Attribute:
		/*
        ListB_AllSort->Items->Add( (String*)"Rare" );
        ListB_AllSort->Items->Add( (String*)"LimitLV" );
        ListB_AllSort->Items->Add( (String*)"LimitStr" );
        ListB_AllSort->Items->Add( (String*)"LimitSta" );
        ListB_AllSort->Items->Add( (String*)"LimitInt" );
        ListB_AllSort->Items->Add( (String*)"LimitMnd" );
        ListB_AllSort->Items->Add( (String*)"LimitAgi" );

        ListB_AllSort->Items->Add( (String*)"ATK" );
        ListB_AllSort->Items->Add( (String*)"Def" );
        ListB_AllSort->Items->Add( (String*)"MDef" );
        ListB_AllSort->Items->Add( (String*)"DMG" );
        ListB_AllSort->Items->Add( (String*)"MPow" );
        ListB_AllSort->Items->Add( (String*)"MAtk" );
        ListB_AllSort->Items->Add( (String*)"STR" );
        ListB_AllSort->Items->Add( (String*)"STA" );
        ListB_AllSort->Items->Add( (String*)"INT" );
        ListB_AllSort->Items->Add( (String*)"MND" );
        ListB_AllSort->Items->Add( (String*)"AGI" );
        ListB_AllSort->Items->Add( (String*)"AtkSpeed" );

        ListB_AllSort->Items->Add( (String*)"MoveSpeed" );
        ListB_AllSort->Items->Add( (String*)"HitRate" );
        ListB_AllSort->Items->Add( (String*)"DodgeRate" );
        ListB_AllSort->Items->Add( (String*)"ParryRate" );
        ListB_AllSort->Items->Add( (String*)"StateHiteRate" );
        ListB_AllSort->Items->Add( (String*)"CritHitRate" );
        ListB_AllSort->Items->Add( (String*)"MaxHP" );
        ListB_AllSort->Items->Add( (String*)"MaxMP" );
        ListB_AllSort->Items->Add( (String*)"MaxSP" );
        ListB_AllSort->Items->Add( (String*)"ReHP" );
        ListB_AllSort->Items->Add( (String*)"ReMP" );
        ListB_AllSort->Items->Add( (String*)"ReSP" );

        ListB_AllSort->Items->Add( (String*)"Earth" );
        ListB_AllSort->Items->Add( (String*)"Water" );
        ListB_AllSort->Items->Add( (String*)"Fire" );
        ListB_AllSort->Items->Add( (String*)"Wind" );
        ListB_AllSort->Items->Add( (String*)"Light" );
        ListB_AllSort->Items->Add( (String*)"Darkness" );
*/

        break;
    case EM_SearchFormTypeENUM_QuestObj:
        break;
    case EM_SearchFormTypeENUM_Quest:
        break;
    case EM_SearchFormTypeENUM_LearnMagic:
        break;
    case EM_SearchFormTypeENUM_BodyObj:
        break;
    case EM_SearchFormTypeENUM_Player:
        break;
    case EM_SearchFormTypeENUM_KeyItem:
        break;
    case EM_SearchFormTypeENUM_Rune:
        break;
    case EM_SearchFormTypeENUM_Title:
        break;
    case EM_SearchFormTypeENUM_Flag:
        break;
    case EM_SearchFormTypeENUM_Recipe:
        break;
	case EM_SearchFormTypeENUM_Shop:
		break;
	case EM_SearchFormTypeENUM_Suit:
		break;

    case EM_SearchFormTypeENUM_SrvScript:
	case EM_SearchFormTypeENUM_CliScript:
        ListB_AllSort->Items->Add( (String*)"FuncName" );
        break;
    case EM_SearchFormTypeENUM_Treasure:
		break;
	case EM_SearchFormTypeENUM_MagicCollect:
		break;
	case EM_SearchFormTypeENUM_EqRefineAbility:
		break;
	case EM_SearchFormTypeENUM_Zone:
		break;
	case EM_SearchFormTypeENUM_CreateRole:
		break;
	case EM_SearchFormTypeENUM_Card:
		break;
    case EM_SearchFormTypeENUM_Unknow:
        break;
	case EM_SearchFormTypeENUM_PE:
		break;
    }
}
//--------------------------------------------------------------------------------------------------
void    SearchObjForm::OnLoadLearnMagic()
{
}
void    SearchObjForm::OnLoadBodyObj()
{
}
void    SearchObjForm::OnLoadPlayer()
{
}
void    SearchObjForm::OnLoadKeyItem()
{
}
void    SearchObjForm::OnLoadRune()
{
}
void    SearchObjForm::OnLoadTitle()
{
}
void    SearchObjForm::OnLoadFlag()
{
}
void    SearchObjForm::OnLoadRecipe()
{
}
void    SearchObjForm::OnLoadMine()
{

}
void    SearchObjForm::OnLoadShop()
{

}


void    SearchObjForm::LearnMagic_Click()
{
    String* Str = "";

    SetObjList( Str , "LearnMagicDB");
}
void    SearchObjForm::BodyObj_Click()
{
    String* Str = "";

    SetObjList( Str , "BodyObjectDB" );
}
void    SearchObjForm::Player_Click()
{
    String* Str = "";

    SetObjList( Str , "PlayerObjectDB" );
}
void    SearchObjForm::KeyItem_Click()
{
    String* Str = "";

    SetObjList( Str , "KeyItemObjectDB" );
}
void    SearchObjForm::Rune_Click()
{
    String* Str = "";

    SetObjList( Str , "RuneObjectDB" );
}
void    SearchObjForm::Title_Click()
{
    String* Str = "";

    SetObjList( Str , "TitleObjectDB" );
}
void    SearchObjForm::Flag_Click()
{
    String* Str = "";

    SetObjList( Str , "FlagObjectDB" );
}
void    SearchObjForm::Recipe_Click()
{
    String* Str = "";

    SetObjList( Str , "RecipeObjectDB" );
}
void    SearchObjForm::Mine_Click()
{
    String* Str = "";

    SetObjList( Str , "MineObjectDB" );
}
void    SearchObjForm::Shop_Click()
{
    String* Str = "";

    SetObjList( Str , "ShopObjectDB" );
}