
/**
 *  A collection of Javascript utilities.
 *  Copyright © 2006 God N Locomotive Works
 *    All Rights Reserved
 */
var org;
if(!org){
  org={};
} else if(typeof org != "object"){
  throw new Error("org already exists and is not an object!");
}
if(!org.pauahtun){
  org.pauahtun={};
} else if(typeof org.pauahtun != "object"){
  throw new Error("org.pauahtun already exists and is not an object!");
}
if(org.pauahtun.GodN){
  // throw new Error("org.pauahtun.GodN already exists!");
} else {
  org.pauahtun.GodN={
    define:function (s){
      if(s==undefined){
        document.write("<p>I am the define function().<br>\n");
      }else{
        document.write("<p>"+s+"<br>\n");
      }
    },
    isNav:false,
    isIE:false,
    // imageSource:"http://www.pauahtun.org/",
    imageSource:"/",
    browserType:function(){
      if (parseInt(navigator.appVersion)>=4) {
        if(navigator.appName=="Netscape") {
          org.pauahtun.GodN.isNav=true;
        } else {
          org.pauahtun.GodN.isIE=true;
        }
      }
    },
    newMail:function(display){
      sps=display.split(' at ');
      if(sps.length<2){
        sps=display.split('@');
      }
      if(sps.length<2){
        document.write(display);
        return
      }
      name=sps[0].replace(' ','');
      domain=sps[1].replace(' ','');
      document.write('<a href=mailto:'+name+'@'+domain+'>'+display+'</a>');
    },
    safemail:function(name,domain,display){
      name=name.replace(' ','');
      domain=domain.replace(' ','');
      displayed=(typeof(display)=="undefined")?name+"@"+domain:display
      document.write('<a href=mailto:'+name+'@'+domain+'>'+displayed+'</a>');
    },
    mayaDate:function(){
      this.lastmod=document.lastModified;
      this.lastmoddate=Date.parse(this.lastmod);
      this.modDate=(this.lastmoddate==0 ? new Date() : new Date(this.lastmoddate));
      this.lastYear=this.modDate.getFullYear();
      this.modSeconds=Date.parse(document.lastModified);
      this.modSeconds=this.modSeconds/1000.0;
      this.mayaday=Math.round(this.modSeconds);
      this.mayaday/=86400;
      this.mayaday+=2440588;
      this.mayaday=Math.floor(this.mayaday);
      this.mayaday-=584285;
      this.longcount=new Array(5);
      this.longcount[0]=Math.floor(this.mayaday/144000);
      tmp=this.mayaday-(this.longcount[0]*144000);
      this.longcount[1]=Math.floor(tmp/7200);
      tmp=tmp-(this.longcount[1]*7200);
      this.longcount[2]=Math.floor(tmp/360);
      tmp=tmp-(this.longcount[2]*360);
      this.longcount[3]=Math.floor(tmp/20);
      tmp=tmp-(this.longcount[3]*20);
      this.longcount[4]=Math.floor(tmp);
      this.glord=((this.longcount[3]*2)+this.longcount[4])%9;
      if (this.glord==0) {
        this.glord=9;
      }
      this.haab=(this.mayaday+348)%365;
      if(this.haab<0) {
        this.haab=365+this.haab;
        this.haab%=365;
      }
      this.trecena=(this.mayaday+4)%13;
      if(this.trecena<0) {
        this.trecena=(13+this.trecena)
        this.trecena%-13;
      }
      if(this.trecena==0) {
        this.trecena=13;
      }
      this.veintena=this.mayaday%20; (this.mayaday+19)%20;
      if(this.veintena<0) {
        this.veintena=20+this.veintena;
        this.veintena%=20;
      }
      this.haabmonth=Math.floor(this.haab/20);
      this.haabday=this.haab-(this.haabmonth*20);
      this.fontcolor="";
      this.fontend="";
      this.st8=0;
      this.calRound=0;
      this.tzolkin=0;
      this.p260();
    }

  };
}
org.pauahtun.GodN.mayaDate.prototype.tzolkinDays=["'Ahaw","'Imix","'Ik'","Ak'bal","K'an","Chik'chan","Kimi",
    "Manik'","Lamat","Muluk","Ok","Chuwen","'Eb","Ben","'Ix",
    "Men","K'ib","Kaban","'Etz'nab","Kawak"
  ];
org.pauahtun.GodN.mayaDate.prototype.haabMonths=["Pohp","Wo","Sip","Sots","Sek","Xul","Yaxk'in","Mol",
    "Ch'en","Yax", "Sac","Keh", "Mak","K'ank'in", "Muwan","Pax",
    "K'ayab","Kumk'u", "Wayeb"
  ];
org.pauahtun.GodN.mayaDate.prototype.monthLengths=[
    31,28,31,30,31,30,31,31,30,31,30,31
  ];
org.pauahtun.GodN.mayaDate.prototype.validMonths={
  0:[0,5,10,15],
  1:[1,6,11,16],
  2:[2,7,12,17],
  3:[3,8,13,18],
  4:[4,9,14,19]
};
/**
 * Legal haab days are found by indexing into the list
 * using the veintena day.  For example, veintena day
 * 0 (Ahaw) can only occur on haab day 8, 13, 18, or 3.
 */
org.pauahtun.GodN.mayaDate.prototype._legal_veintena_haabdays=[
    [8,13,18, 3], // 'Ahaw
    [4, 9,14,19], // 'Imix
    [5,10,15, 0], // 'Ik'
    [6,11,16, 1], // Ak'bal
    [7,12,17, 2], // K'an
    [8,13,18, 3], // Chik'chan
    [4, 9,14,19], // Kimi
    [5,10,15, 0], // Manik'
    [6,11,16, 1], // Lamat
    [7,12,17, 2], // Muluk
    [8,13,18, 3], // Ok
    [4, 9,14,19], // Chuwen
    [5,10,15, 0], // 'Eb
    [6,11,16, 1], // Ben
    [7,12,17, 2], // 'Ix
    [8,13,18, 3], // Men
    [4, 9,14,19], // K'ib
    [5,10,15, 0], // Kaban
    [6,11,16, 1], // 'Etz'nab
    [7,12,17, 2]  // Kawak
  ];
