
var user;
function usercheck(callback) {
  $.ajax({
    url:'/cgi-bin/lplookup.pl',
    data:{ 'login':getCookie('l'),'pw':getCookie('p') },
    type:'POST',
    success:function(udata) {
      eval('user = '+udata);
      if ((user.login == '') || (user.epw == '')) {
        document.location.href = '/newreg.shtml';
      }
      else {
        if (callback) { callback(); }
      }
    }
  });
}

function loggedinmsg() {
  var l = getCookie('l');
  if (l) {
    $('#ablogout').click(function() {
      deleteCookie('l');
      deleteCookie('p');
      window.location.reload(false);
      return(false);
    });
    $('#abloginstatus span').html('You are logged in as '+l+'. ');
    $('#abmyaccount,#ablogout').show();
  }
  else {
    $('#ablogin,#absignup').show();
  }
  $('#abloginstatus').show();
}

function loadoptionlist(formelm,optlist,offset) {
  numopt = formelm.options.length;
  for (i = (numopt - 1); i >= offset; i--) {
    formelm.options[i] = null;
  }
  for (i = 0; i < optlist.length; i++) {
    formelm.options[i+offset] = new Option(optlist[i],optlist[i]);
  }
  formelm.disabled = false;
}

function popup(pagename) {
//  toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,height=455,width=625'
  pagename = window.open('/'+pagename+'.shtml',pagename,'width=650,height=450,resizable=0,location=0,menubar=0,toolbar=0,scrollbars=1');
  pagename.focus();
}

function numericSort(a1,a2) {
  return parseInt(a1[1]) > parseInt(a2[1]) ? -1 : parseInt(a1[1]) < parseInt(a2[1]) ? 1 : 0;
}

function statsSort(a1,a2) {
  return parseInt(a1[0]) > parseInt(a2[0]) ? -1 : parseInt(a1[0]) < parseInt(a2[0]) ? 1 : 0;
}

function yearSort(a1,a2) {
  return parseInt(a1) > parseInt(a2) ? -1 : parseInt(a1) < parseInt(a2) ? 1 : 0;
}

function convertstuff(convtext) {
  return(convtext.replace(/a\/c /ig,'AC ').replace(/ a\/c/ig,' AC').replace(/[ \/]/g,'_').replace(/[^\-\w]/g,'').replace(/__/g,'_').replace(/_-_/g,'-'));
}

function restrictlength(fullstr,strmax) {
  if (fullstr.length > strmax) {
    searchstr = fullstr.substring((strmax - 10),(strmax + 10));
    cutpos = (searchstr.indexOf(' ') + (strmax - 10));
    return(fullstr.substring(0,cutpos)+'....');
  }
  else { return(fullstr); }
}

var mooArray = ['moo.','hi!','drink milk.','please watch out for cows when driving.','lactose intolerant?','milk me!','AutoBeef is udderly cool!','my feet hurt.','got milk?','car got you down? try a cow!','holy cow!','go ahead, have a cow.','beef. it\'s what\'s for dinner.','save a cow, eat a chicken.','I wish I was a horse.','report cow problems too.','fight bovine abuse.','four stomachs, same !@#$% grass.','I love Meg!','Hi Nikki! Hi Mandy-Sunflower! Hi Jake!!','cow-a-bunga!!','my jacket is leather.'];
function moo(imgRef) {
  var moonum = (Math.floor(Math.random() * mooArray.length));
  imgRef.title = mooArray[moonum];
}

function setCookie(name,value,expires,path,domain,secure) {
  var today = new Date();
  today.setTime( today.getTime() );
  if (expires) { expires = (expires * 1000 * 60 * 60 * 24); }
  var expires_date = new Date((today.getTime() + expires));
  document.cookie = name+'='+escape(value) +
    ((expires)?';expires='+expires_date.toGMTString():'') +
    ((path)?';path='+path:';path=/') +
    ((domain)?';domain='+domain:'') +
    ((secure)?';secure':'');
}

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 = dc.indexOf(";",begin);
  if (end == -1) { end = dc.length; }
  return(unescape(dc.substring(begin + prefix.length,end)));
}

function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name+'=' +
      ((path)?';path='+path:';path=/') +
      ((domain)?';domain='+domain:'') +
      ';expires=Thu, 01-Jan-70 00:00:01 GMT';
  }
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0) { date.setTime(date.getTime() - skew); }
}
