\ProvidesPackage{tdclock} [2014/02/21 v2.5 clock accessory]

%
%  Luis Randez  randez@unizar.es
%  Juan I. Montijano  monti@unizar.es
%
% This program can be redistributed and/or modified under the terms
% of the GNU Public License, version 2.


\RequirePackage{hyperref}
\RequirePackage{xcolor}[2004/05/09]
\RequirePackage{xkeyval}


\ifdefined\clock@font\else\def\clock@font{"font.Helv"}\fi
\ifdefined\clock@interval\else\def\clock@interval{29}\fi
\ifdefined\clock@duration\else\def\clock@duration{6000.0}\fi
\ifdefined\clock@timewarningfirst\else\def\clock@timewarningfirst{90}\fi
\ifdefined\clock@timewarningsecond\else\def\clock@timewarningsecond{95}\fi
\ifdefined\clock@death\else\def\clock@death{0}\fi
\ifdefined\clock@colorwarningfirst\else\def\clock@colorwarningfirst{orange}\fi
\ifdefined\clock@colorwarningsecond\else\def\clock@colorwarningsecond{red}\fi
\ifdefined\clock@fillcolorwarningfirst\else\def\clock@fillcolorwarningfirst{"T"}\fi
\ifdefined\clock@fillcolorwarningsecond\else\def\clock@fillcolorwarningsecond{"T"}\fi
%
\ifdefined\clock@fontsize\else\newdimen\clock@fontsize\clock@fontsize=\f@size pt\fi
\ifdefined\clock@height\else\newdimen\clock@height\fi
%\ifdefined\clock@fontcolor\else\newcommand{\clock@fontcolor}{0 0 0}\fi
%
\ifdefined\clock@resetatpages\else\def\clock@resetatpages{none}\fi
\ifdefined\clock@pdfpagetype\else\def\clock@pdfpagetype{pdfpagelabels}\fi

