// Shopping Cart/Order/RFQ Update, Delete, Insert and Status Change Functions
// Copyright (c) 2007 by The Write Edge, LTD
// http://www.WriteEdge.com

function Reject() {  
   document.forms.cart.intAction.value = 2;
   document.forms.cart.ST.value = 12;
   document.forms.cart.submit();
}
function Discard() {  
   document.forms.cart.intAction.value = 2;
   document.forms.cart.ST.value = 13;
   document.forms.cart.submit();
}
function RejectOrder() {  
   document.forms.cart.intAction.value = 2;
   document.forms.cart.TP.value = 4;
   document.forms.cart.ST.value = 12;
   document.forms.cart.submit();
}
function GetShippingForEstimate() {  
   document.forms.cart.intAction.value = 3;
   document.forms.cart.TP.value = 1;
   document.forms.cart.ST.value = 2;
   document.forms.cart.OTSID.value = 2;
   document.forms.cart.txtFocus.value = 'SMD';
   document.forms.cart.submit();
}
function ChangeEstimatesProducts() {  
   document.forms.cart.intAction.value = 3;
   document.forms.cart.TP.value = 1;
   document.forms.cart.ST.value = 1;
   document.forms.cart.OTSID.value = 1;
   document.forms.cart.txtFocus.value = 'SMD';
   document.forms.cart.submit();
}
function ChangeOrderProducts() {  
   document.forms.cart.intAction.value = 3;
   document.forms.cart.TP.value = 3;
   document.forms.cart.ST.value = 1;
   document.forms.cart.OTSID.value = 6;
   document.forms.cart.txtFocus.value = 'SMD';
   document.forms.cart.submit();
}
function GetShippingForOrder() {  
   document.forms.cart.intAction.value = 3;
   document.forms.cart.TP.value = 3;
   document.forms.cart.ST.value = 2;
   document.forms.cart.OTSID.value = 7;
   document.forms.cart.txtFocus.value = 'SMD';
   document.forms.cart.submit();
}
function RoundNumber(value, places) {
	var result = Math.round(value*Math.pow(10,places))/Math.pow(10,places);
	return result;
}
function ChangeShippingMethod() {  
   var availableShipDate = document.forms.cart.ASD;
   if (availableShipDate.length < 1) {
      availableShipDate = new Date();   
   }
   var shipMethod = document.forms.cart.SMD;
   var numberOfItems = document.forms.cart.NOI;
   var orderDiscount = document.forms.cart.OD;
   var subTotal = document.forms.cart.OSTC;
   var state = document.forms.cart.TRS;
   var country = document.forms.cart.STCN;   
   var taxRate = document.forms.cart.TR;
   var curTax = 0
   var daysToShip = 5
   var firstItem = 5.99
   var additionalItems = 3.99
   var boxCharge = 1
   var boxChargeMaxUnits = 5
   var shipDest = document.forms.cart.SMD;
   if (country != "USA") {
      if (shipMethod == "EXP" || shipMethod == "NEXT") {
         alert ("Expedited shipping is available only within the United States")
      }
      if (country == "CAN" || country == "Canada" || country == "MEX" || country == "Mexico") {
        shipMethod == "IMC"
      }
      else {
        shipMethod == "INTL"
      }
      document.forms.cart.SMD.selectedIndex = shipMethod;      
   }      
   if (shipMethod == "EXP") {
      firstItem = 10.99;
      additionalItems = 8.99;
      daysToShip = 3;
   }
   else if (shipMethod == "NEXT") {
      firstItem = 15.99;
      additionalItems = 13.99;
      daysToShip = 1;
   }
   else if (shipMethod == "IMC") {
      firstItem = 17.99;
      additionalItems = 15.99;
      daysToShip = 7;
   }
   else if (shipMethod == "INTL") {
      firstItem = 17.99;
      additionalItems = 15.99;
      daysToShip = 10;
   }
   var shipCharge = curFirstItem;
   if (numberOfItems > 1) {
      shipCharge = shipCharge + (additionalItems * (numberOfItems - 1));
   }
   shipCharge = RoundNumber(shipCharge, 2);
   var shipDate = availableShipDate
   if (shipDate.getDay() == 5) {
      shipDate = shipDate + 3;
   }
   else if (shipDate.getDay() == 6) {
      shipDate = shipDate + 2; 
   }
   else {
      shipDate = shipDate + 1;
   }
   var month = shipDate.getMonth() + 1;
   var day = shipDate.getDate();
   var year = shipDate.getFullYear();
   var deliverDate = month + "/" + day + "/" + year;
   deliverDate = shipDate + daysToShip    ;  
   var month = deliverDate.getMonth() + 1;
   var day = deliverDate.getDate();
   var year = deliverDate.getFullYear();
   deliverDate = month + "/" + day + "/" + year;
   var subTotal = document.forms.cart.subTotal
   if (subTotal > 100 && subTotal < 800) {
      var insurance = (subTotal / 100) * 1.9;
   }
   else if (subTotal > 799.99) {
      var insurance = (subTotal / 100) * .99;  
   }
   else {
      var insurance = 0;
   }
   if (numberOfItems > boxChargeMaxUnits) { 
      var totalBoxCharge =  RoundNumber((numberOfItems / boxChargeMaxUnits)* boxCharge, 0);
   }
   else {
      var totalBoxCharge = boxCharge;
   }
   var totalShippingCost = RoundNumber(shipCharge + totalBoxCharge + insurance,2);
   if (state == "VA" || state == "Virginia") {
      curTax = RoundNumber(taxRate * subTotal,2);
   }
   orderTotal = RoundNumber(subTotal + orderDiscount + curTax + totalShippingCost,2);         
   document.forms.cart.OTC.value = orderTotal;
   document.forms.cart.TBC.value = totalBoxCharge;
   document.forms.cart.THC.value = insurance;
   document.forms.cart.SHC.value = shipCharge;
   document.forms.cart.TSC.value = totalShipCost;
   document.forms.cart.SHD.value = shipDate;
   document.forms.cart.DSD.value = shipDate;
   document.forms.cart.ASD.value = availableShipDate;
   document.forms.cart.EDD.value = deliverDate;
   document.forms.cart.intAction.value = 2;
   document.forms.cart.TP.value = 3;
   document.forms.cart.ST.value = 2;
   document.forms.cart.OTSID.value = 7;
   document.forms.cart.txtFocus.value = 'SMD';
   document.forms.cart.submit();
}
function CalculateShipping() {  
   document.forms.cart.intAction.value = 3;
   document.forms.cart.TP.value = 4;
   document.forms.cart.ST.value = 1;
   document.forms.cart.OTSID.value = 8;
   document.forms.cart.txtFocus.value = 'SMD';
   document.forms.cart.submit();
}
function ChangePaymentMethod() {  
   document.forms.cart.intAction.value = 2;
   document.forms.cart.TP.value = 4;
   document.forms.cart.ST.value = 2;
   document.forms.cart.OTSID.value = 9;
   document.forms.cart.txtFocus.value = 'PMID';
   document.forms.cart.submit();
}
function SubmitOrder() {  
   document.forms.cart.intAction.value = 3;
   document.forms.cart.TP.value = 4;
   document.forms.cart.ST.value = 3;
   document.forms.cart.OTSID.value = 10;   
   document.forms.cart.txtFocus.value = 'PMID';
   document.forms.cart.submit();
}
function GetCustomer()  {  
   var strURL = "/pop_ups/get_member.asp"
   var intCustomer = showModalDialog(strURL,"","dialogWidth: 500px; dialogHeight: 500px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;");
   if(intCustomer != null) {
     document.forms.cart.MID.value = intCustomer;
     document.forms.cart.GUID.value = intCustomer;
     document.forms.cart.intAction.value = 2.1
     document.forms.cart.submit();
   }
}  
function AddNewPart() {
   document.forms.cart.intAction.value = 2.2;
   document.forms.cart.submit();
}
function DeleteItem(strFieldName,intItemID) {  
   objField = eval("document.cart." + strFieldName);
   objField.value = 0;
   document.forms.cart.zap.value = intItemID;
   document.forms.cart.intAction.value = 2.3;
   document.forms.cart.submit();
}
function ConfirmCost() {  
   document.forms.cart.intAction.value = 2.7;
   document.forms.cart.txtFocus.value = "Totals";
   document.forms.cart.submit();
}
function Backorder() {  
   document.forms.cart.intAction.value = 3;
   document.forms.cart.TP.value = 4;
   document.forms.cart.ST.value = 9;
   document.forms.cart.submit();
}
function BackorderItems() {  
   document.forms.cart.intAction.value = 3;
   document.forms.cart.TP.value = 4;
   document.forms.cart.ST.value = 8;
   document.forms.cart.submit();
}
function PartialBackorder() {  
   document.forms.cart.intAction.value = 3;
   document.forms.cart.TP.value = 4;
   document.forms.cart.ST.value = 8;
   document.forms.cart.submit();
}
function OrderProblems() {  
   document.forms.cart.intAction.value = 2.95;
   document.forms.cart.TP.value = 3;
   document.forms.cart.ST.value = 1;
   document.forms.cart.submit();
}
function UpdateCart(thisname,intAction,intOrderType)  {  
   document.forms.cart.intAction.value = intAction;
   document.forms.cart.txtFocus.value = "Totals";
   if (intAction == 2.4 && intOrderType > 2) {
      document.forms.cart.TP.value = 4;
      document.forms.cart.ST.value = 1;
     }
   else if (intAction == 2.4) {
      document.forms.cart.ST.value = 2;
     }
   else if (intAction == 2.6 || intAction == 2.5) {
      document.forms.cart.TP.value = 4;
      document.forms.cart.ST.value = 2;
     }
   document.forms.cart.submit();
}  
function GetCalendar(strFieldName) {
   var objField = eval("document.all." + strFieldName);
   var datInitialDate = objField.value; 
   datInitialDate = datInitialDate.replace(/\//g,'-'); 
   var datReturnDate = showModalDialog("/members/getcalendar.asp",datInitialDate,"status:false; dialogWidth:225px; dialogHeight:225px,screenY=400,top=400,left=400");
   objField.value = datReturnDate;
   objField = eval("document.all." + strFieldName);
   objField.focus();
}  
function add2cart(intItem,intType,strPart,curPrice,curPromotion,intQuantity,intPromotionUnits,intNIV,bReorder,intDays,intWeight,intAvailableShipDays,curBox,intMaxPer,bInsure,intCalculation) {
   var objField = eval("document.forms.cart.OrderType");
   objField.value = intType
   var strFieldName = "Quantity";
   objField = eval("document.cart." + strFieldName);
   objField.Value = intQuantity
   if (curPromotion != 0 && intPromotionUnits > intQuantity) {
      alert("The promotion price applies for purchases over " + intPromotionUnits-1 + " units. To get the discount, please change the quantity of units in your shopping cart"); 
   }      
   if (intQuantity == 0 || intQuantity == undefined) {
      alert("Please use the shopping cart link on your left to remove items from the cart."); 
   } 
   else {
      strFieldName = "Quantity";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = intQuantity;
      var strFieldName = "AvailableShipDays";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = intAvailableShipDays;
      strFieldName = "Item";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = intItem;
      strFieldName = "Product";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = strPart;
      strFieldName = "Price";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = curPrice         
      strFieldName = "SpecialPrice";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = curPromotion;         
      strFieldName = "Units";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = intPromotionUnits;         
      strFieldName = "Weight";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = intWeight;         
      strFieldName = "NIV";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = intNIV;         
      strFieldName = "Reorder";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = bReorder;         
      strFieldName = "Days";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = intDays;         
      strFieldName = "Box";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = curBox;         
      strFieldName = "MaxBox";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = intMaxPer;         
      strFieldName = "Insure";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = bInsure;         
      strFieldName = "Calculate";
      objField = eval("document.forms.cart." + strFieldName);
      objField.value = intCalculation;         
      document.forms.cart.submit();  
    }  
}