﻿// Function to round numbers - Arguments: number to round, number of decimal places
function roundNumber(rnum, rlength) {
  var num = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return num;
}

// Function that updates the shown price
function updatePrice(itemID) {
   var basePriceID = "basePrice" + itemID;
   var sizeID = "size" + itemID;
   var itemLengthID = "itemLength" + itemID;
   var itemWidthID = "itemWidth" + itemID;
   var linenID = "linen" + itemID;

   var updatedPrice = parseFloat(document.getElementById(basePriceID).value);
   
   // Updates the price based off of the linen type selected   
   if(document.getElementById(linenID)!=null) {
      updatedPrice = updatedPrice + parseFloat(document.getElementById(linenID).value);
      updatedPrice = roundNumber(updatedPrice, 2);
   }
   
   // Updates the price based off of the inputed dimensions or selected dimensions
   if(document.getElementById(itemLengthID)!=null) {
      var itemLength = parseFloat(document.getElementById(itemLengthID).value);
      var itemMaxSize = null;
      var itemObject = new Array();
      itemObject = document.getElementById(itemLengthID).name.split(":");
      
      for(var i=0; itemObject[i]; i++) {
         var itemObjectInfo = new Array();
         itemObjectInfo = itemObject[i].split(",");
         if(itemLength >= itemObjectInfo[0] && itemLength <= itemObjectInfo[1]) {
            updatedPrice = updatedPrice + parseFloat(itemObjectInfo[2]);
            updatedPrice = roundNumber(updatedPrice, 2);
            itemMaxSize = null;
            break;
         }
         itemMaxSize = itemObjectInfo[1];
      }
      if(itemMaxSize!=null) {
         alert("The max size of the linen is " + itemMaxSize + " inches");
         document.getElementById(itemLengthID).value = 0;
      }
   }
   else if(document.getElementById(sizeID)!=null)
      updatedPrice = updatedPrice + parseFloat(document.getElementById(sizeID).value);
   
   // Updates the price based off of the inputed width
   if(document.getElementById(itemWidthID)!=null) {
      var itemWidth = document.getElementById(itemWidthID).value;
      var itemWidthArray = new Array();
      var itemWidthArray = document.getElementById(itemWidthID).name.split(",");

      if(itemWidth>parseFloat(itemWidthArray[0])) {
        alert("For any width over " + itemWidthArray[0] + " inches, there is an additional charge");
        updatedPrice = (1 + parseFloat(itemWidthArray[1])) * updatedPrice;
        updatedPrice = roundNumber(updatedPrice, 2);
      }
   }

   // Changes the HTML on the page to show the updated price
   if(parseFloat(document.getElementById(itemID).innerHTML) != updatedPrice) {
      document.getElementById(itemID).innerHTML = updatedPrice;
      document.getElementById(itemID).style.backgroundColor = "#FFFF00";
   }
}
  
// Function that updates based on a given discount
function designDiscount(itemID, designName, discount) {
    alert("The " + designName + " design has been discontinued.  Please call Lynn at 1-888-304-0816 or email her at lynnslinens@mac.com to check the availability of the design.  If you have already done so, please proceed with the purchase.");

   /*var basePriceID = "basePrice" + itemID;
   var sizeID = "size" + itemID;
   var itemLengthID = "itemLength" + itemID;
   var itemWidthID = "itemWidth" + itemID;
   var linenID = "linen" + itemID;
   var designID = itemID;

   var updatedPrice = parseFloat(document.getElementById(basePriceID).value);  
   updatedPrice = updatedPrice * (1 - discount);
   updatedPrice = roundNumber(updatedPrice, 2);

   // Changes the HTML on the page to show the updated price
   if(parseFloat(document.getElementById(itemID).innerHTML) != updatedPrice) {
    //if((updatePrice.length - updatePrice.indexOf('.')) == 2)
    //  document.getElementById(itemID).innerHTML = updatedPrice + "0";
    //else
      //document.getElementById(itemID).innerHTML = updatedPrice;
      //document.getElementById(itemID).style.backgroundColor = "#FFFF00";
   }*/
}
  
// Function that adds an item to the cart.
function addToCart(item, itemID) {
   var sizeID = "size" + itemID;
   var itemWidthID = "itemWidth" + itemID;
   var itemLengthID = "itemLength" + itemID;
   var designID = "design" + itemID;
   var linenID = "linen" + itemID;
   var threadID = "thread" + itemID;
   var quantityID = "quantity" + itemID;
   
   var size = "";
   var dimensions = "";
   var design = "";
   var linen = "";
   var thread = "";
   var quantity = document.getElementById(quantityID).value;
   
   var selectedOption = -1;
   if(document.getElementById(sizeID)!=null) {
      selectedOption = document.getElementById(sizeID).selectedIndex;
      size = ",+" + document.getElementById(sizeID).options[selectedOption].text;
   }
   if(document.getElementById(itemWidthID)!=null && document.getElementById(itemLengthID)!=null) {
      dimensions = ",+User+Dimensions+" + document.getElementById(itemLengthID).value;
      dimensions = dimensions + "x" + document.getElementById(itemWidthID).value;
   }
   if(document.getElementById(designID)!=null) {
      selectedOption = document.getElementById(designID).selectedIndex;
      design = ",+" + document.getElementById(designID).options[selectedOption].text;
   }
   if(document.getElementById(linenID)!=null) {
      selectedOption = document.getElementById(linenID).selectedIndex;
      linen = ",+" + document.getElementById(linenID).options[selectedOption].text;
   }
   if(document.getElementById(threadID)!=null) {
      selectedOption = document.getElementById(threadID).selectedIndex;
      thread = ",+" + document.getElementById(threadID).options[selectedOption].text;
   }
   item = item + size + dimensions + design + linen + thread;
   var price = document.getElementById(itemID).innerHTML;
   
   // Checks to see if the dimensions need to be entered
   if(dimensions!="" && (document.getElementById(itemWidthID).value==0 || document.getElementById(itemLengthID).value==0)) {
      //alert(document.getElementById(itemWidthID));
      alert("You must enter the dimensions in inches!");
   }
   else {
      var url = "http://ww11.aitsafe.com/cf/add.cfm?userid=D123068&product=" + item + "&price=" + price + "&qty=" + quantity + "&return=http://www.altarlinens.com/catelog.aspx"
      window.open(url,"Cart","left=20,top=20,width=550,height=650,toolbar=0,resizable=0,scrollbars=1");
   }
}