function dollarFormat(n)
{
   var dollar = parseFloat(n);
   var dollarAmt = "0.00";

  if ( dollar != 0 )
  {
     n = Math.round(dollar * 100);

     dollarAmt = n;

    if ( n < 10 )
    {
      dollarAmt = "0.0" + n;
    }

    if ( ( n >= 10 ) && ( n <= 99 ) )
    {
      dollarAmt = "0." + n;
    }

    if ( n >= 100 )
    {
      dollarAmt = "" + n / 100 ;

      if ( dollarAmt.charAt(dollarAmt.length - 2) == '.' )
      {
        dollarAmt = dollarAmt + "0" ;
      }

      if ( dollarAmt.indexOf(".") == -1 )
      {
        dollarAmt = dollarAmt + ".00" ;
      }

    }

  }

  return (dollarAmt);
}

function stateTax(state, taxRate)
{
	this.state = state;
	this.taxRate = taxRate;
}
// Start building the tax table.
var taxTable = new Array();

taxTable[1] = new stateTax("CA", "8.25");

taxTable[2] = new stateTax("CA,", "8.25");

taxTable[3] = new stateTax("CA.", "8.25");

taxTable[4] = new stateTax("CAL", "8.25");

//End building the tax table

function setTax()
{

// Erasing any previous taxRate if it exists on the form.
	document.theForm.tax_rate.value=dollarFormat(0);

	var stateCode = "";
	var currState = "";
	var tax = 0;
	var tax_rate = 0;
	var subtotal = document.theForm.subtotal.value;

	//formatState();


	stateCode = document.theForm.b_state.value;
	//setCountry(1, stateCode);

// Read the table and set the tax field appropriatly.

	for (var i=1; i<=taxTable.length - 1; i++)
	{
		currState = taxTable[i].state.toUpperCase();
		stateCode = stateCode.toUpperCase();

		if ( currState == stateCode )
		{
			tax_rate = dollarFormat(taxTable[i].taxRate);
			i = taxTable.length;
		}
	}
	tax = subtotal * tax_rate/100;
	document.theForm.tax_rate.value=tax_rate;
	document.theForm.tax.value = dollarFormat(tax);
	//setCountryTax();
	calcTotal();

}

// End building the tax tables and initializing the objects.

function setShipping()
{
	var shippingMethod = document.theForm.shipmethod.value;
	var shippingCost = 0;
	if (shippingMethod == "UPS")
	{
		shippingCost = document.theForm.UPS.value;
		document.theForm.discount_amount_shipping.value = document.theForm.discounts_shipping.value;
	}
	else if (shippingMethod == "AIR2")
	{
		shippingCost = document.theForm.AIR2.value;
		document.theForm.discount_amount_shipping.value = 0;
	}
	else if (shippingMethod == "AIR1")
	{
		shippingCost = document.theForm.AIR1.value;
		document.theForm.discount_amount_shipping.value = 0;
	}
	document.theForm.shipping.value = dollarFormat(shippingCost);
	calcTotal();
}

/* Completely taking over this function */
function calcTotal()
{
	updateTotalCalc(); // yes, temporary online. Refactor out later	
}

/*
function calcTotal()
{
	var subtotal = Number(document.theForm.subtotal.value);
	var tax = Number(document.theForm.tax.value);
	var shipping = Number(document.theForm.shipping.value);
	var deposit = Number(document.theForm.deposit_amount.value);
	
	//alert(document.theForm.deposit.checked);
	if (document.theForm.deposit_only.checked)
	{
		var total = deposit;
	}
	else
	{
		//var discounts = Number(document.theForm.discounts.value);
		//var discounts_shipping = Number(document.theForm.discount_amount_shipping.value);
		//var total = subtotal + tax + shipping - discounts - discounts_shipping;
		//var total = subtotal + tax + shipping - discounts_shipping;
		var total = subtotal + tax + shipping;
	}
	//document.theForm.total.innerHTML = dollarFormat(total);
	document.getElementById("total").innerHTML = dollarFormat(total);

}
*/

function address(value) {

	if (value == 'y') {
		document.theForm.s_salutation.disabled = true;
		document.theForm.s_salutation.style.backgroundColor = '#AAAAAA';
		//document.theForm.s_company.disabled = true;
		//document.theForm.s_company.style.backgroundColor = '#AAAAAA';
		document.theForm.s_fname.disabled = true;
		document.theForm.s_fname.style.backgroundColor = '#AAAAAA';
		document.theForm.s_lname.disabled = true;
		document.theForm.s_lname.style.backgroundColor = '#AAAAAA';
		document.theForm.s_address1.disabled = true;
		document.theForm.s_address1.style.backgroundColor = '#AAAAAA';
		document.theForm.s_address2.disabled = true;
		document.theForm.s_address2.style.backgroundColor = '#AAAAAA';
		document.theForm.s_city.disabled = true;
		document.theForm.s_city.style.backgroundColor = '#AAAAAA';
		document.theForm.s_state.disabled = true;
		document.theForm.s_state.style.backgroundColor = '#AAAAAA';
		document.theForm.s_zip.disabled = true;
		document.theForm.s_zip.style.backgroundColor = '#AAAAAA';
		document.theForm.s_country.disabled = true;
		document.theForm.s_country.style.backgroundColor = '#AAAAAA';
		//document.theForm.s_phone.disabled = true;
		//document.theForm.s_phone.style.backgroundColor = '#AAAAAA';

	}
	else if (value == 'n') {
		document.theForm.s_salutation.disabled = false;
		document.theForm.s_salutation.style.backgroundColor = '#FFFFFF';
		//document.theForm.s_company.disabled = false;
		//document.theForm.s_company.style.backgroundColor = '#FFFFFF';
		document.theForm.s_fname.disabled = false;
		document.theForm.s_fname.style.backgroundColor = '#FFFFFF';
		document.theForm.s_lname.disabled = false;
		document.theForm.s_lname.style.backgroundColor = '#FFFFFF';
		document.theForm.s_address1.disabled = false;
		document.theForm.s_address1.style.backgroundColor = '#FFFFFF';
		document.theForm.s_address2.disabled = false;
		document.theForm.s_address2.style.backgroundColor = '#FFFFFF';
		document.theForm.s_city.disabled = false;
		document.theForm.s_city.style.backgroundColor = '#FFFFFF';
		document.theForm.s_state.disabled = false;
		document.theForm.s_state.style.backgroundColor = '#FFFFFF';
		document.theForm.s_zip.disabled = false;
		document.theForm.s_zip.style.backgroundColor = '#FFFFFF';
		document.theForm.s_country.disabled = false;
		document.theForm.s_country.style.backgroundColor = '#FFFFFF';
		//document.theForm.s_phone.disabled = false;
		//document.theForm.s_phone.style.backgroundColor = '#FFFFFF';

	}
}
