function openWin(urlstr) {
  newWindow = window.open(urlstr, 'newwin','width=700,height=500,toolbar=yes,location=yes,scrollbars=yes,resizable=yes');
}

function openPicWin(urlstr,x,y) {
  params = 'width=' + x + ',height=' + y + ',toolbar=no,location=no,scrollbars=no,resizable=yes';
  newWindow = window.open(urlstr, 'newwin',params);
}

function showImage(urlstr,caption) {
	params = "width=500,height=600,toolbar=no,scrollbars=no,resizable=yes";
	params = "width=500,height=600";
        newWindow =  window.open('','newWin',params);
	newWindow.document.clear();
	newWindow.document.close();
	newWindow.document.open("text/html", "replace");
	newWindow.document.write('<html><head>');
	newWindow.document.write('<link type="text/css" rel="stylesheet" href="system3styles.css"/>');
	newWindow.document.write('</head><body><table width="100%">')
	newWindow.document.write('<tr><td width="100%" align=center>');
	newWindow.document.write('<img src="');
	newWindow.document.write(urlstr);
	newWindow.document.write('"/></td></tr><tr><td class="captionText" align="center"><br>');
	newWindow.document.write(caption);
	newWindow.document.write('</td></tr><tr><td align="center" class="captionText">');
	newWindow.document.write('<br><br>');
	newWindow.document.write('<br>&copy;2003 System3 Coaching<br>');
	newWindow.document.write('<a class="captionText" href="javascript:window.close();">close window</a>');
	newWindow.document.write('</td></tr>');
	newWindow.document.write('</table></body></html>');
        newWindow.document.close();

        if (window.focus) {
          newWindow.focus();
        }
}

function writeDate() {
 var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
 var monthsFull = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
 dt = new Date();
 document.write(monthsFull[dt.getMonth()]);
 document.write(" ");
 document.write(dt.getDate());
 document.write(", ");
 document.write(dt.getFullYear());
}

function setSelect(obj,val) {
  for (i=0;i<obj.options.length;i++) {
    if (obj.options[i].text == val) {
      obj.selectedIndex = i;
      break;
    }
  }
}

function setSelectByValue(obj,val) {
  for (i=0;i<obj.options.length;i++) {
    if (obj.options[i].value == val) {
      obj.selectedIndex = i;
      break;
    }
  }
}

function formatUnseparated6CharDate(datestring) {
  flen = datestring.length;
  if (flen < 6) {
    return -1;
  }

  datestring = datestring.substring(0,6);
  months=datestring.substring(0,2);
  years = datestring.substring(2);

  if (years < 1860) 
    return -1;
    
  if ((months < 1) || (months > 12)) {
    return -1;
  }

  retstring = months + "/" + years;
  return retstring;
}

function formatSeparated6CharDate(datestring,sepchar) {
  ind = datestring.indexOf(sepchar);
  if (ind < 0) 
    return -1;
  
  months = datestring.substring(0,ind);
  datestring = datestring.substring(ind + 1);

  years = datestring.substring(0,4);  

  if (years < 1860) 
    return -1;
    
  if ((months < 1) || (months > 12)) {
    return -1;
  }

  retstring = months + "/" + years;
  return retstring;
}

function formatUnseparated8CharDate(datestring) {
  flen = datestring.length;
  if (flen < 8) {
    return -1;
  }

  datestring = datestring.substring(0,8);
  months=datestring.substring(0,2);

  if ((months < 1) || (months > 12)) {
    return -1;
  }

  days = datestring.substring(2,4);
  years = datestring.substring(4);

  if (years < 1860) 
    return -1;
    
  if ((months == 1) || (months == 3) || (months == 5) || (months == 7) || (months == 8) || (months == 10) || (months == 12)) {
    if ((days < 0) || (days > 31)) {
      return -1;
    }
  }

  if ((months == 4) || (months == 6) || (months == 9) || (months == 11)) {
    if ((days < 0) || (days > 30)) {
      return -1;
    }
  }

  if ((months == 2)) {
    if ((years % 4 == 0) && (years % 100 != 0 || years % 400 == 0)) {
      if ((days < 0) || (days > 29)) {
        return -1;
      }
    } else {
      if ((days < 0) || (days > 28)) {
        return -1;
      }
    }
  }

  retstring = months + "/" + days + "/" + years;
  return retstring;
}

