$(document).on('change', '#product-calculator select', function() {

	mailingServices();
	folding();
	paperType();

});

function reloadLowestPricesAndSpecs()
{
	if (typeof productGrouping == 'undefined' || !productGrouping) {
		if (pricePerPiece)
		{
			$.getJSON('/store/jsonGetCategoryLowestPricePerPiece.html?ajax=true&categoryId='+$('#category').val()+'&productId='+$('#product').val(), function(data) {

				$('#starting-at-price').html('$'+data.lowestPrice);

			});
		} else
		{
			$.getJSON('/store/jsonGetCategoryLowestPrice.html?ajax=true&categoryId='+$('#category').val()+'&productId='+$('#product').val(), function(data) {

				$('#starting-at-price').html('$'+data.lowestPrice);

			});
		}
	}

	if ( ($('#product').val()!=='') && ($('#product').val()!==null) ) {
        $('.product-specs-container').load('/store/jsonGetProductSpecs.html?ajax=true&productId='+$('#product').val(), function() {
            $('.product-specs-mobile #productSpecs').html($('.product-specs-container').html());
        });

        if ($('.product-specs-desktop').is(':visible') && $('.product-specs-mobile').is(':visible')) {
            $('.product-specs-mobile').hide();
        }
    }
}

$(document).on('change', '#product-calculator #product', function() {

	reloadLowestPricesAndSpecs();

});

function serializeForm(form)
{
    var addDisabled = '';
    $(form).find('select:disabled').each(function() {
        addDisabled += '&' + encodeURIComponent($(this).attr('name')) + '=' + $(this).val();
    });

	if (window.navigator.userAgent.indexOf('Safari')>-1 && window.navigator.userAgent.indexOf('Chrome')==-1 && window.navigator.userAgent.indexOf('Firefox')==-1) {
		var result = '';
		form.find('input').each(function() {
			if (typeof $(this).attr('name')=='undefined') return;
			if (typeof $(this).val()=='undefined') return;
			result += encodeURIComponent($(this).attr('name'))+'='+encodeURIComponent($(this).val())+'&';
		});
		form.find('select').each(function() {
			if (typeof $(this).attr('name')=='undefined') return;
			if (typeof $(this).children('option:selected').attr('value')=='undefined') return;
			result += encodeURIComponent($(this).attr('name'))+'='+encodeURIComponent($(this).children('option:selected').attr('value'))+'&';
		});

		result = result.substring(0, result.length-1) + addDisabled;

		return result;
	} else {
		return form.serialize() + addDisabled;
	}
}

$(document).ready(function() {

    $('#addToFavorites').click(function() {

        var link = $(this);
        $.getJSON('/store/add-product-favorite.html?ajax=true&pID=' + $('#category').val(), function(data) {

            link.replaceWith('<a id="addToFavorites" class="btn btn-default btn-success"><span class="glyphicon glyphicon-thumbs-up"> </span> ' + data.message + '</a>');

        });

        return false;

    });

    $('#priceMatch').click(function() {

        $('#product-calculator select').attr('disabled', false);

        $('#product-calculator').attr('action', '/store/competitor-price-match.html').submit();

        return false;

    });

});