/**
 * By the same token, you can index with a haab day
 * to determine legal veintena days.
 */
org.pauahtun.GodN.mayaDate.prototype._legal_haab_veintenadays=[
    [ 2, 7,12,17], // Haab day 0
    [ 3, 8,13,18], // Haab day 1
    [ 4, 9,14,19], // Haab day 2
    [ 5,10,15, 0], // Haab day 3
    [ 6,11,16, 1], // Haab day 4
    [ 7,12,17, 2], // Haab day 5
    [ 8,13,18, 3], // Haab day 6
    [ 9,14,19, 4], // Haab day 7
    [10,15, 0, 5], // Haab day 8
    [11,16, 1, 6], // Haab day 9
    [12,17, 2, 7], // Haab day 10
    [13,18, 3, 8], // Haab day 11
    [14,19, 4, 9], // Haab day 12
    [15, 0, 5,10], // Haab day 13
    [16, 1, 6,11], // Haab day 14
    [17, 2, 7,12], // Haab day 15
    [18, 3, 8,13], // Haab day 16
    [19, 4, 9,14], // Haab day 17
    [ 0, 5,10,15], // Haab day 18
    [ 1, 6,11,16]  // Haab day 19
  ];
                                             //    0   1   2   3   4   5   6   7   8   9  10 11
