$j(document).ready(function() {

    function validateForm() {
        // Checks the form to make sure that at least one position was submitted
        // and that position values are valid.
        
        var starting = parseInt($j('#starting').val());
        var ending = parseInt($j('#ending').val());
    
        // Make sure the values are in a valid range
        if (isNaN(starting) || starting < 1 || starting > 16569) {
            $j('#warningMessage').html('Start value must be in the range 1 to 16569');
            $j('#starting').focus();
            $j('div.error').show();
            return false;
        }
        else {
            if (isNaN(ending)) {
                // ending is not a valid value, so we are searching only on a single position
                // this is allowed, so do nothing
            }
            else {
                // ending is defined
                if (ending < 1 || ending > 16569) {
                    // ending is not in the valid range
                    $j('#warningMessage').html('Ending value must be in the range 1 to 16569');
                    $j('div.error').show();
                    $j('#ending').focus();
                    return false;
                }
                if (ending < starting) {
                    // ending can't be less than starting
                    $j('#warningMessage').html('ending must be greater than starting');
                    $j('div.error').show();
                    $j('#ending').focus();
                    return false;
                }
                if ( (ending - starting) > 100 ) {
                    $j('#warningMessage').html('Maximum search range is 100 bps.');
                    $j('div.error').show();
                    $j('#starting').focus();
                    return false;
                }
            }
    
        }
        
        // Everything validated so hide the error and show the output
        $j('div.error').hide();
        $j('#searchOutput').show();
        return true;
    };


    $j('#searchForm').ajaxForm({
        target: '#searchOutput',
        beforeSubmit: validateForm
        // success: showResponse
    });
});

