

var oRequest = !window.XMLHttpRequest ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;

var sURL  = "http://"+self.location.hostname+"/Calendar.csv";

var linenum;


function toYYYYMMDD(strDate)
{
    var d = new Date;

    d.setTime(Date.parse(strDate));
    var yyyymmdd = String(d.getFullYear());
    var mm = d.getMonth() + 1; 
    var dd = d.getDate(); 
    if (mm < 10)
      yyyymmdd = yyyymmdd.concat("0");
    yyyymmdd = yyyymmdd.concat(String(mm));
    if (dd < 10)
      yyyymmdd = yyyymmdd.concat("0");
    yyyymmdd = yyyymmdd.concat(String(dd));

    return yyyymmdd;
}

function to24HourTime (strTime)
{
    var hh;
    var mm;
    var t24 = "";
    var ampmIndex = 4;
    var plusTime = 0;
    
    if (strTime[2] == ':')
    {
      hh = Number(strTime.substr(0,2));
      mm = strTime.substr(3,2);
      ampmIndex = 5;
    }
    else
    {
      hh = Number(strTime.substr(0,1));
      mm = strTime.substr(2,2);
    }
      
    if (strTime[ampmIndex] == 'p')
      hh += 12;
      
    if (hh < 10)
      t24 = "0";
      
    t24 = t24.concat(String(hh), ":", mm);
    
    return t24;
}


function line2Event(line)
{
    var i;
    var cols = line.split(/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/g);
    var colDate;
    var colTime;
    var colTxt;
  
    if(cols.length == 6)
    {
      colDate = 0;
      colTime = 1;
      colTxt = 3;  
    }
    else if(cols.length == 3)
    {
      colDate = 0;
      colTime = 1;
      colTxt = 2;  
    }
    else
        return;
        
    for(i=0;i<cols.length;i++)
    {
      if (cols[i].charAt(0) == '"')
        cols[i] = cols[i].substr(1,cols[i].length-2);
    }

    var yyyymmdd = toYYYYMMDD(cols[colDate]);
    var description = "";
    var pad = "";
    if (cols[colTime].length == 5)
      pad = "0";
    description = description.concat(pad, cols[colTime], " ", cols[colTxt]);

    AddEvent(yyyymmdd, description, "", "", "", "", 45, 45, "", "");
}


oRequest.open("GET",sURL,false);
oRequest.setRequestHeader("User-Agent",navigator.userAgent);
oRequest.send(null)

if (oRequest.status==200)
{
  linenum = 0;
  var lines = oRequest.responseText.split("\r\n");
  for(i=0;i<lines.length;i++)
  {
    linenum++;
    line2Event(lines[i]);
  }
} 
else alert("Error executing XMLHttpRequest call!");
