<!--

// General

function back() {
  history.go(-1);
}

function backToMenu() {
  this.location.href = "menu.php";
}

function checkDate(dayIn, monthIn, yearIn) {
  var myMonth    = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  var myDateStr  = dayIn + ' ' + myMonth[monthIn] + ' ' + yearIn;

  // Using form values, create a new date object using the setFullYear function
  var myDate = new Date();
  myDate.setFullYear(yearIn, monthIn, dayIn);

  return myDate.getMonth() == monthIn;
}

function showEditInstruments(personNo) {
  window.open('member_instruments.php?personNo='+personNo, '', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600');
}

function showMemberDetails(personNo) {
  window.open('member_details.php?personNo='+personNo, '', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=800');
}



// Login

function initFocus() {
  if (document.loginform) {
    if (document.loginform.MemberNo) {
      document.loginform.MemberNo.focus();
    }
  }
}

function loginAcceptBoxClick() {
  if (document.loginform) {
    document.loginform.submit.disabled = !document.loginform.acceptbox.checked;
  }
}

function showObligationPopup() {
  window.open('Obligation to Confidentiality.pdf', '', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=512,height=384');
}



// Membership Details Editing
function setOccupationRadioClick() {
  if (document.editform) {
    document.editform.occupation.disabled      = document.editform.occupChoice[1].checked;
    document.editform.occupationOther.disabled = document.editform.occupChoice[0].checked;
  }
}



// Transfer Listing

function submitTransferListForm() {
  if (document.selectform) {
    var myDay   = document.selectform.fromDay.value;
    var myMonth = document.selectform.fromMonth.value - 1;
    var myYear  = document.selectform.fromYear.value;

    var date1ok = checkDate(myDay, myMonth, myYear);

    myDay   = document.selectform.toDay.value;
    myMonth = document.selectform.toMonth.value - 1;
    myYear  = document.selectform.toYear.value;

    var date2ok = checkDate(myDay, myMonth, myYear);

    if (date1ok && date2ok) {
      document.selectform.submit();
    } else {
      dateMsg.innerHTML = "";
      if (!date1ok) dateMsg.innerHTML = "<br />From Date is invalid.";
      if (!date2ok) dateMsg.innerHTML = dateMsg.innerHTML + "<br />To Date is invalid.";
    }
  }  
}



// Password Change

function submitPasswordForm() {
  if (document.passwordchangeform) {
    if (verifyPasswordChange()) {
      document.passwordchangeform.submit();
    } else {
      passMsg.innerHTML = "<b>Unable to submit form</b>: Please make sure your password meets the above criteria and that you have retyped it correctly. <br /><br />";
    }
  }
}

function verifyPasswordChange() {
  // Get the password values
  var pass1 = document.passwordchangeform.newPassword1.value;
  var pass2 = document.passwordchangeform.newPassword2.value;

  // Password field values must match
  var match = (pass1 == pass2);

  // Password must only contain valid characters
  var passReg = /^[0-9A-Za-z]{8,20}$/;
  var valid = pass1.match(passReg);

  return (match && valid);
}



// Pastoral Control

function initTotals() {
  if (document.pastform) {
    var count = 1;
    while (calcTotAttend(count)) {
      count++;
    }
  }
}

function calcTotAttend(rowNo) {
  var obTotal = document.getElementById("total" + rowNo);
  var obAdult = document.getElementById("totAdult" + rowNo);
  var obChild = document.getElementById("totChild" + rowNo);
  var obVisit = document.getElementById("totVisitor" + rowNo);

  if (obTotal && obAdult && obChild && obVisit) {
    if (obAdult.value == "0" && obChild.value == "0" && obVisit.value == "0") {
      obTotal.innerHTML = "0";
    } else {
      var totAdult = 0;
      var totChild = 0;
      var totVisit = 0;

      if (obAdult.value != "") {
        totAdult = parseInt(obAdult.value);
      }
      if (obChild.value != "") {
        totChild = parseInt(obChild.value);
      }
      if (obVisit.value != "") {
        totVisit = parseInt(obVisit.value);
      }

      var total = "";
      if (isNaN(totAdult) || isNaN(totChild) || isNaN(totVisit)) {
        total = "ERROR";
      } else {
        total = totAdult + totChild + totVisit;
      }

      if (total == 0) {
        obTotal.innerHTML = "";
      } else {
        obTotal.innerHTML = total;
      }
    }

    return true;
  } else {
    return false;
  }
}

function calcTotFamPoints () {
  if (document.pastform) {
    var obTot = document.getElementById("famPointsTot");
    var obAfr = document.getElementById("famPointsAfr");
    var obEng = document.getElementById("famPointsEng");
    var obXho = document.getElementById("famPointsXho");
    var obLoz = document.getElementById("famPointsLoz");

    if (obAfr.value == "0" && obEng.value == "0" && obXho.value == "0" && obLoz.value == "0") {
      obTot.innerHTML = "0";
    } else {
      var totAfr = 0;
      var totEng = 0;
      var totXho = 0;
      var totLoz = 0;

      if (obAfr.value != "") {
        totAfr = parseInt(obAfr.value);
      }
      if (obEng.value != "") {
        totEng = parseInt(obEng.value);
      }
      if (obXho.value != "") {
        totXho = parseInt(obXho.value);
      }
      if (obLoz.value != "") {
        totLoz = parseInt(obLoz.value);
      }

      var total = "";
      if (isNaN(totAfr) || isNaN(totEng) || isNaN(totXho) || isNaN(totLoz)) {
        total = "ERROR";
      } else {
        total = totAfr + totEng + totXho + totLoz;
      }

      if (total == 0) {
        obTot.innerHTML = "";
      } else {
        obTot.innerHTML = total;
      }
    }
  }

  return true;
}



// Other Form

function onSubmitForm(defaultAction) {
  if (!document.selectform.formatbox) {
    return;
  }

  if (document.selectform.formatbox.value == 'CSV') {
    document.selectform.action = defaultAction + '?format=csv';
  } else {
    document.selectform.action = defaultAction;
  }

  return true;
}

function submitMainForm(action) {
  if (document.mainform) {
    document.mainform.action = action;
    document.mainform.submit();
  }
}

function setCheckedValue(radioObj, newValue) {
  if (!radioObj)
    return;

  var radioLength = radioObj.length;

  if (radioLength == undefined) {
    radioObj.checked = (radioObj.value == newValue.toString());
    return;
  }

  for (var i = 0; i < radioLength; i++) {
    radioObj[i].checked = false;
    if (radioObj[i].value == newValue.toString()) {
      radioObj[i].checked = true;
    }
  }
}


function softReset() {
  // Does not currently work
  CongregationNo.selectedIndex = 1;
  DistrictAreaNo.selectedIndex = 1;
  BishopAreaNo.selectedIndex = 1;
  ApostleAreaNo.selectedIndex = 1;
}

function sundaySchoolChecks() {
  document.selectform.submit.disabled = !(document.selectform.sun1.checked || 
                                          document.selectform.sun2.checked ||
                                          document.selectform.sun3.checked ||
                                          document.selectform.sun4.checked ||
                                          document.selectform.ri1.checked  ||
                                          document.selectform.ri2.checked  ||
                                          document.selectform.ri3.checked  ||
                                          document.selectform.conf.checked);
}

-->