var errorMessage

function DirectPersonSearch() {
  searchWindow = window.open('prsnsrch.asp?db=' + dbName + '&search=&return=Profile', 'PersonSearch', 'width=200,height=300,scrollbars=yes,left=142,top=412');
}
function submitForm(fn) {
  document.f.fn.value = fn
  document.f.submit()
}
function submitOrAlert(fn) {
  if ( errorMessage == "" ) {
    submitForm(fn)
  }
  else {
    alert(errorMessage)
  }
}
function submitOnSelect( frm ) {
  frm.submit()
}
function setCookie(name, value) {
  var expDate = new Date()
  expDate.setTime(expDate.getTime() +  (24 * 60 * 60 * 1000 * 31 * 365))
  document.cookie = name + "=" + value + "; expires=" + expDate.toGMTString()
}
function getCookie(name) {
  var s
  var i
  var iEnd
  s = document.cookie
  i = s.indexOf(name + "=");
  if (i == -1) {
    return ""
  }
  else {
    i = s.indexOf("=", i) + 1
    iEnd = s.indexOf(";", i)
    if (iEnd == -1) {
      iEnd = s.length
    }
    return unescape(s.substring(i, iEnd));
  }
}
function help(fileName) {
  helpWindow = window.open('../help/' + fileName + '.htm','help','width=400,height=400,toolbar=no,scrollbars=yes,location=no')
}
function ExecCompFunction(page) {
/*  alert(getCookie("CompetitionId")) */
  document.location = page + '.asp?db=' + dbName + '&c=' + getCookie('CompetitionId');
}
function newCompetition() {
  document.form1.r.value = ""
  submitOnSelect( document.form1 )
}
function setFieldWarning(field) {
/*  field.style.color = "FFFFFF" */
  field.style.backgroundColor = "FFCC33"
}
function setFieldError(field) {
  field.style.color = "FFFFFF"
  field.style.backgroundColor = "FF0000"
}
function setFieldCursor(field) {
  field.focus()
  if ( field.type == "text" ) {
    field.select()
  }
}
function badField(f, msg) {
  setFieldError(f)
  if ( errorMessage == "" ) {
    setFieldCursor(f)
    errorMessage = msg
  }
}

function validateGuernseyNbr(fld) {
  if ( fld.value != "" ) {
    if ( StringType(fld.value) < 0 ) {
      leBadField(fld, "Jumper number must be numeric.")
    } else {
      if ( fld.value > 254 ) {
        leBadField(fld, "Jumper number must not exceed 254.")
      }
    }
  }
}

function listboxPicked(fld) {
  var picked;
  picked = 1;
  if ( fld.value == "0" ) {
    picked = 0;
  } else {
    if ( fld.value == "" ) {
      picked = 0;
    }
  }
  return picked;
}

function listboxCheck(fld, desc) {
  if ( fld.value == "0" ) {
    badField(fld, "You must select a " + desc + ".")
  }
}

function poolsListboxStatus() {
  var r, i, pp, j, op;
  r = -1;
  for (i=0; i<document.f.length; i++) {
    pp = document.f.elements[i];
    if ( pp.name == 'pp[]' ) {
      r = 0;
      op = document.f.elements['op[]'];
      for (j=0; j<pp.options.length; j++) {
        if ((pp.options[j].selected) && (op.options[j].selected)) { r++; }
      }
    }
  }
  return r;
}

function setTeamPage(pageSuffix) {
  document.f.action = "team" + pageSuffix + ".php"
  document.f.submit()
}
function StringType(strParm) {
  var iReturn
  var iStart
  var i
  if (strParm == "") {
    return 0
  }
  if (strParm.charAt(0) == "-") {
    iReturn = -1
    iStart = 1
  }
  else {
    iReturn = 1
    iStart = 0
  }
  for (i=iStart; i<strParm.length; i++) {
    if (strParm.charAt(i) < "0") {
      return -2
    }
    if (strParm.charAt(i) > "9") {
      return -2
    }
  }
  return iReturn
}
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == " ") 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == " ")
        strText = strText.substring(0, strText.length-1);

   return strText;
} 
function countSelections(listBox) {
  var i
  var n
  var cnt = 0
  n = listBox.options.length
  for (i=0; i<n; i++) {
    if (listBox.options[i].selected) {
      cnt++
    }
  }
  return cnt
}
function zeroPad(i, width) {
  var s
  s = "00000000" + i
  return s.substr(s.length - width, width)
}

