﻿jQuery(function() {
    jQuery('#mainimage').cycle({ 
        fx:    'fade', 
        speed: 1800,
        pause:  3 
    });
    
    jQuery('#picture').cycle({
        fx:    'fade',
        speed: 1800,
        pause: 10
    });
    
    jQuery('#deliveryaddress').hide();
    jQuery('input[name=deliveryaddress]').bind('click', function() {
        jQuery('#deliveryaddress')[this.value=='seperate'?'slideDown':'slideUp']('normal');
    });
    
    jQuery('#productform').submit(function() {
        var i, t=1, e=0;
        jQuery('input[name^=q]').each(function() { 
            if (isNaN(jQuery(this).val())) i = 1; 
            if (jQuery(this).val() != "0") t = 0; 
            e = (jQuery(this).val() == "" ) ? 1:0;
        });
        if (i==1||t==1||e==1) {
           alert('Please enter a quantity'); 
           return false;
        }
    });

    /*
    Ride of Respect Form 2011
    */

    // Check that there are enough spaces in the timeslot selected
	jQuery("#bikes").keyup(function() {
        var i = jQuery('#times option:selected').text().replace(" spaces available)", "")
        if (jQuery(this).val() > parseInt(i.substr(15, (i.length - 15)))) {
            alert('There are not enough spaces on that arrival time for that many bikes.\nPlease select another arrival time or reduce the amount of bikes.');
        }
        
        // Put the amount of bikes as the starting number of riders
        jQuery('#riders').val( jQuery('#bikes').val() );
        

        // Calculate the total including additional donation
        var riders = parseInt(jQuery('#riders').val()) * 10
        var donation = parseFloat(jQuery('#additional').val())
        if (isNaN(riders)) { riders = 0 };
        if (isNaN(donation)) { donation = 0 };
        jQuery('#total').html( '£ ' + (riders += donation).toFixed(2) ); 

        // Show the cost of the updated rider price
        jQuery('#bikesdonation').html( '£ ' + (riders).toFixed(2) );                 
    });

    // Calculate total cost for riders
    jQuery('#riders').keyup(function() { 
        var riders = parseInt(jQuery('#riders').val()) * 10
        var donation = parseFloat(jQuery('#additional').val())
        if (isNaN(riders)) { riders = 0 };
        if (isNaN(donation)) { donation = 0 };
        jQuery('#bikesdonation').html( '£ ' + (riders).toFixed(2) ); 
        jQuery('#total').html( '£ ' + (riders += donation).toFixed(2) ); 
    });

    // Calculate the total including the additional donation
    jQuery('#additional').keyup(function() {
        var riders = parseInt(jQuery('#riders').val()) * 10
        var donation = parseFloat(jQuery('#additional').val())
        if (isNaN(riders)) { riders = 0 };
        if (isNaN(donation)) { donation = 0 };
        jQuery('#total').html( '£ ' + (riders += donation).toFixed(2) ); 
    });
    
    // Just in case they have an error and need the value to be calculated on page load
    jQuery('#bikesdonation').html( '£ ' + (parseInt(jQuery('#riders').val()) * 10).toFixed(2) );
    
})
