////////////////////////////////////////////////////////////////////////
//                                                                    //
// cookies.js                                                         //
//                                                                    //
////////////////////////////////////////////////////////////////////////

// asciiArray is used for encoding cookie string.
var asciiArray = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~."

// Variables defined for setting cookie parameters
var exp = new Date()
var min = (60 * 1000)
var hour = (60 * min)
var day = (24 * hour)
var year = (365 * day)


/////////////////
// setCookie() //
/////////////////
function setCookie(name, value, time, ttype) {
	var cookieval = ""
	cookieval += name + "=" + value + "; "
	if (time > 0) {
      	if (ttype == "year"){
			var date = exp.getTime() + (time * year);
         		exp.setTime(date); 
      	}
		else if (ttype == "day") {
			var date = exp.getTime() + (time * day);
         		exp.setTime(date); 
		}
		else if (ttype == "hour") {
			var date = exp.getTime() + (time * hour);
         		exp.setTime(date); 
		}
      	cookieval += "expires=" + exp.toGMTString() + "; ";
	}
      cookieval += "path=/";
	document.cookie = cookieval;
}


////////////////////
// getCookieVal() //
////////////////////
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset)
   if (endstr == -1){
      endstr = document.cookie.length
   }
   return(unescape(document.cookie.substring(offset, endstr)))
}


/////////////////
// getCookie() //
/////////////////
function getCookie(name) {
   var arg = name + "="
   var alen = arg.length
   var clen = document.cookie.length
   var i = 0
   while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
           return getCookieVal(j);
           i = document.cookie.indexOf(" ", i) + 1;
           if (i == 0) break;
   }
   return null;
}


//////////////////////
// cookieCruncher() //
//////////////////////
// Function accepts cookie value string and an operation key
// specifying whether the string is to be encrypted or decrypted.
// asciiArray string is used to alter string based upon a specified
// key; new string is returned to calling function. Key = 2

function cookieCruncher(operation, cookieStr) {
   var value = ""
   var ch = ""
   var i = 0
   var newString = ""

   while (i < cookieStr.length){
      ch = cookieStr.charAt(i)
      value = asciiArray.indexOf(ch)
      if (operation == "decrypt"){
         value += 2
      }
      if (operation == "encrypt"){
         value -= 2
      }
      if (value < 0){
         value += 95
      }
      if (value > 94){
         value -= 95
      }
      newString += asciiArray.charAt(value)
      i++
   }
   return newString
}