// Used for Sheet Metal Guy Books only
// ID numbers
// id01 - 'SolidWorks for the Sheet Metal Guy Course 1, 2, and 3 Bundle (Full Color)'
// id02 - 'SolidWorks for the Sheet Metal Guy Course 1, 2, and 3 Bundle (Black and White)'
// id03 - 'SolidWorks for the Sheet Metal Guy Course 1 (Full Color)'
// id04 - 'SolidWorks for the Sheet Metal Guy Course 1 (Black and White)'
// id05 - 'SolidWorks for the Sheet Metal Guy Course 2 (Full Color)'
// id06 - 'SolidWorks for the Sheet Metal Guy Course 2 (Black and White)'
// id07 - 'SolidWorks for the Sheet Metal Guy Course 3 (Full Color)'
// id08 - 'SolidWorks for the Sheet Metal Guy Course 3 (Black and White)'
// id09 - 'Doctor Walt's Introduction to SolidWorks'
// id10 - 'Doctor Walt's SolidWorks Assemblies'
// id11 - 'Doctor Walt's SolidWorks 2006 Workbook - Color
// id12 - 'Doctor Walt's SolidWorks 2006 Workbook - B & W - No Longer Avail
// id13 - 'Doctor Walt's SolidWorks Workbook
// id14 - 'Customizing SolidWorks'
// id15 - SW Keyboard Card 2007
// id16 - SW Keyboard Card 2007 - 3 pack
// id17 - SW Keyboard Card 2008
// id18 - SW Keyboard Card 2008 - 3 pack
// id19 - 'Customizing SolidWorks' eBook
// id20 - 'Doctor Walt's SolidWorks 2007 Workbook - Color
// id21 - 'Doctor Walt's SolidWorks 2008 Workbook - Color
//
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,smgqty=0,dwqty=0,twgt=0,wgt=0,thnd=0;  // totals
var SelCountry = 0;
var SelState = 0;
var SelCnty = 0;
var CSW = 39.95;
var BUNDLEC = 174.95;
var BUNDLEBW = 149.95;

// 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;
	    smgqty = 0;
	    dwqty=0;
//		document.orderf.states.disabled=true;
//		document.orderf.county.disabled=true;
//		countylist.options.length=0;
	    document.orderf.reset();
}

function coupon (Code)
{
	if (Code.toUpperCase()=="EXPRESS")
	{
		CSW = 39.95;
		GetOrder ('id14','Customizing SolidWorks',CSW,1)
	}
	else if (Code.toUpperCase()=="BUNDLE")
	{
		BUNDLEC = 169.95;
		GetOrder ('id01','SolidWorks for the Sheet Metal Guy Course 1, 2, and 3 Bundle (Full Color)',BUNDLEC,1)
		BUNDLEBW = 154.95;
		GetOrder ('id02','SolidWorks for the Sheet Metal Guy Course 1, 2, and 3 Bundle (Black and White)',BUNDLEBW,1)
	}
}

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,smgqty=0,dwqty=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

//  Three book bundles
	if((i == "id01")||(i == "id02"))
	    smgqty = smgqty + qty*3;                 // total quantity
// SW Keyboard Card or ebook
	else if((i == "id15")||(i == "id16")||(i == "id17")||(i == "id18")||(i == "id19"))
	    smgqty = smgqty + qty*0;                 // total quantity	
// Check for Walt books	
	else if((i == "id09")||(i == "id10")||(i == "id11")||(i == "id12")||(i == "id13")||(i == "id20")||(i == "id21"))
		dwqty = dwqty + qty;
	else
	    smgqty = smgqty + 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 ((smgqty >20)||(dwqty >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;
	   }
	}
	walt = new Array(4)
	for (i=0; i < 4; i++) 
	{
	   walt[i] = new Array(11)
	   for (j=0; j < 11; j++) 
	   {
	      walt[i][j] = i+j;
	   }
	}
	  neil [0][0] = 0; //US Shipping rates
	  neil [0][1] = 7;
	  neil [0][2] = 8;
	  neil [0][3] = 11;
	  neil [0][4] = 11;
	  neil [0][5] = 11;
	  neil [0][6] = 11;
	  neil [0][7] = 11;
	  neil [0][8] = 11;
	  neil [0][9] = 11;
	  neil [0][10] = 11;
	  //Start of 18
	  neil [0][11] = 18;
	  neil [0][12] = 18;
	  neil [0][13] = 18;
	  neil [0][14] = 18;
	  neil [0][15] = 18;
	  neil [0][16] = 18;
	  neil [0][17] = 18;
	  neil [0][18] = 18;
	  neil [0][19] = 18;
	  neil [0][20] = 18;
	  	
	  neil [1][0] = 0;//Canada, Alaska, Hawaii Shipping rates
	  neil [1][1] = 12;
	  neil [1][2] = 12;
	  neil [1][3] = 24;
	  neil [1][4] = 24;
	  neil [1][5] = 27;
	  neil [1][6] = 27;
	  neil [1][7] = 27;
	  neil [1][8] = 27;
	  neil [1][9] = 27;
	  neil [1][10] = 30;  
	  neil [1][11] = 30;
	  neil [1][12] = 34;
	  neil [1][13] = 34;
	  neil [1][14] = 48;
	  neil [1][15] = 48;
	  neil [1][16] = 48;
	  neil [1][17] = 48;
	  neil [1][18] = 48;
	  neil [1][19] = 48;
	  neil [1][20] = 48;
	  	      
	  neil [2][0] = 0;//International Shipping rates
	  neil [2][1] = 14;
	  neil [2][2] = 14;
	  neil [2][3] = 28;
	  neil [2][4] = 28;
	  neil [2][5] = 42;
	  neil [2][6] = 42;
	  neil [2][7] = 42;
	  neil [2][8] = 42;
	  neil [2][9] = 42;
	  neil [2][10] = 42;  
	  neil [2][11] = 42;
	  neil [2][12] = 49;
	  neil [2][13] = 49;
	  neil [2][14] = 61;
	  neil [2][15] = 61;
	  neil [2][16] = 74;
	  neil [2][17] = 74;
	  neil [2][18] = 74;
	  neil [2][19] = 74;
	  neil [2][20] = 74;
	  
	  walt [0][0] = 0; //Walt US Shipping rates
	  walt [0][1] = 12;
	  walt [0][2] = 14;
	  walt [0][3] = 15;
	  walt [0][4] = 16;
	  walt [0][5] = 17;
	  walt [0][6] = 18;
	  walt [0][7] = 19;
	  walt [0][8] = 20;
	  walt [0][9] = 21;
	  walt [0][10] = 22;
	  
	  walt [1][0] = 0; //Walt Canada Shipping rates
	  walt [1][1] = 15;
	  walt [1][2] = 18;
	  walt [1][3] = 22;
	  walt [1][4] = 26;
	  walt [1][5] = 28;
	  walt [1][6] = 30;
	  walt [1][7] = 31;
	  walt [1][8] = 32;
	  walt [1][9] = 33;
	  walt [1][10] = 34;
	  
	  walt [2][0] = 0; //Walt International Shipping rates
	  walt [2][1] = 30;
	  walt [2][2] = 42;
	  walt [2][3] = 48;
	  walt [2][4] = 52;
	  walt [2][5] = 54;
	  walt [2][6] = 56;
	  walt [2][7] = 58;
	  walt [2][8] = 60;
	  walt [2][9] = 62;
	  walt [2][10] = 64;	  	  


	  shpr = function (wt) {return 0;}  // zap it
	  shpr=neil[spos-1][smgqty]+walt[spos-1][dwqty];
}
