// 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 = "";
// 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  -    http://www.fedex.com/ratetools/RateToolsMain.do
var uz1  = new Array (0,
6.34, 6.52, 6.57, 6.76, 7.04, 7.25, 7.63, 7.92, 8.13, 8.39, 
8.68, 8.92, 9.20, 9.38, 9.57, 9.71, 9.81, 9.94, 10.12, 10.32, 
10.54, 10.79, 11.04, 11.27, 11.56, 11.87, 12.14, 12.37, 12.65, 12.96, 
13.21, 13.32, 13.58, 13.75, 13.99, 14.23, 14.49, 14.71, 14.92, 15.07, 
15.30, 15.55, 15.75, 15.94, 16.12, 16.29, 16.47, 16.65, 16.83, 17.00, 
17.15, 17.33, 17.49, 17.67, 17.84, 18.00, 18.18, 18.36, 18.54, 18.72, 
18.90, 19.04, 19.21, 19.37, 19.53, 19.68, 19.85, 20.01, 20.17, 20.33);
var uz2  = new Array (0,
6.34, 6.52, 6.57, 6.76, 7.04, 7.25, 7.63, 7.92, 8.13, 8.39, 
8.68, 8.92, 9.20, 9.38, 9.57, 9.71, 9.81, 9.94, 10.12, 10.32, 
10.54, 10.79, 11.04, 11.27, 11.56, 11.87, 12.14, 12.37, 12.65, 12.96, 
13.21, 13.32, 13.58, 13.75, 13.99, 14.23, 14.49, 14.71, 14.92, 15.07, 
15.30, 15.55, 15.75, 15.94, 16.12, 16.29, 16.47, 16.65, 16.83, 17.00, 
17.15, 17.33, 17.49, 17.67, 17.84, 18.00, 18.18, 18.36, 18.54, 18.72, 
18.90, 19.04, 19.21, 19.37, 19.53, 19.68, 19.85, 20.01, 20.17, 20.33);
var uz3  = new Array (0,
6.57, 6.93, 7.25, 7.49, 7.60, 7.84, 8.07, 8.32, 8.55, 8.61, 
8.99, 9.27, 9.54, 9.82, 10.13, 10.28, 10.62, 10.91, 11.28, 11.65, 
12.03, 12.38, 12.76, 13.08, 13.44, 13.76, 14.07, 14.41, 14.69, 15.06, 
15.36, 15.69, 16.05, 16.40, 16.79, 17.02, 17.41, 17.68, 18.03, 18.43, 
18.72, 18.97, 19.31, 19.62, 19.97, 20.28, 20.50, 20.78, 21.04, 21.28, 
21.48, 21.65, 21.88, 22.07, 22.27, 22.47, 22.70, 22.95, 23.20, 23.44, 
23.67, 23.85, 24.09, 24.33, 24.61, 24.91, 25.17, 25.43, 25.67, 25.92);
var uz4  = new Array (0,
6.71, 7.53, 7.92, 8.32, 8.69, 8.83, 9.00, 9.25, 9.40, 9.57, 
9.73, 9.93, 10.08, 10.27, 10.44, 10.61, 10.94, 11.29, 11.71, 12.13, 
12.52, 12.96, 13.39, 13.82, 14.21, 14.64, 14.98, 15.45, 15.91, 16.39, 
16.81, 17.26, 17.69, 18.10, 18.54, 18.94, 19.34, 19.77, 20.21, 20.65, 
21.06, 21.47, 21.91, 22.26, 22.68, 23.01, 23.38, 23.74, 24.08, 24.41, 
24.70, 24.95, 25.18, 25.51, 25.75, 26.04, 26.32, 26.62, 26.93, 27.23, 
27.52, 27.77, 28.03, 28.32, 28.66, 28.98, 29.25, 29.53, 29.80, 30.11);
var uz5  = new Array (0,
7.00, 7.70, 8.19, 8.73, 9.09, 9.32, 9.57, 9.74, 9.98, 10.26, 
10.45, 10.66, 10.91, 11.16, 11.43, 11.64, 12.13, 12.75, 13.33, 13.95, 
14.54, 15.08, 15.64, 16.15, 16.63, 17.19, 17.71, 18.25, 18.72, 19.27, 
19.80, 20.37, 20.92, 21.46, 22.00, 22.55, 23.09, 23.64, 24.17, 24.69, 
25.22, 25.74, 26.28, 26.82, 27.36, 27.86, 28.38, 28.85, 29.36, 29.79, 
30.24, 30.65, 31.05, 31.47, 31.92, 32.38, 32.85, 33.19, 33.50, 33.75, 
34.14, 34.43, 34.72, 34.98, 35.21, 35.45, 35.65, 35.86, 36.07, 36.24);
var uz6  = new Array (0,
7.38, 8.23, 8.75, 9.25, 9.64, 9.82, 10.11, 10.44, 10.67, 11.13, 
11.52, 12.04, 12.58, 13.29, 13.99, 14.52, 15.33, 16.08, 16.78, 17.52, 
18.25, 18.99, 19.71, 20.45, 21.17, 21.96, 22.69, 23.43, 24.12, 24.90, 
25.62, 26.35, 27.04, 27.77, 28.49, 29.19, 29.86, 30.52, 31.20, 31.83, 
32.46, 33.05, 33.68, 34.28, 34.88, 35.42, 35.91, 36.44, 36.92, 37.39, 
37.84, 38.31, 38.75, 39.22, 39.68, 40.13, 40.57, 41.03, 41.40, 41.74, 
42.12, 42.43, 42.69, 43.00, 43.38, 43.82, 44.17, 44.40, 44.64, 44.78);
var uz7  = new Array (0,
7.47, 8.40, 9.01, 9.55, 10.03, 10.35, 10.62, 11.08, 11.66, 12.50, 
13.32, 14.15, 15.02, 15.83, 16.69, 17.40, 18.35, 19.17, 20.01, 20.86, 
21.69, 22.55, 23.38, 24.20, 25.02, 25.84, 26.68, 27.51, 28.26, 29.07, 
29.89, 30.73, 31.55, 32.38, 33.20, 34.02, 34.84, 35.67, 36.50, 37.28, 
38.07, 38.85, 39.69, 40.50, 41.32, 42.14, 42.96, 43.78, 44.61, 45.44, 
46.21, 46.93, 47.51, 47.95, 48.30, 48.66, 49.03, 49.31, 49.59, 49.85, 
50.13, 50.43, 50.76, 51.10, 51.38, 51.62, 51.86, 52.15, 52.44, 52.67);
var uz8  = new Array (0,
7.57, 8.73, 9.66, 10.32, 10.90, 11.25, 11.61, 12.32, 13.09, 13.97, 
14.90, 15.93, 16.95, 17.98, 19.01, 19.90, 21.00, 22.06, 23.13, 24.20, 
25.22, 26.31, 27.38, 28.44, 29.51, 30.57, 31.64, 32.74, 33.75, 34.83, 
35.88, 36.96, 38.01, 39.06, 40.09, 41.04, 41.83, 42.61, 43.40, 44.14, 
44.89, 45.63, 46.42, 47.19, 47.97, 48.76, 49.54, 50.33, 51.12, 51.90, 
52.68, 53.47, 54.26, 55.04, 55.81, 56.60, 57.32, 57.88, 58.32, 58.67, 
59.03, 59.39, 59.74, 60.09, 60.46, 60.89, 61.05, 61.19, 61.35, 61.44);
var uzx  = new Array (0,uz1,uz2,uz3,uz4,uz5,uz6,uz7,uz8);

