/* 
 This file was generated by Dashcode.  
 You may edit this file to customize your widget or web page 
 according to the license.txt file included in the project.
 */

var BTN_READINGS = 'button1';
var BTN_REFLECTION = 'button';

var currDate = new Date();

var subs = new Array(4);
subs[2] = new Array(28);
subs[3] = new Array(31);
subs[4] = new Array(4);
subs[2][17]='Ash Wednesday';
subs[2][18]='Thursday after Ash Wednesday';
subs[2][19]='Friday after Ash Wednesday';
subs[2][20]='Saturday after Ash Wednesday';
subs[2][21]='1st Sunday of Lent';
subs[2][22]='Monday, 1st Week of Lent';
subs[2][23]='Tuesday, 1st Week of Lent';
subs[2][24]='Wednesday, 1st Week of Lent';
subs[2][25]='Thursday, 1st Week of Lent';
subs[2][26]='Friday, 1st Week of Lent';
subs[2][27]='Saturday, 1st Week of Lent';
subs[2][28]='2nd Sunday of Lent';
subs[3][1]='Monday, 2nd Week of Lent';
subs[3][2]='Tuesday, 2nd Week of Lent';
subs[3][3]='Wednesday, 2nd Week of Lent';
subs[3][4]='Thursday, 2nd Week of Lent';
subs[3][5]='Friday, 2nd Week of Lent';
subs[3][6]='Saturday, 2nd Week of Lent';
subs[3][7]='3rd Sunday of Lent';
subs[3][8]='Monday, 3rd Week of Lent';
subs[3][9]='Tuesday, 3rd Week of Lent';
subs[3][10]='Wednesday, 3rd Week of Lent';
subs[3][11]='Thursday, 3rd Week of Lent';
subs[3][12]='Friday, 3rd Week of Lent';
subs[3][13]='Saturday, 3rd Week of Lent';
subs[3][14]='4th Sunday of Lent';
subs[3][15]='Monday, 4th Week of Lent';
subs[3][16]='Tuesday, 4th Week of Lent';
subs[3][17]='Feast of St. Patrick';
subs[3][18]='Thursday, 4th Week of Lent';
subs[3][19]='Feast of St. Joseph';
subs[3][20]='Saturday, 4th Week of Lent';
subs[3][21]='5th Sunday of Lent';
subs[3][22]='Monday, 5th Week of Lent';
subs[3][23]='Tuesday, 5th Week of Lent';
subs[3][24]='Wednesday, 5th Week of Lent';
subs[3][25]='Thursday, 5th Week of Lent';
subs[3][26]='Friday, 5th Week of Lent';
subs[3][27]='Saturday, 5th Week of Lent';
subs[3][28]='Palm Sunday';
subs[3][29]='Monday of Holy Week';
subs[3][30]='Tuesday of Holy Week';
subs[3][31]='Wednesday of Holy Week';
subs[4][1]='Holy Thursday';
subs[4][2]='Good Friday';
subs[4][3]='Holy Saturday';
subs[4][4]='Easter Sunday';
var m_names = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");

