var Days = new Array("", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31");
var Months = new Array("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var MonthList = optionList(Months);
var DayList = optionList(Days);

function uniqueInCheckBoxGroup(checkBox, checkBoxList)
// effects radio-group behaviour for a list of checkboxes
{
  if (document.getElementById(checkBox).checked) {
    for (i=0; i<checkBoxList.length; i++) {
      if (checkBoxList[i] != checkBox) {
        document.getElementById(checkBoxList[i]).checked = "";
      }
    }
  }
}

function tableRow() {
  document.write('</table><table border=1 cellspacing=0 cellpadding=1 width=747 class="standard"><tr>');
}

function textBox(name, label) {
  myLabel = label || name;
  document.write('<tr><td>' + myLabel + ':&nbsp;<td><input name="' + name + '" type="text" size="60" maxlength="128" onBlur="aggregate();">');
}

function bold(someText) {
  return '<b>' + someText + '</b>';
}

function textCell(label) {
  document.write('<td bgcolor="#EBE9DC">' + label);
}

function sizedTextCell(label, width) {
  strLabel = String("label");
  myWidth = width || strLabel.length * 2;
  document.write('<td bgcolor="#EBE9DC" width="' + myWidth + '">' + label);
/*  document.write('<td bgcolor="#EBE9DC" ' + (myWidth==0)?'':(' width="' + myWidth + '"' ) + '>' + label);*/
}

function inputCell(name, size, maxlength, onBlurF) {
  myMaxLength = maxlength || '';
  myOnBlurF = onBlurF || "";
  document.write('<td><input ID="' + name + '" name="' + name + '" type="text" autocomplete="off" size="' + size + '"' + (maxlength?' maxlength = "' + myMaxLength + '"':'') + ((myOnBlurF!="")?" onBlur=\"" + myOnBlurF + '\"':'') + '>');
}

function hidden(name) {
  document.write('<td><input name="' + name + '" type="hidden">');
}

function checkCell(name, onClickF) {
  myOnClickF = onClickF || "";
  document.write('<td  width="20" align="center"><input ID="' + name + '" name="' + name + '" type="checkbox"' + ((myOnClickF!="")?" onClick=\"" + myOnClickF + '\"':'') + '>');
}

function optionList(values) {
// wraps <option> tags around a list for use in a <select> tag
  options = '';
  for (i=0 ; i<values.length; i++) {
    options = options + '<option>' + values[i] + '</option>';
  }
  return options;
}

function popUp(name, options, onChangeF) {
  myOnChangeF = onChangeF || "";
  document.write('<td><select ID=' + name + ' name=' + name + ' size = 1 ' + ((myOnChangeF!="")?" onChange=\"" + myOnChangeF + '\"':'') + '>' + options + '</select>');
}

function disableComboList(comboName, listLength) {
// disables all items of lower preference (higher in the list) than the first unselected item
  for (j=2; j<=listLength; j++) {
    var predicate = false;
    for (i=1; i<j; i++) {
      predicate = predicate || (document.getElementById(comboName + i).selectedIndex == 0)
    }
    document.getElementById(comboName + j).disabled = (predicate)?"disabled":"";
    if (predicate) {
      document.getElementById(comboName + j).selectedIndex = 0;
    }
  }
}

function disableComboListAndAdjoiningFields(comboName, listLength, FieldList) {
// does what it says on the tin (but see disableComboList above)
  disableComboList(comboName, listLength);
  for (j=2; j<=listLength; j++) {
    for (i=0; i<FieldList.length; i++) {
// check that the key field hasn't been included in the list (floors IE)
      if (document.getElementById(comboName + j).name != document.getElementById(FieldList[i] + j).name) {
        if (document.getElementById(comboName + j).disabled) {
          document.getElementById(FieldList[i] + j).value = "";
          document.getElementById(FieldList[i] + j).disabled = "disabled";
        } else {
          document.getElementById(FieldList[i] + j).disabled = "";
        }
      }
    }
  }
}

function isUniqueComboChoice(name, listLength, preferenceIndex) {
//for a list of combos, checks whether the most recent choice is not already selected in one of the other combos
  var predicate = false;
  for (i=1; i<=listLength; i++) {
    if (i != preferenceIndex && document.getElementById(name + preferenceIndex).selectedIndex > 0) {
      predicate = predicate || document.getElementById(name + i).selectedIndex == document.getElementById(name + preferenceIndex).selectedIndex;
    }
  }
  return !predicate;
}

function repopulateCombo(comboName, valueArray) {
  var combo = document.getElementById(comboName);
  combo.options.length=0;
  for (var i=0; i<valueArray.length; i++) {
    combo.options[i] = new Option(valueArray[i]);
  }
}

function findValueIndex(comboName, value) {
  var combo = document.getElementById(comboName);
  for (var i=0; i<combo.options.length; i++) {
    if (combo.options[i].text==value) {
      return i;
    }
  }
  return -1;
}

function logo(include_address) {
  include_address = include_address || 0;
  document.write("<img src=\"CrestOnly.gif\">")
  document.write("<br><span style='font-size:6pt;'>" + (include_address?"<br>191 Falls Road, Belfast,<br>Northern Ireland<br>BT12 6FE.":"") + "</span>")
}

function logo_and_text(include_address) {
  include_address = include_address || 0;
  document.write("<table border=0 cellspacing=0 cellpadding=0 align=right>")
  document.write("<tr>")
  document.write("<td width=181> <h4>ST. MARY’S<br>UNIVERSITY<br>COLLEGE</h4></td>")
  document.write("<td width=84> <img src=\"CrestOnly.gif\"></td>")
  document.write("</tr>")
  document.write("<tr>")
  document.write("<td colspan=2><span style='font-size:6pt;'>A COLLEGE OF THE QUEEN'S UNIVERSITY OF BELFAST" + (include_address?"<br>191 Falls Road, Belfast,<br>Northern Ireland<br>BT12 6FE.":"") + "</span></td>")
  document.write("</tr>")
  document.write("</table>")
}

function fourDigitYear(strYear) {
  if (strYear.length==2) {
    var d = new Date();
    var y = d.getFullYear()
    if (parseInt(strYear)>parseInt(y%100)) {
      return "19" + strYear
    } else {
      return "20" + strYear
    }
  } else {
    return strYear
  }
}

function ClipBoard()
{
holdtext.innerText = getFormString(document.dataForm, true);
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
}

function dmGetFormString(formRef) {
// cribbed from mjw_lib.js, it wraps values up in tags
  for( var x = 0, oStr = ''; formRef.elements[x]; x++ ) {
    if( formRef.elements[x].type ) {
      var oE = formRef.elements[x]; var oT = oE.type.toLowerCase(); var strE = '';
      if( oT == 'text' || oT == 'textarea' || ( oT == 'password' ) ) {
        strE = oE.value.replace(/%/g,'%p').replace(/,/g,'%c');
      } else if( oT == 'radio' || oT == 'checkbox' ) {
        strE = ( oE.checked ? '1' : '' );
      } else if( oT == 'select-one' ) {
        strE = String(oE.selectedIndex);
      } else if( oT == 'select-multiple' ) {
        for( var oO = oE.options, i = 0; oO[i]; i++ ) {
          strE = ( oO[i].selected ? '1' : '' );
        }
      }
      var strOb = String(strE);
      if (strOb.length>0) {
//        oStr += oE.name + ':' + strE;
      oStr += '[' + oE.name + ']' + strE + '[/' + oE.name + ']';
      }
    }
  }
  return oStr;
}

function checkForFieldNamesInTextFields(formRef) {
  for( var x = 0; formRef.elements[x]; x++ ) {
    if( formRef.elements[x].type ) {
      var oE = formRef.elements[x]; var oT = oE.type.toLowerCase();
      if ((oT == 'text' || oT == 'textarea') && oE.value.indexOf('<')>-1 && oE.value.indexOf('>')>-1) {
        for( var y = 0; formRef.elements[y]; y++ ) {
          if( formRef.elements[y].type ) {
            var strFieldName = '<' + formRef.elements[y].name + '>';
            if (oE.value.indexOf(strFieldName)>-1) {
              alert("The text \"" + strFieldName + "\" appears in field \"" + oE.name + "\".\nPlease amend this before submitting the form.");
              return true;
            }
          }
        }
      }
    }
  }
  return false;
}

function recoverOneInput( formRef, oStr, fieldName ) {
// retrieves the value of the combo called fieldName when it was cookie-stored using mwj_lib
  oStr = oStr.split( ',' );
  for (var x = 0, y = 0; formRef.elements[x]; x++) {
    if (formRef.elements[x].type) {
      var oE = formRef.elements[x]; var oT = oE.type.toLowerCase();
      if (oT=='text' || oT=='textarea' || oT=='radio' || oT=='checkbox' || oT=='select-one') {
        if (oE.name==fieldName) return parseInt( oStr[y] );
        y++;
      }
    }
  }
}

function IsNumeric(sText)
{
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;

  for (i = 0; i < sText.length && IsNumber == true; i++) { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) {
      IsNumber = false;
    }
  }
  return IsNumber; 
}