function SetFedExZ () {  // set FedEx lower 48 ground Zones
  // built from FedEx Phoenix zone chart, lower 48
  SetZone (
  0, 8, 4, 8, 10, 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 CalcCostOnly (obj1) {  // given weight and ZIP, calc ship cost
var str = "(error)";
  wte = Math.ceil (itmw / 1.0);  // nearest pounds
	
	zip = get_cookie("ziponfile");       // Check for zip code in cookie

  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
	create_add_to_cart_form();   // Replaces the contents of the div tag "add_to_cart_form" with "Add to Cart" form
	
}

function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < 1) rnd = 1;
	
	val = val + 3.45; // add $2.20 for residential and 50% for DAS resi charge of $2.50
	val = val*1.06; //fuel surcharge   -   http://www.fedex.com/us/services/fuelsurcharge.html
	
  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
  }
}

function SetWt (lbs) {  // set item weight in ounces
  itmw = lbs;
}

function SetItemName (itemname) {  // set item weight in ounces
  itmname = itemname;
}

function SetItemNumber (itemnumber) {  // set item weight in ounces
  itmnumber = itemnumber;
}

function SetItemPrice (itemprice) {  // set item weight in ounces
  itmprice = itemprice;
}

function get_cookie ( cookie_name )  // Search though the cookie variables and return the value of the indicated "cookie_name"
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