\DeclareOptionX{font}{\def\clock@font{"font.#1"}}
\DeclareOptionX{timeinterval}{\renewcommand{\clock@interval}{#1}}
\DeclareOptionX{timeduration}{\renewcommand{\clock@duration}{#1}}
\DeclareOptionX{timewarningfirst}{\renewcommand{\clock@timewarningfirst}{#1}}
\DeclareOptionX{timewarningsecond}{\renewcommand{\clock@timewarningsecond}{#1}}
\DeclareOptionX{timedeath}{\renewcommand{\clock@death}{#1}}
\DeclareOptionX{colorwarningfirst}{\renewcommand{\clock@colorwarningfirst}{#1}}
\DeclareOptionX{colorwarningsecond}{\renewcommand{\clock@colorwarningsecond}{#1}}
\DeclareOptionX{fillcolorwarningfirst}{\renewcommand{\clock@fillcolorwarningfirst}{#1}}
\DeclareOptionX{fillcolorwarningsecond}{\renewcommand{\clock@fillcolorwarningsecond}{#1}}
\DeclareOptionX{resetatpages}{\renewcommand{\clock@resetatpages}{#1}}
\DeclareOptionX{pageresetcontrol}{\renewcommand{\clock@pdfpagetype}{#1}}

%

\ProcessOptionsX

%\def\resetatthispage{\def\clock@resetatpages{\clock@resetatpages,\HyPL@thisLabel}}

\def\a@a{1.0}
\def\b@b{1.080}
\def\a@one{1.1}
\def\b@one{0.3}
\def\a@h{0.1}
\def\b@h{0.1}

\def\@Helv{"font.Helv"}
\def\@HelvB{"font.HelvB"}
\def\@HelvI{"font.HelvI"}
\def\@HelvBI{"font.HelvBI"}
\def\@Times{"font.Times"}
\def\@TimesB{"font.TimesB"}
\def\@TimesI{"font.TimesI"}
\def\@TimesBI{"font.TimesBI"}
\def\@Cour{"font.Cour"}
\def\@CourB{"font.CourB"}
\def\@CourI{"font.CourI"}
\def\@CourBI{"font.CourBI"}
\ifx\clock@font\@Helv \def\@hfac{0.9}\def\a@a{1.1}\def\b@b{1.080}\def\a@one{1.1}\def\b@one{0.300}\def\a@h{0.10}\def\b@h{0.10}\fi
\ifx\clock@font\@HelvB \def\@hfac{0.9}\def\a@a{1.1}\def\b@b{1.080}\def\a@one{1.1}\def\b@one{0.300}\def\a@h{0.10}\def\b@h{0.10}\fi
\ifx\clock@font\@HelvI \def\@hfac{0.9}\def\a@a{1.1}\def\b@b{1.080}\def\a@one{1.1}\def\b@one{0.300}\def\a@h{0.10}\def\b@h{0.10}\fi
\ifx\clock@font\@HelvBI \def\@hfac{0.9}\def\a@a{1.1}\def\b@b{1.080}\def\a@one{1.1}\def\b@one{0.300}\def\a@h{0.10}\def\b@h{0.10}\fi
\ifx\clock@font\@Times \def\@hfac{0.9}\def\a@a{1.0}\def\b@b{1.000}\def\a@one{1.10}\def\b@one{0.280}\def\a@h{0.10}\def\b@h{0.10}\fi
\ifx\clock@font\@TimesB \def\@hfac{0.9}\def\a@a{1.0}\def\b@b{1.000}\def\a@one{1.10}\def\b@one{0.280}\def\a@h{0.10}\def\b@h{0.10}\fi
\ifx\clock@font\@TimesI \def\@hfac{0.9}\def\a@a{1.0}\def\b@b{1.000}\def\a@one{1.10}\def\b@one{0.280}\def\a@h{0.10}\def\b@h{0.10}\fi
\ifx\clock@font\@TimesBI \def\@hfac{0.9}\def\a@a{1.0}\def\b@b{1.000}\def\a@one{1.10}\def\b@one{0.280}\def\a@h{0.10}\def\b@h{0.10}\fi
\ifx\clock@font\@Cour \def\@hfac{0.8}\def\a@a{1.0}\def\b@b{1.200}\def\a@one{1.1}\def\b@one{0.590}\def\a@h{0.04}\def\b@h{0.07}\fi
\ifx\clock@font\@CourB \def\@hfac{0.8}\def\a@a{1.0}\def\b@b{1.200}\def\a@one{1.1}\def\b@one{0.590}\def\a@h{0.04}\def\b@h{0.07}\fi
\ifx\clock@font\@CourI \def\@hfac{0.8}\def\a@a{1.0}\def\b@b{1.200}\def\a@one{1.1}\def\b@one{0.590}\def\a@h{0.04}\def\b@h{0.07}\fi
\ifx\clock@font\@CourBI \def\@hfac{0.8}\def\a@a{1.0}\def\b@b{1.200}\def\a@one{1.1}\def\b@one{0.590}\def\a@h{0.04}\def\b@h{0.07}\fi

\def\clock@temp#1{\def\clock@tempa##1,##2,##3\@nil{##1 ##2 ##3}%
\extractcolorspec{#1}\clock@tempb\expandafter\convertcolorspec\clock@tempb{rgb}%
\clock@tempb\edef\clock@tempa{\expandafter\clock@tempa\clock@tempb\@nil}}

\def\fac@font{1.0}
\def\factorclockfont#1{\def\fac@font{#1}}

\def\clock@setfonsize{\clock@fontsize=\f@size pt\clock@fontsize=\fac@font\clock@fontsize}
\def\clock@setfontcolor{\clock@temp{.}\let\clock@fontcolor=\clock@tempa}
\def\clock@setheight{\clock@height=\@hfac\clock@fontsize}

\def\clock@tempf#1{\def\clock@tempaf##1,##2,##3\@nil{##1 ##2 ##3}%
\extractcolorspec{#1}\clock@tempbf\expandafter\convertcolorspec\clock@tempbf{rgb}%
\clock@tempbf\edef\clock@tempaf{\expandafter\clock@tempaf\clock@tempbf\@nil}}

\def\clock@tempff#1{\def\clock@tempaff##1,##2,##3\@nil{##1 ##2 ##3}%
\extractcolorspec{#1}\clock@tempbff\expandafter\convertcolorspec\clock@tempbff{rgb}%
\clock@tempbff\edef\clock@tempaff{\expandafter\clock@tempaff\clock@tempbff\@nil}}

\def\clock@tempfff#1{\def\clock@tempafff##1,##2,##3\@nil{##1 ##2 ##3}%
\extractcolorspec{#1}\clock@tempbfff\expandafter\convertcolorspec\clock@tempbfff{rgb}%
\clock@tempbfff\edef\clock@tempafff{\expandafter\clock@tempafff\clock@tempbfff\@nil}}

\def\@Transp{"T"}
\clock@temp{\clock@colorwarningfirst}\def\colorninety{["RGB",\clock@tempb]}
\clock@tempf{\clock@colorwarningsecond}\def\colorninetyfive{["RGB",\clock@tempbf]}
\ifx\clock@fillcolorwarningfirst\@Transp\def\fillcolorninety{["T"]}%
\else\clock@tempff{\clock@fillcolorwarningfirst}\def\fillcolorninety{["RGB",\clock@tempbff]}\fi
\ifx\clock@fillcolorwarningsecond\@Transp\def\fillcolorninetyfive{["T"]}%
\else\clock@tempfff{\clock@fillcolorwarningsecond}\def\fillcolorninetyfive{["RGB",\clock@tempbfff]}\fi

%
\def\toggleclock#1{\PushButton[name=button1, onclick={\auxiliar;}]{#1}}
\def\resetcrono#1{\PushButton[name=button2, onclick={\resetclock;}]{{#1}}}

\def\dateseparator{\pdfslash}
\def\timeseparator{\pdfcolon}
%
\def\tdclock{\tddate\ \ \tdtime}
\def\tddate{\tdday\dateseparator\tdmonth\dateseparator\tdyear}
\def\tdtime{\tdwarningbox{\tdhours\timeseparator\tdminutes\timeseparator\tdseconds}}
\def\crono{\tdwarningbox{\cronohours\timeseparator\cronominutes\timeseparator\cronoseconds}}
%\def\crono{\cronohours\timeseparator\cronominutes\timeseparator\cronoseconds}
%\def\tdtime{\tdhours\timeseparator\tdminutes\timeseparator\tdseconds}
%
\def\mm{%
\def\tdtime{\tdwarningbox{\tdminutes}}%
\def\crono{\tdwarningbox{\cronominutes}}%
}
%
\def\hhmm{%
\def\tdtime{\tdwarningbox{\tdhours\timeseparator\tdminutes}}%
\def\crono{\tdwarningbox{\cronohours\timeseparator\cronominutes}}%
}
%
\def\hhmmss{%
\def\tdtime{\tdwarningbox{\tdhours\timeseparator\tdminutes\timeseparator\tdseconds}}%
\def\crono{\tdwarningbox{\cronohours\timeseparator\cronominutes\timeseparator\cronoseconds}}%
}

\def\mmddyyyy{%
\def\tddate{\tdmonth\dateseparator\tdday\dateseparator\tdyear}%
}
\def\ddmmyyyy{%
\def\tddate{\tdday\dateseparator\tdmonth\dateseparator\tdyear}%
}

\def\tdday{\clockfield{day}}
\def\tdmonth{\clockfield{month}}
\def\tdhours{\clockfield{hours}}
\def\tdminutes{\clockfield{minutes}}
\def\tdseconds{\clockfield{seconds}}
\def\cronohours{\clockfield{cronohours}}
\def\cronominutes{\clockfield{cronominutes}}
\def\cronoseconds{\clockfield{crseconds}}

\newbox\sizebox

\def\tdyear{%
\clock@setfonsize\clock@setheight\clock@setfontcolor%\clock@setwidth
\setbox\sizebox=\hbox{\TextField[readonly=true,name=year, width=60pt, height=\clock@height, align = 0, color =\clock@fontcolor, charsize = \clock@fontsize]{}}%
\vrule width 0pt height 2pt\kern-1pt\kern-\wd\sizebox\kern 60pt{\raisebox{-\b@h\clock@fontsize}{\raisebox{-\a@h pt}{\mbox{\TextField[name=year, width=60pt, height=\clock@height, align = 0, color =\clock@fontcolor, charsize = \clock@fontsize,readonly=true,
value={}]{}\kern-60pt\kern \a@a pt\kern \b@b\clock@fontsize\kern \b@b\clock@fontsize\vrule width 0pt height 2pt}}}}%
}

\def\pdfslash{%
\clock@setfonsize\clock@setheight\clock@setfontcolor%\clock@setwidth
\setbox\sizebox=\hbox{\TextField[readonly=true, width=60pt, height=\clock@height, align = 0, color =\clock@fontcolor, charsize = \clock@fontsize,value=/]{}}%
\vrule width 0pt height 2pt\kern-0.75pt\kern-\wd\sizebox\kern 60pt{\raisebox{-\b@h\clock@fontsize}{\raisebox{-\a@h pt}{\mbox{\TextField[name=separatordate, width=60pt, height=\clock@height, align = 0, color =\clock@fontcolor, charsize = \clock@fontsize,
readonly=true,value=/]{}\kern-60pt\kern \a@one pt\kern \b@one\clock@fontsize\vrule width 0pt height 2pt}}}}%
}

\def\pdfcolon{%
\clock@setfonsize\clock@setheight\clock@setfontcolor%\clock@setwidth
\setbox\sizebox=\hbox{\TextField[readonly=true, name=separatortime, width=0.90\clock@fontsize, height=\clock@height, align = 0, color =\clock@fontcolor, charsize = \clock@fontsize,value=:]{}}%
\vrule width 0pt height 2pt\kern-0.4pt\kern-\wd\sizebox\kern 0.90\clock@fontsize{\raisebox{-\b@h\clock@fontsize}{\raisebox{-\a@h pt}%
{\mbox{\TextField[name=separatortime, width=0.90\clock@fontsize, height=\clock@height, align = 0, color =\clock@fontcolor, charsize = \clock@fontsize,
readonly=true,value=:]{}\kern-0.90\clock@fontsize\kern\a@one pt\kern \b@one\clock@fontsize\vrule width 0pt height 2pt}}}}%
}

\def\clockfield#1{%
\clock@setfonsize\clock@setheight\clock@setfontcolor%\clock@setwidth
\setbox\sizebox=\hbox{\TextField[readonly=true, name=#1, width=1.31\clock@fontsize, height=\clock@height, align = 0, color =\clock@fontcolor, charsize = \clock@fontsize]{}}%
\vrule width 0pt height 2pt\kern-\a@a pt\kern-\wd\sizebox\kern 1.31\clock@fontsize{\raisebox{-\b@h\clock@fontsize}%
{\raisebox{-\a@h pt}{\mbox{\TextField[name=#1, width=1.31\clock@fontsize, height=\clock@height, align = 0, color =\clock@fontcolor, charsize = \clock@fontsize,readonly=true,
value={}]{}\kern-1.31\clock@fontsize\kern\a@a pt\kern \b@b\clock@fontsize\vrule width 0pt height 2pt}}}}%
}

\def\tdwarningbox#1{%
\clock@setfonsize\clock@setheight\clock@setfontcolor%
\setbox\sizebox=\hbox{#1}%
\vrule width 0pt height 2pt\kern-2pt\kern-\a@a pt\raisebox{-\dp\sizebox}{\raisebox{-\a@h pt}%
{\mbox{\TextField[readonly=true, name=cronobox, width=1.01\wd\sizebox, height=1.08\ht\sizebox, align = 0, readonly=true,value=  ]{}}}}%
\vrule width 0pt height 2pt\kern0.1pt\kern-1.01\wd\sizebox\vrule width 0pt height 2pt#1\kern 1pt}

\def\initfields{%
\begin{Form}
\mbox{\TextField[format=\startclock,name=resultado2, hidden=true, width=0truecm, height=0truecm, bordercolor= 1 1 1, backgroundcolor= 1 1 1,value={}]{}}
\end{Form}%
%\TextField[name=resultado1, hidden=true, width=0pt, height=0pt,charsize=0pt]{}
\TextField[readonly=true,name=hours, hidden=true, width=0pt, height=0pt,charsize=0pt]{}%
\TextField[readonly=true,name=minutes, hidden=true, width=0pt, height=0pt,charsize=0pt]{}%
\TextField[readonly=true,name=seconds, hidden=true, width=0pt, height=0pt,charsize=0pt]{}%
\TextField[readonly=true,name=cronohours, hidden=true, width=0pt, height=0pt,charsize=0pt]{}%
\TextField[readonly=true,name=cronominutes, hidden=true, width=0pt, height=0pt,charsize=0pt]{}%
\TextField[readonly=true,name=crseconds, hidden=true, width=0pt, height=0pt,charsize=0pt]{}%
\TextField[readonly=true,name=day, hidden=true, width=0pt, height=0pt,charsize=0pt]{}%
\TextField[readonly=true,name=month, hidden=true, width=0pt, height=0pt,charsize=0pt]{}%
\TextField[readonly=true,name=year, hidden=true, width=0pt, height=0pt,charsize=0pt]{}%
\PushButton[name=button1, onclick={\auxiliar;},hidden=true]{}%
\PushButton[name=button2, onclick={\resetclock;},hidden=true]{}%
\TextField[readonly=true,name=separatordate, hidden=true, width=0pt, height=0pt,charsize=0pt, value=/]{}%
\TextField[readonly=true,name=separatortime, hidden=true, width=0pt, height=0pt,charsize=0pt, value=:]{}%
\TextField[readonly=true,name=cronobox, hidden=true, width=0pt, height=0pt,charsize=0pt, value= ]{}%
}
%
\def\initclock{%
\setbox\sizebox=\hbox{\initfields}%
\initfields%
\kern -\wd\sizebox%
\unskip
}
%
\def\resetclock{%
fecha1 = rresett();
}
%
\def\auxiliar{%
var iop= -iop;
}%
%
\def\startclock{%
%
%global.page;
%global.run;
%
var iop=1;
var fuente=eval(\clock@font);
var duration=eval(\clock@duration)*60;
var firstwarning=eval(\clock@timewarningfirst)/100;
var secondwarning=eval(\clock@timewarningsecond)/100;
var death=eval(\clock@death);
%
var page=0;
var all="all";
var none="none";
var resetatpages=[\clock@resetatpages];
%
fecha1 = rresett();
fecha1 = reloj();
run = app.setInterval("reloj();",1000*\clock@interval);
%run= app.setTimeOut("reloj();",\clock@interval020);
% app.clearTimeOut(global.run);
%run= app.setTimeOut("reloj();",\clock@interval020);
%fecha1=b();
%function b() {
%  run = app.setTimeOut("reloj();app.clearTimeOut(run);b()",\clock@interval000);
%}
%
if (resetatpages[0] !="none")
{
   run2 =app.setInterval("pagina(this);",1000);
}
%
function rresett()
{
var fObj0 = new Date();
horas0 = fObj0.getHours();
minutos0 = fObj0.getMinutes();
segundos0 = fObj0.getSeconds();
}
%
function alerta(kolor,kolorfill)
{
this.getField("minutes").textColor=kolor;
this.getField("hours").textColor=kolor;
this.getField("seconds").textColor=kolor;
this.getField("cronominutes").textColor=kolor;
this.getField("cronohours").textColor=kolor;
this.getField("crseconds").textColor=kolor;
this.getField("separatortime").textColor=kolor;
this.getField("cronobox").fillColor=kolorfill;
}
%
%
function pagina(doc)
{
   var pdfpagelabels=0;
   var pdfpagenumbers=1;
  if (\clock@pdfpagetype==0)
  {
    labelold=this.getPageLabel(page);
    labelnew=this.getPageLabel(this.pageNum);
  }
   else
  {
    labelold=page;
    labelnew=this.pageNum;
  }
  if ( labelold != labelnew)
  {
    resethere=0;
    page=this.pageNum;
    for (var j=0;j<resetatpages.length;j++)
    {
      if (resetatpages[j] ==  labelnew || resetatpages[j] == "all")
      {
          resethere=1;
      }
    }
    if (resethere == 1)
    {
       rresett();
    }
  }
}
%
%
function reloj()
{
%
var fObj     = new Date();
var dia      = formateo(fObj.getDate());
var mes      = formateo(fObj.getMonth()+1);
var year     = fObj.getFullYear();
var horas    = fObj.getHours(); horas1 = horas;
var minutos  = fObj.getMinutes() ; minutos1 = minutos;
var segundos = fObj.getSeconds(); segundos1= segundos;
%
horas    = formateo(horas);
minutos  = formateo(minutos);
segundos = formateo(segundos);
%
%resta = (eval(horas1-global.horas0)*60+eval(minutos1-global.minutos0))*60+eval(segundos1-global.segundos0);
resta = (eval(horas1-horas0)*60+eval(minutos1-minutos0))*60+eval(segundos1-segundos0);
h = Math.floor(resta/3600);        htexto=formateo(h);
m = Math.floor((resta-3600*h)/60); mtexto=formateo(m);
s = resta-3600*h-60*m;             stexto=formateo(s);
%
%
%
var cociente = eval(resta/duration);
if ( cociente < firstwarning) {
this.getField("minutes").fillColor=["T"];
this.getField("hours").fillColor=["T"];
this.getField("seconds").fillColor=["T"];
this.getField("cronominutes").fillColor=["T"];
this.getField("cronohours").fillColor=["T"];
this.getField("crseconds").fillColor=["T"];
this.getField("separatortime").fillColor=["T"];
this.getField("cronobox").fillColor=["T"];
}
if ( cociente >= firstwarning) {
  alerta(\colorninety,\fillcolorninety);
  if ( cociente >= secondwarning) {alerta(\colorninetyfive,\fillcolorninetyfive);}
}
if( (death == 1) && ( resta > duration*1.10 )) { closeDoc();}
%
%console.println("aaa " + fuente+ " " + duration);
if (iop == 1)
{fecha1= dia+"/"+mes+"/"+year+ "  "+horas + ":"+minutos+":"+segundos;
casio1=horas;casio2=minutos;casio3=segundos;}
else
{fecha1=dia+"/"+mes+"/"+year+ "  "+htexto+":"+mtexto+":"+stexto;
casio1=htexto;casio2=mtexto;casio3=stexto;}
%this.getField("resultado1").value=fecha1;
%this.getField("resultado1").textFont=fuente;
%this.getField("resultado1").fillColor=["T"];
%this.getField("resultado1").strokeColor=["T"];
%
this.getField("hours").value=casio1;
this.getField("hours").textFont=fuente;
%this.getField("hours").fillColor=["T"];
this.getField("hours").strokeColor=["T"];
%
this.getField("minutes").value=casio2;
this.getField("minutes").textFont=fuente;
%this.getField("minutes").fillColor=["T"];
this.getField("minutes").strokeColor=["T"];
%
this.getField("seconds").value=casio3;
this.getField("seconds").textFont=fuente;
%this.getField("seconds").fillColor=["T"];
this.getField("seconds").strokeColor=["T"];
casio4=htexto;
this.getField("cronohours").value=casio4;
this.getField("cronohours").textFont=fuente;
%this.getField("cronohours").fillColor=["T"];
this.getField("cronohours").strokeColor=["T"];
casio5=mtexto;
this.getField("cronominutes").value=casio5;
this.getField("cronominutes").textFont=fuente;
%this.getField("cronominutes").fillColor=["T"];
this.getField("cronominutes").strokeColor=["T"];
casio6=stexto;
this.getField("crseconds").value=casio6;
this.getField("crseconds").textFont=fuente;
%this.getField("crseconds").fillColor=["T"];
this.getField("crseconds").strokeColor=["T"];
casio7=dia;
this.getField("day").value=casio7;
this.getField("day").textFont=fuente;
this.getField("day").fillColor=["T"];
this.getField("day").strokeColor=["T"];
casio8=mes;
this.getField("month").value=casio8;
this.getField("month").textFont=fuente;
this.getField("month").fillColor=["T"];
this.getField("month").strokeColor=["T"];
casio9=year;
this.getField("year").value=casio9;
this.getField("year").textFont=fuente;
this.getField("year").fillColor=["T"];
this.getField("year").strokeColor=["T"];
this.getField("separatordate").textFont=fuente;
this.getField("separatordate").fillColor=["T"];
this.getField("separatordate").strokeColor=["T"];
this.getField("separatortime").textFont=fuente;
%this.getField("separatortime").fillColor=["T"];
this.getField("separatortime").strokeColor=["T"];
this.getField("cronobox").strokeColor=["T"];
%this.getField("button1").textFont=fuente;
this.getField("button1").fillColor=["T"];
this.getField("button1").strokeColor=["T"];
%this.getField("button2").textFont=fuente;
this.getField("button2").fillColor=["T"];
this.getField("button2").strokeColor=["T"];
}
%
function formateo(uin)
{
if (uin <= 9) uin = "0" + uin;
return uin;
}
}

%\initclock
%
\endinput