///////////////////////////////////////////////////////////
// Author:			Stefan Liersch
// Contact:			stefan.liersch@googlemail.com,
//					stefan.liersch@ufz.de
//
// Date:			2008-03-14
// last modified:	2008-09-18
//
//---------------------------------------------------------
// DESCRIPTION
//---------------------------------------------------------
//
//---------------------------------------------------------
// ToDo
//---------------------------------------------------------
//	- There is a weak point in _Get_InsertString(), the function
//		is taking the selected value of m_pCombo_Field
//		to set fid.
///////////////////////////////////////////////////////////

#include "pg_soil_salinity_assessment_dlg.h"

#include <fstream>  // test output only

//---------------------------------------------------------------------

BEGIN_EVENT_TABLE(CSoilSalinityAssessmentDlg, wxDialog)
	EVT_COMBOBOX(ID_CMD_SSA_COMBO_ID,					_On_Change_ID_Column)
	EVT_COMBOBOX(ID_CMD_SSA_COMBO_FIELD,				_On_Change_Feature)
	EVT_TEXT(ID_CMD_SSA_TXT_YEAR,						_On_Slider_ZeroSalinityValues)
	EVT_COMBOBOX(ID_CMD_SSA_COMBO_MONTH,				_On_Slider_ZeroSalinityValues)
	EVT_BUTTON(ID_CMD_SSA_BUTTON_LOAD_SETTINGS,			_On_Button_Load_Settings)
	EVT_BUTTON(ID_CMD_SSA_BUTTON_WEIGHTS,				_On_Button_Weights)

	EVT_SLIDER(ID_CMD_SSA_SLIDER_SOIL_COLOUR_LOCAL,		_On_Slider_ZeroSalinityValues)
	EVT_COMBOBOX(ID_CMD_SSA_COMBO_nLEACHING,			_On_Slider_ZeroSalinityValues)
	EVT_SLIDER(ID_CMD_SSA_SLIDER_WATERQUALITYLEACHING,	_On_Slider_ZeroSalinityValues)
	EVT_SLIDER(ID_CMD_SSA_SLIDER_WATERQUALITYIRRIGATION,_On_Slider_ZeroSalinityValues)
	EVT_COMBOBOX(ID_CMD_SSA_COMBO_WATERVOLUMEIRRIGATION,_On_Slider_ZeroSalinityValues)
	EVT_SLIDER(ID_CMD_SSA_CMD_SLIDER_EVENNESS,			_On_Slider_ZeroSalinityValues)
	EVT_COMBOBOX(ID_CMD_SSA_COMBO_MORPHOLOGY,			_On_Slider_ZeroSalinityValues)

	EVT_SLIDER(ID_CMD_SSA_SLIDER_SOIL_TEXTURE,			_On_Slider_ZeroSalinityValues)
	EVT_SLIDER(ID_CMD_SSA_SLIDER_PLANTGROWTH,			_On_Slider_ZeroSalinityValues)
	EVT_SLIDER(ID_CMD_SSA_SLIDER_GW_SALINITY,			_On_Slider_ZeroSalinityValues)
	EVT_COMBOBOX(ID_CMD_SSA_COMBO_GW_LEVEL,				_On_Slider_ZeroSalinityValues)
	EVT_COMBOBOX(ID_CMD_SSA_COMBO_N_CHANNELS,			_On_Slider_nChannels)
	EVT_COMBOBOX(ID_CMD_SSA_COMBO_CHANNEL_DISTANCE,		_On_Slider_ZeroSalinityValues)
	EVT_COMBOBOX(ID_CMD_SSA_COMBO_MAINTENANCE,			_On_Slider_ZeroSalinityValues)

	EVT_BUTTON(ID_CMD_SSA_BUTTON_CALC_SALINITY,			_On_Button_CalcSalinity)
	EVT_BUTTON(ID_CMD_SSA_BUTTON_SAVE,					_On_Save)
END_EVENT_TABLE()
//---------------------------------------------------------------------

//---------------------------------------------------------------------
// Constructor
CSoilSalinityAssessmentDlg::CSoilSalinityAssessmentDlg()
{
	m_zero_ok = false;
}

CSoilSalinityAssessmentDlg::CSoilSalinityAssessmentDlg(wxWindow *parent, wxWindowID id, const wxString &title)
	: wxDialog(parent, id, title,
	  wxDefaultPosition, wxDefaultSize,
	  wxDEFAULT_DIALOG_STYLE),// | wxRESIZE_BORDER),
	  CSoilSalinityDlgBase()
{
	m_zero_ok = false;

	_Resize_Vectors();

	_CreateDialog(NULL);
}

CSoilSalinityAssessmentDlg::CSoilSalinityAssessmentDlg(CSoilSalinityMap* pSSMap, wxWindow *parent, wxWindowID id, const wxString &title)
	: wxDialog(parent, id, title,
	  wxPoint(200,50),//wxDefaultPosition,
	  wxDefaultSize,
	  wxDEFAULT_DIALOG_STYLE),// | wxRESIZE_BORDER),
	  CSoilSalinityDlgBase(pSSMap)
{
	m_zero_ok = false;

	_Resize_Vectors();

	_CreateDialog(pSSMap->Get_SimpleFeature());
}

//---------------------------------------------------------------------
// Destructor
CSoilSalinityAssessmentDlg::~CSoilSalinityAssessmentDlg()
{
	// Delete all member dialog controls
	_Delete_Controls();
}
//---------------------------------------------------------------------


///////////////////////////////////////////////////////////////////////
//
//	EVENT HANDLER FUNCTIONS
//
///////////////////////////////////////////////////////////////////////

//---------------------------------------------------------------------

void CSoilSalinityAssessmentDlg::_On_Change_ID_Column(wxCommandEvent &event)
{
	m_ID_Column = m_pCombo_ID->GetCurrentSelection();
	_Set_ChoiceBox(m_pCombo_Field, _Get_FeatureIDs(m_pSimpleFeature, m_ID_Column), _Get_Selection(m_pSimpleFeature) );
	event.Skip();
}
//---------------------------------------------------------------------

void CSoilSalinityAssessmentDlg::_On_Change_Feature(wxCommandEvent& event)
{
	m_pWKSP_SimpleFeature->Get_Shapes()->Get_Table().Select(m_pCombo_Field->GetCurrentSelection(),false);
	m_pWKSP_SimpleFeature->Get_Shapes()->Select(m_pCombo_Field->GetCurrentSelection(),false);
	m_pSimpleFeatureManager->Update_Views(m_pSimpleFeature);

	m_CurrentSelection = m_pCombo_Field->GetCurrentSelection();

	_Zero_Controls();
	m_pButton_Save->Enable(false);

	event.Skip();
}
//---------------------------------------------------------------------

void CSoilSalinityAssessmentDlg::_On_Button_Load_Settings(wxCommandEvent &event)
{
	int			pk = 0; // primary key
	CSG_String	plant = "";

	if ( g_pSAGA_Frame->db->CheckAccessibility(m_pSSMap->Get_SettingsTableName().c_str()) &&
		 g_pSAGA_Frame->db->CheckAccessibility(m_pSSMap->Get_AttributeTableName().c_str()) ) {

		// find primary key
		pk = _Get_PK_for_settings(convert_sl::StringToInt(m_pCombo_Field->GetValue().c_str()),
			 convert_sl::StringToInt(m_pTxtCtrl_Year->GetValue().c_str()),
			 m_pCombo_Month->GetValue().c_str());

		if ( pk >= 0 ) {
			// load corresponding settings
			PGresult* res = g_pSAGA_Frame->db->ExecSQL_wx2(_Get_SQL_Load_Settings(pk).c_str());

			if (PQresultStatus(res) != PGRES_TUPLES_OK)
			{
				PQclear(res);
			} else {
				if ( PQntuples > 0 ) {
					int j = 0;
					for (int i = 0; i < m_pSSMap->Get_NumberSSVariables(); i++) {
						m_checked[i] = convert_sl::StringToBool( PQgetvalue(res,0,j) );
						m_settings[i] = convert_sl::StringToInt( PQgetvalue(res,0,j+1) );
						j+=2;
					}
					plant = PQgetvalue(res,0,m_pSSMap->Get_NumberSSVariables()*2); // assign plant
				}
				PQclear(res);
			}
		} else {
			MSG_ADD("No settings available!");
		}
	} else {
		MSG_ADD("unable to open settings table");
	}	

	// Modify dialog controls according to settings
	_Set_Settings();
	_On_Change_nChannels();
	m_pCombo_Plant->SetValue(plant.c_str());
}
//---------------------------------------------------------------------

void CSoilSalinityAssessmentDlg::_On_Button_Weights(wxCommandEvent &event)
{
	CSoilSalinityParameterWeightsDlg* pWeights = new CSoilSalinityParameterWeightsDlg(m_pSSMap, this, wxID_ANY, "Parameter Weights");
	pWeights->ShowModal();
	delete pWeights;
}

