var aqty = new Array ();  // amount qty breakpoint
var aamt = new Array ();  // amount to charge
var an   = 0;             // number of discount brkpts

var hqty = new Array ();  // handling qty breakpoints
var hamt = new Array ();  // amount charged
var hn   = 0;             // number of handling brkpts

var qqty = new Array ();  // quantity discount breakpoints
var qamt = new Array ();  // amount of discount
var qd   = 0;             // number of qty breakpoints

var sqty = new Array ();  // shipping qty breakpoints
var samt = new Array ();  // amount charged
var sn   = 0;             // number of shipping brkpts
var stxt = "";            // shipping type text

var taxp = 0;             // tax percent for this order

var coup = -1;               // discount coupon not active
var cdis = 0;                // amount of coupon discount (%)
var coupVal = 0;
var coupons = new Array ();  // array of valid coupon names
coupons[0] = "USMIL-D1";        // list as many as you want...
coupons[1] = "BCMZ3-D1";
coupons[2] = "JCOBH-D1";
var discount = new Array (); // array of discount corresponding to the name
discount[0] = 8;
discount[1] = 8;
discount[2] = 20;

var on  = 1;
var off = 0;

var root = new Object ();
root.xx_ship = off;       // default is no shipping

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


function ChkCoup (val) {  // check for a discount coupon
var i;
  coup = -1;              // assume the worst
  cdis = 0;
  for (i=0; i<coupons.length; i++) {
    if (val == coupons[i]) {
      coup = 1;           // user hit the coupon value
      cdis = discount[i];         // remember the discount amt
	  coupVal = val;
      alert ("Valid coupon code!");
      return;
    }
  }
  alert (val + " is not a valid code!");
}

function ChkFlg (amt, temp) { // check for special flag char
var pos;
  pos  = temp.indexOf ("@");  // is there an initial value?  
  if (pos >= 0) amt = temp.substring (pos + 1)*1.0;
  pos  = temp.indexOf ("+");  // is there a price adjustment?  
  if (pos >= 0) amt = amt + temp.substring (pos + 1)*1.0;
  pos  = temp.indexOf ("%");  // is there a percent adjustment?  
  if (pos >= 0) amt = amt * (1.0 + temp.substring (pos + 1)/100.0);
  return amt;
}

function ClearAll () {
  sn = 0;  // reset shipping indicator
}

