var discnt = 0;   // no default percent discount

var coupons = new Array (  // place to put coupon codes
  "NN01",                 // 1st coupon value - comma seperated
  "Charity",                 // 2nd coupon value - add all you want
  "nn01",                 // 3rd coupon value
  "charity"                  // 4th coupon value
);
var coupdc  = new Array (  // place to put discounts for coupon vals
  10,
  10,
  10,
  10
);
var coupval = "(no coupon)"; // what user entered as coupon code

function ChkCoup () {      // check user coupon entry
var i;
  discnt = 0;              // assume the worst
  for (i=0; i<coupons.length; i++) {
    if (coupval == coupons[i]) {
      discnt = coupdc[i];  // remember the discount amt
      alert ("Valid coupon number! \n\n" + discnt + 
             "% discount now in effect.");
      return;
    }
  }
  alert ("'" + coupval + "'  is not a valid code!");
}

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, tst) { // Read the user form
var i,j,amt,des,obj,pos,val,op1a="",op1b="",op2a="",op2b="";
  amt = obj1.baseamt.value*1.0;       // base amount
  des = obj1.basedes.value;           // base description
  if (obj1.baseon0) op1a = obj1.baseon0.value;  // base options
  if (obj1.baseos0) op1b = obj1.baseos0.value;
  if (obj1.baseon1) op2a = obj1.baseon1.value;
  if (obj1.baseos1) op2b = obj1.baseos1.value;
  for (i=0; i<obj1.length; i++) {     // run entire form
    obj = obj1.elements[i];           // a form element
    if (obj.type == "select-one" &&   // just 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
    } else
    if (obj.type == "checkbox" ||     // just get checkboxex
        obj.type == "radio") {        //  and radios
      if (obj.checked) {
        val = obj.value;              // the value of the selection
        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, or text
      }
    } else
    if (obj.type == "select-multiple") {  //one or more
      for (j=0; j<obj.options.length; j++) {  // run all options
        if (obj.options[j].selected) {
          val = obj.options[j].value; // selected value (default)
          if (des.length = 0) des = amt;
          else des = des + ", " + val;// accumulate value, or text
          pos  = val.indexOf ("+");   // price increment?
          if (pos > 0) amt = amt + val.substring (pos + 1)*1.0;
        }
      }
    } else
    if (obj.type == "text" &&         // just read text,
        obj.name != "tot" &&          //  but not from here
        obj.name != "coupcode" &&
        obj.name != "quantity") {
      val = obj.value;                // get the data
      if (val == "" && tst) {         // force an entry
        alert ("Enter data for " + obj.name);
        return false;
      }
      tag = obj.name.substring (0, 2);  // get special flag
      if      (tag == "1a") op1a = op1a + " " + val;
      else if (tag == "1b") op1b = op1b + " " + val;
      else if (tag == "2a") op2a = op2a + " " + val;
      else if (tag == "2b") op2b = op2b + " " + val;
      else if (des.length == 0) des = val;
      else des = des + ", " + val;
    }
  }
// Now summarize stuff we just processed, above
  if (op1a.length > 0) obj1.on0.value = op1a;
  if (op1b.length > 0) obj1.os0.value = op1b;
  if (op2a.length > 0) obj1.on1.value = op2a;
  if (op2b.length > 0) obj1.os1.value = op2b;

  if (discnt > 0) {                   // only if discount is active
    amt = Dollar (amt - (amt * discnt/100.0));
    if (obj1.item_number && obj1.basenum)
      obj1.item_number.value = obj1.basenum.value + 
        " " + discnt + "% dis, COUP = " + coupval;
  }

  obj1.item_name.value = des;
  obj1.amount.value = Dollar (amt);
  if (obj1.tot) obj1.tot.value = "£" + Dollar (amt);
}
