
var page_id = 0 ;
var c_name  = "jmatch" ;
var c_pwd   = "pass" ;
var c_logId = "Visitor" ;
var c_found = 0 ;

// name is a string of the name of your cookie
// value is the value corresponding to name
//
function SetCookie(name,value) {
//  var today_is = new Date("January 1, 2004") ;
  var today_was = new Date("January 1, 2004") ;
//    today_was.setYear(102);
  var expire = "; expires=" + today_was.toGMTString();
  var full_string = c_name + "=" + escape(value) +
   expire + "; path=/" ;
//      alert (full_string);
  document.cookie = full_string ;

}

// returns value of cookie or null if cookie does not exist
//
function GetCookie(name) {
 var result = null;
 var my_cookie = c_name + "=";
 var searchName = " " + c_name + "=";
 var start_cookie = document.cookie.indexOf(searchName);
 var end_cookie;
 // alert("GET:" + name + " AT " + start_cookie );
 if (start_cookie != -1) {
  start_cookie += searchName.length; // skip past name of cookie
  end_cookie = document.cookie.indexOf(";", start_cookie);
  if ( end_cookie == -1 ) end_cookie =
                                        document.cookie.length ;
  result = unescape(document.cookie.substring(start_cookie, end_cookie));
 }
 else
 {
    start_cookie = document.cookie.indexOf(my_cookie);
        if(start_cookie != 0 ) return result ;
        start_cookie += my_cookie.length ;
   end_cookie = document.cookie.indexOf(";", start_cookie);
  if ( end_cookie == -1 ) end_cookie =
                                        document.cookie.length ;
  result = unescape(document.cookie.substring(start_cookie, end_cookie));
}
 return result;
}


function save_user(LogId,pwd)
{
  c_LogId = LogId ;
  c_pwd = pwd ;
  text = LogId + "?" + pwd;
  SetCookie(c_name,text);
if ( LogId > '-' ) { alert("Password saved for "+LogId); }
else { alert("Password deleted"); }
}

function retrieve_user()
{
  var c_text ;
  c_found = 0 ;
  c_text = GetCookie(c_name);
  if(!c_text) return false ;
  l = c_text.length ;
  if ( l < 5 ) return false ;
  a = c_text.indexOf("?",0) ;
  if ( a < 2 ) return false ;
  id = c_text.substring(0,a);
  pwd = c_text.substring(a+1,l);
  if ( pwd.length < 2 ) return false ;
  c_LogId = id ;
  c_pwd = pwd ;
  c_found = 1 ;
  return true ;
}

function log_in_click(f)
{ if(f.pwd.value>' '){f.submit();}else{put_user(f);}}
function pwd_click(f) {if(f.pwd.value<=' '){put_user(f);}}
function  put_user(f)
{if(c_found<1) {retrieve_user();}
 if(c_found>0) {f.nameid.value=c_LogId ; f.pwd.value=c_pwd ;
 f.submit(); return true;}
}

function  put_buttons()
{
  if(c_found<1) {retrieve_user();}
  if(c_found>0) {
document.writeln("<br><center>");
document.writeln("   <input type='button' value='Use my saved name and password' ");
document.writeln("   onClick='put_user()'>");
document.writeln("</center><br>");
 }
}

function  found_buttons()
{
  if(c_found<1) {retrieve_user();}
  if(c_found<1) {return ;}
document.writeln("<center>");
document.writeln("<br>There is a user name saved on this compute<br>");
documemt.writeln("<form name='log_in' action='../match/pw_user.cgi' method='get'>");
document.writeln("  <br><center>");
document.writeln("  <input type='button' value='Log in using my saved name and password' ");
document.writeln("  onClick='put_user()'></form></center><br>");
}
