function formval( aform )
{
  var phone = aform.required_phone1.value + '-' +
              aform.required_phone2.value + '-' +
              aform.required_phone3.value ;
  var aDate = aform.required_date;

  var valid = true;
  var valid2 = true;
  var msg = 'Form errors:\n\n';

  var field = aform.required_name;
  if( ! chkMan( field ) ) valid = false;

  field = aform.required_email;
  if( ! specialVal( field ) ) valid = false;

  field = aform.required_address;
  if( ! specialVal( field ) ) valid = false;

  field = aform.required_city;
  if( ! specialVal( field ) ) valid = false;

  field = aform.required_zipcode;
  if( ! specialVal( field ) ) valid = false;

  field = aform.required_amount;
  //if( ! chkMan( field ) || ! isNumeric( field ) ) valid = false;
  if( ! isNumeric( field ) ) valid = false;

  var phoneEl = document.getElementById( 'required_phone_er' );
  if( ! isValidPhone( phone ) )
  {
    phoneEl.style.display = 'block';
    valid = false;
  }
  else
  {
    phoneEl.style.display = 'none';
  }

  var dateEl = document.getElementById( 'required_date_er' );
  if( ! isValidDate( aDate.value ) )
  {
    dateEl.style.display = 'block';
    valid = false;
  }
  else
  {
    dateEl.style.display = 'none';
  }

  return valid;
}

// valid phone format is 123-456-7890,
// where 123 is the area code
function isValidPhone( strString )
{
  var strValidChars = "0123456789";
  var strChar;
  var blnResult = true;
  var len = strString.length;
  //alert( strString );

  if( len == 0 || len != 12 ) return false;
  if( strString == '###-###-####' ) return false;

  //  test strString consists of valid characters listed above
  for( var i = 0; i < len; i++ )
  {
    strChar = strString.charAt( i );
    if( i != 3 && i != 7 )
    {
      if( strValidChars.indexOf( strChar ) == -1 )
      {
        blnResult = false;
        break;
      }
    }
    else
    {
      if( strChar != '-' && strChar != '.' )
      {
        blnResult = false;
        break;
      }
    }
  }

  return blnResult;
}

// valid date format is dd/mm/yyyy
function isValidDate( strString )
{
  var strValidChars = "0123456789";
  var strChar;
  var blnResult = true;
  var len = strString.length;

  if( len == 0 || len != 10 ) return false;
  if( strString == 'dd/mm/yyyy' ) return false;

  //  test strString consists of valid characters listed above
  for( var i = 0; i < len; i++ )
  {
    strChar = strString.charAt( i );
    if( i != 2 && i != 5 )
    {
      if( strValidChars.indexOf( strChar ) == -1 )
      {
        blnResult = false;
        break;
      }
    }
    else
    {
      if( strChar != '-' && strChar != '/' )
      {
        blnResult = false;
        break;
      }
    }
  }

  return blnResult;
}

function phoneFocus( val )
{
  //if( val.value == '###-###-####' )
  if( val.value == '###' || val.value == '####' )
  {
    val.value = '';
  }
}

function dAdd( val, evt )
{
  var value = val.value;

  if( evt && evt.keyCode == 8 )
  {
    return true; 
  }

  if( value.length == 2 || value.length == 5 )
  {
    val.value += '/';
  }
}

function dateFocus( val )
{
  if( val.value == 'dd/mm/yyyy' )
  {
    val.value = '';
  }
}

function chkMan( elem )
{
  var valid = true;
  var erfld = document.getElementById( elem.name + '_er' );
  var value = elem.value.replace( /^\s+|\s+$/g, '' ) ;

  erfld.innerHTML = 'Required';

  if( ! value )
  {
    erfld.style.display = 'block';
    valid = false;
  }
  else
  {
    erfld.style.display = 'none';
  }

  return valid;
}

// Add specialized validation here
function specialVal( field )
{
  
  var rc = true;
  var erfld = document.getElementById( field.name + '_er' );
  var value = field.value.replace( /^\s+|\s+$/g, '' ) ;
  var name = field.name;
  var pos = -1;

  rc = chkMan( field );

  if( ! rc )
  {
    return rc;
  }

  // Is email field?
  if( name.indexOf( 'email' ) >= 0 )
  { 
    pos = value.indexOf( '@' );

    if( value.indexOf( '@' ) <= 0 )
    {
      erfld.innerHTML = 'Must be valid email address';
      erfld.style.display = 'block';
      rc = false;
    }
    else if( pos == value.length-1 ) 
    {
      erfld.innerHTML = "Email must have a valid domain after '@'";
      erfld.style.display = 'block';
      rc = false;
    }

    var atCnt = 0;
    var strChar;

    if( rc && pos > 0 )
    {
      // make sure there's a domain
      for( var i=pos; i < value.length; i++ )
      {
        strChar = value.charAt( i );

        if( strChar == '@' )
        {
          atCnt++;
          if( atCnt > 1 )
          {
            erfld.innerHTML = "Too many '@' signs";
            erfld.style.display = 'block';
            rc = false;
            break;
          }
        }
      }
      //alert( value.substring( pos, value.length ) );

      var domain = value.substring( pos, value.length );

      // confirm at least one '.'
      if( rc )
      {
        var dotPos = domain.indexOf( '.' );

        //alert( dotPos + ' : ' +  domain.length );
        if( dotPos <= 0 || dotPos == domain.length-1 || domain.charAt( domain.length-1 ) == '.' )
        {
          erfld.innerHTML = "Invalid domain after '@'";
          erfld.style.display = 'block';
          rc = false;
        }
      }
    }

  } // Valid number?

  return rc;
}

// Check for valid numeric strings	
function isNumeric( elem )
{
  var strValidChars = "$0123456789,.";
  var strChar;
  var blnResult = true;
  var strString = elem.value.replace( /^\s+|\s+$/g, '' ) ;
  //var strString = elem.value;

  var erfld = document.getElementById( elem.name + '_er' );

  erfld.innerHTML = 'Required';

  if( strString.length == 0 )
  {
    elem.value = '$';
    erfld.style.display = 'block';
    return false;
  }

  if( strString.indexOf( '$' ) == 0 && strString.length == 1 )
  {
    blnResult = false;
    erfld.style.display = 'block';
    return false;
  }

  // Test strString consists of valid digits listed above
  for( i = 0; i < strString.length; i++ )
  {
    strChar = strString.charAt( i );

    if( strValidChars.indexOf( strChar ) == -1 )
    {

      blnResult = false;
      //var erfld = document.getElementById( elem.name + '_er' );
      erfld.innerHTML = 'Invalid number';
      erfld.style.display = 'block';

      break;
    }
  }

  if( blnResult )
  {
    erfld.style.display = 'none';
  }

  return blnResult;
}


