﻿// Since the controls to use are dynamic, the UserControl file
// sets this array with the necessary values. The index of these
// values is such:
// The array name is: AddToCartJSVars
// 0 -> Cart Path
// 1 -> Quantity Control ClientID
// 2 -> Upsell Select Control ClientID
// 3 -> int ProductID
// 4 -> product price hidden field (only on ram-upgrades.aspx)
//
// aryBundlePrices is an array of the prices of items in the bundle list


function checkFreeShipping() {
    var qtyObj = document.getElementById(AddToCartJSVars[1]);
    var selObj = document.getElementById(AddToCartJSVars[2]);
    
    var price = (isNaN(AddToCartJSVars[4])) ? parseFloat(document.getElementById(AddToCartJSVars[4]).value) : parseFloat(AddToCartJSVars[4]);
    var shippingObj = document.getElementById("shippingWrapper");

    var totalAmount = 0;
    var freeShippingAllowed = true;
    if (selObj != null && selObj.selectedIndex > 0) {
        var index = selObj.selectedIndex;
        index -= 1;
        // Have to subtract 1 from the index because we
        // are already checking for > 0 and because
        // upsell list starts with a blank item at 0 but
        // the bundle prices array does not
        if (aryBundlePrices.length > index) {
            totalAmount = parseFloat(aryBundlePrices[index]);
        }
    }

    freeShippingAllowed = (totalAmount >= 0) ? freeShippingAllowed : false;

    if (qtyObj != null &&
        qtyObj.value != null && qtyObj.value != "" &&
        !isNaN(qtyObj.value)) {
        var qty = parseInt(qtyObj.value);
        totalAmount += (qty * price);
        if (totalAmount > 50.0 && freeShippingAllowed) {
            shippingObj.style.display = "block";
        }
        else {
            shippingObj.style.display = "none";
        }
    }
}

function qtyKeyUp() {
    var qtyObj = document.getElementById(AddToCartJSVars[1]);
    
    var reQty = new RegExp("[^0-9]","g");
    qtyObj.value = qtyObj.value.replace(reQty, "");
    checkFreeShipping();
}

function addBundle() {
    var cartPath = AddToCartJSVars[0];
    var qtyID = AddToCartJSVars[1];
    var selectID = AddToCartJSVars[2];
    // if the productID is Not a Number, then assume it's the id for a hidden field and get
    // that field's value, otherwise just use it as productID
    var productID = (isNaN(AddToCartJSVars[3])) ? document.getElementById(AddToCartJSVars[3]).value : AddToCartJSVars[3];
    var category = null;

    var loc = cartPath;

    var selectObj = document.getElementById(selectID);
    var bundleProduct = null;
    var bundleQty = 0;
    if (selectObj != null && selectObj.value != null && selectObj.value != "") {
        bundleProduct = selectObj.value;
        bundleQty = 1;
    }

    var qty = -1;
    // Sometimes the qtyID passed in will always be 1
    // and there will not be a Quantity field on the form
    if (isNaN(qtyID)) {
        var qtyObj = document.getElementById(qtyID);
        if (qtyObj != null && qtyObj.value != null) {
            qty = parseInt(qtyObj.value);
        }
    }
    else {
        qty = parseInt(qtyID);
    }
    if (!isNaN(qty) && qty <= 999 && qty > 0) {
        loc += "product_id=" + productID + "|" + qty + ((bundleProduct) ? "," + bundleProduct + "|" + bundleQty : "") + ((category) ? "&category_id=" + category : "");
        window.location = loc;
    }
    else {
        alert("To complete your purchase, please enter a valid quantity - a number between 1 and 999. For larger orders, please contact us at orders@edgetechcorp.com");
    }

    return false;
}

function qtyKeyDown(event) {
    if (event.which || event.keyCode) {
        var key = (event.which) ? event.which : event.keyCode;
        if (key == 13) {
            return addBundle();
        }
        else if (key != 8 && key != 27 && key != 46 && (key < 48 || key > 57)) {
            if (event.which) {
                event.cancel = true;
                return false;
            } else {
                event.keyCode = null;
                return true;
            }
        }
    }
    else {
        return true
    }
}
