// This file is part of the aMule Project
//
// Copyright (c) 2003-2004 aMule Project ( http://www.amule-project.net )
// Copyright (C) 2002 Merkur ( merkur-@users.sourceforge.net / http://www.emule-project.net )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


// SearchDlg.cpp : implementation file
//

#include "sys.h"
#include <wx/wx.h>		// Some systems (Mac?) need it
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/tokenzr.h>

#include "SearchDlg.h"		// Interface declarations.
#include "UploadQueue.h"	// Needed for CUploadQueue
#include "CMemFile.h"		// Needed for CMemFile
#include "SearchList.h"		// Needed for CSearchList
#include "DownloadQueue.h"	// Needed for CDownloadQueue
#include "otherfunctions.h"	// Needed for URLEncode
#include "packets.h"		// Needed for Packet
#include "server.h"		// Needed for CServer
#include "ServerList.h"		// Needed for CServerList
#include "sockets.h"		// Needed for CServerConnect
#include "CamuleAppBase.h"	// Needed for theApp
#include "amule.h"          // Needed for websearch
#include "SearchListCtrl.h"	// Needed for CSearchListCtrl
#include "CMuleNotebookBase.h"	// Needed for EVT_MULENOTEBOOK_PAGE_CLOSED
#include "muuli_wdr.h"		// Needed for IDC_STARTS
#include "GetTickCount.h"

#define ID_SEARCHTIMER 55219

// just to keep compiler happy
static wxCommandEvent nullEvent;

IMPLEMENT_DYNAMIC_CLASS(CSearchDlg,wxPanel)

BEGIN_EVENT_TABLE(CSearchDlg, wxPanel)
	EVT_BUTTON(IDC_STARTS, CSearchDlg::OnBnClickedStarts)
	EVT_TEXT_ENTER(IDC_SEARCHNAME,CSearchDlg::OnBnClickedStarts)
	EVT_TEXT_ENTER(IDC_EDITSEARCHMIN,CSearchDlg::OnBnClickedStarts)
	EVT_TEXT_ENTER(IDC_EDITSEARCHMAX,CSearchDlg::OnBnClickedStarts)
	EVT_TEXT_ENTER(IDC_EDITSEARCHEXTENSION,CSearchDlg::OnBnClickedStarts)
	EVT_TEXT_ENTER(IDC_EDITSEARCHAVAIBILITY,CSearchDlg::OnBnClickedStarts)
	EVT_TEXT(IDC_SEARCHNAME,CSearchDlg::OnFieldsChange)
	EVT_TEXT(IDC_EDITSEARCHMIN,CSearchDlg::OnFieldsChange)
	EVT_TEXT(IDC_EDITSEARCHMAX,CSearchDlg::OnFieldsChange)
	EVT_TEXT(IDC_SEARCHWEBNAME, CSearchDlg::OnFieldsChange2)
	EVT_TEXT(IDC_EDITSEARCHEXTENSION,CSearchDlg::OnFieldsChange)
	EVT_TEXT(IDC_EDITSEARCHAVAIBILITY,CSearchDlg::OnFieldsChange)
	EVT_BUTTON(IDC_CANCELS,CSearchDlg::OnBnClickedCancels)
	EVT_BUTTON(IDC_CLEARALL,CSearchDlg::OnBnClickedClearall)
	EVT_BUTTON(ID_WEBSEARCH_SUBMIT, CSearchDlg::OnBtnWebSearch)
	EVT_TIMER(ID_SEARCHTIMER, CSearchDlg::OnTimer)
	EVT_LIST_ITEM_SELECTED(ID_SEARCHLISTCTRL,CSearchDlg::OnListItemSelected)
	EVT_BUTTON(IDC_SDOWNLOAD,CSearchDlg::OnBnClickedSdownload)
	EVT_MULENOTEBOOK_PAGE_CLOSED(ID_NOTEBOOK,CSearchDlg::OnSearchClosed)
	EVT_BUTTON(IDC_SEARCH_RESET,CSearchDlg::OnBnClickedSearchReset)
	EVT_LIST_COL_CLICK(ID_SERVERLIST,CSearchDlg::OnColumnClick)
	EVT_TEXT_ENTER(IDC_SEARCHWEBNAME, CSearchDlg::OnBtnWebSearch)
END_EVENT_TABLE()

