
// define an array to hold product information
// data has the form sku:price:name

var stateList = new Array();

stateList['AZ'] = new Array("26", "38", "50", "76");
stateList['CA'] = new Array("0", "0", "0", "0", "19", "31");
stateList['CO'] = new Array("26", "38", "50", "76");
stateList['CT'] = new Array("34", "50", "61", "96");
stateList['DC'] = new Array("34", "50", "61", "96");
stateList['DE'] = new Array("34", "50", "61", "96");
stateList['FL'] = new Array("34", "50", "61", "96");
stateList['GA'] = new Array("34", "50", "61", "96");
stateList['IA'] = new Array("31", "47", "59", "94");
stateList['ID'] = new Array("26", "38", "50", "76");
stateList['IL'] = new Array("34", "50", "61", "96");
stateList['IN'] = new Array("34", "50", "61", "96");
stateList['KS'] = new Array("31", "47", "59", "94");
stateList['KY'] = new Array("34", "50", "61", "96");
stateList['LA'] = new Array("34", "50", "61", "96");
stateList['ME'] = new Array("34", "50", "61", "96");
stateList['MI'] = new Array("34", "50", "61", "96");
stateList['MN'] = new Array("31", "47", "59", "94");
stateList['MO'] = new Array("31", "47", "59", "94");
stateList['MT'] = new Array("26", "38", "50", "76");
stateList['NC'] = new Array("34", "50", "61", "96");
stateList['ND'] = new Array("31", "47", "59", "94");
stateList['NE'] = new Array("31", "47", "59", "94");
stateList['NH'] = new Array("34", "50", "61", "96");
stateList['NJ'] = new Array("34", "50", "61", "96");
stateList['NM'] = new Array("27", "39", "56", "89");
stateList['NV'] = new Array("25", "38", "38", "60");
stateList['NY'] = new Array("34", "50", "61", "96");
stateList['OH'] = new Array("34", "50", "61", "96");
stateList['OK'] = new Array("31", "47", "59", "94");
stateList['OR'] = new Array("25", "38", "38", "60");
stateList['RI'] = new Array("34", "50", "61", "96");
stateList['SC'] = new Array("34", "50", "61", "96");
stateList['SD'] = new Array("31", "47", "59", "94");
stateList['TN'] = new Array("34", "50", "61", "96");
stateList['TX'] = new Array("34", "50", "61", "96");
stateList['VA'] = new Array("34", "50", "61", "96");
stateList['VT'] = new Array("34", "50", "61", "96");
stateList['WA'] = new Array("26", "38", "50", "76");
stateList['WI'] = new Array("34", "50", "61", "96");
stateList['WV'] = new Array("34", "50", "61", "96");
stateList['WY'] = new Array("26", "38", "50", "76");


function checkData2(sku,data) {
   if(data.length > 0) {
      var targetName = sku + "_total";
      if (isNaN(data)) {
         alert("Quantity field can only accept numbers.");
         document.forms.orderform[sku].value = "";
         document.forms.orderform[targetName].value = "";
      } else  {
         SetChip(sku,data);
         if (data == "0") {
            document.forms.orderform[sku].value='';
            document.forms.orderform[targetName].value='';
         } else {
            var newValue = (parseFloat(data) * parseFloat(bottles[sku][0]));
            if (document.forms.orderform.wineclub.checked && (sku != 'TROIKA')) {
               newValue = newValue * 0.85;
            }
            newValue = newValue.toFixed(2) 
            document.forms.orderform[targetName].value = newValue;
         }
         for (k in Chips) {
            if ((k != '') && (Chips[k] != '') && (Chips[k] != "0")) {
               document.forms.orderform[k].value=Chips[k];
            }
         }
         updateTotal();
      }
   }
}

function updateItems() {
   bottleCount = GetChips();
   for (k in Chips) {
     if (k != '') {  document.forms.orderform[k].value=Chips[k]; }
   }
   for (j in bottles) { if (document.forms.orderform[j].value != '') { checkData2(j,document.forms.orderform[j].value) } }
   for ( i in document.forms.orderform.state.options ) {
      if (document.forms.orderform.state.options[i] == Chips['state']) {
         document.forms.orderform.state.options[i].selected = true; break;
      }
   }
   for ( i in document.forms.orderform.ship_method.options ) {
      if (document.forms.orderform.ship_method.options[i] == Chips['ship_method']) {
         document.forms.orderform.ship_method.options[i].selected = true; break;
      }
   }
}