//---------------------------------------------------------------------
int CSoilSalinityAssessmentDlg::_Get_PK_for_settings(int fid, int year, CSG_String month)
{
	int					pk = 0;
	PGresult*			res;
	std::ostringstream	sql;

	sql << "SELECT PK_ID FROM " << m_pSSMap->Get_AttributeTableName();
	sql << " WHERE fid = " << fid;
	sql << " AND yr = " << year;
	sql << " AND mnth = '" << month << "'";

	res = g_pSAGA_Frame->db->ExecSQL_wx2(sql.str().c_str());
	if (PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		PQclear(res);
		return(-999);
	} else {
		if ( PQntuples(res) == 1 ) {
			pk = convert_sl::StringToInt(PQgetvalue(res,0,0));
		} else {
			pk = -999;
		}
		PQclear(res);
		return(pk);
	}
}
//---------------------------------------------------------------------
CSG_String CSoilSalinityAssessmentDlg::_Get_SQL_Load_Settings(int fk_id)
{
	std::ostringstream sql;

	sql << "SELECT b_sc_l, sc_l, b_nleaching, nleaching, b_wq_l, wq_l, b_wq_i, wq_i, ";
	sql << "b_wv_i, wv_i, b_even, even, b_morph, morph, ";
	sql << "b_s_t, s_t, b_pg, pg, b_gw_s, gw_s, b_gw_l, gw_l, ";
	sql << "b_nchannels, nchannels, b_distance, distance, b_maintenance, maintenance, plant FROM ";
	sql << m_pSSMap->Get_SettingsTableName() << ", " << m_pSSMap->Get_AttributeTableName();
	sql << " WHERE " << m_pSSMap->Get_SettingsTableName() << ".fk_id = " << fk_id;
	sql << " AND " << m_pSSMap->Get_AttributeTableName() << ".pk_id = " << fk_id;
	return(sql.str().c_str());
}
//---------------------------------------------------------------------
void CSoilSalinityAssessmentDlg::_Set_Settings()
{
	m_pCheck_SoilColour->SetValue(m_checked[0]);
	m_pSlider_SoilColour_Local->SetValue(m_settings[0]);
	
	m_pCheck_Leaching->SetValue(m_checked[1]);
	m_pCombo_nLeaching->SetSelection(m_settings[1]);
	
	m_pCheck_WQ_Leaching->SetValue(m_checked[2]);
	m_pSlider_WaterQualityLeaching->SetValue(m_settings[2]);

	m_pCheck_WQ_Irrigation->SetValue(m_checked[3]);
	m_pSlider_WaterQualityIrrigation->SetValue(m_settings[3]);

	m_pCheck_WaterVolume_Irrigation->SetValue(m_checked[4]);
	m_pCombo_WaterVolumeIrrigation->SetSelection(m_settings[4]);

	m_pCheck_Evenness->SetValue(m_checked[5]);
	m_pSlider_Evenness->SetValue(m_settings[5]);

	m_pCheck_Morphology->SetValue(m_checked[6]);
	m_pCombo_Morphology->SetSelection(m_settings[6]);

	m_pCheck_SoilTexture->SetValue(m_checked[7]);
	m_pSlider_SoilTexture->SetValue(m_settings[7]);

	m_pCheck_PlantGrowth->SetValue(m_checked[8]);
	m_pSlider_PlantGrowth->SetValue(m_settings[8]);

	m_pCheck_GW_Salinity->SetValue(m_checked[9]);
	m_pSlider_GW_Salinity->SetValue(m_settings[9]);

	m_pCheck_GW_Level->SetValue(m_checked[10]);
	m_pCombo_GW_Level->SetSelection(m_settings[10]);

	m_pCheck_nChannels->SetValue(m_checked[11]);
	m_pCombo_nChannels->SetSelection(m_settings[11]);

	m_pCheck_Distance->SetValue(m_checked[12]);
	m_pCombo_ChannelDistance->SetSelection(m_settings[12]);

	m_pCheck_Maintenance->SetValue(m_checked[13]);
	m_pCombo_Maintenance->SetSelection(m_settings[13]);
}
//---------------------------------------------------------------------
void CSoilSalinityAssessmentDlg::_On_Slider_ZeroSalinityValues(wxCommandEvent& event)
{
	if ( m_zero_ok ) {
		_Zero_Controls();
		m_pButton_Save->Enable(false);
	}
}
//---------------------------------------------------------------------
void CSoilSalinityAssessmentDlg::_On_Slider_nChannels(wxCommandEvent& event)
{
	_On_Change_nChannels();
	_On_Slider_ZeroSalinityValues(event);
}
//---------------------------------------------------------------------

void CSoilSalinityAssessmentDlg::_On_Change_nChannels()
{
	if ( m_pCombo_nChannels->GetSelection() == 0 ) {
		m_pCombo_ChannelDistance->Enable(true);
		m_pCheck_Distance->Enable(true);
		m_pCheck_Distance->SetValue(true);
		m_pCheck_nChannels->SetValue(false);
		m_pCheck_nChannels->Enable(false);
	} else {
		m_pCombo_ChannelDistance->Enable(false);
		m_pCheck_Distance->Enable(false);
		m_pCheck_Distance->SetValue(false);
		m_pCheck_nChannels->SetValue(true);
		m_pCheck_nChannels->Enable(true);
	}
}
//---------------------------------------------------------------------

void CSoilSalinityAssessmentDlg::_On_Save(wxCommandEvent &event)
{
	int pk_colnumber = 0;

	if (_Record_Exists(pk_colnumber)) {
		if (wxYES == wxMessageBox(_Get_Message_Exist().c_str(),"Record exists!",
			wxNO_DEFAULT|wxYES_NO|wxICON_INFORMATION,this) ) {
				
				// UPDATE record (attribute table)
				if (g_pSAGA_Frame->db->ExecSQL_wx(_Get_SQL_Update_Record(pk_colnumber).c_str()) ) {
					MSG_ADD("Salinity values have been successfully updated");

					// UPDATE record (settings table)
					if ( g_pSAGA_Frame->db->ExecSQL_wx(_Get_SQL_Update_Settings(pk_colnumber).c_str()) ) {
						MSG_ADD("Settings values have been successfully updated");
					} else {
						MSG_ADD("UPDATE of settings was NOT successful!");
					}

				} else {
					MSG_ADD("UPDATE of attributes was NOT successful!");
					MSG_ADD(_Get_SQL_Update_Record(pk_colnumber).c_str());
				}
		}
	} else {
		// write salinity values to attribute table
		if ( g_pSAGA_Frame->db->ExecSQL_wx(_Get_InsertString().c_str()) ) {
			MSG_ADD("Salinity values have been successfully saved to the database");

			// store the automatically assigned primary key value (serial) in
			// variable m_last_serial
			m_last_serial = g_pSAGA_Frame->db->_Get_last_serial(m_pSSMap->Get_AttributeTableName(),
				g_pSAGA_Frame->db->Get_PrimaryKeyColumnName(m_pSSMap->Get_AttributeTableName()).c_str());

			// write parameter settings to the settings table
			if ( g_pSAGA_Frame->db->ExecSQL_wx(_Get_InsertString_Settings().c_str()) ) {
				MSG_ADD("Settings values have been successfully saved to the database");
			} else {
				MSG_ADD("Saving of settings table was NOT successful!");
			}

		} else {
			MSG_ADD("Saving of attribute table was NOT successful!");
		}
	}

	m_pButton_Save->Enable(false);

	_Refresh_Plants();

	event.Skip();
}
//---------------------------------------------------------------------


//---------------------------------------------------------------------
// CALCULATE SALINITY
//---------------------------------------------------------------------
void CSoilSalinityAssessmentDlg::_On_Button_CalcSalinity(wxCommandEvent &event)
{
	_Resize_Vectors();

	_Assign_Parms();

	_Assign_Settings();

	_Assign_ParmsChecked();

	CSalinity* pSalinity = new CSalinity(m_parameters, m_pSSMap->Get_Weights(), m_checked);
	
	m_pTxtCtrl_SalinityClass->SetValue(pSalinity->Get_Salinity_3str().c_str());


	wxColour green1(127,255,0);
	wxColour green2(0,205,0);
	wxColour yellow(255,255,0);
	wxColour orange(255,127,0);
	wxColour red(255,0,0);
	wxColour gold(255,215,0);
	double salinity = pSalinity->Get_Salinity();
	if ( salinity <= 0.15 ) m_pTxtCtrl_SalinityClass->SetBackgroundColour(green1);
	if ( salinity > 0.15 && salinity <= 0.3 ) m_pTxtCtrl_SalinityClass->SetBackgroundColour(green2);
	if ( salinity > 0.3 && salinity <= 0.5 ) m_pTxtCtrl_SalinityClass->SetBackgroundColour(yellow);
	if ( salinity > 0.5 && salinity <= 0.75 ) m_pTxtCtrl_SalinityClass->SetBackgroundColour(orange);
	if ( salinity > 0.75 ) m_pTxtCtrl_SalinityClass->SetBackgroundColour(red);

	m_pSliderShowSalinity->SetValue(salinity * 100);

	//if ( pSalinity->Get_Salinity_3str().compare("medium") == 0 ) m_pTxtCtrl_SalinityClass->SetBackgroundColour(yellow);
	//if ( pSalinity->Get_Salinity_3str().compare("high") == 0 ) m_pTxtCtrl_SalinityClass->SetBackgroundColour(*wxRED);

	m_pTxtCtrl_SalinityValue->SetValue(convert_sl::Double2String(pSalinity->Get_Salinity()).c_str());

	m_pTxtCtrl_SalinityValueLocal->SetValue(convert_sl::Double2String(pSalinity->Get_Salinity_Local()).c_str());

	m_pTxtCtrl_SalinityValueExpert->SetValue(convert_sl::Double2String(pSalinity->Get_Salinity_Expert()).c_str());

	if (pSalinity->Get_MembershipDegree(0) < 0.0) 
		m_pTxtCtrl_MSLow->SetValue("0");
	else
		m_pTxtCtrl_MSLow->SetValue(convert_sl::Double2String(pSalinity->Get_MembershipDegree(0)).c_str());

	if (pSalinity->Get_MembershipDegree(1) < 0.0)
		m_pTxtCtrl_MSMedium->SetValue("0");
	else
		m_pTxtCtrl_MSMedium->SetValue(convert_sl::Double2String(pSalinity->Get_MembershipDegree(1)).c_str());

	if (pSalinity->Get_MembershipDegree(2) < 0.0)
		m_pTxtCtrl_MSHigh->SetValue("0");
	else
		m_pTxtCtrl_MSHigh->SetValue(convert_sl::Double2String(pSalinity->Get_MembershipDegree(2)).c_str());

	m_pButton_Save->Enable(true);

	delete pSalinity;

	event.Skip();
}
//---------------------------------------------------------------------

