//Javascript functions for checking for mandatory fields in forms, as well as validity and conformity of data entered
//against pre-determined criteria


function isGroup(element)
{
  return (element.type == 'radio' || element.type == 'checkbox');
}

function orderGroupElements(e1, e2)
{
  if (e1.name < e2.name)
    return (-1);
  if (e1.name > e2.name) 
    return 1;
  if (e1.name == e2.name) 
    return 0;
}

function checkGroupMandatory(groupElements, group)
{
  var element, foundSelectedElement = false; 
  
  for (var i = 0; i < groupElements.length; i++) {
    element = groupElements[i];

    if (element.name == group && element.checked) {
      foundSelectedElement = true;
      break;
    }

<!-- The list is sorted according to group, so there is no need to search once a "larger" group is found-->
    if (element.name > group) 
      break;
  }

  return foundSelectedElement;
}




function checkForm(form)
{
  var element, omissionMessage, invalidityMessage, disapprovalMessage;

  var groupElements = new Array();
  var singleElements = new Array();
  
<!-- Separate form elements into an array of group elements and an array of single elements -->  
  
  var groupCounter = 0, singleCounter = 0;
  for (var i = 0; i < form.length; i++) {
    element = form.elements[i];
    if (isGroup(element)) 
      groupElements[groupCounter++] = element;
    else
      singleElements[singleCounter++] = element;
  }
  
  groupElements = groupElements.sort(orderGroupElements);   <!-- sort group elements according to group for convenience -->

  <!-- First deal with the group elements -->

<!--For the sake of optimization, make sure that isMandatory is set to true in just one (any one) control per individual group-->
  for (var i = 0; i < groupElements.length; i++) {
    element = groupElements[i];

    if (element.isMandatory != undefined  && element.isMandatory.toString() == 'true' && !checkGroupMandatory(groupElements, element.name)) {
      omissionMessage = element.omissionMessage;
      if (omissionMessage == undefined) 
        omissionMessage = 'This field may not be left empty';
      alert(omissionMessage);
      return false;
    }
  }

  for (var i = 0; i < singleElements.length; i++) {
    element = singleElements[i];  
      
    if (element.isMandatory != undefined  && element.isMandatory.toString() == 'true' && !checkSingleMandatory(element)) {
      omissionMessage = element.omissionMessage;  	
      if (omissionMessage == undefined) 
        omissionMessage = 'This field may not be left empty';
      fixElement(element, omissionMessage);
      return false;
    }
<!-- If function reaches this point, then one of the following holds: -->

<!--        - the element/field is mandatory and a value has been entered, in which    -->
<!--          case we should proceed with its validation    -->
<!--        - the element is non-mandatory and a value has been entered, so we should    -->
<!--          proceed with the validation     -->
<!--        - the element is non-mandatory, but no value has been entered, so the     -->
<!--          validation should not proceed     -->
     
<!--       So the validation should only proceed if element.value != '' -->

    if (element.value != '' && element.isToBeValidated != undefined && element.isToBeValidated.toString() == 'true' && !checkValidity(element)) {
      invalidityMessage = element.invalidityMessage;
      if (invalidityMessage == undefined) 
        invalidityMessage = 'The input for this field is invalid';
      fixElement(element, invalidityMessage);
      return false;
    }
    
    if (element.value != '' && element.isToBeApproved != undefined && element.isToBeApproved.toString() == 'true' && !checkApproval(element)) {
      disapprovalMessage = element.disapprovalMessage;
      if (disapprovalMessage == undefined) 
        disapprovalMessage = 'The input for this field is not approved';
      fixElement(element, disapprovalMessage);
      return false;
    }
  } 

<!-- if function reaches this point, then all is well: all mandatory fields have been entered, all inputs are valid and approved -->
  return true;      
}

<!--If you need to check a new type of field, add it here-->
function checkSingleMandatory(element) 
{
  if (element.type == 'text' || element.type == 'textarea')
    return (element.value != '');
   
  if (element.type == 'select-one')  
    return (element.options[element.selectedIndex].text != '');  <!-- covers single selection boxes with or without an empty selection -->
     
  if (element.type == 'select-multiple') 
    return (element.selectedIndex != -1);

  return true;
}

function checkValidity(element)
{
  var validityPattern = element.validityPattern;
  var regularExpression;
  
  if (validityPattern != undefined) {
    regularExpression = new RegExp(validityPattern);
   
    if (!(regularExpression.test(element.value)))  
      return false;
    else 
      return true;
  }
 
  return true;
}

function checkApproval(element)
{
<!--placeholder for functionality that is particular to the setting-->
  return true;

}


function fixElement(element, message)
{
  alert(message);
  element.focus();
}