function updateTotal() {
   GetChips();
   var subTotal = 0.00;
   for (key in bottles) {
      var targetName = key + "_total";
      if (parseFloat(document.forms.orderform[targetName].value)) {
         subTotal += parseFloat(document.forms.orderform[targetName].value);
      }
   }
   subTotal = subTotal.toFixed(2);
   document.forms.orderform.subtotal.value = subTotal;
   
   if (document.forms.orderform.state.value != "") {
      document.forms.orderform.state[0].disabled=true;
      if (document.forms.orderform.state.value == "CA") {
         var theTax= parseFloat(document.forms.orderform.subtotal.value) * (0.09);
         theTax = theTax.toFixed(2)
         document.forms.orderform.ca_tax.value = theTax;
         document.forms.orderform.ship_method[3].disabled = false;
         document.forms.orderform.ship_method[3].selected = true;
         document.forms.orderform.ship_method[1].disabled = true;
         document.forms.orderform.ship_method[2].disabled = true;
      } else {
         document.forms.orderform.ca_tax.value = "0.00";
         if (Chips['state'] == 'CA') {
            document.forms.orderform.ship_method[0].disabled = false;
            document.forms.orderform.ship_method[3].disabled = true;
            document.forms.orderform.ship_method[0].selected = true;
            document.forms.orderform.ship_method[1].disabled = false;
            document.forms.orderform.ship_method[2].disabled = false;
            document.forms.orderform.shipping.value="";
            SetChip('ship_method','0');
         }
      }
      SetChip('state', document.forms.orderform.state.value);
      
      
   }
   if (document.forms.orderform.ship_method.value != "") {
      document.forms.orderform.ship_method[0].disabled=true;
      SetChip('ship_method', document.forms.orderform.ship_method.value);
   }
      
   if ((document.forms.orderform.ship_method.value != "") && (document.forms.orderform.state.value != "")) {
      calculateShipping(document.forms.orderform.state.value);
   }
   var theTotal = 0.00;
   if (parseFloat(document.forms.orderform.subtotal.value)) { theTotal += parseFloat(document.forms.orderform.subtotal.value); }
   if (parseFloat(document.forms.orderform.ca_tax.value)) { theTotal += parseFloat(document.forms.orderform.ca_tax.value); }
   if (parseFloat(document.forms.orderform.shipping.value)) { theTotal += parseFloat(document.forms.orderform.shipping.value); }
   theTotal = theTotal.toFixed(2);
   document.forms.orderform.total.value = (theTotal);
}

function calculateShipping(state) {
   document.forms.orderform.ship_method[0].disabled=true;
   if (document.forms.orderform.state.value != "") {
      if ((document.forms.orderform.state.value == "CA") 
      && ((document.forms.orderform.ship_method[1].selected==true)
      ||  (document.forms.orderform.ship_method[2].selected==true))) {
         document.forms.orderform.ship_method[3].disabled = false;
         document.forms.orderform.ship_method[3].selected=true;
         document.forms.orderform.ship_method[1].disabled = true;
         document.forms.orderform.ship_method[2].disabled = true;
         alert("Only GSO Overnight shipping is available in CA.");
      }
      var bottleCount = 0;
      var shippingCharge = 0;
      for (key in bottles) {
         if (parseInt(document.forms.orderform[key].value)) {
            bottleCount += parseInt(document.forms.orderform[key].value);
         }
      }
      var cases = Math.floor(bottleCount/12.00);
      if ((bottleCount - (12 * cases) ) > 6) {
        cases ++;
      } else if ((bottleCount - (12 * cases)) > 0) {
        if (document.forms.orderform.ship_method[1].selected==true) {
          shippingCharge += parseFloat(stateList[document.forms.orderform.state.value][0])
        } else if (document.forms.orderform.ship_method[2].selected==true) {
          shippingCharge += parseFloat(stateList[document.forms.orderform.state.value][2])
        } else {
          shippingCharge += parseFloat(stateList[document.forms.orderform.state.value][4])
        }
      }
      if (document.forms.orderform.ship_method[1].selected==true) {
        shippingCharge += (cases * parseFloat(stateList[document.forms.orderform.state.value][1]))
      } else if (document.forms.orderform.ship_method[2].selected==true) {
        shippingCharge += (cases * parseFloat(stateList[document.forms.orderform.state.value][3]))
      } else {
        shippingCharge += (cases * parseFloat(stateList[document.forms.orderform.state.value][5]))
      }
      shippingCharge = shippingCharge.toFixed(2);
      document.forms.orderform.shipping.value = shippingCharge;
   }
}