var readings = new Array(4);
readings[2] = new Array(28);
readings[3] = new Array(31);
readings[4] = new Array(4);
readings[2][17]='Joel 2:12-18%Psalm 51:3-4, 5-6ab, 12-13, 14 and 17%2 Corinthians 5:20—6:2%Matthew 6:1-6, 16-18';
readings[2][18]='Deuteronomy 30:15-20%Psalm 1:1-2, 3, 4 and 6%%Luke 9:22-25';
readings[2][19]='Isaiah 58:1-9a%Psalm 51:3-4, 5-6ab, 18-19%%Matthew 9:14-15';
readings[2][20]='Isaiah 58:9b-14%Psalm 86:1-2, 3-4, 5-6%%Luke 5:27-32';
readings[2][21]='Deuteronomy 26:4-10%Psalm 91:1-2, 10-11, 12-13, 14-15%Romans 10:8-13%Luke 4:1-13';
readings[2][22]='Leviticus 19:1-2, 11-18%Psalm 23:1-3a, 4, 5, 6%%Matthew 25:31-46';
readings[2][23]='Isaiah 55:10-11%Psalm 34:4-5, 6-7, 16-17, 18-19%%Matthew 6:7-15';
readings[2][24]='Jonah 3:1-10%Psalm 51:3-4, 12-13, 18-19%%Luke 11:29-32';
readings[2][25]='Esther C:12, 14-16, 23-25%Psalm 138:1-2ab, 2cde-3, 7c-8%%Matthew 7:7-12';
readings[2][26]='Ezekiel 18:21-28%Psalm 130:1-2, 3-4, 5-7a, 7bc-8%%Matthew 5:20-26';
readings[2][27]='Deuteronomy 26:16-19%Psalm 119:1-2, 4-5, 7-8%%Matthew 5:43-48';
readings[2][28]='Genesis 15:5-12, 17-18%Psalm 27:1, 7-8, 8-9, 13-14%Philippians 3:17-4:1%Luke 9:28b-36';
readings[3][1]='Daniel 9:4-10%Psalm 79:8, 9, 11 and 13%%Luke 6:36-38';
readings[3][2]='Isaiah 1:10, 16-20%Psalm 50:8-9, 16bc-17, 21 and 23%%Matthew 23:1-12';
readings[3][3]='Jeremiah 18:18-20%Psalm 31:5-6, 14, 15-16%%Matthew 20:17-28';
readings[3][4]='Jeremiah 17:5-10%Psalm 1:1-2, 3, 4 and 6%%Luke 16:19-31';
readings[3][5]='Genesis 37:3-4, 12-13a, 17b-28a%Psalm 105:16-17, 18-19, 20-21%%Matthew 21:33-43, 45-46';
readings[3][6]='Micah 7:14-15, 18-20%Psalm 103:1-2, 3-4, 9-10, 11-12%%Luke 15:1-3, 11-32';
readings[3][7]='Exodus 3:1-8a, 13-15%Psalm 103: 1-2, 3-4, 6-7, 8, 11%1 Corinthians 10:1-6, 10-12%Luke 13:1-9';
readings[3][8]='2 Kings 5:1-15b%Psalm 42:2, 3; 43:3, 4%%Luke 4:24-30';
readings[3][9]='Daniel 3:25, 34-43%Psalm 4:4-5ab, 6 and 7bc, 8-9%%Matthew 18:21-35';
readings[3][10]='Deuteronomy 4:1, 5-9%Psalm 147:12-13, 15-16, 19-20%%Matthew 5:17-19';
readings[3][11]='Jeremiah 7:23-28%Psalm 95:1-2, 6-7, 8-9%%Luke 11:14-23';
readings[3][12]='Hosea 14:2-10%Psalm 81:6c-8a, 8bc-9, 10-11ab, 14 and 17%%Mark 12:28b-34';
readings[3][13]='Hosea 6:1-6%Psalm 51:3-4, 18-19, 20-21ab%%Luke 18:9-14 ';
readings[3][14]='Joshua 5:9a, 10-12%Psalm 23: 1-3a, 3b-4, 5, 6%2 Corinthians 5:17-21%Luke 15:1-3, 11-32';
readings[3][15]='Isaiah 65:17-21%Psalm 30:2 and 4, 5-6, 11-12a and 13b%%John 4:43-54';
readings[3][16]='Ezekiel 47:1-9, 12%Psalm 46:2-3, 5-6, 8-9%%John 5:1-3, 5-16 ';
readings[3][17]='Isaiah 49:8-15%Psalm 145:8-9, 13cd-14, 17-18%%John 5:17-30 ';
readings[3][18]='Exodus 32:7-14%Psalm 106:19-20, 21-22, 23%%John 5:31-47 ';
readings[3][19]='Wisdom 2:1a, 12-22%Psalm 89:2-3, 4-5, 27 and 29%%John 7:1-2, 10, 25-30';
readings[3][20]='Jeremiah 11:18-20%Psalm 7:2-3, 9bc-10, 11-12%%John 7:40-53';
readings[3][21]='Isaiah 43:16-21%Psalm 126:1-2, 2-3, 4-5, 6%Philippians 3:8-14%John 8:1-11';
readings[3][22]='Daniel 13:1-9, 15-17, 19-30, 33-62%Psalm 23:1-3a, 3b-4, 5, 6%%John 8:12-20';
readings[3][23]='Numbers 21:4-9%Psalm 102:2-3, 16-18, 19-21%%John 8:21-30';
readings[3][24]='Daniel 3:14-20, 91-92, 95%Daniel 3:52-56%%John 8:31-42';
readings[3][25]='Genesis 17:3-9%Psalm 40:7-8a, 8b-9, 10, 11%%John 8:51-59';
readings[3][26]='Jeremiah 20:10-13%Psalm 18:2-3a, 3bc-4, 5-6, 7%%John 10:31-42';
readings[3][27]='Ezekiel 37:21-28%Jeremiah 31:10, 11-12abcd%%John 11:45-56';
readings[3][28]='Isaiah 50:4-7%Psalm 22:8-9, 17-18, 19-20, 23-24%Philippians 2:6-11%Luke 22:14-23:56';
readings[3][29]='Isaiah 42:1-7%Psalm 27:1, 2, 3, 13-14%%John 12:1-11';
readings[3][30]='Isaiah 49:1-6%Psalm 71:1-2, 3-4a, 5ab-6ab, 15 and 17%%John 13:21-33, 36-38';
readings[3][31]='Isaiah 50:4-9a%Psalm 69:8-10, 21-22, 31 and 33-34%%Matthew 26:14-25';
readings[4][1]='Exodus 12:1-8, 11-14%Psalm 116:12-13, 15-16bc, 17-18%1 Corinthians 11:23-26%John 13:1-15';
readings[4][2]='Isaiah 52:13-53:12%Psalm 31:2, 6, 12-13, 15-16, 17, 25%Hebrews 4:14-16; 5:7-9%John 18:1-19:42';
readings[4][3]='%%%';
readings[4][4]='Acts 10:34a, 37-43%Psalm 118:1-2, 16-17, 22-23%Colossians 3:1-4 or 1 Corinthians 5:6b-8%Luke 24:13-35';


