function showErrors (input, errors) {
    $("span.error").remove();
    if (errors) {
        $.each(errors, function(i, error) {
            $("#id_"+i).before('<span class="error">'+error+'</b>');
        });
        return false;
    }
    else {
        return true;
    }
}

function post_data (input) {
    $.post (
        '/contact/', 
        $('form').formToArray(), 
        function (data) {
            var json = eval('('+data+')');
            if (showErrors(input, json.errors) == true) {
                $('form#contactForm').hide("1000");
                $('div#contact-form').empty().append("<h2>Email sent</h2>");
                
            }
        }
    );
}

$(function() { 
    $("input#contactSubmitButton").click(function() { 
        post_data(); 
        return false; 
    }); 
});

$(function() {
    $("input[name=contact_method]").click(function() {
        if ( $(this).attr('value') != 'email') {
            $('#id_'+$(this).attr('value')).parent().toggle();
        }
    });
});

$(document).ready(function() {
    $('.hidden').parent().hide();
});