/********************************************************************
* this function puts the decimal in the subtotal amount and limits 
* the cents value 2 places
********************************************************************/

function FormatNumber(expr, decplaces) 
{
	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
	while (str.length <= decplaces) 
	{
		str = "0" + str;
	}

	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint, str.length);
}

/********************************************************************
* this function adds all the subtotals to give a month and yearly total
********************************************************************/

function frmValid(form) 
{
	if (isNaN(document.forms[0].cig_a.value)) 

	{
		alert ("Please enter a valid number");
		document.forms[0].cig_a.focus();
		document.forms[0].cig_a.select();
		return false;
	}
	
	if (isNaN(document.forms[0].cig_b.value)) 

	{
		alert ("Please enter a valid number");
		document.forms[0].cig_b.focus();
		document.forms[0].cig_b.select();
		return false;
	}

    if (isNaN(document.forms[0].alcohol_a.value)) 
	{
		alert ("Please enter a valid number");
		document.forms[0].alcohol_a.focus();
		document.forms[0].alcohol_a.select();
		return false;
	}
    if (isNaN(document.forms[0].alcohol_b.value)) 
	{
		alert ("Please enter a valid number");
		document.forms[0].alcohol_b.focus();
		document.forms[0].alcohol_b.select();
		return false;
	}
    if (isNaN(document.forms[0].alcohol_c.value)) 
	{
		alert ("Please enter a valid number");
		document.forms[0].alcohol_c.focus();
		document.forms[0].alcohol_c.select();
		return false;
	}
   
	return true;
		 	
}



function a_plus_b(form) 
{
	a=eval(form.cig_ans.value)
	b=eval(form.alcohol_ans.value)
		g=(a+b)
	form.month_total.value = FormatNumber(g, 2)
		i=g*12
	form.year_total.value = FormatNumber(i, 2)
		 	
}

/********************************************************************
* this function checks for any Null values, an empty space 
* and a non-numeric values in the Cigarette equation then multiplies them
* to get a subtotal 
********************************************************************/
function a_times_b(form) 
{
	if (document.forms[0].cig_a.value=="")
	{
		alert ("You did not enter a number here, please enter a valid number");
		document.forms[0].cig_a.focus();
		document.forms[0].cig_a.select();
		return false;
	}
	
	if (document.forms[0].cig_a.value==" ")
	{
		alert ("You did not enter a number here, please enter a valid number");
		document.forms[0].cig_a.focus();
		document.forms[0].cig_a.select();
		return false;
	}

	if (isNaN(document.forms[0].cig_a.value)) 
	{
		alert ("Please enter a valid number");
		document.forms[0].cig_a.focus();
		document.forms[0].cig_a.select();
		return false;
	}
		if (document.forms[0].cig_b.value=="") 
		{
			alert ("You did not enter a number here, please enter a valid number");
			document.forms[0].cig_b.focus();
			document.forms[0].cig_b.select();
			return false;
		}
		
		if (document.forms[0].cig_b.value==" ")
		{
			alert ("You did not enter a number here, please enter a valid number");
			document.forms[0].cig_b.focus();
			document.forms[0].cig_b.select();
			return false;
		}

		if (isNaN(document.forms[0].cig_b.value)) 
		{
			alert ("Please enter a valid number");
			document.forms[0].cig_b.focus();
			document.forms[0].cig_b.select();
			return true;
		}
	{
		a=eval(form.cig_a.value)
		b=eval(form.cig_b.value)
		c=(a*b)*4
		form.cig_ans.value=FormatNumber(c, 2)
	}	
}

/********************************************************************
* this function checks for any Null values, an empty space 
* and a non-numeric values in the Alcohol equation then multiplies them
* to get a subtotal 
********************************************************************/
function a_times_b_times_c(form) 
{
	if (document.forms[0].alcohol_a.value=="") 
	{
		alert ("You did not enter a number here, please enter a valid number");
		document.forms[0].alcohol_a.focus();
		document.forms[0].alcohol_a.select();
		return false;
	}

	if (document.forms[0].alcohol_a.value==" ") 
	{
		alert ("You did not enter a number here, please enter a valid number");
		document.forms[0].alcohol_a.focus();
		document.forms[0].alcohol_a.select();
		return false;
	}

	if (isNaN(document.forms[0].alcohol_a.value)) 
	{
		alert ("Please enter a valid number");
		document.forms[0].alcohol_a.focus();
		document.forms[0].alcohol_a.select();
		return false;
	}
		if (document.forms[0].alcohol_b.value==" ") 
		{
			alert ("You did not enter a number here, please enter a valid number");
			document.forms[0].alcohol_b.focus();
			document.forms[0].alcohol_b.select();
			return false;
		}
		
		if (document.forms[0].alcohol_b.value=="") 
		{
			alert ("You did not enter a number here, please enter a valid number");
			document.forms[0].alcohol_b.focus();
			document.forms[0].alcohol_b.select();
			return false;
		}

		if (isNaN(document.forms[0].alcohol_b.value)) 
		{
			alert ("Please enter a valid number");
			document.forms[0].alcohol_b.focus();
			document.forms[0].alcohol_b.select();
			return false;
		}
			if (document.forms[0].alcohol_c.value==" ") 
			{
				alert ("You did not enter a number here, please enter a valid number");
				document.forms[0].alcohol_c.focus();
				document.forms[0].alcohol_c.select();
				return false;
			}

			if (document.forms[0].alcohol_c.value=="") 
			{
				alert ("You did not enter a number here, please enter a valid number");
				document.forms[0].alcohol_c.focus();
				document.forms[0].alcohol_c.select();
				return false;
			}

			if (isNaN(document.forms[0].alcohol_c.value)) 
			{
				alert ("Please enter a valid number");
				document.forms[0].alcohol_c.focus();
				document.forms[0].alcohol_c.select();
				return true;
			}
	{
		a=eval(form.alcohol_a.value)
		b=eval(form.alcohol_b.value)
		c=eval(form.alcohol_c.value)
		d=((a*b)*c)*4
		form.alcohol_ans.value= FormatNumber(d, 2)
	}
}




