var edition = '';
var users = 0;
var license = '';
var price = 0;
var discount = 1;

$(document).ready(function(){

	$('#editions').change(function () {
		clear_prices();
		edition = $(this).val();
		if(run_checks())		
			show_prices();
	});

	$('#users').change(function () {
		users = $(this).val();
		
		if(!run_checks())
			return;
		
		clear_prices();
		discount = 1;
		$('#discount').html('');
		discount = calc_discount_percent();
		if (discount > 0)
			$('#discount').html(discount + '% Discount');				
		show_prices();
	});
	
	$('#licenses').change(function () {
		clear_prices();
		license = $(this).val();		
		if(run_checks())			
			show_prices();
	});

});

Number.prototype.formatMoney = function(c, d, t)
{
var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "." : d, t = t == undefined ? "," : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 
: 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

function run_checks()
{
		clear_prices();
		if (isNaN(users))
		{
			$('#discount').html('<span style="color: red">Please enter a numerical amount.</span>');
			return false;				
		}
		if (users < 0)
		{
			$('#discount').html('<span style="color: red">Please enter a positive amount.</span>');
			return false;	
		}
		if(users == undefined || users == null || users === 0)
			return false;

		switch(edition)
		{
			case 'PostBooks':
				if (discount > 0)
      		$('#discount').html(discount + '% Discount'); 
			break;

			case 'Enterprise':
				if(license == 'onsite' || license == 'Perpetual')
				{
					if (users < 10)
					{
						$('#discount').html('<span style="color: red">You need at least 10 users for the selected license and edition.</span>');
						return false;
					}
					else
					{
						if (discount > 0)
          		$('#discount').html(discount + '% Discount');
						else
							$('#discount').html('');		 
					}		
				}
				if(license == 'cloud')
        {
            if (discount > 0)
              $('#discount').html(discount + '% Discount');             
            else
              $('#discount').html('');     
        }
			break;
			
			case 'Standard':
			case 'Project':
			case 'Manufacturing':
				if(license == 'onsite' || license == 'Perpetual')
				{
					if (users < 5)
					{
						$('#discount').html('<span style="color: red">You need at least 5 users for the selected license and edition.</span>');
						return false;
					}
					else
					{							
						if (discount > 0)
          	$('#discount').html(discount + '% Discount'); 	
						else
							$('#discount').html('');		 
					}				
				}
				if(license == 'cloud')
				{
						if (discount > 0)
          		$('#discount').html(discount + '% Discount'); 						
						else
							$('#discount').html('');		 
				}
			break;
		}
		
		return true;
}

function clear_prices()
{	$('#license-total').html('');	
	$('#mait-total').html('');	
	$('#support-total').html('');	
	$('#license-monthly').html('');	
	$('#mait-monthly').html('');	
	$('#support-monthly').html('');	
}

function show_prices()
{
	if(users>0 && license != '' && edition != '')
	{
		var lic = Number(parseInt(calc_license(false))).formatMoney(0);		
		lic = '$'+lic;
		var lic_month = Number(parseInt(calc_license(true))).formatMoney(0);		
		lic_month = '$'+lic_month;

		if(license == 'Perpetual')
		{
			lic=lic+'&nbsp;(One Time)';
			lic_month=lic_month+"&nbsp;(No license payment after 1st year)";
		}

		var mait = calc_mait(false);
		if(mait == false)
			mait = 'Included';
		else
			mait = '$'+Number(parseInt(mait)).formatMoney(0);
		var mait_month = calc_mait(true);
		if(mait_month == false)
			mait_month = 'Included';
		else
			mait_month = '$'+Number(parseInt(mait_month)).formatMoney(0);
		
		var sup = Number(parseInt(calc_support(false))).formatMoney(0);
		sup = '$'+sup;
		var sup_month = Number(parseInt(calc_support(true))).formatMoney(0);
		sup_month = '$'+sup_month;

		$('#license-total').html(lic);
		$('#license-monthly').html(lic_month);
		$('#mait-total').html(mait);	
		$('#mait-monthly').html(mait_month);	
		$('#support-total').html(sup);
		$('#support-monthly').html(sup_month);
		$('#price').show();	
	}
}

function calc_discount_percent()
{
	if(users < 10 && users > 0)
	{
		return 0;	
	}
	if(users > 9 && users < 55)
	{
		for (var temp = 10; temp<55; temp++)
		{
			if (temp == users)
			{
				return discount;	
			}
			else
			{
				discount++;	
			}	
		}
	}
	if (users > 54)
		return 45;
}	

function calc_cost()
{
	var cost = 0;
	switch(license)
	{
		case 'onsite':
		
		switch(edition)
		{
			case 'PostBooks':
				cost = 400;
			break;
			case 'Standard':
				cost = 700;
			break;
			case 'Project':
				cost = 900;
			break;
			case 'Manufacturing':
				cost = 1200;
			break;
			case 'Enterprise':
				cost = 1500;
			break;				
		}		

		break;

		case 'cloud':
		
		switch(edition)
		{
			case 'PostBooks':
				cost = 70;
			break;
			case 'Standard':
				cost = 100;
			break;
			case 'Project':
				cost = 120;
			break;
			case 'Manufacturing':
				cost = 150;
			break;
			case 'Enterprise':
				cost = 180;
			break;				
		}		

		break;

		case 'Perpetual':
		
		switch(edition)
		{
			case 'PostBooks':
				cost = 1200;
			break;
			case 'Standard':
				cost = 2100;
			break;
			case 'Project':
				cost = 2700;
			break;
			case 'Manufacturing':
				cost = 3600;
			break;
			case 'Enterprise':
				cost = 4500;
			break;				
		}		

		break;

	}	

	return cost;
}

function calc_license(monthly)
{
	var cost = calc_cost();
	var total = (cost * users);
	if(monthly==true)
	{
		total = total - (total * (discount / 100));
		if (license == 'cloud')
		{
			return total;			
		}
		else
		{
			return (total / 10);
		}
	}
	else
	{
		total = total - (total * (discount / 100));
		if (license == 'cloud')
		{
			return (total * 10);			
		}
		else
			return total;
	}
}

function calc_mait(monthly)
{
	if (license == 'cloud' || license == 'onsite')
		return false;
	else
	{
		var cost = calc_cost();
		var total = (cost * users);
		//total = total - (total * (10 / 100))
		total = (total * (10 / 100));
		if(monthly==true)
		{
			return (total/10);
		}
		else
			return total;			
	}
}

function calc_support(monthly)
{
		var cost = calc_cost();
		var total = (cost * users);
		var license_temp = license;
		//total = total - (total * (15 / 100))
			license = "Perpetual";
			cost = calc_cost();
			total = (cost * users);
			license = license_temp;
			var support = (total * (10 / 100))
		
		if (monthly == true)
		{
			//var support = (total * (10 / 100))
			//if(license != 'cloud')
			//{
				support = support / 10;
			//}
				
			if (support > 150)
				return support;
			else
				return 150;	
		}
		else
		{
			//if(license == 'cloud')
			//{
			//	support = support * 10;
			//}

			if(support > 1500)
				return support;
			else
				return 1500;
		}
}



