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;
	if (option != '') {
    myHREF = "/preorderform.asp?dept=" + dept + "&cat=" + cat + "&prod=" + prodID + "&options=" + option + "&qty=1&personalize=no";
  } else {
  	myHREF = "/preorderform.asp?dept=" + dept + "&cat=" + cat + "&prod=" + prodID + "&quantity=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 setOption(optValue, changePrice) {
	$('options').value = $('option_' + optValue).value;
	
	if (changePrice) 
	{
		if ($('option_' + optValue + '_extra')) 
		{
			var offset = $('option_' + optValue + '_extra').value;
    	if (parseFloat(offset) >= 0)
    	{
    		var newList = parseFloat($('baseList').value) + parseFloat(offset);
    		var newPrice = parseFloat($('basePrice').value) + parseFloat(offset);
    	
    		if ($('ListPrice')) { $('ListPrice').innerHTML = 'List Price: $' + formatCurrency(newList); }    	
    		$('Price').innerHTML = 'Our Price: $' + formatCurrency(newPrice);
  		}
  	}
  }
}

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 = 9;

            if (stockStatus != 9) {
                $('StockStatus').removeClassName('OutOfStock');
                $('AddToCartButton').show();
                $('Qty').disabled = false;
            }
            else {
                $('StockStatus').addClassName('OutOfStock');
                $('AddToCartButton').hide();
                $('Qty').disabled = true;
            }

            if ($('stockStatusMessage_' + stockStatus))
                $('StockStatus').innerHTML = $('stockStatusMessage_' + stockStatus).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;
}

function setAccOption(optValue, accID) {
	if (optValue > -1) {
		$('options_' + accID).value = $('accOption_' + optValue).value;	
  }
  else {
  	$('options_' + accID).value = '';    	
  }
}
jQuery.noConflict();
jQuery(document).ready(function () {
    jQuery('.lightwindow').live('click', function () {
        //Get dimensions from rel tag
        var jWinDimensions = jQuery(this).attr('rel');

        var jWinWidth = 400;
        var jWinHeight = 300;
        var jWinTitle = " ";
        if (jWinDimensions != "") {
            var jWinWidthStart = jWinDimensions.indexOf("width") + 6;
            var jWinHeightStart = jWinDimensions.indexOf("height") + 7;
            var jWinHeightEnd;

            if (jWinWidthStart < jWinHeightStart) {
                jWinWidthEnd = jWinDimensions.indexOf(",", jWinWidthStart);
                jWinWidth = parseInt(jWinDimensions.substring(jWinWidthStart, jWinWidthEnd));
                jWinHeight = parseInt(jWinDimensions.substring(jWinHeightStart, jWinDimensions.length));

            }
            else {
                jWinWidth = parseInt(jWinDimensions.substring(jWinWidthStart, jWinDimensions.length));
                jWinHeightEnd = jWinDimensions.indexOf(",", jWinHeightStart);
                jWinHeight = parseInt(jWinDimensions.substring(jWinHeightStart, jWinHeightEnd));
            }
        }
        jQuery.fn.colorbox({ href: jQuery(this).attr('href'), iframe: true, width: jWinWidth, height: jWinHeight, rel: "nofollow" });
        return false;
    });
    // opens non-modal window - no iframe
    jQuery('.nolightwindow').live('click', function () {
        //Get dimensions from rel tag
        var jWinDimensions = jQuery(this).attr('rel');
        var jWinWidth = 400;
        var jWinHeight = 300;
        var jWinTitle = " ";
        if (jWinDimensions != "") {
            var jWinWidthStart = jWinDimensions.indexOf("width") + 6;
            var jWinHeightStart = jWinDimensions.indexOf("height") + 7;
            var jWinHeightEnd;

            if (jWinWidthStart < jWinHeightStart) {
                jWinWidthEnd = jWinDimensions.indexOf(",", jWinWidthStart);
                jWinWidth = parseInt(jWinDimensions.substring(jWinWidthStart, jWinWidthEnd));
                jWinHeight = parseInt(jWinDimensions.substring(jWinHeightStart, jWinDimensions.length));

            }
            else {
                jWinWidth = parseInt(jWinDimensions.substring(jWinWidthStart, jWinDimensions.length));
                jWinHeightEnd = jWinDimensions.indexOf(",", jWinHeightStart);
                jWinHeight = parseInt(jWinDimensions.substring(jWinHeightStart, jWinHeightEnd));
            }
        }
        jQuery.fn.colorbox({ href: jQuery(this).attr('href'), iframe: false, width: jWinWidth, height: jWinHeight, rel: "nofollow" });
        return false;
    });
});