function continueButton() {
   updateTotal();
   if (parseFloat(document.forms.orderform.subtotal.value)) {
      if (document.forms.orderform.state.value == "") {
         alert("Please select the US state you are shipping to.")
      } else {
         if (document.forms.orderform.ship_method.value == "") {
            alert("Please select your preferred shipping method.")
         } else {
            calculateShipping(document.forms.orderform.state.value);
            displaySummary();
         }
      }
   } else {
      alert("Please select at least one bottle.")
   }
}

function displaySummary() {

   var bottleCount = 0;
   var shippingCharge = 0;
   for (k in Chips) {
      if ((Chips[k] != '') && (k != 'state') && (k != 'ship_method') && (k != '')) {
         bottleCount += parseInt(Chips[k]);
      }
   }
   var summaryCode='\
<p class="head">\
<b>Order Summary</b>\
</p><p>\
<form name="summaryform">\
<table border=0 align=center width="90%" cellpadding=2 cellspacing=0>';
   for (key in bottles) {
      var targetName = key + "_total";
      if (document.forms.orderform[key].value != "") {
         var bottleText = "";
         if (document.forms.orderform[key].value == "1") { bottleText="bottle" }
         else { bottleText="bottles"; }
         summaryCode += '<tr>' +
         '<td><p class="smallertext">' + bottles[key][1] + '</p></td>' +
         '<td><p class="smallertext">(' + key + ')</p></td>' +
         '<td><p class="smallertext" width="90">' + document.forms.orderform[key].value + ' ' + bottleText + ' @ $' + bottles[key][0]
         if (document.forms.orderform.wineclub.checked && (key != 'TROIKA')) { summaryCode += ' <font size="-2">(less 15% wine club discount)</font>'; }
         summaryCode += '</p></td>' +
         '<td align=right width="50"><p class="smallertext">$' + document.forms.orderform[targetName].value + '</p></td></tr>';
      }
   }
   summaryCode += '<tr><td></td><td></td><td align="right"><p class="smallertext">Subtotal</td><td align=right><p class="smallertext">$' +
   document.forms.orderform.subtotal.value + '</p></td>';
   summaryCode += '<tr><td></td><td></td><td align="right"><p class="smallertext">Tax</td><td align=right><p class="smallertext">$' +
   document.forms.orderform.ca_tax.value + '</p></td>';
   summaryCode += '<tr><td>' + '\
   <form name="dummy"><input type="button" value="<- Back to Order Form" onClick="displayContent()" \
style="height: 18px; margin: 0; padding: 1px 2px; border: 1px solid #7C4469; outline: 1px solid #7C4469; font-size: 9pt;">\
</form></td><td></td><td align="right"><p class="smallertext">Shipping ' + bottleCount + ' bottle';
   if (bottleCount > 1) { summaryCode += 's' }
   summaryCode += ' to ' + document.forms.orderform.state.value + ' by '; 
   if (Chips['ship_method'] == document.forms.orderform.ship_method[1].value) { summaryCode += 'Ground'; }
   else if (Chips['ship_method'] == document.forms.orderform.ship_method[2].value) { summaryCode += '2nd Day Air'; }
   else if (Chips['ship_method'] == document.forms.orderform.ship_method[3].value) { summaryCode += 'GSO Overnight'; }
   summaryCode += '</p></td><td align=right><p class="smallertext">$' +
   document.forms.orderform.shipping.value + '</p></td>';
   summaryCode += '<tr><td></td><td></td><td align="right"><p class="smallertext">Total</td><td align=right><p class="smallertext">$' +
   document.forms.orderform.total.value + '</p></td>';
   summaryCode +='\
</table>\
<p class="subhead">Payment Options</p>\
<p class="smallertext"><input type="radio" name="payment" value="paypal"> Order Online (coming soon)</p> \
<p class="smallertext"><input type="radio" name="payment" value="credit card"> Credit Card orders \
may be FAXed or called in to our office at (707) 887-2703.</p>\
<p class="smallertext"><input type="radio" name="payment" value="check"> Check - \
Please send your check with a printout of this Order Summary to the address listed below.</p>\
<p>&nbsp;<br />\
<table border=0 align=center width=90% cellpadding=2 cellspacing=0>\
  <tr>\
    <td valign=top align=right><p class="smallertext">Name:\
    </td>\
    <td valign="top"><input name="wineclub_name" type="text" size="30" value="">\
    </td>\
    <td valign=top align=right><p class="smallertext">Telephone:\
    </td>\
    <td valign="top"><input name="wineclub_name" type="text" size="30" value="">\
    </td>\
  </tr>\
  <tr>\
    <td valign=top align=right><p class="smallertext">Billing address:\
    </td>\
    <td valign="top"><input name="wineclub_name" type="text" size="30" value="">\
    </td>\
    <td valign=top align=right><p class="smallertext">Shipping address:\
    </td>\
    <td valign="top"><input name="wineclub_name" type="text" size="30" value="">\
    </td>\
  </tr>\
  <tr>\
    <td valign=top align=right><p class="smallertext">&nbsp;\
    </td>\
    <td valign="top"><input name="wineclub_name" type="text" size="30" value="">\
    </td>\
    <td valign=top align=right><p class="smallertext">&nbsp;\
    </td>\
    <td valign="top"><input name="wineclub_name" type="text" size="30" value="">\
    </td>\
  </tr>\
  <tr>\
    <td valign=top align=right><p class="smallertext">Email:</p>\
    </td>\
    <td valign="top"><input name="wineclub_name" type="text" size="30" value="">\
    </td>\
  </tr>\
  <tr>\
    <td valign=top align=right><p class="smallertext">Credit Card Number:<br />&nbsp;</p>\
    </td>\
    <td valign=top align=left colspan=3><p class="smallertext"><input name="wineclub_name" type="text" size="20" value="">\
&nbsp; &nbsp; Exp Date: <input name="wineclub_name" type="text" size="12" value="">\
&nbsp; &nbsp; Security code: <input name="wineclub_name" type="text" size="6" value=""></p>\
    </td>\
  </tr>\
  <tr>\
    <td valign=top align=right><p class="smallertext">Signature: \
    </td>\
    <td valign=top align=left colspan=3><p class="smallertext"><img src="images/black.gif" height=1 width=200>\
    &nbsp; &nbsp; Date: <img src="images/black.gif" height=1 width=80><br />&nbsp;\
    </td>\
  </tr>\
  <tr>\
    <td valign="top">\
      <p class="smallertext" align="right">Mailing address:</p>\
</td><td valign="top"><p class="smallertext">\
Woodenhead Vintners<br />\
PO Box 1448<br />\
Forestville, CA  95436\
</td></tr></table>\
</p><p class="smallertext">\
If you have questions, call Zina at (707) 887-2703 or e-mail <a href="mailto:zina@woodenheadwine.com">zina@woodenheadwine.com</a>.\
\
<p><form name="dummy"><input type="button" value="<- Back to Order Form" onClick="displayContent()" \
style="height: 18px; margin: 0; padding: 1px 2px; border: 1px solid #7C4469; outline: 1px solid #7C4469; font-size: 9pt;">\
</form>';

   document.getElementById('summary').innerHTML = summaryCode;
   document.getElementById('content').style.visibility='hidden'; document.getElementById('content').style.display='none'; 
   document.getElementById('summary').style.visibility="visible"; document.getElementById('summary').style.display="block"; 

}

function displayContent() {
   document.getElementById('summary').style.visibility='hidden'; document.getElementById('summary').style.display='none'; 
   document.getElementById('content').style.visibility="visible"; document.getElementById('content').style.display="block"; 
}

function clearCart2() {
   for (j in Chips) { SetChip(j,"0"); }
   DeleteCookie('cart'); 
   updateCart();
   for (i in bottles) {
      if (i != '') {
          var targetName = i + "_total";
          document.forms.orderform[i].value='';
          document.forms.orderform[targetName].value='';
      }
   }
   document.forms.orderform.subtotal.value='';
   document.forms.orderform.ca_tax.value='';
   document.forms.orderform.shipping.value='';
   document.forms.orderform.total.value='';
   document.forms.orderform.ship_method[0].disabled=false;
   document.forms.orderform.ship_method[0].selected=true;
   document.forms.orderform.state[0].disabled=false;
   document.forms.orderform.state[0].selected=true;
}



