// Registration.cpp : implementation file
//

#include "stdafx.h"
#include "Registration.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRegistration dialog


CRegistration::CRegistration(CWnd* pParent /*=NULL*/)
	: CDialog(CRegistration::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRegistration)
	m_cKeyCode = _T("");
	m_cKeyPassword = _T("");
	//}}AFX_DATA_INIT

  m_hIcon = LoadIcon( AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));
  m_nFirst = 1;
}


void CRegistration::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRegistration)
	DDX_Control(pDX, IDC_PRODUCT, m_cProduct);
	DDX_Control(pDX, IDC_COMPANY, m_cCompany);
	DDX_Control(pDX, IDC_EMAILLINK, m_cEmailLink);
	DDX_Control(pDX, IDOK, m_cButtonOk);
	DDX_Control(pDX, IDCANCEL, m_cButtonCancel);
	DDX_Control(pDX, IDC_KEYPASSWORD, m_cEditKeyPassword);
	DDX_Control(pDX, IDC_KEYCODE, m_cEditKeyCode);
	DDX_Text(pDX, IDC_KEYCODE, m_cKeyCode);
	DDV_MaxChars(pDX, m_cKeyCode, 6);
	DDX_Text(pDX, IDC_KEYPASSWORD, m_cKeyPassword);
	DDV_MaxChars(pDX, m_cKeyPassword, 16);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRegistration, CDialog)
	//{{AFX_MSG_MAP(CRegistration)
	ON_BN_CLICKED(IDC_PRODUCT, OnProduct)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRegistration message handlers

//////////////////////////////////////////////////////////////////////
// BOOL CRegistration::OnInitDialog() 
// 
// Initilise all the data and controls of the dialog box just before 
// showing.
//////////////////////////////////////////////////////////////////////
BOOL CRegistration::OnInitDialog() 
{

	CDialog::OnInitDialog();

	//set the icon for this dialog. The framework does this automatically
	//when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			//set big icon
	SetIcon(m_hIcon, FALSE);		//set small icon

  //Button styles
  m_cButtonOk.SetIcon( IDI_OK);
  m_cButtonOk.SetFlat( FALSE);
  m_cButtonCancel.SetIcon( IDI_EXPLORE);
  m_cButtonCancel.SetFlat( FALSE);

	//Set HyperLink for E-Mail
	m_cEmailLink.SetURL(_T(IDS_MAILADDR));
	m_cEmailLink.SetUnderline(TRUE);
	m_cEmailLink.SetLinkCursor(LoadCursor( AfxGetResourceHandle(), MAKEINTRESOURCE(IDC_HAND)));

  SetWindowText( AfxGetAppName());

  m_cCompany.SetWindowText( GetCompanyName());
  m_cProduct.SetWindowText( GetProductName());
  SetWindowText( GetProductName());

	return TRUE;  //return TRUE unless you set the focus to a control
	              //EXCEPTION: OCX Property Pages should return FALSE
}

//////////////////////////////////////////////////////////////////////
// void CRegistration::OnOK() 
// 
// Registration code entered by user, validate and report result
//////////////////////////////////////////////////////////////////////
void CRegistration::OnOK() 
{
	//update the dialog box data, befor using
  UpdateData();
  int ready = TRUE;

  if (!m_oEncrypt.SetKey( m_cKeyCode))
  {

    CString invalid;
    if (!invalid.LoadString( IDS_INVALIDSERIAL))
      invalid = _T("Invalid serial number!");

    //popup message box with error report
    AfxMessageBox( invalid, MB_OK|MB_ICONEXCLAMATION|MB_TOPMOST);
    //MessageBox( invalid, GetProductName(), MB_OK|MB_ICONEXCLAMATION|MB_TOPMOST);
    
    return;
  }

  //evaluate the serial number ans registration code
  if (!m_oEncrypt.SetPassword( m_cKeyPassword)) 
    ready = FALSE;
  else
  	if (!m_oEncrypt.SavePasswordToRegistry()) 
      ready = FALSE;

  //tell user if regisration has failed, just a simple message box
  if (ready) CDialog::OnOK();
  else
  {

    //load invalid regsitration message from resource
    CString invalid;
    if (!invalid.LoadString( IDS_INVALIDREG))
      invalid = _T("Invalid combination of serial number and registration code!");

    //popup message box with error report
    //MessageBox( invalid, GetProductName(), MB_OK|MB_ICONEXCLAMATION|MB_TOPMOST);
    AfxMessageBox( invalid, MB_OK|MB_ICONEXCLAMATION|MB_TOPMOST);
  }
}

//////////////////////////////////////////////////////////////////////
// BOOL CRegistration::IsRegistrated()
// 
// Check for product registration, function returns TRUE if registra-
// tion is found and ok, otherwise FALSE is returned
//////////////////////////////////////////////////////////////////////
BOOL CRegistration::IsRegistered()
{

  //try to setup encryption, quit and return failure
  if (!m_oEncrypt.Setup( GetProductName())) return FALSE;

  //try to read the pasword, return the failure status
  if (!m_oEncrypt.ReadPasswordFromRegistry()) return FALSE;

  //registration is found
  return TRUE;
}

//////////////////////////////////////////////////////////////////////
// void CRegistration::OnProduct() 
// 
// 
//////////////////////////////////////////////////////////////////////
void CRegistration::OnProduct() 
{
	// TODO: Add your control notification handler code here
	
}

//////////////////////////////////////////////////////////////////////
// void CRegistration::OnCancel() 
// 
// 
//////////////////////////////////////////////////////////////////////
void CRegistration::OnCancel() 
{
	// TODO: Add extra cleanup here
	
  EndDialog( 0);
	//CDialog::OnCancel();
}


void CRegistration::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	//If we don't have a console, our dialog is minimized, just show it
  if (m_nFirst)
  {

    //we only needs this the first time
	  m_nFirst = 0;

    //get our window position
    RECT Rect;
    GetWindowRect( &Rect);

    //the restore position is already determined, normally centered
    SetWindowPos( &wndTopMost, Rect.left, Rect.top, 0, 0, SWP_SHOWWINDOW|SWP_NOSIZE);

    //make sure we restore the window, otherwise it will still be minimized
    ShowWindow( SW_RESTORE);
  }
}