function ShowShipping () {  // given weight and ZIP, calc ship cost
	
	 wte = Math.ceil (itmw / 1.0);
	 
	zip = get_cookie("ziponfile");  // Check for existing zip code in cookie
	
	if (zip == null)  // If no cookie, return -1
	{
     return -1;
	}
	else
	{
				if (zip.length == 0) {          // made entry before?
	         return -1;
				}
	}

	document.cookie = "ziponfile=" + escape(zip) + "; path=/" ;  // escape converts zip number to string
//	document.cookie = "ziponfile=" + escape(zip);
  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

  return (Dollar(amt));
}

function create_add_to_cart_form() {

var str = "(error)";
  wte = Math.ceil (itmw / 1.0);  // nearest pounds
   
	zip = get_cookie("ziponfile");

  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

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 type="hidden" name="cmd" value="_cart" />');
the_form = the_form + ('<input type="hidden" name="add" value="1" />');
the_form = the_form + ('<input type="hidden" name="business" value="customer_order@ceiling-fans-by-hunter.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="' + itmprice + '" />');
the_form = the_form + ('<input type="hidden" name="return" value="http://www.ceiling-fans-by-hunter.com/thanks.htm">');
the_form = the_form + ('<input type="hidden" name="cancel_return" value="http://www.ceiling-fans-by-hunter.com/cancel.htm">');
the_form = the_form + ('<input type="hidden" name="currency_code" value="USD" />');
the_form = the_form + ('<input type="hidden" name="no_note" value="1">');
the_form = the_form + ('<input type="hidden" name="lc" value="US" />');
the_form = the_form + ('<input type="hidden" name="shipping" value="' + Dollar (amt) +'" />');
the_form = the_form + ('<input type="hidden" name="shipping2" value="' + Dollar (amt) + '" />');
the_form = the_form + ('<input type="hidden" name="on0" value="shipping" />');
temp_value = 'zone ' + zne + ', $' + Dollar(amt) + '/item';
the_form = the_form + ('<input type="hidden" name="os0" value= "' + temp_value + '">');
the_form = the_form + ('<input type="hidden" name="quantity" value="1" /> ');
the_form = the_form + ('<input name="submit" type="image" src="http://www.ceiling-fans-by-hunter.com/images/cart_add.jpg" alt="add to cart" width="103" height="21" />');
the_form = the_form + ('  </form>');

document.getElementById("add_to_cart_form").innerHTML= '<span class="size-10B">Shipping: $'+ Dollar(amt) +'</span><br/><br/>' + the_form + view_cart_button();
}

function  shipcost_and_addcart(the_weight, the_item_name, the_item_number, the_item_price)   // Create the shipping button and add to cart button 

