
<!-- Paste this code into an external JavaScript file  -->

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Kevin Hartig :: http://www.grafikfx.net/ */

// Calculate the total for items in the form which are selected.
function calculateTotal(inputItem) {
  with (inputItem.form) {
	var product = inputItem.name
	var tvalue = inputItem.value
	var disc = inputItem.form[product + "_disc"].value
//	var amt = inputItem.value;
    // Process each of the different input types in the form.
    if (inputItem.type == "radio") {   // Process radio buttons.
	  var amt = inputItem.form[product + tvalue + "_price"].value
      // Subtract the previously selected radio button value from the total, add the current amount.
      calculatedTotal.value = eval(calculatedTotal.value) - eval(inputItem.form[product + "_prev"].value) + eval(amt);
      if (disc == "Y") {
	      discountTotal.value = eval(discountTotal.value) - eval(inputItem.form[product + "_prev"].value) + eval(amt);
      }
      // Save the current radio selection value.
      inputItem.form[product + "_prev"].value = eval(amt);
      // Add the current radio button selection value to the total.
//      calculatedTotal.value = eval(calculatedTotal.value) + eval(amt);
      inputItem.form[product + "_tot"].value= formatCurrency(amt);
    } else {   // Process check boxes.
	  var amt = inputItem.form[product + "_price"].value
      if (inputItem.checked == false) {   // Item was uncheck. Subtract item value from total.
          calculatedTotal.value = eval(calculatedTotal.value) - eval(amt);
	      if (disc == "Y") {
	      	discountTotal.value = eval(discountTotal.value) - eval(amt);
	      }
          inputItem.form[product + "_tot"].value= "";
      } else {   // Item was checked. Add the item value to the total.
          calculatedTotal.value = eval(calculatedTotal.value) + eval(amt);
	      if (disc == "Y") {
	      	discountTotal.value = eval(discountTotal.value) + eval(amt);
	      }
          inputItem.form[product + "_tot"].value= formatCurrency(amt);
      }
    }
    // Total value should never be less than 0.
    if (calculatedTotal.value < 0) {
      InitForm();
	} else {
		calcDiscount(inputItem);
    }
    // Return total value.
    return(formatCurrency(calculatedTotal.value));
  }
}


// Format a value as currency.
function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
     num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
      cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + num + '.' + cents);
}

// Format a value as currency.
function formatDiscountCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
     num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
      cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
  return ('(' + num + '.' + cents + ')');
}

function calcDiscount(inputItem) {
  with (inputItem.form) {
	var discCode = discountCode.value.toUpperCase();
	var discountAmt = 0;
	var discpercent = 0;
	discountPercent.value = "";
	if (discCode == "CS0531") {
		discpercent = .15;
		discountPercent.value = "15";
	} else {
  	if (discCode == "RSR777") {
  	 	discpercent = .50;
  		discountPercent.value = "50";
  	} else {
		  if (discCode > "") {
			 alert(discCode+" is not a valid discount code.");
			}
		}
	}
	discountAmount.value = eval(discountTotal.value) * eval(discpercent);
	calculatedTotal.value = eval(calculatedTotal.value) + eval(discountAmount_prev.value) - eval(discountAmount.value);
    inputItem.form["calculatedTotal"].value= formatCurrency(calculatedTotal.value);
//		alert("cT = "+calculatedTotal.value+", prev="+discountAmount_prev.value);
	discountAmount_prev.value = eval(discountAmount.value);
    inputItem.form["discountAmount"].value= formatDiscountCurrency(discountAmount.value);
    return(calculatedTotal.value);
  }
}
