<!--
// Mail-To Shopping Cart with shipping-cost and autocalculation
// Copyright (C) 2005 Marc Ahlfs http://skreddypedals.com
// All Rights Reserved

var nextfield = "Name"; // name of first box on page
var netscape = "";
var enableEnter=true;

function noop() {
        return false;
}

window.onError = "noop";

function validate_order_form() {
  window.onError = "noop";
  var theForm = document.OrderForm;
  calculate_product_total();
  if (theForm.name.value == "") {
    alert("Please enter a value for the \"Name\" field.");
    theForm.name.focus();
    return (false);
  }
  if (theForm.name.value.length < 1) {
    alert("Please enter at least 1 characters in the \"Name\" field.");
    theForm.name.focus();
    return (false);
  }
  if (theForm.street.value == "") {
    alert("Please enter a value for the \"Street Address\" field.");
    theForm.street.focus();
    return (false);
  }
  if (theForm.street.value.length < 1) {
    alert("Please enter at least 1 characters in the \"Street Address\" field.");
    theForm.street.focus();
    return (false);
  }
  if (theForm.phone.value.length < 10) {
    alert("Please enter at a full 10-digit phone number in the \"Telephone\" field.");
    theForm.phone.focus();
    return (false);
  }
  if (theForm.city.value == "") {
    alert("Please enter a value for the \"City\" field.");
    theForm.city.focus();
    return (false);
  }
  if (theForm.city.value.length < 1) {
    alert("Please enter at least 1 characters in the \"City\" field.");
    theForm.city.focus();
    return (false);
  }
  if (theForm.state.value == "") {
    alert("Please enter a 2-character value for the \"State\" field.");
    theForm.state.focus();
    return (false);
  }
  if (theForm.state.value.length < 2) {
    alert("Please enter a 2-character value in the \"State\" field.");
    theForm.state.focus();
    return (false);
  }
  if (theForm.zip.value == "") {
    alert("Please enter a value for the \"Zip Code\" field.");
    theForm.zip.focus();
    return (false);
  }
  if (theForm.zip.value.length < 5) {
    alert("Please enter at least 5 characters in the \"Zip Code\" field.");
    theForm.zip.focus();
    return (false);
  }
  if (theForm.email.value == "") {
    alert("Please enter a value for the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }
  if (theForm.email.value.length < 5) {
    alert("Please enter at least 5 characters in the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }
  if (theForm.email.value.length > 255) {
    alert("Please enter at most 255 characters in the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }
  if (!validEMail(theForm.email.value)) {
    alert("The email address you entered appears to be invalid.");
    theForm.email.focus();
    return false;
  }
  return (true);
}

function validEMail(addy) {
        var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(addy)) return true;
        else return false;
}

function calculate_total(ProductID) {
        window.onError="noop";
        var PriceField = eval("document.OrderForm." + ProductID + "Price");
        var QuantField = eval("document.OrderForm." + ProductID + "Quantity");
        var TotalField = eval("document.OrderForm." + ProductID + "Total");
        if (QuantField) {
        	if (PriceField) {
        		if (TotalField) {
        			if (isNaN(QuantField.value)) QuantField.value = "0";
				QuantField.value = Math.abs(QuantField.value);
         			TotalField.value = formatCurrency(QuantField.value * PriceField.value);
        		}
        	}
        }
        calculate_product_total();
}

function calculate_product_total() {
  var F = document.OrderForm;
  var whichitem = 0;
  var itemname;
  var filter = /Total$/;
  var calcTotal = 0.0;
  while (whichitem < F.length) {
    itemname = F[whichitem].name;
    if (filter.test(itemname)) {
       calcTotal = calcTotal + Number(F[whichitem].value.toString().replace(/\$|\,/g,''));
    }
    whichitem++;
  }
  F.ProductTot.value = formatCurrency(calcTotal);
  calculate_sales_tax();
  CalculateShipping();
  calcTotal = calcTotal + Number(F.catax.value.toString().replace(/\$|\,/g,'')) + Number(F.shipfee.value.toString().replace(/\$|\,/g,''))
  F.GrandTot.value = formatCurrency(calcTotal);
}

function calculate_sales_tax() {
	var taxRate = .0725;
	if (document.OrderForm.AddCalTax.checked) {
		document.OrderForm.catax.value = formatCurrency(Number(document.OrderForm.ProductTot.value.toString().replace(/\$|\,/g,'')) * taxRate);
	} else {
		document.OrderForm.catax.value = "0";
	}
}

function formatCurrency(num) {
	var sign, cents;
	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);
}

function CalculateShipping() {
  window.onError = "noop";
  var numItems = 0;
  var optShippingMethod;
  var curShippingFee = 0;
  var F = document.OrderForm;
  var whichitem = 0;
  var itemname;
  var arrShippingCharges = new Array();
  arrShippingCharges[1] = new Array(14.75, 18.25, 24.95, 29.50);
  //arrShippingCharges[2] = new Array(19.50, 32.25, 45.00, 58.75);
  var idxShippingMethod = 1;  //assume FedExGround
  var filter = /Quantity$/;
  while (whichitem < F.length) {
    itemname = F[whichitem].name;
    if (filter.test(itemname)) {
       numItems = numItems + Number(F[whichitem].value);
    }
    whichitem++;
  }
  F.NumBottles.value = numItems;
  // comment out following code to just keep FedExGround for now...
  //optShippingMethod = getCheckedValue(F.ShippingMethod);
  //switch (optShippingMethod) {
	//case "UPSGround":
	//  idxShippingMethod = 1;
	//  break;
	//case "2ndDayAir":
	//  idxShippingMethod = 2;
  //}
  if (numItems > 12) {
	curShippingFee = (arrShippingCharges[idxShippingMethod][3] * Math.floor(numItems / 12));
	numItems = (numItems % 12);
  }
  switch (numItems) {
	case 1:
	case 2:
	case 3:
		curShippingFee = curShippingFee + arrShippingCharges[idxShippingMethod][0];
		break;
	case 4:
	case 5:
	case 6:
		curShippingFee = curShippingFee + arrShippingCharges[idxShippingMethod][1];
		break;
	case 7:
	case 8:
	case 9:
		curShippingFee = curShippingFee + arrShippingCharges[idxShippingMethod][2];
		break;
	case 10:
	case 11:
	case 12:
		curShippingFee = curShippingFee + arrShippingCharges[idxShippingMethod][3];
		break;
  }
  F.shipfee.value = formatCurrency(curShippingFee);
} 

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function GetParamValue(InputParam) {
   window.onError="noop";
   var GetParStr1;
   var GetParStr2;
   var GetParChr1;
   var GetParIndex1;
   var GetParIndex2;
   GetParStr1 = location.href;
   GetParStr2 = InputParam + "=";
   GetParIndex1 = GetParStr1.indexOf(GetParStr2, 0);
   if (GetParIndex1 < 1)
      return "";
   GetParChr1 = GetParStr1.substring(GetParIndex1 - 1, GetParIndex1);
   if ((GetParChr1 != "?") && (GetParChr1 != "&"))
      return "";
   if ((GetParIndex1 + GetParStr2.length) == GetParStr1.length) {
      return ""
   }
   GetParIndex2 = GetParStr1.indexOf("&", GetParIndex1 + GetParStr2.length);
   if (GetParIndex2 < 0) {
      return GetParStr1.substring(GetParIndex1 + GetParStr2.length, GetParStr1.length);
   }
   else {
      return GetParStr1.substring(GetParIndex1 + GetParStr2.length, GetParIndex2);
   }
}

// -->
