var nowdate = new Date();
var nowday = nowdate.getDate();
var nowmonth = nowdate.getMonth();
var nowyear = nowdate.getFullYear();
var highlight = 1;

function checkEnabled(m, y) {
  if (y != nowyear || m != nowmonth+1)
    {highlight = 0;}
}
function dayCheck(d) {
  if (highlight && nowday == d)
    {document.write("<td bgcolor='#DDFFDD'>")}
  //{document.write("<td bgcolor='#004000'>")}
  else
    {document.write("<td>")}
}
function dayCheckS(d) {
  if (highlight && nowday == d)
    {document.write("<td bgcolor='#EADDF9'>")}
  //{document.write("<td bgcolor='#500060'>")}
  else
    {document.write("<td bgcolor='#e2ecfc'>")}
  //{document.write("<td bgcolor='#222255'>")}
}
function dayCheckH(d) {
  if (highlight && nowday == d)
    {document.write("<td bgcolor='#660066'>")}
  else
    {document.write("<td bgcolor='#330033'>")}
}
function read(ref) {
  var url = "http://";
  if (document.myform.cjbversion && document.myform.cjbversion.checked)
    {url = url + "bible.crosswalk.com/OnlineStudyBible/bible.cgi?new=1" + 
      "&section=0&version=cjb&language=en&showtools=0&word=" + ref;}
  else {
    url = url + "www.biblegateway.com/passage/index.php?x=12";
    url = url + "&version=" + document.myform.version.value;
    //alert(document.myformB.printview.checked);
    if (document.myform.printview.checked)
      {url = url + "&interface=print";}
    url = url + "&search=" + ref;
  }
  //alert(url);
  if (document.myform.openwin.checked)
    {var win = window.open(url);}
  else
    {location.href = url;}
}
// For when the CJB is like the Jewish Bibles and different from English Bibles
function readCJB(jewishCJBref,nonjewishref) {
  if (document.myform.version.value==81 || 
      (document.myform.cjbversion && document.myform.cjbversion.checked))
    {read(jewishCJBref);}                  //show Jewish-Bible verse numbering
  else                                     // for both Hebrew and CJB
    {read(nonjewishref);}
}
// For when the CJB is different from Jewish Bibles and like the English Bibles
function readTanach(jewishref,nonjewishCJBref) {
  if (document.myform.version.value==81)  //only show Jewish-Bible verse number
    {read(jewishref);}                    // when viewing Hebrew
  else
    {read(nonjewishCJBref);}
}

function updateboxes() {
  if (document.myform.cjbversion.checked) {
    document.myform.printview.disabled = true;
    document.myform.version.disabled = true;
    //document.all.pview.style.color="gray";
  } else {
    //document.all.pview.style.color="#5A6370";
    document.myform.printview.disabled = false;
    document.myform.version.disabled = false;
  }
}

function showCJBcheckbox() {
  document.writeln("<input type='checkbox' id='cjbversion' name='cjbversion' " +
    "value='0' onclick='updateboxes();' class='calcheckbox' /> Complete " + 
    "Jewish Bible version");
}

function showcheckbox12() {
  document.writeln("<input type='checkbox' id='openwin' name='openwin' " + 
    "value='0' class='calcheckbox' /> Open links in new window");
  document.writeln("&nbsp; <span id='pview'><input type='checkbox' " + 
    "id='printview' name='printview' value='1' checked class='calcheckbox' />" +
    " Printer-friendly</span>");
}

function showcheckbox1() {
  document.writeln("<input type='checkbox' id='openwin' name='openwin' " + 
    "value='0' class='calcheckbox' /> Open links in new window");
}

function showcheckbox2() {
  document.writeln("<span id='pview'><input type='checkbox' id='printview' " + 
    "name='printview' value='1' checked class='calcheckbox' />" + 
    " Printer-friendly</span>");
}