//  EVT_MENU(MP_RESUME,CSearchDlg::OnBnClickedSdownload)

void CSearchDlg::OnColumnClick(wxListEvent& evt)
{
	printf("COLCLICK\n");
}

// we must implement Rclick here as wxWindows does not redirect events correctly :(
void CSearchDlg::OnListItemSelected(wxListEvent& event)
{
	// change selection
	//CSearchListCtrl* src=(CSearchListCtrl*)event.GetEventObject();
	FindWindowById(IDC_SDOWNLOAD)->Enable(TRUE);
}

void CSearchDlg::OnSearchClosed(CMuleNotebookEvent& evt)
{
	// yes it is more than confusing.. to make things even a bit easier
	// we'll prevent closing if search is active.

	// Query the search id
	CMuleNotebook* nb=(CMuleNotebook*)FindWindowById(ID_NOTEBOOK);
	wxWindow* page=(wxWindow*)nb->GetPage(evt.GetSelection());
	CSearchListCtrl* sl=(CSearchListCtrl*)page->FindWindowById(ID_SEARCHLISTCTRL,page);
	if(!sl) {
		// hmm? can this happen?
		FindWindowById(IDC_CLEARALL)->Enable(FALSE);
		return; // do not close anything then
	}
	uint16 sID=sl->GetSearchId();
	// and then finally. close the tab
	DeleteSearch(sID);
}

//IMPLEMENT_DYNAMIC(CSearchDlg, CDialog)
CSearchDlg::CSearchDlg(wxWindow* pParent /*=NULL*/)
: wxPanel(pParent,CSearchDlg::IDD), m_timer(this,ID_SEARCHTIMER)
{
	m_nSearchID = 0;
	global_search_timer = 0;
	last_search_time = 0;
	globsearch = false;
  
	searchpacket=NULL;
	searchprogress=NULL; // will be set later...

	SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWFRAME));
	wxSizer* content=searchDlg(this,TRUE);
	content->Show(this,TRUE);

	// initialise the image list
	m_ImageList.Add(wxBitmap(amuleSpecial(3)));
	m_StateImageList.Add(wxBitmap(amuleSpecial(4)));
	// and set it to the notebook too...
	wxStaticCast(FindWindowById(ID_NOTEBOOK),CMuleNotebook)->SetImageList(&m_ImageList);
	wxStaticCast(FindWindowById(ID_NOTEBOOK),CMuleNotebook)->SetStateImageList(&m_StateImageList);

	searchlistctrl=NULL;
}

CSearchDlg::~CSearchDlg()
{
	// Let's remove the lists.
	if (m_ImageList.GetImageCount()>0) {
		m_ImageList.RemoveAll();
	}
	if (m_StateImageList.GetImageCount()>0) {
		m_StateImageList.RemoveAll();
	}
	
}

// CSearchDlg message handlers

typedef void (CSearchListCtrl::*slistFunc)(wxListEvent&);

void CSearchDlg::OnBnClickedStarts(wxEvent& evt)
{
	// lugdunum will kill us if we don't fix this ;)	
 	         if ((GetTickCount() - last_search_time)  > 2000) /* 2 secs */{	
 	 	
 	                 last_search_time = GetTickCount();
	/*if (((wxTextCtrl*)FindWindowById(IDC_SEARCHNAME))->GetLineLength(0) !=0) {
		OnBnClickedCancels(nullEvent);*/
		// start a search
		StartNewSearch();
	}
}

void CSearchDlg::OnFieldsChange(wxEvent& evt)
{
	if (((wxTextCtrl*)FindWindowById(IDC_SEARCHNAME))->GetLineLength(0) !=0) {
		FindWindowById(IDC_SEARCH_RESET)->Enable(TRUE);
		FindWindowById(IDC_STARTS)->Enable(TRUE);
	} else {
		FindWindowById(IDC_SEARCH_RESET)->Enable(FALSE);
		FindWindowById(IDC_STARTS)->Enable(FALSE);
	}
}

void CSearchDlg::OnFieldsChange2(wxEvent& evt)
{
    if (((wxTextCtrl*)FindWindowById(IDC_SEARCHWEBNAME))->GetLineLength(0) !=0) {
		FindWindowById(IDC_SEARCH_RESET)->Enable(TRUE);
		FindWindowById(ID_WEBSEARCH_SUBMIT)->Enable(TRUE);
	} else {
		FindWindowById(ID_WEBSEARCH_SUBMIT)->Enable(FALSE);
	}
}

