﻿function submitForm(sNewFormAction)
{
   //validate form
   if (validateForm(document.forms[0]))
   {
      //set SalesForce link as form action
      document.forms[0].action = sNewFormAction;
      document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
      return true;
   }
   else return false;
}

function checkEmail(v)
{
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(v)) return true;
  return false;
}

function validateForm(el)
{
  var error = "You must fill in all fields before you can proceed";
  var valid = true;
  var items = el.getElementsByTagName('input');

  var i = items.length - 1;
  if (i >= 0)
  {
    do
    {
      if (items[i].type == 'text')
      {
        //check if the field has information
        if (items[i].value == '')
        {
          valid = false;
          break;
        }

        //check if the email field has a valid email address
        if (items[i].name == 'email')
        {
          if (!checkEmail(items[i].value))
          { 
            valid = false;
            error = "Please enter a valid email address";
            break;
          }
        }
      }
    } while (i--);
  }

  if (valid) return true;

  alert(error);
  return false;
}