// Provide the XMLHttpRequest class for IE 5.x-6.x:
// Other browsers (including IE 7.x-8.x) ignore this
//   when XMLHttpRequest is predefined
if (typeof(XMLHttpRequest)  === "undefined") {
  XMLHttpRequest = function() {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
      catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
      catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }
      catch(e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); }
      catch(e) {}
    //throw new Error("This browser does not support XMLHttpRequest.");
  };
}


//new ActiveXObject("Microsoft.XMLHTTP")
var filecontents; 
oRequest = new XMLHttpRequest();
var sURL  = "/LibraryHoursTable.txt";
oRequest.open("GET",sURL,false);
oRequest.setRequestHeader("User-Agent",navigator.userAgent);
oRequest.send(null);
filecontents = oRequest.responseText;
// if the datatable is missing or can't load write a link to our hours page
if (oRequest.status==200) {document.write ("");}
else {var nodata = "Yes";}
//document.write (filecontents);
//split file into array
//split data file string into an array at line breaks
var s, ss;
var s = filecontents;
// Split at each line return character.
ss = s.split("\n");


var todaydate=new Date()
var m=todaydate.getMonth()+1 //get current month (1-12)
var y=todaydate.getFullYear() //get current year
var day=todaydate.getDate() //get day of the month today
var MsorDs="days"
var nTimes=1
var ShowRef=0
var ShowLate=1

//This draws the calendar and and puts the correct date in the correct box
function buildCal(m, y, day, MsorDs, nTimes, ShowRef , ShowLate){
var t=""
if (nodata == "Yes") {document.write('<Center><a href="http://library.newpaltz.edu/library_hours.html">Library Hours Page</a></center>');return;}
document.write ('<LINK REL=StyleSheet HREF="/css/4calendarscript.css" TYPE="text/css">')
for (g = 1; g <= nTimes; g++)
{
if (MsorDs=="days") {var newdate = new Date (y, m-1, day);
newdate.setDate(newdate.getDate()+(g-1));
var easydate = ((newdate.getMonth() + 1) + "/"+ (newdate.getDate()) + "/"+ (newdate.getFullYear()));
var alinebreak = " ";
t+=("<br>"+newdate.toLocaleDateString()+"<br>");
t+=libhours(easydate,ShowRef,ShowLate, alinebreak);}
else {
var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
var Day_names = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"); 

var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st
if (m >= 13) {
m = m-12; 
y++;
}

var todaydate=new Date() //DD added
var alinebreak = "<br>"
var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added
dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
t+='<center><div class="main"><table class="main" cols="7" cellpadding="0" cellspacing="3"><tr align="center">';
t+='<td colspan="7" align="center" class="month">' + mn[m-1] + ' - ' + y + '<BR \> Sojourner Truth Library Hours</td></tr><tr align="center">';
for(s=0;s<7;s++)
t+='<td class="daysofweek">'+Day_names[s]+'</td>';
t+='</tr><tr align="center">';
for(i=1;i<=42;i++){
var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
if (x!='&nbsp;')var easydate = m+"/"+x+"/"+y;
else easydate = "";
if (easydate != "") var thehours = libhours(trim(easydate),ShowRef,ShowLate, alinebreak);
else var thehours = "";
if (x==scanfortoday) x='<span id="today">'+x+' TODAY</span>' //DD added

if (x!='&nbsp;') t+='<td class="days">'+x+'<br><center> <a href="http://library.newpaltz.edu/library_hours.html">'+ thehours + '</a></center></td>';
else t+='<td> </td>'
if(((i)%7==0)&&(i<36))t+='</tr><tr align="center">';

}
//document.write ("<br>",m," ",g,"<br>")
++m;
t+='</tr></table></div></center><br class="breakhere">';
}
}
return t;
}
// set function to trim junk characters from any variable for matching via if statements
function trim(stringToTrim) { 	return stringToTrim.replace(/^\s+|\s+$/g,"");}

//get hours out of data table
function libhours(HoursOfTheDay, ShowRef, ShowLate, alinebreak) {
var mydate = HoursOfTheDay;

var foundIt = "";
var theline = "0\tNA\tHours Not Set\t0\t0\t0\t0\t0\t0";
// look through the array for the simple date asign the line it is on to a variable
for ( var i=0, len=ss.length; i<len; ++i ){
  foundIt = ss[i].match(mydate);
  if (foundIt == mydate){
   theline = ss[i]; 
//document.write (mydate + foundIt);
//document.write ("<br>");
//document.write (theline);
//document.write ("<br>");
  }
}

//split the line from the table string by the tab character into an array then
// assign each field to its correct variable 

   var datastring, datastringS;
   // Split at each tab character.
   datastringS = theline.split("\t");
    var LineNum = datastringS[0] + " ";
    var linedate = datastringS[1] + " ";
    var DayType = datastringS[2] + " ";
    var dayoWeek = datastringS[3];
    var LibOpen = datastringS[4] + " ";
    var LibClose = datastringS[5];
    var RefOpen = datastringS[6];
    var RefClose = datastringS[7];
    var Latenight = datastringS[8];

// Check to see if the library is open format line accordingly
  if (dayoWeek=="0"){ var  Thelibhours =("<b>" +DayType + "</b><br />");}
  else if (trim(LibOpen) == "Closed")    {  var  Thelibhours =("<b>" +DayType + "</b><br />"); }
  else { var Thelibhours =(DayType + alinebreak +"<b>" + LibOpen + "-" + LibClose + "</b><br />");}
// Check to see if refdesk is open format line accordingly
   if (dayoWeek=="0"){ Thelibhours += alinebreak;}
   else if (ShowRef=="0"){ Thelibhours += alinebreak;}
   else if (trim(RefOpen) == "0") {Thelibhours +=(" ");}
   else  { Thelibhours += ("Reference" + alinebreak +  RefOpen + "- " + RefClose + "<br />");}
// Check to see if the latenight room is open format line accordingly
  var itisclosed = "Closed"
  if (dayoWeek=="0"){ Thelibhours += alinebreak;}
  else if (ShowLate=="0"){ Thelibhours += alinebreak;}
  else if (trim(Latenight) == itisclosed) {Thelibhours += ("Late Room " +  trim(Latenight) + "<br />");}
  else { Thelibhours += ("Late Room " + alinebreak + "until " +  trim(Latenight) + "<br />");}
return Thelibhours;
} 