
function submitPaypalForm(name)
{
  var form = document.forms[name];
  FixBusi(form);
  form.target='paypal'; 
  form.submit();
}

// force to valid dollar amount
function Dollar(val)
{
  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(".");
  if (pos > 0) str = str.substring(rnd, pos + 3);
  return str;
}

// record selected shipping option
function SetShip (obj1)
{
  var j,obj,pos,tok,val,txt;
  var ary = new Array();
  obj = obj1.shp;          // refer to shipping select
  pos = obj.selectedIndex; // get selection
  if (pos < 2) {          // force a selection
    alert ("You must choose a state to ship to!");
    return false;
  }
  
  obj1.target='paypal'; 
  obj1.handling.value = "0";
  obj1.handling_cart.value = "0";
  obj1.shipping.value = "0";
  obj1.shipping2.value = "0";
  val = obj.options[pos].value;   // selected value
  txt = obj.options[pos].text;    // the text value
  ary = val.split(" ");           // break apart
  for (j=0; j<ary.length; j++)    // look at all items
  {
    // do 3-character tokens...
    if (ary[j].length < 4) continue;
    tok = ary[j].substring(0,3); // first 3 chars
    val = ary[j].substring(3);   // get data
    if (tok == "hn=")             // value for item handling
      obj1.handling.value = val;
    if (tok == "hc=")             // value for handling cart
      obj1.handling_cart.value = val;
    if (tok == "s1=")             // value for shipping
      obj1.shipping.value = val;
    if (tok == "s2=")             // value for shipping2
      obj1.shipping2.value = val;
  }
  obj1.os0.value = txt;           // stuff the text into options field
  
  return true;
}

function getShipStates()
{
  return new Array(
    "AR", "Arkansas",
    "AZ", "Arizona",
    "CA", "California",
    "CO", "Colorado",
    "CT", "Connecticut",
    "DC", "District of Columbia",
    "DE", "Deleware",
    "GA", "Georgia",
    "HI", "Hawaii",
    "IA", "Iowa",
    "ID", "Idaho",
    "IL", "Illinois",
    "IN", "Indiana",
    "KS", "Kansas",
    "KY", "Kentucky",
    "LA", "Louisiana",
    "MI", "Michigan",
    "ME", "Maine",
    "MN", "Minnesota",
    "MO", "Missouri",
    "NC", "North Carolina",
    "ND", "North Dakota",
    "NE", "Nebraska",
    "NJ", "New Jersey",
    "NM", "New Mexico",
    "NV", "Nevada",
    "OH", "Ohio",
    "OK", "Oklahoma",
    "OR", "Oregon",
    "RI", "Rhode Island",
    "SC", "South Carolina",
    "SD", "South Dakota",
    "TN", "Tennessee",
    "TX", "Texas",
    "VA", "Virginia",
    "VT", "Vermont",
    "WA", "Washington",
    "WI", "Wisconsin",
    "WV", "West Verginia",
    "WY", "Wyoming"
  );
}

// Item definitions for GetShipping()
var item_2Btls = 0;
var item_4Btls = 1;
var item_halfCase = 2;
var item_fullCase = 3;
var item_addlCase = 4;
var item_2Mags = 5;
var item_4Mags = 6;
var item_6Mags = 7;

// Gets the shipping charge for the specified state and amount
function GetShipping(state, itemNumber)
{
  // Set up shipping zone tables
  //                        "2-btl", "4-btl", "half-", "case-", "addl-", "2-Mag", "4-Mag", "6-Mag"
  var shipZone1 = new Array("28.00", "28.00", "25.00", "37.00", "35.00", "28.00", "28.00", "53.00");
  var shipZone2 = new Array("28.00", "29.00", "28.00", "42.00", "38.00", "28.00", "29.00", "57.00");
  var shipZone3 = new Array("29.00", "32.00", "34.00", "52.00", "50.00", "30.00", "33.00", "64.00");
  var shipZone4 = new Array("32.00", "36.00", "37.00", "59.00", "57.00", "33.00", "37.00", "70.00");
  var shipZone5 = new Array("",      "",      "58.00", "86.00", "82.00", "",      "",      "");
  var shipZone6 = new Array("",      "",      "93.00", "138.00","133.00","",      "",      "");

   var shipZone;
   switch (state)
   {
      case "CA":
      case "OR": shipZone = shipZone1;
         break;
      case "AZ":
      case "CO":
      case "ID":
      case "NM":
      case "NV":
      case "WA":
      case "WY": shipZone = shipZone2;
         break;
      case "IA":
      case "KS":
      case "ND":
      case "MO":
      case "NE":
      case "OK":
      case "SD": shipZone = shipZone3;
         break;
      case "CT":
      case "DC":
      case "DE":
      case "GA":
      case "IL":
      case "IN":
      case "KY":
      case "LA":
      case "ME":
      case "MI":
      case "MN":
      case "NC":
      case "NJ":
      case "OH":
      case "RI":
      case "SC":
      case "TN":
      case "TX":
      case "VA":
      case "VT":
      case "WI":
      case "WV": shipZone = shipZone4;
         break;
      case "AR": shipZone = shipZone5;
         break;
      case "HI": shipZone = shipZone6;
         break;
      default:
        alert("Invalid shipping state. Please contact PMV to report the problem.");
   }   
   
   if (shipZone != null)
   {
     return shipZone[itemNumber];
   }
   
   return 0;
}

