
function getPrices()
{
	var numEmailAddresses = $("input[name='numEmailAddresses']").val();
	if (isNaN(numEmailAddresses))
	{
		alert('"' + numEmailAddresses + '" is not a number');
		return;
	}
	var billingFrequency = $("select[name='billingFrequency']").val();
	var product = $("input[name='product']:checked").val();
	var quotaOption = "";
	if (2 == product)
	{
		quotaOption = $("select[name='quotaOption']").val();
	}
	var numAliases = $("input[name='numAliases']").val();
	$.post(
		'/services/pricing_info',
		{
			action: 'get_prices',
			numEmailAddresses: numEmailAddresses,
			billingFrequency: billingFrequency,
			product: product,
			quotaOption: quotaOption,
			numAliases: numAliases
		},
		function(data) {
			updatePrices(data);
		},
		'xml'
	);
}

function updatePrices(data)
{
	var numEmailAddresses = $(data).find('numEmailAddresses').text();
	var billingFrequency = $(data).find('billingFrequency').text();

	var monthlyTable = $('table#monthlyTable').empty();
	var pfDiscount = 0;
	var count = 0;
	$(data).find('lineItem').each(function() {
		var myClass = (count) ? 'light' : '';
		displayLineItem(monthlyTable, $(this), myClass);
		if ('PaymentFrequencyDiscount' == $(this).attr('name'))
		{
			pfDiscount = $(this).find('amount').text();
		}
		count++;
	});
	var total = ($(data).find('quote > amount').text() * 1).toFixed(2);
	addRow(monthlyTable, 'Monthly Cost', total, 'dark');
	if ('Monthly' != billingFrequency)
	{
		var numMonths = getBillingMonths(billingFrequency);
		var pfDiscountTotal = (pfDiscount * numMonths).toFixed(2);
		var total = (total * numMonths).toFixed(2);
		addRow(monthlyTable, '', '');
		addRow(monthlyTable, 'Discount for ' + billingFrequency + ' Payment in Advance', pfDiscountTotal);
		addRow(monthlyTable, 'Total for ' + billingFrequency + ' Billing', total, 'light');
	}
	$('div#notesDiv').css('display', 'block');
	$('div#printLink').css('display', 'block');

	// scroll result into view
	targetOffset = $('div#printLink').offset().top - 450;
	window.scrollTo(0,targetOffset);
}

function displayLineItem(table, item, myClass)
{
	var desc = item.find('displayName').text();
	var code = item.attr('code');
	var amt = (item.find('amount').text() * 1).toFixed(2);
	var type = item.attr('type');
	var unit = item.find('unit').text();
    
	if (item.find('details').text())
	{
        desc += " (" + item.find('details').text() + ")";
    }
	if ('PaymentFrequencyDiscount' == code)
	{
		desc = "<em>" + desc + "</em>";
	}

	addRow(table, desc, amt, myClass);
}

function addRow(table, name, price, myClass)
{
	price = addCommas(price);
	var myClass = (myClass) ? " " + myClass : "";
	var row = "<tr>";
	row += "<td class='nameCol" + myClass + "'><nobr>" + name + "</nobr></td>";
	row += "<td class='numberCol" + myClass + "'><nobr>";
	if (price) { row += "$"; }
	row += price + "</nobr></td>";
	row += "</tr>";
	table.append(row);
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function getBillingMonths(billingFrequency)
{
	switch (billingFrequency)
	{
		case 'Monthly':
			return 1
			break;
		case 'Quarterly':
			return 3
			break;
		case 'Semi-annual':
			return 6
			break;
		case 'Annual':
			return 12 
			break;
		default:
			return 1;
	}
}

function pluralize(item)
{
	var count = item.find('unitCount').text();
	var unit = item.find('unit').text();
	if (count != 1)
	{
		var s = ["domain"];
		for (var i = 0; i<s.length; i++)
		{
			if (s[i] == unit) { return "s"; }
		}
		var es = ["address", "email address"];
		for (var i = 0; i<es.length; i++)
		{
			if (es[i] == unit) { return "es"; }
		}
		return "";
	}
	else
	{
		return "";
	}
}

function showHostingOptions()
{
	$('tr#quotaOptionRow').show();
	//$('tr#numAliasesRow').show();
}

function hideHostingOptions()
{
	$('tr#quotaOptionRow').hide();
	//$('tr#numAliasesRow').hide();
}

function showPrinterVersion()
{
	var d = new Date();
	var today = (d.getMonth()+1) + "-" + d.getDate() + "-" + d.getFullYear();
	var html = "";
	var quote = $('div#quote').html();
	var style = $('style#pricing').html();
	html += "<html>\n";
	html += "<head>\n"
	//html += "<style>@import url('/common/styles/default.css');</style>";
	html += "<style>@import url('/css/pricing_print.css');</style>\n";
	html += "</head>\n";
	html += "<body>\n";
	html += "<table border='0' cellpadding='0' cellspacing='0' align='center' height='100%'>\n";
	html += "<tr>\n";
	html += "<td height='1'>\n";
	html += "<table border='0' cellpadding='0' cellspacing='0' align='center'>\n";
	html += "<tr>\n";
	html += "<td style='padding-right: 25px;'>\n";
	html += "<img src='/images/logo_ome.gif' width='264' height='71'>\n";
	html += "</td>\n";
	html += "</tr>\n";
	html += "<tr>\n";
	html += "<td align='center' style='padding-top: 3em;'>\n";
	html += "<h1><nobr>MX-Defender Price Quote</nobr></h1>\n";
	html += "<span class='medium'><strong>" + today + "</strong></span></td>\n";
	html += "</td>\n";
	html += "</tr>\n";
	html += "</table>\n";
	html += "</td>\n";
	html += "</tr>\n";
	html += "<tr>\n";
	html += "<td align='center' style='padding-top: 2em; padding-bottom: 4em;'>\n";
	html += quote;
	html += "</td>\n";
	html += "</tr>\n";
	html += "<tr>\n";
	html += "<td align='center'>\n";
	html += "<span class='medium'><strong>For more information visit www.OnlyMyEmail.com</strong></span></td>\n";
	html += "</td>\n";
	html += "</tr>\n";
	html += "</table>\n";
	html += "</body>\n";
	html += "</html>";
	myWin = window.open('', 'printerVersion');
	$(myWin.document).ready( function() {
		myWin.document.open();
		myWin.document.write(html);
		myWin.document.close();
	});
}
