// Product IDs
// CADKEY id01 - id19
// KeyCreator id20 - id29
// SketchUp id30 - id39
// Solid Edge id40 - id49

var order = new Object();  // object to store order items in
var root  = new Object();  // selection criteria
var on    = true;
var off   = false;
var cntr  = 0;             // items in object
var opts  = 5;             // number of options to allow
var shpr  = function (wt) {return 0;} // what to charge for shipping;
//var shpr  = 0;  // what to charge for shipping;
var stxt  = "";            // shipping text
var spos  = 0;            // shipping position selector - country code
//var spos = document.orderf.shp;
var ttax  = 0;             // percent for taxes
var ttxt  = "0";           // tax text
var tpos  = 0;            // tax position selector
var tamt=0,tqty=0,twgt=0,wgt=0,thnd=0;  // totals
var SelCountry = 0;
var SelState = 0;
var SelCnty = 0;


// place for user-specific options
root.xx_can  = "http://www.sheetmetalguy.com"; // place for PayPal cancel return path
root.xx_cur  = "USD"; // enter default currency code (or null)
root.xx_id   = "books@sheetmetalguy.com"; // PayPal ID
root.xx_img  = ""; // image URL
root.xx_lc   = ""; // enter default country code (or null)
root.xx_ret  = "http://www.sheetmetalguy.com"; // place for PayPal return path
root.xx_sty  = ""; // place for PayPal page style
root.xx_xtra = ""; // place for other PayPal commands

function clearall()
{
	  	cntr = 0;  // zap cart
	    shpr = function (wt) {return 0;}
	    spos = 0;
		SelState=0;
		ttax=0;
	    tpos = 0;
	    tamt = 0;
	    tqty = 0;
//		document.orderf.states.disabled=true;
//		document.orderf.county.disabled=true;
//		countylist.options.length=0;
	    document.orderf.reset();
}

function DispTots () {  // display totals on the page
  var tmp,d;
  d = document.orderf;
  d.sub.value = Dollar (tamt);
  //if (isNaN (shpr)) {alert("NaN alert");shpr=0}  
  d.shp.value = Dollar (shpr);
  if (isNaN (d.shp.value)) d.shp.value = 0;
  tmp = tamt * ttax/100;
  d.tax.value = Dollar (tmp);
  d.tot.value = Dollar (tamt + shpr + tmp);
//  UpShp();
}

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 (".");  // should be one, but OK if not
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;               // return valid string
}

function GetOrder (id, des, amt, wgt) 
{  // get all ordered items
  var i,nr,val,qty,pos;
  var op = new Array ();    // accumulate options here
	shp = orderf.shp.value;
  tamt=0,tqty=0,twgt=0;   // zero totals
  nr = id.substring (2);  // get number part of ID
  qty = document.orderf["qty" + nr].value;  // get qty
  if (isNaN (qty)) 
  {      // test entry
    alert ("That is not a valid number! Try again.");
    return;
  }
  for (i=1; i<=opts; i++) 
  {     // see if any options
    if (document.orderf["op" + i + nr]) 
    {
      val = document.orderf["op" + i + nr].value;  // get option
      op[i] = val;
      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);
    }
    else op[i] = "";
  }
  document.orderf["prc" + nr].value = Dollar (qty * amt);
//  document.orderf["toz" + nr].value = qty * wgt;
  if (cntr == 0) order = new Object (); // zap object
  cntr = cntr + 1;               // bump counter so no zap next time
  order[id] = new Object ();     // create new entry
  for (i=1; i<=opts; i++)        // load options
    if (op[i] != "")
    	des = des + ", OP" + i + "=" + op[i];
  order[id].des = des;           // load up values
  order[id].amt = Dollar (amt);
  order[id].qty = qty;
  if (wgt)
  	order[id].wgt = wgt;
  else
  	order[id].wgt = 0;
  for (i in order) 
  {             // calc totals we might use
    qty = order[i].qty*1.0;
    tamt = tamt + order[i].amt * qty;  // total amount
//  Two book bundles
	if(i == "id30")
	    tqty = tqty + qty*2;                 // total quantity
	else
	    tqty = tqty + qty;                 // total quantity
    twgt = twgt + order[i].wgt * qty;  // total ounces
  }
  twgt = Math.floor ((twgt + 15.99)/16.0);  // get pounds
  UpShp ();
  DispTots ();                         // calc totals
}