function AllowsMagnums(state)
{
   switch (state)
   {
      case "CA":
      case "OR":
      case "AZ":
      case "CO":
      case "NV":
      case "WA":
      case "NM":
      case "ID":
      case "WY":
      case "ND":
      case "IA":
      case "KS":
      case "DC":
      case "MI":
      case "WV":
      case "LA":
      case "SC":
      case "TX":
      case "IL":
      case "KY":
         return true;
     default:
        return false;
   }
}

/**
 * Sets shipping charges for the selected state and amount ordered
 * form1 - Paypal form being submitted
 * amountIndex - Index of the amount (0=2btls, 1=4btls, 2=half, 3=full)
 */
function SetShipping(form1, amountIndex)
{
  var idxState = form1.shp.selectedIndex; // get state selection
  if (idxState < 2)
  {          
    // force a selection
    alert ("You must choose a state to ship to!");
    return false;
  }
  
  // Init form values
  form1.target='paypal';
  form1.handling.value = "0";
  form1.handling_cart.value = "0";
  form1.shipping.value = "0";
  form1.shipping2.value = "0";
  
  // Get text to be placed on order form and correct index for shipping table
  switch (amountIndex)
  {
    case item_2Btls:
      form1.item_name.value = form1.base_item_name.value + " 2 Bottles.";
      break;
    case item_4Btls:
      form1.item_name.value = form1.base_item_name.value + " 4 Bottles.";
      break;
    case item_halfCase:
      form1.item_name.value = form1.base_item_name.value + " Half Case.";
      break;
    case item_fullCase:
      form1.item_name.value = form1.base_item_name.value + " Full Case.";
      break;
    default:
      alert("Invalid wine amount. Please contact PMV to report the problem.");
      return false;
  }

  // Get selected state code
  var stateCode = form1.shp.options[idxState].value;

  // Set shipping charge on the form
  form1.shipping.value = GetShipping(stateCode, amountIndex);
  //alert("ship value="+shipTable[stateCode][amountIndex]);
  
  // Set shipping charge for each additional item
  if (amountIndex == item_fullCase)
  {
     form1.shipping2.value = GetShipping(stateCode, item_addlCase);
  }
  else
  {
     form1.shipping2.value = form1.shipping.value;
  }
  
  // Set the state name into options field
  form1.os0.value = form1.shp.options[idxState].text;

  // Save the user's state selection in a cookie
  setCookie("state", stateCode, 365);

  return true;
}

// Sets shipping charges for the selected state and amount ordered
function SetShip4State(form1)
{
  // Amount selection; 0=full, 1=half case
  var amountIndex = form1.amount.selectedIndex;
  if (amountIndex == 1)
  {
    return SetShipping(form1, item_halfCase);
  }
  else
  {
    return SetShipping(form1, item_fullCase);
  }
}

function SetShip4SamplePack(form1)
{
  SetShipping(form1, item_4Btls);
}

// global variables to form MY addresses
var pref = ""; // special prefix, or ""
var at1 = "@";
var dot = ".";
var typ = "com";
var id1 = "in";    // real id1
var id2 = "fo";    // real id2
var url = "paulmathewvineyards";       // real URL
var fid1 = "paul";      // fake id1
var fid2 = "002"    // fake id2
var furl = "yahoo";    // fake URL
var busId1 = "ma"; // real business id1
var busId2 = "t"; // real business id2

function FixBusi (obj1) {  // PayPal FORM business value
var tmp;
  if (obj1.business) {     // see what is in this form 
    tmp = obj1.business.value;
    obj1.business.value = busId1 + busId2 + at1 + url + dot + typ;
    //alert ("Fake business was ... \n   " + tmp + "\n\n\n" +
    //       "Real business is  ... \n   " + obj1.business.value);
  }
  return true;             // make it work...
}

function FixLink (obj1) {  // fix any PayPal link in calling FORM
var tmp,org,ary=new Array();
  for (i=0; i<obj1.length; i++) {  // run whole FORM
    obj = obj1.elements[i];        // address an element
alert(obj.name);
    org = obj.value;
    tmp = org;  // place holder
    ary = tmp.split (furl);  // do we have a fake
    if (ary.length > 1) {    // still a fake
      tmp = ary.join (pref + url);
      obj.value = tmp;
alert("Fix Link... \n\n" +
      obj.name + " value was ... \n   " + org + "\n\n\n" +
      "New value IS  ... \n   " + tmp);
    }
    org = tmp;  // any more stuff to fix?
    ary = tmp.split (fid1 + fid2);  // have a fake ID here?
    if (ary.length > 1) {    // still a fake
      tmp = ary.join (id1 + id2);
      obj.value = tmp;
alert("Fix ID... \n\n" +
      obj.name + " value was ... \n   " + org + "\n\n\n" +
      "New value IS  ... \n   " + tmp);
    }
  }
}