void CSearchDlg::OnTimer(wxTimerEvent& evt)
{
	if (theApp.serverconnect->IsConnected()) {
		CServer* toask = theApp.serverlist->GetNextSearchServer();
		if (toask == theApp.serverlist->GetServerByAddress(theApp.serverconnect->GetCurrentServer()->GetAddress(),theApp.serverconnect->GetCurrentServer()->GetPort())) {
			toask = theApp.serverlist->GetNextSearchServer();
		}
		if (toask && theApp.serverlist->GetServerCount()-1 != servercount) {
			servercount++;
			theApp.serverconnect->SendUDPPacket(searchpacket,toask,false);
			//searchprogress.StepIt();
			searchprogress->SetValue(searchprogress->GetValue()+1);
		} else {
			OnBnClickedCancels(nullEvent);
			//printf("Cancel-test\n");
			//m_timer.Stop();
		}
	} else {
		OnBnClickedCancels(nullEvent);
		//printf("Cancel-test\n");
		//m_timer.Stop();
	}
}

bool CSearchDlg::CheckTabNameExists(wxString searchString) {

	CMuleNotebook* nb=(CMuleNotebook*)FindWindowById(ID_NOTEBOOK);
	int nPages = nb->GetPageCount();
	
	for (int n=0;n<nPages;n++) {
		if (nb->GetPageText(n)==searchString) {
			return true;
		}
		
	}
	return false;	
}

void CSearchDlg::CreateNewTab(wxString searchString,uint32 nSearchID)
{
	// open new search window
	CMuleNotebook* nb=(CMuleNotebook*)FindWindowById(ID_NOTEBOOK);
	
	wxPanel* sizCont=new wxPanel(nb,-1);
	searchPage(sizCont,TRUE);	// Create searchPage.
	nb->AddPage(sizCont,searchString,TRUE,0,nSearchID);

	wxGauge* gauge=(wxGauge*)sizCont->FindWindowById(ID_SEARCHPROGRESS,sizCont);
	searchprogress=gauge;
	//printf("Searchprogress=%lx\n");
	// set the range here!
	searchprogress->SetRange(theApp.serverlist->GetServerCount()-1);

	GetParent()->Layout();
	searchlistctrl=(CSearchListCtrl*)sizCont->FindWindowById(ID_SEARCHLISTCTRL,sizCont);
	searchlistctrl->Init(theApp.searchlist);
	searchlistctrl->ShowResults(nSearchID);
	// printf("Found ->%i<- results\n",SearchList::GetResultCount(nSearchID));
	FindWindowById(IDC_CLEARALL)->Enable(TRUE);
	// FindWindowById(IDC_SDOWNLOAD)->Enable(TRUE);
}

void CSearchDlg::OnBnClickedCancels(wxEvent &evt)
{
	canceld = true;
	globsearch = false;
	if (global_search_timer) {
		delete searchpacket;
		//KillTimer(global_search_timer);
		m_timer.Stop();
		global_search_timer = 0;
		if(searchprogress) {
			searchprogress->SetValue(0);
		}
	}
	FindWindowByName("CancelSearch")->Enable(false);
	FindWindowByName("StartSearch")->Enable(true);
}

void CSearchDlg::LocalSearchEnd(uint16 count)
{
	if (!canceld) {	
		if (!globsearch) {
			FindWindowByName("CancelSearch")->Enable(false);
			FindWindowByName("StartSearch")->Enable(true);
		} else {
			//global_search_timer = SetTimer(1, 750, 0);
			m_timer.Start(750);
			global_search_timer=(UINT*)1;
		}
	}
}

void CSearchDlg::AddUDPResult(uint16 count)
{
	#if 0
	if (!canceld && count > MAX_RESULTS) {
		OnBnClickedCancels(nullEvent);
	}
	#endif
}

void CSearchDlg::OnBnClickedSdownload(wxCommandEvent &evt)
{
	//start download(s)
	DownloadSelected();
}

