function setAttendees(num) {
    var myForm      = $('#prereg');
    var attendees   = $('.attendee');
    for (i=0;i<attendees.length;i++) {
        if (i<num-1) {
            $(attendees[i]).show(1500);
        } else {
            $(attendees[i]).hide(1500);
        }
    }
}

function setTicket(ticket) {
    var myForm  = document.getElementById('reg');
    var ps      = document.getElementsByTagName("p");
    for (i=0;i<ps.length;i++){
        var p = ps[i];
        if (p.className == 'car') {
            if (ticket == 'S') {
                p.style.display = "none";
            } else {
                p.style.display = "";
            }
        }
    }
}

$(function()
{
    $('#email').bt({
      trigger: ['focus', 'blur'],
      positions: ['right'],
      animate: true,
      spikeLength: 20,
      fill: 'rgba(255, 255, 102, .6)'
    });

    $('#adults,#children').bt({
      trigger: ['focus', 'blur'],
      positions: ['right'],
      animate: true,
      spikeLength: 200,
      fill: 'rgba(255, 255, 102, .6)'
    });

    $('#dinnernum').bt({
      trigger: ['focus', 'blur'],
      positions: ['right'],
      animate: true,
      spikeLength: 70,
      width: 330,
      fill: 'rgba(255, 255, 102, .6)'
    });

    $('#carpool').bt({
      trigger: ['focus', 'blur'],
      positions: ['right'],
      animate: true,
      spikeLength: 100,
      width: 150,
      fill: 'rgba(255, 255, 102, .6)'
    });

    $.each(
        $('.attendee'),
        function(){
            $(this).hide();
        }
    );

    $('#fullname').focus(
        function() {
            $.scrollTo('#preregform');
    });

    $('#friday').click(function(){
        if ($(this).attr('checked')) {
            $('#havecarDiv').show(1000);
        } else {
            $('#havecarDiv').hide(1000);
        }
    });

    $('[name=dinner]').click(function(){
        if($(this).fieldValue()=="n") {
            $('#dinnernumDiv').hide(1000);
        } else {
            $('#dinnernumDiv').show(1000);
        }
    });

    $('[name=havecar]').click(function(){
        if($(this).fieldValue()=="n") {
            $('#carpoolDiv').hide(1000);
        } else {
            $('#carpoolDiv').show(1000);
        }
    });

    function calculate() {
            var adults  = $('#adults').val();
            if (adults=='') {
                adults = 0;
            }

            var children= $('#children').val();
            if (children=='') {
                children = 0;
            }

            var at = parseInt(adults)+parseInt(children);
            if (at>1) {
                setAttendees(at);
                $('#attendees').show(1000);
            } else {
                $('#attendees').hide(1000);
            }

            var url = '/registration?action=0&adults='+adults+'&children='+children+'&days='+$('[name=days[]]').fieldValue()+'&currency='+$('[name=currency]').fieldValue()+'&random=' + (new Date()).getTime();

            if ($('[name=currency]').fieldValue()=='') {
                return;
            } else {
                $('#tickets').load(url);
            }
    }

    // validate signup form on keyup and submit
    $('#prereg').validate({
        errorPlacement: function(error, element) {
            if ( element.is(":radio") ) {
                error.insertBefore( element.next().next() );
            } else {
                error.insertAfter( element );
            }
        },
        rules: {
            fullname: "required",
            adults: {
                required: true,
                min: 1
            },
            email: {
                required: true,
                email: true
            },
            currency: "required",
            havecar: {
                required: "#friday:checked"
            },
            dinner: "required",
            captcha: {
                required: true,
                equalTo: "#captcha2"
            }
        },
        messages: {
            fullname: "Please enter your full name.",
            adults: "At least 1 adult needs to attend AvCon.",
            email: "Please enter a working e-mail address.",
            currency: " Please choose a currency.",
            havecar: " Please choose an option.",
            dinner: " Please choose an option.",
            captcha: "Please enter <strong>AvCon2009</strong> into the field for security reason."
        }
    });

    $('#attendees').hide();
    $('#dinnernumDiv').hide();
    $('#carpoolDiv').hide();
    $('#adults').blur(calculate);
    $('#children').blur(calculate);
    $('.currency').click(calculate);
    $('.days').click(calculate);
});
