// 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));
}

function securityWarning(checkbox){
  if (checkbox.checked == false) return;
  if ( ! AreCookiesEnabled() ) {
    alert('It appears that your browser blocks cookies.\n' +
      'Please enable cookies if you would like to use the "Remember Me" feature.');
    checkbox.checked = false;
    return;
  }
  var msg = "\nCaution!\n"+
    "\nTo prevent unauthorized use do not use this option if you use a computer that can or will be accessed by others, such as a shared computer in a workplace, college or public library.\n"+
    "\nBy clicking OK, you acknowledge this potential security risk.\n"+
    "\nClick CANCEL to choose another security option.\n";
  if (!confirm(msg)){
    checkbox.checked = false;
  }
  return;
}

function AreCookiesEnabled() {
  var expires = new Date();
  expires.setTime(expires.getTime() + 60 * 60 * 1000);
  setCookie("mddiCookieTest", "Test whether cookies are enabled", expires);
  if ( getCookie("mddiCookieTest") == null )
    return false;
  expires = new Date();
  expires.setTime(expires.getTime() - 1);
  setCookie("mddiCookieTest", "Remove test cookie", expires);  
  return true;
}

function AreSessionCookiesEnabled() {
  setCookie("mddiSessionCookieTest", "Test whether session cookies are enabled");
  return getCookie("mddiSessionCookieTest") != null;
}

function doLoginCookie ()
{
  if ( document.frmLogin.RememberMe == null )
    return;
  if (document.frmLogin.RememberMe.checked == true){
    setRememberIdAndPasswordCookie();
  }else{
    unsetRememberIdAndPasswordCookie();
    return false;
  }
  
  return true;
}

function setRememberIdAndPasswordCookie()
{
  var now = new Date();
  now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
  RememberIdAndPasswordCookie(now);
}

function unsetRememberIdAndPasswordCookie()
{
  var expires = new Date();
  expires.setTime(expires.getTime() - 1);
  RememberIdAndPasswordCookie(expires);
}

function RememberIdAndPasswordCookie(expiration) {
  var frm = document.frmLogin;

  if ( frm.user != null )
    setCookie("mddiSaveId", frm.user.value, expiration);
  if ( frm.pw != null )
    setCookie("mddiSavePwd", frm.pw.value, expiration);
  if ( frm.libcard != null )
    setCookie("mddiSaveLib", frm.libcard.value, expiration);
}

function openContactInfo(page){
  var style="height=300,width=350,status=no,toolbar=no,status=no,menubar=no,location=no,titlebar=no,resizable=no";
  window.open(page,'contactinfo',style);
  return;
}
function openTechAssist(page){
  var style="height=130,width=350,status=no,toolbar=no,status=no,menubar=no,location=no,titlebar=no,resizable=no";
  window.open(page,'techassistance',style);
  return;
}
function loginSubmit(){
  doLoginCookie();
  return true;
}

function setUserInfo(){
  var usercookie=getCookie("mddiSaveId");
  var pwcookie=getCookie("mddiSavePwd");
  var libcookie=getCookie("mddiSaveLib");

  usercookie=usercookie!=null?usercookie:"";
  pwcookie=pwcookie!=null?pwcookie:"";
  libcookie=libcookie!=null?libcookie:"";

  var frm = document.frmLogin;
  if ( frm.user != null )
    frm.user.value=usercookie;
  if ( frm.pw != null )
    frm.pw.value=pwcookie;
  if ( frm.libcard != null )
    frm.libcard.value=libcookie;
  if ( frm.user!=null && usercookie!="" && pwcookie!="" || frm.libcard!=null && libcookie!="" )
    frm.RememberMe.checked = true;
  if ( frm.user!=null && usercookie=="" )
    frm.user.focus();
  else if ( frm.libcard!=null && libcookie=="" )
    frm.libcard.focus();
}
