/* --------------------------------------------------*/
/* ---------->>> AUTHOR: Sarah Phinney <<<-----------*/
/* ---------->>> Revised May 24, 2010 <<<-----------*/
/* --------------------------------------------------*/

// Gets the navigation div with the ID of links
// and populates it with text and href tags needed to make the navigation menu.
// Then calls the lastModified function.
window.onload = function() {
  var linkNode = document.getElementById("links");
  var links = new Array();
  links[0] = '<a href="default.htm">PC Online Home</a>';
  links[1] = '<a href="orient/">Is Online for You?</a>';
  links[2] = '<a href="outlines/">Course Outlines</a>';
  links[3] = '<a href="moodle/">Moodle Guide</a>';
  links[4] = '<a href="help/">Technical Help</a>';
  links[5] = '<a href="http://inside.portervillecollege.edu/">Course Login</a>';
  for (var i=0; i<linkNode.children.length; i++) {
    linkNode.children[i].innerHTML = links[i];
  }
  lastModified();
}

// Determines the date the page was last modified
// and writes that date to the page in the paragraph with the id of date.
// Then calls the getCookie function if the page is the Learning Moodle title.
function lastModified() {
  var updated = new Date(document.lastModified);
  var m = updated.getMonth() + 1;
  var d = updated.getDate();
  var y = updated.getFullYear();
  var updatedText = "Last updated on " +m+ "/" +d+ "/" +y;
  var dateNode = document.getElementById("date");
  dateNode.innerHTML = updatedText;
  if(document.title=='Porterville College Online Courses: Moodle Guide') {getCookie();} //ensures the getCookie function runs on page load
}

// Applies to the page - moodle/default.htm
// Is called from the onclick event from links on the page.
// Sets the cookie for each click on a link for each of the steps
// Cookie will expire in seven days
// Then calls the getCookie function
function setCookie(cName, cValue) {
  var exp = new Date();
  exp.setDate(exp.getDate()+7);
  document.cookie = cName+ "=" +escape(cValue)+ ";" +((exp==null) ? "" : "; expires=" +exp);
  getCookie(); 
}


// Applies only to the page with the title - Porterville College Online Courses: Learning Moodle
// Gets the cookies stored from the function setCookie
// If the cookie has been set to 'done'
// it replaces the box before that step with a checked box.
// The for loop has hard coded length dependent on the number of steps 
// so if steps increase, the value to test must increase.
function getCookie() {
  if (document.cookie.length>0) {
    for(var i=0; i<8; i++) {
      var cookies = new Array();
      cookies[i] = 'step'+i;
      var startCookie = new Array();
      startCookie[i] = document.cookie.indexOf(cookies[i]) +1;
      if (startCookie[i]>0) {
        var cookieValuePos = new Array();
        cookieValuePos[i] = document.cookie.indexOf("=", startCookie[i]) +1;
        var cookieEnd = new Array();
        cookieEnd[i] = document.cookie.indexOf(";",startCookie[i]);
        var cookieValue = new Array();
        cookieValue[i] = document.cookie.substring(cookieValuePos[i],cookieEnd[i]);
        if (unescape(cookieValue[i])=='done') {document.getElementById(cookies[i]).innerHTML = "&#9745;";}
      }
    }
  }
}
