﻿//***************************************************************************
//*The following function takes a floating-point number n and convers it to a
//*dollar amount, by rounding to the nearest 100th (eg. 5.6785 -> $5.68)
function dollar_format(n) {
  dollars = Math.floor(n);
  cents = 100 * (n - dollars);
  cents = Math.round(cents);
  if(cents >= 100) {
    dollars += 1;
    cents -= 100;
  }
  if (cents < 10) {
    cents = "0" + cents;
  }
  amount = "\$" + dollars + "." + cents;
  return(amount);
}

//***************************************************************************
//*Checks if n is an integer greater or equal to 0
function validq(n) {
  answer = 1;

  if((n == parseInt(n)) && (n >= 0)) {
    answer = 0;
  }
  return(answer);
}

//***************************************************************************
function amount(a, p) { this.lbs = a; this.price = p }

//***************************************************************************
//*The following is a constructor function for the class grade
//*It initializes the name of the coffee grade, the available amounts in pounds, 
//*the prices for each amount, and the total available amount in pounds
function grade(name, code, amounts, qtyavail, prices) {
  this.name = name;
  this.code = code;
  this.qtyavail = new Array();  
  this.qtyavail = qtyavail;
  this.amount = new Array();
  for(j = 0; j < amounts.length; j++) {
    this.amount.push(new amount(amounts[j], prices[j]));
  }
}

//***************************************************************************
function total_weight() {
  total = 0.5;
  for(i=0; i<inventory.grade.length; i++) {
    j = document.orderform["amount" + i].selectedIndex;
    quantity = document.orderform["quantity" + i].value;
    total += quantity * inventory.grade[i].amount[j].lbs;
  }
  return(total);
}

//***************************************************************************
function update_shipping(method) {
  //alert(method);
  document.orderform.shipMethod.value = method;
}

//***************************************************************************
//*The following function updates the subtotal in the last column 
function update_subtotal() {
  subtotal = 0;
  for(i=0; i<inventory.grade.length; i++) {
    subtotal += parseFloat(document.orderform["price" + i].value.slice(1));
  }
  document.orderform.subtotal.value = dollar_format(subtotal);
}

//***************************************************************************
//*The following function updates the tax, shipping and total in the last column 
function update_total() {
  w = total_weight();
  shipping = 0;
  //alert(document.orderform.shipMethod.value);
  if (document.orderform.shipMethod.value == "PMS") {
    if(w > 0.5) {
      shipping = 4.80;
      if(w > 1) {
        shipping = 5.75;
        if(w > 2) {
          shipping = 8.55;
          if(w > 3) {
            shipping = 10.55;
            if(w > 4) {
              shipping = 13.45;
              if(w > 5) {
              shipping = 15.85;
              if(w > 6) {
                shipping += (w-6);
               }  
              }
            }
          }
        }
      }
    }
  }
  else if (document.orderform.shipMethod.value == "EMS") {
    if(w > 0.5) {
      shipping = 17.50;
      if(w > 2) {
        shipping = 17.50;
        if(w > 2.5) {
          shipping = 35.60;
          if(w > 4) {
            shipping = 39.60;
            if(w > 5) {
                shipping += (w-5);
            }
          }
        }
      }
    }
  }
  else if (document.orderform.shipMethod.value == "FLT") {
  //alert('flatrate');
    if(w > 0.5) {
      shipping = 4.95;
      if(w >2) {
        shipping = 4.95;
        if(w > 2.5) {
          shipping = 5.18;
          if(w > 7) {
            shipping = 6.98;
            if(w > 10) {
            shipping = 10.35;
            if(w > 16) {
             shipping += (w-16);
                         
            }
            }  
          }
        }
      }
    }
  }

  if (document.orderform.toHawaii.checked) {
    taxrate = 0.0416;
  }
  else {
    taxrate = 0.0;
  }

  document.orderform.shipping.value = dollar_format(shipping);
  subtotal = parseFloat(document.orderform.subtotal.value.slice(1));
  document.orderform.tax.value = dollar_format(parseFloat(taxrate) * subtotal);
  total =  subtotal + (taxrate * subtotal) + shipping;
  document.orderform.total.value = dollar_format(total);
}

