/******
 *
 * This is the local javascript necessary to locally validate the form data 
 * for the various forms located at http://www.banpoundseizuremn.org. They
 * are dependent on the jQuery javascript framework, version 1.3.1,
 * downloadable at http://docs.jquery.com/Downloading_jQuery .
 *
 ******/
var states = { AL: 1, AK: 1, AZ: 1, AR: 1, CA: 1, CO: 1, CT: 1, DE: 1, DC: 1, 
               FL: 1, GA: 1, HI: 1, ID: 1, IL: 1, IN: 1, IA: 1, KS: 1, KY: 1,
               LA: 1, ME: 1, MD: 1, MA: 1, MI: 1, MN: 1, MS: 1, MO: 1, MT: 1,
               NE: 1, NV: 1, NH: 1, NJ: 1, NM: 1, NY: 1, NC: 1, ND: 1, OH: 1, 
               OK: 1, OR: 1, PA: 1, RI: 1, SC: 1, SD: 1, TN: 1, TX: 1, UT: 1,
               VT: 1, VA: 1, WA: 1, WV: 1, WI: 1, WY: 1 };

function validate( input ) {
    var user_value = input.val().replace(/(^\s+|\s+$)/, '');
    var field_name = input.attr('name');
    var validator  = 'validate_' + field_name;

    // All fields, save 'phone' are required, let's start validating
    // that any non-'phone' field has _a_ value, then work from there.
    if ( user_value.length > 0 || field_name == 'phone' ) {
        if ( eval("typeof " + validator + " == 'function'") ) {
            eval( validator + '(input)' );
        }
        else {
            input_accepted( input );
        }
    }
    else {
        input_error( input );
    }
};

function validate_state( input ) {
    var state = input.val().toUpperCase();

    if ( states[state] ) {
        input_accepted( input );
        return true;
    }
    else {
        input_error( input );
        return false;
    }
}

function validate_first_name( input ) {
    return _validate_string( input );
}

function validate_your_name( input ) {
    return _validate_string( input );
}

function validate_friend_name( input ) {
    return _validate_string( input );
}

function validate_last_name( input ) {
    return _validate_string( input );
}

function validate_city( input ) {
    return _validate_string( input );
}

function validate_email( input ) {
    // RegExp: ignore the following chars in both the local and system parts:
    // , ( ) : ; < > [ ] \ ~ | !
    // Allow everything else. General, I know.
    var regex = new RegExp(/^[^,\(\):;<>\[\]\\~\|!]+@[^,\(\):;<>\[\]\\~\|!]+$/);

    // Let's take this opportunity to check on the confirm email address
    // field if it exists. 
    //
    // If it doesn't match, we'll just eliminate it's value. The
    // validator will then flag it as an error when it checks it.
    var confirm = $('#confirm_email');
    if ( confirm &&  input.val() != confirm.val() ) {
        confirm.val('');
    }

    return _validate_regex( input, regex );
}

function validate_message( input ) {
    if ( input.text().length > 0 ) {
        input_accepted( input );
        return true;
    }
    else {
        input_error( input );
        return false;
    }
}

function validate_your_email( input ) {
    return validate_email( input );
}

function validate_friend_email( input ) {
    return validate_email( input );
}

// Again, let's be general.
function validate_address( input ) {
    var regex = new RegExp(/^\d+\s+.+$/);
    return _validate_regex( input, regex );
}

function validate_zip( input ) {
    var regex = new RegExp(/^\d{5}(\-\d{4})?$/);
    return _validate_regex( input, regex );
}

function validate_phone( input ) {
    var regex = new RegExp(/^\(?\d{3}\)?\s*\-?\d{3}(\-?|\s*)\d{4}$/);

    if ( input.val() ) {
        return _validate_regex( input, regex );
    }
    else {
        input_accepted( input );
        return true;
    }
}

function _validate_string( input ) {
    if ( typeof input.val() == 'string' ) {
       var regex = new RegExp(/^[a-zA-z\-\.\'\s]+$/);
       return _validate_regex( input, regex );
    }

    input_error( input );
    return false;
}

function _validate_regex( input, regex ) {
    if ( regex.test( input.val() ) ) {
        input_accepted( input );
        return true;
    }
    else {
        input_error( input );
        return false;
    }
}

function field_name_from_input( input ) {
    // This will be the text of the LABEL element associated with INPUT.
    var field_name = input.prev().text();
    var str_length = field_name.length - 1;

    return field_name.substring( 0, str_length );
}

function input_error( input ) {
    input.prev().addClass( 'error' );
    input.addClass( 'input-error' );
    input.val('');
    has_errors = true;
}

function input_accepted( input ) {
    input.prev().removeClass( 'error' );
    input.removeClass( 'input-error' );
}

function validate_form() {
    has_errors = false;
    $('.form-field input, textarea').each( function() {
        validate( $( this ) );
    });

    var element = $('#submit-error');
    if ( has_errors ) {
        var message = 'The information you gave us contains errors. ';
        message += 'Please re-enter.';

        element.text(message).attr('style', 'display: block;');
        return false;
    }
    else {
        element.text('').attr('style', 'display: none;');
        return true;
    }
}
