// JavaScript Document
var zn = 0;               // ZIP, zone entries index
var zzip = new Array ();  // ZIP entry
var zone = new Array ();  // corresponding zone for ZIP

// stuff for shipping support...
var zip="";   // shipping zip code
var wte;      // total weight of order
var zne;      // shipping zone
var amt;      // priority shipping charge
var itmw = 0; // wright of single item (lbs)
var itmname = "";
var itmnumber = "";
var itmprice = "";
var the_item_price = "";
var dscntnd = 0;
// FedEx ground, lower 48 charges for 1 to 100 lbs. 26 June 2007
// FYI uz1 and uz2 are the same and both are zone 2
var uz1  = new Array (0,
4.00, 4.04, 4.08, 4.19, 4.48, 4.55, 4.81, 5.00, 5.15, 5.33, 
5.51, 5.67, 5.86, 5.98, 6.13, 6.17, 6.26, 6.37, 6.52, 6.66, 
6.82, 6.97, 7.15, 7.31, 7.48, 7.68, 7.86, 8.03, 8.21, 8.41, 
8.57, 8.65, 8.82, 8.98, 9.14, 9.30, 9.47, 9.62, 9.75, 9.90, 
10.04, 10.19, 10.33, 10.47, 10.58, 10.69, 10.78, 10.90, 10.99, 11.09, 
11.20, 11.31, 11.41, 11.52, 11.63, 11.73, 11.83, 11.93, 12.03, 12.15, 
12.25, 12.35, 12.46, 12.57, 12.67, 12.77, 12.88, 12.96, 13.10, 13.25, 
13.84, 14.56, 15.28, 16.00, 16.72, 17.44, 18.16, 18.88, 19.60, 20.32, 
21.04, 21.78, 22.53, 23.29, 24.06, 24.84, 25.63, 26.44, 27.26, 28.08, 
28.78, 29.49, 30.21, 30.94, 31.67, 32.41, 33.16, 33.92, 34.69, 35.46);
var uz2  = new Array (0,
4.00, 4.04, 4.08, 4.19, 4.48, 4.55, 4.81, 5.00, 5.15, 5.33, 
5.51, 5.67, 5.86, 5.98, 6.13, 6.17, 6.26, 6.37, 6.52, 6.66, 
6.82, 6.97, 7.15, 7.31, 7.48, 7.68, 7.86, 8.03, 8.21, 8.41, 
8.57, 8.65, 8.82, 8.98, 9.14, 9.30, 9.47, 9.62, 9.75, 9.90, 
10.04, 10.19, 10.33, 10.47, 10.58, 10.69, 10.78, 10.90, 10.99, 11.09, 
11.20, 11.31, 11.41, 11.52, 11.63, 11.73, 11.83, 11.93, 12.03, 12.15, 
12.25, 12.35, 12.46, 12.57, 12.67, 12.77, 12.88, 12.96, 13.10, 13.25, 
13.84, 14.56, 15.28, 16.00, 16.72, 17.44, 18.16, 18.88, 19.60, 20.32, 
21.04, 21.78, 22.53, 23.29, 24.06, 24.84, 25.63, 26.44, 27.26, 28.08, 
28.78, 29.49, 30.21, 30.94, 31.67, 32.41, 33.16, 33.92, 34.69, 35.46);
var uz3  = new Array (0,
4.09, 4.29, 4.49, 4.65, 4.73, 4.93, 5.08, 5.25, 5.40, 5.44, 
5.70, 5.89, 6.08, 6.27, 6.47, 6.59, 6.81, 7.03, 7.26, 7.50, 
7.74, 7.97, 8.20, 8.41, 8.64, 8.85, 9.05, 9.27, 9.48, 9.72, 
9.92, 10.13, 10.36, 10.59, 10.83, 11.03, 11.27, 11.48, 11.70, 11.96, 
12.16, 12.38, 12.60, 12.81, 13.04, 13.25, 13.44, 13.63, 13.80, 13.96, 
14.08, 14.21, 14.36, 14.48, 14.61, 14.74, 14.89, 15.01, 15.13, 15.26, 
15.37, 15.49, 15.64, 15.80, 15.98, 16.18, 16.40, 16.64, 16.90, 17.03, 
17.45, 18.11, 18.77, 19.43, 20.09, 20.75, 21.41, 22.07, 22.73, 23.39, 
24.05, 24.71, 25.37, 26.03, 26.69, 27.35, 28.01, 28.67, 29.33, 29.99, 
30.65, 31.31, 31.97, 32.63, 33.29, 33.95, 34.61, 35.19, 35.77, 36.37);
var uz4  = new Array (0,
4.22, 4.72, 4.96, 5.24, 5.49, 5.58, 5.72, 5.87, 5.99, 6.14, 
6.25, 6.39, 6.50, 6.63, 6.75, 6.85, 7.06, 7.29, 7.57, 7.83, 
8.08, 8.37, 8.64, 8.93, 9.21, 9.48, 9.76, 10.06, 10.36, 10.67, 
10.95, 11.24, 11.53, 11.81, 12.10, 12.36, 12.64, 12.92, 13.21, 13.49, 
13.76, 14.03, 14.31, 14.55, 14.82, 15.04, 15.28, 15.51, 15.74, 15.95, 
16.17, 16.37, 16.56, 16.78, 16.94, 17.13, 17.31, 17.48, 17.63, 17.79, 
17.94, 18.09, 18.29, 18.49, 18.69, 18.88, 19.07, 19.25, 19.45, 19.66, 
19.86, 20.48, 21.10, 21.72, 22.34, 22.96, 23.58, 24.20, 24.82, 25.44, 
26.06, 26.68, 27.29, 27.81, 28.42, 29.04, 29.76, 30.38, 30.99, 31.61, 
32.33, 32.95, 33.67, 34.28, 35.00, 35.72, 36.34, 37.06, 37.78, 38.50);
var uz5  = new Array (0,
4.39, 4.80, 5.12, 5.46, 5.72, 5.89, 6.10, 6.24, 6.40, 6.58, 
6.75, 6.93, 7.09, 7.25, 7.42, 7.57, 7.88, 8.28, 8.66, 9.06, 
9.44, 9.80, 10.16, 10.49, 10.81, 11.16, 11.50, 11.85, 12.20, 12.56, 
12.90, 13.26, 13.63, 13.98, 14.33, 14.68, 15.04, 15.40, 15.74, 16.10, 
16.46, 16.81, 17.17, 17.52, 17.88, 18.20, 18.53, 18.85, 19.17, 19.46, 
19.75, 20.02, 20.29, 20.55, 20.85, 21.15, 21.45, 21.68, 21.89, 22.05, 
22.19, 22.34, 22.49, 22.64, 22.79, 22.95, 23.05, 23.20, 23.35, 23.50, 
23.85, 24.53, 25.19, 25.81, 26.43, 27.05, 27.22, 28.29, 28.91, 29.53, 
30.15, 30.77, 31.39, 32.01, 32.63, 33.25, 33.87, 34.49, 35.11, 35.62, 
36.24, 36.75, 37.37, 37.88, 38.50, 39.11, 39.73, 40.24, 40.76, 41.38);
var uz6  = new Array (0,
4.62, 5.15, 5.50, 5.82, 6.09, 6.29, 6.52, 6.74, 6.91, 7.20, 
7.51, 7.85, 8.23, 8.69, 9.15, 9.50, 9.98, 10.46, 10.93, 11.41, 
11.88, 12.36, 12.83, 13.31, 13.79, 14.29, 14.78, 15.26, 15.75, 16.26, 
16.72, 17.20, 17.65, 18.13, 18.60, 19.06, 19.49, 19.93, 20.36, 20.80, 
21.23, 21.64, 22.05, 22.44, 22.83, 23.18, 23.51, 23.85, 24.17, 24.48, 
24.78, 25.08, 25.37, 25.67, 25.98, 26.27, 26.56, 26.85, 27.10, 27.33, 
27.58, 27.78, 27.95, 28.15, 28.32, 28.47, 28.62, 28.77, 28.92, 29.07, 
29.56, 30.13, 30.70, 31.27, 31.84, 32.41, 32.98, 33.55, 34.12, 34.69, 
35.26, 35.83, 36.40, 36.97, 37.54, 38.10, 38.61, 39.13, 39.65, 40.17, 
40.69, 41.21, 41.73, 42.25, 42.76, 43.30, 43.82, 44.35, 44.87, 45.39);
var uz7  = new Array (0,
4.70, 5.28, 5.69, 6.02, 6.34, 6.60, 6.85, 7.18, 7.57, 8.11, 
8.69, 9.27, 9.86, 10.43, 11.02, 11.49, 12.05, 12.60, 13.15, 13.71, 
14.26, 14.82, 15.36, 15.93, 16.50, 17.03, 17.56, 18.10, 18.70, 19.32, 
19.90, 20.47, 21.06, 21.65, 22.24, 22.82, 23.40, 23.99, 24.56, 25.15, 
25.73, 26.32, 26.89, 27.46, 28.04, 28.54, 29.06, 29.57, 30.05, 30.54, 
31.00, 31.37, 31.72, 31.99, 32.22, 32.38, 32.58, 32.76, 32.95, 33.13, 
33.28, 33.49, 33.65, 33.80, 33.98, 34.14, 34.27, 34.40, 34.50, 34.60, 
34.65, 35.15, 35.95, 36.65, 37.35, 38.05, 38.75, 39.41, 39.97, 40.53, 
41.09, 41.65, 42.21, 42.74, 43.20, 43.66, 44.12, 44.58, 45.04, 45.48, 
45.94, 46.44, 46.93, 47.43, 47.93, 48.42, 48.92, 49.42, 49.91, 50.41);
var uz8  = new Array (0,
4.81, 5.59, 6.21, 6.61, 7.01, 7.25, 7.58, 8.10, 8.62, 9.21, 
9.87, 10.59, 11.30, 12.02, 12.72, 13.29, 13.99, 14.69, 15.41, 16.13, 
16.83, 17.53, 18.24, 18.95, 19.66, 20.36, 21.05, 21.77, 22.49, 23.21, 
23.91, 24.63, 25.33, 26.03, 26.74, 27.44, 28.13, 28.80, 29.46, 30.13, 
30.79, 31.45, 32.12, 32.79, 33.45, 34.07, 34.68, 35.25, 35.76, 36.25, 
36.70, 37.17, 37.55, 37.90, 38.20, 38.45, 38.71, 38.97, 39.20, 39.44, 
39.68, 39.89, 40.12, 40.29, 40.48, 40.70, 40.80, 40.90, 41.00, 41.10, 
41.24, 41.43, 41.62, 41.81, 42.00, 42.19, 42.38, 42.69, 43.21, 43.73, 
44.25, 44.76, 45.27, 45.78, 46.29, 46.81, 47.31, 47.84, 48.37, 48.87, 
49.40, 49.93, 50.44, 50.97, 51.49, 52.01, 52.55, 53.06, 53.59, 54.11);
var uzx  = new Array (0,uz1,uz2,uz3,uz4,uz5,uz6,uz7,uz8);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function SetFedExZ () {  // set FedEx lower 48 ground Zones
  SetZone (
  0, 8, 4, 8, 242,	 7, 244,	 8, 246,	 7, 249,	 8, 250,	 7, 254,	 8, 255,	 7, 262,	 8, 266,	 7, 
  267,	 8, 280,	 7, 283,	 8, 286,	 7, 294,	 8, 296,	 7, 327,	 8, 337,	 7, 338,	 8, 344,	 7, 
  346,	 8, 350,	 7, 365,	 6, 367,	 7, 369,	 6, 370,	 7, 375,	 6, 376,	 7, 380,	 6, 384,	 7, 
  386,	 6, 398,	 7, 420,	 6, 421,	 7, 424,	 6, 425,	 7, 476,	 6, 478,	 7, 500,	 6, 530,	 7, 
  535,	 6, 539,	 7, 540,	 6, 541,	 7, 546,	 6, 549,	 7, 550,	 6, 575,	 5, 576,	 6, 577,	 5, 
  580,	 6, 590,	 5, 592,	 6, 593,	 5, 595,	 6, 596,	 5, 599,	 6, 600,	 7, 610,	 6, 664,	 5, 
  667,	 6, 668,	 5, 680,	 6, 683,	 5, 687,	 6, 688,	 5, 700,	 6, 730,	 5, 749,	 6, 750,	 5, 
  755,	 6, 757,	 5, 759,	 6, 760,	 5, 770,	 6, 778,	 5, 790,	 4, 792,	 5, 793,	 4, 795,	 5, 
  797,	 4, 807,	 5, 808,	 4, 820,	 5, 829,	 4, 832,	 5, 838,	 6, 840,	 4, 850,	 2, 864,	 3, 
  870,	 4, 873,	 3, 874,	 4, 879,	 3, 880,	 4, 889,	 3, 893,	 4, 919,	 3, 926,	 4, 940,	 5, 
  950,	 4, 954,	 5, 961,	 4, 962,	 5, 970,	 6, 973,	 5, 980,	 6, 987,	 5, 988,	 6, 993,	 5);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function Dollar (val) {  // force to valid dollar amount
  var str,pos,rnd=0;
  if (val < 1) rnd = 1;
    val = val + 1.85; // add for residential
    val = val*1.045; //fuel surcharge
    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 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
  }
}

