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;
}

var amt,des,obj,val,op1a,op1b,op2a,op2b,itmn;

function ChkTok (obj1) {
var j,tok,ary=new Array ();       // where we parse
  ary = val.split (" ");          // break apart
  for (j=0; j<ary.length; j++) {  // look at all items
// first we do single character tokens...
    if (ary[j].length < 2) continue;
    tok = ary[j].substring (0,1); // first character
    val = ary[j].substring (1);   // get data
    if (tok == "@") amt = val * 1.0;
    if (tok == "+") amt = amt + val*1.0;
    if (tok == "%") amt = amt + (amt * val/100.0);
    if (tok == "#") {             // record item number
      if (obj1.item_number) obj1.item_number.value = val;
      ary[j] = "";                // zap this array element
    }
// Now we do 3-character tokens...
    if (ary[j].length < 4) continue;
    tok = ary[j].substring (0,3); // first 3 chars
    val = ary[j].substring (3);   // get data
    if (tok == "s1=") {           // value for shipping
      if (obj1.shipping)  obj1.shipping.value  = val;
      ary[j] = "";                // clear it out
    }
    if (tok == "s2=") {           // value for shipping2
      if (obj1.shipping2) obj1.shipping2.value = val;
      ary[j] = "";                // clear it out
    }
  }
  val = ary.join (" ");           // rebuild val with what's left
}

function StorVal () {
var tag;
  tag = obj.name.substring (obj.name.length-2);  // get 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 (tag == "3i") itmn = itmn + " " + val;
  else if (des.length == 0) des = val;
  else des = des + " " + val;
}

function SetShip (obj1) {  // record selected shipping option
var j,obj,pos,tok,val,txt;
var ary = new Array ();
  obj = obj1.shp;          // refer to shipping select
  pos = obj.selectedIndex; // get selection
  if (pos == 0) {          // force a selection
    alert ("Please make a shipping selection!");
    return false;
  }
  obj1.handling.value = "0";
  obj1.handling_cart.value = "0";
  obj1.shipping.value = "0";
  obj1.shipping2.value = "0";
  val = obj.options[pos].value;   // selected value
  txt = obj.options[pos].text;    // the text value
  ary = val.split (" ");          // break apart
  for (j=0; j<ary.length; j++) {  // look at all items
// do 3-character tokens...
    if (ary[j].length < 4) continue;
    tok = ary[j].substring (0,3); // first 3 chars
    val = ary[j].substring (3);   // get data
    if (tok == "hn=")             // value for item handling
      obj1.handling.value  = val;
    if (tok == "hc=")             // value for handling cart
      obj1.handling_cart.value  = val;
    if (tok == "s1=")             // value for shipping
      obj1.shipping.value  = val;
    if (tok == "s2=")             // value for shipping2
      obj1.shipping2.value = val;
  }
  obj1.os0.value = txt;           // stuff the text into options field
  return true;
}

var discnt = 0;   // no default percent discount

var coupons = new Array (  // place to put codes
  "positive",                 // 1st code - comma seperated
  "preg1g",               // 2nd code - add all you want
  "preg1t",               // 3rd code - add all you want
  "RNT",               // 4th code - add all you want
  "preg2t",              // 5th code
  "news20",                 // 6th code - for newsletter
  "dv40jr",                 // 7th code - for newsletter
  "dv40ab",                 // 8th code - for newsletter
  "radio20"                // 9th code - for radio 
);
var coupdc  = new Array (  // place to put percentage discounts
  10,                      // 1st discount
  20,                      // 2nd discount
  20,                       // 3rd discount
  20,                      // 4th discount
  20,                      // 5th discount
  20,                      // 6th discount
  40,                      // 7th discount
  40,                      // 8th discount
  20                      // 9th discount
);
var coupval = "(blanket)"; // 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 ("Thank you! \n\n" + discnt + 
             "% discount now in effect for this Item.");
      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,pos;
  amt=0;des="";op1a="";op1b="";op2a="";op2b="";itmn="";
  if (obj1.baseamt) amt  = obj1.baseamt.value*1.0;  // base amount
  if (obj1.basedes) 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;
  if (obj1.baseitn) itmn = obj1.baseitn.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
      if (obj.name == "quantity" ||
          obj.name == "shipping" ||
          obj.name == "shipping2" ||
          obj.name == "shp" ||
          obj.name == "amount") continue;
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // selected value
      ChkTok (obj1);                  // check for any specials

      if (obj.name == "on0" ||        // let this go where it wants
          obj.name == "os0" ||
          obj.name == "on1" ||
          obj.name == "os1") continue;

      StorVal ();

    } else
    if (obj.type == "checkbox" ||     // just get checkboxex
        obj.type == "radio") {        //  and radios
      if (obj.checked) {
        val = obj.value;              // the value of the selection
        ChkTok (obj1);
        StorVal ();
      }
    } 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)
          ChkTok (obj1);
          StorVal ();
        }
      }
    } else
    if ((obj.type == "text" ||        // just read text,
         obj.type == "textarea") &&
         obj.name != "tot" &&         //  but not from here
         obj.name != "quantity") {
      val = obj.value;                // get the data
//      if (val == "" && tst) {         // force an entry
//        alert ("Enter data for " + obj.name);
//        return false;
//      }
      StorVal ();
    }
  }
// 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 (itmn.length > 0) obj1.item_number.value = itmn;
  if (discnt > 0) {                   // only if discount is active
    amt = Dollar (amt - (amt * discnt/100.0));
    des = des + " code applied for " + discnt + "% discount"; }
  obj1.item_name.value = des;
  obj1.amount.value = Dollar (amt);
  if (obj1.tot) obj1.tot.value = "$" + Dollar (amt);
}

