// global variables...
var wte,zip,zne,amt,mpp;

var zn = 0;                // ZIP, zone entries index
var zzip = new Array ();   // ZIP entry
var zone = new Array ();   // corresponding zone for ZIP

// these are the priority mail zone charge tables (1-8) for up to 18 lbs
// Current from Oct 2007
var pm1  = new Array (0,
 4.60,  4.60,  5.05, 5.70,  6.30,  6.85, 7.35,  7.75, 8.15, 8.50,  8.80, 9.15, 9.50, 9.80, 10.15, 10.35,  10.50, 10.70);
var pm2  = new Array (0,
 4.60,  4.60,  5.05, 5.70,  6.30,  6.85, 7.35,  7.75, 8.15, 8.50,  8.80, 9.15, 9.50, 9.80, 10.15, 10.35,  10.50, 10.70);
var pm3  = new Array (0,
 4.60,  4.90,  5.70, 6.65,  7.55,  8.25, 8.85,  9.60, 10.25, 10.75,  11.20, 11.70, 12.20, 12.70, 13.20, 13.45,  13.70, 13.90);
var pm4  = new Array (0,
 4.60,  5.30,  6.40, 7.70,  8.90,  10.00, 11.00,  11.95, 12.50, 13.10,  13.65, 14.20, 14.75, 15.35, 15.90, 16.20,  16.50, 16.80);
var pm5  = new Array (0,
 4.60,  6.20,  8.25, 10.20,  11.90,  12.95, 13.95,  14.90, 15.90, 16.95,  17.95, 18.95, 20.00, 20.90, 21.55, 22.00,  22.55, 23.00);
var pm6  = new Array (0,
 4.60,  6.55,  9.10, 11.10,  12.90,  13.10, 14.35,  15.60, 16.85, 18.25,  19.75, 21.30, 22.85, 24.10, 24.65, 25.20,  25.85, 26.35);
var pm7  = new Array (0,
 4.60,  7.00,  9.65, 12.20,  14.35,  14.75, 16.40,  18.00, 19.60, 21.30,  22.90, 24.10, 25.05, 26.50, 26.75, 27.35,  28.05, 28.60);
var pm8  = new Array (0,
 4.60,  7.50,  10.55, 13.45,  15.85,  16.05, 18.30,  20.55, 22.85, 25.05,  26.35, 27.50, 28.45, 29.85, 30.50, 31.25,  32.10, 32.85);
var ppx  = new Array (0,pm1,pm2,pm3,pm4,pm5,pm6,pm7,pm8);

function CalcCost (obj1) {  // given weight and ZIP, calc cost
 wte =  getWeight2(obj1);      // this is the weight (could be arg)
if (obj1.s_zip.value != ""){
	zip = obj1.s_zip.value.substring(0, 3);
}
else {
  zip = obj1.b_zip.value.substring(0, 3);  // 1st 3 chars of zip
}
  for (var i=zn-1; i>=0; i--) { // run table backwards
    if (zip >= zzip[i]) {       // zip value
      zne = zone[i];            // corresponding zone
      break;                    // get out
    }
  }
  obj1.zoner.value = zne;   // output the zone for user
  mpp = ppx[zne][wte];   // charges to that zone
  mpp = mpp + parseFloat("2.50");
  obj1.pmc.value  = "$" + Dollar (mpp);  // output pp charge
  return mpp;
}

function Dollar (val) {  // force to valid dollar amount
var str,pos;
  str = escape (val*1.0 + 0.005001);  // float, round, escape
  pos = str.indexOf (".");            // should be one
  if (pos > 0) str = str.substring (0, pos + 3);
  return str;
}

function SetZone () {  // record zip/zone info into table
  zn = 0;                      // count of breakpoints
  for (var i=0; i<arguments.length; i=i+2) {
    zzip[zn] = arguments[i];   // zip code index
    zone[zn] = arguments[i+1]; // zone
    zn = zn + 1;               // number of bkpts
  }
}

