// CookieHandler component
// Contains functions that help make cookies easier to use
// 
// Part of the Quality Assessor tool written for CIS 375
//
// Written by Aaron Curley
// Design by Aaron Curley
// Version 1.1
//
// Changes:
// 2/28/2010 - fix for getCookie where split("=") result length was not checked
//

// (helper function, should not be called by outsiders)
// returns the date to set the cookie to expire at given number of days
function getCookieExpireDate(numDays){ 
  var today = new Date();
  var expr = new Date(today.getTime()+numDays*24*60*60*1000);
  return expr.toGMTString();
}

// saves cookie information safely
// if the cookie already exists, its information is replace with the new information
// "name" is the name of the cookie
// "value" is the data to be stored in the cookie
// "numberOfDays" is the number of days till the cookie expires
//     if number of days is not >0, then expire data is set to the end of the session
// Return value: none
function setCookie(name, value, numberOfDays){
  if (numberOfDays > 0){
    document.cookie = (escape(name) + "=" + escape(value) + ";expires=" + getCookieExpireDate(numberOfDays)); 
  }else{
    document.cookie = (escape(name) + "=" + escape(value));
  }
}

// deletes a cookie set by "setCookie()"
// "name" is the name of the cookie to delete
// Return value: none
function deleteCookie(name){
  document.cookie = (escape(name) + "=" + "dud;expires=" + getCookieExpireDate(-1));
}

// returns the data stored in a cookie
// "name" is the name of the cookie to retrieve
// Return value: string - The data stored in the cookie.
function getCookie(name){
  var cookieStr = document.cookie;
 
  // split each cookie into an array
  var separatedCookies=cookieStr.split(';');

  // for each cookie,
  for (var counter = 0; counter < separatedCookies.length; counter++){
    // separate name and value
    var splitCookie = separatedCookies[counter].split('=');
     
    // remove space in front of name, if it exists.
    if (splitCookie[0].charAt(0) == ' '){
      splitCookie[0] = splitCookie[0].substr(1);
    }
    
    // after unescaping the name, is it the one we're looking for?
    if (unescape(splitCookie[0]) == name){
      if (splitCookie.length < 2){
        return "";
      }
      // if so,
      return unescape(splitCookie[1]);
    }
  }
  return "";
}

// tests if SESSION cookies are enabled on this browser
//
// WARNING!  To do the test, this function creates and removes a cookie called "testCookie1154@".  
//    If by some rare chance this cookie exists before this function is called, it will DELETE the original cookie's data!
//
// Return value: boolean - True if cookies work, false if they don't.
//
// NOTE:  This only checks to see if SESSION cookies are allowed.  It DOES NOT test if permanent cookies are permitted.
function testCookie(){
  var teststring = "ak2!334;:";
  setCookie("testCookie1154@",teststring);
  var retstring = getCookie("testCookie1154@");
  if (teststring == retstring){
    deleteCookie("testCookie1154@");
    return true;
  }else{
    return false;
  }
}
