$(function() {
    var pulsateWin500 = function() {
        $('#win500').animate({ opacity: 0.4 }, 300, 'swing', function() {
            $(this).animate({ opacity: 1.0 }, 300, 'swing', function() {
                pulsateWin500();
            });
        });
    };

    pulsateWin500();

    $('#ncSubmit').click(function() {
        var rf = { 'calgarytoyota':null, 'edmontonlexus':null, 'name':null, 'email':null, 'telephone':null,'street':null,'city':null,'province':null,'postcode':null };
    
        error = null;

        for ( f in rf ) {
            $('#'+f).css({ borderColor: '#ffffff' });
            rf[f] = $('#'+f).val();

            if ( f == 'calgarytoyota' || f== 'edmontonlexus' )
                continue;

            if ( $('#'+f).val() == '' ) {
                $('#'+f).css({ borderColor: '#dd0000' });
                error = 'Please complete all required fields';
            }
        }

        if ( rf.calgarytoyota == ''  && rf.edmontonlexus == '' )
            error = 'You must enter a suggested name for at least one dealership!';

        if ( error != null )
            alert(error);
        else {
            $('#ncOL').show();
            $('#ncOL').css({ cursor: 'progress' });

            $.ajax({
                type: 'POST',
                timeout: 3000,
                url: 'entry.php',
                data: rf,
                success: function (jsd) {
                    $(window).scrollTop(0);
                    $('#calgarytoyota, #edmontonlexus').val('');
                    $('#ncSuccess').show();
                    $('#ncOL').css({ cursor: 'default' });
                },
                error: function() {
                    $('#ncOL').hide();
                    alert('There was a problem submitting, please try again later!');
                }
            });

        }

    });

    $('#closeMore').click(function() {
        $('#ncOL, #ncSuccess').hide();
    });

    $('#sendMore').click(function() {
        for ( i = 1; i<6; i++ ) {
            if ( $('#calgarytoyotaSug'+i).val() != '' ) {
                $('#calgarytoyota').val( $('#calgarytoyotaSug'+i).val() );
                $('#calgarytoyotaSug'+i).val('');
            }

            if ( $('#edmontonlexusSug'+i).val() != '' ) {
                $('#edmontonlexus').val( $('#edmontonlexusSug'+i).val() );
                $('#edmontonlexusSug'+i).val('');
            }
            
            if ( $('#calgarytoyota').val() != '' || $('#edmontonlexus').val() != '' )
                $('#ncSubmit').click();
        }


        alert('Thank you!');
    });

});
