// ----------------------------------------------------------------------
// Javascript form validation routines.
// ----------------------------------------------------------------------

// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}


function padleft(val, ch, num)
{
  var pad = "";

  for (var j=0; j<num; j++)  pad += ch;

  return (pad + val);
}

function padright(val, ch, num){
  var pad = "";

  for (var j=0; j<num; j++)  pad += ch;

  return (val + pad);
}

// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function MyValidateEmail (valfield)
{
  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var TestEmpty = tfld == "";
  if (TestEmpty)
  {
    return true;
  }

  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
//  var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/ ;
  if (!email.test(tfld))
  {
    alert("ERROR: not a valid e-mail address");
    setfocus(valfield);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/ ;
  if (!email2.test(tfld))
  {
    alert("Unusual e-mail address - check if correct");
    return true;
  }
}

// --------------------------------------------
//            validateTelnr
// Validate telephone number
// Returns true if so (and also if could not be executed because of old browser)
// Permits spaces, hyphens, brackets and leading +
// --------------------------------------------

function MyValidateTelnr (valfield)
{
  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var TestEmpty = tfld == "";
  if (TestEmpty)
  {
    return true;
  }

  var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
  if (!telnr.test(tfld)) {
    alert("ERROR: not a valid telephone number. Characters permitted are digits, space ()- and leading +");
    setfocus(valfield);
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  var newtfld = '';
  for (var j=0; j<tfld.length; j++)
    if (!(tfld.charAt(j)==' ' || tfld.charAt(j)=='(' || tfld.charAt(j)==')'  || tfld.charAt(j)=='-'))
    {
      newtfld = newtfld + tfld.charAt(j);
    }

  if (newtfld.length <= 10)
  {
    tfld =  padleft(newtfld, ' ', (10 - newtfld.length));
    newtfld = '';
    for (var j=0; j<tfld.length; j++)
    switch (j)
    {
      case 0: newtfld = newtfld + "(" + tfld.charAt(j); break;
      case 3: newtfld = newtfld + ") " + tfld.charAt(j); break;
      case 6: newtfld = newtfld + "-" + tfld.charAt(j); break;
      default: newtfld = newtfld + tfld.charAt(j)
   }
  }
  valfield.value = newtfld;

  if (numdigits<6)
  {
    alert("ERROR: " + numdigits + " digits - too short");
    setfocus(valfield);
    return false;
  }

  if (numdigits>14)
  {
    alert("ERROR: " + numdigits + " digits - too long");
    return false;
  }
  else
  {
    if (numdigits<10)
    {
      alert("Only " + numdigits + " digits - check if correct");
    }
  }

  return true;
}