%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Chapter and Appendix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\usepackage{titlesec}

\makeatletter

\def\ChapTitle/{}
\def\ChapAbbrev/{}
\def\ChapterNumber{0}
\def\ChapterName{}

%\newcount\count@Chapter
%\newcount\count@Appendix
\newcounter{Chapter}
\newcounter{Appendix}
%\count@Chapter= 0  
%\count@Appendix=`\A   %   Capital A
%\count@Appendix= 0   %   Capital A

\def\GiveChapNumber#1{%
	\stepcounter{Chapter}%
	%\expandafter\xdef\csname Chap#1\endcsname{\the\count@Chapter}%
	\expandafter\xdef\csname Chap#1\endcsname{\arabic{Chapter}}%
	%\advance\count@Chapter by1}
}

\gdef\GiveAppLetter#1{%
	\stepcounter{Appendix}%
	%\expandafter\xdef\csname App#1\endcsname{\char\the\count@Appendix}%
	\expandafter\xdef\csname refApp#1\endcsname{\Alph{Appendix}}% 1 Oct 2012, to allow ref by letter
	\expandafter\xdef\csname App#1\endcsname{\arabic{Appendix}}%   changed 20 Sept 2012
	%\advance\count@Appendix by1}
}

%\def\Chapter#1#2{%
%	\gdef\ChapTitle/{Chapter}%
%	\gdef\ChapAbbrev/{Chap}%
%	\gdef\runninghead{#2}%   % added15 Aug 2011 for 241
%	\@Chapter{#1}{#2}%
%}


%\def\Chapter#1#2{%  EXPERIMENT 20 April 2014
%	\ifDRAFT
%		\pagestyle{draft}\else\pagestyle{normal}%
%	\fi	
%	\thispagestyle{startchap}%
%	\setcounter{ProbNum}{0}% added7aug14
%%	\gdef\ChapTitle/{Chapter}%
%%	\gdef\ChapAbbrev/{Chap}%
%	\global\tok@Chap={#1::}% added :: 21 april 2014
%%	\gdef\runninghead{#2}%   % added15 Aug 2011 for 241
%%	\@Chapter{#1}{#2}%
%%	\gdef\ChapterNumber{\csname \ChapAbbrev/#1\endcsname}%
%%  	\setcounter{chapter}{\ChapterNumber}% for Latex numbering of sections
%	\gdef\TheNumber{\csname Chap#1\endcsname}%
%  	\setcounter{chapter}{\TheNumber}% for Latex numbering of sections
%  	\advance\c@chapter\m@ne
%	\renewcommand{\thechapter}{\arabic{chapter}}% 8 July 2015
%	\renewcommand{\@chapapp}{Chapter}%
%	\gdef\ChapterName{#2}%
%	\chapter{#2}%
%%\ifDRAFT
%%\pagestyle{draft}
%%\thispagestyle{startchap}%
%%\fi
%}

\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
%	\thispagestyle{plain}% 15 July 2015, to allow different pagestyle
	\global\@topnum\z@
	\@afterindentfalse
	\secdef\@chapter\@schapter}

\def\Chapter#1#2{
	\clearpage%	added 24 nov 2015
	\global\tok@Chap={#1::}% added :: 1 July 2015
	\gdef\ChapterName{#2}%
	\gdef\ChapTitle/{Chapter}%	added 20 nov 2015

  	\setcounter{chapter}{\csname Chap#1\endcsname}% for Latex numbering of sections
	\renewcommand{\thechapter}{\arabic{chapter}}% 8 July 2015
	\renewcommand{\@chapapp}{Chapter}%
	\@Chapter{#2}%
}

%\def\Appendix#1#2{
%	\ifDRAFT
%		\pagestyle{draft}\else\pagestyle{normal}%
%	\fi	
%	\thispagestyle{startchap}%
%	
%	\setcounter{ProbNum}{0}% added7aug14
%	\global\tok@Chap={#1::}% added :: 1 July 2015
%	\gdef\TheNumber{\csname App#1\endcsname}%
%  	\setcounter{chapter}{\TheNumber}% for Latex numbering of sections
%  	\advance\c@chapter\m@ne% needed because \chapter does a refstep
%	\renewcommand{\thechapter}{\Alph{chapter}}% 8 July 2015
%	\renewcommand{\@chapapp}{Appendix}%
%	\gdef\ChapterName{#2}%
%	\chapter{#2}%
%}

\def\Appendix#1#2{
	\clearpage%	added 24 nov 2015
	\global\tok@Chap={#1::}% added :: 1 July 2015
	\gdef\ChapterName{#2}%
	\gdef\ChapTitle/{Appendix}%	added 20 nov 2015
  	\setcounter{chapter}{\csname App#1\endcsname}% for Latex numbering of sections
	\renewcommand{\thechapter}{\Alph{chapter}}% 8 July 2015
	\renewcommand{\@chapapp}{Appendix}%
	\@Chapter{#2}%
}


\def\@Chapter#1{% #1 = chap/app title
	\ifDRAFT
		\pagestyle{draft}\else\pagestyle{normal}%
	\fi	
	\thispagestyle{startchap}%
	\setcounter{ProbNum}{0}% added7aug14
	\advance\c@chapter\m@ne% needed because \chapter does a refstep
	\chapter{#1}%
}

\def\first@page#1#2{\relax}  %written to .aux file by \@Chapter
\def\last@page#1#2{\relax} 
\def\endChapter{%
	\protected@write \@auxout {}{\string \last@page{\thisChap}{\thepage}}%
%	\protected@write \@auxout {}{^^J}%
}
\def\endChapter{%  EXPT 20 April 2014
	\relax
	%\protected@write \@auxout {}{\string \last@page{\thisChap}{\thepage}}%
%	\protected@write \@auxout {}{^^J}%
}
\let\endAppendix=\endChapter


%\makeatletter
\newcommand{\TOC}{ \@starttoc{toc}}
%\makeatother

\newcommand\TableofContents{%
\bgroup
\pagestyle{roman}%
\tableofcontents
\vskip1in
\ifDRAFT Printed: \today\fi
%\vfil\newpage
\egroup
}

%\newcommand{\oldmakechapter}{%
%%\showlabels
%\begin{document}%
%%\setcounter{page}{0}%		4 Mar 2014, causes empty first page?
%%\thispagestyle{startchap}%
%%\pagestyle{startchap}%
%%\TableofContents
%\TOC
%\setcounter{page}{0}%
%%\bigskip
%%Version: \version
%\ifDRAFT
%	\vskip1in
%	Printed: \today
%	\thispagestyle{startchap}%
%\fi
%\input chapter.tex
%\end{document}%
%}


\newcommand{\makechapter}{%
%\input dbp-hyperref.sty
\DRAFTtrue
\getGLOBALS
\showlabels

\begin{document}%
\LOCALlabels

\TOC
\setcounter{page}{0}%
\ifDRAFT
	\vskip1in
	Printed: \today
	\thispagestyle{startchap}%
\fi
\input chapter.tex
\end{document}%
}


\newcommand{\Preface}[1]{%
%	\pagestyle{roman}%
	\pagestyle{preface}%
	\thispagestyle{startchap}%
	\noindent{\Huge \textbf {Preface #1}\hfil}\par% 
	\bigskip
	\addcontentsline{toc}{chapter}{\qquad Preface #1}%
}
\makeatother


\renewcommand{\thechapter}{\arabic{chapter}}

\titleformat{\chapter}[display]
{\bfseries\Huge}
%{\filright{\large\bfseries\textsc{\chaptertitlename~\Large\thechapter}}}
{\filright{\large\textsc{Chapter}~\Large\thechapter}}
{0.5ex}
{%\titlerule
%\vspace{2ex}%
\filright}
[%\vspace{2ex}%
%\titlerule
]
