
// When the DOM is ready...
$(function(){
	
	// Hide stuff with the JavaScript. If JS is disabled, the form will still be useable.
	// NOTE:
	// Sometimes using the .hide(); function isn't as ideal as it uses display: none; 
	// which has problems with some screen readers. Applying a CSS class to kick it off the
	// screen is usually prefered, but since we will be UNhiding these as well, this works.
	$(".name_wrap").hide();
	$("#company_name_wrap").hide();
    
	// Reset form elements back to default values
	$("#submit_button").attr("disabled",true);
	$("#tour").val( $('#selected_tour').val() );
    $("#duration").val('2 hours');

    if ( $('#selected_tour').val() == 'Bike Rentals' || $('#selected_tour').val() == 'Central Park Rickshaw Tour') {
        $('#duration_box').show();
    }
    else {
        $('#duration_box').hide();
    }
	$("#step_2 input[type=radio]").each(function(){
		this.checked = false;
	});
	$("#rock").each(function(){
		this.checked = false;
	});
	
	// Fade out steps 2 and 3 until ready
	$("#step_2").css({ opacity: 0.3 });
	$("#step_3").css({ opacity: 0.3 });
	
	$.stepTwoComplete_one = "not complete";
	$.stepTwoComplete_two = "not complete"; 
    
    // When a dropdown selection is made
	$("#duration").change(function() {
        showRickshawNums();
    });

    // When a dropdown selection is made
	$("#tour").change(function() {
        if ($("#tour option:selected").text() == 'Bike Rentals' || $("#tour option:selected").text() == 'Central Park Rickshaw Tour') {
            if ( $("#tour option:selected").text() == 'Central Park Rickshaw Tour' )  {
                $("#duration option[value='3']").remove();
                $("#duration option[value='4']").remove();
            }
            else {
                if (! $("#duration option[value='3']").text() && ! $("#duration option[value='4']").text()) {
                    $('#duration').append($(document.createElement("option")).attr("value","3").text("3 hours"));
                    $('#duration').append($(document.createElement("option")).attr("value","4").text("All day special"));
                }
            }
            $('#duration_box').show();
        }
        else {
            $('#duration_box').hide();
        }
        if ($("#num_kids_attendees option:selected").text() != 'Please Choose'
         && $("#num_attendees option:selected").text() != 'Please Choose' ) {
            showRickshawNums();
            $("#step_1")
	        .animate({
	        	paddingBottom: "120px"
	        })
	        .css({
	        	"background-image": "url(../img/check.png)",
	        	"background-position": "bottom center",
	        	"background-repeat": "no-repeat"
	        });
	        $("#step_2").css({
	        	opacity: 1.0
	        });
	        $("#step_2 legend").css({
	        	opacity: 1.0 // For dumb Internet Explorer
	        });
        }
    });
    
	// When a dropdown selection is made
	$("#num_attendees").change(function() {
        if ($("#num_kids_attendees option:selected").text() != 'Please Choose'
         && $("#tour option:selected").text() != 'Please Choose') {
            showRickshawNums();
            $("#step_1")
	        .animate({
	        	paddingBottom: "120px"
	        })
	        .css({
	        	"background-image": "url(../img/check.png)",
	        	"background-position": "bottom center",
	        	"background-repeat": "no-repeat"
	        });
	        $("#step_2").css({
	        	opacity: 1.0
	        });
	        $("#step_2 legend").css({
	        	opacity: 1.0 // For dumb Internet Explorer
	        });
        }
    });

    $("#num_kids_attendees").change(function() {
        if ($("#num_attendees option:selected").text() != 'Please Choose'
         && $("#tour option:selected").text() != 'Please Choose') {
            showRickshawNums();
            $("#step_1")
	        .animate({
	        	paddingBottom: "120px"
	        })
	        .css({
	        	"background-image": "url(../img/check.png)",
	        	"background-position": "bottom center",
	        	"background-repeat": "no-repeat"
	        });
	        $("#step_2").css({
	        	opacity: 1.0
	        });
	        $("#step_2 legend").css({
	        	opacity: 1.0 // For dumb Internet Explorer
	        });
        }
    });


	$(".name_input").blur(function(){
		var all_complete = true;
				
		$(".name_input").each(function(){
			if ($(this).val() == '' ) {
				all_complete = false;
			};
		});
		
		if (all_complete) {
            $.stepTwoComplete_one = "complete"; 
            $.stepTwoComplete_two = "complete"
		};
        stepTwoTest();
	});

    function validate_email() {
        var email = $("#email_toggle_on").val();
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        
        if (emailPattern.test(email))
           {
               return true;
           }
        else
           {
               return false;
           }
    }
    
    $("#email_toggle_on").change(function() {
        if ( validate_email() )
           {
              $('#help').text('');
           }
        else
           {
              $('#help').text('- Please, enter valid email');
           }
    });

    function showRickshawNums() {
        var prices = {
            'Central Park Rickshaw Tour': { 'price': { '1': '79', '2': '189' } },
            'Central Park Walking Tour': { 'price': '47' },
            'Central Park Bike Tour': { 'price': '47' },
            'Bike Rentals': { 'price': { '1': '15', '2': '20', '3': '25', '4': '45' } },
            'Brooklyn Bridge Bike Tour': { 'price': '60' },
        };

        var adults = parseInt( $("#num_attendees option:selected").text() ) || 0;
        var kids   = parseInt( $("#num_kids_attendees option:selected").text() ) || 0;

        var tour   = $("#tour option:selected").text();
        
        var total = adults + kids;
        
        $("#service").text( tour );
        $("#num_kids").text( kids  );
        $("#num_adults").text( adults );

        if ( tour == 'Bike Rentals' ) {
            $("#rickshaw_nums").text( "You are reserving " + total + " bikes" );
            $("#duration_txt").text( "Duration: " + $("#duration option:selected").text() );
            
            $("#total_price").text( total * prices[ 'Bike Rentals' ][ 'price' ][ $("#duration option:selected").val() ] + "$" );
        }
        else if ( tour == 'Central Park Bike Tour' ) {
            $("#rickshaw_nums").text( "You are reserving " + total + " bikes" );
            
            $("#total_price").text( total * prices[ 'Central Park Bike Tour' ][ 'price' ] + "$" );
        }
        else if ( tour == 'Central Park Rickshaw Tour' ) {
            var rickshaw_nums = parseInt( (adults + kids + 1) / 2);
            $("#rickshaw_nums").text( "You are reserving " + rickshaw_nums + " rickshaws");
            $("#duration_txt").text( "Duration: " + $("#duration option:selected").text() );
            
            $("#total_price").text( rickshaw_nums * prices[ 'Central Park Rickshaw Tour' ][ 'price' ][ $("#duration option:selected").val() ] + "$" );
        }
        else if ( tour == 'Central Park Walking Tour' ) {
            $("#rickshaw_nums").text( "You are reserving " + total + " tickets");
            
            $("#total_price").text( total * prices[ 'Central Park Walking Tour' ][ 'price' ] + "$" );
        }
        else if ( tour == 'Brooklyn Bridge Bike Tour' ) {
            $("#rickshaw_nums").text( "You are reserving " + total + " bikes");
            
            $("#total_price").text( total * prices[ 'Brooklyn Bridge Bike Tour' ][ 'price' ] + "$" );
        }
        else {
            $("#rickshaw_nums").text( "There is a problem with your reservation. Please, contact us." );
        }
    }
	
	function stepTwoTest() {
		if (($.stepTwoComplete_one == "complete") && ($.stepTwoComplete_two == "complete")) {
            if ( validate_email() ) {
			    $("#step_2")
			    .animate({
			    	paddingBottom: "120px"
			    })
			    .css({
			    	"background-image": "url(../img/check.png)",
			    	"background-position": "bottom center",
			    	"background-repeat": "no-repeat"
			    });
			    $("#step_3").css({
			    	opacity: 1.0
			    });
			    $("#step_3 legend").css({
			    	opacity: 1.0 // For dumb Internet Explorer
			    });
            }
            else {
                $('#help').text('- Please, enter valid email');
            }
		}
	};
	
	$("#rock").click(function(){
		if (this.checked && $("#num_attendees option:selected").text() != 'Please Choose'
		  	&& $.stepTwoComplete_one == 'complete' && $.stepTwoComplete_two == 'complete')
        {
            $("#submit_button").attr("disabled",false);
    	} 
        else {
	    	$("#submit_button").attr("disabled",true);
		}
	});
	
});