org.pauahtun.GodN.mayaDate.prototype._l819coefs=[360,648,675,396,549,333,108,522,612,774,738,18];
org.pauahtun.GodN.mayaDate.prototype.fontColor=function(clr){
  this.fontcolor="<font color="+clr+">";
  this.fontend="</font>";
}
org.pauahtun.GodN.mayaDate.prototype.toString=function(){
  return this.fontcolor+this.LC()+"&nbsp;  "+this.CR()+" ["+this.G()+"]"+this.fontend;
}
org.pauahtun.GodN.mayaDate.prototype.LC=function(){
  return this.longcount[0]+"."+this.longcount[1]+"."+this.longcount[2]+"."+this.longcount[3]+"."+this.longcount[4];
}
org.pauahtun.GodN.mayaDate.prototype.G=function(){
  return "G"+this.glord;
}
org.pauahtun.GodN.mayaDate.prototype.CR=function(){
  return this.trecena+" "+this.tzolkinDays[this.veintena]+" "+this.haabday+" "+this.haabMonths[this.haabmonth];
}
org.pauahtun.GodN.mayaDate.prototype.gregorian=function(){
  return this.modDate.toLocaleString();
}
org.pauahtun.GodN.mayaDate.prototype.getFullYear=function(){
  return this.modDate.getFullYear();
}
org.pauahtun.GodN.mayaDate.prototype.st819=function(){
  var l=this.longcount;
  l.reverse()
  var j=0;
  var c=1
  var e=3;        // 819 Day station for 0.0.0.0.0
  for(var i=0;i<l.length;i++){
    if(j==0){
        c=1;
    }else if(j==1){
        c=20;
    }else{
        c=this._l819coefs[(j-2)%12];
    }
    e=(e+(l[i]*c))%819;
    j=j+1;
  }
  l.reverse()
  this.st8=e;
  return e
}
org.pauahtun.GodN.mayaDate.prototype.st819lst=function(){
}
function integer(f){
  if(f>0.0){
    return Math.floor(f);
  }
  return Math.ceil(f);
}
org.pauahtun.GodN.mayaDate.prototype.julday=function(mm,id,iyyy){
  //"""Arguments:  month, day, year and dayofweek are all integers, and
  //are expected to be in the Gregorian calendar; percentofday is a float, and is optional.
  //Return value:  tuple (Julian day #.#, day of week)."""
  if(mm==undefined){
    mm=this.modDate.getMonth();
    id=this.modDate.getDate();
    iyyy=this.modDate.getFullYear();
  }
  mm=mm+1;
  if(mm>2){
    jy=iyyy;
    jm=mm+1;
  }else{
    jy=iyyy-1;
    jm=mm+13;
  }
  tm=0.0; // Time of day, decimal, for future use.
  jul=integer(Math.floor(365.25*jy)+Math.floor(30.6001*jm)+(id+1720995.0+tm));
  ja=integer(0.01*jy);
  jul=integer(jul+(2-ja+(integer(0.25*ja))));
  return jul;
}
org.pauahtun.GodN.mayaDate.prototype.pcalRound=function(){   //: # tzolkin = 0-259, haab = 0-364
    /*"""Arguments:  integer tzolkin, haab
    Return value:  integer position in the Calendar Round (ranging from 0 to 18979)
    from the tzolk'in tzolkin and the haab."""*/
    var c12=-16;
    var m1=260;
    var m2=73;
    var u1=this.tzolkin-156;
    if(u1<0){
      u1=u1+260;
    }
    var u2=this.haab%m2;
    var m=m1*m2;
    var v1=modulo(u1,m1);
    var v2=modulo(((u2-v1)*c12),m2);
    var u=((v2*m1)+v1);
    this.calRound=u;
    return u;
}
function modulo(x, y) {
  var k = x % y;
  return (k!=0&&(k>0^y>0)&&isFinite(y))?k+y:k;
}
org.pauahtun.GodN.p260=function(trecena,veintena){
    // """Arguments:  integer trecena, v
    // Return value:  integer representing the position in the tzolk'in (a number
    // ranging from 0 to 259) from the given values of the trecena T and
    // veintena v."""
    var c12=-3;
    var m1=13;
    var m2=20;
    var u1=trecena-1;
    var u2=veintena-1;
    if(u2<0){
      u2=19;
    }
    var m=m1*m2;
    var v1=modulo(u1,m1);
    var v2=modulo(((u2-v1)*c12),m2);
    var u=(v2*m1)+v1;
    return u;
}
org.pauahtun.GodN.mayaDate.prototype.p260=function(){
    // """Arguments:  integer trecena, v
    // Return value:  integer representing the position in the tzolk'in (a number
    // ranging from 0 to 259) from the given values of the trecena T and
    // veintena v."""
    var c12=-3;
    var m1=13;
    var m2=20;
    var u1=this.trecena-1;
    var u2=this.veintena-1;
    if(u2<0){
      u2=19;
    }
    var m=m1*m2;
    var v1=modulo(u1,m1);
    var v2=modulo(((u2-v1)*c12),m2);
    var u=(v2*m1)+v1;
    this.tzolkin=u;
    return u;
}
org.pauahtun.GodN.mayaDate.prototype.loun=function(){
  return modulo(((40*((this.trecena-1)-(this.veintena-1)))+(this.veintena-1)),260);
}
org.pauahtun.GodN.frameModified=function(firstYear,copyrighter,mailto,fTitle) {
  wmd=new org.pauahtun.GodN.mayaDate();
  lastYear=wmd.getFullYear();
  if(firstYear==undefined||firstYear=="") {
    firstYear=wmd.getFullYear();
  }
  if(copyrighter==undefined) {
    copyrighter="I. Van Laningham";
  }
  if(mailto==undefined) {
    mailto="ivanlan at pauahtun.org";
  }
  document.write("<p align=center><font size=1>");
  document.write((fTitle==undefined?"Contents of this frame":fTitle)+"—Revised:<br>"+wmd);
  document.write("<br>("+wmd.gregorian()+")\n<br>");
  if(firstYear!=-1) {
    document.write("Copyright &copy; ");
    if(firstYear==lastYear) {
      document.write(firstYear);
    } else {
      document.write(firstYear,"-",lastYear);
    }
  }
  document.write(" "+copyrighter+"<br>");
  if(firstYear!=-1) {
    document.write("All Rights Reserved<br>");
  }
  org.pauahtun.GodN.newMail(mailto);
  document.write("<br><font size=3></p>\n");
}
org.pauahtun.GodN.copyrighted=function(copyrighter,mailto){
  wmd=new org.pauahtun.GodN.mayaDate();
  firstYear=wmd.getFullYear();
  if(copyrighter==undefined) {
    copyrighter="listowner";
  }else{
    copyrighter=copyrighter.toLowerCase();
  }
  if(mailto==undefined){
    mailto="ivanlan at pauahtun.org";
  }else{
    mailto=mailto.toLowerCase();
  }
  document.write("<p align=center><font size=1>");
  document.write("URL of this page&mdash;Revised:<br>"+wmd.gregorian());
  document.write("<br>Copyright &copy; ");
  document.write(firstYear);
  document.write(" "+copyrighter+"<br>");
  document.write("All Rights Reserved<br>");
  org.pauahtun.GodN.newMail(mailto);
  document.write("<br></font></p>\n");
}
org.pauahtun.GodN.mayamodified=function(firstYear,copyrighter,mailto,altMailto,cls) {
  wmd=new org.pauahtun.GodN.mayaDate();
  lastYear=wmd.getFullYear();
  if(firstYear==undefined||firstYear=="") {
    firstYear=wmd.getFullYear();
  }
  if(copyrighter==undefined) {
    copyrighter="I. Van Laningham";
  }
  if(mailto==undefined && altMailto==undefined){
    mailto="ivanlan at pauahtun.org";
    altMailto="ivanlan9 at gmail.com";
  }else if(mailto==undefined) {
    mailto="ivanlan at pauahtun.org";
  }
  if(cls==undefined){
    document.write("<p align=center><font size=1>");
  }else{
    document.write("<p class="+cls+" align=center><font size=1>");
  }
  document.write("URL of this page&mdash;Revised:<br>"+wmd);
  document.write("<br>("+wmd.gregorian()+")\n<br>");
  if(firstYear!=-1) {
    document.write("Copyright &copy; ");
    if(firstYear==lastYear) {
      document.write(firstYear);
    } else {
      document.write(firstYear,"-",lastYear);
    }
  }
  document.write(" "+copyrighter+"<br>");
  if(firstYear!=-1) {
    document.write("All Rights Reserved<br>");
  }
  org.pauahtun.GodN.newMail(mailto);
  if(altMailto!=undefined){
    document.write(", ");
    org.pauahtun.GodN.newMail(altMailto);
  }
  document.write("<br><font size=3></p>\n");
}
org.pauahtun.GodN.additionalMaterials=function(cpr,y,str,em) {
  lastmod=document.lastModified;
  lastmoddate=Date.parse(lastmod);
  var modDate;
  if (lastmoddate == 0){
    modDate=new Date();
  } else {
    modDate=new Date(lastmoddate);
  }
  if((cpr==undefined)||(cpr=="")){
    return;
  }
  if((y==undefined)||(y=="")){
    y=modDate.getFullYear();
  }
  if((str==undefined)||(str=="")){
    str="Additional materials ";
  }
  document.write("<p align=center><font size=1>");
  document.write(str+"Copyright &copy; "+y);
  if(y<modDate.getFullYear()){
    document.write("-"+modDate.getFullYear());
  }
  document.write(" "+cpr+"<br>\n");
  document.write("All Rights Reserved<br>");
  if((em!=undefined)&&(em!="")){
    org.pauahtun.GodN.newMail(em);
    document.write("<br>");
  }
  document.write("<font size=3></p>\n");
}
org.pauahtun.GodN.oldNearestHaabDay=function(vi,hd,lim){
  if((vi>=0 && vi<20)&&(hd>=0 && hd<20)){
    var x=org.pauahtun.GodN.mayaDate.prototype._legal_veintena_haabdays[vi];
    var n=-1;
    for(var i=0;i<x.length;i++){
      n=x[i];
      if(lim==5){
        if(n<lim){
          return n;
        }
        continue;
      }
      if(hd>=n-2&&hd<n+2){
        return n;
      }
    }
    return n;
  }else if(vi<0){
    if(hd<lim){
      return hd;
    }
    return 1;
  }
  return -1;
}
org.pauahtun.GodN.nearestHaabDay=function(vi,hd,lim){
  var x=org.pauahtun.GodN.mayaDate.prototype._legal_veintena_haabdays[vi];
  var n=-1;
  for(var i=0;i<4;i++){
    n=x[i];
    if((hd>=n-2&&hd<n+2)){
      return i;
    }
  }
  return 0;
}
org.pauahtun.GodN.isLegalHaab=function(vi,hd){
  /* """Arguments:  integer vi, hd
  Return value:  1 if the haabday hd is legal for the specified
  veintena vi, 0 if it is not.""" */
  if((vi>=0 && vi<20)&&(hd>=0&&hd<20)){
    var x=org.pauahtun.GodN.mayaDate.prototype._legal_veintena_haabdays[vi];
    // document.write("_legal_ventena_haabdays["+vi+"] returned "+x+"<br>");
    for(var i=0;i<x.length;i++){
      if(hd==x[i]){
        return true;
      }
    }
  }
  return false;
}
org.pauahtun.GodN.isLegalVeintena=function(hd,vi){
  /** """Arguments:  integer hd, vi
  Return value:  1 if the veintena vi is legal for the specified
  haabday hd, 0 if it is not.""" */
  if((vi>=0&&vi<20)&&(hd>=0&&hd<20)){
    var x=org.pauahtun.GodN.mayaDate.prototype._legal_haab_veintenadays[hd];
    for(var i=0;i<x.length;i++){
      if(vi==x[i]){
        return true
      }
    }
  }
  return false;
}
org.pauahtun.GodN.veintenaDayCheck=function(){
  var hdMenu=document.getElementsByName("Haabday")[0];
  var hd=hdMenu.selectedIndex;
}
org.pauahtun.GodN.haabDayLite=function(suffix){
  if(suffix==undefined){
    suffix="";
  }
  var hdMenu=document.getElementsByName("Haabday"+suffix)[0];
  var hmMenu=document.getElementsByName("Haabmonth"+suffix)[0];
  var hm=org.pauahtun.GodN.mayaDate.prototype.haabMonths[hmMenu.selectedIndex];
  var hd=hdMenu.selectedIndex;
  haabDays=hdMenu.options;
  var dayLimit=20;
  if(hm=="Wayeb"){
    dayLimit=5;
  }
  for(var o=0;o<haabDays.length;o++){
    if(o<dayLimit){
      haabDays[o].disabled=false;
    }else{
      haabDays[o].disabled=true;
    }
  }
  var p=org.pauahtun.GodN.nearestHaabDay(-1,hd,dayLimit);
  hdMenu.selectedIndex=p;
}
org.pauahtun.GodN.oldHaabDayCheck=function(suffix){
  if(suffix==undefined){
    suffix="";
  }
  var vMenu=document.getElementsByName("Veintena"+suffix)[0];
  var vi=vMenu.selectedIndex;
  var hdMenu=document.getElementsByName("Haabday"+suffix)[0];
  var hmMenu=document.getElementsByName("Haabmonth"+suffix)[0];
  var hm=org.pauahtun.GodN.mayaDate.prototype.haabMonths[hmMenu.selectedIndex];
  var hd=hdMenu.selectedIndex;

  haabDays=hdMenu.options;
  var dayLimit=20;
  if(hm=="Wayeb"){
    dayLimit=5;
  }
  var db=0;
  var en=0;
  for(var o=0;o<haabDays.length;o++){
        haabDays[o].disabled=true;
        db++;
  }
  for(var o=0;o<haabDays.length;o++){
    if(org.pauahtun.GodN.isLegalHaab(vi,o)&&o<dayLimit){
        haabDays[o].disabled=false;
        en++;
    }
  }
  var p=org.pauahtun.GodN.nearestHaabDay(vi,hd,dayLimit);
  alert("vi "+vi+" hm "+hm+" hd "+hd+" dayLimit "+dayLimit+" p "+p+" enabled "+en+" disabled "+db);
  hdMenu.selectedIndex=p;
}
org.pauahtun.GodN.haabDayCheck=function(suffix){
  /**
   * Activated when user selects veintena day from Veintena menu.
   *  --Veintena not changeable, HaabMonth not changeable,
   *  --we can only change the haab day.
   */
  if(suffix==undefined){
    suffix="";
  }
  var vMenu=document.getElementsByName("Veintena"+suffix)[0];
  var vi=vMenu.selectedIndex;
  var hdMenu=document.getElementsByName("Haabday"+suffix)[0];
  var hmMenu=document.getElementsByName("Haabmonth"+suffix)[0];
  var hm=org.pauahtun.GodN.mayaDate.prototype.haabMonths[hmMenu.selectedIndex];
  var hdi=hdMenu.selectedIndex;
  haabDays=hdMenu.options;
  var hd=Number(haabDays[hdi].value);
  var dayLimit=20;
  if(hm=="Wayeb"){
    dayLimit=5;
  }
  if(hd>=dayLimit){
    hd=0;
  }
  haabDays.length=0; // Destroy the old options.
  for(var o=0;o<dayLimit;o++){
    if(org.pauahtun.GodN.isLegalHaab(vi,o)){
      var foo=new Option(""+o,""+o,false,false);
      haabDays[haabDays.length]=foo;
    }
  }
  var p=0;
  if(dayLimit==20){
    p=org.pauahtun.GodN.nearestHaabDay(vi,hd,dayLimit);
  }
  hdMenu.selectedIndex=p;
}

