// JavaScript Document
function number_format(number, decimals, decPoint, thousandsSep)
{
	if(!decimals || decimals == '')        { decimals = '';      } // how many decimal places?
	if(!decPoint || !decPoint == '')       { decPoint = '.';     } // default decimal point
	if(!thousandsSep || thousandsSep == '') { thousandsSep = ','; } // default thousands sperator
	
	// convert number to string
	rawNumber = number.toString();
	
	// split number at decimal
	tempNumber = rawNumber.split(decPoint);
	
	afterDecimal = '';
	if(decimals >= 1)
	{
		if(tempNumber[1] >= 0)
		{
			afterDecimal = tempNumber[1].substr(0, decimals);
		}
		else
		{
			afterDecimal = '';
		}
	
		if(afterDecimal.length != decimals)
		{
			for(i=afterDecimal.length; i < decimals; i++)
			{
				afterDecimal += '0';
			}
		}
			
		afterDecimal = decPoint + afterDecimal;
	}
	
	formattedNumber = tempNumber[0] + afterDecimal;
	
	return formattedNumber;
}

function calculateCost(postCards, postageClass)
{
	/*
	PRICE LIST: 
	(Per Year)

	600 - 2400 $.34/Card 
	2400 - 3600 $.32/Card 
	3600 - 7800 $.31/Card 
	7800 - 12000 $.28/Card 
	12000 - 24000 $.25/Card 

	Add $.30/card for Standard 1st Class Postage 
	Add $.42/card for First Class Postage 
	Add $.04 /card for COLOR imprinting 

	(Prices subject to change  Shipping quotes required)
	*/
	// Annual
	postCards = (postCards * 12);
	
	// Cost Per Postcard
	if(postCards <= (199 * 12))       { var costPerCard = 0.42; } // lower than 2400
	else if(postCards <= (350 * 12))  { var costPerCard = 0.39; } //  2400 - 3599
	else if(postCards <= (650 * 12))  { var costPerCard = 0.385; } //  3600 - 7799
	else if(postCards <= (1000 * 12)) { var costPerCard = 0.365; } //  7800 - 11999
	else if(postCards <= (2000 * 12)) { var costPerCard = 0.355; } // 12000 - 24000
	else if(postCards >= (2001 * 12)) { var costPerCard = 0.33; } // 12000 - 24000
	
	// Color Imprinting
	// costPerCard = (costPerCard + 0.07);
	
	// Postage Rate
	if(postageClass == 'first') { var postageRate  = 0.42; /* Standard Mail Postage */ }
	else                        { var postageRate  = 0.30; /* First Class Postage */ }
	
	// Calculate Total
	monthlyCards = (postCards / 12);
	cardTotal    = (costPerCard * monthlyCards);
	postageTotal = (postageRate * monthlyCards);
	cost         = (cardTotal + postageTotal);
	
	if(postageRate == .3)
	{
		postageString = '0.30'.toString();
	}
	else
	{
		postageString = postageRate;
	}
	
	// Return data to predefined IDs
	document.getElementById('totalCardCost').innerHTML    = '$' + number_format(costPerCard, 2) + '/ea x ' + monthlyCards + ' = $' + number_format(cardTotal, 2); // $0.00/ea x 0 = $0.00
	document.getElementById('totalPostageRate').innerHTML = '$' + postageString + '/ea x ' + monthlyCards + ' = $' + number_format(postageTotal, 2); // $0.00/ea x 0 = $0.00
	document.getElementById('esitmatedCost').innerHTML    = '$' + number_format((postageRate + costPerCard),2) + '/ea x ' + monthlyCards + ' = $' + number_format(cost, 2); // $0.00/ea x 0 = $0.00
}