function formatSeparated8CharDate(datestring,sepchar) {
  ind = datestring.indexOf(sepchar);
  if (ind < 0) 
    return -1;
  
  months = datestring.substring(0,ind);
  datestring = datestring.substring(ind + 1);

  ind = datestring.indexOf(sepchar);
  if (ind < 0) 
    return -1;

  days = datestring.substring(0,2);
  years = datestring.substring(3);  

  if (years < 1860) 
    return -1;
    
  if ((months < 1) || (months > 12)) {
    return -1;
  }

  if ((months == 1) || (months == 3) || (months == 5) || (months == 7) || (months == 8) || (months == 10) || (months == 12)) {
    if ((days < 0) || (days > 31)) {
      return -1;
    }
  }

  if ((months == 4) || (months == 6) || (months == 9) || (months == 11)) {
    if ((days < 0) || (days > 30)) {
      return -1;
    }
  }

  if ((months == 2)) {
    if ((years % 4 == 0) && (years % 100 != 0 || years % 400 == 0)) {
      if ((days < 0) || (days > 29)) {
        return -1;
      }
    } else {
      if ((days < 0) || (days > 28)) {
        return -1;
      }
    }
  }

  retstring = months + "/" + days + "/" + years;
  return retstring;
}

function format8CharDate(datestring) {
  sepchar = "/";
  if (datestring.indexOf(sepchar) < 0) 
    sepchar = "-";
  if (datestring.indexOf(sepchar) < 0) 
    sepchar = ".";
  if (datestring.indexOf(sepchar) < 0) 
    retval = formatUnseparated8CharDate(datestring);
  else 
    retval = formatSeparated8CharDate(datestring,sepchar);

  return retval;
}

function format6CharDate(datestring) {
  sepchar = "/";
  if (datestring.indexOf(sepchar) < 0) 
    sepchar = "-";
  if (datestring.indexOf(sepchar) < 0) 
    sepchar = ".";
  if (datestring.indexOf(sepchar) < 0) 
    retval = formatUnseparated6CharDate(datestring);
  else 
    retval = formatSeparated6CharDate(datestring,sepchar);

  return retval;
}

function validate8CharDateField(obj) {
  if (obj.value == "" || obj.value == null)
    return;
    
  retval = format8CharDate(obj.value);
  if (retval == -1) {
    alert("Please enter a valid date.\nFormat can be as follows:\n  mmddyyyy\n  mm/dd/yyyy\n  mm-dd-yyyy\n  mm.dd.yyyy");
    obj.select();
    obj.focus();
    return false;
  } else {
    obj.value = retval;
    return true;
  }
}

function validate6CharDateField(obj) {
  if (obj.value == "" || obj.value == null)
    return;
    
  retval = format6CharDate(obj.value);
  if (retval == -1) {
    alert("Please enter a valid date.\nFormat can be as follows:\n  mmyyyy\n  mm/yyyy\n  mm-yyyy\n  mm.yyyy");
    obj.select();
    obj.focus();
    return false;
  } else {
    obj.value = retval;
    return true;
  }
}

function setElementVisible(elementid, visibility) {
  obj = document.getElementById(elementid);
  if (obj != null) {
    if(visibility)
      obj.style.visibility = "visible";
    else
      obj.style.visibility = "hidden";
  }
}

function validateNumberField(obj) {
  numberString = obj.value;
  if (validateNumberString(numberString) == -1) {
    alert("Invalid number value. Formats are 'nnnn' or 'nnnn.nnn'.");
    obj.select();
    obj.focus();
    return false;
  }
  return true;
}

function validateNumberString(numberString) {
  if (numberString >= 0 && numberString < 999999)
    return 0;
  else
    return -1;
}