isleap=function(year){
  if((modulo(year,400))==0){
    return 29;
  }
  if((modulo(year,100))==0){
    return 28;
  }
  if((modulo(year,4))==0){
    return 29;
  }
  return 28;
}
org.pauahtun.GodN.oldGregDayCheck=function(suffix){
  if(suffix==undefined){
    suffix="";
  }
  var mMenu=document.getElementsByName("month"+suffix)[0];
  var m=mMenu.selectedIndex;
  var dMenu=document.getElementsByName("day"+suffix)[0];
  var d=dMenu.selectedIndex;
  var y=document.getElementsByName("year"+suffix)[0].value;
  var fLength=isleap(Number(y));
  days=dMenu.options;
  var lim=org.pauahtun.GodN.mayaDate.prototype.monthLengths[m];
  if(m==1){
    lim=fLength;
  }
  var da=0;
  var db=0;
  for(var o=0;o<days.length;o++){
    if(o<lim){
      days[o].disabled=false;
      da+=1;
    }else{
      days[o].disabled=true;
      db+=1;
    }
  }
  if(d>lim){
    dMenu.selectedIndex=lim-1;
  }
}
org.pauahtun.GodN.gregDayCheck=function(suffix){
  if(suffix==undefined){
    suffix="";
  }
  var mMenu=document.getElementsByName("month"+suffix)[0];
  var m=mMenu.selectedIndex;
  var dMenu=document.getElementsByName("day"+suffix)[0];
  var d=dMenu.selectedIndex;
  var y=document.getElementsByName("year"+suffix)[0].value;
  var fLength=isleap(Number(y));
  days=dMenu.options;
  var lim=org.pauahtun.GodN.mayaDate.prototype.monthLengths[m];
  if(m==1){
    lim=fLength;
  }
  days.length=0; // Destroy the old options.
  for(var o=1;o<=lim;o++){
    var foo=new Option(""+o,""+o,false,false);
    days[days.length]=foo;
  }
  if(d>lim){
    dMenu.selectedIndex=lim-1;
  } else {
    dMenu.selectedIndex=d;
  }
}
function isSignOrDigit(num) {
  if (num.length>1){
    return false;
  }
  var string="-+1234567890";
  if (string.indexOf(num)!=-1){
    return true;
  }
  return false;
}
org.pauahtun.GodN.floatAndCorrelationChecker=function(nam,cc){
  var txt=document.getElementsByName(nam)[0];
  var v=txt.value;
  var b=(parseFloat(v,10)==(v*1));
  if(b==false){
    alert("Text field "+nam+" is not a valid float. ["+v+"]");
    return false;
  }
  return org.pauahtun.GodN.yearChecker(cc);
}
org.pauahtun.GodN.yearChecker=function(nam){
  var txt=document.getElementsByName(nam)[0];
  var v=txt.value;
  for(var i=0;i<v.length;i++){
    c=v.charAt(i);
    if(!isSignOrDigit(c)){
      alert("Text field "+nam+" can only contain numbers. ["+v+"]");
      return false;
    }
  }
  var n=Number(v);
  if(n==NaN){
    alert("Value in text field "+nam+" is too large. ["+v+"]");
    return false;
  }
  return true;
}
org.pauahtun.GodN.scrollit_r2l=function(seed) {
  var msg="In the beginning, water covered the Earth "+
    "and all creatures lived in the underworld below.  "+
    "People could talk, animals could talk, rocks could talk.  "+
    "Everything could talk.  "+
    "—Jicarilla Apache Tradition "+
    " … … … … "+
    "Rocks talk here. ";
  var out=" ";
  var c=1;
  if (seed>100) {
    seed--;
    var cmd="org.pauahtun.GodN.scrollit_r2l("+seed+")";
    timerTwo=window.setTimeout(cmd,100);
  } else if (seed<=100&&seed>0) {
    for (c=0;c<seed;c++) {
      out+=" ";
    }
    out+=msg;
    seed--;
    var cmd="org.pauahtun.GodN.scrollit_r2l("+seed+")";
    window.status=out;
    timerTwo=window.setTimeout(cmd,100);
  } else if (seed<=0) {
    if (-seed<msg.length) {
      out+=msg.substring(-seed,msg.length);
      seed--;
      var cmd="org.pauahtun.GodN.scrollit_r2l("+seed+")";
      window.status=out;
      timerTwo=window.setTimeout(cmd,100);
    } else {
      window.status=" ";
      timerTwo=window.setTimeout("org.pauahtun.GodN.scrollit_r2l(100)",75);
    }
  }
}
if(org.pauahtun.Checker){
  // throw new Error("org.pauahtun.Checker already exists!");
} else {
  org.pauahtun.Checker={

    // BEGIN
    // Email Validation Javascript
    // copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

    // You have permission to copy and use this javascript provided that
    // the content of the script is not changed in any way.

    validateEmail:function (addr,man,db){
      if(addr==''&&man){
        if(db){
          alert('email address is mandatory');
        }
      return false;
      }
      var invalidChars='\/\'\\ ";:?!()[]\{\}^|';
      for(i=0;i<invalidChars.length;i++){
        if(addr.indexOf(invalidChars.charAt(i),0)> -1){
          if(db){
            alert('email address contains invalid characters');
          }
          return false;
        }
      }
      for(i=0;i<addr.length;i++){
        if(addr.charCodeAt(i)>127){
          if(db){
            alert("email address contains non ascii characters.");
          }
          return false;
        }
      }

      var atPos=addr.indexOf('@',0);
      if(atPos==-1){
        if(db){
          alert('email address must contain an @');
        }
        return false;
      }
      if(atPos==0){
        if(db){
          alert('email address must not start with @');
        }
        return false;
      }
      if(addr.indexOf('@',atPos+1)>-1){
        if(db){
          alert('email address must contain only one @');
        }
        return false;
      }
      if(addr.indexOf('.',atPos)==-1){
        if(db){
          alert('email address must contain a period in the domain name');
        }
        return false;
      }
      if(addr.indexOf('@.',0)!=-1){
        if (db){
          alert('period must not immediately follow @ in email address');
        }
        return false;
      }
      if(addr.indexOf('.@',0)!=-1){
        if(db){
          alert('period must not immediately precede @ in email address');
        }
        return false;
      }
      if(addr.indexOf('..',0)!=-1) {
        if(db){
          alert('two periods must not be adjacent in email address');
        }
        return false;
      }
      var suffix=addr.substring(addr.lastIndexOf('.')+1);
      if(suffix.length!=2&&suffix!='com'&&suffix!='net'&&suffix!='org'&&
        suffix!='edu'&&suffix!='int'&&suffix!='mil'&&suffix!='gov'&&
        suffix!='arpa'&&suffix!='biz'&&suffix!='aero'&&suffix!='name'&&
        suffix!='coop'&&suffix!='info'&&suffix!='pro'&&suffix!='museum'){
        if(db){
          alert('invalid primary domain in email address');
        }
        return false;
      }
      return true;
    }
    // END
  };
}
org.pauahtun.GodN.MM_swapImgRestore=function() {
  var i,x,a=document.MM_sr;
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++){
    x.src=x.oSrc;
  }
}
org.pauahtun.GodN.MM_preloadImages=function() {
  var d=document;
  if(d.images){
    if(!d.MM_p){
      d.MM_p=new Array();
    }
    var i,j=d.MM_p.length,a=org.pauahtun.GodN.MM_preloadImages.arguments;
    for(i=0; i<a.length; i++){
      if (a[i].indexOf("#")!=0){
        d.MM_p[j]=new Image;
        d.MM_p[j++].src=a[i];
      }
    }
  }
}
org.pauahtun.GodN.MM_findObj=function(n,d) {
  var p,i,x;
  if(!d){
    d=document;
  }
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;
    n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all){
    x=d.all[n];
  }
  for (i=0;!x&&i<d.forms.length;i++){
    x=d.forms[i][n];
  }
  for(i=0;!x&&d.layers&&i<d.layers.length;i++){
    x=org.pauahtun.GodN.MM_findObj(n,d.layers[i].document);
  }
  if(!x && document.getElementById){
    x=document.getElementById(n);
  }
  return x;
}
org.pauahtun.GodN.MM_swapImage=function() {
  var i,j=0,x,a=org.pauahtun.GodN.MM_swapImage.arguments;
  document.MM_sr=new Array;
  for(i=0;i<(a.length-2);i+=3){
    if ((x=org.pauahtun.GodN.MM_findObj(a[i]))!=null){
      document.MM_sr[j++]=x;
      if(!x.oSrc){
        x.oSrc=x.src;
      }
      x.src=a[i+2];
    }
  }
}
org.pauahtun.GodN.makeButton=function(backlink,backname,pressedImage,docTarget,normalImage,altText,noCat) {
  var bName=backlink;
  var p=bName.lastIndexOf("/");
  var pName=bName.substring(p);
  var theText=altText;
  if(noCat==undefined){
    theText=altText+"->"+pName;
  }
  document.write("<a href=\""+backlink+"\" \
  onmouseover=\"org.pauahtun.GodN.MM_swapImage('"+backname+"','','"+pressedImage+"',1)\" \
  onmouseout=\"org.pauahtun.GodN.MM_swapImgRestore()\" name=\""+docTarget+"\"><img src=\""+normalImage+
    "\" border=0 align=absmiddle name=\""+backname+"\" alt=\""+theText+"\" title=\""+theText+"\"></a>\n");
}