wxString CSearchDlg::CreateWebQuery()
{
	wxString query;
	query = "http://www.filedonkey.com/fdsearch/index.php?media=";
	//switch (typebox.GetCurSel()){
	int x=1;
	switch(x) {
		case 1:
			query += "Audio";
			break;
		case 2:
			query += "Video";
			break;
		case 3:
			query += "Pro";
			break;
		default:
			;
	}
	CString tosearch;
	//GetDlgItem(IDC_SWEB)->GetWindowText(tosearch);
	tosearch = CString(URLEncode(tosearch).GetData());
	tosearch.Replace("%20","+");
	query += "&pattern=";
	query += tosearch;
	query += "&action=search&name=FD-Search&op=modload&file=index&requestby=amule";
	return query;
}

void CSearchDlg::DownloadSelected()
{
	int index=-1;
	// download from the selected page.
	CMuleNotebook* nb=(CMuleNotebook*)FindWindowById(ID_NOTEBOOK);
	wxMutexLocker slock(nb->m_LockTabs);
	if(nb->GetSelection()==-1) {
		return;
	}

	CSearchListCtrl* searchlistctrl=(CSearchListCtrl*)nb->FindWindowById(ID_SEARCHLISTCTRL,nb->GetPage(nb->GetSelection()));

	for(;;) {
		index=searchlistctrl->GetNextItem(index,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);
		if(index==-1) {
			break;
		}
		theApp.downloadqueue->AddSearchToDownload((CSearchFile*)searchlistctrl->GetItemData(index));
		//searchlistctrl->SetSelection(index,FALSE);
		FindWindowById(IDC_SDOWNLOAD)->Enable(FALSE);
	}
}

void CSearchDlg::Localize()
{
}

void CSearchDlg::OnBnClickedClearall()
{
	OnBnClickedCancels(nullEvent);
	DeleteAllSearchs();
	OnBnClickedSearchReset(nullEvent);
}