//<a href="mailto:ron853211@yahoo.com" onclick="FixMail (this);">email</a>.
function FixMail (obj1) {  // fix a hyperlink mail addr
var tmp;
  //tmp = obj1.href;
  //alert ("Fake E-mail address was ... \n   " + tmp + "\n\n\n" +
  obj1.href = "mailto:" + id1 + id2 + at1 + url + dot + typ;
  //       "Real E-mail address is  ... \n   " + obj1.href);
  return true;             // make it work...
}

function FixPaypalVerified(obj1)
{
  obj1.href = "https://www.paypal.com/us/verified/pal=" + id1 + id2 + "%40" + url + "%2e" + typ;
  return true;
}

// Fixes form action and selects user's state
function InitForm()
{
    InitFormAction();
    InitStateSelection();
}

// Set the correct form action here so users without javascript can't submit form
function InitFormAction()
{
  var form = window.document.orderForm;
  if (form)
  {
      form.action = "https://www.paypal.com/cgi-bin/webscr";
  }
}
/*
function InitStateSelection2()
{
  var selStateCode = getCookie("state");
  if (selStateCode != "")
  {
    //alert("selStateCode="+selStateCode);
    var form = window.document.orderForm;
    if (form != null)
    {
      var states = form.shp.options;
      for (state in states)
      {
        //alert(states[state].value);
        if (states[state].value == selStateCode)
        {
          form.shp.options[state].selected = "selected";
          break;
        }
      }
    }
  }
}
*/
// Sets state select to the state the user chose previously
function InitStateSelection()
{
  var shpSelects = document.getElementsByName("shp");
  if (shpSelects != null)
  {
    //alert("shpSelects.length="+shpSelects.length);
    for (var i = 0; i < shpSelects.length; i++)
    {
      InitStateSelect(shpSelects[i]);
    }
  }
}

// Sets options for state dropdown
function InitStateSelect(stateSelect)
{
  // Remove old items
  for (var i = stateSelect.options.length - 1; i >= 0; i--)
  {
     stateSelect.remove(i);
  }

  addOption(stateSelect, "--Choose Your State--", "");
  addOption(stateSelect, "---------------------", "");
  
  // Get default state selection
  var selStateCode = getCookie("state");
  //alert("selStateCode="+selStateCode);
  
  // Build list of states
  var states = getShipStates();
  for (i = 0; i < states.length; i += 2)
  {
    var optn = addOption(stateSelect, states[i+1], states[i]);
    if (states[i] == selStateCode)
    {
      //alert("selected option=" + selStateCode);
      optn.selected = "selected";
    }
  }
}

function addOption(selectbox, text, value)
{
  var optn = document.createElement("option");
  optn.text = text;
  optn.value = value;
  selectbox.options.add(optn);
  return optn;
}

function InitWineSelection()
{
  //alert("InitWineSelection");
  WineSelectionChanged(window.document.quickForm);

  InitStateSelection();
}

function WineSelectionChanged(form)
{
  if (form != null)
  {
    var selection = form.wine_name.options[form.wine_name.selectedIndex].value;
    //alert(selection + " selected");
    // Set the paypal fields
    form.item_name.value = selection;
    form.base_item_name.value = selection;

    // Change the amounts
    if (selection == "2006 Russian River Valley Pinot Noir.")
    {
      form.amount.options[0].value = "345.60";
      form.amount.options[0].text = "Full Case: $345.60 ea.";
      form.amount.options[1].value = "192.00";
      form.amount.options[1].text = "Half Case: $192.00 ea.";
    }
    else if (selection == "2006 Sonoma Coast Pinot Noir.")
    {
      // 2006 Sonoma Coast Pinot Noir
      form.amount.options[0].value = "324.00";
      form.amount.options[0].text = "Full Case: $324.00 ea.";
      form.amount.options[1].value = "180.00";
      form.amount.options[1].text = "Half Case: $180.00 ea.";
    }
    else if (selection == "2006 Knight's Valley Gamay Noir.")
    {
      // 2006 Knight's Valley Gamay Noir
      form.amount.options[0].value = "162.00";
      form.amount.options[0].text = "Full Case: $162.00 ea.";
      form.amount.options[1].value = "90.00";
      form.amount.options[1].text = "Half Case: $90.00 ea.";
    }
    else
    {
      alert("Invalid selection. Please contact us if you have a problem ordering wine.");
    }
  }
}

function setCookie(c_name, value, expiredays)
{
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
  if (document.cookie.length > 0)
  {
    c_start = document.cookie.indexOf(c_name + "=");
    if (c_start != -1)
    { 
      c_start = c_start + c_name.length+1; 
      c_end = document.cookie.indexOf(";",c_start);
      if (c_end == -1) c_end = document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}