org.pauahtun.GodN.attack=function(){
  var e=document.getElementById("urgent");
  e.style.border="solid black 10px";
  e.style.padding="50px";
  var colors=["white","yellow","orange","red","blue","green"];
  var nextColor=0;
  setInterval(function(){
    e.style.borderColor=colors[nextColor++%colors.length];
  },500);
}
org.pauahtun.GodN.setImageSource=function(s){
  org.pauahtun.GodN.imageSource=s;
}
org.pauahtun.GodN.getImageSource=function(){
  return org.pauahtun.GodN.imageSource;
}
var trecenaImages;
var veintenaImages;
org.pauahtun.GodN.cacheImages=function(){
  if(document.images){
    trecenaImages=new Object();
    trecenaImages[0]=new Image(48,64);
    trecenaImages[0].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m13bc.gif";
    trecenaImages[1]=new Image(16,64);
    trecenaImages[1].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m1bc.gif";
    trecenaImages[2]=new Image(16,64);
    trecenaImages[2].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m2bc.gif";
    trecenaImages[3]=new Image(16,64);
    trecenaImages[3].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m3bc.gif";
    trecenaImages[4]=new Image(16,64);
    trecenaImages[4].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m4bc.gif";
    trecenaImages[5]=new Image(16,64);
    trecenaImages[5].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m5bc.gif";
    trecenaImages[6]=new Image(32,64);
    trecenaImages[6].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m6bc.gif";
    trecenaImages[7]=new Image(32,64);
    trecenaImages[7].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m7bc.gif";
    trecenaImages[8]=new Image(32,64);
    trecenaImages[8].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m8bc.gif";
    trecenaImages[9]=new Image(32,64);
    trecenaImages[9].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m9bc.gif";
    trecenaImages[10]=new Image(32,64);
    trecenaImages[10].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m10bc.gif";
    trecenaImages[11]=new Image(48,64);
    trecenaImages[11].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m11bc.gif";
    trecenaImages[12]=new Image(48,64);
    trecenaImages[12].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m12bc.gif";
    trecenaImages[13]=new Image(48,64);
    trecenaImages[13].src=org.pauahtun.GodN.getImageSource()+"MayaNumbers/m13bc.gif";
    trecenaImages.length=13;

    veintenaImages=new Object();
    veintenaImages[0]=new Image(64,64);
    veintenaImages[0].src=org.pauahtun.GodN.getImageSource()+"Days/ahau_m.gif";
    veintenaImages[1]=new Image(64,64);
    veintenaImages[1].src=org.pauahtun.GodN.getImageSource()+"Days/imix_m.gif";
    veintenaImages[2]=new Image(64,64);
    veintenaImages[2].src=org.pauahtun.GodN.getImageSource()+"Days/ik_m.gif";
    veintenaImages[3]=new Image(64,64);
    veintenaImages[3].src=org.pauahtun.GodN.getImageSource()+"Days/akbal_m.gif";
    veintenaImages[4]=new Image(64,64);
    veintenaImages[4].src=org.pauahtun.GodN.getImageSource()+"Days/kan_m.gif";
    veintenaImages[5]=new Image(64,64);
    veintenaImages[5].src=org.pauahtun.GodN.getImageSource()+"Days/chicchan_m.gif";
    veintenaImages[6]=new Image(64,64);
    veintenaImages[6].src=org.pauahtun.GodN.getImageSource()+"Days/cimi_m.gif";
    veintenaImages[7]=new Image(64,64);
    veintenaImages[7].src=org.pauahtun.GodN.getImageSource()+"Days/manik_m.gif";
    veintenaImages[8]=new Image(64,64);
    veintenaImages[8].src=org.pauahtun.GodN.getImageSource()+"Days/lamat_m.gif";
    veintenaImages[9]=new Image(64,64);
    veintenaImages[9].src=org.pauahtun.GodN.getImageSource()+"Days/muluc_m.gif";
    veintenaImages[10]=new Image(64,64);
    veintenaImages[10].src=org.pauahtun.GodN.getImageSource()+"Days/oc_m.gif";
    veintenaImages[11]=new Image(64,64);
    veintenaImages[11].src=org.pauahtun.GodN.getImageSource()+"Days/chuen_m.gif";
    veintenaImages[12]=new Image(64,64);
    veintenaImages[12].src=org.pauahtun.GodN.getImageSource()+"Days/eb_m.gif";
    veintenaImages[13]=new Image(64,64);
    veintenaImages[13].src=org.pauahtun.GodN.getImageSource()+"Days/ben_m.gif";
    veintenaImages[14]=new Image(64,64);
    veintenaImages[14].src=org.pauahtun.GodN.getImageSource()+"Days/ix_m.gif";
    veintenaImages[15]=new Image(64,64);
    veintenaImages[15].src=org.pauahtun.GodN.getImageSource()+"Days/men_m.gif";
    veintenaImages[16]=new Image(64,64);
    veintenaImages[16].src=org.pauahtun.GodN.getImageSource()+"Days/cib_m.gif";
    veintenaImages[17]=new Image(64,64);
    veintenaImages[17].src=org.pauahtun.GodN.getImageSource()+"Days/caban_m.gif";
    veintenaImages[18]=new Image(64,64);
    veintenaImages[18].src=org.pauahtun.GodN.getImageSource()+"Days/etznab_m.gif";
    veintenaImages[19]=new Image(64,64);
    veintenaImages[19].src=org.pauahtun.GodN.getImageSource()+"Days/cauac_m.gif";
    veintenaImages[20]=new Image(64,64);
    veintenaImages[20].src=org.pauahtun.GodN.getImageSource()+"Days/ahau_m.gif";
    veintenaImages.length=20;
  }
}
org.pauahtun.GodN.cacheImages();