//
// Function: load()
// Called by HTML body element's onload event when the web application is ready to start
//
function load()
{
    dashcode.setupParts();
    
    populatePopup();
    limitDate();
    document.getElementById('popup').value=(currDate.getMonth()+1)+"/"+currDate.getDate();
    dateChanged(currDate);
    toggleButtons(BTN_REFLECTION);
}

function toggleButtons(buttonID)
{
  var btnReadings = document.getElementById(BTN_READINGS).object;
  var btnReflection = document.getElementById(BTN_REFLECTION).object;
  
  if (buttonID == BTN_READINGS)
  {
    btnReadings.setState(PushButton.STATE_ON);
    btnReflection.setState(PushButton.STATE_OFF);
  }
  else
  {
    btnReadings.setState(PushButton.STATE_OFF);
    btnReflection.setState(PushButton.STATE_ON);
  }
}

function setViewToReadings(event)
{
    var stackLayout = document.getElementById('stackLayout').object;
    stackLayout.setCurrentView('Readings');

    toggleButtons(BTN_READINGS);
}


function setViewToReflection(event)
{
    var stackLayout = document.getElementById('stackLayout').object;
    stackLayout.setCurrentView('Reflection');
    
    toggleButtons(BTN_REFLECTION);
}

function setHeadings(date)
{  
  var mnth = date.getMonth() +1;
  var day = date.getDate(); 

  document.getElementById('Date').innerHTML = m_names[mnth-1] + ' ' + day;
  document.getElementById('Subtitle').innerHTML = subs[mnth][day];
}

