﻿function Product(id, capacity, price, availability, freeShippingEligible, image, discount, retailPrice, unitPrice, discountPercentage, rebate, rebateAmount) {
    this.product_id = id;
    this.capacity = capacity;
    this.price = price;
    this.availability = availability;
    this.freeShippingEligible = freeShippingEligible;
    this.image = image;
    this.discount = discount;
    this.retailPrice = retailPrice;
    this.unitPrice = unitPrice;
    this.discountPercentage = discountPercentage;
    this.rebate = rebate;
    this.rebateAmount = rebateAmount;
}

function clickProduct(objIn) {
    var prod = (this["product"] != null) ? this["product"] : objIn;
    window["SELECTED_PRODUCT"] = prod;

    // clearing the class for all of the product capacities / options
    var links = productJSArrayControls[productJSArrayControls.length - 1];
    if (links != null) {
        for (var i = 0; i < links.length; i++) {
            links[i].className = "";
        }
    }

    // setting the class for only the selected link
    var link = (this["product"] != null) ? this : objIn["link"];
    if (link != null && link.className != null)
        link.className = "capacitySelected";

    if (prod != null) {
        // AddToCartJSVars is used in bundler.net.js
        // to check for free shipping
        AddToCartJSVars[4] = prod.price;

        // productJSArrayControls[0] is a hidden field which
        // holds the product_id
        if (productJSArrayControls[0] != null)
            productJSArrayControls[0].value = prod.product_id;
        
        if (productJSArrayControls[1] != null)
            productJSArrayControls[1].innerHTML = prod.capacity;

        if (productJSArrayControls[2] != null)
            productJSArrayControls[2].innerHTML = prod.availability;

        var priceTable = productJSArrayControls[5];
        if (priceTable != null) {
            // Deleting all of the rows from the table
            for (var i = 0; i < priceTable.childNodes.length; i++) {
                var child = priceTable.childNodes[i];
                priceTable.removeChild(child);
            }

            // if there is a rebate then we add rows for the
            // Before Rebate amount and Rebate Amount
            if (prod.rebate) {
                // the first row to add - "Before Rebate: $#.##"
                var originalAmountRow = priceTable.insertRow(-1);
                var labelCell = originalAmountRow.insertCell(-1);
                labelCell.setAttribute("align", "left");
                labelCell.appendChild(document.createTextNode("Before Rebate:"));
                
                var amountCell = originalAmountRow.insertCell(-1);
                amountCell.setAttribute("align", "right");
                amountCell.style.textDecoration = "line-through";
                amountCell.appendChild(document.createTextNode("$" + prod.unitPrice));

                var rebateRow = priceTable.insertRow(-1);

                // rebateLabelCell has "Rebate Amount" and the popup / link
                var rebateLabelCell = rebateRow.insertCell(-1);
                rebateLabelCell.setAttribute("align", "left");
                rebateLabelCell.appendChild(document.createTextNode("Rebate Amount:"));
                var rebateLink = document.createElement("A");
                rebateLink.className = "helpPopUp";
                rebateLink.setAttribute("href", "javascript:void(0)");
                rebateLink.onclick = function() {
                    window.open('/help/popup/help-rebates.htm', 'name', 'height=300,width=385,scrollbars=1,resizable=1');
                }
                var img = document.createElement("IMG");
                img.setAttribute("src", "/Assets/images/icons/help-popup.gif");
                img.setAttribute("border", "0");
                rebateLink.appendChild(img);
                rebateLabelCell.appendChild(rebateLink);

                // rebateAmountCell just has the rebate amount
                var rebateAmountCell = rebateRow.insertCell(-1);
                rebateAmountCell.setAttribute("align", "right");
                rebateAmountCell.appendChild(document.createTextNode("-$" + prod.rebateAmount));
            }
            
            // pricing row will always be added regardless of rebate or discount
            var priceRow = priceTable.insertRow(-1);

            // used document.createElement("TH") instead of priceRow.insertCell(-1)
            // so that I could get "TH" cells instead of "TD" - that way the
            // actual price will always be emphasized
            var actualPriceLabelColumn = document.createElement("TH");
            actualPriceLabelColumn.setAttribute("align", "left");

            // if there is a rebate then the price label says "Final Price"
            // otherwise it will say "Your Price"
            actualPriceLabelColumn.appendChild((prod.rebate) ? document.createTextNode("Final Price:") : document.createTextNode("Your Price:"));
            priceRow.appendChild(actualPriceLabelColumn);

            var actualPriceColumn = document.createElement("TH");
            actualPriceColumn.childNodes.length
            actualPriceColumn.setAttribute("align", "right");
            actualPriceColumn.appendChild(document.createTextNode("$" + prod.price));
            priceRow.appendChild(actualPriceColumn);

            // now we go to the discount row
            if (prod.discount) {
                var discountRow = priceTable.insertRow(-1);
                var discountLabelCell = discountRow.insertCell(-1);
                discountLabelCell.setAttribute("align", "left");
                discountLabelCell.setAttribute("valign", "top");

                discountLabelCell.appendChild(document.createTextNode("Was:"));
                discountLabelCell.appendChild(document.createElement("BR"));

                var discountAmountCell = discountRow.insertCell(-1);
                discountAmountCell.setAttribute("align", "right");
                
                var discountAmount = document.createElement("SPAN");
                discountAmount.style.textDecoration = "line-through";
                discountAmount.appendChild(document.createTextNode("$" + prod.retailPrice));

                discountAmountCell.appendChild(discountAmount);
                discountAmountCell.appendChild(document.createElement("BR"));
                
                // tack on a <p> element with "(Save #%)"
                var savePercent = document.createElement("SPAN");
                savePercent.id = "priceDropSavings";
                savePercent.style.textDecoration = "none";
                savePercent.appendChild(document.createTextNode("(Save " + prod.discountPercentage + "%)"));
                discountAmountCell.appendChild(savePercent);
            }
        }

        // checkFreeShipping() is in bundler.net.js
        if (prod.freeShippingEligible != false) {
            checkFreeShipping();
        }
        
        // swapping the product image
        if (productJSArrayControls[4] != null && prod.image != null) {
            productJSArrayControls[4].src = prod.image;
        }
    }
}

