/*

Written by Richard Zadorozny @ theory11
	(except getElementsByClassName)
	
Get your own sandwich.

*/

function getElementsByClassName(oElm, strTagName, strClassName){
	/*
		getElementsByClassName
		Written by Jonathan Snook, http://www.snook.ca/jonathan
		Add-ons by Robert Nyman, http://www.robertnyman.com
	*/
	
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function setupBuyDecks() {
	var ups = getElementsByClassName(document, 'div', 'dbd_up');
	for (var i = 0; i < ups.length; ++i) {
		var matches = ups[i].id.match(/^dbd_up_([0-9])+$/);
		if (matches) {
			ups[i].deckId = matches[1];
			var counter = document.getElementById('dbd_count_' + matches[1]);
			counter.value = 0;
			counter.onchange = function() {
				updateTotals();
			}
			
			ups[i].onclick = function() {
				var counter = document.getElementById('dbd_count_' + this.deckId);
				counter.value = parseInt(counter.value) + 1;
				updateTotals();
				return false;
			}
		}
	}

	var downs = getElementsByClassName(document, 'div', 'dbd_down');
	for (var i = 0; i < downs.length; ++i) {
		var matches = downs[i].id.match(/^dbd_down_([0-9])+$/);
		if (matches) {
			downs[i].deckId = matches[1];
			downs[i].onclick = function() {
				var counter = document.getElementById('dbd_count_' + this.deckId);
				if (parseInt(counter.value) > 0) {
					counter.value = parseInt(counter.value) - 1;
					updateTotals();
				}
				return false;
			}
		}
	}
}

function price(p) {
	if (p == 0) return '0.00';
	return (parseInt(p*100) + '').replace(/([0-9]{2})$/, '.$1');
}

function updateTotals() {
	var totalCount = 0;
	for (var i = 1; i <= 3; ++i) {
		totalCount += parseInt(document.getElementById('dbd_count_' + i).value);
	}

	var discounts = [3, 6, 12];
	var found = 0;
	for (var i = discounts.length - 1; i >= 0; --i) {
		var div = document.getElementById('dbuy_discount_' + (i + 1));
		var img = div.firstChild;
		
		if (!found && totalCount >= discounts[i]) {
			found = i + 1;
			img.src = img.src.replace(/\_off.gif/, '_on.gif');
		} else {
			img.src = img.src.replace(/\_on.gif/, '_off.gif');
		}
	}
	
	if (found) {
		var discount = totalCount * Math.round(4.95 * found * 5) / 100;
		
		document.getElementById('dbuy_price').childNodes[1].innerHTML = '<strike>$' + price(totalCount * 4.95) + '</strike> <strong>$' + price(totalCount * 4.95 - discount) + '</strong>';
		document.getElementById('dbuy_savings').childNodes[1].innerHTML = '$' + price(discount);
	} else {
		document.getElementById('dbuy_price').childNodes[1].innerHTML = '<strong>$' + price(totalCount * 4.95) + '</strong>';
		document.getElementById('dbuy_savings').childNodes[1].innerHTML = '$0.00';
	}
}

function decks_buy() {
	// build the url
	var items = 0;
	var ids = [[73, 76], [72, 75], [74, 77]];
	var url = 'http://www.theory11.com/store/quick_location.php?';
	for (var i = 0; i < 3; ++i) {
		var count = document.getElementById('dbd_count_' + (i+1)).value;
		if (count >= 12) {
			++items;
			url += 'id' + items + '=' + ids[i][1] + '&qty' + items + '=' + (count / 12) + '&';
			count = parseInt(count % 12);
		}
		if (count > 0) {
			++items;
			url += 'id' + items + '=' + ids[i][0] + '&qty' + items + '=' + count + '&';
		}
	}
	if (!items) {
		alert('Use the up and down arrows above to tell us how many of each colour you would like.');
	} else {
		location.href = url + 'loc=www.theory11.com/added-to-cart_suggestions.php';
	}
}

window.onload = function() { setupBuyDecks() };