void CSoilSalinityAssessmentDlg::_Resize_Vectors()
{
	_Resize(m_pSSMap->Get_NumberSSVariables());
}

void CSoilSalinityAssessmentDlg::_Resize(int size)
{
	m_parameters.resize(size);
	//m_weights.resize(size);
	m_settings.resize(size);
	m_checked.resize(size);
}
//---------------------------------------------------------------------
void CSoilSalinityAssessmentDlg::_Assign_Settings()
{
	// local knowledge
	m_settings[0] = int(m_pSlider_SoilColour_Local->GetValue());
	m_settings[1] = m_pCombo_nLeaching->GetCurrentSelection();
	m_settings[2] = int(m_pSlider_WaterQualityLeaching->GetValue());
	m_settings[3] = int(m_pSlider_WaterQualityIrrigation->GetValue());
	m_settings[4]	= m_pCombo_WaterVolumeIrrigation->GetCurrentSelection();
	m_settings[5] = int(m_pSlider_Evenness->GetValue());
	m_settings[6] = m_pCombo_Morphology->GetCurrentSelection();
	// expert knowledge
	m_settings[7] = int(m_pSlider_SoilTexture->GetValue());
	m_settings[8] = int(m_pSlider_PlantGrowth->GetValue());
	m_settings[9] = int(m_pSlider_GW_Salinity->GetValue());
	m_settings[10] = m_pCombo_GW_Level->GetCurrentSelection();
	m_settings[11]  = m_pCombo_nChannels->GetCurrentSelection();
	m_settings[12] = m_pCombo_ChannelDistance->GetCurrentSelection();
	m_settings[13] = m_pCombo_Maintenance->GetCurrentSelection();
}
//---------------------------------------------------------------------
void CSoilSalinityAssessmentDlg::_Assign_Parms()
{
	// local knowledge
	m_parameters[0] = double(m_pSlider_SoilColour_Local->GetValue()) / double(m_pSlider_SoilColour_Local->GetMax());
	m_parameters[1] = _Assign_LeachingValue();
	//m_parameters[2] = double(m_pSlider_WaterQualityLeaching->GetMax() - m_pSlider_WaterQualityLeaching->GetValue()) / double(m_pSlider_WaterQualityLeaching->GetMax());
	m_parameters[2] = double(m_pSlider_WaterQualityLeaching->GetValue()) / double(m_pSlider_WaterQualityLeaching->GetMax());
	m_parameters[3] = double(m_pSlider_WaterQualityIrrigation->GetValue()) / double(m_pSlider_WaterQualityIrrigation->GetMax());
	m_parameters[4]	= _Assign_WaterVolumeIrrigation();
	m_parameters[5] = double(m_pSlider_Evenness->GetValue()) / double(m_pSlider_Evenness->GetMax());
	m_parameters[6] = _Assign_Morphology();
	// expert knowledge
	m_parameters[7] = double(m_pSlider_SoilTexture->GetMax() - m_pSlider_SoilTexture->GetValue()) / double(m_pSlider_SoilTexture->GetMax());
	m_parameters[8] = double(m_pSlider_PlantGrowth->GetValue()) / double(m_pSlider_PlantGrowth->GetMax());
	m_parameters[9] = double(m_pSlider_GW_Salinity->GetValue()) / double(m_pSlider_GW_Salinity->GetMax());
	m_parameters[10] = _Assign_GW_Level();
	m_parameters[11] = _Assign_nChannels();
	m_parameters[12] = _Assign_3Selections( m_pCombo_ChannelDistance->GetCurrentSelection() );
	m_parameters[13] = _Assign_3Selections( m_pCombo_Maintenance->GetCurrentSelection() );
}
//---------------------------------------------------------------------
void CSoilSalinityAssessmentDlg::_Assign_ParmsChecked()
{
	// local knowledge
	m_checked[0]	= m_pCheck_SoilColour->IsChecked();
	m_checked[1]	= m_pCheck_Leaching->IsChecked();
	m_checked[2]	= m_pCheck_WQ_Leaching->IsChecked();
	m_checked[3]	= m_pCheck_WQ_Irrigation->IsChecked();
	m_checked[4]	= m_pCheck_WaterVolume_Irrigation->IsChecked();
	m_checked[5]	= m_pCheck_Evenness->IsChecked();
	m_checked[6]	= m_pCheck_Morphology->IsChecked();
	// expert knowledge
	m_checked[7]	= m_pCheck_SoilTexture->IsChecked();
	m_checked[8]	= m_pCheck_PlantGrowth->IsChecked();
	m_checked[9]	= m_pCheck_GW_Salinity->IsChecked();
	m_checked[10]	= m_pCheck_GW_Level->IsChecked();
	m_checked[11]	= m_pCheck_nChannels->IsChecked();
	m_checked[12]	= m_pCheck_Distance->IsChecked();
	m_checked[13]	= m_pCheck_Maintenance->IsChecked();
}

//---------------------------------------------------------------------
double CSoilSalinityAssessmentDlg::_Assign_LeachingValue()
{
	switch( m_pCombo_nLeaching->GetCurrentSelection() )
	{
		case 0: return(0.0); break;		// nLeaching = 0
		case 1: return(0.17); break;
		case 2: return(0.33); break;
		case 3: return(0.5); break;
		case 4: return(0.5); break;
		case 5: return(0.83); break;
		case 6: return(1.0); break;		// nLeaching  >= 6
		default: return(0.5); break;
	}
}
//---------------------------------------------------------------------
double CSoilSalinityAssessmentDlg::_Assign_WaterVolumeIrrigation()
{
	switch( m_pCombo_WaterVolumeIrrigation->GetCurrentSelection() )
	{
		case 0: return(0.0); break;	// 0-100 mm
		case 1: return(0.25); break;
		case 2: return(0.5); break;
		case 3: return(0.75); break;
		case 4: return(1.0); break;	// > 400 mm

		default: return(0.5); break;
	}
}
//---------------------------------------------------------------------
double CSoilSalinityAssessmentDlg::_Assign_Morphology()
{
	switch( m_pCombo_Morphology->GetCurrentSelection() )
	{
		case 0: return(1.0); break;	// sink
		case 1: return(0.0); break;	// hill
		case 2: return(0.5); break;	// in between
		
		default: return(0.5); break;
	}
}
//---------------------------------------------------------------------
double CSoilSalinityAssessmentDlg::_Assign_GW_Level()
{
	switch( m_pCombo_GW_Level->GetCurrentSelection() )
	{
		case 0: return(0.0); break;
		case 1: return(0.33); break;
		case 2: return(0.66); break;
		case 3: return(1.0); break;
		
		default: return(0.5); break;
	}
}
//---------------------------------------------------------------------
double CSoilSalinityAssessmentDlg::_Assign_nChannels()
{
	switch( m_pCombo_nChannels->GetCurrentSelection() )
	{
		case 0: return(1.0); break;
		case 1: return(0.75); break;
		case 2: return(0.5); break;
		case 3: return(0.25); break;
		case 4: return(0.0); break;
		
		default: return(0.5); break;
	}
}
//---------------------------------------------------------------------
double CSoilSalinityAssessmentDlg::_Assign_3Selections(int selection)
{
	switch( selection )
	{
		case 0: return(0.0); break;	// best value
		case 1: return(0.5); break;	// medium
		case 2: return(1.0); break;	// bad value
		
		default: return(0.5); break;
	}
}
//---------------------------------------------------------------------