void CSearchDlg::StartNewSearch()
{
	if (!theApp.serverconnect->IsConnected()) {
		// MessageBox(CString(_("You are not connected to a server!")),CString(_("Not Connected")),64);
		wxMessageDialog* dlg=new wxMessageDialog(this,CString(_("You are not connected to a server!")),CString(_("Not Connected")),wxOK|wxCENTRE|wxICON_INFORMATION);
		dlg->ShowModal();
	} else {
	        m_nSearchID++;
	       	wxString typeText;
		wxString searchString;
		searchString=((wxTextCtrl*)FindWindowById(IDC_SEARCHNAME))->GetValue();
		// printf("searchString ->%s<-\n", searchString.c_str());
		typeText=((wxChoice*)FindWindowById(IDC_TypeSearch))->GetStringSelection();
		theApp.searchlist->NewSearch(CString(typeText),m_nSearchID);
		FindWindowByName("StartSearch")->Enable(FALSE);
		FindWindowByName("CancelSearch")->Enable(TRUE);
		canceld = false;
		long typeNemonic = 0x00030001;
		long extensionNemonic = 0x00040001;
		long avaibilityNemonic = 0x15000101;
		long minNemonic = 0x02000101;
		long maxNemonic = 0x02000102;
		byte stringParameter = 1;
		byte typeParameter = 2;
		byte numericParameter = 3;
		int andParameter = 0x0000;
		// int orParameter = 0x0100;
		CMemFile* data = new CMemFile(100); 
		uint16 nSize;
		char* formatC;
		unsigned long avaibility;
		wxString sizeMin,sizeMax,extension,avaibilitystr,type;
		sizeMin=((wxTextCtrl*)FindWindowById(IDC_EDITSEARCHMIN))->GetValue();
		// printf("sizeMin ->%s<-\n", sizeMin.c_str());
		sizeMax=((wxTextCtrl*)FindWindowById(IDC_EDITSEARCHMAX))->GetValue();
		// printf("sizeMax ->%s<-\n", sizeMax.c_str());
		unsigned long max=atol(sizeMax.GetData())*1048576;
		unsigned long min=atol(sizeMin.GetData())*1048576;
		if (max==0 || max<min) {
			max=2147483646; // max file size
		}
		extension=((wxTextCtrl*)FindWindowById(IDC_EDITSEARCHEXTENSION))->GetValue();
		// printf("extension ->%s<-\n", extension.c_str());
		if (extension.Length()>0 && extension.GetChar(0)!='.') {
			extension="."+extension;
		}
		type="";
		if (CString(_("Audio"))==typeText) {
			type.Printf("Audio");
		}
		if (CString(_("Video"))==typeText) {
			type.Printf("Video");
		}
		if (CString(_("Programs"))==typeText) {
			type.Printf("Pro");
		}
		if (CString(_("Pictures"))==typeText) {
			type.Printf("Image");
		}
		avaibilitystr=((wxTextCtrl*)FindWindowById(IDC_EDITSEARCHAVAIBILITY))->GetValue();
		// printf("avaibilitystr ->%s<-\n", avaibilitystr.c_str());
		avaibility=atol(avaibilitystr.GetData());
		if ( searchString.Length()==0 && extension.Length()==0) {
			delete data;
			return;
		}
		int parametercount=0; // must be written parametercount-1 parameter headers
		if ( searchString.Length()>0) {
			parametercount++;
			if (parametercount>1) {
				data->Write(&andParameter,2);
			}
		}
		if (type.Length()>0) {
			parametercount++;
			if (parametercount>1) {
				data->Write(&andParameter,2);
			}
		}
		if (min>0) {
			parametercount++;
			if (parametercount>1) {
				data->Write(&andParameter,2);
			}
		}
		if (max>0) {
			parametercount++;
			if (parametercount>1) {
				data->Write(&andParameter,2);
			}
		}
		if (avaibility>0) {
			parametercount++;
			if (parametercount>1) {
				data->Write(&andParameter,2);
			}
		}
		if (extension.Length()>0) {
			parametercount++;
			if (parametercount>1) {
				data->Write(&andParameter,2);
			}
		}
		// body
		// search a string
		if (searchString.Length()>0) {
			data->Write(&stringParameter,1); // write the parameter type
			nSize=((wxTextCtrl*)FindWindowById(IDC_SEARCHNAME))->GetValue().Length();
			data->Write(&nSize,2);  // write parameter length
			char* searchstr = new char[nSize+1];
			strcpy(searchstr,((wxTextCtrl*)FindWindowById(IDC_SEARCHNAME))->GetValue().GetData());
			data->Write(searchstr,nSize); // write the searched string
			delete[] searchstr;
		}
		if (type.Length()>0) {
			data->Write(&typeParameter,1); // write the parameter type
			nSize=type.Length();
			data->Write(&nSize,2); // write the length
			formatC=(char*)type.GetData();
			data->Write(formatC,nSize); // write parameter
			data->Write(&typeNemonic,3); // nemonic for this kind of parameter (only 3 bytes!!)
		}
		if (min>0) {
			data->Write(&numericParameter,1); // write the parameter type
			data->Write(&min,4); // write the parameter
			data->Write(&minNemonic,4); // nemonic for this kind of parameter
		}
		if (max>0) {
			data->Write(&numericParameter,1); // write the parameter type
			data->Write(&max,4); // write the parameter
			data->Write(&maxNemonic,4); // nemonic for this kind of parameter
		}
		if (avaibility>0) {
			data->Write(&numericParameter,1); // write the parameter type
			data->Write(&avaibility,4); // write the parameter
			data->Write(&avaibilityNemonic,4); // nemonic for this kind of parameter
		}
		if (extension.Length()>0) {
			data->Write(&stringParameter,1); // write the parameter type
			nSize=extension.Length(); 
			data->Write(&nSize,2);	// write the length
			formatC=(char*)extension.GetData();
			data->Write(formatC,nSize); // write parameter
			data->Write(&extensionNemonic,3); // nemonic for this kind of parameter (only 3 bytes!!)
		}
		Packet* packet = new Packet(data);
		packet->opcode = OP_SEARCHREQUEST;
		delete data;
		theApp.uploadqueue->AddUpDataOverheadServer(packet->size);
		theApp.serverconnect->SendPacket(packet,false);
		if(((wxCheckBox*)FindWindowByName("globalSearch"))->IsChecked()) {
			if( theApp.glob_prefs->Score() ) {
				theApp.serverlist->ResetSearchServerPos();
			}
			searchpacket = packet;
			searchpacket->opcode = OP_GLOBSEARCHREQ;
			servercount = 0;
			// in createnewtab():
			// searchprogress.SetRange32(0,theApp.serverlist->GetServerCount()-1);
			globsearch = true;
		} else {
			globsearch = false;
			delete packet;
		}
		CreateNewTab(searchString,m_nSearchID);
	}
}

