// HRA User ID Program copyright Peter Hobbs Aug 2004

var utim = 365;
var yrun = "";

// Set main cookie values. Only name+value are mandatory
//
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape(value)
    + ((expires) ? "; expires=" + expires.toGMTString() : "")
    + ((path) ? "; path=" + path : "")
    + ((domain) ? "; domain=" + domain : "")
    + ((secure) ? "; secure" : "");
}

// Fn to return a cookie value, given the name of the cookie.
// Note the use of unescape to decode special chars in the cookie value.
//
function getCookie(Name) {
   var search = Name + "="
   if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value
         if (end == -1) 
            end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      } 
   }
}

function dealWith(namvalue) {
  if (namvalue=="") Delete_Cookie("HRUNAM")
  else if(confirm('Please confirm you have chosen the username \"'+namvalue+'\".')) register(namvalue)
  history.go(0)
}

function Delete_Cookie(name,path,domain) {
  if (getCookie(name)) document.cookie = name + "="
    + ( (path) ? ";path=" + path : "")
    + ( (domain) ? ";domain=" + domain : "")
    + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
  if (getCookie("HRUNAM"))  unregMsg("",utim)
}

// Fn to create a cookie with name "ID" and the value passed to it as an argument.
//
function register(namvalue) {
  var today = new Date()
  var expires = new Date()
  expires.setTime(today.getTime() + 1000*60*60*24*utim)
  setCookie("HRUNAM", namvalue, expires);
  if (!getCookie("HRUNAM") || getCookie("HRUNAM")!=namvalue) unregMsg(namvalue,utim)
}

function unregMsg(namvalue,utim) {
  alert("CHANGE REJECTED:"
	 +"\n\nTo set a change to \"" +namvalue.toUpperCase() +"\" you must be online"
	 +"\nand accept the standard browser cookie sent by the server."
	 +"\n\nThe cookie stores your new information for "+utim+" days, after which"
	 +"\ntime the cookie is automatically erased by your browser.");
}

var yrun = getCookie('HRUNAM')
