function trim( stringObject )
{
    var whitespace    = " \t\n\r"
    var stringLength  = 0
    var i             = 0
    var string        = stringObject.value
    
    /* left trim */
    stringLength = string.length;
    
    for ( i = 0; i < stringLength; i++ )
    {
        if ( whitespace.indexOf( string.charAt( i ) ) == -1 )
        {
            break;
        }
    }
    
    string = string.substring( i, stringLength );
    
    /* right trim */
    stringLength = string.length;
    
    for ( i = ( stringLength - 1 ); i > -1; i-- )
    {
        if ( whitespace.indexOf( string.charAt( i ) ) == -1 )
        {
            break;
        }
    }
    
    stringObject.value = string = string.substring( 0, i + 1 );
    
    return stringObject;
}

function isValidPhoneNumber( textObject )
{
    var number          = "0123456789"
    var validChars      = ".,-+()"
    var strippedNumber  = ""
    var minLength       = 7
    var maxLength       = 13

    /* Nothing to validate */
    if ( 0 == textObject.value.length )
    {
        return true;
    }

    /* Check for valid characters and numbers */
    for ( var i = 0; i < textObject.value.length; i++ )
    {
        if ( number.indexOf( textObject.value.charAt( i ) ) == -1 )
        {
            if ( validChars.indexOf( textObject.value.charAt( i ) ) == -1 )
            {
                alert( "'" + textObject.value.charAt( i ) + "' is an invalid character.\r\n\r\nPlease enter a valid phone number.\r\n" );
                textObject.focus();
                textObject.select();
                return false;
            }
        }
        else
        {
            strippedNumber += textObject.value.charAt( i );
        }
    }

    /* Check the length of the 'stripped' phone number */
    if ( strippedNumber.length < minLength || strippedNumber.length > maxLength )
    {
        alert( "The length of the phone number provided is " + strippedNumber.length + "\r\nThe phone number must be at least " + minLength + " digits and no more than " + maxLength + " digits long.\r\n\r\nPlease enter a valid phone number.\r\n" );
        textObject.focus();
        textObject.select();
        return false;
    }

    return true;
}

/* Simple validation.  Looking for an '@' character followed by a '.' */
function isValidEmailAddress( textObject )
{
    var AT        = "@"
    var DOT       = "."
    var atFound   = false
    var dotFound  = false

    /* Nothing to validate */
    if ( 0 == textObject.value.length )
    {
        return true;
    }

    for ( var i = 0; i < textObject.value.length; i++ )
    {
        if ( AT == textObject.value.charAt( i ) )
        {
            /* '@' is never the first character in an email address */
            if ( 0 == i )
            {
                alert( "The username of your email address is missing.\r\n\r\nPlease provide a valid email address\r\n" );
                textObject.focus();
                textObject.select();
                return false;
            }
            atFound = true;
        }

        if ( atFound && DOT == textObject.value.charAt( i ) )
        {
            dotFound = true;
            break;
        }
    }

    if ( !atFound )
    {
        alert( "The '@' character is missing from your email address.\r\n\r\nPlease provide a valid email addres\r\n" );
        textObject.focus();
        textObject.select();
        return false;
    }

    if ( !dotFound )
    {
        alert( "The domain of your email address is incorrect.\r\n\r\nPlease provide a valid email address\r\n" );
        textObject.focus();
        textObject.select();
        return false;
    }

    return true;
}

function isFormComplete()
{
    var error         = new Array();
    var errorCount    = 0;
    var errorOutput   = "";
    var objectToFocus = "";
    
    if ( "" == (trim( document.contact.fName )).value )
    {
        if ( "" == objectToFocus )
        {
            objectToFocus = document.contact.fName;
        }
        error[ errorCount ] = "First Name";
        errorCount++;
    }
    
    if ( "" == (trim( document.contact.lName )).value )
    {
        if ( "" == objectToFocus )
        {
            objectToFocus = document.contact.lName;
        }
        error[ errorCount ] = "Last Name";
        errorCount++;
    }
    
    if ( "" == document.contact.company.value )
    {
        if ( "" == objectToFocus )
        {
            objectToFocus = document.contact.company;
        }
        error[ errorCount ] = "Company";
        errorCount++;
    }
    
    if ( "" == document.contact.direct.value )
    {
        if ( "" == objectToFocus )
        {
            objectToFocus = document.contact.direct;
        }
        error[ errorCount ] = "Direct Phone";
        errorCount++;
    }
    
    if ( "" == document.contact.email.value )
    {
        if ( "" == objectToFocus )
        {
            objectToFocus = document.contact.email;
        }
        error[ errorCount ] = "E-mail Address";
        errorCount++;
    }
    
    if ( "" == document.contact.prodDesc.value )
    {
        if ( "" == objectToFocus )
        {
            objectToFocus = document.contact.prodDesc;
        }
        error[ errorCount ] = "Description";
        errorCount++;
    }
    
    if ( errorCount > 0 )
    {
        errorOutput = "The following fields are required\r\n\r\n";
        
        for ( var i = 0; i < errorCount; i++ )
        {
            errorOutput += error[ i ] + "\r\n";
        }
        errorOutput += "\r\n";
        alert( errorOutput );
        objectToFocus.focus();
        return false;
    }
    
    /* Validate one more time */
    if ( !isValidPhoneNumber( document.contact.direct ) )
    {
        return false;
    }
    
    if ( !isValidEmailAddress( document.contact.email ) )
    {
        return false;
    }
    return true;
}