$('.btn-number').click(function(e){
    e.preventDefault();
    
    var fieldName = $(this).attr('data-field');
    var type      = $(this).attr('data-type');
    var input = $('input[id="'+fieldName+'"]');
    var currentVal = parseInt(input.val());
	
    if (!isNaN(currentVal)) {
        if(type == 'minus') {
            
            if(currentVal > input.attr('min')) {
                input.val(currentVal - 1).change();
            } 
            if(parseInt(input.val()) == input.attr('min')) {
                $(this).attr('disabled', true);
            }

        } else if(type == 'plus') {

            if(currentVal < input.attr('max')) {
                input.val(currentVal + 1).change();
            }
            if(parseInt(input.val()) == input.attr('max')) {
                $(this).attr('disabled', true);
            }
        }
    } else {input.val(0);}
});
$('.input-number').focusin(function(){
   $(this).data('oldValue', $(this).val());
});
$('.input-number').change(function() {
    
    var minValue =  parseInt($(this).attr('min'));
    var maxValue =  parseInt($(this).attr('max'));
    var valueCurrent = parseInt($(this).val());
    
    name = $(this).attr('id');
    if(valueCurrent >= minValue) {$('.btn-number[data-type="minus"][data-field="'+name+'"]').removeAttr('disabled')} else {
        alert('Sorry, the minimum value was reached');
        $(this).val($(this).data('oldValue'));
    }
    if(valueCurrent <= maxValue) {$('.btn-number[data-type="plus"][data-field="'+name+'"]').removeAttr('disabled')} else {
        alert('Sorry, the maximum value was reached');
        $(this).val($(this).data('oldValue'));
    }
    
    var fieldName = $(this).attr('id');
    var currentVal = $(this).val();
    var currPrice = $('div.check_price'+fieldName).attr('data-price');
	var currTot = (currPrice*currentVal).toFixed(2);
    console.log('currPrice '+currPrice);
    	
	$('.check_item_subtotal'+fieldName).html(currTot);
    $('.check_item_subtotal'+fieldName).attr('data-subtotal', currTot);
    
    var sum = 0;
    $( '.subT' ).each(function( index ) {
    	var subTot = (sum += Number ($(this).attr('data-subtotal'))).toFixed(2);
    	$('th.check_order_subtotal').attr('data-ordersubtotal', subTot);
		$('th.check_order_subtotal').html(subTot);
		if($.cookie('exp__currency') === 'GB' ) { 
			var shipRate = $( "#shipping_method option:selected" ).data('base_rate');
		} else if ($.cookie('exp__currency') === 'US' ) { 
			var shipRate = $( "#shipping_method option:selected" ).data('dollar_rate');
		} else if ($.cookie('exp__currency') === 'EU' ) { 
			var shipRate = $( "#shipping_method option:selected" ).data('euro_rate');
		}
		var shipCost = $('.check_order_shipping').data('shipping');
        var ordSubTot = $('th.check_order_subtotal').attr('data-ordersubtotal');  
        var taxrate = $('.ordertax').attr('data-taxrate');
        var taxCalc = $('.ordertax').attr('data-taxsum');
    	var taxsum = (taxrate*(+ordSubTot + +shipCost)).toFixed(2);
    	$('.ordertax').html(taxsum);
        $('.ordertax').attr('data-taxsum', taxsum);
		$('.check_order_shipping').html(shipRate);
		$('.check_order_shipping').attr('data-shipping', shipRate);          
        var ordTotal = $('.check_order_total').data('ordertotal');
		var grdTotal = (+ordSubTot + +taxsum + +shipCost).toFixed(2);
        $('.check_order_total').html(grdTotal);
        $('.check_order_total').attr('data-ordertotal', grdTotal);
	});
            
    if($.cookie('exp__currency') === 'GB' ) { 
		$('.check_item_subtotal'+fieldName).formatCurrency({region: 'en-GB', roundToDecimalPlace:-1});
    	$('th.check_order_subtotal').formatCurrency({region: 'en-GB', roundToDecimalPlace:-1});
        $('.ordertax').formatCurrency({region: 'en-GB', roundToDecimalPlace:-1});
        $('.check_order_total').formatCurrency({region: 'en-GB', roundToDecimalPlace:-1});
	} else if ($.cookie('exp__currency') === 'US' ) { 
		$('.check_item_subtotal'+fieldName).formatCurrency({region: 'en-US', roundToDecimalPlace:-1});
    	$('th.check_order_subtotal').formatCurrency({region: 'en-US', roundToDecimalPlace:-1});
        $('.ordertax').formatCurrency({region: 'en-US'});
        $('.check_order_total').formatCurrency({region: 'en-US', roundToDecimalPlace:-1});
	} else if ($.cookie('exp__currency') === 'EU' ) { 
		$('.check_item_subtotal'+fieldName).formatCurrency({region: 'en-IE', roundToDecimalPlace:-1});
    	$('th.check_order_subtotal').formatCurrency({region: 'en-IE', roundToDecimalPlace:-1});
        $('.ordertax').formatCurrency({region: 'en-IE'});
        $('.check_order_total').formatCurrency({region: 'en-IE', roundToDecimalPlace:-1});
	}
        
});

$(function () {
	$('.input-number').change();
   console.log("The quantity script has been triggered");
});

$('.input-number').keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right
            (e.keyCode >= 35 && e.keyCode <= 39)) {
                 // let it happen, dont do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {e.preventDefault();}
    });
