
var fontHeights = [];

function initializeVariables() {
    initializeFontHeights();
    buildStandardColorList();
    buildPremiumColorList();
    buildFontList();
    buildFontHeightList();
}

function initializeFontHeights() {
    for (var i = 0; i <= 21; i++) {
        fontHeights[i] = i + 1 // The index of an array is zero based so add one to get the value we want.
    }
}

function buildFontList() {
    var fontSelectList = Builder.node('select', {id:'selectFont'});
    fonts.each(function(s) {
        fontSelectList.appendChild(Builder.node('option', {value:s[0]}, s[0]));
    });
    $('selectFontSpan').appendChild(fontSelectList);
}

function buildFontHeightList() {
    fontHeights.each(function(s) {
        $('selectHeight').appendChild(Builder.node('option', {value:s}, s + '"'));
    });
}

function buildStandardColorList() {
    standardColorList = Builder.node('select', {id:'selectColor'});
    Element.extend(standardColorList);
    standardColors.each(function(s) {
        standardColorList.appendChild(Builder.node('option', {value:s[0]}, s[1]));
    });
}

function buildPremiumColorList() {
    premiumColorList = Builder.node('select', {id:'selectColor'});
    Element.extend(premiumColorList);
    premiumColors.each(function(s) {
        premiumColorList.appendChild(Builder.node('option', {value:s[0]}, s[1]));
    });
}

function setStandardColorList() {
    Event.stopObserving($('selectColor'), 'change', updateColor);
    Element.remove($('standardColors').firstDescendant());
    $('standardColors').insert(standardColorList);
    $('selectColor').selectedIndex = 0;
    Event.observe($('selectColor'), 'change', updateColor);
    updateColor();
    calculatePrice();

}

function setPremiumColorList() {
    Event.stopObserving($('selectColor'), 'change', updateColor);
    Element.remove($('standardColors').firstDescendant());
    $('standardColors').insert(premiumColorList);
    $('selectColor').selectedIndex = 0;
    Event.observe($('selectColor'), 'change', updateColor);
    updateColor();
    calculatePrice();
}

function clearText() {
    $('textarea').clear();
    $('selectColor').value = 'Black';
    $('textarea').style.color = 'Black';
    $('item_price').value = '';
    $('item_total').value = '';
    $('bold').checked = false;
    $('italic').checked = false;
    $('reverse').checked = false;
    $('selectFont').selectedIndex = 0;
    $('selectHeight').selectedIndex = 0;
    $('selectColor').selectedIndex = 0;
    $('standardColorRadio').checked = true;
    $('premiumColorRadio').checked = false;
}

function widthHelp() {
    alert('Different fonts will result in different widths. \nIf exact width is important to you, indicate the \nnumber in inches and we will adjust it accordingly. \nif you select this option it could increase your price.');
}

function reverseHelp() {
    alert("Printing vinyl in reverse allows you to adhere it to the inside of glass so that it is legible on the outside.");
}

function calculatePrice() {
    var newString = $F('textarea');
    var spaceIndex = 0;
    var numSpaces = 0;
    do  {
        spaceIndex = newString.indexOf(' ', spaceIndex);
        if (spaceIndex > -1) {
            numSpaces++;
            spaceIndex++;
        }
    } while (spaceIndex > -1);
    var billableCharacters = newString.length - numSpaces;

    var pricePerCharacter = priceMap[$F('selectHeight')];
    var vinylType = Form.getInputs('priceCalculator', 'radio', 'vinylType').find(function(radio) {
        return radio.checked;
    }).value
    if (vinylType.include('premium')) {
        var innerArray = premiumColors[$('selectColor').selectedIndex];
        pricePerCharacter += (pricePerCharacter * innerArray[2]);
    }
    var fontInnerArray = fonts[$('selectFont').selectedIndex];
    pricePerCharacter += (pricePerCharacter * fontInnerArray[1]);

    var price = (pricePerCharacter * billableCharacters).toFixed(2);

    $('priceQuote').innerHTML = price;
    $('item_price').value = price;
    $('item_total').value = price;
	$('pprice').value = price;
	$('itemid').value = $('itemid').value + 1;
	$('itemname').value = $('textarea').value;
}

function toggleWidth() {
    if ($('txtWidth').disabled) {
        $('txtWidth').enable();
        new Effect.SlideDown($('customWidth'));
        $('txtWidth').focus();
    } else {
        $('txtWidth').clear();
        $('txtWidth').disable();
        new Effect.SlideUp($('customWidth'));
    }

//    $('txtWidth').disabled = !$('txtWidth').disabled;
}

function updateColor() {
    var color = $F('selectColor');
    $('colorPreview').style.background = color;
    if (color == 'White') {
        $('textarea').style.color = 'Black';
    } else {
        $('textarea').style.color = color;
    }
    calculatePrice(); // since premium colors vary, we need to recalculate
}

function updateFont() {
    $('textarea').style.fontFamily = $('selectFont').value;
}

function submitDetails() {
    var vinylType = Form.getInputs('priceCalculator', 'radio', 'vinylType').find(function(radio) {
        return radio.checked;
    }).value
    var customText = $F('textarea');
    if (!$('txtWidth').disabled && $F('txtWidth').length > 0) {
        customText += ' | Cust w: ' + $F('txtWidth');
    }
    customText += ' | ' + $F('selectFont');
    customText += ' | H: ' + $F('selectHeight') + ' inches.';
    customText += ' | B: ' + ($F('bold') ? ' yes ' : ' no ');
    customText += ' | I: ' + ($F('italic') ? ' yes ' : ' no ');
    customText += ' | R: ' + ($F('reverse') ? ' yes ' : ' no ');
    customText += ' | Type: ' + (vinylType.include('standard') ? 'S' : 'M');                        // standard  ||  premium
    var selectColor = $('selectColor');
    customText += ' | C: ' + selectColor.options[selectColor.selectedIndex].text;


    $('item_sku').value = customText;
    $('item_rnd').value = Math.round(Math.random() * 2147483647);
    var totalPrice = $F('item_total');
    var shipping = 3.95;
    if (totalPrice >= 10.00 && totalPrice < 40.00) {
        shipping = 6.95;
    } else if (totalPrice >= 40.00 && totalPrice < 70.00) {
        shipping = 8.10;
    } else if (totalPrice >= 70.00 && totalPrice < 100.00) {
        shipping = 13.95;
    } else if (totalPrice >= 100) {
        shipping = 18.95;
    }
    $('shipping').value = shipping;
    return true;
}