Product.prototype.capacity = "";
Product.prototype.price = 0.00;
Product.prototype.availability = "";
Product.prototype.freeShippingEligible = true;
Product.prototype.image = "";
Product.prototype.discount = false;
Product.prototype.retailPrice = 0.00;
Product.prototype.unitPrice = 0.00;
Product.prototype.discountPercentage = 0;
Product.prototype.rebate = false;
Product.prototype.rebateAmount = 0.00;
Product.prototype.link = null;

// productJSArray is populated in code with the products in the same order as the links
// productJSArrayControls is also populated in code with the necessary clientID's
// 0 => hidden product field,
// 1 => capacityLabel,
// 2 => availabilityLabel,
// 3 => shippingInfoDiv,
// 4 => big image swapper (null for now),
// 5 => pricing table
function fixProductLinks() {
    for (var n = 0; n < productJSArrayControls.length; n++) {
        productJSArrayControls[n] = (productJSArrayControls[n] != null) ? document.getElementById(productJSArrayControls[n]) : null;
    }
    var div = document.getElementById("capacityList");
    var links = (div != null) ? div.getElementsByTagName("A") : null;

    if (links != null) {

        productJSArrayControls[productJSArrayControls.length] = links;

        links[0].className = "capacitySelected";
        productJSArray[0].link = links[0];

        for (var i = 0; i < links.length; i++) {
            links[i]["product"] = productJSArray[i];
            links[i].onclick = clickProduct;
        }
    }

    productJSArrayControls[3].style.display = "none";

    var prod = productJSArray[0];
    clickProduct(prod);
}

function doUpsellChange() {
    var prod = window["SELECTED_PRODUCT"];
    if (prod != null && prod.freeShippingEligible)
        checkFreeShipping();
}