/*
script for applying discounts to PayPal shopping cart via coupon code

based on script posted by paypalhelper@aol.com at
http://members.aol.com/paypalhelper/simpcoup.html, with modifications of mine
*/

var discnt = 0;			// discount in effect (in percent); defaults to 0
var coupon = "belinda";		// lower-case version of correct discount code
var coupdc = 25;		// discount that correct code will activate

function ChkCoup (coupval) {	// check user coupon entry
  if (coupval.toLowerCase() == coupon) {
    discnt = coupdc;		// correct code; set the discount and notify user
    alert ("Valid coupon code! \n\n" + discnt + "% discount now in effect.");
  }
  else alert ("'" + coupval + "' is not a valid coupon code!");	// otherwise notify of invalid code
}

function ReadForm (form_data) {	// apply the discount, if active
  if (discnt == 0) {		// no discount
    form_data.amount.value = form_data.init_amount.value;
    form_data.item_name.value = form_data.init_item_name.value;
  }
  else {					// apply discount
    form_data.amount.value = to_nearest_penny (form_data.init_amount.value * (1 - (discnt / 100)));
    form_data.item_name.value = form_data.init_item_name.value + ", " +
			        discnt + "% off ($" + form_data.amount.value + " each)";
  }
}

function to_nearest_penny (n) {
  return Math.round ( n * 100 ) / 100;
}
