function formatCurrency(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}


function addProduct(prodID, dept, cat, option) {
    var myHREF = "/preorderform.asp?dept=" + dept + "&cat=" + cat + "&prod=" + prodID + "&options=" + option + "&qty=1&personalize=no";
    location.href = myHREF;
}

function selectColor(option, text) {
    $('hdnColor').value = option.replace('\'', '');
    $('lblColor').innerHTML = text.replace('\'', '');
}

function validateProduct() {
    if ($('hdnColor').value.length > 0) {
        return true;
    }
    else {
        alert('Please select a color from the color list.');
        return false;
    }
}

function replaceImage(htmlID, oldwidth, newwidth, container, num) {
    //remove the selected class from whatever thumbnail was selected
    $$('#' + container + ' .ThumbnailSelected').each(function(item) { item.removeClassName('ThumbnailSelected').addClassName('Thumbnail'); });

    //add the selected class to the thumbnail being selected
    $(container + '_link_' + num).removeClassName('Thumbnail').addClassName('ThumbnailSelected');

    //now replace the image
    var oldparams = "&w=" + oldwidth + "&h=" + oldwidth;
    var newparams = "&w=" + newwidth + "&h=" + newwidth;

    $(htmlID).src = $(container + '_img_' + num).src.replace(oldparams, newparams);
}

function changeSwatch(colorID, switchDropdown) {
    //remove the selected class from whatever swatch was selected
    $$('#ColorSwatches .Selected').each(function(item) { item.removeClassName('Selected').addClassName('Unselected'); });

    //add the selected class to the swatch being selected
    $('swatch_' + colorID).removeClassName('Unselected').addClassName('Selected');

    //change the dropdown box value
    //  should be false if event was triggered by dropdown
    if (switchDropdown) {
        for (var i = 0; i < $('selectableOptions').length; i++) {
            if ($('selectableOptions')[i].value == colorID) {
                $('selectableOptions').selectedIndex = i;
                break;
            }
        }
    }

    //hide all the thumbnail sections
    $$('#productThumbnails div').each(function(item) { item.hide(); });

    //show the thumbnails for this option
    $('thumbs_' + colorID).show();

    //replace the main image with the first thumbnail
    replaceImage('imgMain', 70, 385, 'thumbs_' + colorID, 0);

    $('options').value = $('option_' + colorID).value;
    
    changePrice(colorID);
    changeSKU(colorID);
    changeStockStatus(colorID);
}

function changePrice(colorID) {
    if ($('values_' + colorID)) {
    	var offset = $('values_' + colorID).value.split(';')[0];
    	if (parseFloat(offset) >= 0) {
    		if (parseFloat($('baseSale').value) == 0) {
    			$('Price1').innerHTML = '';
    			$('Price2').innerHTML = '$' + formatCurrency(parseFloat($('basePrice').value) + parseFloat(offset));
    		}
    		else {
    			$('Price1').innerHTML = '$' + formatCurrency(parseFloat($('basePrice').value) + parseFloat(offset));
    			$('Price2').innerHTML = '$' + formatCurrency(parseFloat($('baseSale').value) + parseFloat(offset));
    		}
    	} 
    	else {
    		if (parseFloat($('baseSale').value) == 0) {
    			$('Price1').innerHTML = '$' + formatCurrency(parseFloat($('basePrice').value));
    			$('Price2').innerHTML = '$' + formatCurrency(parseFloat($('basePrice').value) + parseFloat(offset));
    		}
    		else {
    			$('Price1').innerHTML = '$' + formatCurrency(parseFloat($('basePrice').value)) + ' $' + formatCurrency(parseFloat($('baseSale').value));
    			$('Price2').innerHTML = '$' + formatCurrency(parseFloat($('baseSale').value) + parseFloat(offset));
    		}
    	}
    }
}

function changeSKU(colorID) {
    if ($('values_' + colorID)) {
        if ($('Sku')) {
            $('Sku').innerHTML = $('values_' + colorID).value.split(';')[1];
        }
    }
}

function changeStockStatus(colorID) {
    if ($('values_' + colorID)) {
        if ($('StockStatus')) {
        		var iThreshold = 0
            //  NR:  Waiting for stock status in product options
            var quantity = $('values_' + colorID).value.split(';')[2];
            var stockStatus = 1;

            if (quantity <= iThreshold)
                stockStatus = 2;
            if (stockStatus == 1) {
                $('StockStatus').removeClassName('OutOfStock');
                $('AddToCartButton').show();
                $('Qty').disabled = false;
                var quantity = $('values_' + colorID).value.split(';')[2];
                var stockStatus = $('values_' + colorID).value.split(';')[3];
            }
            else {
                $('StockStatus').addClassName('OutOfStock');
                $('AddToCartButton').hide();
                $('Qty').disabled = true;
                stockStatus = 2;
            }

            if ($('stockStatusMessage_' + stockStatus))
                $('StockStatus').innerHTML = $('stockStatusMessage_' + stockStatus).value;
            else
                $('StockStatus').innerHTML = $('stockStatusMessage_1').value;
        }
    }
}

function verifyQty() {
	if ($('selectableOptions')) {
		var colorID = $('selectableOptions').value;
		var quantity = $('values_' + colorID).value.split(';')[2];
		if (parseInt($('Qty').value) > parseInt(quantity)) {
			alert('Low stock alert, only ' + quantity + ' available for purchase.');
			return false;
		}
	}	
	else {
		if (parseInt($('Qty').value) > parseInt($('baseQty').value)) {
			alert('Low stock alert, only ' + $('baseQty').value + ' available for purchase.');
			return false;
		}
	}
	return true;
}