/**
 * ImageLoop.js: An ImageLoop class for performing image animations
 *
 * Constructor Arguments: 
 *   imageIds:   array of ids of the <img> tags which will be animated
 *   fps:       the number of frames to display per second
 *   frameArrays: an array of objects containing Images with URLs, one for each frame of the animation
 *
 * Public Methods: 
 *   start():   start the animation (but wait for all frames to load first)
 *   stop():    stop the animation
 * 
 * Public Properties:
 *   loaded:    true if all frames of the animation have loaded,
 *              false otherwise
 */
/*
function countLoadedFrames() {
  if(!shown){
    shown=true;
  }
  var myloop=fnurgleloop;
  if(myloop==null||myloop==undefined){
    myloop=window.fnurgleloop;
  }
  if(myloop==null||myloop==undefined){
    alert("I still can't get it!");
  }
  fnurgleloop.loadedFrames++;
  if (fnurgleloop.loadedFrames == fnurgleloop.totalLength) {
    fnurgleloop.loaded = true;
    if (fnurgleloop.startOnLoad){
      fnurgleloop.start();
    }
  }
}
*/

function ImageLoop(imageIds, fps, frameArrays) {
    // Remember the image id. Don't look it up yet since this constructor
    // may be called before the document is loaded.
    this.name="ImageLoop";
    this.imageIds = imageIds;
    this.numberImages=this.imageIds.length;
    // Compute the time to wait between frames of the animation
    this.frameInterval = 1000/fps;
    // An array for holding Image objects for each frame
    this.frames = frameArrays;
    this.frameLengths=[];
    for(var i=0;i<frameArrays.length;i++){
      this.frameLengths[i]=this.frames[i].length;
    }

    this.image = [null,null];             // The <img> element, looked up by id
    this.loaded = false;           // Whether all frames have loaded
    this.loadedFrames = 0;         // How many frames have loaded
    this.startOnLoad = false;      // Start animating when done loading?
    this.frameNumber = 0;         // What frame is currently displayed
    this.vNumber=0;
    this.timer = null;             // The return value of setInterval()
    this.totalLength=0;
    var fnurgleloop;
    var shown=false;


    // This nested function is an event handler that counts how many 
    // frames have finished loading.  When all are loaded, it sets a flag,
    // and starts the animation if it has been requested to do so.
    fnurgleloop = this;
    window.fnurgleloop=fnurgleloop;
/*    function countLoadedFrames() {
      if(fnurgleloop==undefined||fnurgleloop==null||window.fnurgleloop==undefined||window.fnurgleloop==null){
        alert("bad loop "+fnurgleloop+" wfnurgleloop "+window.fnurgleloop);
      }else{
        alert("good loop "+fnurgleloop+" wfnurgleloop "+window.fnurgleloop);
      }
      window.fnurgleloop.loadedFrames++;
      if (window.fnurgleloop.loadedFrames == window.fnurgleloop.totalLength) {
          window.fnurgleloop.loaded = true;
          if (window.fnurgleloop.startOnLoad) window.fnurgleloop.start();
      }
    }
    this.countLoadedFrames=function(){
      fnurgleloop.loadedFrames++;
      if (fnurgleloop.loadedFrames == fnurgleloop.totalLength) {
        fnurgleloop.loaded = true;
        if (fnurgleloop.startOnLoad){
          window.fnurgleloop.start();
        }
      }
    };
    this.sayHello=function(){
      alert("Hello!");
    };
*/
    // Initialize the frames[] array and preload the images
    for(var j=0;j<frameArrays.length;j++){
      for(var k=0;k<this.frameLengths[j];k++) {
        // Register an event handler so we know when the frame is loaded
        // this.frames[j][k].onload=countLoadedFrames; // defined later
        //this.frames[j][k].name="George"+j+"+"+k;
        //this.frames[j][k].onload=this.countLoadedFrames;
        //if(j===0 && k===0){
        //  this.frames[j][k].onunload=this.sayHello;
        //}
        this.totalLength++;
        this.loadedFrames++;
      }
    }
    this.loaded=true;

    // Here we define a function that displays the next frame of the
    // animation.  This function can't be an ordinary instance method because
    // setInterval() can only invoke functions, not methods.  So we make
    // it a closure that includes a reference to the ImageLoop object
    this._displayNextFrame = function() {
        // First, increment the frame number. The modulo operator (%) means
        // that we loop from the last to the first frame
        fnurgleloop.frameNumber = (fnurgleloop.frameNumber+1)%fnurgleloop.frames[0].length;
        fnurgleloop.vNumber = (fnurgleloop.vNumber+1)%fnurgleloop.frames[1].length;
        // Update the src property of the image to the URL of the new frame
        fnurgleloop.image[0].src = fnurgleloop.frames[0][fnurgleloop.frameNumber].src;
        fnurgleloop.image[1].src = fnurgleloop.frames[1][fnurgleloop.vNumber].src;
        var xx=document.getElementById("trecenaNum").firstChild;
        xx.nodeValue=""+(fnurgleloop.frameNumber===0?13:fnurgleloop.frameNumber);
        var yy=document.getElementById("veintenaName").firstChild;
        yy.nodeValue=org.pauahtun.GodN.mayaDate.prototype.tzolkinDays[fnurgleloop.vNumber];
        var zz=document.getElementById("p260Num").firstChild;
        zz.nodeValue=org.pauahtun.GodN.p260(fnurgleloop.frameNumber,fnurgleloop.vNumber);
    };
}

