﻿// *********************************************************************
// Function to validate
// *********************************************************************

function ValidateRegExp(pattern, text) {
  var regExp = new RegExp(pattern);
  var match = regExp.exec(text);

  return (match == null) ? false : true;
}

function DisplayError(id) {
  var em = document.getElementById(id);
  if (em != null) {
    em.style.display = 'block';
    if (em.parentNode.className.indexOf('full') > -1) {
      em.parentNode.previousSibling.previousSibling.className = 'error';
    }
    else {
      em.parentNode.className = 'error';
      em.parentNode.previousSibling.className = 'error';
    }
  }
  else {
    alert(id);
  }
}

function DisplayErrorCustom(id) {
  var em = document.getElementById(id);
  if (em != null)
    em.style.display = 'block';
  else {
    alert(id);
  }
}

function ValidateTextBox(pattern, elementId, errorId) 
{
  var element = document.getElementById(elementId);
  var result = ValidateRegExp(pattern, element.value);
  if (!result) 
  {
    DisplayError(errorId);
    element.focus();
  }
  else 
  {
    var em = document.getElementById(errorId);
    if (em != null) 
    {
      em.style.display = 'none';
      em.parentNode.className = 'required';
      em.parentNode.previousSibling.className = 'required';
    }
  }
  return result;
}

// *********************************************************************
// the purpose of this function is to allow the enter key to 
// point to the correct button to click.
// *********************************************************************

function TrapEnterKey(buttonName, e) 
{
  var key = (window.event) ? window.event.keyCode : e.which; ;     //IE, firefox

  if (key == 13) 
  {
    var btn = document.getElementById(buttonName);
    if (btn != null) 
    {
      btn.click();
      event.keyCode = 0
    }
  }
}
