//  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
//  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
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;
}
//
//  Function to create or update a cookie.
//
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" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//
//--------------------------------------
//--------------------------------------
// PPP - SAGE II javascript

browser = (((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 3 )) || ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 3 )))
//internetV3 = (((navigator.appName == "Microsoft Internet Explorer") || (navigator.appName == "Netscape") || (navigator.appName == "Opera")  || (navigator.appName == "Mozilla")) && (parseInt(navigator.appVersion) >= 3 ))
window.top.name="sage";

SetCookie("launch", "defaultblank", "", "/");
medCenter=GetCookie("medctr");
e_Mail=GetCookie("email");
alertStr="";


if (medCenter!=null) {
   alertStr+=medCenter+":";
}else {
   medCenter="";
}

if (e_Mail!=null) {
   alertStr+=e_Mail;
}else {
   e_Mail="";
}


function reDisplay() {
  document.login.med_center.value=medCenter;
  document.login.email.value=e_Mail;
  document.login.user_name.focus();
}  // end of reDisplay()


function copyrightyr() {
     brsrType=navigator.appName;
     brsrMSIE=(navigator.appName == "Microsoft Internet Explorer");
     brsrNS=(navigator.appName == "Netscape");
     var timestamp=document.lastModified;
     var timesecs=Date.parse(timestamp)
     var datetime=new Date();
     datetime.setTime(timesecs);
     if (brsrMSIE) {
        document.write(datetime.getYear());     
     } else {
        document.write(datetime.getYear()+1900);
     }
}  // end of copyrightyr()


function chkFields(form) {
  // save current cookies for sagediagram.com...
  // called from submit button...
  
  if (form.user_name.value.length<2) {
     alert("'user name' is a required field...");
     return false;
  }
  if (form.med_center.value.length<2) {
     alert("'medical center' is a required field...");
     return false;
  }
  if (form.email.value.length<2) {
     alert("'email' is a required field...");
     return false;
  }
  return true;
}


function getSage(form) {
  // save current cookies for sagediagram.com...
  // called from submit button...
  if (form.user_name.value.length<2) {
     alert("'user name' is a required field...");
     return false;
  }  
  if (form.med_center.value.length<2) {
     alert("'medical center' is a required field...");
     return false;
  }
  if (form.email.value.length<2) {
     alert("'email' is a required field...");
     return false;
  }

  var expdate = new Date ();
  FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
  expdate.setTime (expdate.getTime() + (90 * 24 * 60 * 60 * 1000)); // 90 days from now 
  SetCookie ("medctr", form.med_center.value, expdate, "/");
  SetCookie ("user", form.user_name.value, expdate, "/");
  SetCookie ("email", form.email.value, expdate, "/");

  SetCookie ("launch", "native", "", "/"); 

  if (form.getversion[0].checked) {
    SetCookie ("launch", "attempted", "", "/");
    form.sageversion.value="attempted SAGE285";
    form.subject.value="attempted access to SAGE IIc...";
  }
  if (form.getversion[1].checked) {
    SetCookie ("launch", "native", "", "/"); 
    form.sageversion.value="SAGE221";
    form.subject.value="native access to SAGE II...";
  }
  if (form.getversion[2].checked) {
    SetCookie ("launch", "plugin", "", "/");
    form.sageversion.value="PLUGIN";
    form.subject.value="plugin access to SAGE II...";
  }

  open('sagestart.htm', 'sage2', 'menubar=no,directories=no,location=no,sizable=yes,toolbar=no,scrollbars=yes,status=yes,width=750,height=575');
  
  window.location.replace("sage2.htm");
  
  return true;
} // end of getSage()