function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < 1) rnd = 1;
  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 Process (obj1) {      // process the form, but no submit
var i,j,obj,bqty,bamt,btxt,tdis,thnd,tshp,ttax,val,pos;
  
  if (root.xx_ship == on) {  // is shipping on?
    if (sn == 0) {
      alert("Select shipping method!");
      return false;
    }
  }
  if (!obj1.quantity) {
    alert ("HTML must have quantity field!");
    return false;
  }
  if (!obj1.basedes) {
    alert ("HTML must have basedes field!");
    return false;
  }
  if (!obj1.baseamt) {
    alert ("HTML must have baseamt field!");
    return false;
  }
  btxt = obj1.basedes.value;          // reload desc
  bamt = obj1.baseamt.value*1.0;      //  and amount
  bqty = obj1.quantity.value;         // selected quantity
  if (isNaN(bqty) || bqty == "" || bqty < 1) {
    alert ("Minimum quantity is 1");
    bqty = 1;
    obj1.quantity.value = "1";

  }
  bqty = bqty*1.0;                    // float that sucker

  for (i=0; i<obj1.length; i++) {     // run whole form
    obj = obj1.elements[i];           // ref particular element
    if (obj.name != "") continue;     // skip named elements
    if (obj.type == "select-one") {   // dropdowns
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // get selection
      bamt = ChkFlg (bamt, val);      // any flag chars
      btxt = btxt + ", " + val;
    } 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;
          bamt = ChkFlg (bamt, val);     // any flag chars
          btxt = btxt + ", " + val;
        }
      }
    } else
    if (obj.type == "checkbox") {     // checkboxes
      if (obj.checked) {              // just the selected ones
        val = obj.value;              // the value of it
        bamt = ChkFlg (bamt, val);    // any flag chars
        btxt = btxt + ", " + val;
      }
    } else
    if (obj.type == "text") {         // text
      if (obj.value.length > 0) {
        val = obj.value;              // the value of it
        btxt = btxt + ", " + val;
      }
    }
  }

  for (i=an-1; i>=0; i--) {       // qty amount?
    if (bqty >= aqty[i]) {        // qty brkpt
      bamt = aamt[i]*1.0;         // set amount from qty
      btxt = btxt + ", AMT=" + Dollar (bamt);
      break;                      // get out, now
    }
  }

  tdis = 0;                       // qty discounts
  for (i=qd-1; i>=0; i--) {       // run backwards
    if (bqty >= qqty[i]) {        // qty brkpt
      tdis = qamt[i]*1.0;
      bamt = bamt - tdis;         // Deduct from item price
      btxt = btxt + ", QDIS=$" + Dollar (tdis);
      break;                      // get out, now
    }
  }

  if (coup > 0 &&                 // coupon discount percent?
      cdis > 0) {
    bamt = bamt * (1.0 - cdis/100.0);   
    btxt = btxt + ", Code=" + coupVal + ", Discount="+ cdis + "%";
  }

  thnd = 0;                  // handling charges
  for (i=hn-1; i>=0; i--) {  // run backwards
    if (bqty >= hqty[i]) {   // qty brkpt
      thnd = hamt[i];        // set hand amount
      obj1.handling.value = Dollar (thnd);
      break;                 // get out, now
    }
  }

  tshp = 0;                  // shipping charges
  for (i=sn-1; i>=0; i--) {  // run backwards
    if (bqty >= sqty[i]) {   // qty brkpt
      tshp = samt[i]*1.0;    // set shipping amount
      btxt = btxt + ", SHP:" + stxt;
      break;                 // get out, now
    }
  }

/* At this point everything has been calculated except tax.  
We need to figure out what we need to calculate tax on.  Handling 
is most often put into the tax calculation, but shipping is a toss-up.

In the following statement you may need to modify the Dollar amount
to include shipping - I have already included handling (thnd) - just
add tshp to include shipping in the tax calculation... */

  ttax = 0;
  if (taxp > 0 &&              // have a percent recorded?
      obj1.tax) {              // check for override
    ttax = taxp/100.0;         // yep - calculate it.
    obj1.tax.value = Dollar (ttax * bqty * (bamt + thnd));
  }

  obj1.shipping.value = Dollar (tshp);
  obj1.amount.value = Dollar (bamt);  // plug amount
  obj1.item_name.value = btxt;        // and descriptiuon
  return ReadForm (obj1, true);
}

function SetHN (q1, h1) {      // set qty handling breakpoints
var i;
  hn = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    hqty[hn] = arguments[i];   // qty breakpoint
    hamt[hn] = arguments[i+1]; // handling charge
    hn = hn + 1;               // number of bkpts
  }
}

function SetQA (q1, a1) {      // set qty amount breakpoints
var i;
  an = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    aqty[an] = arguments[i];   // quantity
    aamt[an] = arguments[i+1]; // amount
    an = an + 1;               // number of discount bkpts
  }
}

function SetQD (q1, d1) {      // set qty discount breakpoints
var i;
  qd = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    qqty[qd] = arguments[i];   // qty breakpoint
    qamt[qd] = arguments[i+1]; // discount amount
    qd = qd + 1;               // number of bkpts
  }
}

function SetSH (q1, s1) {      // set shipping breakpoints
var i;
  sn = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    sqty[sn] = arguments[i];   // price breakpoint
    samt[sn] = arguments[i+1]; // shipping charge
    sn = sn + 1;               // number of bkpts
  }
}

function SetTX (obj1) {  // Set tax percent for this order
var pos;
  pos  = obj1.selectedIndex;           // which item selected
  taxp = obj1.options[pos].value*1.0;  // float it
}