{
	if (the_item_price < 2000)
	 {
	 the_item_price = the_item_price / 55;
	 }
  else if (the_item_price < 3000)
		{
		the_item_price = the_item_price / 57.6;
		}
  else if (the_item_price < 4000)
		{
		the_item_price = the_item_price / 58.8;
		}
  else if (the_item_price < 5000)
		{
		the_item_price = the_item_price / 60.1;
		}
  else if (the_item_price < 6000)
		{
		the_item_price = the_item_price / 61.4;
		}
  else if (the_item_price < 7000)
		{
		the_item_price = the_item_price / 62.7;
		}
  else if (the_item_price < 8000)
		{
		the_item_price = the_item_price / 64;
		}
  else if (the_item_price < 9000)
		{
		the_item_price = the_item_price / 65.3;
		}
  else if (the_item_price < 10000)
		{
		the_item_price = the_item_price / 66.5;
		}
  else if (the_item_price < 11000)
		{
		the_item_price = the_item_price / 67.8;
		}
  else if (the_item_price < 12000)
		{
		the_item_price = the_item_price / 69.1;
		}
  else if (the_item_price < 13000)
		{
		the_item_price = the_item_price / 70.4;
		}
  else if (the_item_price < 14000)
		{
		the_item_price = the_item_price / 71.7;
		}
  else if (the_item_price < 15000)
		{
		the_item_price = the_item_price / 72.9;
		}
  else if (the_item_price < 16000)
		{
		the_item_price = the_item_price / 74.2;
		}
  else if (the_item_price < 17000)
		{
		the_item_price = the_item_price / 75.5;
		}
  else if (the_item_price < 18000)
		{
		the_item_price = the_item_price / 76.8;
		}
  else if (the_item_price < 19000)
		{
		the_item_price = the_item_price / 78.1;
		}
  else if (the_item_price < 20000)
		{
		the_item_price = the_item_price / 79.4;
		}
  else if (the_item_price > 19999)
		{
		the_item_price = the_item_price / 80;
		}

            the_item_price = the_item_price * 1.00;
						the_item_price = the_item_price.toFixed(2)
						document.getElementById("item_price").innerHTML=  "Price: $" + the_item_price;

						SetWt (the_weight);
						SetItemName(the_item_name);
						SetItemNumber(the_item_number);
						SetItemPrice(the_item_price);
						rtn = ShowShipping();
						if (rtn == -1)
						{
								 document.write('<a herf="" style="cursor:hand" onClick="get_zip_code(\'Enter U. S. zip code\',\'\');"><img src="http://www.ceiling-fans-by-hunter.com/images/cart_ship.jpg" alt ="shipping cost" width="103" height="21" ></a><br><br>');
								 document.write(view_cart_button());
						}
						else
						{
								 CalcCostOnly(this);
						} 
}

function enter_clicks_go()
{
  if(window.event && window.event.keyCode == 13)
  {
     process_zip_code();
     return false;
  }
  return true;
}

function get_zip_code(top_text, the_status)
{

the_form = '';

the_form = the_form + ('<form name="zipcodeform">');
the_form = the_form + ('<input type="text" size="6" name="textzipcode" onkeypress="return enter_clicks_go()"  value="'  +  the_status +  '" />');
the_form = the_form + ('&nbsp;&nbsp;<a herf="" style="cursor:hand" onClick="process_zip_code();"><img src="/images/go_button.jpg" alt ="shipping cost" width="36" height="19" valign="bottom"></a>');
the_form = the_form + ('  </form>');

document.getElementById("add_to_cart_form").innerHTML=  '<table border="1" cellpadding="8" bgcolor="#FFFFCC"><tr><td>' + top_text + the_form + '</td></tr></table>' + view_cart_button();


}

function process_zip_code()
{

zip =  document.zipcodeform.textzipcode.value;

if (zip == "")
{
   get_zip_code('<font color="red">Enter U. S. zip code</font>',' invalid');
	 return(0);
}


if(zip.substring(0,3)=="967" || zip.substring(0,3)=="968")
{
   get_zip_code('<font color="red">Sorry, we do not<br>ship to Hawaii</font>','');
	 return(0);
}

if(zip.substring(0,3)=="995" || zip.substring(0,3)=="996" || zip.substring(0,3)=="997" || zip.substring(0,3)=="998" || zip.substring(0,3)=="999")
{
   get_zip_code('<font color="red">Sorry, we do not<br>ship to Alaska</font>','');
	 return(0);
}

 

					if (zip.length == 3)
					{
					   zip = '00' + zip;
					}
					if (zip.length == 4)
					{
					   zip = '0' + zip;
					}					
					zip = zip.substring(0, 3);    // 1st 3 chars of zip

  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
    }
  }

	if(zne == null)
	{
		 get_zip_code('<font color="red">Enter U. S. Zip Code</font>',' invalid');
		 return(0);
	}

	document.cookie = "ziponfile=" + escape(zip) + "; path=/" ;  // escape converts zip number to string

  amt = uzx[zne][wte];            // FedEx charges
	create_add_to_cart_form();   // Replaces the contents of the div tag "add_to_cart_form" with "Add to Cart" form

}

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@ceiling-fans-by-hunter.com" />');
the_form = the_form + ('<input type="hidden" name="display" value="1" /> ');
the_form = the_form + ('<input name="submit" type="image" src="http://www.ceiling-fans-by-hunter.com/images/cart_view.jpg" alt="view cart" width="103" height="21" />');
the_form = the_form + ('  </form>');

return(the_form);

}
