// JavaScript Document

//~~~~~~~~~~~~~~~~~~~~~~~~~ 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 = 10000
	mu_low_end = 68
	hu_high_end = 85
	
    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 / hu_high_end;
  }

	//  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;
  }

  var theReferrer = document.cookie.match ( "referreronfile" + '=(.*?)(;|$)' ); // added to track the referrer
  theReferrer = ( unescape ( theReferrer[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="custom" value="' + theReferrer + '" />'); 
  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);
}