function showlistbox() {
  document.writeln("<select id='version' name='version' style='margin-bottom: 0px; margin-top: 4px;'>");
  document.writeln("<OPTION class=lang value=28>&#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577; (AR)</OPTION>");
  document.writeln("<OPTION value=28>&nbsp; &nbsp; &nbsp;Arabic Life Application Bible</OPTION>");
  document.writeln("<OPTION class=lang value=21>&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080; (BG)</OPTION>");
  document.writeln("<OPTION value=21>&nbsp; &nbsp; &nbsp;Bulgarian Bible</OPTION>");
  document.writeln("<OPTION value=82>&nbsp; &nbsp; &nbsp;1940 Bulgarian Bible</OPTION>");
  document.writeln("<OPTION class=lang value=23>Kreyol (CPF)</OPTION>");
  document.writeln("<OPTION value=23>&nbsp; &nbsp; &nbsp;Haitian Creole Version</OPTION>");
  document.writeln("<OPTION class=lang value=29>&#268;eština (CS)</OPTION>");
  document.writeln("<OPTION value=29>&nbsp; &nbsp; &nbsp;Slovo na cestu</OPTION>");
  document.writeln("<OPTION class=lang value=11>Dansk (DA)</OPTION>");
  document.writeln("<OPTION value=11>&nbsp; &nbsp; &nbsp;Dette er Biblen pÃ¥ dansk</OPTION>");
  document.writeln("<OPTION class=lang value=54>Deutsch (DE)</OPTION>");
  document.writeln("<OPTION value=54>&nbsp; &nbsp; &nbsp;Elberfelder</OPTION>");
  document.writeln("<OPTION value=33>&nbsp; &nbsp; &nbsp;Hoffnung für Alle</OPTION>");
  document.writeln("<OPTION value=10>&nbsp; &nbsp; &nbsp;Luther Bibel 1545</OPTION>");
  document.writeln("<OPTION class=lang value=31>English (EN)</OPTION>");
  document.writeln("<OPTION value=31>&nbsp; &nbsp; &nbsp;New International Version</OPTION>");
  document.writeln("<OPTION value=49>&nbsp; &nbsp; &nbsp;New American Standard Bible</OPTION>");
  document.writeln("<OPTION value=65>&nbsp; &nbsp; &nbsp;The Message</OPTION>");
  document.writeln("<OPTION value=45>&nbsp; &nbsp; &nbsp;Amplified Bible</OPTION>");
  document.writeln("<OPTION value=51>&nbsp; &nbsp; &nbsp;New Living Translation</OPTION>");
  document.writeln("<OPTION value=9>&nbsp; &nbsp; &nbsp;King James Version</OPTION>");
  document.writeln("<OPTION value=47>&nbsp; &nbsp; &nbsp;English Standard Version</OPTION>");
  document.writeln("<OPTION value=46>&nbsp; &nbsp; &nbsp;Contemporary English Version</OPTION>");
  document.writeln("<OPTION value=50 selected>&nbsp; &nbsp; &nbsp;New King James Version</OPTION>");
  document.writeln("<OPTION value=48>&nbsp; &nbsp; &nbsp;21st Century King James Version</OPTION>");
  document.writeln("<OPTION value=8>&nbsp; &nbsp; &nbsp;American Standard Version</OPTION>");
  document.writeln("<OPTION value=15>&nbsp; &nbsp; &nbsp;Young's Literal Translation</OPTION>");
  document.writeln("<OPTION value=16>&nbsp; &nbsp; &nbsp;Darby Translation</OPTION>");
  document.writeln("<OPTION value=74>&nbsp; &nbsp; &nbsp;New Life Version</OPTION>");
  document.writeln("<OPTION value=77>&nbsp; &nbsp; &nbsp;Holman Christian Standard Bible</OPTION>");
  document.writeln("<OPTION value=76>&nbsp; &nbsp; &nbsp;New International Reader's Version</OPTION>");
  document.writeln("<OPTION value=53>&nbsp; &nbsp; &nbsp;Wycliffe New Testament</OPTION>");
  document.writeln("<OPTION value=73>&nbsp; &nbsp; &nbsp;Worldwide English (New Testament)</OPTION>");
  document.writeln("<OPTION value=64>&nbsp; &nbsp; &nbsp;New International Version - UK</OPTION>");
  document.writeln("<OPTION class=lang value=60>Español (ES)</OPTION>");
  document.writeln("<OPTION value=60>&nbsp; &nbsp; &nbsp;Reina-Valera 1960</OPTION>");
  document.writeln("<OPTION value=42>&nbsp; &nbsp; &nbsp;Nueva Versión Internacional</OPTION>");
  document.writeln("<OPTION value=61>&nbsp; &nbsp; &nbsp;Reina-Valera 1995</OPTION>");
  document.writeln("<OPTION value=41>&nbsp; &nbsp; &nbsp;Castillian</OPTION>");
  document.writeln("<OPTION value=6>&nbsp; &nbsp; &nbsp;Reina-Valera Antigua</OPTION>");
  document.writeln("<OPTION value=57>&nbsp; &nbsp; &nbsp;Biblia en Lenguaje Sencillo</OPTION>");
  document.writeln("<OPTION value=59>&nbsp; &nbsp; &nbsp;La Biblia de las Américas</OPTION>");
  document.writeln("<OPTION class=lang value=2>Français (FR)</OPTION>");
  document.writeln("<OPTION value=2>&nbsp; &nbsp; &nbsp;Louis Segond</OPTION>");
  document.writeln("<OPTION value=32>&nbsp; &nbsp; &nbsp;La Bible du Semeur</OPTION>");
  document.writeln("<OPTION class=lang value=68>&#922;&#959;&#953;&#957;&#951; (GRC)</OPTION>");
  document.writeln("<OPTION value=68>&nbsp; &nbsp; &nbsp;1881 Westcott-Hort New Testament</OPTION>");
  document.writeln("<OPTION value=69>&nbsp; &nbsp; &nbsp;1550 Stephanus New Testament</OPTION>");
  document.writeln("<OPTION value=70>&nbsp; &nbsp; &nbsp;1894 Scrivener New Testament</OPTION>");
  document.writeln("<OPTION class=lang value=81>&#1514;&#1497;&#1512;&#1489;&#1497;&#1506; (HE)</OPTION>");
  document.writeln("<OPTION value=81>&nbsp; &nbsp; &nbsp;The Westminster Leningrad Codex</OPTION>");
  document.writeln("<OPTION class=lang value=71>Ilonggo (HIL)</OPTION>");
  document.writeln("<OPTION value=71>&nbsp; &nbsp; &nbsp;Hiligaynon Bible</OPTION>");
  document.writeln("<OPTION class=lang value=62>Hrvatski (HR)</OPTION>");
  document.writeln("<OPTION value=62>&nbsp; &nbsp; &nbsp;Croatian Bible</OPTION>");
  document.writeln("<OPTION class=lang value=17>Magyar (HU)</OPTION>");
  document.writeln("<OPTION value=17>&nbsp; &nbsp; &nbsp;Hungarian KÃ¡roli</OPTION>");
  document.writeln("<OPTION class=lang value=18>Íslenska (IS)</OPTION>");
  document.writeln("<OPTION value=18>&nbsp; &nbsp; &nbsp;Icelandic Bible</OPTION>");
  document.writeln("<OPTION class=lang value=55>Italiano (IT)</OPTION>");
  document.writeln("<OPTION value=55>&nbsp; &nbsp; &nbsp;La Nuova Diodati</OPTION>");
  document.writeln("<OPTION value=34>&nbsp; &nbsp; &nbsp;La Parola è Vita</OPTION>");
  document.writeln("<OPTION class=lang value=20>&#54620;&#44397;&#50612; (KO)</OPTION>");
  document.writeln("<OPTION value=20>&nbsp; &nbsp; &nbsp;Korean Bible</OPTION>");
  document.writeln("<OPTION class=lang value=24>M&#257;ori (MI)</OPTION>");
  document.writeln("<OPTION value=24>&nbsp; &nbsp; &nbsp;Maori Bible</OPTION>");
  document.writeln("<OPTION class=lang value=56>Plautdietsch (NDS)</OPTION>");
  document.writeln("<OPTION value=56>&nbsp; &nbsp; &nbsp;Reimer 2001</OPTION>");
  document.writeln("<OPTION class=lang value=30>Nederlands (NL)</OPTION>");
  document.writeln("<OPTION value=30>&nbsp; &nbsp; &nbsp;Het Boek</OPTION>");
  document.writeln("<OPTION class=lang value=5>Norsk (NO)</OPTION>");
  document.writeln("<OPTION value=5>&nbsp; &nbsp; &nbsp;Det Norsk Bibelselskap 1930</OPTION>");
  document.writeln("<OPTION value=35>&nbsp; &nbsp; &nbsp;Levande Bibeln</OPTION>");
  document.writeln("<OPTION class=lang value=36>Português (PT)</OPTION>");
  document.writeln("<OPTION value=36>&nbsp; &nbsp; &nbsp;Nova Versão Internacional</OPTION>");
  document.writeln("<OPTION value=37>&nbsp; &nbsp; &nbsp;O Livro</OPTION>");
  document.writeln("<OPTION value=25>&nbsp; &nbsp; &nbsp;João Ferreira de Almeida Atualizada&nbsp;&nbsp;</OPTION>");
  document.writeln("<OPTION class=lang value=14>Român&#259; (RO)</OPTION>");
  document.writeln("<OPTION value=14>&nbsp; &nbsp; &nbsp;Romanian</OPTION>");
  document.writeln("<OPTION value=26>&nbsp; &nbsp; &nbsp;Romanian Cornilescu Version</OPTION>");
  document.writeln("<OPTION value=38>&nbsp; &nbsp; &nbsp;Romanian</OPTION>");
  document.writeln("<OPTION class=lang value=13>&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081; (RU)</OPTION>");
  document.writeln("<OPTION value=13>&nbsp; &nbsp; &nbsp;Russian Synodal Version</OPTION>");
  document.writeln("<OPTION value=39>&nbsp; &nbsp; &nbsp;Slovo Zhizny</OPTION>");
  document.writeln("<OPTION class=lang value=40>Sloven&#269;ina (SK)</OPTION>");
  document.writeln("<OPTION value=40>&nbsp; &nbsp; &nbsp;Nádej pre kazdého</OPTION>");
  document.writeln("<OPTION class=lang value=1>Shqip (SQ)</OPTION>");
  document.writeln("<OPTION value=1>&nbsp; &nbsp; &nbsp;Albanian Bible</OPTION>");
  document.writeln("<OPTION class=lang value=44>Svenska (SV)</OPTION>");
  document.writeln("<OPTION value=44>&nbsp; &nbsp; &nbsp;Levande Bibeln</OPTION>");
  document.writeln("<OPTION value=7>&nbsp; &nbsp; &nbsp;Svenska 1917</OPTION>");
  document.writeln("<OPTION class=lang value=75>Kiswahili (SW)</OPTION>");
  document.writeln("<OPTION value=75>&nbsp; &nbsp; &nbsp;Swahili New Testament</OPTION>");
  document.writeln("<OPTION class=lang value=43>Tagalog (TL)</OPTION>");
  document.writeln("<OPTION value=43>&nbsp; &nbsp; &nbsp;Ang Salita ng Diyos</OPTION>");
  document.writeln("<OPTION class=lang value=19>Tiêng Viêt (VI)</OPTION>");
  document.writeln("<OPTION value=19>&nbsp; &nbsp; &nbsp;1934 Vietnamese Bible</OPTION>");
  document.writeln("<OPTION class=lang value=22>&#27721;&#35821; (ZH)</OPTION>");
  document.writeln("<OPTION value=22>&nbsp; &nbsp; &nbsp;Chinese Union Version (Traditional)</OPTION>");
  document.writeln("<OPTION value=80>&nbsp; &nbsp; &nbsp;Chinese Union Version (Simplified)</OPTION>");
  document.writeln("</select>");
}



