$(function() {
    $("body").delegate(".read-more","click",function(e){
        e.preventDefault();
        e.stopPropagation();
        $(".hide").hide();
        $(this).parents(".show").next().show();
    });

    $("body").delegate(".hide","click",function(e){
        $(this).hide();
    });

    $("#golf-outing-signup").submit(function(e){
        e.preventDefault();

        $.post('/golfer',$(this).serialize(),function(data){
            if(data == 'success'){
                var selection = $('[name=selection]:checked').val();
                $('#'+selection).find('form').submit();
            } else {
                alert('Please fill out all required fields');
            }
        });
    });
    
    var required = $("#employment-form label:contains('*')").nextAll(":text"),
    requiredRadios = $("#employment-form label:contains('*')").nextAll(":radio");

    $("#employment-form").submit(function(e){
        var okToSubmit = true;

        required.each(function(i,el){
            if(!$.trim($(el).val())){
                okToSubmit = false;
                $(el).prevAll("label:first").css("border","red solid 3px");
            }
        });
        requiredRadios.each(function(i,el){
            if(el.checked === false && !$(el).siblings(":checked").length){
                okToSubmit = false;
                $(el).prevAll("label:first").css("border","red solid 3px");
            }
        });

        if(!okToSubmit){
            e.preventDefault();
            e.stopPropagation();
            alert("Missing required fields, please provide an answer to the fields indicated in red.")
        }
    });
    $("#rotator").cycle();
});