function XXyear4(iYY) {
  if ( iYY > 99 ) {
    return iYY
  }
  return iYY + ( iYY < centuryThreshold ? 2000 : 1900 )
}

function XXisLeapYear(iYear) {
  return (iYear % ((iYear % 100 == 0) ? 400 : 4) == 0)
}

function XXvalidDMY(iDay, iMonth, iYear) {
  var iDIM
  if ( iMonth < 1 ) { return 1 }
  if ( iMonth > 12 ) { return 2 }
  if ( iDay < 1 ) { return 3 }
  if ( iMonth == 2 ) {
    iDIM = isLeapYear(iYear) ? 29 : 28
  } else {
    iDIM = (iMonth==4 || iMonth==6  || iMonth==9 || iMonth==11) ? 30 : 31
  }
  if ( iDay > iDIM ) { return 4 }
  return 0
}

/* The idea behind this function is that it will accept a date as an input string and validate it.
   It expects the date in the format ddXmmXyyyy where X can be " ", ".", "-" or "/", the yyyy is optional (it will 
   take the current year if omitted) and the dd and mm can of course be zero suppressed if less than 10.
   It will return the date in the format yyyymmdd or a negative integer if invalid. */
   
function examineInputDate(dt, dels) {
  var s, del, i, ch, rc, arrDate, iYear, iDay, iMonth, iDIM;
  var systemTime = new Date();
  s = trim(dt);
  del = " ";
  rc = 0;
  for (i=0; i<s.length; i++) {
    ch = s.charAt(i);
    if ( (ch < "0") || (ch > "9") ) {
      if ( ch != " " ) {
        if ( dels.indexOf(ch) >= 0 ) {
          if ( del == " " ) {
            del = ch;
          } else {
            if ( ch != del ) { rc = -2; }
          }
        } else {
          rc = -1;
        }
      }
    }
  }
  if ( rc < 0 ) { return rc; }
  arrDate = s.split(del);
  if ( arrDate.length == 2 ) {
    iYear = systemTime.getYear()
  } else {
    if ( arrDate.length == 3 ) {
      iYear = trim(arrDate[2]) - 0;
      if ( iYear < 100 ) { iYear += ( ( iYear < centuryThreshold ) ? 2000 : 1900 ); }
    } else {
      return -3;
    }
  }
  iDay = trim(arrDate[0]) - 0;
  iMonth = trim(arrDate[1]) - 0;
  if ( iMonth < 1 ) { return -4; }
  if ( iMonth > 12 ) { return -5; }
  if ( iDay < 1 ) { return -6; }
  if ( iMonth == 2 ) {
    iDIM = (iYear % ((iYear % 100 == 0) ? 400 : 4) == 0) ? 29 : 28;
  } else {
    iDIM = (iMonth==4 || iMonth==6  || iMonth==9 || iMonth==11) ? 30 : 31;
  }
  if ( iDay > iDIM ) { return -7; }
  return zeroPad(iYear, 4) + zeroPad(iMonth, 2) + zeroPad(iDay, 2);
}

function examineInputTime(tm, dels) {
  var s, del, i, ch, rc, arrTime, iHours, iMinutes;
  s = trim(tm);
  del = " ";
  rc = 0;
  for (i=0; i<s.length; i++) {
    ch = s.charAt(i);
    if ( (ch < "0") || (ch > "9") ) {
      if ( ch != " " ) {
        if ( dels.indexOf(ch) >= 0 ) {
          if ( del == " " ) {
            del = ch;
          } else {
            if ( ch != del ) { rc = -2; }
          }
        } else {
          rc = -1;
        }
      }
    }
  }
  if ( rc < 0 ) { return rc; }
  arrTime = s.split(del);
  if ( arrTime.length == 1 ) {
    iMinutes = 0;
  } else {
    if ( arrTime.length == 2 ) {
      iMinutes = trim(arrTime[1]) - 0;
    } else {
      return -3;
    }
  }
  iHours = trim(arrTime[0]) - 0;
  if ( iHours < 0 ) { return -4; }
  if ( iHours > 23 ) { return -5; }
  if ( iMinutes < 0 ) { return -6; }
  if ( iMinutes > 59 ) { return -7; }
  return zeroPad(iHours * 100 + iMinutes, 4);
}

