// JavaScript Functions used in the Archie class web sites

function daystilldue(dueyear,duemonth,dueday)
{
    //Set the two dates
    var today=new Date();
    var due=new Date(dueyear, duemonth, dueday); //Month is 0-11 in JavaScript
    //Set 1 day in milliseconds
    var one_day=1000*60*60*24;

    // are we already past due?
    //if (today.getFullYear() > due.getFullYear() && 
     //   today.getMonth() > due.getMonth() && today.getDate() > due.getDate())
    if (today > due)
	 {
        return (Math.ceil((today.getTime()-due.getTime())/(one_day))+
        " days past due!");
    }
    else 
	 {
        return (Math.ceil((due.getTime()-today.getTime())/(one_day))+
        " days til due!");
    }
} // daystilldue

function weekstartday(startdate,week)
{
    // split the start date
	 // the format of the start date is yyyy|mm|dd
	 var s = startdate.split("|");
    var today=new Date();
    var sday=new Date(s[0], s[1]-1, s[2]); //Month is 0-11 in JavaScript
    //Set 1 day in milliseconds
    var one_day=1000*60*60*24;
	 var wk = (week-1) * 7 * one_day;
	 today.setTime(sday.getTime() + wk);

	 var m = today.getMonth()+1;
	 var td = week + "&nbsp;" + m + "/" + today.getDate() + "/" + today.getFullYear();
    return (td);
} // weekstartday

function checkclick()
{
	alert("clicked in body");
}
/* set up the path prefix to be put in
front of the references to other parts of the site.
This allows the controls to work on all web pages
in the site regardless of how deep they are.
level 0 is in the site folder.
The access to the base materials will be handled
as needed
*/
function makeprepath(level)
{
	// special case for files in the base
	if(level == -1) {
	   return("../" + getCookie("classid") + "/");
	}
   var prepath="";
   for(i=0; i<level; i++) prepath += "../";
	return prepath;
} // makeprepath

// used at the bottom of every page
function footer(level)
{
	var today = new Date(document.lastModified);
	var prepath = makeprepath(level);
	document.writeln("<hr>");
	document.writeln("Last Updated : " + today.toLocaleString());
	var year = new Date().getFullYear();
	document.write(" | &copy; ");
	document.writeln("<a href=\"" + prepath+ "../base/copy.html\">Copyright</a> " + year + " Kent Archie");
} // footer

function navcontrolstart(level)
{
	var prepath = makeprepath(level);
	document.writeln('<div class="control">');
	document.writeln('<a href="javascript:window.scrollTo(0,0);"><img width=28 height=41 alt="Top Image" src="' + prepath + '../base/images/top.jpg"></a>');
	document.writeln('</div>');
} // navcontrol

function writenavtable(level,sep)
{
	var prepath = makeprepath(level);
	var front='<td><a href="' + prepath;
	var back = '</a>' + sep + '</td>';
   document.writeln(front + 'sched.html">Schedule' + back);
   document.writeln(front + 'asgs/index.html">Assignments' + back);
   document.writeln(front + 'notes/index.html">Notes' + back);
   document.writeln(front + 'examples/index.html">Examples' + back);
   document.writeln(front + 'syll.html">Syllabus' + back);
   document.writeln(front + 'links.html">Links' + back);
   document.writeln(front + 'index.php">News' + back);
   document.writeln(front + '../base/bcard.html">Instructor' + back);
   document.writeln(front + '../base/comments.html">Comments' + back);
	document.ondblclick=checkclick;
} // writenavtable

// print the nav and title table
// the level value tells how many levels up
// to the place where the links are
function navup(level)
{
   title = getCookie("classtitle");
   navcontrolstart(level);

   document.writeln('<center>');
   document.writeln('<table border cellspacing="5" cellpadding="3">');
   document.writeln('<tr>');
   document.writeln('<th colspan=9 style="font-size:14"> ' + title);
   document.writeln('</th>\n</tr>\n<tr>');
	writenavtable(level,"");
   document.writeln('</tr>\n</table>\n</center>\n<hr>');
} // navup

// print the nav and title table in  the footer
// the level value tells how many levels up
// to the place where the links are
function navdown(level)
{
	var prepath = makeprepath(level);
	var basepath = prepath + "../base/";
   document.writeln('<hr><center>');
   document.writeln('<table><tr>');
	writenavtable(level,"|");
   document.writeln('</tr>\n</table>\n</center>');
} // navdown

// ----- cookie code from http://www.webreference.com/js/column8/functions.html

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