function SendCart () 
{  // send the cart to PayPal
	var frst = true;  // 1st pass thru items.
	var winpar = "width=710,height=390,scrollbars," +
	             "location,resizable,status";
	var strn   = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" +
	             "&upload=1" +
	             "&business=" + root.xx_id + root.xx_xtra;
	var i,j=0,des;
	if ((spos == 1)&&(SelState == 0))  // check for US with a state selected.
	{
		alert("You must first select a state!   ");
		return;
	}
	if ((SelState == 35)&&(SelCnty == 0))
	{
		alert("You must first select an Ohio county!   ");
		return;
	}

	if (root.xx_cur.length > 0)
	    strn = strn + "&currency_code=" + root.xx_cur;
	if (root.xx_lc.length > 0)
	    strn = strn + "&lc=" + root.xx_lc;
	if (root.xx_can.length > 0)
	    strn = strn + "&cancel_return=" + root.xx_can;
	if (root.xx_ret.length > 0)
	    strn = strn + "&return=" + root.xx_ret;
	if (root.xx_sty.length > 0)
	    strn = strn + "&page_style=" + root.xx_sty;
	if (root.xx_img.length > 0)
    	strn = strn + "&image_url=" + root.xx_img;
	if (tpos > 0)
		strn = strn + "&tax_cart=" + Dollar (tamt*ttax/100);
//alert (strn);
	for (i in order) 
	{  // send all valid data
    	if (order[i].qty > 0) 
    	{
			j = j + 1;
    		des = order[i].des;
    		if (j == 1) 
    		{  // put in descriptions for 1st item
    	    	des = des + ", SHP=" + stxt;
    	    	des = des + ", TAX=" + ttxt;
        		strn = strn + "&handling_cart=" + Dollar (thnd);
    		}
    		strn = strn + "&item_name_"    + j + "=" + escape (des) +
    		              "&item_number_"  + j + "=" + i +
    		              "&quantity_"     + j + "=" + order[i].qty +
    		              "&amount_"       + j + "=" + order[i].amt;
    		if (spos >= 0) 
    		{  // there is some shipping activity
        		if (frst) 
        		{     // first time thru
					strn = strn + "&shipping_" + j + "=" + Dollar (shpr);
				} 
				else 
				{        // every other time thru
        			strn = strn + "&shipping_" + j + "=0";
        		}
      		}
      		frst = false;
    	}
	}
	if (j > 0) window.open (strn, "paypal", winpar);
}

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
  SelCnty  = obj1.selectedIndex;           // which item selected
  tpos=SelCnty;
  ttxt = obj1.options[tpos].text;
  ttax = obj1.options[SelCnty].value*1.0;  // float it
//alert ("SelCnty="+SelCnty+" tpos="+tpos+" ttxt="+ttxt+" ttax="+ttax); 
  DispTots ();
}

function updatestates(obj1)
{
  var obj;
  SetSH ();  // Don't delete this!  Shows bad selection was made.
  spos = obj1.selectedIndex;       // which item was selected?
  stxt = obj1.options[spos].text;  // user selection...
	stateslist.options.length=0
	if (spos==1)
	{
		root.state = on;
		for (i=0; i<states.length; i++)
			stateslist.options[stateslist.options.length]=new Option(states[i])
		document.orderf.states.disabled=false;
	}
	else 
	{
		document.orderf.states.disabled=true
		document.orderf.county.disabled=true
		countylist.options.length=0
		ttax=0
		SelState=0
	}
  UpShp();
  DispTots ();
}

function updatecounty(selectedstatesgroup)
{
	SelState=selectedstatesgroup
	countylist.options.length=0
	if (selectedstatesgroup==35)
	{
		for (i=0; i<county.length; i++)
			countylist.options[countylist.options.length]=new Option(county[i].split("|")[0], county[i].split("|")[1])
		document.orderf.county.disabled=false
	}
	else
	{
		document.orderf.county.disabled=true
		ttax=0
	}
	UpShp();
	DispTots ();
}

function UpShp () 
{  // set shipping on user selection
	  if ((spos <1)||(spos >3))   // check for country selection.
	  {
	  	alert ("You must first select a country!   ");
//		clearall();
	    return;
	  }
	  if (tqty >10) // check max quantity
	  {
	  	alert ("Unable to process current quantity.\nTo place your order, please call\nSheet Metal Guy at (513)607-8524.");
	  	clearall();
	  	return;
	  }    
	if ((spos==2)&&(SelState >0))
		spos = 1;
    if ((spos==1)&&((SelState==2)||(SelState==11)))
    {
    	spos=2;
    } 
	neil = new Array(4)
	for (i=0; i < 4; i++) 
	{
	   neil[i] = new Array(11)
	   for (j=0; j < 11; j++) 
	   {
	      neil[i][j] = i+j;
	   }
	}

	  neil [0][0] = 0; //US Shipping rates
	  neil [0][1] = 14;
	  neil [0][2] = 16;
	  neil [0][3] = 17;
	  neil [0][4] = 18;
	  neil [0][5] = 19;
	  neil [0][6] = 20;
	  neil [0][7] = 21;
	  neil [0][8] = 22;
	  neil [0][9] = 23;
	  neil [0][10] = 24;
	
	  neil [1][0] = 0;//Canada, Alaska,Hawaii Shipping rates
	  neil [1][1] = 24;
	  neil [1][2] = 29;
	  neil [1][3] = 31;
	  neil [1][4] = 32;
	  neil [1][5] = 33;
	  neil [1][6] = 34;
	  neil [1][7] = 35;
	  neil [1][8] = 36;
	  neil [1][9] = 37;
	  neil [1][10] = 38;  
    
	  neil [2][0] = 0;//International Shipping rates
	  neil [2][1] = 34;
	  neil [2][2] = 40;
	  neil [2][3] = 52;
	  neil [2][4] = 55;
	  neil [2][5] = 58;
	  neil [2][6] = 60;
	  neil [2][7] = 62;
	  neil [2][8] = 64;
	  neil [2][9] = 66;
	  neil [2][10] = 68; 

	  shpr = function (wt) {return 0;}  // zap it
	
	  shpr=neil[spos-1][tqty];
}
