$(document).ready(function() {
	// Close panels when from_panel buttons are clicked
	// Also, update headlines
	$('#signup_form').submit(function(e) {
        var form = $(this);
        var valid = true;
        form.find('input[type="text"]').each( function(i) {
            input = $(this)
            label = form.find('label[for="' + input.attr('id') + '"]')
            if ( !input.attr('value') ) { 
                input.css('color','red');
                label.css('color','red'); 
                valid = false 
            } else {
                input.css('color','');
                label.css('color',''); 
            }
        })

        if (!valid) {
            $('#signup-error').show();
            return false;
        }

        $.post('/survey/process.pl', form.serialize(), function(data) {
            if ( !data.success ) {
                $('#signup-error').show();
		jQuery.each(data.invalid, function() { mark_invalid(this); });
		jQuery.each(data.missing, function() { mark_invalid(this); });

                return false;
            }

            window.user = data.id

            // bank their faxes
            var senators = 'both of your senators'
            $.post('bank.pl', { 'id': data.id });

    	    faxed();
        }, 'json');


	return false;
	});

    $('#f_whymarch').submit(function(e) {
        form = $(this)
        $.post('/survey/process.pl', form.serialize(), function(data) {}, 'json')
        $('.puff_container').append(
            '<div class="puff hide version-3">' + 
            '<p><b>I\'m marching because...</b><br/>' +
             this.Q_why_march.value + '<br/>' +
          '<b>&mdash;<b> You! </b></p>' +
          '</div>'
        )

        window.themagicpuff = true

        $('#f_whymarch input[name="Q_why_march"]').attr(
            'value', 'Thanks! Another?')
    

        return false
    })

    $('#thewhybutton').click(function(e) {
        e.preventDefault()
        $('#f_whymarch').submit()
        return false
    })
});


function called_and_faxed() {
  close_sliders();
  $('#thanks_call_and_fax').show().pause(6000).slideUp();
  $('#next_steps').load("next_steps.html", { 'id' : window.user }, 
     function() { 
	$('#call').hide();
        $('#next_steps').show();
     });
}

function faxed() {
  close_sliders();
  $("#signup").hide();
  $('#thanks_signup').show().pause(6000).slideUp(); 
  $('#call').load("call.html", { 'id' : window.user }, 
     function() { 
        $('#call').show();
        $('#call_button').click(function() { $.get(this.href); 	called_and_faxed(); return false; });
     }
  );
  // tell me all about it
  $('.whymarch input[name="id"]').attr('value',window.user)
  $('.whymarch').show()
}

function show_signup() {
  $('#signup').show();
}

function close_sliders() {
  $('#thanks_call_and_fax').hide();
  $('#thanks_signup').hide();
}

function mark_invalid(id) {
  input = $('#' + id)
  label = $('label[for="' + id + '"]');
  input.css('color','red');
  label.css('color','red'); 
  valid = false 
}

