function subscribeMain() { 
	jQuery.validator.messages.required = "";
	
	$("#editForm").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		}
	});
	
	$("select[ id ^= 'intSubscriptionQuantity_' ]").change(function() {updateTotals(this)});

	$("#strDiscountCode").change(function () {
			var params = "DiscountCode=" + escape($(this).val());
			$("#DiscountLookupResult").load("/subscribe/check-discount.cfm", params);
		}
	);
}

function updateTotals(e) {
	// loop through all selects and update total for each row and grand total
	// relies on values set in main page
	var grandTotal = 0.0;

	$("select[ id ^= 'intSubscriptionQuantity_' ]").each(function (i) { 
			var selectedValue = $(this).val();
			var total = 0.0;
			switch (selectedValue) {
				case '1': 
					total = oneRate;
					break;
				case '2':
					total = twoRate;
					break;
				case '3':
					total = threeRate;
					break;
			}
			grandTotal = grandTotal + total;
			
			var currentMagazineID = $(this).attr('id').substring($(this).attr('id').indexOf('_')+1);
			$('#magazineTotal_' + currentMagazineID).html(dollarformat(total));
		}
	);

	$('#orderTotal').html(dollarformat(grandTotal));		
}

 function dollarformat (number) {
	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(number);
	num.setPlaces('2');
	num.setCurrencyValue('$');
	num.setCurrency(true);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	num.setSeparators(true, ',', ',');
	return num.toFormatted();
}

