jQuery.fn.selectOptions = function(value) {
	this.each(
		function()	{
			if(this.nodeName.toLowerCase() != "select") return;
			
			// get number of options
			var optionsLength = this.options.length;
			
			
			for(var i = 0; i<optionsLength; i++) {
				if (this.options[i].value == value) {
					this.options[i].selected = true;
				};
			}
		}
	)
	return this;
}

function enableOptions() {
	$('#additional_mailboxes').removeAttr('disabled');
	$('#add_storage').removeAttr('disabled');
	$('#bbmobile').removeAttr('disabled');
	$('#asmobile').removeAttr('disabled');
}

function disableOptions() {
	$('#additional_mailboxes').attr('disabled', 'disabled');
	$('#add_storage').attr('disabled', 'disabled');
	$('#asmobile').selectOptions("");
	$('#bbmobile').attr('disabled', 'disabled');
	$('#asmobile').attr('disabled', 'disabled');
}

function resetValues() {
	$('#mailbox_price').html("<em>3 Mailbox minimum</em>");
	$('#final_total').html("$0.00");
	$('#bbmobile').selectOptions("0");
	$('#asmobile').selectOptions("0");
	$('#add_storage').selectOptions("0");
	$('#additional_mailboxes').selectOptions("0");
	$('#total_storage').html($('#add_storage').val() + "GB extra");	
	$('#bb_total').html($('#bbmobile').val() + " Blackberry");
	$('#as_total').html($('#asmobile').val() + " Blackberry");
}	
function mailboxTotal() {
	$('#mailbox_price').html("$10.00/per mailbox");
}

function calcTotal() {
	mailboxTotal();	
	var mailboxes = $('#mailboxes').val() * 10.00;
	var additionalMailboxes = $('#additional_mailboxes').val() * 10.00 || 0;
	var storage = $('#add_storage').val() * 3.00 || 0;
	var bbmobile = $('#bbmobile').val() * 10.00 || 0;
	var asmobile = $('#asmobile').val() * 3.00 || 0;
	var total = parseInt(mailboxes) + parseInt(additionalMailboxes) + parseInt(storage) + parseInt(bbmobile) + parseInt(asmobile);
	var formattedTotal = "$" + total;
	$('#final_total').html(formattedTotal + ".00");
}

$(function() {
	$('#mailboxes').change(function() { 
		if ($(this).val() != '') { 
			enableOptions();
			calcTotal();
		} else {
			disableOptions();
			resetValues();
		}			
	});
}); 

$(function() {
	$('#additional_mailboxes').change(function() { 
		calcTotal();			
	});
}); 
$(function() {
	$('#add_storage').change(function() { 
		$('#total_storage').html($('#add_storage').val() + "GB extra");
		calcTotal();			
	});
});
$(function() {
	$('#bbmobile').change(function() { 
		$('#bb_total').html($('#bbmobile').val() + " Blackberry");
		calcTotal();			
	});
});
$(function() {
	$('#asmobile').change(function() { 
		$('#as_total').html($('#asmobile').val() + " Blackberry");
		calcTotal();			
	});
});