function ListEditNewKey() {
  document.f.fn.value = ""
  document.f.submit()
}

function xYearCheck(year) {
  if ( StringType(year) != 1 ) {
    return -1
  }
  year = year - 0
  if ( year > 9999 ) {
    return -2
  }
  year = Year4(year)
  return year
}

function XvalidDateStr(strDate) {
  var returnCode
  var strArray
  var dd
  var mm
  var yr
  var ok
  strArray = strDate.split(".")
  if ( strArray.length == 3 ) {
    dd = strArray[0] - 0
    mm = strArray[1] - 0
    yr = YearCheck(strArray[2])
    if ( yr < 0 ) {
      returnCode = 10 - yr
    }
    else {
      if ( ValidDate(dd, mm, yr) ) {
        returnCode = 0
      }
      else {
        returnCode = 20
      }
    }
  }
  else {
    returnCode = 30
  }
  return returnCode
}

function xOneDateToRange(strTextDate) {
  var strArray
  var yr
  var mnth
  var strTemp
  var returnValue
  strArray = strTextDate.split(".")
  if ( strArray.length == 1 ) {
    yr = strArray[0]
    yr = YearCheck(yr)
    if ( yr >= 0 ) {
      returnValue = yr + "0101" + yr + "1231"
    }
    else {
      returnValue = yr - 20
    }
  }
  else {
    if ( strArray.length == 2 ) {
      yr = strArray[1]
      yr = YearCheck(yr)
      if ( yr >= 0 ) {
        mnth = strArray[0]
        if ( ( mnth >= 1 ) && ( mnth <= 12 ) ) {
          strTemp = yr + ZeroPad(mnth, 2)
          returnValue = strTemp + "01" + strTemp + "40"
        }
        else {
          returnValue = -35
        }
      }
      else {
        returnValue = yr - 30
      }
    }
    else {
      if ( strArray.length == 3 ) {
        yr = strArray[2]
        yr = YearCheck(yr)
        if ( yr >= 0 ) {
          mnth = strArray[1]
          dd = strArray[0]
          if ( ValidDate(dd, mnth, yr) ) {
            strTemp = yr + ZeroPad(mnth, 2) + ZeroPad(dd, 2)
            returnValue = strTemp + strTemp
          }
          else {
            returnValue = -45
          }
        }
        else {
          returnValue = yr - 40
        }
      }
      else {
        returnValue = -11
      }
    }
  }
  return returnValue
}
function xDateInputToRange(strTextDate) {
  var iHyphenPosn
  var strDate11
  var strDate12
  var strDate21
  var strDate22
  var iRC1
  var iRC2
  var returnValue
  iHyphenPosn = strTextDate.indexOf("-")
  if ( iHyphenPosn == -1 ) {
    returnValue = OneDateToRange(strTextDate)
  }
  else {
    iRC1 = OneDateToRange(strTextDate.substr(0, iHyphenPosn))
    iRC2 = OneDateToRange(strTextDate.substr(iHyphenPosn + 1, strTextDate.length - iHyphenPosn))
    if ( ( iRC1 < 0 ) || ( iRC2 < 0 ) ) {
      returnValue = iRC1 * 100 + iRC2
    }
    else {
      if ( iRC1.substr(8,8) <= iRC2.substr(0,8) ) {
        returnValue = iRC1.substr(0,8) + iRC2.substr(8,8)
      }
      else {
        returnValue = -9000
      }
    }
  }
  return returnValue
}
function XcheckDateRange() {
  var dateRange
  document.form1.fromdate.value = 0
  document.form1.todate.value = 0
  if ( document.form1.dr.value != "" ) {
    dateRange = DateInputToRange(document.form1.dr.value)
    if ( dateRange >= 0 ) {
      document.form1.fromdate.value = dateRange.substr(0,8)
      document.form1.todate.value = dateRange.substr(8,8)
    }
    else {
      badField(document.form1.dr, "Date input invalid. RC=" + dateRange + ".")
    }
  }
}


  