function showStatus(statusString) {
  status=statusString;
  return true;
}

function overLink(obj) {
  obj.style.backgroundColor='#ffffff';
  obj.style.cursor='hand';
}

function outLink(obj) {
  obj.style.backgroundColor='';
}

function upLink(request) {
  if (request == "") {
    document.location = "/";
  } else {
    document.location = request + ".php";
  }
}

function logDelete(id) {
  if (confirm("Are you sure you want to delete this entry?")) {
    var tf = document.forms[0];
    tf.action="journal/jrnl_db.php";
    tf.entryId.value = id;
    tf.logAction.value="Delete";
    tf.from.value = window.location;
    tf.submit();
  }
}

function remindMe() {
  appform.remind.value = "true";
  appform.action=document.location;
  appform.submit();
}

function hideElement(objname) {
  document.getElementById(objname).style.visibility="hidden";
}

function showElement(objname, offsetX, offsetY) {
  var obj = document.getElementById(objname);
  obj.style.left = document.body.scrollLeft + offsetX;
  obj.style.top = document.body.scrollTop + offsetY;
  obj.style.visibility='visible';
}

function shadeElement(objname, clr) {
  document.getElementById(objname).style.background=clr;
}

function unshadeElement(objname) {
  document.getElementById(objname).style.background='';
}

function swap(nm1,nm2) {
  var obj1 = document.getElementById(nm1);
  var obj2 = document.getElementById(nm2);
  obj1.style.position="absolute";
  obj2.style.position="absolute";
  obj2.style.left=obj1.style.left;
  obj2.style.top=obj1.style.top;
  obj1.style.visibility="hidden";
  obj2.style.visibility="visible";
}

function setUpCalendar(dt) {
  var stDate = new Date(dt);
  var yrNo = stDate.getFullYear();
  var moNo = stDate.getMonth();
  var dyNo = stDate.getDate();
  var stDateStr = parseInt(moNo) + 1 + "/1/" + yrNo;

  var startDay = (new Date(stDateStr)).getDay() - 1;
  if (startDay == -1) {
    startDay += 7;
  }

  document.forms[0].calMonth.selectedIndex = moNo;
  var index = parseInt(yrNo) - 2000;
  document.forms[0].calYear.selectedIndex = index;
  var endDay;

  if (moNo == 3 || moNo == 5 || moNo == 8 || moNo == 10) {
    endDay = 30;
  } else if (moNo == 1) {
    if (yrNo % 4 == 0) {
      endDay = 29;
    } else {
      endDay = 28;
    }
  } else {
    endDay = 31;
  }
  
  for (i = startDay + 1; i < endDay + 1 + startDay; i++) {
    elNm = "b" + i;
    mnth = moNo + 1;
    yr = yrNo;
    document.forms[0].elements[elNm].value = i - startDay;
  }
}

function updateCal() {
  var moNo = parseInt(document.forms[0].calMonth.value) + 1;
  var yrNo = document.forms[0].calYear.value;
  var dyNo = 1;
  dtStr = moNo + "/" + dyNo + "/" + yrNo;
  var dt = new Date(dtStr);
  var startDay = dt.getDay() - 1;
  if (startDay == -1) {
    startDay += 7;
  }
  var endDay;
  if (moNo == 4 || moNo == 6 || moNo == 9 || moNo == 11) {
    endDay = 30;
  } else if (moNo == 2) {
    if (yrNo % 4 == 0) {
      endDay = 29;
    } else {
      endDay = 28;
    }
  } else {
    endDay = 31;
  }
  
  for (i = 1; i<43; i++) {
    elNm = "b" + i;
    nbr = i - startDay;
    if (nbr < 1 || nbr > endDay) {
      document.forms[0].elements[elNm].value = "";
    } else {
      document.forms[0].elements[elNm].value = nbr;
    }
  }
}

function setEntryDate(dayStr) {
  var mo = (parseInt(document.forms[0].calMonth.value) + 1);
  var yr = (parseInt(document.forms[0].calYear.value));
  str = mo + "/" + dayStr + "/" + yr;
  document.forms[0].entryDate.value = str;
}

