// ConfigAdminDlg.cpp : 구현 파일입니다.
//

#include "stdafx.h"
#include "ConsoleApp.h"
#include "ConfigAdminDlg.h"
#include ".\configadmindlg.h"
#include ".\RexAlgorithm.h"
#include ".\RexInterface.h"
#include <string>

// CConfigAdminDlg 대화 상자입니다.

IMPLEMENT_DYNAMIC(CConfigAdminDlg, CDialog)
CConfigAdminDlg::CConfigAdminDlg(CWnd* pParent /*=NULL*/)
	: CConfigSubDlg(CConfigAdminDlg::IDD, pParent)
{
	m_show=false;
}

CConfigAdminDlg::~CConfigAdminDlg()
{
}

void CConfigAdminDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDT_NAME1, m_name_01);
	DDX_Text(pDX, IDC_EDT_NAME2, m_name_02);
	DDX_Text(pDX, IDC_EDT_NAME3, m_name_03);

	DDX_Text(pDX, IDC_EDT_PHONE1, m_phone_01);
	DDX_Text(pDX, IDC_EDT_PHONE2, m_phone_02);
	DDX_Text(pDX, IDC_EDT_PHONE3, m_phone_03);

	DDX_Text(pDX, IDC_EDT_MAIL1, m_mail_01);
	DDX_Text(pDX, IDC_EDT_MAIL2, m_mail_02);
	DDX_Text(pDX, IDC_EDT_MAIL3, m_mail_03);
}


BEGIN_MESSAGE_MAP(CConfigAdminDlg, CDialog)
	ON_EN_KILLFOCUS(IDC_EDT_NAME1, OnEnKillfocusEdtName1)
	ON_EN_KILLFOCUS(IDC_EDT_PHONE1, OnEnKillfocusEdtPhone1)
	ON_EN_KILLFOCUS(IDC_EDT_MAIL1, OnEnKillfocusEdtMail1)
	ON_EN_KILLFOCUS(IDC_EDT_NAME2, OnEnKillfocusEdtName2)
	ON_EN_KILLFOCUS(IDC_EDT_PHONE2, OnEnKillfocusEdtPhone2)
	ON_EN_KILLFOCUS(IDC_EDT_MAIL2, OnEnKillfocusEdtMail2)
	ON_EN_KILLFOCUS(IDC_EDT_NAME3, OnEnKillfocusEdtName3)
	ON_EN_KILLFOCUS(IDC_EDT_PHONE3, OnEnKillfocusEdtPhone3)
	ON_EN_KILLFOCUS(IDC_EDT_MAIL3, OnEnKillfocusEdtMail3)
END_MESSAGE_MAP()


// CConfigAdminDlg 메시지 처리기입니다.

BOOL CConfigAdminDlg::OnInitDialog()
{
	CConfigSubDlg::OnInitDialog();

	// TODO:  여기에 추가 초기화 작업을 추가합니다.

	m_name_01			= COptions::GetOption(OPTION_NAME_01);
	m_name_02			= COptions::GetOption(OPTION_NAME_02);
	m_name_03			= COptions::GetOption(OPTION_NAME_03);

	m_phone_01			= COptions::GetOption(OPTION_PHONE_01);
	m_phone_02			= COptions::GetOption(OPTION_PHONE_02);
	m_phone_03			= COptions::GetOption(OPTION_PHONE_03);

	m_mail_01			= COptions::GetOption(OPTION_MAIL_01);
	m_mail_02			= COptions::GetOption(OPTION_MAIL_02);
	m_mail_03			= COptions::GetOption(OPTION_MAIL_03);

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 예외: OCX 속성 페이지는 FALSE를 반환해야 합니다.
}

void CConfigAdminDlg::OnEnKillfocusEdtName1()
{

	if (m_show)
	{
		CheckName(IDC_EDT_NAME1);
	}
}

void CConfigAdminDlg::OnEnKillfocusEdtPhone1()
{
	if (m_show)
	{
		CheckPhone(IDC_EDT_PHONE1);
	}
}

void CConfigAdminDlg::OnEnKillfocusEdtMail1()
{
	if (m_show)
	{
		CheckMail(IDC_EDT_MAIL1);
	}
}

void CConfigAdminDlg::OnEnKillfocusEdtName2()
{
	if (m_show)
	{
		CheckName(IDC_EDT_NAME2);
	}	
}

void CConfigAdminDlg::OnEnKillfocusEdtPhone2()
{
	if (m_show)
	{
		CheckPhone(IDC_EDT_PHONE2);
	}
}

void CConfigAdminDlg::OnEnKillfocusEdtMail2()
{
	if (m_show)
	{
		CheckMail(IDC_EDT_MAIL2);
	}
}

void CConfigAdminDlg::OnEnKillfocusEdtName3()
{
	if (m_show)
	{
		CheckName(IDC_EDT_NAME3);
	}
}

void CConfigAdminDlg::OnEnKillfocusEdtPhone3()
{
	if (m_show)
	{
		CheckPhone(IDC_EDT_PHONE3);
	}
}

void CConfigAdminDlg::OnEnKillfocusEdtMail3()
{
	if (m_show)
	{
		CheckMail(IDC_EDT_MAIL3);
	}
}


bool CConfigAdminDlg::CheckName(int id)
{
	CString szValue;
	GetDlgItemText(id , szValue);

	if (szValue.GetLength()>0)
	{
		if (szValue.GetLength()>10)
		{
			AfxMessageBox("관리자 이름은 한글5자(영문10자) 이하로 입력하십시오.");
			DeleteAndSetFocus(id);
			return false;
		}
	}

	return true;
}

bool CConfigAdminDlg::CheckPhone(int id)
{
	CString szValue;
	GetDlgItemText(id , szValue);

	if (szValue.GetLength()>0)
	{
		if (szValue.GetLength()>12)
		{
			AfxMessageBox("핸드폰 번호는 12자리 숫자로 입력하십시오.");
			DeleteAndSetFocus(id);
			return false;
		}

		for (int i=0; i < szValue.GetLength(); i++)
		{
			if (FALSE==isdigit(szValue.GetAt(i)))
			{
				AfxMessageBox("핸드폰 번호는 12자리 숫자로 입력하십시오.");
				DeleteAndSetFocus(id);
				return false;
			}
		}


	}
	return true;
}

bool CConfigAdminDlg::CheckMail(int id)
{
	CString szValue;
	GetDlgItemText(id , szValue);

	if (szValue.GetLength()>0)
	{
		if (szValue.GetLength()>50)
		{
			AfxMessageBox("관리자 메일주소는 영문50자 이하로 입력하십시오.");
			DeleteAndSetFocus(id);
			return false;
		}

		REXI_Search rexs;
		REXI_DefErr err;
		err= rexs.SetRegexp("[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z0-9]+");

		int nMatchLen=0;
		bool bEosFound=false;
		bool bRightEmail=false;
		const char* pcsValue = szValue; 

		while(rexs.Find(pcsValue ,nMatchLen,bEosFound))
		{
			return true;
		}
		
		if (!bRightEmail)
		{
			AfxMessageBox("메일주소를 정확히 입력하십시오." );
			DeleteAndSetFocus(id);
			return false;
		}
	}

	return true;
}

void CConfigAdminDlg::DeleteAndSetFocus(int id)
{
	UpdateData(FALSE);
	this->GetDlgItem(id)->SetFocus();
}