var currentFormPrefix = '';

function FormVerifyDate(field, text)
{
  var ok = 0;

  var d_obj = document.getElementsByName(currentFormPrefix + field)[0];
  var d = new Date(d_obj.value);
  
  if (d == "NaN")
  {
    alert("Invalid date: " + text + "!");
    d_obj.focus();
  }
  else
    ok = 1;

  return ok;
}

function FormRadioRequired(field, text)
{
  var ok = 0;

  var o = document.getElementsByName(currentFormPrefix + field);
  
  for (i = 0; i < o.length; i++)
    if (o[i].checked) ok = 1;

  if (ok == 0)
  {
    alert(text + ' is required!');
    o[0].focus();
  }

  return ok;
}

function FormIntRequired(field, text)
{
  var ok = 1;

  var o = document.getElementsByName(currentFormPrefix + field)[0];

  if (FormTrim(o.value) == '')
  {
    alert(text + ' is required!');
    ok = 0;
    o.focus();
  }
  else if (o.value != parseInt(o.value))
  {
    alert(text + ' must be an number!');
    o.focus();
    ok = 0;
  }
  
  return ok;
}

function FormFieldMatch(field1, field2, text)
{
  var ok = 1;
  var o1 = document.getElementsByName(currentFormPrefix + field1)[0];
  var o2 = document.getElementsByName(currentFormPrefix + field2)[0];

  if (o1.value != o2.value)
  {
    alert(text);
    o1.focus();
    ok = 0;
  }
  
  return ok;
}

function FormYear(field, futureReq)
{
  var ok = 1;
  var fullname = currentFormPrefix + field;
  var o = document.getElementsByName(fullname)[0];
  var year = parseInt(o.value);
  var yearLen = FormTrim(o.value).length;

  if (futureReq) {
    if (yearLen == 4 && year < 2002) 
      ok = 0;
    else if (yearLen == 2 && year < 2)
      ok = 0;
    else if (yearLen != 2 && yearLen != 4)
      ok = 0;
  }
  else if (yearLen == 0)
    ok = 0;

  if (!ok) {
    alert('Invalid year!');
    o.focus();
  }
  
  return ok;
}

function FormMonth(field)
{
  var ok = 1;
  var fullname = currentFormPrefix + field;
  var o = document.getElementsByName(fullname)[0];
  var month = parseInt(o.value);
 
  if (FormTrim(o.value) == '' || month < 1 || month > 12 || o.value.length != 2) 
    ok = 0;

  if (!ok) {
    alert('Invalid month!');
    o.focus();
  }
  
  return ok;
}

function FormCCRequired(field, name)
{
   var ok = 1;
   var fullname = currentFormPrefix + field;
   var o = document.getElementsByName(fullname)[0];
   var cc = FormTrim(o.value);
  
   if (cc.length < 14) ok = 0;

   var w = cc.indexOf(' ');
 
   if (w != -1) {
      ok = 0;
      alert('Please remove spaces from your card number');
      o.focus();
   }
   else if (!ok) {
      alert('Invalid credit card number');
      o.focus();
   }

   return ok;
}

function FormEmailRequired(field, name)
{
   var fullname = currentFormPrefix + field;
   var o = document.getElementsByName(fullname)[0];
   var email = o.value;
   var ok = 1;

    at = email.lastIndexOf("@");
    dot = email.lastIndexOf(".");

    if (at<0 || dot<0 || at>dot) ok = 0;

    if (email.indexOf(":")>=0 || email.indexOf("/")>=0 || email.indexOf(";")>=0 || email.indexOf(" ")>=0) ok = 0;

    if (ok) {
      box = email.substring(0,at);
      if (box.length < 1) ok = 0;
      if (box.indexOf("@") >= 0) ok = 0;
    }

    if (ok) {
      dom = email.substring(at+1);
      if (dom.indexOf("[")>=0 || dom.indexOf("]")>=0 || dom.indexOf("<")>=0 || dom.indexOf(">")>=0) ok = 0;
    }

    if (ok) {
      tld = email.substring(dot+1);
      if (tld.length < 2) ok = 0;
    }

    if (ok) {
      host = email.substring(at+1,dot);
      if (host.length < 1) ok = 0;
    }

    if (!ok) {
      if (name == undefined) name = field;
      alert('Invalid e-mail address for field ' + name);
      o.focus();
    }

    return ok;
}

function FormSelectionRequired(field, name)
{
  var fullname = currentFormPrefix + field;
  var o = document.getElementsByName(fullname)[0];
  var ok = 0;
  
  if (FormTrim(o.value) != '')
    ok = 1
  else {
    if (name == undefined) name = field;
    alert('Please choose a ' + name);
    o.focus();
  }

  return ok;
}

function FormRequired(field, name)
{
  var fullname = currentFormPrefix + field;
  var o = document.getElementsByName(fullname)[0];
  var ok = 0;

  if (FormTrim(o.value) != '')
    ok = 1
  else {
    if (name == undefined) name = field;
    alert(name + ' cannot be blank!');
    o.focus();
  }

  return ok;
}

function FormTrim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }

   if (inputString == '')
     return inputString;

   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   return retValue; // Return the trimmed string back to the user
}

function IsValidEmail(email)
{
   var ok = 1;

    at = email.lastIndexOf("@");
    dot = email.lastIndexOf(".");

    if (at<0 || dot<0 || at>dot) ok = 0;

    if (email.indexOf(":")>=0 || email.indexOf("/")>=0 || email.indexOf(";")>=0 || email.indexOf(" ")>=0) ok = 0;

    if (ok) {
      box = email.substring(0,at);
      if (box.length < 1) ok = 0;
      if (box.indexOf("@") >= 0) ok = 0;
    }

    if (ok) {
      dom = email.substring(at+1);
      if (dom.indexOf("[")>=0 || dom.indexOf("]")>=0 || dom.indexOf("<")>=0 || dom.indexOf(">")>=0) ok = 0;
    }

    if (ok) {
      tld = email.substring(dot+1);
      if (tld.length < 2) ok = 0;
    }

    if (ok) {
      host = email.substring(at+1,dot);
      if (host.length < 1) ok = 0;
    }

    return ok;
}