function setChangeDate(dayStr) {
  var mo = (parseInt(document.forms[0].wcalMonth.value) + 1);
  var yr = (parseInt(document.forms[0].wcalYear.value));
  str = mo + "/" + dayStr + "/" + yr;
  document.forms[0].changeDate.value = str;
}

function calSelect(obj) {
  hideElement('cal');
  document.getElementById('bdy').style.cursor='wait';
  var mo = parseInt(document.forms[0].calMonth.value) + 1;
  var yr = parseInt(document.forms[0].calYear.value);
  var dy = obj.value;
  var dt = mo + "/" + dy + "/" + yr;
  val = document.forms[0].calTarget.value;
  if (val == "changeViewWeek") {
    newUrl = "journal.php?";
    if (document.forms[0].athleteid.value != "" && document.forms[0].athleteid.value != null) {
      newUrl = newUrl + "athleteid=" + 
      document.forms[0].athleteid.value;
    }
    newUrl = newUrl + "&week=" + dt;
    document.location=newUrl;
    return 0;
  } else if (val == "changeEntryDate") {
    //??
  }
}

function calwSelect(id) {
  switch (id) {
    case 'b1':
      obj = document.forms[0].b1;
      break;
    case 'b2':
      obj = document.forms[0].b2;
      break;
    case 'b3':
      obj = document.forms[0].b3;
      break;
    case 'b4':
      obj = document.forms[0].b4;
      break;
    case 'b5':
      obj = document.forms[0].b5;
      break;
    case 'b6':
      obj = document.forms[0].b6;
      break;
  }
  hideElement('cal');
  document.getElementById('bdy').style.cursor='wait';
  var mo = parseInt(document.forms[0].calMonth.value) + 1;
  var yr = parseInt(document.forms[0].calYear.value);
  var dy = obj.value;
  var dt = mo + "/" + dy + "/" + yr;
  val = document.forms[0].calTarget.value;
  if (val == "changeViewWeek") {
    newUrl = "journal.php?";
    if (document.forms[0].athleteid.value != "" && document.forms[0].athleteid.value != null) {
      newUrl = newUrl + "athleteid=" + 
      document.forms[0].athleteid.value;
    }
    newUrl = newUrl + "&week=" + dt;
    document.location=newUrl;
    return 0;
  } else if (val == "changeEntryDate") {
    //??
  }
}

function calmSelect(mthStr) {
  hideElement('cal');
  document.getElementById('bdy').style.cursor='wait';
  var dt = mthStr + "/" + document.forms[0].calYear.value;
  val = document.forms[0].calTarget.value;
  if (val == "changeViewMonth") {
    newUrl = "journal.php?";
    if (document.forms[0].athleteid.value != "" && document.forms[0].athleteid.value != null) {
      newUrl = newUrl + "athleteid=" + 
      document.forms[0].athleteid.value;
    }
    newUrl = newUrl + "&month=" + dt;
    document.location=newUrl;
    return 0;
  } else if (val == "changeEntryDate") {
    //??
  }
}
function changeViewWeek(dt) {
  shadeElement('bdy','#dddddd');
  showElement('cal',200,100);
  document.forms[0].calTarget.value="changeViewWeek";
  setUpCalendar(dt);
}

function changeViewMonth(yr) {
  shadeElement('bdy','#dddddd');
  showElement('cal',200,100);
  document.forms[0].calTarget.value="changeViewMonth";
  document.forms[0].calYear.value=yr;
}

