// Functions for memberlogin.php
function doPWclick() {
  var em=document.getElementById('loginname').value;
  if (em=='') {
    alert('Your club email address must be entered.');
    return;
  }
  self.location='forgotpassword.php?em='+em;
}

// Functions for library.php
function ApplyFilter() {
  var selector = document.getElementById('selcategory');
  var filter = "";
  if (selector.selectedIndex == 0) {
    self.location='library.php';
  }
  else {
    self.location='library.php?selcategory='+selector.options[selector.selectedIndex].value;
  }
}

// Functions for mycompimages.php
function uploadSubject() {
  var btn=document.getElementById('cmdUploadSubject');
  if (btn) btn.disabled=true;
  btn=document.getElementById('cmdUploadOpen');
  if (btn) btn.disabled=true;
  document.forms.frmUpload.action='mycompimages.php?action=upload&cat=S';
  document.forms.frmUpload.submit();
}
function doChangeTitle(id) {
  var frame=document.getElementById('frametitle');
  frame.src='changeimagetitle.php?itemid='+id;
  var dv=document.getElementById('divtitle');
  dv.style.visibility='visible';
}
function doHideTitle() {
  var dv=parent.document.getElementById('divtitle');
  dv.style.visibility='hidden';
}

function uploadOpen() {
  var btn=document.getElementById('cmdUploadSubject');
  if (btn) btn.disabled=true;
  btn=document.getElementById('cmdUploadOpen');
  if (btn) btn.disabled=true;
  document.forms.frmUpload.action='mycompimages.php?action=upload&cat=O';
  document.forms.frmUpload.submit();
}

// Functions for mycompresults.php
function selectcomp() {
    var s = 'mycompresults.php';
    var ctl1 = document.getElementById('competition');
    s = s+'?competition='+ctl1.options[ctl1.selectedIndex].value;
    self.location = s+'&selectby=C';
}
function selectyear() {
    var s = 'mycompresults.php';
    var ctl1 = document.getElementById('year');
    s = s+'?year='+ctl1.options[ctl1.selectedIndex].value;
    self.location = s+'&selectby=Y';
}
function EnableDisable() {
  var ctl = document.getElementById('optYear');
  var sel1 = document.getElementById('year');
  var sel2 = document.getElementById('competition');
  var s = 'mycompresults.php?selectby=';
  if (ctl.checked) {
  sel1.selectedIndex=0;
    s=s+'Y';
    sel1.disabled=false;
    sel2.selectedIndex=0;
    sel2.disabled=true;
  }
  else {
    s=s+'C';
    sel1.selectedIndex=0;
    sel1.disabled=true;
    sel2.selectedIndex=0;
    sel2.disabled=false;
  }
  self.location=s;
}

// Functions for mygroups.php
function groupsenabledisable() {
  if (document.frmJoin.groupid.selectedIndex<1) {
    document.frmJoin.cmdJoin.disabled=true;
  } else {
    document.frmJoin.cmdJoin.disabled=false;
  }
}

// Functions for sendemail.php
function doSubmit() {
  if (document.frmEmail.subject.value=='' || document.frmEmail.message.value=='') {
    alert ('Subject and message are both required');
  }
  else {
    document.frmEmail.submit();
  }
}

// Functions for memberlist.php
function decision(message, url) {
  if(confirm(message)) location.href = url;
}
function doFilter() {
  document.forms['frmFilter'].action='memberlist.php?action=filter';
  document.forms['frmFilter'].submit();
}
function doOrder() {
  document.forms['frmFilter'].action='memberlist.php?action=orderby';
  document.forms['frmFilter'].submit();
}

// Functions for printablememberlist.php
function CheckWidth() {
  els = document.forms[0].elements;
  total=50;
  for (i=0; i<els.length; i++) {
    ctl = els[i];
    if (ctl.type == 'checkbox' && ctl.checked) {
      ctl2=document.getElementById(ctl.name+'width');
      total = total + parseInt(ctl2.value);
    }
  }
  ctl=document.getElementById('cmdGo');
  if (total > 275) {
    ctl.disabled=true;
    alert('Selected columns will not fit on the page: deselect one or more.');
  }
  else {
    ctl.disabled=false;
  }
}

// Functions for memberadd.php and memberedit.php
function saveMemberSubmit() {	// Check for required fields
  var msg = '';
  var ctl=document.getElementById('txtSurname');
  if (ctl.value=='') {
    msg = msg+'Surname\n';
  }
  ctl=document.getElementById('txtFirstName');
  if (ctl.value=='') {
    msg=msg+'First Name\n';
  }
  ctl=document.getElementById('cmbClass');
  if (ctl.selectedIndex==0) {
    msg=msg+'Member Class\n';
  }
  ctl=document.getElementById('txtDateJoined');
  if (ctl.value=='') {
    msg=msg+'Date Joined\n';
  }
  ctl=document.getElementById('txtFinancialTo');
  if (ctl.value=='') {
    msg=msg+'Financial To\n';
  }

  if (msg=='') {
    ctl=document.getElementById('txtEmail');
    if (ctl.value!='') {
      ctl=document.getElementById('txtPassword');
      if (ctl.value=='') {
        alert('If email is entered, the member must be given a password, so that they can log in to the website.');
        return;
      }
    }
    document.forms['frmMember'].submit();
  }    
  else {
    msg='The following  fields must be entered or selected:\n\n'+msg.substr(0,msg.length-1);
    alert(msg);
  }
}