// built from 04086zone chart Oct 2007
SetZone (000, 8,  // executed at load time
  5, 3,   6, 7,  10, 2,  38, 1,  44, 2,
 45, 1,  46, 2,  48, 1,  49, 2, 60, 3,
68, 3, 80, 4, 88, 3, 120, 3, 124, 3,
128, 3, 129, 2, 130, 3, 140, 4, 214, 4,
240, 5, 244, 4, 245, 5, 254, 4, 255, 5,
260, 4, 261, 5, 262, 4, 266, 5, 267, 4,
270, 5, 310, 6, 311, 5, 312, 6, 313, 5,
315, 6, 344, 6, 346, 6, 349, 6, 354, 6,
370, 5, 375, 6, 376, 5, 380, 6, 385, 5,
386, 6, 399, 5, 420, 6, 421, 5, 430, 5,
439, 4, 448, 5, 500, 6, 520, 6, 530, 5,
534, 5, 537, 5, 540, 6, 541, 5, 546, 6,
549, 5, 550, 6, 553, 6, 570, 6, 575, 7,
580, 6, 585, 7, 590, 8, 592, 7, 594, 8,
600, 5, 612, 6, 613, 5, 614, 6, 617, 5,
620, 6, 622, 6, 624, 5, 625, 6, 633, 6,
644, 6, 660, 6, 664, 6, 669, 7, 673, 6,
674, 7, 680, 6, 683, 6, 688, 7, 700, 7,
703, 7, 704, 6, 705, 7, 710, 7, 712, 6,
713, 7, 716, 6, 718, 7, 719, 6, 730, 7,
733, 7, 743, 7, 749, 6, 750, 7, 769, 8,
770, 7, 780, 8, 786, 7, 788, 8, 789, 7,
793, 8, 795, 7, 797, 8, 800, 7, 811, 8,
820, 7, 821, 8, 822, 7, 823, 8, 826, 7,
829, 8, 840, 8, 850, 8, 852, 8, 855, 8,
859, 8, 863, 8, 870, 8, 877, 8, 889, 8,
893, 8, 897, 8, 900, 8, 910, 8, 930, 8,
988, 8
);

function getWeight2(form){
	var POSTERS = "0";
	var BOOKS = "0";
	var WEIGHT = "0";
	var SUBWEIGHT = "0";
	
BOOKS = (form.B1.selectedIndex + form.B2.selectedIndex);
POSTERS = (form.P10.selectedIndex + form.P11.selectedIndex + form.P12.selectedIndex + form.P13.selectedIndex + form.P14.selectedIndex + form.P15.selectedIndex + form.P16.selectedIndex + form.P17.selectedIndex + form.P18.selectedIndex + form.P19.selectedIndex + form.P20.selectedIndex + form.P21.selectedIndex + form.P22.selectedIndex);
var error = "Sorry! This combination/quantity of items will not fit in one box! Please read the IMPORTANT ORDERING INFO and try again!";
if (BOOKS >= "13" && POSTERS >= "1"){SUBWEIGHT="0"; alert(error);}
else if (BOOKS >= "1" && POSTERS > "24"){SUBWEIGHT="0"; alert(error);}
else if (BOOKS >= "3" && BOOKS <= "12" && POSTERS >= "1" && POSTERS <= "24"){SUBWEIGHT = "16";}
else if (BOOKS >= "1" && BOOKS <= "2" && POSTERS >= "5" && POSTERS > "24"){SUBWEIGHT="0"; alert(error);}
else if (BOOKS >= "1" && BOOKS <= "2" && POSTERS >= "5" && POSTERS <= "24"){SUBWEIGHT="16";}
else if (BOOKS >= "1" && BOOKS <= "2" && POSTERS >= "1" && POSTERS <= "4"){SUBWEIGHT="5";}
else if (BOOKS >= "13" && BOOKS <= "24"){SUBWEIGHT = "18";}
else if (BOOKS > "24"){SUBWEIGHT="0"; alert(error);}
else if (BOOKS >= "4" && BOOKS <= "12"){SUBWEIGHT = "10";}
else if (BOOKS >= "1" && BOOKS <= "3"){SUBWEIGHT = "2";}
else if (POSTERS >= "13" && POSTERS <= "24"){SUBWEIGHT = "7";}
else if (POSTERS >= "3" && POSTERS <= "12"){SUBWEIGHT = "4";}
else if (POSTERS >= "1" && POSTERS <= "2"){SUBWEIGHT = "1";}
else if (POSTERS > "24"){SUBWEIGHT="0"; alert(error);}
else {}
return SUBWEIGHT;
}
function getSubtotal(form){
	var POSTERPRICE="13.00";
	var BOOKPRICE="9.00";
	var SUBTOTAL="0";
	var TOTAL="0";
	var POSTERQTY="0";
	var BOOKQTY="0";
	var BOOKTOTAL="0";
	var POSTERTOTAL="0";
	var TOTALTOTAL="0";
	
BOOKQTY = (form.B1.selectedIndex + form.B2.selectedIndex);
POSTERQTY = (form.P10.selectedIndex + form.P11.selectedIndex + form.P12.selectedIndex + form.P13.selectedIndex + form.P14.selectedIndex + form.P15.selectedIndex + form.P16.selectedIndex + form.P17.selectedIndex + form.P18.selectedIndex + form.P19.selectedIndex + form.P20.selectedIndex + form.P21.selectedIndex + form.P22.selectedIndex);
BOOKTOTAL = (BOOKPRICE * BOOKQTY);
POSTERTOTAL = (POSTERPRICE * POSTERQTY);
SUBTOTAL = (BOOKTOTAL + POSTERTOTAL);
TOTAL="$" + Dollar (SUBTOTAL);
form.subtotal.value  = (TOTAL);
return SUBTOTAL;
}

