function isNull(obj) {
  if(obj==null || obj=="undefined") return true;
  else return false;
}
function notNull(obj) {
  return !isNull(obj);
}
function isEmpty(val) {
  if(isNull(val) || val=="undefined") return true;
  else if(typeof(val)=="string" && val.length==0) return true;
  else return false;
}
function notEmpty(sVal) {
  // return the opposite of isNull
  var rc=isEmpty(sVal);
  if(notNull(rc)) return !rc;
  else return null;
}
function setEmpty(val) {
  // If the argument is null, set it to the empty string.
  if(isNull(val) || val=="undefined") val="";
  return val;
}
function trim(sVal) {
/** Remove leading and trailing spaces from the argument. */
  if(isNull(sVal)) return "";
  var space=" ";
  while(1) {
   // Left trim
    if(sVal.substring(0, 1)!=space) break;
    sVal=sVal.substring(1, sVal.length); // Remove 1 leading space
  }
  while(1) {
    // Right trim
    if(sVal.substring(sVal.length-1, sVal.length)!=space) break;
    sVal=sVal.substring(0, sVal.length-1); // Remove 1 trailing space
  }
  return sVal;
}
function setEmpty(val, doTrim) {
  if(isNull(val) || val=="undefined") val="";
  else if(doTrim) val=trim(val);
  return val;
}
function isNumber(sVal) { // opposite of built-in function isNaN
  if(sVal.length=0 || isNaN(sVal)) return false;
  return true;
}
function right(str, n) {
  if(n<=0) return "";
  else if(n>String(str).length) return str;
  else {
    var iLen=String(str).length;
    return String(str).substring(iLen, iLen-n);
  }
}
/* quick getElement reference */
function $() {
  var elems=new Array();
  for(var i=0;i<arguments.length;i++) {
    var elem=arguments[i];
    if(typeof(elem)=="string") elem=document.getElementById(elem);
    if(arguments.length==1) return elem;
    elems.push(elem);
  }
  return elems;
}
function getYear() {
  var dt=new Date();
  var year=dt.getFullYear();
  return year;
}
function upper(obj) {
  if(notNull(obj)) obj.value = obj.value.toUpperCase();
}
function busy() {
  document.body.style.cursor="wait";
}
function notBusy() {
  document.body.style.cursor="";
}
function validateContribSearch(theForm) {
  var checkOK="0123456789-.,";
  var checkStr=theForm.as_amount.value;
  var allValid=true;
  var decPoints=0;
  var allNum="";
  var posPnt=0;
  if(checkStr.indexOf('.',0)>0) {
    // Remove any decimal part of the number - keep it an integer
    checkStr=checkStr.substring(0,posPnt);
    theForm.as_amount.value=checkStr;
  }
  for(i=0;i<checkStr.length;i++) {
    ch=checkStr.charAt(i);
    for(j=0;j<checkOK.length;j++) {
      if(ch==checkOK.charAt(j)) break;
    }
    if(j==checkOK.length) {
      allValid=false;
      break;
    }
    if(ch==".") {
      allNum+=".";
      decPoints++;
    }
    else if(ch!=",") allNum+=ch;
  }
  if(!allValid) {
    alert('Please enter only digit characters in the "Amount" field.');
    theForm.as_amount.focus();
    notBusy();
    return false;
  }
  if(decPoints>1) {
    alert('Please enter a valid number in the "Amount" field.');
    theForm.as_amount.focus();
    notBusy();
    return false;
  }
  return true;
}
function submitHandler(f) {
  floatValue=parseFloat(f.as_amount.value)
  if(isNaN(floatValue)) {
    alert("Amount must be a numeric value")
    return false;
  }
  return true;
}
function validateQuery(theForm) {
  if(isNull(theForm)) return false;
  var yr=theForm.as_year.value;
  var valid=validateContribSearch(theForm);
  if(valid) {
    if(yr>=getYear()) theForm.action="/eodb/contribsearch_current.jsp";
    else theForm.action="/eodb/contribsearch.jsp";
    theForm.submit();
    return true;
  } else return false;
}
function submitForm(theForm, basicActionName) {
  if(isNull(theForm)) return false;
  if(isEmpty(basicActionName)) return false;
  busy();
  var currentSuffix="_current";
  var actionExtension=".jsp";
  var currentYear=getYear();
  var queryYear=theForm.as_year.value;
  var action=basicActionName;
  if(queryYear==currentYear || queryYear==(currentYear - 1)) action+=currentSuffix;
  action+=actionExtension;
  theForm.action=action;
  theForm.submit();
  return true;
}