//***************************************************************************
//*The following function updates the price for the ith grade in the last column 
//*of the orderform table as well as the total and subtotal in response to the user's selection
function update(i) {
  
  quant = document.orderform["quantity" + i].value;
  thisgrade = inventory.grade[i];
  // Determine the index of the amount selected for the ith grade
  j = document.orderform["amount" + i].selectedIndex;
  
  if (validq(quant) != 0) {
    
    if(quant != "") {
      alert("The quantity must be a whole numebr.");
    }
     
    document.orderform["quantity" + i].value = 0;
    document.orderform["quantity" + i].select();
    
    document.orderform["price" + i].value = dollar_format(
      thisgrade.amount[j].price * document.orderform["quantity" + i].value);
    update_subtotal();
    update_total();
    
    
    return;
  }
  
  // check if there is enough of this grade of coffee to fill the order
  if (parseFloat(document.orderform["quantity" + i].value) > thisgrade.qtyavail[j]) {


    alert("PLEASE NOTE:  It may take up to two weeks us to complete your roast of "+inventory.grade[i].name+".  Aikane Kona Coffee is roasted in small batches to assure freshness.");
    
    //********* CODE TO DISALLOW OVERDRAWING INVENTORY ********
    //document.orderform["quantity" + i].value = thisgrade.qtyavail[j];
    //document.orderform["price" + i].value = dollar_format(thisgrade.amount[j].price * document.orderform["quantity" + i].value);
    //update_subtotal();
    //update_total();
    //return;
    //*********************************************************
  }
     
  // update the price for the coffee grade modified by user
  document.orderform["price" + i].value = dollar_format(thisgrade.amount[j].price * document.orderform["quantity" + i].value);
   
  // now update the subtotal, tax, shipping and total
  update_subtotal();
  update_total();
}

//***************************************************************************
//                     CHECKOUT CODE HERE
//***************************************************************************
//*Generates a string listing the purchased items
function item_description() {
  descr = "Aikane Kona Coffee: ";
  for(i=0; i<inventory.grade.length; i++) {
    if( document.orderform["quantity" + i].value != "0" ) {
      j = document.orderform["amount" + i].selectedIndex; 
      descr += document.orderform["quantity" + i].value + " " + inventory.grade[i].amount[j].lbs +
        "lb bag(s) of " + inventory.grade[i].name + ";  ";
    }
  }
  return(descr);
}

//***************************************************************************
//*Generates a string encoding the order for invoice
function order_code() {
  descr = "";
  for(i=0; i<inventory.grade.length; i++) {
    if( document.orderform["quantity" + i].value != "0" ) {
      j = document.orderform["amount" + i].selectedIndex; 
      descr += inventory.grade[i].amount[j].lbs + inventory.grade[i].code + ":" + 
	document.orderform["quantity" + i].value + ":" + inventory.grade[i].amount[j].price + "&";
    }
  }
  descr += document.orderform.shipMethod.value+":1:" + document.orderform.shipping.value.slice(1) + "&";
  descr += "TAX:" + document.orderform.toHawaii.checked + ":" + document.orderform.tax.value.slice(1) + "&";
  return(descr);
}

//***************************************************************************
function order_data() {
  d = new Date();
  return_data = document.orderform.shipMethod.value + "|";
  for(i=0; i<inventory.grade.length; i++) {
    if( document.orderform["quantity" + i].value != "0" ) {
      j = document.orderform["amount" + i].selectedIndex; 
      return_data += inventory.grade[i].name + ":" + document.orderform["quantity" + i].value + ":" 
        + inventory.grade[i].amount[j].lbs + "+";
    }
    i++;
  }

  return(return_data);
}

//***************************************************************************
//get_address() {
//
//  address = "Address";
//
//  return(address);
//
//}