function calCancel() {
  val = document.forms[0].calTarget.value;
  hideElement('cal');
  if (val == "changeViewWeek" || val == "changeViewMonth") 
    shadeElement('bdy','#ffffff');
  else if (val == "changeEntryDate") 
    shadeElement('entry','#ffffff');
}
function calOver(obj) {
  if (obj.value != "")
    obj.style.backgroundColor='#99CCFF';
}
function calOut(obj) {
  if (obj.value != "")
    obj.style.backgroundColor='';
}
function calwOver(id) {
  var obj = document.getElementById(id);
  if (obj != null) {
    switch (id) {
      case 'r1':
        obj.style.backgroundColor='#99CCFF';
        document.forms[0].b1.style.backgroundColor='#99CCFF';
        document.forms[0].b2.style.backgroundColor='#99CCFF';
        document.forms[0].b3.style.backgroundColor='#99CCFF';
        document.forms[0].b4.style.backgroundColor='#99CCFF';
        document.forms[0].b5.style.backgroundColor='#99CCFF';
        document.forms[0].b6.style.backgroundColor='#99CCFF';
        document.forms[0].b7.style.backgroundColor='#99CCFF';
        break;
    
      case 'r2':
        obj.style.backgroundColor='#99CCFF';
        document.forms[0].b8.style.backgroundColor='#99CCFF';
        document.forms[0].b9.style.backgroundColor='#99CCFF';
        document.forms[0].b10.style.backgroundColor='#99CCFF';
        document.forms[0].b11.style.backgroundColor='#99CCFF';
        document.forms[0].b12.style.backgroundColor='#99CCFF';
        document.forms[0].b13.style.backgroundColor='#99CCFF';
        document.forms[0].b14.style.backgroundColor='#99CCFF';
        break;

      case 'r3':
        obj.style.backgroundColor='#99CCFF';
        document.forms[0].b15.style.backgroundColor='#99CCFF';
        document.forms[0].b16.style.backgroundColor='#99CCFF';
        document.forms[0].b17.style.backgroundColor='#99CCFF';
        document.forms[0].b18.style.backgroundColor='#99CCFF';
        document.forms[0].b19.style.backgroundColor='#99CCFF';
        document.forms[0].b20.style.backgroundColor='#99CCFF';
        document.forms[0].b21.style.backgroundColor='#99CCFF';
        break;

      case 'r4':
        obj.style.backgroundColor='#99CCFF';
        document.forms[0].b22.style.backgroundColor='#99CCFF';
        document.forms[0].b23.style.backgroundColor='#99CCFF';
        document.forms[0].b24.style.backgroundColor='#99CCFF';
        document.forms[0].b25.style.backgroundColor='#99CCFF';
        document.forms[0].b26.style.backgroundColor='#99CCFF';
        document.forms[0].b27.style.backgroundColor='#99CCFF';
        document.forms[0].b28.style.backgroundColor='#99CCFF';
        break;

      case 'r5':
        obj.style.backgroundColor='#99CCFF';
        document.forms[0].b29.style.backgroundColor='#99CCFF';
        document.forms[0].b30.style.backgroundColor='#99CCFF';
        document.forms[0].b31.style.backgroundColor='#99CCFF';
        document.forms[0].b32.style.backgroundColor='#99CCFF';
        document.forms[0].b33.style.backgroundColor='#99CCFF';
        document.forms[0].b34.style.backgroundColor='#99CCFF';
        document.forms[0].b35.style.backgroundColor='#99CCFF';
        break;

      case 'r6':
        if (document.forms[0].b36.value != '') {
          obj.style.backgroundColor='#99CCFF';
          document.forms[0].b36.style.backgroundColor='#99CCFF';
          document.forms[0].b37.style.backgroundColor='#99CCFF';
          document.forms[0].b38.style.backgroundColor='#99CCFF';
          document.forms[0].b39.style.backgroundColor='#99CCFF';
          document.forms[0].b40.style.backgroundColor='#99CCFF';
          document.forms[0].b41.style.backgroundColor='#99CCFF';
          document.forms[0].b42.style.backgroundColor='#99CCFF';
          break;
        }
    }
  }
}
function calwOut(id) {
  var obj = document.getElementById(id);
  if (obj != null)
    switch (id) {
      case 'r1':
        obj.style.backgroundColor='';
        document.forms[0].b1.style.backgroundColor='';
        document.forms[0].b2.style.backgroundColor='';
        document.forms[0].b3.style.backgroundColor='';
        document.forms[0].b4.style.backgroundColor='';
        document.forms[0].b5.style.backgroundColor='';
        document.forms[0].b6.style.backgroundColor='';
        document.forms[0].b7.style.backgroundColor='';
        break;
    
      case 'r2':
        obj.style.backgroundColor='';
        document.forms[0].b8.style.backgroundColor='';
        document.forms[0].b9.style.backgroundColor='';
        document.forms[0].b10.style.backgroundColor='';
        document.forms[0].b11.style.backgroundColor='';
        document.forms[0].b12.style.backgroundColor='';
        document.forms[0].b13.style.backgroundColor='';
        document.forms[0].b14.style.backgroundColor='';
        break;

      case 'r3':
        obj.style.backgroundColor='';
        document.forms[0].b15.style.backgroundColor='';
        document.forms[0].b16.style.backgroundColor='';
        document.forms[0].b17.style.backgroundColor='';
        document.forms[0].b18.style.backgroundColor='';
        document.forms[0].b19.style.backgroundColor='';
        document.forms[0].b20.style.backgroundColor='';
        document.forms[0].b21.style.backgroundColor='';
        break;

      case 'r4':
        obj.style.backgroundColor='';
        document.forms[0].b22.style.backgroundColor='';
        document.forms[0].b23.style.backgroundColor='';
        document.forms[0].b24.style.backgroundColor='';
        document.forms[0].b25.style.backgroundColor='';
        document.forms[0].b26.style.backgroundColor='';
        document.forms[0].b27.style.backgroundColor='';
        document.forms[0].b28.style.backgroundColor='';
        break;

      case 'r5':
        obj.style.backgroundColor='';
        document.forms[0].b29.style.backgroundColor='';
        document.forms[0].b30.style.backgroundColor='';
        document.forms[0].b31.style.backgroundColor='';
        document.forms[0].b32.style.backgroundColor='';
        document.forms[0].b33.style.backgroundColor='';
        document.forms[0].b34.style.backgroundColor='';
        document.forms[0].b35.style.backgroundColor='';
        break;

      case 'r6':
        if (document.forms[0].b36.value != '') {
          obj.style.backgroundColor='';
          document.forms[0].b36.style.backgroundColor='';
          document.forms[0].b37.style.backgroundColor='';
          document.forms[0].b38.style.backgroundColor='';
          document.forms[0].b39.style.backgroundColor='';
          document.forms[0].b40.style.backgroundColor='';
          document.forms[0].b41.style.backgroundColor='';
          document.forms[0].b42.style.backgroundColor='';
          break;
        }
    }
}