///////////////////////////////////////////////////////////////////////
//
//	DIALOG
//
///////////////////////////////////////////////////////////////////////
void CSoilSalinityAssessmentDlg::_CreateDialog(CSG_SimpleFeature* pSimpleFeature)
{
	wxBoxSizer* bSizerMainBoxH;
	bSizerMainBoxH = new wxBoxSizer( wxHORIZONTAL );
	
	wxBoxSizer* bSizerMainSettingsLocalKnowledge = new wxBoxSizer( wxVERTICAL );
	wxBoxSizer* bSizerCalcExpertKnowledge = new wxBoxSizer( wxVERTICAL);


	wxStaticBox *sbs1 = new wxStaticBox( this, -1, wxT("Main Settings") );
	sbs1->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
	wxStaticBoxSizer* sbSizerMainSettings = new wxStaticBoxSizer (sbs1, wxVERTICAL);
	
	wxBoxSizer* bSizer4;
	bSizer4 = new wxBoxSizer( wxHORIZONTAL );
	
	// ID --------------------------------------
	wxStaticText* txt_id;
	txt_id = new wxStaticText( this, wxID_ANY, wxT("Select ID Column"), wxDefaultPosition, wxSize( 50,-1 ), 0 );
	bSizer4->Add( txt_id, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
	
	m_pCombo_ID = new wxComboBox( this, ID_CMD_SSA_COMBO_ID, wxEmptyString, wxDefaultPosition, wxSize( 100,-1 ), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY ); 
	m_pCombo_ID->SetToolTip( wxT("Select the Column to identify the fields") );
	wxArrayString as = _Get_ColumnNames(pSimpleFeature);
	for (unsigned int i = 0; i < as.size(); i++) {
		m_pCombo_ID->Append(as[i]);
	}
	m_pCombo_ID->SetSelection(0);

	bSizer4->Add( m_pCombo_ID, 0, wxALIGN_CENTER|wxALL, 5 );
	bSizer4->Add( 25, 0, 1, wxEXPAND, 5 );
	
	// FIELD --------------------------------------
	wxStaticText* txt_field;
	txt_field = new wxStaticText( this, wxID_ANY, wxT("Selected Field"), wxDefaultPosition, wxSize( 80,-1 ), 0 );
	bSizer4->Add( txt_field, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pCombo_Field = new wxComboBox( this, ID_CMD_SSA_COMBO_FIELD, wxEmptyString, wxDefaultPosition, wxSize( 100,-1 ), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY ); 
	wxArrayString asCF = _Get_FeatureIDs(pSimpleFeature, 0);
	for (unsigned int i = 0; i < asCF.size(); i++) {
		m_pCombo_Field->Append(asCF[i]);
	}
	m_pCombo_Field->SetSelection(m_CurrentSelection);

	bSizer4->Add( m_pCombo_Field, 0, wxALIGN_CENTER|wxALL, 5 );
	bSizer4->Add( 0, 0, 1, wxEXPAND, 5 );
	
	sbSizerMainSettings->Add( bSizer4, 0, wxLEFT|wxRIGHT, 5 );
	
	wxBoxSizer* bSizer211;
	bSizer211 = new wxBoxSizer( wxHORIZONTAL );
	
	// YEAR --------------------------------------
	wxStaticText* txt_year;
	txt_year = new wxStaticText( this, wxID_ANY, wxT("Year"), wxDefaultPosition, wxSize( 50,-1 ), 0 );
	bSizer211->Add( txt_year, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pTxtCtrl_Year = new wxTextCtrl( this, ID_CMD_SSA_TXT_YEAR, wxT("2008"), wxDefaultPosition, wxSize( 40,-1 ), 0 );
	m_pTxtCtrl_Year->SetToolTip( wxT("Type in the year (YYYY)") );
	
	bSizer211->Add( m_pTxtCtrl_Year, 0, wxALIGN_CENTER|wxALL, 5 );
	bSizer211->Add( 20, 0, 1, wxEXPAND, 5 );
	
	// MONTH --------------------------------------
	wxStaticText* txt_month;
	txt_month = new wxStaticText( this, wxID_ANY, wxT("Month"), wxDefaultPosition, wxSize( 40,-1 ), 0 );
	bSizer211->Add( txt_month, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pCombo_Month = new wxComboBox( this, ID_CMD_SSA_COMBO_MONTH, wxEmptyString, wxDefaultPosition, wxSize( 45,-1 ), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	wxArrayString amon = _Get_Month_Array();
	for (unsigned int i = 0; i < amon.size(); i++) {
		m_pCombo_Month->Append(amon[i]);
	}
	m_pCombo_Month->SetSelection(10);
	bSizer211->Add( m_pCombo_Month, 0, wxALIGN_CENTER|wxALL, 5 );
	bSizer211->Add( 15, 0, 0, 0, 0 );
	
	// PLANT --------------------------------------
	wxStaticText* m_staticText33;
	m_staticText33 = new wxStaticText( this, wxID_ANY, wxT("Plant"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizer211->Add( m_staticText33, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pCombo_Plant = new wxComboBox( this, ID_CMD_SSA_COMBO_PLANT, wxT("Cotton"), wxDefaultPosition, wxSize( 100,-1 ), 0, NULL, 0 );
	wxArrayString aP = _Get_Plants();
	for (unsigned int i = 0; i < aP.size(); i++) {
		m_pCombo_Plant->Append(aP[i]);
	}
	m_pCombo_Plant->SetSelection(0);
	bSizer211->Add( m_pCombo_Plant, 0, wxALIGN_CENTER|wxALL, 5 );
	
	sbSizerMainSettings->Add( bSizer211, 1, wxALL, 5 );

	wxBoxSizer* bSizerSpace = new wxBoxSizer(wxHORIZONTAL);
	bSizerSpace->Add(0,0,0,wxALL,5);

	sbSizerMainSettings->Add(bSizerSpace, 0, wxALL, 5);

	// Button Load Settings
	wxBoxSizer* bSizer22;
	bSizer22 = new wxBoxSizer( wxHORIZONTAL );
	m_pButton_LoadSettings = new wxButton( this, ID_CMD_SSA_BUTTON_LOAD_SETTINGS, wxT("Load Settings"), wxDefaultPosition, wxSize( 100,-1 ), 0 );
	bSizer22->Add( m_pButton_LoadSettings, 0, wxALIGN_CENTER|wxALL, 5 );
	
	// Button Weights
	m_pButton_Weights  = new wxButton( this, ID_CMD_SSA_BUTTON_WEIGHTS, wxT("Weights"), wxDefaultPosition, wxSize( 100,-1 ), 0 );
	bSizer22->Add( m_pButton_Weights, 0, wxALIGN_CENTER|wxALL, 5 );
	
	sbSizerMainSettings->Add( bSizer22, 1, wxALIGN_CENTER|wxALL, 5 );
	
	////////////////////////////////////////////////////
	bSizerMainSettingsLocalKnowledge->Add( sbSizerMainSettings, 0, wxALIGN_CENTER|wxALL, 5 );
	//bSizerMainBoxH->Add( sbSizerMainSettings, 0, wxALIGN_CENTER|wxALL, 5 );
	////////////////////////////////////////////////////


	////////////////////////////////////////////////////
	// LOCAL KNOWLEDGE
	////////////////////////////////////////////////////
	wxStaticBox *sbs2 = new wxStaticBox( this, wxID_ANY, wxT("Soil Salinity Variables (local knowledge)") );
	sbs2->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
	wxStaticBoxSizer* sbSizerLocalKnowledge = new wxStaticBoxSizer( sbs2,wxVERTICAL );

	// Soil colour (local) --------------------------------------
	wxBoxSizer* bSizer9;
	bSizer9 = new wxBoxSizer( wxHORIZONTAL );
	
	m_pCheck_SoilColour = new wxCheckBox( this, ID_CMD_SSA_CHECK_SOIL_COLOUR_LOCAL, wxT("Soil Colour"), wxDefaultPosition, wxSize( 150,-1 ), 0 );
	m_pCheck_SoilColour->SetValue(true);
	bSizer9->Add( m_pCheck_SoilColour, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_scbrown_local;
	txt_scbrown_local = new wxStaticText( this, wxID_ANY, wxT("brown"), wxDefaultPosition, wxSize( 50,-1 ), 0 );
	bSizer9->Add( txt_scbrown_local, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pSlider_SoilColour_Local = new wxSlider( this, ID_CMD_SSA_SLIDER_SOIL_COLOUR_LOCAL, 0, 0, 100, wxDefaultPosition, wxSize( 100,-1 ), wxSL_HORIZONTAL );
	bSizer9->Add( m_pSlider_SoilColour_Local, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_scred_local;
	txt_scred_local = new wxStaticText( this, wxID_ANY, wxT("red"), wxDefaultPosition, wxSize( 80,-1 ), 0 );
	bSizer9->Add( txt_scred_local, 0, wxALIGN_CENTER|wxALL, 5 );
	
	sbSizerLocalKnowledge->Add( bSizer9, 0, wxLEFT|wxRIGHT, 5 );
	
	// number of leaching --------------------------------------
	wxBoxSizer* bSizer10;
	bSizer10 = new wxBoxSizer( wxHORIZONTAL );
	
	m_pCheck_Leaching = new wxCheckBox( this, ID_CMD_SSA_CHECK_LEACHING, wxT("Number of leaching"), wxDefaultPosition, wxSize( 150,-1 ), 0 );
	m_pCheck_Leaching->SetValue(true);
	bSizer10->Add( m_pCheck_Leaching, 0, wxALIGN_CENTER|wxALL, 5 );

	bSizer10->Add( 60, 0, 1, wxEXPAND, 5 );
	
	m_pCombo_nLeaching = new wxComboBox( this, ID_CMD_SSA_COMBO_nLEACHING, wxEmptyString, wxDefaultPosition, wxSize( 100,-1 ), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	m_pCombo_nLeaching->Append( wxT("0") );
	m_pCombo_nLeaching->Append( wxT("1") );
	m_pCombo_nLeaching->Append( wxT("2") );
	m_pCombo_nLeaching->Append( wxT("3") );
	m_pCombo_nLeaching->Append( wxT("4") );
	m_pCombo_nLeaching->Append( wxT("5") );
	m_pCombo_nLeaching->Append( wxT(">=6") );
	bSizer10->Add( m_pCombo_nLeaching, 0, wxALIGN_CENTER|wxALL, 5 );
	m_pCombo_nLeaching->SetSelection(3);
	
	sbSizerLocalKnowledge->Add( bSizer10, 0, wxLEFT|wxRIGHT, 5 );
	
	// water quality (leaching) --------------------------------------
	wxBoxSizer* bSizer11;
	bSizer11 = new wxBoxSizer( wxHORIZONTAL );
	
	m_pCheck_WQ_Leaching = new wxCheckBox( this, ID_CMD_SSA_CHECK_WQ_LEACHING, wxT("Water quality (leaching)"), wxDefaultPosition, wxSize( 150,-1 ), 0 );
	m_pCheck_WQ_Leaching->SetValue(true);
	bSizer11->Add( m_pCheck_WQ_Leaching, 0, wxALIGN_CENTER|wxALL, 5 );

	wxStaticText* txt_wqleaching_good;
	txt_wqleaching_good = new wxStaticText( this, wxID_ANY, wxT("good"), wxDefaultPosition, wxSize( 50,-1 ), 0 );
	bSizer11->Add( txt_wqleaching_good, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pSlider_WaterQualityLeaching = new wxSlider( this, ID_CMD_SSA_SLIDER_WATERQUALITYLEACHING, 0, 0, 100, wxDefaultPosition, wxSize( 100,-1 ), wxSL_HORIZONTAL );
	bSizer11->Add( m_pSlider_WaterQualityLeaching, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_wqleaching_bad;
	txt_wqleaching_bad = new wxStaticText( this, wxID_ANY, wxT("bad"), wxDefaultPosition, wxSize( 80,-1 ), 0 );
	bSizer11->Add( txt_wqleaching_bad, 0, wxALIGN_CENTER|wxALL, 5 );
	
	sbSizerLocalKnowledge->Add( bSizer11, 0, wxLEFT|wxRIGHT, 5 );
	
	// water quality (irrigation) --------------------------------------
	wxBoxSizer* bSizer12;
	bSizer12 = new wxBoxSizer( wxHORIZONTAL );

	m_pCheck_WQ_Irrigation = new wxCheckBox( this, ID_CMD_SSA_CHECK_WQ_IRRIGATION, wxT("Water quality (irrigation)"), wxDefaultPosition, wxSize( 150,-1 ), 0 );
	m_pCheck_WQ_Irrigation->SetValue(true);
	bSizer12->Add( m_pCheck_WQ_Irrigation, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_wqirrigation_good;
	txt_wqirrigation_good = new wxStaticText( this, wxID_ANY, wxT("good"), wxDefaultPosition, wxSize( 50,-1 ), 0 );
	bSizer12->Add( txt_wqirrigation_good, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pSlider_WaterQualityIrrigation = new wxSlider( this, ID_CMD_SSA_SLIDER_WATERQUALITYIRRIGATION, 0, 0, 100, wxDefaultPosition, wxSize( 100,-1 ), wxSL_HORIZONTAL );
	bSizer12->Add( m_pSlider_WaterQualityIrrigation, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_wqirrigation_bad;
	txt_wqirrigation_bad = new wxStaticText( this, wxID_ANY, wxT("bad"), wxDefaultPosition, wxSize( 80,-1 ), 0 );
	bSizer12->Add( txt_wqirrigation_bad, 0, wxALIGN_CENTER|wxALL, 5 );
	
	sbSizerLocalKnowledge->Add( bSizer12, 0, wxLEFT|wxRIGHT, 5 );
	
	// water consumption (irrigation) --------------------------------------
	wxBoxSizer* bSizer13;
	bSizer13 = new wxBoxSizer( wxHORIZONTAL );
	
	m_pCheck_WaterVolume_Irrigation = new wxCheckBox( this, ID_CMD_SSA_CHECK_WATERVOLUME_IRRIGATION, wxT("Water used for irrigation"), wxDefaultPosition, wxSize( 150,-1 ), 0 );
	m_pCheck_WaterVolume_Irrigation->SetValue(true);
	bSizer13->Add( m_pCheck_WaterVolume_Irrigation, 0, wxALIGN_CENTER|wxALL, 5 );

	bSizer13->Add( 60, 0, 1, wxEXPAND, 5 );
	
	m_pCombo_WaterVolumeIrrigation = new wxComboBox( this, ID_CMD_SSA_COMBO_WATERVOLUMEIRRIGATION, wxEmptyString, wxDefaultPosition, wxSize( 100,-1 ), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	m_pCombo_WaterVolumeIrrigation->Append( wxT("0-100mm") );
	m_pCombo_WaterVolumeIrrigation->Append( wxT("100-200mm") );
	m_pCombo_WaterVolumeIrrigation->Append( wxT("200-300mm") );
	m_pCombo_WaterVolumeIrrigation->Append( wxT("300-400mm") );
	m_pCombo_WaterVolumeIrrigation->Append( wxT("> 400 mm") );
	bSizer13->Add( m_pCombo_WaterVolumeIrrigation, 0, wxALIGN_CENTER|wxALL, 5 );
	m_pCombo_WaterVolumeIrrigation->SetSelection(0);
	sbSizerLocalKnowledge->Add( bSizer13, 0, wxLEFT|wxRIGHT, 5 );
	
	wxStaticBoxSizer* sbSizer71;
	sbSizer71 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Surface") ), wxVERTICAL );
	
	// evenness --------------------------------------
	wxBoxSizer* bSizer14;
	bSizer14 = new wxBoxSizer( wxHORIZONTAL );
	
	m_pCheck_Evenness = new wxCheckBox( this, ID_CMD_SSA_CHECK_EVENNESS, wxT("Evenness"), wxDefaultPosition, wxSize( 145,-1 ), 0 );
	m_pCheck_Evenness->SetValue(true);
	bSizer14->Add( m_pCheck_Evenness, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_even_even;
	txt_even_even = new wxStaticText( this, wxID_ANY, wxT("even"), wxDefaultPosition, wxSize( 50,-1 ), 0 );
	bSizer14->Add( txt_even_even, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pSlider_Evenness = new wxSlider( this, ID_CMD_SSA_CMD_SLIDER_EVENNESS, 0, 0, 100, wxDefaultPosition, wxSize( 100,-1 ), wxSL_HORIZONTAL );
	bSizer14->Add( m_pSlider_Evenness, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_even_noteven;
	txt_even_noteven = new wxStaticText( this, wxID_ANY, wxT("not even"), wxDefaultPosition, wxSize( 60,-1 ), 0 );
	bSizer14->Add( txt_even_noteven, 0, wxALIGN_CENTER|wxALL, 5 );
	
	sbSizer71->Add( bSizer14, 0, wxLEFT|wxRIGHT, 0 );
	
	// morphology --------------------------------------
	wxBoxSizer* bSizer15;
	bSizer15 = new wxBoxSizer( wxHORIZONTAL );

	m_pCheck_Morphology = new wxCheckBox( this, ID_CMD_SSA_CHECK_MOPRPHOLOGY, wxT("Morphology"), wxDefaultPosition, wxSize( 145,-1 ), 0 );
	m_pCheck_Morphology->SetValue(true);
	bSizer15->Add( m_pCheck_Morphology, 0, wxALIGN_CENTER|wxALL, 5 );
	
	bSizer15->Add( 60, 0, 1, wxEXPAND, 5 );
	
	m_pCombo_Morphology = new wxComboBox( this, ID_CMD_SSA_COMBO_MORPHOLOGY, wxEmptyString, wxDefaultPosition, wxSize( 100,-1 ), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	m_pCombo_Morphology->Append( wxT("sink") );
	m_pCombo_Morphology->Append( wxT("hill") );
	m_pCombo_Morphology->Append( wxT("in between") );
	bSizer15->Add( m_pCombo_Morphology, 0, wxALIGN_CENTER|wxALL, 5 );
	m_pCombo_Morphology->SetSelection(0);
	sbSizer71->Add( bSizer15, 0, wxLEFT|wxRIGHT, 0 );
	
	sbSizerLocalKnowledge->Add( sbSizer71, 1, wxBOTTOM|wxLEFT|wxTOP, 5 );
	
	////////////////////////////////////////////////////
	bSizerMainSettingsLocalKnowledge->Add( sbSizerLocalKnowledge, 0, wxALL, 5 );
	//bSizerMainBoxH->Add( sbSizerLocalKnowledge, 0, wxALL, 5 );
	////////////////////////////////////////////////////

	bSizerMainBoxH->Add( bSizerMainSettingsLocalKnowledge, 0, wxALL, 5 );
	
	////////////////////////////////////////////////////
	//EXPERT KNOWLEDGE
	////////////////////////////////////////////////////
	wxStaticBox* sbs3 = new wxStaticBox( this, wxID_ANY, wxT("Soil Salinity Variables (expert knowledge)") );
	sbs3->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
	wxStaticBoxSizer* sbSizerExpertKnowledge = new wxStaticBoxSizer( sbs3, wxVERTICAL );
	
	// soil texture --------------------------------------
	wxBoxSizer* bSizer16;
	bSizer16 = new wxBoxSizer( wxHORIZONTAL );

	m_pCheck_SoilTexture = new wxCheckBox( this, ID_CMD_SSA_CHECK_SOIL_TEXTURE, wxT("Soil texture"), wxDefaultPosition, wxSize( 155,-1 ), 0 );
	m_pCheck_SoilTexture->SetValue(true);
	bSizer16->Add( m_pCheck_SoilTexture, 0, wxALIGN_CENTER|wxALL, 5 );

	wxStaticText* txt_scexpert_sand;
	txt_scexpert_sand = new wxStaticText( this, wxID_ANY, wxT("sand"), wxDefaultPosition, wxSize( 50,-1 ), 0 );
	bSizer16->Add( txt_scexpert_sand, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pSlider_SoilTexture = new wxSlider( this, ID_CMD_SSA_SLIDER_SOIL_TEXTURE, 0, 0, 100, wxDefaultPosition, wxSize( 100,-1 ), wxSL_HORIZONTAL );
	bSizer16->Add( m_pSlider_SoilTexture, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_scexpert_clay;
	txt_scexpert_clay = new wxStaticText( this, wxID_ANY, wxT("clay"), wxDefaultPosition, wxSize( 80,-1 ), 0 );
	bSizer16->Add( txt_scexpert_clay, 0, wxALIGN_CENTER|wxALL, 5 );
	
	sbSizerExpertKnowledge->Add( bSizer16, 0, wxLEFT|wxRIGHT, 5 );
	
	// plant growth --------------------------------------
	wxBoxSizer* bSizer17;
	bSizer17 = new wxBoxSizer( wxHORIZONTAL );

	m_pCheck_PlantGrowth = new wxCheckBox( this, ID_CMD_SSA_CHECK_PLANT_GROWTH, wxT("Plant growth characteristics"), wxDefaultPosition, wxSize( 155,-1 ), 0 );
	m_pCheck_PlantGrowth->SetValue(true);
	bSizer17->Add( m_pCheck_PlantGrowth, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_pg_normally;
	txt_pg_normally = new wxStaticText( this, wxID_ANY, wxT("normally"), wxDefaultPosition, wxSize( 50,-1 ), 0 );
	bSizer17->Add( txt_pg_normally, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pSlider_PlantGrowth = new wxSlider( this, ID_CMD_SSA_SLIDER_PLANTGROWTH, 0, 0, 100, wxDefaultPosition, wxSize( 100,-1 ), wxSL_HORIZONTAL );
	bSizer17->Add( m_pSlider_PlantGrowth, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_pg_underdev;
	txt_pg_underdev = new wxStaticText( this, wxID_ANY, wxT("underdeveloped"), wxDefaultPosition, wxSize( 80,-1 ), 0 );
	bSizer17->Add( txt_pg_underdev, 0, wxALIGN_CENTER|wxALL, 5 );
	
	sbSizerExpertKnowledge->Add( bSizer17, 0, wxLEFT|wxRIGHT, 5 );

	// ground water salinity --------------------------------------
	wxBoxSizer* bSizer171;
	bSizer171 = new wxBoxSizer( wxHORIZONTAL );

	m_pCheck_GW_Salinity = new wxCheckBox( this, ID_CMD_SSA_CHECK_GW_SALINITY, wxT("Ground water salinity"), wxDefaultPosition, wxSize( 155,-1 ), 0 );
	m_pCheck_GW_Salinity->SetValue(true);
	bSizer171->Add( m_pCheck_GW_Salinity, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_gws_low;
	txt_gws_low = new wxStaticText( this, wxID_ANY, wxT("low"), wxDefaultPosition, wxSize( 50,-1 ), 0 );
	bSizer171->Add( txt_gws_low, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pSlider_GW_Salinity = new wxSlider( this, ID_CMD_SSA_SLIDER_GW_SALINITY, 0, 0, 100, wxDefaultPosition, wxSize( 100,-1 ), wxSL_HORIZONTAL );
	bSizer171->Add( m_pSlider_GW_Salinity, 0, wxALIGN_CENTER|wxALL, 5 );
	
	wxStaticText* txt_pg_gws_high;
	txt_pg_gws_high = new wxStaticText( this, wxID_ANY, wxT("high"), wxDefaultPosition, wxSize( 80,-1 ), 0 );
	bSizer171->Add( txt_pg_gws_high, 0, wxALIGN_CENTER|wxALL, 5 );
	
	sbSizerExpertKnowledge->Add( bSizer171, 0, wxLEFT|wxRIGHT, 5 );
	
	// ground water level --------------------------------------
	wxBoxSizer* bSizer151;
	bSizer151 = new wxBoxSizer( wxHORIZONTAL );

	m_pCheck_GW_Level = new wxCheckBox( this, ID_CMD_SSA_CHECK_GW_LEVEL, wxT("Ground water level"), wxDefaultPosition, wxSize( 155,-1 ), 0 );
	m_pCheck_GW_Level->SetValue(true);
	bSizer151->Add( m_pCheck_GW_Level, 0, wxALIGN_CENTER|wxALL, 5 );
	
	bSizer151->Add( 60, 0, 1, wxEXPAND, 5 );
	
	m_pCombo_GW_Level = new wxComboBox( this, ID_CMD_SSA_COMBO_GW_LEVEL, wxEmptyString, wxDefaultPosition, wxSize( 100,-1 ), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	m_pCombo_GW_Level->Append( wxT("> 200 cm") );
	m_pCombo_GW_Level->Append( wxT("> 100 < 200 cm") );
	m_pCombo_GW_Level->Append( wxT(">50 < 100 cm") );
	m_pCombo_GW_Level->Append( wxT("50 cm") );
	m_pCombo_GW_Level->SetSelection(0);
	bSizer151->Add( m_pCombo_GW_Level, 0, wxALIGN_CENTER|wxALL, 5 );
		
	sbSizerExpertKnowledge->Add( bSizer151, 0, wxLEFT|wxRIGHT, 5 );

	wxStaticBoxSizer* sbSizer111;
	sbSizer111 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Drainage System") ), wxVERTICAL );
	
	wxBoxSizer* bSizer291;
	bSizer291 = new wxBoxSizer( wxHORIZONTAL );
	
	// number of channels --------------------------------------
	wxBoxSizer* bSizer301;
	bSizer301 = new wxBoxSizer( wxVERTICAL );

	m_pCheck_nChannels = new wxCheckBox( this, ID_CMD_SSA_CHECK_N_CHANNELS, wxT("Channels"), wxDefaultPosition, wxSize( 100,-1 ), 0 );
	m_pCheck_nChannels->SetValue(true);
	bSizer301->Add( m_pCheck_nChannels, 0, wxALL, 5 );

	m_pCombo_nChannels = new wxComboBox( this, ID_CMD_SSA_COMBO_N_CHANNELS, wxEmptyString, wxDefaultPosition, wxSize( 100,-1 ), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	m_pCombo_nChannels->Append( wxT("0") );
	m_pCombo_nChannels->Append( wxT("1") );
	m_pCombo_nChannels->Append( wxT("2") );
	m_pCombo_nChannels->Append( wxT("3") );
	m_pCombo_nChannels->Append( wxT("4") );
	bSizer301->Add( m_pCombo_nChannels, 0, wxALIGN_CENTER|wxALL, 5 );
	m_pCombo_nChannels->SetSelection(0);
	m_pCombo_nChannels->Select(0);
	bSizer291->Add( bSizer301, 0, wxALIGN_CENTER, 5 );
	
	// distance to nearest channel --------------------------------------
	wxBoxSizer* bSizer311;
	bSizer311 = new wxBoxSizer( wxVERTICAL );
	
	m_pCheck_Distance = new wxCheckBox( this, ID_CMD_SSA_CHECK_DISTANCE, wxT("Distance to ch."), wxDefaultPosition, wxSize( 100,-1 ), 0 );
	bSizer311->Add( m_pCheck_Distance, 0, wxALL, 5 );
	
	m_pCombo_ChannelDistance = new wxComboBox( this, ID_CMD_SSA_COMBO_CHANNEL_DISTANCE, wxEmptyString, wxDefaultPosition, wxSize( 100,-1 ), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	m_pCombo_ChannelDistance->Append( wxT("close") );
	m_pCombo_ChannelDistance->Append( wxT("moderately") );
	m_pCombo_ChannelDistance->Append( wxT("far") );
	bSizer311->Add( m_pCombo_ChannelDistance, 0, wxALIGN_CENTER|wxALL, 5 );
	m_pCombo_ChannelDistance->SetSelection(0);
	bSizer291->Add( bSizer311, 0, wxALIGN_CENTER, 5 );

	_On_Change_nChannels();
	
	// channel maintenance --------------------------------------
	wxBoxSizer* bSizer321;
	bSizer321 = new wxBoxSizer( wxVERTICAL );

	m_pCheck_Maintenance = new wxCheckBox( this, ID_CMD_SSA_CHECK_MAINTENANCE, wxT("Maintenance"), wxDefaultPosition, wxSize( 100,-1 ), 0 );
	m_pCheck_Maintenance->SetValue(true);
	bSizer321->Add( m_pCheck_Maintenance, 0, wxALL, 5 );
	
	m_pCombo_Maintenance = new wxComboBox( this, ID_CMD_SSA_COMBO_MAINTENANCE, wxEmptyString, wxDefaultPosition, wxSize( 100,-1 ), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	m_pCombo_Maintenance->Append( wxT("good") );
	m_pCombo_Maintenance->Append( wxT("medium") );
	m_pCombo_Maintenance->Append( wxT("bad") );
	bSizer321->Add( m_pCombo_Maintenance, 0, wxALIGN_CENTER|wxALL, 5 );
	m_pCombo_Maintenance->SetSelection(0);
	bSizer291->Add( bSizer321, 0, wxALIGN_CENTER, 5 );
	
	sbSizer111->Add( bSizer291, 0, 0, 5 );
	
	sbSizerExpertKnowledge->Add( sbSizer111, 0, wxBOTTOM|wxLEFT|wxTOP, 5 );
	
	/////////////////////////////////////////////////////
	// later:	bSizerCalcExpertKnowledge->Add( sbSizerExpertKnowledge, 0, wxALL, 5 );
	//bSizerMainBoxH->Add( sbSizerExpertKnowledge, 0, wxALL, 5 );
	
	/////////////////////////////////////////////////////
	// CALCULATIONS
	/////////////////////////////////////////////////////
	wxStaticBox* sbs4 = new wxStaticBox( this, wxID_ANY, wxT("Calculate Salinity") );
	sbs4->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
	wxStaticBoxSizer* sbSizerCalculate = new wxStaticBoxSizer(sbs4, wxVERTICAL);

	wxBoxSizer* bSizerAssessmentMembershipButton;
	bSizerAssessmentMembershipButton = new wxBoxSizer( wxVERTICAL );
	
	wxBoxSizer* bSizerAssessmentMembership;
	bSizerAssessmentMembership = new wxBoxSizer( wxHORIZONTAL );
	
	wxStaticBoxSizer* sbSizerAssessment;
	sbSizerAssessment = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Salinity Assessment") ), wxHORIZONTAL );
	
	wxBoxSizer* bSizer24;
	bSizer24 = new wxBoxSizer( wxVERTICAL );
	
	wxStaticText* m_staticText38 = new wxStaticText( this, wxID_ANY, wxT("Value"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizer24->Add( m_staticText38, 0, wxLEFT|wxTOP, 3 );
	
	m_pTxtCtrl_SalinityValue = new wxTextCtrl( this, ID_CMD_SSA_TXTCTRL_SALINITY_VALUE, wxEmptyString, wxDefaultPosition, wxSize( 60,-1 ), wxTE_READONLY );
	bSizer24->Add( m_pTxtCtrl_SalinityValue, 0, wxALL, 5 );
	
	wxStaticText* m_staticText36 = new wxStaticText( this, wxID_ANY, wxT("local"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizer24->Add( m_staticText36, 0, wxLEFT|wxTOP, 4 );
	
	m_pTxtCtrl_SalinityValueLocal = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 60,-1 ), wxTE_READONLY );
	bSizer24->Add( m_pTxtCtrl_SalinityValueLocal, 0, wxALL, 5 );
	
	sbSizerAssessment->Add( bSizer24, 0, wxALL, 2 );
	
	wxBoxSizer* bSizer25;
	bSizer25 = new wxBoxSizer( wxVERTICAL );
	
	wxStaticText* m_staticText39 = new wxStaticText( this, wxID_ANY, wxT("Category"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizer25->Add( m_staticText39, 0, wxLEFT|wxTOP, 3 );
	
	m_pTxtCtrl_SalinityClass = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 60,-1 ), wxTE_READONLY );
	bSizer25->Add( m_pTxtCtrl_SalinityClass, 0, wxALL, 5 );
	m_pTxtCtrl_SalinityClass->SetBackgroundColour(*wxWHITE);
	
	wxStaticText* m_staticText37 = new wxStaticText( this, wxID_ANY, wxT("expert"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizer25->Add( m_staticText37, 0, wxLEFT|wxTOP, 4 );
	
	m_pTxtCtrl_SalinityValueExpert = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 60,-1 ), wxTE_READONLY );
	bSizer25->Add( m_pTxtCtrl_SalinityValueExpert, 0, wxALL, 5 );
	
	sbSizerAssessment->Add( bSizer25, 0, wxALL, 2 );
	
	bSizerAssessmentMembership->Add( sbSizerAssessment, 0, wxALL, 5 );
	
	
	bSizerAssessmentMembership->Add( 20, 0, 0, wxALL, 5 );
	
	wxStaticBoxSizer* sbSizerMembership;
	sbSizerMembership = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Membership") ), wxVERTICAL );
	
	wxBoxSizer* bSizer26;
	bSizer26 = new wxBoxSizer( wxHORIZONTAL );
	
	wxStaticText* txt_mslow;
	txt_mslow = new wxStaticText( this, wxID_ANY, wxT("low"), wxDefaultPosition, wxSize( 40,-1 ), 0 );
	bSizer26->Add( txt_mslow, 0, wxALL, 5 );
	
	m_pTxtCtrl_MSLow = new wxTextCtrl( this, ID_CMD_SSA_TXTCTRL_MSLOW, wxEmptyString, wxDefaultPosition, wxSize( 60,-1 ), wxTE_READONLY );
	bSizer26->Add( m_pTxtCtrl_MSLow, 0, wxALIGN_CENTER_VERTICAL, 5 );
	
	sbSizerMembership->Add( bSizer26, 0, wxALL, 5 );
	
	wxBoxSizer* bSizer27;
	bSizer27 = new wxBoxSizer( wxHORIZONTAL );
	
	wxStaticText* txt_msmedium;
	txt_msmedium = new wxStaticText( this, wxID_ANY, wxT("medium"), wxDefaultPosition, wxSize( 40,-1 ), 0 );
	bSizer27->Add( txt_msmedium, 0, wxALL, 5 );
	
	m_pTxtCtrl_MSMedium = new wxTextCtrl( this, ID_CMD_SSA_TXTCTRL_MSMEDIUM, wxEmptyString, wxDefaultPosition, wxSize( 60,-1 ), wxTE_READONLY );
	bSizer27->Add( m_pTxtCtrl_MSMedium, 0, wxALIGN_CENTER_VERTICAL, 5 );
	
	sbSizerMembership->Add( bSizer27, 0, wxALL, 5 );
	
	wxBoxSizer* bSizer28;
	bSizer28 = new wxBoxSizer( wxHORIZONTAL );
	
	wxStaticText* txt_mshigh;
	txt_mshigh = new wxStaticText( this, wxID_ANY, wxT("high"), wxDefaultPosition, wxSize( 40,-1 ), 0 );
	bSizer28->Add( txt_mshigh, 0, wxALL, 5 );
	
	m_pTxtCtrl_MSHigh = new wxTextCtrl( this, ID_CMD_SSA_TXTCTRL_MSHIGH, wxEmptyString, wxDefaultPosition, wxSize( 60,-1 ), wxTE_READONLY );
	bSizer28->Add( m_pTxtCtrl_MSHigh, 0, wxALIGN_CENTER_VERTICAL, 5 );
	
	sbSizerMembership->Add( bSizer28, 0, wxALL, 5 );
	
	bSizerAssessmentMembership->Add( sbSizerMembership, 0, wxALL, 5 );

	bSizerAssessmentMembership->Add( 10, 0, 0, wxALL, 5 );

	// Slider show salinity ---------------------------------------------
	wxBoxSizer* bSizerSliderSalinity;
	bSizerSliderSalinity = new wxBoxSizer( wxVERTICAL );
	
	wxStaticText* m_staticText40 = new wxStaticText( this, wxID_ANY, wxT("max"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerSliderSalinity->Add( m_staticText40, 0, wxALIGN_CENTER_HORIZONTAL, 5 );
	
	m_pSliderShowSalinity = new wxSlider( this, ID_CMD_SSA_SLIDER_SHOW_SALINITY, 0, 0, 100, wxDefaultPosition, wxSize( -1,100 ), wxSL_BOTH|wxSL_INVERSE|wxSL_VERTICAL );
	bSizerSliderSalinity->Add( m_pSliderShowSalinity, 0, wxALIGN_CENTER_HORIZONTAL, 5 );
	m_pSliderShowSalinity->Enable( false );

	wxStaticText* m_staticText391 = new wxStaticText( this, wxID_ANY, wxT("min"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerSliderSalinity->Add( m_staticText391, 0, wxALIGN_CENTER_HORIZONTAL, 5 );
	
	bSizerAssessmentMembership->Add( bSizerSliderSalinity, 0, wxALL, 5 );


	bSizerAssessmentMembershipButton->Add( bSizerAssessmentMembership, 0, 0, 5 );

	// button calculate salinity ---------------------------------------------
	wxBoxSizer* bSizerButton;
	bSizerButton = new wxBoxSizer( wxHORIZONTAL );
	
	m_pButton_CalcSalinity = new wxButton( this, ID_CMD_SSA_BUTTON_CALC_SALINITY, wxT("Calculate Salinity"), wxDefaultPosition, wxSize( 200,-1 ), 0 );
	m_pButton_CalcSalinity->SetDefault(); 
	m_pButton_CalcSalinity->SetFont( wxFont( wxNORMAL_FONT->GetPointSize(), 70, 90, 90, false, wxEmptyString ) );
	
	bSizerButton->Add( m_pButton_CalcSalinity, 0, wxALIGN_CENTER, 5 );
	
	bSizerAssessmentMembershipButton->Add( bSizerButton, 0, wxALIGN_CENTER|wxALL, 5 );
	
	sbSizerCalculate->Add( bSizerAssessmentMembershipButton, 0, wxALL, 5 );



	/////////////////////////////////////////////////////
	bSizerCalcExpertKnowledge->Add( sbSizerCalculate, 0, wxALL, 5);
	bSizerCalcExpertKnowledge->Add( sbSizerExpertKnowledge, 0, wxALL, 5 );
	//bSizerMainBoxH->Add( sbSizerExpertKnowledge, 0, wxALL, 5 );
	/////////////////////////////////////////////////////
	
	wxBoxSizer* bSizer21;
	bSizer21 = new wxBoxSizer( wxHORIZONTAL );
	
	m_pButton_Save = new wxButton( this, ID_CMD_SSA_BUTTON_SAVE, wxT("Save"), wxDefaultPosition, wxSize( 150,-1 ), 0 );
	m_pButton_Save->SetDefault(); 
	bSizer21->Add( m_pButton_Save, 0, wxALIGN_CENTER|wxALL, 5 );
	
	m_pButton_Cancel = new wxButton( this, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition, wxSize( 150,-1 ), 0 );
	m_pButton_Cancel->SetDefault(); 
	bSizer21->Add( m_pButton_Cancel, 0, wxALIGN_CENTER|wxALL, 5 );
	
	/////////////////////////////////////////////////////
	bSizerCalcExpertKnowledge->Add( bSizer21, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxTOP, 10 );
	//bSizerMainBoxH->Add( bSizer21, 0, wxALIGN_CENTER|wxTOP, 5 );
	/////////////////////////////////////////////////////

	bSizerMainBoxH->Add( bSizerCalcExpertKnowledge, 0, wxALL, 5 );

	this->SetSizer( bSizerMainBoxH );
	this->Layout();
	bSizerMainBoxH->Fit( this );

	m_pButton_Save->Enable(false);

	m_zero_ok = true;
}


//---------------------------------------------------------------------

wxArrayString CSoilSalinityAssessmentDlg::_Get_Plants()
{
	wxArrayString plants = NULL;
	std::ostringstream sql;

	if ( g_pSAGA_Frame->db->CheckAccessibility(m_pSSMap->Get_AttributeTableName().c_str()) ) {

		sql << "SELECT DISTINCT plant FROM " << m_pSSMap->Get_AttributeTableName();

		PGresult* res = PQexec(g_pSAGA_Frame->db->GetConn(),sql.str().c_str());
		if (PQresultStatus(res) != PGRES_TUPLES_OK)
		{
			PQclear(res);
			return(plants);
		} else {
			for (int i = 0; i < PQntuples(res); i++) {
				plants.Add(PQgetvalue(res,i,0));
			}
			PQclear(res);
			return(plants);
		}
	} else {
		return(plants);
	}
}
//---------------------------------------------------------------------
void CSoilSalinityAssessmentDlg::_Refresh_Plants()
{
	wxArrayString plants = _Get_Plants();

	m_pCombo_Plant->Clear();

	for (unsigned int i = 0; i < plants.size(); i++) {
		m_pCombo_Plant->Append(plants[i]);
	}
}

//---------------------------------------------------------------------

CSG_String CSoilSalinityAssessmentDlg::_Get_InsertString()
{
	CSG_String sql;
	sql.Append("INSERT INTO ");
	sql.Append(m_pSSMap->Get_AttributeTableName());
	sql.Append(" (fid, Yr, Mnth, salinity, salinity_v, salinity_l, salinity_e, ms_low, ms_medium, ms_high, plant) ");
	sql.Append("VALUES (");
	sql.Append(m_pCombo_Field->GetStringSelection().c_str());
	sql.Append(", ");
	sql.Append(m_pTxtCtrl_Year->GetValue().c_str());
	sql.Append(", '");
	sql.Append(m_pCombo_Month->GetValue().c_str());
	sql.Append("', '");
	sql.Append(m_pTxtCtrl_SalinityClass->GetValue());
	sql.Append("', ");
	sql.Append(m_pTxtCtrl_SalinityValue->GetValue());
	sql.Append(", ");
	sql.Append(m_pTxtCtrl_SalinityValueLocal->GetValue());
	sql.Append(", ");
	sql.Append(m_pTxtCtrl_SalinityValueExpert->GetValue());
	sql.Append(", ");
	sql.Append(m_pTxtCtrl_MSLow->GetValue());
	sql.Append(", ");
	sql.Append(m_pTxtCtrl_MSMedium->GetValue());
	sql.Append(", ");
	sql.Append(m_pTxtCtrl_MSHigh->GetValue());
	sql.Append(", '");
	sql.Append(m_pCombo_Plant->GetValue().c_str());
	sql.Append("')");

	return(sql);
}

//---------------------------------------------------------------------

CSG_String CSoilSalinityAssessmentDlg::_Get_InsertString_Settings()
{
	std::ostringstream insert;
	std::ostringstream sql;
	int fk = 0; // foreign key value

	insert << "INSERT INTO ";
	insert << m_pSSMap->Get_SettingsTableName();
	insert << " (fk_id, fid, b_sc_l, sc_l, b_nleaching, nleaching, b_wq_l, wq_l, ";
	insert << "b_wq_i, wq_i, b_wv_i, wv_i, b_even, even, b_morph, morph, ";
	insert << "b_s_t, s_t, b_pg, pg, b_gw_s, gw_s, b_gw_l, gw_l, ";
	insert << "b_nchannels, nchannels, b_distance, distance, b_maintenance, maintenance) ";
	insert << "VALUES (";
	insert << m_last_serial << ", ";
	insert << m_pCombo_Field->GetStringSelection().c_str();
	insert << (", ");
	for (int i = 0; i < m_pSSMap->Get_NumberSSVariables() - 1; i++) {
		insert << m_checked[i] << ", ";
		insert << m_settings[i] << ", ";
	}
	insert << m_checked[m_pSSMap->Get_NumberSSVariables() - 1] << ", ";
	insert << m_settings[m_pSSMap->Get_NumberSSVariables() - 1] << ")";

	return( insert.str().c_str() );
}
//---------------------------------------------------------------------

CSG_String CSoilSalinityAssessmentDlg::_Get_Message_Exist()
{
	std::ostringstream message;
	message << "The record already exists!\n" << "Year: " << m_pTxtCtrl_Year->GetValue().c_str();
	message << "\n Month: " << m_pCombo_Month->GetValue().c_str();
	message << "\n FieldID: " << m_pCombo_Field->GetCurrentSelection()+1; 
	message << "\n\n Overwrite?";

	return(message.str().c_str());
}
//---------------------------------------------------------------------

CSG_String CSoilSalinityAssessmentDlg::_Get_SQL_Update_Record(int pk_colnumber)
{
	std::ostringstream update;
	update << "UPDATE " << m_pSSMap->Get_AttributeTableName();
	update << " SET salinity = '" << m_pTxtCtrl_SalinityClass->GetValue() << "',";
	update << " salinity_v = " << m_pTxtCtrl_SalinityValue->GetValue() << ",";
	update << " salinity_l = " << m_pTxtCtrl_SalinityValueLocal->GetValue() << ",";
	update << " salinity_e = " << m_pTxtCtrl_SalinityValueExpert->GetValue() << ",";
	update << " ms_low = " << m_pTxtCtrl_MSLow->GetValue() << ",";
	update << " ms_medium = " << m_pTxtCtrl_MSMedium->GetValue() << ",";
	update << " ms_high = " << m_pTxtCtrl_MSHigh->GetValue() << ",";
	update << " plant = '" << m_pCombo_Plant->GetValue().c_str() << "'";
	update << " WHERE pk_id = " << pk_colnumber;

	return(update.str().c_str());
}
//---------------------------------------------------------------------

CSG_String CSoilSalinityAssessmentDlg::_Get_SQL_Update_Settings(int pk_colnumber)
{
	_Assign_Settings();

	_Assign_ParmsChecked();

	std::ostringstream update;
	update << "UPDATE " << m_pSSMap->Get_SettingsTableName();
	update << " SET fid = " << m_pCombo_Field->GetValue() << ",";
	update << " b_sc_l = " << m_checked[0] << ", ";
	update << " sc_l = " << m_settings[0] << ",";
	update << " b_nleaching = " << m_checked[1] << ", ";
	update << " nleaching = " << m_settings[1] << ",";
	update << " b_wq_l = " << m_checked[2] << ", ";
	update << " wq_l = " << m_settings[2] << ",";
	update << " b_wq_i = " << m_checked[3] << ", ";
	update << " wq_i = " << m_settings[3] << ",";
	update << " b_wv_i = " << m_checked[4] << ", ";
	update << " wv_i = " << m_settings[4] << ",";
	update << " b_even = " << m_checked[5] << ", ";
	update << " even = " << m_settings[5] << ",";
	update << " b_morph = " << m_checked[6] << ", ";
	update << " morph = " << m_settings[6] << ",";
	update << " b_s_t = " << m_checked[7] << ", ";
	update << " s_t = " << m_settings[7] << ", ";
	update << " b_pg = " << m_checked[8] << ", ";
	update << " pg = " << m_settings[8] << ",";
	update << " b_gw_s = " << m_checked[9] << ", ";
	update << " gw_s = " << m_settings[9] << ", ";
	update << " b_gw_l = " << m_checked[10] << ", ";
	update << " gw_l = " << m_settings[10] << ", ";
	update << " b_nchannels = " << m_checked[11] << ", ";
	update << " nchannels = " << m_settings[11] << ",";
	update << " b_distance = " << m_checked[12] << ", ";
	update << " distance = " << m_settings[12] << ",";
	update << " b_maintenance = " << m_checked[13] << ", ";
	update << " maintenance = " << m_settings[13];
	update << " WHERE fk_id = " << pk_colnumber;
	
	return(update.str().c_str());
}
//---------------------------------------------------------------------

bool CSoilSalinityAssessmentDlg::_Record_Exists(int &pk_colnumber)
{
	pk_colnumber = -999;

	//CSG_String attr_name = m_pSSMap->Get_AttributeTableName();
	CSG_String sql = "SELECT * FROM ";
	sql.Append(m_pSSMap->Get_AttributeTableName());
	sql.Append(" WHERE fid=");
	sql.Append(m_pCombo_Field->GetStringSelection().c_str());
	sql.Append(" AND Yr=");
	sql.Append(m_pTxtCtrl_Year->GetValue().c_str());
	sql.Append(" AND Mnth='");
	sql.Append(m_pCombo_Month->GetValue().c_str());
	sql.Append("'");
	
	PGresult* res = PQexec(g_pSAGA_Frame->db->GetConn(),sql.c_str());
	if (PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		MSG_ADD("unable to open table");
		MSG_ADD(wxString::Format("%s", PQresultErrorMessage(res)));
		PQclear(res);
		return(true);
	} else {
		if ( PQntuples(res) == 0 ) {
			PQclear(res);
			return(false);
		} else {		
			pk_colnumber = convert_sl::StringToInt(PQgetvalue( res,0,g_pSAGA_Frame->db->_GetPrimaryKeyColumnNumber(m_pSSMap->Get_AttributeTableName().c_str())) );
			PQclear(res);
			return(true);
		}
	}
}
//---------------------------------------------------------------------

void CSoilSalinityAssessmentDlg::_Delete_Controls()
{
	delete m_pCombo_ID;
	delete m_pCombo_Field;
	delete m_pTxtCtrl_Year;
	delete m_pCombo_Month;
	delete m_pCombo_Plant;

	// local knowledge
	delete m_pCheck_SoilColour;
	delete m_pSlider_SoilColour_Local;
	delete m_pCheck_Leaching;
	delete m_pCombo_nLeaching;
	delete m_pCheck_WQ_Leaching;
	delete m_pSlider_WaterQualityLeaching;
	delete m_pCheck_WQ_Irrigation;
	delete m_pSlider_WaterQualityIrrigation;
	delete m_pCheck_WaterVolume_Irrigation;
	delete m_pCombo_WaterVolumeIrrigation;
	delete m_pCheck_Evenness;
	delete m_pSlider_Evenness;
	delete m_pCheck_Morphology;
	delete m_pCombo_Morphology;

	// expert knowledge
	delete m_pCheck_SoilTexture;
	delete m_pSlider_SoilTexture;
	delete m_pCheck_PlantGrowth;
	delete m_pSlider_PlantGrowth;
	delete m_pCheck_GW_Salinity;
	delete m_pSlider_GW_Salinity;
	delete m_pCheck_GW_Level;
	delete m_pCombo_GW_Level;
	delete m_pCheck_nChannels;
	delete m_pCombo_nChannels;
	delete m_pCheck_Distance;
	delete m_pCombo_ChannelDistance;
	delete m_pCheck_Maintenance;
	delete m_pCombo_Maintenance;

	delete m_pButton_CalcSalinity;
	delete m_pTxtCtrl_SalinityValue;
	delete m_pTxtCtrl_SalinityClass;
	delete m_pTxtCtrl_MSLow;
	delete m_pTxtCtrl_MSMedium;
	delete m_pTxtCtrl_MSHigh;

}
//---------------------------------------------------------------------


void CSoilSalinityAssessmentDlg::_Zero_Controls()
{
	m_pTxtCtrl_SalinityValue->SetValue("");
	m_pTxtCtrl_SalinityClass->SetValue("");
	m_pTxtCtrl_SalinityValueLocal->SetValue("");
	m_pTxtCtrl_SalinityValueExpert->SetValue("");
	m_pTxtCtrl_MSLow->SetValue("");
	m_pTxtCtrl_MSMedium->SetValue("");
	m_pTxtCtrl_MSHigh->SetValue("");
	m_pTxtCtrl_SalinityClass->SetBackgroundColour(*wxWHITE);
}
//---------------------------------------------------------------------
