// This function calculates the tip amount

// This calculates the exact percent of the bill
function exactPercent( bill, percent )
{
	return bill * percent;
}

// This function finds the tip percent. It rounds it to the nearest integer.
function tipPercent( bill, tip )
{
	percent = (tip / bill) * 100;
	return percent.toFixed();
}

// Bar Tip Calculator
// This function is going to calculate the tip and display the total of the bill
function updateBarTip()
{
	bill = 0.0;
	tip = 0;
	
	// Get all the data for the fields
	bill = parseFloat( document.getElementById( "bill" ).value );
	// I'm not allowing the user to choose a percent
	/*p = document.getElementById( "barTipPercent" );
	percent = p.options[ p.selectedIndex ].value;*/	
	
	// The percent is goint to be 20% (if I change my mind, this should be deleted)
	percent = 0.2;
	
	if( !isNaN( bill ) && bill > 0 )
	{
		// Exact Tip
		exactBarTip = exactPercent( bill, percent );		
		// Round the tip
		tip = Math.round( exactBarTip );
		// The tip should be at least $1
		if( tip < 1 )
		{
			tip = 1;
		}
		// Total is the bill + the rounded tip
		total = bill + tip;
		
		// I'm not going to display the exact tip
		//document.getElementById( "exactBarTip" ).value = exactBarTip.toFixed(2);
		// The round tip is going to be displayed
		document.getElementById( "barTipAmount" ).value = tip;
		// The total
		document.getElementById( "barTotal" ).value = total.toFixed(2);
		
	}
	else
	{
		//document.getElementById( "exactBarTip" ).value = "";
		document.getElementById( "barTipAmount" ).value = "";
		document.getElementById( "barTotal" ).value = "";
	}	
}

// Restaurant Tip Calculator
function updateTip( roundTip )
{
	
	subtotal = 0.0;
	tax = 0.0;
	check = 0.0;
	total = 0.0;
	
	// Get all the data for the fields
	subtotal = parseFloat( document.getElementById( "subtotal" ).value );
	tax = parseFloat( document.getElementById( "tax" ).value );
	p = document.getElementById( "tipPercent" );
	percent = p.options[ p.selectedIndex ].value;
	
	if( !isNaN( subtotal ) && subtotal > 0 )
	{
		// If the subtotal is a number then get the tax
		if( !isNaN( tax ) && tax >= 0 )
		{
			check = subtotal + tax;
			// Display the check amount
			document.getElementById( "check" ).value = check.toFixed(2);
			
			// If Exclude Tax is checked, get the percent by the subtotal
			if( document.getElementById( "checkbox" ).checked )
			{
				// If bad service, tip is a quarter
				if( percent == -1 )
				{
					tip = 0.25;
				}
				// Calculate the tip on the Subtotal
				else
				{
					tip = exactPercent( subtotal, percent );
				}
			}
			// If it is not checked, include the tax on the calculation of the tip
			else
			{
				// If bad service, tip is a quarter
				if( percent == -1 )
				{
					tip = 0.25;
				}
				else
				{
					// Calculate the tip on the Check Amount
					tip = exactPercent( check, percent );
				}
			}			
			
			if( roundTip == "no" )
			{
				document.getElementById( "round1" ).checked = true;
			}
			
			// Round the tip
			if( roundTip == "tip" )
			{
				tip = Math.round( tip );				
			}		
			
			// Find the total
			total = check + tip;
			
			// Round the Total
			if( roundTip == "total" )
			{
				total = Math.round( total );
				
				// If the total is less than the check amount, add one dollar
				if( total < check )
				{
					total = total + 1;						
				}
				
				// Calculate the tip
				tip = total - check;
			}
			// Display the tip amount			
			document.getElementById( "tipAmount" ).value = tip.toFixed(2);
			// Display the total amount to pay
			document.getElementById( "total" ).value = total.toFixed(2);
		}
		else
		{
			document.getElementById( "check" ).value = "";			
			document.getElementById( "tipAmount" ).value = "";
			document.getElementById( "total" ).value = "";			
		}
	}
	else
	{
		document.getElementById( "check" ).value = "";
		document.getElementById( "tipAmount" ).value = "";
		document.getElementById( "total" ).value = "";
	}
	
}