function calChgMonth(obj) {
  updateCal();
}
function calChgYear(obj) {
  updateCal();
}

function calIncrementMonth() {
  var mo = document.forms[0].calMonth;
  var yr = document.forms[0].calYear;
  if (mo.selectedIndex ==  mo.length - 1 && yr.selectedIndex != yr.length - 1) {
    mo.selectedIndex = 0;
    yr.selectedIndex += 1;
  } else if (mo.selectedIndex < mo.length - 1) 
    mo.selectedIndex += 1;
  updateCal();
}

function calDecrementMonth() {
  var mo = document.forms[0].calMonth;
  var yr = document.forms[0].calYear;
  if (mo.selectedIndex ==  0 && yr.selectedIndex != 0) {
    mo.selectedIndex = mo.length - 1;
    yr.selectedIndex -= 1;
  } else if (mo.selectedIndex > 0) 
    mo.selectedIndex -= 1;
  updateCal();
}

function calmIncrement() {
  var tf = document.forms[0];
  if (tf.calYear.selectedIndex < tf.calYear.options.length - 1) {
    tf.calYear.selectedIndex++;
  }
}

function calmDecrement() {
  var tf = document.forms[0];
  if (tf.calYear.selectedIndex > 0) {
    tf.calYear.selectedIndex--;
  }
}

