// Begin Calendar Functions

// If the date matches one of the special dates defined in the array then return true else return false
function dateIsSpecial(year, month, day) {
     var m = sdays[month];     
     if (!m) return false;
     for (var i in m) {
        if (m[i] == day) { return true; }
     }
     return false;
}

function dateChanged(calendar) {
     // Beware that this function is called even if the end-user only
     // changed the month/year. In order to determine if a date was
     // clicked you can use the dateClicked property of the calendar:

    //alert("Cal Clicked");
     if (calendar.dateClicked) {
         // OK, a date was clicked, redirect to /yyyy/mm/dd/index.php
         var y = calendar.date.getFullYear();
         var m = calendar.date.getMonth()+1; // integer, 0..11
         var d = calendar.date.getDate(); // integer, 1..31
         var winW = 630, winH = 460;  
         if (parseInt(navigator.appVersion)>3) {
            if (navigator.appName=="Netscape") {
                winW = window.innerWidth;
                winH = window.innerHeight;
            }
            if (navigator.appName.indexOf("Microsoft")!=-1) {
                winW = document.body.offsetWidth;
                winH = document.body.offsetHeight;
            }
         }
         var t_d = d+1
         var sdate = Date.parse(y+'/'+m+'/'+t_d) / 1000;
		 var udate = (Date.parse(y+'/'+m+'/'+d)) / 1000; // drop milliseconds to convert to unix time
		 //buildCal(tdate);
		 //selectedDate = tdate; // Save this as the currently selected date
         //  alert(sdate);
         //   var request_string = 'json_server.php?command=get-tracks&sdate='+sdate+'&gap='+gap+'&user='+suser;
		 //document.getElementById("varTest").innerHTML = 'I got here';
		 //document.getElementById("varTest").innerHTML = 'tdate: '+tdate+':'+toDateString(Date(tdate*1000));
		 
         //var request_string = 'json_server.php?command=get-tracks&sdate='+udate+'&gap='+gap+'&user='+suser+'&pt='+spt;
		 setFilters();
		 //var request_string = 'gps-server.php?command=get-tracks&sdate='+udate+filters;
		 var request_string = 'gps-server.php?command=get-tracks&sdate='+sdate+filters;
         //   alert(request_string);
         makeRequest(request_string, gotTracks)	 
     }
}

function ourDateStatusFunc(date, y, m, d) {
     if (dateIsSpecial(y, m, d))
         return "special";
         //return false;
     else
         //return false; // other dates are enabled
         //return true; // return true if you want to disable other dates
         return "disabled";
}

function buildCal(sdate){
    //sdate = sdate/1;
	//document.getElementById("varTest").innerHTML = 'sdate in build cal: '+sdate;
     // Clear the calendar-container DIV so we can rebuild the calendar in the same location
     document.getElementById("calendar-container").innerHTML = ''; 
     // The following calls the function to setup the calendar and passes the the following arguments as an object
     Calendar.setup( 
         {
         flat           : "calendar-container",
         //date           : (sdate * 1000), // unix datetime for last data day converted to javascript date object        
		 date           : (sdate), // unix datetime for last data day converted to javascript date object
         dateStatusFunc : ourDateStatusFunc, // defines name of function which processes special days (i.e. days where data is found)
         cache          : false, 
         weekNumbers    : false,
         align          : "c,L",
         flatCallback   : dateChanged
         }
     );
     
}
// End Calendar Functions