//~~~~~~~Search though the cookie variables and return the value of the indicated "cookie_name"~~~~~~~~~~

function get_cookie ( cookie_name ) {
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~ Create the shipping button and add to cart button ~~~~~~~~~~~~~~~~~~~~~~~~~    

function  shipcost_and_addcart(the_weight, the_item_name, the_item_number, the_item_price) {
  // the 4 variables below are assume that I want to divide items costing $1 by .55 and
	// items costing $200 to be divided .8 and everything between on a sliding scale - example
	// example items costing $50 would be divided by ~ .611558 or $81.76 retail
	low_cost = 100 
	high_cost = 20000
	mu_low_end = 55
	hu_high_end = 80
	
    if (the_item_price == 0) {
    dscntnd = -1;
  }
	
	if (the_item_price < high_cost) {
    t_i_p = the_item_price / (((the_item_price - 100) * ((hu_high_end - mu_low_end) / (high_cost - low_cost))) + mu_low_end);
  }
  else if (the_item_price > high_cost - 1) {
    t_i_p = the_item_price / 80;
  }

	//  use the next line to adjust all prices up or down by % 
   // t_i_p = t_i_p * .90;


// the following if statement added for items with UMRP pricing
    if (the_weight == 0) {
    t_i_p = the_item_price / 100;
  }

  itmw = the_weight;
	itmname = the_item_name;
  itmnumber = the_item_number;
  itmprice = t_i_p;
  frmnumber = the_item_number;
	create_add_to_cart_form();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function create_add_to_cart_form() {
  var str = "(error)";
  wte = Math.ceil (itmw / 1.0);  // nearest pounds21
  zip = get_cookie("ziponfile");
	if (zip == null) { // If no cookie, return -1
    return -1;
  }
  SetFedExZ ();     // set up FedEx ground shipping zones
  str = "Shipping cost for FedEx Ground";
  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, now
    }
  }
  amt = uzx[zne][wte];          // FedEx charges

  shipamt = Dollar(amt)*1 + itmprice;


	if (itmw == 0) {
    shipamt = itmprice;
  }

	shipamt = shipamt.toFixed(2);

//	
	if (dscntnd == -1) {
    shipamt = '  DISCONTINUED';
    dscntnd = 0;
  }
//

	the_form = '';
  the_form = the_form + ('<form name="addtocart" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">');
  the_form = the_form + ('<input name="submit" type="image" src="http://www.fans-ceilingfans.com/images/cart_add.jpg" alt="add to cart" width="103" height="21" />');
  the_form = the_form + ('<input type="hidden" name="add" value="1" />');
  the_form = the_form + ('<input type="hidden" name="cmd" value="_cart" />');
  the_form = the_form + ('<input type="hidden" name="business" value="customer_order@fans-ceilingfans.com" />');
  the_form = the_form + ('<input type="hidden" name="item_name" value="' + itmname + '" />');
  the_form = the_form + ('<input type="hidden" name="item_number" value="' + itmnumber + '" />');
  the_form = the_form + ('<input type="hidden" name="amount" value="' + shipamt + '" />');
  the_form = the_form + ('<input type="hidden" name="return" value="http://www.fans-ceilingfans.com/thanks.htm">');
  the_form = the_form + ('<input type="hidden" name="cancel_return" value="http://www.fans-ceilingfans.com/cancel.htm">');
  the_form = the_form + ('<input type="hidden" name="no_note" value="1">');
  the_form = the_form + ('<input type="hidden" name="currency_code" value="USD" />');
  the_form = the_form + ('  </form>');
  document.getElementById(frmnumber).innerHTML= '<font size="2" color="black">Price: $'+ shipamt +'</font><br><br>' + the_form + view_cart_button();
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function view_cart_button() {
  the_form = '';
  the_form = the_form + (' <form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">');
  the_form = the_form + ('<input type="hidden" name="cmd" value="_cart" />');
  the_form = the_form + ('<input type="hidden" name="business" value="customer_order@fans-ceilingfans.com" />');
  the_form = the_form + ('<input type="hidden" name="display" value="1" /> ');
  the_form = the_form + ('<input name="submit" type="image" src="http://www.fans-ceilingfans.com/images/cart_view.jpg" alt="view cart" width="103" height="21" />');
  the_form = the_form + ('  </form>');
  return(the_form);
}