/* This script combines multiple user-selected options into
one so it can be passed to PayPal.
Original author: My Colorful Treasures
http://www.mycolorfultreasureswebdesign.com
Last Updated: Dec. 1, 2007

Usage Description:
form_name - the form that the options are coming from.
max_values - the maximum number of choices that are available for current item.
*/

function combineChoices(form_name, max_values){
    var options = "";
    for (var x = 1; x <= max_values; x++) {
      if (x != 1) {
	    options += ", ";
	  }
	  options += document.forms[form_name].elements["v"+x].value;
	  	if (document.forms[form_name].elements["o"+x].value == "Coupon") {
		   if(!checkCoupon(document.forms[form_name], x)) {
		     alert("Invalid coupon entered!");
			 return false;
		   }
		}
    }
    document.forms[form_name].elements["os0"].value = options;

    return true;
}

function addValues(form_name, max_values, price){
  var stringVal = "";
  for (var x = 1; x <= max_values; x++) {
    stringVal = document.forms[form_name].elements["v"+x].value.split("$");
	if (stringVal[1] > 0) {
	   price += parseFloat(stringVal[1]);
	}
  }
  document.forms[form_name].elements["amount"].value = price;
  document.forms[form_name].elements["baseamount"].value = price;
  return true;
}

var coupons = new Array (  // place to put coupon codes
  "", "MOMPACK20", "mompack20", "THANKYOU10", "thankyou10", "THANKYOU15", "thankyou15", "WINTER15", "winter15", "SPRING15", "spring15", "SUMMER15", "summer15", "FALL15", "fall15", "HELPCURE", "helpcure"
);
var coupdc  = new Array (  // place to put discounts for coupon vals
  0, 20, 20, 10, 10, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15
);

function checkCoupon (form, pos) {      // check user coupon entry
  for (i=0; i<coupons.length; i++) {
    if (form.elements["v"+pos].value == coupons[i]) {
      var discnt = coupdc[i];  // remember the discount amt
	  amt = form.elements["baseamount"].value;
	  form.elements["amount"].value = Dollar (amt - (amt * discnt/100.0));
      return true;
    }
  }
  return false;
}

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;
}

