var weekday=new Array(7)
weekday[0]="Sunday"
weekday[1]="Monday"
weekday[2]="Tuesday"
weekday[3]="Wednesday"
weekday[4]="Thursday"
weekday[5]="Friday"
weekday[6]="Saturday"

var month=new Array(12)
month[0]="January"
month[1]="February"
month[2]="March"
month[3]="April"
month[4]="May"
month[5]="June"
month[6]="July"
month[7]="August"
month[8]="September"
month[9]="October"
month[10]="November"
month[11]="December"

function gdateHelper(gCalTime) {

	var regex = /^([0-9]{4})-?([0-9]{1,2})-?([0-9]{1,2})(T?)([0-9]{2}):?([0-9]{2}):?([0-9]{2})?\.?([0-9]{3})?(Z)?([\+\-])([0-9]{2}):?([0-9]{2})/;
	var results = regex.exec(gCalTime);
	if( null == results )	{
		// alert('failed: ' + gCalTime);
		return new Date(0);
	}

  // minutes of correction between gCalTime and GMT
  var totalCorrMins = 0;

  var year = results[1];
  var month = results[2];
  var dateMonth = results[3];
  var timeOrNot = results[4];

  // if a DATE-TIME was matched in the regex 
  if (timeOrNot == 'T') {
    var hours = results[5];
    var mins = results[6]
    var zuluOrNot = results[9];

    // if time from server is not already in GMT, calculate offset
    if (zuluOrNot != 'Z') {
      var corrPlusMinus = results[10];
      if (corrPlusMinus != '') {
        var corrHours = results[11];
        var corrMins = results[12];
        totalCorrMins = (corrPlusMinus=='-' ? 1 : -1) * 
            (Number(corrHours) * 60 + 
	    (corrMins=='' ? 0 : Number(corrMins)));
      }
    } 

    // get time since epoch and apply correction, if necessary
    // relies upon Date object to convert the GMT time to the local
    // timezone
    var originalDateEpoch = Date.UTC(year, month - 1, dateMonth, hours, mins);
    var gmtDateEpoch = originalDateEpoch + totalCorrMins * 1000 * 60;
    var ld = new Date(gmtDateEpoch);
    return ld;
  }
 }
/**
 * Lists blog entries from the specified JSON feed
 * by creating a new 'ul' element in the DOM.  Each
 * bullet is the title of one blog entry, and contains
 * a hyperlink to that entry's URL.
 *
 * @param {JSON} json is the JSON object pulled from the Blogger service.
 */
function listEntries(json) {
  // Clear any information displayed under the "data" div.
  //removeOldResults();
  //var ul = document.createElement('ul');

  for (var i = 0; i < json.feed.entry.length; i++) {
    var entry = json.feed.entry[i];
    var alturl;

    for (var k = 0; k < entry.link.length; k++) {
      if (entry.link[k].rel == 'alternate') {
        alturl = entry.link[k].href;
        break;
      }
    }

    var li = document.createElement('li');
    var a = document.createElement('a');
    a.href = alturl;
    a.target = '_blank';

    document.getElementById('data').innerHTML += '<h2>' + entry.title.$t + '</h2>' + entry.content.$t + '<hr style="clear:both;">';
    //a.appendChild(txt);
    //li.appendChild(a);

    //ul.appendChild(li);
  }

  // Install the bullet list of blog posts.
  document.getElementById('data').appendChild(ul);

  // Re-enable the search button.
  //var search_button = document.getElementById('search_button');
  //search_button.removeAttribute('disabled');
}

/**
 * Called when the user clicks the 'Search' button to
 * retrieve a blog's JSON feed.  Creates a new script
 * element in the DOM whose source is the JSON feed
 * 'query'.blogspot.com, and specifies that the callback
 * function is 'listEntries' (above).
 *
 * @param {String} query is the blog to be retrieved, specified
 *     as a prefix of ".blogspot.com".
 */
function do_body_onload(query) {
  // Delete any previous JSON script nodes.
  removeOldJSONScriptNodes();
  // Clear any old data to prepare to display the Loading... message.
  removeOldResults();

  // Show a "Loading..." indicator.
  //var div = document.getElementById('data');
  //var p = document.createElement('p');
  //p.appendChild(document.createTextNode('Loading...'));
 // div.appendChild(p);

  // Disable the search button.
  //var search_button = document.getElementById('search_button');
  //search_button.disabled = 'true';

  // Retrieve the JSON feed.
  var script = document.createElement('script');
  script.setAttribute('src', 'http://troyfoodcoopemployment.blogspot.com/feeds/posts' +
                      '/default?alt=json-in-script&callback=listEntries');
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

/**
 * Deletes any old script elements which have been created by previous calls
 * to search().
 */
function removeOldJSONScriptNodes() {
  var jsonScript = document.getElementById('jsonScript');
  if (jsonScript) {
    jsonScript.parentNode.removeChild(jsonScript);
  }
}

/**
 * Deletes pre-existing children of the data div from the page. The data div
 * may contain a "Loading..." message, or the results of a previous query. 
 * This old data should be removed before displaying new data.
 */
function removeOldResults() {
  var div = document.getElementById('data');
  if (div.firstChild) {
    div.removeChild(div.firstChild);
  }
}