function el(tid) {
  return document.getElementById(tid);
}
function IO(U, V) {//LA MOD String Version. A tiny ajax library.  by, DanDavis
  var X = !window.XMLHttpRequest 
    ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
  X.open(V ? 'PUT' : 'GET', U, false );
  X.setRequestHeader('Content-Type', 'text/html')
  X.send(V ? V : '');
  return X.responseText;
}	
function doprtn(prtn1a, prtn1b, prtn2a, prtn2b) {
    var ret = '<tr><td colspan="7">';
    ret += prtn1a + '\n';
    if (prtn1b != "")
      ret += prtn1b + '\n';
    if (prtn2a != "")
      ret += '<br />' + prtn2a + '\n';
    if (prtn2b != "")
      ret += prtn2b + '\n';
    ret += '<\/td><\/tr><tr>';
    return ret;
}
function dotdstart(day, weekday, holiday) {
  var ret = '';
  if (holiday) {
    if (highlight && nowday == day)
      ret = '<td class=\'tdholidaytoday\'>';
    else
      ret = '<td class=\'tdholiday\'>';
  }else if (weekday == 7) {
    if (highlight && nowday == day)
      ret = '<td class=\'tdshabbattoday\'>';
    else
      ret = '<td class=\'tdshabbat\'>';
  }else {
    if (highlight && nowday == day)
      ret = '<td class=\'tdweekdaytoday\'>';
    else
      ret = '<td class=\'tdweekday\'>';
  }
  return ret;
  /*
  var ret2 = '<script type="text\/javascript">dayCheck';
  if (weekday == 7) 
    ret2 += 'S';
  ret2 += '(' + day + ')<\/scr' + 'ipt><noscr' + 'ipt><td><\/noscr' + 'ipt>';
  */
}
function dodaynum(day, hebday) {
  var ret = '';
  var hebdaysplit = hebday.split(' ');
  if (hebdaysplit[0] == '1') {
    ret = '<a class="holy" title="' + hebday;
    ret += '" href="javascript:read(\'Numbers 28:1-15\');">';
    ret += '<b><i>' + day + '</i></b></a>';
  }else {
    ret = '<a title="' + hebday + '">' + day + '</a>';
  }
  return ret;
}
function doprov(day) {
  var ret = '<a class="prov" href="javascript:read(\'Proverbs ';
  ret += day + '\');">Proverbs ';
  ret += day + '</a><br \/>';
  return ret;
}
function dopslm(day) {
  var ret = "";
  if (day == "31") {
    ret = '<a class="pslm" href="javascript:read(\'Psalms 119\');">Psalm 119';
    ret += '</a><br \/><br \/><br \/>';
  }else if (day == "29") {
    ret = '<a class="pslm" href="javascript:read(\'Psalms 29, 59, 89, 149\');"';
    ret += '>Psalms 29,59,<br />89,149</a><br \/><br \/>';
  }else {
    ret = '<a class="pslm" href="javascript:read(\'Psalms ';
    ret += day + ', ';
    ret += (parseInt(day) + 30) + ', ';
    ret += (parseInt(day) + 60) + ', ';
    ret += (parseInt(day) + 90) + ', ';
    ret += (parseInt(day) + 120) + '\');">Psalms ';
    ret += day + ',';
    ret += (parseInt(day) + 30) + ',<br \/>';
    ret += (parseInt(day) + 60) + ',';
    ret += (parseInt(day) + 90) + ',';
    ret += (parseInt(day) + 120) + '</a><br \/><br \/>';
  }
  return ret;
}
function dordng(weekday) {
  var ret = 'Reading ';
  switch (weekday) {
    case 1: ret += "I"; break;
    case 2: ret += "II"; break;
    case 3: ret += "III"; break;
    case 4: ret += "IV"; break;
    case 5: ret += "V"; break;
    case 6: ret += "VI"; break;
    case 7: ret += "VII"; break;
  }
  ret += ':<br \/>';
  return ret;
}
function doalyh(book, main, othr) {
  var ret = '';
  if (othr == "") {
    ret = '<a class="alyh" href="javascript:read(\'';
    ret += book + ' ' + main.replace("--", "-") + '\');">';
    ret += book + '<br \/>';
    ret += main + '</a><br \/>&nbsp;';
  }else {
    ret = '<a class="alyh" href="javascript:readCJB(\'';
    ret += book + ' ' + main.replace("--", "-") + '\',\'';
    ret += book + ' ' + othr.replace("--", "-") + '\');">';
    ret += book + '<br \/>';
    ret += main + '<br \/><i>(';
    ret += othr + ')</i><\/a>';
  }
  return ret;  
}
function writecalendar() {
  // Load current month
  var fname = '' + nowyear;
  if (nowmonth + 1 < 10) fname += '0';
  fname += (nowmonth + 1) + ".txt";
  var lines = IO(fname).split(/\r?\n/g);
  
  // Calendar Title 
  document.writeln("<h2>" + lines[0] + "</h2>");
  
  checkEnabled( parseInt(lines[1]), parseInt(lines[2]) );
  
  document.write('<table border="1" cellpadding="3" bordercolordark="#606060" '
  +'bordercolorlight="#7F7F7F" class="readcal">\n'
  +'<tr>\n'
  +'  <th>Yom Rishon<br \/><i>Sunday<\/i><\/th>\n'
  +'  <th>Yom Sh\'ni<br \/><i>Monday<\/i><\/th>\n'
  +'  <th>Yom Sh\'lishi<br \/><i>Tuesday<\/i><\/th>\n'
  +'  <th>Yom Revi\'i<br \/><i>Wednesday<\/i><\/th>\n'
  +'  <th>Yom Shishi<br \/><i>Thursday<\/i><\/th>\n'
  +'  <th>Yom Chamishi<br \/><i>Friday<\/i><\/th>\n'
  +'  <th>Yom Shabbat<br \/><i>Saturday<\/i><\/th>\n'
  +'<\/tr>\n');
  
  var block = 1;
  var day = 0;
  var holiday = 0;
  
  for (d = 1; d <= lines.length - 5; d += 40) {
    document.writeln( doprtn( lines[block*5+1], lines[block*5+2], 
                              lines[block*5+3], lines[block*5+4] ) ); 
    block++;
    for (weekday = 1; weekday <= 7; weekday++) {
      if (block*5 >= lines.length)
        break;
      if (lines[block*5] == "empty") {
        document.writeln('<td>&nbsp;<\/td>');
        block++;
      }else {
        if (lines[block*5].substring(0,1) == 'H') {
          holiday = 1;
          day = parseInt(lines[block*5].substr(1));
        }else {
          holiday = 0;
          day = lines[block*5];
        }
        document.writeln( dotdstart( day, weekday, holiday ) );
        document.writeln( dodaynum( day, lines[block*5+1] ) );
        document.writeln('<center>');
        document.writeln( doprov( day ) );
        document.writeln( dopslm( day ) );
        document.writeln( dordng( weekday ) );
        document.writeln( doalyh( lines[block*5+2], lines[block*5+3], 
                                  lines[block*5+4] ) );
        document.writeln('<\/center>');
        document.writeln('<\/td>');
        block++;
      }
    }
    document.writeln('<\/tr>');
  }
  document.write('<\/table>');
}

