var requiredMissing = false
function checkRequired(oForm)
{
  requiredMissing = false
  return requiredCompleted(oForm, 0)
}

function requiredCompleted(currentElement, depth)
{
  // recursively look through DOM for <label class="required"> and then check next <input> field after <label> to see if value == ''
  // if value == '' break out of recursion and return false
  if (currentElement) {
    if (currentElement.nodeType == 1 && currentElement.nodeName.toLowerCase() == 'label') {
      if (currentElement.className.indexOf('required') != -1 || currentElement.childNodes[0].nodeValue.toLowerCase().indexOf('email') != -1) {
        if (currentElement.className.indexOf('required') != -1) {
          var emptyNotAllowed = true
        } else {
          var emptyNotAllowed = false
        }
        adjacentElement = currentElement.nextSibling
        while (adjacentElement) {
          if (adjacentElement.nodeType == 1) {
            // check <input type=text> and <textarea>
            if ((adjacentElement.nodeName.toLowerCase() == 'input' && adjacentElement.type == 'text') || adjacentElement.nodeName.toLowerCase() == 'textarea') {
              if (emptyNotAllowed && adjacentElement.value == '') {
                // replace _ with space
                fieldName = adjacentElement.name.replace(/_/g, ' ')
                // make initial capitals
                fieldName = fieldName.replace(/\b[a-z]/g, function (fullPattern) { return fullPattern.toUpperCase() })
                alert('Please enter a value for ' + fieldName)
                adjacentElement.focus()
                requiredMissing = true
              // check email addresses
              } else if (adjacentElement.name.indexOf('email') != -1 && !isEmailValid(adjacentElement)) {
                alert('Please enter a valid email address')
                adjacentElement.focus()
                requiredMissing = true
              } else {
                // speed up recursion by returning at this point because value != '' and no need to check further siblings
                return true
              }
            // check <select>
            } else if (adjacentElement.nodeName.toLowerCase() == 'select') {
              if (adjacentElement.options[adjacentElement.selectedIndex].value == '') {
                // replace _ with space
                fieldName = adjacentElement.name.replace(/_/g, ' ')
                // make initial capitals
                fieldName = fieldName.replace(/\b[a-z]/g, function (fullPattern) { return fullPattern.toUpperCase() })
                alert('Please select a value for ' + fieldName)
                adjacentElement.focus()
                requiredMissing = true
              } else {
                return true
              }
            }
          }
          adjacentElement = adjacentElement.nextSibling
        }
      }
    }
    // Traverse the tree
    var i = 0
    var currentElementChild = currentElement.childNodes[i]
    while (currentElementChild) {
      // Recursively traverse the tree structure of the child node
      if (requiredMissing) {
        return false
      }
      requiredCompleted(currentElementChild, depth + 1)
      i++
      currentElementChild = currentElement.childNodes[i]
    }
    return true
  }
}

function isEmailValid(oEmail)
{
  str = oEmail.value
  r1 = new RegExp('(@.*@)|(\\.\\.)|(@\\.)|(^\\.)')
  r2 = new RegExp('^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$')
  return (!r1.test(str) && r2.test(str))
}

function externalLinks()
{
  if (!document.getElementsByTagName) {
    return
  }
  var anchor
  var anchors = document.getElementsByTagName('a')
  for (var i = 0; i < anchors.length; i++) {
    anchor = anchors[i]
    if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external') {
      anchor.target = '_blank'
    }
  }
}

function multipleOnload()
{
  externalLinks()
}

onload = multipleOnload