//***************************************************************************
//*Updates the information in the PayPal submit button
function update_submit() {

  if( parseFloat(document.orderform.total.value.slice(1)) == 0) {
   alert("Please select the quantity of coffee you want before checking out.");
  }
  else {

    if(document.orderform.billship.checked) {
      document.payflow.name.value = document.orderform.name.value;
      document.payflow.address.value = document.orderform.street.value;
      document.payflow.city.value = document.orderform.city.value;
      document.payflow.state.value = document.orderform.state.value;
      document.payflow.zip.value = document.orderform.zip.value;
      document.payflow.country.value = document.orderform.country.value;
    }

    document.payflow.nametoship.value = document.orderform.name.value;
    document.payflow.addresstoship.value = document.orderform.street.value;
    document.payflow.citytoship.value = document.orderform.city.value;
    document.payflow.statetoship.value = document.orderform.state.value;
    document.payflow.ziptoship.value = document.orderform.zip.value;
    document.payflow.countrytoship.value = document.orderform.country.value;

    document.payflow.description.value = item_description();
    document.payflow.amount.value = document.orderform.total.value.slice(1);

    d = new Date();
    document.payflow.user1.value = d.toString();
    document.payflow.user2.value = document.orderform.shipMethod.value;

    document.payflow.user3.value = document.orderform.source.value;
    document.payflow.user4.value = order_code();
    //alert(document.payflow.user4.value);

    document.payflow.submit();
  }
}

//***************************************************************************
function drawPriceSheet() {
  document.writeln('    <table width="676">');
  document.writeln('      <tr>');
  document.writeln('        <th width="113" height="16" nowrap style="border-left-width: 1; border-right-width: 1; border-top-width: 1; border-bottom-style: solid; border-bottom-width: 1"><font face="Arial">Product</font></th>');
  document.writeln('        <th width="299" height="16" nowrap style="border-left-width: 1; border-right-width: 1; border-top-width: 1; border-bottom-style: solid; border-bottom-width: 1"><font face="Arial">Description</font></th>');
  document.writeln('        <th width="103" height="16" nowrap style="border-left-width: 1; border-right-width: 1; border-top-width: 1; border-bottom-style: solid; border-bottom-width: 1"><font face="Arial">Amount</font></th>');
  document.writeln('        <th width="91" height="16" nowrap style="border-left-width: 1; border-right-width: 1; border-top-width: 1; border-bottom-style: solid; border-bottom-width: 1"><font face="Arial">Quantity</font></th>');
  document.writeln('        <th width="64" height="16" nowrap style="border-left-width: 1; border-right-width: 1; border-top-width: 1; border-bottom-style: solid; border-bottom-width: 1"><font face="Arial">Price</font></th>');
  document.writeln('      </tr>');
  for(i=0; i<D.list.length; i++) {
    document.writeln('    <tr>');
    document.writeln('      <td width="113" height="30" align="center"><font face="Arial" size="2"><i><a href="../grades.htm">'+D.list[i].NAME+'</a></i></font></td>');
    document.writeln('      <td width="299" height="30" align="center"><font face="Arial" size="2">'+D.list[i].DESCRIPTION+'</font></td>');
    document.writeln('      <td width="103" height="30" align="center"><font face="Arial">');
    document.writeln('      <select size="1" name="amount'+i+'" onChange="update('+i+');">');
    for(j=0; j<D.list[i].AMOUNT.length; j++) {
      document.writeln('      <option value="'+j+'">'+D.list[i].AMOUNT[j]+'lb. (\$'+D.list[i].PRICE[j]+')</option>');
    }
    document.writeln('      </select></font></td>');
    document.writeln('      <td width="91" height="30" align="center"><font face="Arial">');
    document.writeln('        <input type="text" name="quantity'+i+'" size="3" value="0" onChange="update('+i+');"></font></td>');
    document.writeln('      <td width="64" height="30" align="center"><font face="Arial">');
    document.writeln('        <input type="text" readonly name="price'+i+'" size="8" value="\$0.00"></font></td>');
    document.writeln('    </tr>');
  }
  document.writeln('    </table>');
}