// Global vars
var LastButtonClicked;
var focusFld;
var lastVal;

// val is the value to assign to hcmd before the post.
function purchase(val) {
  var cmdfld = document.getElementById('hcmd');
  if( !cmdfld) return(false);
  cmdfld.value = 'purch_'+val;
  var el = document.getElementById('form1');
  if( el) el.submit();
}

function recLastBtn(e) {
  LastButtonClicked = e;
}

function getURL() {
  return(unescape(document.URL));
}

function getQuery() {
  retstr = "";
  fullURL = document.URL;
  if (fullURL.indexOf('?') > 0) {
    retstr = fullURL.substring(fullURL.indexOf('?')+1, fullURL.length);
    retstr = unescape(retstr);
  }
  return (retstr);
}

function resizeContainer(objid, w, h) {
  // alert('resize '+w+' '+h);
  el = document.getElementById(objid);
  if( el) {
    el.height = h; el.width = w;
    el.resizedContainer();
  }
}

function recFocus(e) {
  focusFld = e;
  lastVal = e.value;
}

// This allows the php program to process a text link as
// though it were a button.
// form1 must have a hidden field called hcmd for this to work!
//
function backToCameFrom() {
  var cmdfld = document.getElementById('hcmd');
  if( !cmdfld) return(false);
  cmdfld.value = "backtocamefrom";
  var el = document.getElementById('form1');
  if( el) el.submit();
}

// val is the value to assign to hcmd before the post.
function camefrom_jump(val) {
  var cmdfld = document.getElementById('hcmd');
  if( !cmdfld) return(false);
  cmdfld.value = val;
  var el = document.getElementById('form1');
  if( el) el.submit();
}

function validate_email(field) {
  with (field) {
    apos=value.indexOf("@");
    dotpos=value.lastIndexOf(".");
    if (apos<1 || dotpos-apos<2) {
      alert('Invalid email address.'); return false;
    }
    if( value.length - dotpos < 3) {
      alert('Invalid email address!'); return false;
    }
    if( value.indexOf(",") > 0) {
      alert('Invalid email address,'); return false;
    }
    return true;
  }
}

function startTimer(millisecs) {
  var el = document.getElementById("btn_submit");
  if( el) setTimeout('enableSubmit()', millisecs);
}

function enableSubmit() {
  var el = document.getElementById("btn_submit");
  if( el) el.disabled = false;
}

function trim(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
 chars = chars || "\\s";
 return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

// This function returns true if there are any characters in (inChars)
// that are not legal. Returns false if all chars are OK.
function NotLegalChars(inChars, legalChars) {
  var i;
  if( inChars.length < 1 ) return false;
  for( i=0; i < inChars.length; i++) {
    if( legalChars.indexOf(inChars.charAt(i)) == -1) return true;
  }
  return false;
}
function NotAnyLegal(inChars) {
  return NotLegalChars(inChars, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !@#$%&()_-+=\/,.<>:;");
}
function NotAlphaNumPunct(inChars) {
  return NotLegalChars(inChars, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789., #&()-/:");
}
function NotAlphaNumeric(inChars) {
  return NotLegalChars(inChars, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
}
function NotAlphaSpace(inChars) {
  return NotLegalChars(inChars, "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
function NotDigits(inChars) {
  return NotLegalChars(inChars, "0123456789");
}

function showPopMsg(hdln, msg) {
  var el = document.getElementById('popfrm');
  if( el) {
    if( hdln != '') {
      var hd = document.getElementById('pophd');
      if( hd) hd.innerHTML = hdln;
    }
    var hd = document.getElementById('popmsg');
    if( hd) hd.innerHTML = msg;
    el.style.visibility = "visible";
    el.style.left = "240px";
  }
}

function showItem(elname) {
  var el = document.getElementById(elname);
  if( el) {
    el.style.visibility = "visible";
    el.style.display = "inline";
  }
}

function hideItem(elname) {
  var el = document.getElementById(elname);
  if( el) {
    el.style.visibility = "hidden";
    el.style.display = "none";
    if( elname == 'popfrm') {
      el.style.left = "-1000px";
    }
  }
}

function hideMe(el) {
  if( el) el.style.visibility = "hidden";
}

// Returns true if (s) does not contain any of the chars in charSet
function AllLegalChars(s, charSet) {
  if( s.length < 1 || charSet.length < 1) return true;
  for( var i=0; i < charSet.length; i++) {
    if( s.indexOf(charSet.charAt(i)) >= 0) return false;
  }
  return true;
}

function imageSubmit(e) {
  var el = document.getElementById('hcmd');
  if( el) {
    el.value = e.id;
    el = document.getElementById('form1');
    if( el) el.submit();
  }
}

// Set isDelete to 1 to ask the delete message.
function imageConfirm(e, isDelete) {
  msg = 'Are you sure?';
  if( isDelete == 1) {
    msg = 'OK to DELETE this?';
  }
  if( !window.confirm(msg)) return;
  var el = document.getElementById('hcmd');
  if( el) {
    el.value = e.id;
    el = document.getElementById('form1');
    if( el) el.submit();
  }
}

function filterEnterKey(e) {
  var keynum;

  if(window.event) {
    keynum = e.keyCode; // IE
  } else if(e.which) {
    keynum = e.which; // Netscape/Firefox/Opera
  }
  if( keynum != 13) return true;
  return false;
}

function scrollToTop() {
  window.scrollTo(0,0);
}
