﻿function stringCheck(form, name, required, max, prettyName)
{
  var v = getValue(form,name);
  if ( !required && v == "" )
  {
      return true;
  }
  if ( required && v == "" )
  {
      alert("Please a enter value for " + prettyName);
      form[name].focus();
      return false;
  }
  if ( v.length > max )
  { 
      alert("Maximum value for " + prettyName + " is " + max + " characters.");
      form[name].focus();
      return false;
  }
  return true;
}

function checkEmail(form, name, required, max, prettyName)
{
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  var v = getValue(form,name);
  if ( !required && v == "" )
  {
      return true;
  }
  //stringCheck makes sure if required then it's there
  if ( !stringCheck(form,name, required, max, prettyName))
  {
     return false;
  }
  if (!filter.test(v))
  {
       alert(v + " does not appear to be a valid email address.")
       form[name].focus();
       return false;
  }
  return true;
}
/*use null for length if don't want it checked*/
function checkPhone(form, name, required, length, prettyName)
{
  var v = getValue(form,name);
  //if we dont need one...
  if ( !required && v == "" )
  {
      return true;
  }
  //stringCheck makes sure if required then it's there
  if ( !stringCheck(form,name, required, 50, prettyName))//we assume numbers not bigger than 50
  {
     return false;
  }
  
  var stripped = v.replace(/[\(\)\.\-\ ]/g, '');
  //strip out acceptable non-numeric characters
  if (isNaN(parseInt(stripped))) 
  {
      alert(prettyName + " appears to contain invalid characters.");
      form[name].focus();
      return false;
  }
  if ( length!=null )
  {
      if ( stripped.length<length )
      {
      	alert("The " + prettyName + " is not long enough.  Is the area code missing?");
        form[name].focus();
        return false;
      }
  }
  return true;
}

/*use null for length if don't want it checked*/
function checkZip(form, name, required, length, prettyName)
{
  var v = getValue(form,name);
  //if we dont need one...
  if ( !required && v == "" )
  {
      return true;
  }
  //stringCheck makes sure if required then it's there
  if ( !stringCheck(form,name, required, 15, prettyName))//we assume numbers not bigger than 50
  {
     return false;
  }
  
  var stripped = v.replace(/[\(\)\.\-\ ]/g, '');
  //strip out acceptable non-numeric characters
  if (isNaN(parseInt(stripped))) 
  {
      alert(prettyName + " appears to contain invalid characters.");
      form[name].focus();
      return false;
  }
  if ( length!=null )
  {
      if ( stripped.length<length )
      {
      	alert("The " + prettyName + " is not long enough.");
        form[name].focus();
        return false;
      }
  }
  return true;
}

function checkDropdown(form, name, prettyName) {
    
    if (form[name].selectedIndex == 0) 
    {
      	alert("Please make selection for " + prettyName);
        form[name].focus();
        return false;
    }    
    return true;
} 

function checkRadio(form, name, prettyName)
{
    checked = false;
    for ( var j = 0; j< form[name].length; j++)
    {
        if (form[name][j].checked)
            checked = true;
    }
    if ( checked == false )
    {
      	alert("Please make selection for " + prettyName);
        //form[name].focus();
        return false;
    }
    return true;
}

function checkLength(form, name, max, prettyName)
{
  if ( form[name].value.length > max )
  {
      alert(prettyName + " can contain at most " + max + " characters.");
      return false;
  }
  return true;
}

function getValue(form, name)
{
    var v = form[name].value;
    if ( v==null )
    {
      alert(name + " does not appear to be form variable.  See admin.");
      return v;
    }
    else
      return trim(v);
}

function trim(v)
{
  return v.replace(/^\s*|\s*$/g,"");
}



