


function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < .995) rnd = 1;  // for old Netscape browsers
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}



function ReadForm (obj1) { // process un-named selects
	setAction(obj1);
var i,amt,des,obj,pos,val;
  amt = obj1.baseamt.value*1.0;       // base amount
  des = obj1.basedes.value;           // base description
  for (i=0; i<obj1.length; i++) {     // run entire form
    obj = obj1.elements[i];           // a form element
    if (obj.type == "select-one" && // just get selects
        obj.name == "") {             // must be un-named
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // selected value
      pos  = val.indexOf ("@");       // price set?
      if (pos > 0) amt = val.substring (pos + 1)*1.0;
      pos  = val.indexOf ("+");       // price increment?
      if (pos > 0) amt = amt + val.substring (pos + 1)*1.0;
      pos  = val.indexOf ("%");       // percent change?
      if (pos > 0) amt = amt + (amt * val.substring (pos + 1)/100.0);
      if (des.length == 0) des = val;
      else des = des + ", " + val;    // accumulate value
    }
  }
  obj1.item_name.value = des;
  obj1.amount.value = Dollar (amt);
  if (obj1.tot) obj1.tot.value = "$" + Dollar (amt);
}







function updateForm (obj1,obj2){

mems = obj1.membership_type.options[obj1.membership_type.selectedIndex].value;
subtotal = 0;
if (mems > 0){
	obj1.membership_cost.value = "$" + Dollar (mems);
	subtotal += parseInt(mems);
} else {
		obj1.membership_cost.value = "";
}
	
books = parseInt(obj1.book_number.value);
if (mems > 1)
{
  if (isNaN(books))
  {
     obj1.book_number.value = "0";
  }
}
books = parseInt(obj1.book_number.value);

if (books > 0){
    books = books;
	obj1.book_cost.value = "$" + Dollar (books * 25);
	subtotal += books * 25;
	} else {
	 obj1.book_cost.value = "";
	}
region = obj1.region.value;
if (region == "usa"){
  obj1.delivery_method.options[1].text = "Surface";
  obj1.delivery_method.options[1].value = "surface";
} else if (region == "california"){
  obj1.delivery_method.options[1].text = "Surface";
  obj1.delivery_method.options[1].value = "surface";
} else {
  obj1.delivery_method.options[1].text = "Air Mail";
  obj1.delivery_method.options[1].value = "air";
}
shipping_method = obj1.delivery_method.value;
shipping = 0;
salestax = 0;
	if (mems == 20){
		if (region == "usa" || region =="california"){
			shipping = "1.00";
		} else {
			shipping = "6.00";
		}
		// did they add books?
		if (books > 0){
 			addition = (books) * 1.95;
 			shipping = parseInt(shipping) + addition;
	    }
		obj1.shipping_cost.value = "$" + Dollar (shipping);
		obj2.shipping.value = "" + shipping;
		subtotal += shipping * 1.0;
		if (salestax > 0){
			obj1.salestax.value = "$" + Dollar (salestax * subtotal);
		} else {
			obj1.salestax.value = "";
		}
	} else 	if ((mems > 0) || (books > 0)){
if (region == "usa"){
  shipping = "5.00";
  if (shipping_method == "air"){
  alert("We're located in California. Air mail will be very inefficient!");
  obj1.delivery_method.options[1].text = "Surface";
  obj1.delivery_method.options[1].value = "surface";
  }
} else if (region == "california"){
  salestax = .0775;
  shipping = "5.00";
  if (shipping_method == "air"){
  alert("We're located in California. Air mail will be very inefficient!");
  obj1.delivery_method.options[1].text = "Surface";
  obj1.delivery_method.options[1].value = "surface";
  }
} else if (region == "canada"){
   if (shipping_method == "air"){
      shipping = "12.00";
   } else {
      shipping = "10.00";
   }
} else if (region == "mexico"){
   if (shipping_method == "air"){
      shipping = "15.00";
   } else {
      shipping = "12.00";
   }
} else if (region == "southamerica"){
   if (shipping_method == "air"){
      shipping = "23.00";
   } else {
      shipping = "15.00";
   }
} else if (region == "europe"){
   if (shipping_method == "air"){
      shipping = "22.00";
   } else {
      shipping = "14.00";
   }
} else if (region == "aa"){
   if (shipping_method == "air"){
      shipping = "23.00";
   } else {
      shipping = "15.00";
   }
} else if (region == "pacrim"){
   if (shipping_method == "air"){
      shipping = "21.00";
   } else {
      shipping = "13.00";
   }
} 

//shipping for extra books
if (books > 0){
 addition = (books) * 1.95;
 shipping = parseInt(shipping) + addition;
}
obj1.shipping_cost.value = "$" + Dollar (shipping);
obj2.shipping.value = "" + shipping;
subtotal += shipping * 1.0;
if (salestax > 0){
obj1.salestax.value = "$" + Dollar (salestax * subtotal);
} else {
obj1.salestax.value = "";
}
	} else {// bypassed if only donation
		obj1.salestax.value = "";
		obj1.shipping.value = "";
	}
donation = obj1.donation.value;
if (donation.indexOf("$") != -1){
   donation = donation.substr(1,donation.length);
}
donation = parseFloat(donation);
if (isNaN(donation)){
donation = 0;
}
if (donation < 0.01){
donation = 0;
}
if (donation > 10000){
alert("That's a bit too much, but thanks anyway.");
donation = 0;
}

total = subtotal + salestax * subtotal;
total = total + donation;
obj1.total.value = "$" + Dollar (total);

inc = obj1.membership_type.selectedIndex;
if (inc == 1){
	farp = "Individual Regular Membership. ";
} else if (inc == 2){
	farp = "Joint Regular Membership. ";
} else if (inc == 3){
	farp = "Individual Associate Membership. ";
} else if (inc == 4){
	farp = "Joint Associate Membership. ";
} else if (inc == 3){
	farp = "Book set only. ";
}
farp = farp + "Shipping: " + shipping_method + ". ";
if (books > 0){
	farp = farp + "Additional Book Sets: " + books + ". ";
}
if (donation > 0){
	farp = farp + " Donation: $" + Dollar(donation) + ". ";
}
obj2.item_name.value = farp;
obj2.amount.value =  Dollar (total - shipping);
}