// Functions for memberrole.php
function DoUpdate(code) {
  document.forms[0].action='memberrole.php?update='+code;
  document.forms[0].submit();
}
function DoEdit(code) {
  document.forms[0].action='memberrole.php?edit='+code;
  document.forms[0].submit();
}
function DoCancel () {
  document.forms[0].action='memberrole.php';
  document.forms[0].submit();
}
function CheckInsert() {
  if (document.forms[0].newCode.value == '' ||
      document.forms[0].newDescription.value == '') {
      alert ('Both Code and Description are required');
      return false;
  }
  return true;
}

// Functions for grouplist.php
function addMember(tagid) {
  var dd1=document.getElementById('member');
  var opt=dd1.options[dd1.selectedIndex];
  var url='grouplist.php?action=addmem&groupid='+tagid+'&mem='+opt.value;
  self.location=url;
}
function groupselected() {
  var dd2=document.getElementById('groupid');
  if (dd2) {
    var url='grouplist.php?groupid='+dd2.options[dd2.selectedIndex].value;
    self.location=url;
  }
}

// Functions for programadd.txt
function TestText() {
  src=document.getElementById("message");
  dst=document.getElementById("testit");
  dst.innerHTML=src.value;
}

// Functions for programedit.php
function doSave() {
  var doc=document.forms.frmProgram;
  doc.action='programedit.php?action=update';
  doc.submit();
}
function doSaveExit() {
  var doc=document.forms.frmProgram;
  doc.action='programlist.php?action=update';
  doc.submit();
}

// Functions for librarymaint.php
function CheckLibData() {
  if (document.forms[0].catnumber.value == '' ||
      document.forms[0].title.value == '' ||
      document.forms[0].category.selectedIndex == 0) {
      alert ('Items require at least a Catalogue Number, Title and Category');
      return false;
  }
  return true;
}

// Functions for librarycats.php
function doLibCatSubmit() {
  document.forms[0].submit();
}

// Functions for filesets.php
function doFileSetSubmit() {
  document.forms[0].submit();
}

// Functions for uploadwinners.php
function refreshWinners() {
  var ctlyy=document.getElementById('txtYear');
  var ctlmm=document.getElementById('cmbMonth');
  location='uploadwinners.php?yy='+ctlyy.value+'&mm='+ctlmm.options[ctlmm.selectedIndex].value;
}

function doWinnerSubmit() {
  var ctl, ctl2;
  ctl=document.getElementById('txtYear');
  ctl2=document.getElementById('cmbMonth');
  if (ctl.value=='' || ctl2.selectedIndex==0) {
    alert("You must select the Year and the Month");
    return;
  }
  document.forms.frmUpload.submit();
}

// Functions for entercompresults.php
function repaintResults() {
    var s = 'entercompresults.php?competition=';
    var ctl1 = document.getElementById('competition');
    var ctl2 = document.getElementById('category');
    if (ctl1.selectedIndex > 0 && ctl2.selectedIndex > 0) {
      s = s+ctl1.options[ctl1.selectedIndex].value;
      s = s+'&category='+ctl2.options[ctl2.selectedIndex].value;
      self.location = s;
    }
}
function submitResult () {
  var ctl1 = document.getElementById('competition');
  var ctl2 = document.getElementById('category');
    var comp = ctl1.options[ctl1.selectedIndex].value;
    var cat = ctl2.options[ctl2.selectedIndex].value;
    document.forms[0].action='entercompresults.php?action=update&competition='+comp+'&category='+cat;
    document.forms[0].submit();
}

// Functions for scorereports.php
function goProgress() {
  var ctl=document.getElementById('year');
  var s='progress.php?year='+ctl.options[ctl.selectedIndex].text;
  window.open(s);
}
function goSummary() {
  var ctl=document.getElementById('competition');
  var s='compsummaryreport.php?comp='+ctl.options[ctl.selectedIndex].value;
  var win=window.open(s);
}
function goMemberSumm() {
  var ctl=document.getElementById('member');
  var ctlyear=document.getElementById('year');
  var ctlcomp=document.getElementById('competition');
  var chk1=document.getElementById('chkyear');
  var chk2=document.getElementById('chkcomp');
  var s='membersummaryreport.php?mem='+ctl.options[ctl.selectedIndex].value;
  if (chk1.checked) s = s+'&year='+ctlyear.options[ctlyear.selectedIndex].text;
  if (chk2.checked) s = s+'&comp='+ctlcomp.options[ctlcomp.selectedIndex].value;
  var win=window.open(s);
}
function goEntrySumm() {
  var ctl=document.getElementById('year');
  var s='entrysummaryreport.php?year='+ctl.options[ctl.selectedIndex].value;
  var win=window.open(s);
}

function yearChange() {
  var chk1=document.getElementById('chkyear');
  var chk2=document.getElementById('chkcomp');
  if (chk1.checked) chk2.checked=false;
}
function compChange() {
  var chk1=document.getElementById('chkcomp');
  var chk2=document.getElementById('chkyear');
  if (chk1.checked) chk2.checked=false;
}

// Functions for files.php
function showFileset() {
  var ctl=document.getElementById('cmbFileset');
  var theset=ctl.options[ctl.selectedIndex].value;
  location='files.php?fileset='+theset;
}
function onFileChanged() {
  var ctl=document.getElementById("uplFile");
  var btn=document.getElementById("cmdUpload");
  var chk=document.getElementById("chkResize");
  if (ctl.value != "") {
    btn.disabled=false;
    chk.disabled=false;
  }
}
function enableFileResize() {
  var chk=document.getElementById("chkResize");
  var ctl=document.getElementById("maxSize");
  ctl.disabled=(!chk.checked);
}
