﻿var printDescription = document.getElementById('printDescription');
var fabricImage = document.getElementById('fabricImage');
var fabricDescription = document.getElementById('fabricDescription');
var limitedSelectionCell = document.getElementById('limitedSelectionCell');
var priceCell = document.getElementById('priceCell');
var discountCell = document.getElementById('discountCell');
var pocketSelectionRow = document.getElementById('pocketSelectionRow');
var bottomPocketRow = document.getElementById('bottomPocketRow');
var userPocketSelection;

OnEmbroidery(embroideryCellID, embroideryCheckboxID);
RestoreSelectedPrintOnPostBack();

function RestoreSelectedPrintOnPostBack() {

    selectDescription = printDescriptionTextBox.value;

    for (var index = 0; index < printList.length; index++) {
        if (printList.options[index].innerHTML == selectDescription) {
            printList.selectedIndex = index;
            break;
        }
    }

    OnPrintSelection();
}

// Print Selection from Combo Box
function OnPrintSelection() {

    var inventoryDetails = printList.options[printList.selectedIndex].value;

    printDescription.innerHTML = printList.options[printList.selectedIndex].innerHTML;
    fabricDescription.innerHTML = GetFabricDescription(inventoryDetails);
    fabricImage.src = GetImageFileName(inventoryDetails);

    printDetailTextBox.value = inventoryDetails;
    printDescriptionTextBox.value = printDescription.innerHTML;

    priceCell.innerHTML = "regular price $" + Price(inventoryDetails);

    if (IsLimited(inventoryDetails)) {

        limitedSelectionCell.style.display = '';
        OnSnapupChange();
        LimitRadioButtons(inventoryDetails);
    }
    else {

        limitedSelectionCell.style.display = 'none';
        
        onePocketRadio.disabled = false;
        twoPocketRadio.disabled = false;
        threePocketRadio.disabled = false;

        OnSnapupChange();
    }

    DefineAvailableQuantity();

    var discount = Discount(inventoryDetails);

    if (discount == 1) {
        discountCell.innerHTML = "<img alt='close out' style='height:28px;' src='../../Images/CloseOutLarge.png' />";
    }
    else if (discount == 2) {
        discountCell.innerHTML = "<img alt='special deal' style='height:28px;' src='../../Images/SpecialDealLarge.png' />";
        priceCell.innerHTML = "regular price $23.99";
    }
    else if (discount == 3) {
        discountCell.innerHTML = "<img alt='limit qty sale' style='height:28px;' src='../../Images/LimitedQtyLarge.png' />";
    }
    else {
        discountCell.innerHTML = "";
    }
}

function LimitRadioButtons(inventoryDetails) {

    var numberofpockets = Pockets(inventoryDetails);

    if (numberofpockets == 1) {
        onePocketRadio.checked = true;

        onePocketRadio.disabled = false;
        twoPocketRadio.disabled = true;
        threePocketRadio.disabled = true;
    }
    else if (numberofpockets == 2) {
        twoPocketRadio.checked = true;

        onePocketRadio.disabled = true;
        twoPocketRadio.disabled = false;
        threePocketRadio.disabled = true;
    }
    else {
        threePocketRadio.checked = true;

        onePocketRadio.disabled = true;
        twoPocketRadio.disabled = true;
        threePocketRadio.disabled = false;
    }
}

function DefineAvailableQuantity() {
    var inventoryDetails = printList.options[printList.selectedIndex].value;
    var qtyAvailable = GetAvailableQuantity(inventoryDetails);

    if (quantityList.options.length != qtyAvailable) {
        quantityList.options.length = 0;

        for (var qty = 1; qty <= qtyAvailable; qty++) {
            quantityList.options.add(new Option(qty));
        } 
    }
}

function OnSnapupChange() {
   
    var inventoryDetails = printList.options[printList.selectedIndex].value;

    // Don't allow finished good to change snap-up style
    if (IsLimited(inventoryDetails)) {
        snapupCheckbox.checked = (Product(inventoryDetails) == " 1");
    }

    if (snapupCheckbox.checked) 
    {
        pocketSelectionRow.style.display = 'none';
        bottomPocketRow.style.display = '';
    }
    else {
        pocketSelectionRow.style.display = '';
        bottomPocketRow.style.display = 'none';
    }
}