function Shipper (obj1) {  // process user-selected shipping
var obj,pos;
  SetSH ();  // Don't delete this!  Shows bad selection was made.
  pos = obj1.selectedIndex;       // which item was selected?
  stxt = obj1.options[pos].text;  // user selection...
  switch (pos) {                  // item selected
    case 0:  // no selection made, return.
      break;
    case 1:  // West
      SetSH (1,2.00, 2,3.00, 5,4.00);  
      break;
    case 2:  // Mid
      SetSH (1,1.50, 2,1.90, 3,200, 5,2.50);
      break;
    case 3:  // East
      SetSH (1,2.00, 2,3.00, 5,4.00);  
      break;
    case 4:  // Can
      SetSH (1,3.00, 2,4.00, 5,7.00);
      break;
    case 5:  // Mex
      SetSH (1,2.50, 2,3.50, 5,6.00);
      break;
    default:
      SetSH (1, 99);  // outlandish, to detect.
      break;
  }
}

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 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 == "amount" || 
		  obj.name == "cpon") continue;
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // selected value

      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
        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)
          StorVal ();
        }
      }
    } else
    if ((obj.type == "text" ||        // just read text,
         obj.type == "textarea") &&
         obj.name != "tot" &&         //  but not from here
         obj.name != "quantity"
		 && obj.name != "cpon") {
      val = obj.value;                // get the data
      if (val == "" && tst) {         // force an entry
	   	if (obj.name == "make_1b")
        alert ("Please enter the make of the car");
		if (obj.name == "model_1b")
        alert ("Please enter the model of the car");
		if (obj.name == "year_1b")
        alert ("Please enter the year of the car");
		if (obj.name == "paint_name_2b")
        alert ("Please enter the paint name");
		if (obj.name == "paint_code_2b")
        alert ("Please enter the paint code");
        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;
  return true;
}

function submitform(obj1)
{
 if(obj1.onsubmit())
 	{
    obj1.submit();
	}
}

function disablefield(obj1)
{
	obj1.quantity.disabled = true;
	obj1.make_1b.disabled = true;
	obj1.model_1b.disabled = true;
	obj1.style_1b.disabled = true;
	obj1.year_1b.disabled = true;
	obj1.paint_name_2b.disabled = true;
	obj1.paint_code_2b.disabled = true;
	obj1.button23.disabled = true;
} 
	
function enablefield(obj1)
{
	obj1.quantity.disabled = false;
	obj1.make_1b.disabled = false;
	obj1.model_1b.disabled = false;
	obj1.style_1b.disabled = false;
	obj1.year_1b.disabled = false;
	obj1.paint_name_2b.disabled = false;
	obj1.paint_code_2b.disabled = false;
	obj1.button23.disabled = false;
}


function SubClear(obj1) {
  if (Process(obj1)) {
    obj1.target = 'paypal';
    obj1.submit();
	alert ("Product added to cart");
    obj1.reset();
	Change2To("Mazda");
  }
}
OptLot=new Array()
OptLot[0]=new Array("Mazda3","Mazda5","Mazda6","MazdaSpeed6", "Protege5")
OptLot[1]=new Array("Vibe")
OptLot[2]=new Array("Corolla","Matrix","Prius","Yaris")
OptLot[3]=new Array("Beetle","Golf/GTI","Jetta","Passat")

function Change2To(x){
document.F1.model_1b.length=0
var y;
if(x=="empty")return
if(x=="Mazda") y=0;
if(x=="Pontiac") y=1;
if(x=="Toyota") y=2;
if(x=="VW") y=3;
y=parseInt(y)
for(i=0;i<OptLot[y].length;i++){
document.F1.model_1b.length++
document.F1.model_1b.options[i].text=OptLot[y][i]
document.F1.model_1b.options[i].value=OptLot[y][i]
}
}

function ModelWarning(x){
if(x=="Passat"){
alert ("Note: This product is not available for Passat models with the OnStar option.");
}
}