void CSearchDlg::DeleteSearch(uint16 nSearchID)
{
	theApp.searchlist->RemoveResults(nSearchID);
	/* get the searchlist notebook widget */
	CMuleNotebook* nb=(CMuleNotebook*)FindWindowById(ID_NOTEBOOK);
	if(nb == NULL) {
		// well.. if this actually happens..
		return;
	}
	/* find the searchlist & its parent in the notebooks page list */
	#ifdef DEBUG
	printf("debug: want to delete search with id = %d\n", nSearchID);
	#endif
	wxMutexLocker sLock(nb->m_LockTabs);
	if(nb->GetPageCount() > 0) {
		/* Remove the notebook page */
		for(int i=0; i < nb->GetPageCount(); i++) {
			/* get searchlist->GetId() from the current notebook entry (?) */
			wxWindow * page = (wxWindow *)nb->GetPage(i);
			wxWindow * slctrl = page->FindWindowById(ID_SEARCHLISTCTRL, page);
			if(!slctrl) {
				continue;
			}
			uint16 sID = ((CSearchListCtrl *)slctrl)->GetSearchId();
			#ifdef DEBUG
			printf("debug: searchId=%d | page->slctrl->Id=%d, page->slctrl->searchId=%d | nb->GetPage(%d)->Id=%d\n", nSearchID, slctrl->GetId(), sID, i, page->GetId());
			#endif
			/* is this the searchlist tab we want to remove ? */
			if(sID == nSearchID) {
				#ifdef DEBUG
				printf("debug: removing searchId=%d tab\n", sID);
				#endif
				nb->RemovePage(i);
		  		/* Madcat - If the page deleted was last page, then there might
				 have been a global Search in progress on this page, resulting
				 a crash if more results are added to this nonexistent page.
				 This check should avoid it. */
				if (i++ == nb->GetPageCount()) {
					OnBnClickedCancels(nullEvent);
				}
				/* Force further actions on the searchlistctrl to fail */
				((CSearchListCtrl *)slctrl)->InvalidateSearchId();
				/* ToDo: remove searchlistctrl (slctrl) ? */
				break;
			}
		}
	}
}

void CSearchDlg::DeleteAllSearchs()
{
	theApp.searchlist->Clear();
	/* get the searchlist notebook widget */
	CMuleNotebook* nb=(CMuleNotebook*)FindWindowById(ID_NOTEBOOK);
	if(nb == NULL) {
		printf("error: nb=NULL in deletesearch\n");
		return;
	}
	if(nb->GetPageCount() > 0) {
		/* Remove all notebook pages */
		for(int i=nb->GetPageCount()-1; i>=0; i--) {
			if(nb->GetPage(i) != NULL) {
				nb->RemovePage(i);
			}
			/* ToDo: remove searchlistctrl (slctrl) ? */
		}
	}
	printf("todo. deleteallsearchs\n");
	FindWindowById(IDC_CLEARALL)->Enable(FALSE);
}

void CSearchDlg::OnBnClickedSearchReset(wxEvent& evt)
{
	((wxTextCtrl*)FindWindowById(IDC_SEARCHNAME))->SetValue("");
	((wxTextCtrl*)FindWindowById(IDC_SEARCHWEBNAME))->SetValue("");
	((wxTextCtrl*)FindWindowById(IDC_EDITSEARCHMIN))->SetValue("");
	((wxTextCtrl*)FindWindowById(IDC_EDITSEARCHMAX))->SetValue("");
	((wxTextCtrl*)FindWindowById(IDC_EDITSEARCHEXTENSION))->SetValue("");
	((wxTextCtrl*)FindWindowById(IDC_EDITSEARCHAVAIBILITY))->SetValue("");
	wxChoice* Stypebox=(wxChoice*)FindWindowById(IDC_TypeSearch);
	Stypebox->SetSelection(Stypebox->FindString(CString(_("Any"))));
	FindWindowById(IDC_SEARCH_RESET)->Enable(FALSE);
}

void CSearchDlg::OnBtnWebSearch(wxCommandEvent& WXUNUSED(evt))
{
	wxTextCtrl* txtctrl = (wxTextCtrl*)FindWindow(IDC_SEARCHWEBNAME);
    theApp.LaunchUrl(theApp.GenWebSearchUrl(txtctrl->GetValue()));
}
