// This bit of script was cobbled together by John Banister
// It's available as www.John.Banister.name/csvEvents2.txt
// (Rename it to csvEvents.js to use it.), and a sample of
// the .csv file I designed it to read is available at
// www.John.Banister.name/Town_Events.csv the only information
// It extracts are the first and fourth items, the date and the
// event text. It ignores everything else.
// My contact info is a www.John.Banister.name/comm.html, but
// I'm often on ships at sea, so don't expect to necessarily reach
// me in a timely manner.
// This bit of script is made to interoperate with the modification to
// Kevin's Calendar, I made, building the calendar using the HTML DOM.
// That version is available as www.John.Banister.name/Calendar2.txt
// Kevin's original calendar.js script found via http://calendar.ilsen.net
// or Google "Kevin's Kalendar"
// The DefineEvent and ShowCalendar functions are located there, and this
// isn't useful without them.
// To make this work, follow Kevin's instructions and then, the modifications
// to those instructions at the top of Calendar2.
// add to your page, just like
// you would add in Kevin's instructions
// But, remember, with Calendar2.js, these have to appear in the web page
// AFTER the tag with the reference to Calendar2.js or the browser thinks
// DefineEvent is a variable instead of a function and it doesn't work.
// Put this file and the .csv file on the server in the same place you put
// Calendar2.js
//
// The reason you can't call ShowCalendar directly, is I have
// to make it wait for the server to fetch the .csv file.
// It reads a .csv file created by exporting events from Broderbund's Calendar Creator
// And creates from it events that can be displayed by Kevin's Calendar
// Doubtless, it can be manipulated to read other sorts of .csv files as well.
// Also, this bit of script like the one in www.John.Banister.name/CSV_READ.html
// needs to be on a server to work. It won't necessarily work on your computer
// at home.
// The one variable whose value (the part between the quotes) any user might want to change
// is the one here below. It's the name of the .csv file you put on the server along with
// this file and calendar.js and your html file displaying the results.
var csv_file = "Town_Events.csv" ;
var Town_Event_Date = 19950101 ;
var Town_Event_Text = "Not a big event" ;
// I found this function in Stoyan Stefanov's article at
// http://www.sitepoint.com/article/take-command-ajax
makeHttpRequest(csv_file, 'CalendarizeContents');
function makeHttpRequest(url, callback_function)
{
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Unfortunatelly you browser doesn\'t support this feature.');
return false;
}
http_request.onreadystatechange = function() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
eval(callback_function + '(http_request.responseText)');
} else {
alert('There was a problem with the request.(Code: ' + http_request.status + ')');
}
}
}
http_request.open('GET', url, true);
http_request.send(null);
}
function CalendarizeContents(text) {
BCCcsv_to_events(text);
ShowCalendar();
}
// A lot of the ideas in this function originated in the words (code) of
// Laurent Bugnion (of GalaSoft) in replies to a question at
// http://www.thescripts.com/forum/thread91725.html
function BCCcsv_to_events(csv_events) {
var lines = csv_events.split('\n');
for (i=0; i < lines.length; i++)
{
var One_Town_Event = lines[i].split( ',' ) ;
// Change Date from Calendar Creator format to Kevin's Calendar format
var strDate = One_Town_Event[0].split('/') ;
if (strDate[0].length != 2)
{
if (strDate[0].length = 1)
{
strDate[0] = "0" + strDate[0] ;
} else {
strDate[0] = "01" ;
}
}
if (strDate[1].length != 2)
{
if (strDate[1].length = 1)
{
strDate[1] = "0" + strDate[1] ;
} else {
strDate[1] = "01" ;
}
}
if (strDate[2].length != 4)
{
if (strDate[2].length = 2)
{
strDate[2] = "20" + strDate[2] ;
} else {
strDate[2] = "1995" ;
}
}
var Date_Str = strDate[2] + strDate[0] + strDate[1] ;
Town_Event_Date = parseInt(Date_Str);
// Extract the event text from the Calendar Creator .csv & strip quotes
Town_Event_Text = One_Town_Event[3].substring( 1, One_Town_Event[3].length - 1);
// Call DefineEvent with the appropriate null values in the other four spots
DefineEvent(Town_Event_Date, Town_Event_Text, "", "", 0, 0);
}
}