
function parseDecimal(d, zeros, trunc) { try { d = d.replace(/[a-zA-Z\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\|\[\]\\\:\"\;'\<\>\?\,\/\~\`]/g, ""); } catch (e) { return 0; } while (d.indexOf(".") != d.lastIndexOf(".")) d = d.replace(/\./, ""); if (typeof zeros == 'undefined' || zeros == "") { return parseFloat(d); } else { var mult = Math.pow(10, zeros); if (typeof trunc == 'undefined' || (trunc) == false) return parseFloat(Math.round(d * mult) / mult); else return parseFloat(Math.floor(d * mult) / mult); } }

function cleanMoney(d) {
	// hack to make sure there are 2 places after the decimal in the grand total.
	var ds = "" + d;
	var l = ds.length;
	var p = ds.indexOf('.');
	while (l - p <= 2) {
		ds = ds + "0";
		l = ds.length;
		p = ds.indexOf('.');
	}
	if (l - 3 > p) {
		ds = ds.substr(0, p + 2);
	}
	if (ds.length == "00") ds = "0.00";
	return ds;
}

$(document).ready(function() { if ($('div.feature').length > 0) prepareFeatures(); });

function prepareFeatures() { var features = $('.feature'); var highestZ = 5000; for (var i = features.length; i-- > 0; ) { var el = $(features[i]); var offset = $(el).position(); $(el).attr('id', 'div-feature' + i); $(el).css('position', 'absolute'); $(el).css('left', offset.left); $(el).css('top', offset.top); $(el).css('z-index', highestZ - i); $(el).bind('mouseenter', function(e) { $(this).addClass('feature-hover'); $('.details', this).show('fast'); }); $(el).bind('mouseleave', function(e) { $(this).removeClass('feature-hover'); $('.details', this).hide('fast'); }); } }

function setPaymentMethod() {
	var paymentOption = $('input[name=paymentMethod]:checked').attr('value');
	$('.payment-option').hide();
	$('.payment-option-' + paymentOption).show();
	if (paymentOption == "offline") {
		$('.shipping-address-info').show();
	}
	else {
		if ($('#sameBillingAndShippingAddress:checked').length > 0) $('.shipping-address-info').hide();
		else $('.shipping-address-info').show();
	}
}

function enableBillingInput() {
	$('input[type=text]').each(function() {
		var n = $(this).attr('name');
		if ($(this).attr('name').indexOf('ShippingAddress.') > -1) {
			$(this).bind('change', function() {
				updateShippingOptions();
				updateTax();
			});

			$(this).bind('blur', function() {
				updateShippingOptions();
				updateTax();
			});
		}
	});
	$('select').each(function() {
		var n = $(this).attr('name');
		if ($(this).attr('name').indexOf('ShippingAddress.') > -1) {
			$(this).bind('change', function() {
				updateShippingOptions();
				updateTax();
			});

			$(this).bind('blur', function() {
				updateShippingOptions();
				updateTax();
			});
		}
	});
}

function bindBillingToShipping() {
	$('input[type=text]').each(function() {
		var n = $(this).attr('name');
		if ($(this).attr('name').indexOf('BillingAddress.') > -1) {
			$(this).bind('keyup', function() {
				var bound = $('#sameBillingAndShippingAddress:checked').length > 0;
				if (bound) {
					var n = $(this).attr('name').replace('Billing', 'Shipping');
					var v = $(this).val();
					$('input[name=' + n + ']').val(v);
				}
			});

			$(this).bind('blur', function() {
				var bound = $('#sameBillingAndShippingAddress:checked').length > 0;
				if (bound) {
					var n = $(this).attr('name').replace('Billing', 'Shipping');
					var v = $(this).val();
					var fld = $('input[name=' + n + ']');
					if (fld.val() == v) return;
					fld.val(v);
					updateShippingOptions();
					updateTax();
				}
			});

			$(this).bind('change', function() {
				var bound = $('#sameBillingAndShippingAddress:checked').length > 0;
				if (bound) {
					updateShippingOptions();
					updateTax();
				}
			});
		}
	});
	$('select').each(function() {
		var n = $(this).attr('name');
		if ($(this).attr('name').indexOf('BillingAddress.') > -1) {
			$(this).bind('change', function() {
				var bound = $('#sameBillingAndShippingAddress:checked').length > 0;
				if (bound) {
					var n = $(this).attr('name').replace('Billing', 'Shipping');
					var v = $(this).attr('selectedIndex');
					$('select[name=' + n + ']').attr('selectedIndex', v);
					updateShippingOptions();
					updateTax();
				}
			});

			$(this).bind('blur', function() {
				var bound = $('#sameBillingAndShippingAddress:checked').length > 0;
				if (bound) {
					var n = $(this).attr('name').replace('Billing', 'Shipping');
					var v = $(this).attr('selectedIndex');
					var fld = $('select[name=' + n + ']');
					if (fld.attr('selectedIndex') == v) return;
					fld.attr('selectedIndex', v);
					updateShippingOptions();
					updateTax();
				}
			});
		}
	});
}

function updateShippingAddress() {
	var same = $('#sameBillingAndShippingAddress:checked').length > 0;
	if (same) {
		$('input[type=text]').each(function() {
			var n = $(this).attr('name');
			if ($(this).attr('name').indexOf('BillingAddress.') > -1) {
				var n = n.replace('Billing', 'Shipping');
				var v = $(this).val();
				$('input[name=' + n + ']').val(v);
			}
		});

		$('select').each(function() {
			var n = $(this).attr('name');
			if ($(this).attr('name').indexOf('BillingAddress.') > -1) {
				var n = n.replace('Billing', 'Shipping');
				var v = $(this).attr('selectedIndex');
				$('select[name=' + n + ']').attr('selectedIndex', v);
			}
		});

		$('.shipping-address-info').hide();
	}
	else { $('.shipping-address-info').show(); }
}

function updateGrandTotal(selectedOption) {
	var isNull = (selectedOption == undefined || selectedOption == null);
	var s = isNull ? 0 : $(selectedOption).attr('shippingCost');
	var m = isNull ? '' : $(selectedOption).attr('shippingMethod');
	var t = $('#subtotalAmount').text();
	var d = $('#discountAmount').length > 0 ? $('#discountAmount').text() : '0.00';
	var x = $('#taxAmount').text();
	$('#shippingAmount').text('$' + s);
	var gt = parseDecimal((parseDecimal(t) + parseDecimal(s) + parseDecimal(x) - parseDecimal(d)).toString(), 2, true);
	// hack to make sure there are 2 places after the decimal in the grand total.
	var gtS = "" + gt; var l = gtS.length; var p = gtS.indexOf('.'); while (l - p <= 2) { gtS = gtS + "0"; l = gtS.length; p = gtS.indexOf('.'); }
	$('#grandtotalAmount').text('$' + gtS);
	if (s == undefined) s = 0;
	$('#Shipping').val(s);
	$('#ShippingMethod').val(m);
}

function setupShippingOptions(data) {
	$('#shippingOptions').html(data);
	$('.shipping-options input[type=radio]').click(function() { updateGrandTotal($(this)); });
	var shipPrice = $('#Shipping').val();
	var shipMethod = $('#ShippingMethod').val();
	$('.shipping-options input[type=radio]').each(function() {
		var tShipPrice = $(this).attr('shippingCost');
		var tShipMethod = $(this).attr('shippingMethod');
		if ($(this).attr('shippingCost') == shipPrice && $(this).attr('shippingMethod') == shipMethod) {
			$(this).attr('checked', 'checked');
			updateGrandTotal(this);
		}
	});
	if (updateShippingDiscount != undefined) updateShippingDiscount();
}