function getTax(form){
	var TAX="0";
	var TAXRATE="0.05";
	var STATE=form.state.selectedIndex;
	var SSTATE=form.Sstate.selectedIndex;
	var SUBTOTAL="0";
	var TAXRETURN="0";
	
SUBTOTAL=getSubtotal(form);
SUBTOTAL=parseFloat(SUBTOTAL);
if (SSTATE != "0"){
	if (SSTATE=="20") {TAX=TAXRATE*SUBTOTAL;}
	else {TAX="0";}
}	
else if (SSTATE == "0"){
	if (STATE=="20") {TAX=TAXRATE*SUBTOTAL;}
	else {TAX="0";}
}
TAXRETURN=TAX;
TAX=parseFloat(TAX);	
TAX="$"+Dollar (TAX);
form.tax.value=(TAX);
return TAXRETURN;
}

function getTotal(form){
	var SUBTOTAL=getSubtotal(form);
	var SHIPPING=CalcCost(form);
	var TAX=getTax(form);
	var TOTAL="0";
	var TOTALRETURN="0";
	
TOTAL=SUBTOTAL+TAX+SHIPPING;
TOTALRETURN=TOTAL;
TOTAL="$"+Dollar (TOTAL);
form.total.value=(TOTAL);
return TOTALRETURN;
}

function getGrandTotal(form){
	var SUBTOTAL=getSubtotal(form);
	var SHIPPING=CalcCost(form);
	var TAX=getTax(form);
	var GRANDTOTAL;
GRANDTOTAL=SUBTOTAL+SHIPPING+TAX;
form.grandtotal.value="$"+Dollar (GRANDTOTAL);
}
function doErrorCheck(form){
	var error1="Please fill in all the required Billing Address information";
	var error2="Please fill in all the required Payment Method information";
	var error3="Sorry! This combination/quantity of items will not fit in one box! Please read the IMPORTANT ORDERING INFO and try again!";
	var POSTERS = "0";
	var BOOKS = "0";
	var returnValue=true;

if (form.b_name.value=="" || form.b_addy.value=="" || form.b_city.value=="" || form.state.selectedIndex=="0" || form.b_zip.value=="" || form.b_phone.value=="" || form.b_email.value==""){
	alert(error1);
	returnValue=false;}
if (form.payment_type.selectedIndex != "2"){
	if (form.card_name.value=="" || form.cc_1.value == "" || form.cc_2.value == "" || form.cc_3.value == "" || form.cc_4.value == "" || form.card_month.selectedIndex == "0" || form.card_year.selectedIndex == "0" || form.card_code.value == ""){
		alert(error2);
		returnValue=false;}
	}
BOOKS = (form.B1.selectedIndex + form.B2.selectedIndex);
POSTERS = (form.P10.selectedIndex + form.P11.selectedIndex + form.P12.selectedIndex + form.P13.selectedIndex + form.P14.selectedIndex + form.P15.selectedIndex + form.P16.selectedIndex + form.P17.selectedIndex + form.P18.selectedIndex + form.P19.selectedIndex + form.P20.selectedIndex + form.P21.selectedIndex + form.P22.selectedIndex);
if (BOOKS >= "13" && POSTERS >= "1"){alert(error3);returnValue=false;}
else if (BOOKS >= "1" && POSTERS > "24"){alert(error3);returnValue=false;}
else if (BOOKS >= "1" && BOOKS <= "2" && POSTERS >= "5" && POSTERS > "24"){alert(error3);returnValue=false;}
else if (BOOKS > "24"){alert(error3);returnValue=false;}
else if (POSTERS > "24"){alert(error3);returnValue=false;}
else {}
return returnValue;
}