function updateEntry(ind) {
  ind;
  if (workoutData.length <= ind) {
    alert('There was a problem with the<br>journal. Cancelling action.');
    return -1;
  }

  var tf = document.forms[0];
  tf.entryId.value = workoutData[ind][0];
  tf.entryDate.value = workoutData[ind][1];
  setSelect(tf.entryHours,workoutData[ind][2]);
  setSelect(tf.entryMinutes,workoutData[ind][3]);
  setSelect(tf.entryAmPm,workoutData[ind][4]);
  setSelect(tf.entryDiscipline,workoutData[ind][5]);
  tf.entryDistance.value = workoutData[ind][7];
  setSelect(tf.entryDistanceUnit,workoutData[ind][8]);
  setSelect(tf.entryIntensity,workoutData[ind][9]);
  tf.entryDetails.value = workoutData[ind][10];

  durHr = "00";
  durMin = "00";
  durVal = workoutData[ind][6];
  if (durVal == null)  durVal = "00:00";
  colonInd = durVal.indexOf(":");
  if (colonInd != -1) {
    durHr = durVal.substring(0,colonInd);
    durMin = durVal.substring(colonInd + 1, colonInd + 3)
  }
  tf.entryDurationHr.value = durHr;
  tf.entryDurationMin.value = durMin;

  shadeElement('bdy','#dddddd');
  showElement('entry',200,100);
}

function newEntry(dt) {
  var tf = document.forms[0];
  tf.entryId.value = "0";
  tf.entryDate.value = dt;

  shadeElement('bdy','#dddddd');
  showElement('entry',200,100);
}

function setSelect(obj,val) {
  if (obj == null || val == null) return -1;
  obj.selectedIndex = 0;
  for (i=0;i<obj.options.length;i++) {
    if (obj.options[i].value == val) {
      obj.selectedIndex = i;
      return 0;
    }
  }
}

function journalEntry() {
  //alert("Within journalEntry");
  var tf = document.forms[0];

  //alert("Finished part a");

  if (tf.entryDiscipline.value == "") {
    alert("Please select a discipline.");
    return;
  }

  //alert("Finished part b");

  if (tf.entryDistance.value == "") {
    tf.entryDistance.value = "0";
  }

  //alert("Finished part c");

  if (tf.entryDurationHr.value == "") {
    tf.entryDurationHr.value = "00";
  }

  //alert("Finished part d");

  if (tf.entryDurationMin.value == "") {
    tf.entryDurationMin.value = "00";
  }

  //alert("Finished part e");

//  if (!checkJournalEntryFields())
//    return false;

  //alert("Finished part 1");

  journalCctDtTime(tf.entryDate,tf.entryHours,tf.entryMinutes,tf.entryAmPm,tf.entryDateTime);

  //alert("Finished part 2");

  journalCvtTimeFieldsToSec(tf.entryDurationHr,tf.entryDurationMin,tf.entryAbsoluteDuration);

  //alert("Finished part 3");

  tf.method="POST";
  tf.action="journal/jrnl_db.php";
  tf.logAction.value="Update";
  tf.from.value = document.location;
  tf.submit();
}

function deleteCurrentEntry() {
  var id = document.forms[0].entryId.value;
  if (id == 0) {
    hideElement('entry');
    unshadeElement('bdy');
  } else {
    logDelete(id);
  }
}

function journalCctDtTime(dtObj,hrObj,minObj,amPmObj,dtTimeObj) {
  dateStr = dtObj.value;
  months = dateStr.substring(0,2);
  days = dateStr.substring(3,5);
  years = dateStr.substring(6,10);
  var hrInt = parseInt(hrObj.value);
  mins = minObj.value;
  if (amPmObj.value == "PM" && hrInt < 12) {
    hrInt += 12;
  } else if (amPmObj.value == "AM" && hrInt == 12) {
    hrInt = 0;
  }
  if (hrInt < 10) {
    hrs = "0" + hrInt;
  } else {
    hrs = "" + hrInt;
  }
  sec = "00";
  dtTimeObj.value = years + "-" + months + "-" + days + " " + hrs + ":" + mins + ":" + sec;
}