/**
 * This method starts an ImageLoop animation.  If the frame images have not
 * finished loading, it instead sets a flag so that the animation will 
 * automatically be started when loading completes
 */
ImageLoop.prototype.start = function() {
    if (this.timer != null) return;   // Already started
    // If loading is not complete, set a flag to start when it is
    if (!this.loaded){
      this.startOnLoad = true;
    } else {
        // If we haven't looked up the image by id yet, do so now
        if (!this.image[0]) this.image[0] = document.getElementById(this.imageIds[0]);
        if (!this.image[1]) this.image[1] = document.getElementById(this.imageIds[1]);
        // Display the first frame immediately
        this._displayNextFrame();
        // And set a timer to display subsequent frames
        this.timer = setInterval(this._displayNextFrame, this.frameInterval);
    }
};

/** Stop an ImageLoop animation */
ImageLoop.prototype.stop = function() {
    if (this.timer) clearInterval(this.timer);
    this.timer = null;
};

org.pauahtun.GodN.Tzolkin=function(){
  var iids=["trecenas","veintenas"];
  var fArrays=[trecenaImages,veintenaImages];
  return new ImageLoop(iids,1,fArrays);
}
org.pauahtun.GodN.initBackground=function(img,bName){
  if(bName!=undefined){
    org.pauahtun.GodN.bgButtonName=bName;
  }else{
    org.pauahtun.GodN.bgButtonName="button";
  }
  document.body.hiddenBackground=undefined;
  document.body.background=img;
}
org.pauahtun.GodN.setBackground=function(){
  var buts=document.getElementsByName(org.pauahtun.GodN.bgButtonName);
  for(var i=0;i<buts.length;i++){
    var but=buts[i];
    but.value="Hide background image";
    but.onclick=org.pauahtun.GodN.hideBackground;
  }
  var im=document.body.background;
  document.body.background=document.body.hiddenBackground;
  document.body.hiddenBackground=im;
}
org.pauahtun.GodN.hideBackground=function(){
  var buts=document.getElementsByName(org.pauahtun.GodN.bgButtonName);
  for(var i=0;i<buts.length;i++){
    var but=buts[i];
    but.value="Show background image";
    but.onclick=org.pauahtun.GodN.setBackground;
  }
  var im=document.body.background;
  document.body.background=document.body.hiddenBackground;
  document.body.hiddenBackground=im;
}
