
function getPrices()
{
	var scroll_result = (undefined != arguments[0]) ? arguments[0] : true;

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

}

function updatePrices(data, scroll_result)
{
	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, billingFrequency);
		count++;
	});
	var numMonths = getBillingMonths(billingFrequency);
	var total = ($(data).find('quote > amount').text() * 1);
	var total = (total * numMonths);
	
	if (0 < total)
	{
		addRow(monthlyTable, 'Total for ' + billingFrequency + ' Billing', total, 'dark');
		
		$('div#notesDiv').css('display', 'block');
		$('div#printLink').css('display', 'block');

		if (scroll_result)
		{
			// scroll result into view
			targetOffset = $('div#printLink').offset().top - 450;
			window.scrollTo(0,targetOffset);
		}
	}
	else
	{
		// no total, so no price to show
		$('div#notesDiv').css('display', 'none');
		$('div#printLink').css('display', 'none');
	}
}

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

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

function addRow(table, name, price, myClass)
{
	displayPrice = 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)
    {
        if (0 > price)
        {
            row += "-$";
    	    row += displayPrice + "</nobr></td>";
        }
        else
        {
            row += "$";
    	    row += displayPrice + "</nobr></td>";
        }
    }
	row += "</tr>";
	table.append(row);
}

function addCommas(nStr)
{
    nStr = Math.abs(nStr);
    nStr = nStr.toFixed(2);
	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 prepForm(showHosting)
{
	if (showHosting)
	{
		$('input[id=product1]').attr('checked', 'checked');
	}
	if ($('input[id=product1]').attr('checked'))
	{
		showHostingOptions();
	}
	else
	{
		hideHostingOptions();
	}
}

function showHostingOptions()
{
	$('select#quota_option').removeAttr('disabled');
	$('label#quota_option_label').removeAttr('class');
}

function hideHostingOptions()
{
	$('select#quota_option').attr('disabled', 'true');
	$('label#quota_option_label').attr('class', 'is_disabled');
}

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();
	});
}