function journalCvtTimeFieldsToSec(hrObj,minObj,absoluteTimeObj) {
  hours = (hrObj.value * 3600);
  minutes = (minObj.value * 60);
  totalTime = hours + minutes;
  absoluteTimeObj.value = totalTime;
}

function checkJournalEntryFields() {
  var tf = document.forms[0];
  returnValue = validate8CharDateField(tf.entryDate);
  if (returnValue == false)
    return false;

  returnValue = validateIntegerTimeField(appform.DurationHours);
  if (returnValue == false)
    return false;

  returnValue = validateIntegerTimeField(appform.DurationMinutes);
  if (returnValue == false)
    return false;

  returnValue = validateIntegerTimeField(appform.DurationSeconds);
  if (returnValue == false)
    return false;

  returnValue = validateNumberField(appform.Distance);
  if (returnValue == false)
    return false;

  return true;
}

function journalStatsEntry() {
  var tf = document.forms[0];
  tf.method="POST";
  tf.action="journal/jrnl_db_stats.php";
  tf.from.value = document.location;
  tf.logAction.value="Update";
  tf.submit();
  hideElement('stats');
  unshadeElement('bdy');
}

function toggleDailyStats() {
  var tf = document.forms[0];
  tf.method="POST";
  tf.action="journal/jrnl_db_stats.php";
  tf.logAction.value="Toggle";
  tf.from.value = document.location;
  tf.submit();
}

function toggleView(param) {
  var tf = document.forms[0];
  tf.method="POST";
  tf.action="journal/jrnl_db_view.php";
  tf.logAction.value="Toggle";
  tf.viewName.value=param;
  tf.from.value = document.location;
  tf.submit();
}

function editStats(ind) {
  var tf = document.forms[0];
  tf.statDate.value = statsData[ind][0];
  tf.statDateID.value = statsData[ind][1];
  tf.statSleep.value = statsData[ind][2];
  tf.statPulse.value = statsData[ind][3];
  tf.statWeight.value = statsData[ind][4];
  tf.statFatigue.value = statsData[ind][5];
  tf.statSoreness.value = statsData[ind][6];
  tf.statStress.value = statsData[ind][7];
  tf.statDetails.value = statsData[ind][8];

  shadeElement('bdy','#dddddd');
  showElement('stats',200,100);
}
function encryptPasscode(pwObj,chObj) {
  var conStr;
  pwObj.value = hex_md5(pwObj.value);
  if (chObj != null) {
    conStr = pwObj.value + chObj.value;
  } else {
    conStr= pwObj.value;
  }
  var encStr = hex_md5(conStr);
  pwObj.value = encStr;
}
function encryptNewPasscode(pwObj,chObj) {
  var conStr;
  if (chObj != null) {
    conStr = pwObj.value + chObj.value;
  } else {
    conStr= pwObj.value;
  }
  var encStr = hex_md5(conStr);
  pwObj.value = encStr;
}
function secureLogin() {
  var tf = document.forms[0];
  encryptPasscode(tf.password,tf.sessionid);
  tf.method="POST";
  tf.action=document.location;
  tf.authAction.value="LOGIN";
  if (tf.persistLogin != null && tf.persistLogin.checked) {
    tf.persistentLogin.value="True";
  } else {
    tf.persistentLogin.value="False";
  }
  tf.submit();
}
function secureLogout() {
  var tf = document.forms[0];
  tf.method="POST";
  tf.action=document.location;
  tf.authAction.value="LOGOUT";
  tf.submit();
}
function secureRegister() {
  var tf = document.forms[0];
  var nullObj;
  if (tf.regpassword.value != tf.verifypassword.value) {
    alert("The two passwords you entered do not match.\r\nPlease try again.");
    return;
  }
  if (tf.email.value.indexOf("@") < 0) {
    alert("Please enter a valid email address.");
    return;
  }
  tf.verifypassword.value = "";
  tf.verifypassword.disabled = true;
  encryptNewPasscode(tf.regpassword,nullObj);
  tf.method="POST";
  tf.action=document.location;
  tf.submit();
}