function setContent(date)
{
  var mnth = date.getMonth() +1;
  var day = date.getDate();
  var readingRefs = new Array(3);
  readingRefs = readings[mnth][day].split('%');
  var linkprefix;

  xmlRequest = new XMLHttpRequest();
  xmlRequest.onload = onloadHandler;
  xmlRequest.onerror = function() {alert("Could not connect — please check that you have a working internet connection and try again.");};
  xmlRequest.open("GET", "Reflections/" + mnth + day + ".html");
  xmlRequest.setRequestHeader("Cache-Control", "no-cache");
  xmlRequest.send(null);
  
  document.getElementById('holysaturday').style.display = 'none';
  if (mnth == 3 && day == 28)
  {
    linkprefix = 'http://www.usccb.org/nab/032810a.shtml#';
  }
  else if (mnth == 4 && day == 1)
  {
    linkprefix = 'http://www.usccb.org/nab/040110b.shtml#';
  }
  else if (mnth == 4 && day == 3)
  {
    document.getElementById('holysaturday').style.display = 'block';
  }
  else
  {
    if (day < 10) { day = "0" + day; }
    linkprefix = 'http://www.usccb.org/nab/0' + mnth + day + '10.shtml#';
  }
  
  if (readingRefs[0].length > 0)
  {
    document.getElementById('reading1').style.display = 'block';
    document.getElementById('reading1link').href = linkprefix + 'reading1';
    document.getElementById('reading1ref').innerHTML = readingRefs[0];
  }
  else {document.getElementById('reading1').style.display = 'none';}
  
  if (readingRefs[1].length > 0)
  {
    document.getElementById('psalm').style.display = 'block';
    document.getElementById('psalmlink').href = linkprefix + 'psalm';
    document.getElementById('psalmref').innerHTML = readingRefs[1];
  }
  else {document.getElementById('psalm').style.display = 'none';}

  if (readingRefs[2].length > 0)
  {
    document.getElementById('reading2').style.display = 'block';
    document.getElementById('reading2link').href = linkprefix + 'reading2';
    document.getElementById('reading2ref').innerHTML = readingRefs[2];
  }
  else {document.getElementById('reading2').style.display = 'none';}
  
  if (readingRefs[3].length > 0)
  {
    document.getElementById('gospel').style.display = 'block';
    document.getElementById('gospellink').href = linkprefix + 'gospel';
    document.getElementById('gospelref').innerHTML = readingRefs[3];
  }
  else {document.getElementById('gospel').style.display = 'none';}

}

function dateChanged(date)
{
  setHeadings(date);
  setContent(date);
}

function dateDecrement(event)
{
  currDate.setTime(currDate.valueOf() - (1000*60*60*24));
  limitDate();
  dateChanged(currDate);
  document.getElementById('popup').value=(currDate.getMonth()+1)+"/"+currDate.getDate();
}

function dateIncrement(event)
{
  currDate.setTime(currDate.valueOf() + (1000*60*60*24));
  limitDate();
  dateChanged(currDate);
  document.getElementById('popup').value=(currDate.getMonth()+1)+"/"+currDate.getDate();
}

function limitDate()
{
  if (currDate < Date.parse("Feb 17 2010")) {
    currDate.setTime(Date.parse("Feb 17 2010"));
  }
  else if (currDate > Date.parse("Apr 4 2010")) {
    currDate.setTime(Date.parse("Apr 4 2010"));
  }
  
  document.getElementById('button3').object.setEnabled(currDate < Date.parse("Apr 4 2010"));
  document.getElementById('button2').object.setEnabled(currDate > Date.parse("Feb 17 2010"));
  
}


/* XMLHttpRequest Setup */
var onloadHandler  = function() { xmlLoaded(xmlRequest,'ReflectionBody'); };
var onloadHandler2 = function() { xmlLoaded(xmlRequest2,'ReadingsBody'); };
var xmlRequest;
var xmlRequest2;

function xmlLoaded(xmlRequest,theID) 
{
	if (xmlRequest.readyState == 4) {
    document.getElementById(theID).innerHTML = xmlRequest.responseText;
	}
}

function populatePopup()
{
  var mnth;
  var day;
  var optionsSize = 0;
  
  for (mnth in subs)
  {
    if (subs[mnth].length > 0)
    {
      for (day in subs[mnth])
      {
        if (subs[mnth][day].length > 0)
        {
          document.getElementById('popup').options[optionsSize] = new Option(mnth+"/"+ day + " " + subs[mnth][day],mnth + "/" + day,false,false);
          optionsSize++;
        }
      }
    }
  }
}

function popupChanged(event)
{
  currDate.setTime(Date.parse(document.getElementById('popup').value + "/2010"));
  limitDate()
  dateChanged(currDate);
}
