addLoadEvent(prepareAttributes);

/* -----------------------------------------
----------- TOOL BAR -----------------------
----------------------------------------- */
var textarea;

String.prototype.trim = function() {
	return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
}

var attribute_req;
var selectedFlavor = '';
var numFlavors = '';
var countWarning = false;
var firstRun = true;

function update_selection() {
	var selects = document.getElementsByTagName('select');
	var info = document.getElementById('attribute_selection_info');
	var quantity = document.cart_quantity.cart_quantity.value;
	if (quantity != '' && quantity != null) {
		if (quantity < 1 || !IsNumeric(quantity)) { 
			quantity = 1;
			document.cart_quantity.cart_quantity.value = 1;
		}
	}
	var s_value = info.firstChild.nodeValue.trim();
	var parts = s_value.split(',');
	s_value = parts[0];
	var price = parseFloat(document.getElementById('product_price').value);
	for (var i=0; i<selects.length; i++) {
		parts = selects[i].options[selects[i].selectedIndex].text.split('(');
		s_value += ', '+parts[0];
		if (parts.length > 1) {
			var ac = parts[1].split('$');
			var ac2 = ac[1].split(')');
			price = parseFloat(ac2[0]);
		}
	}
	price = Math.round(price * 100);
	price = price * quantity;
	price = price.toString();
	var dollars = price.substring(0,price.length-2);
	var cents = price.substring(price.length-2,price.length);
	
	s_value += ' x '+quantity;
	info.firstChild.nodeValue = s_value;
	document.getElementById('attribute_selection_price').firstChild.nodeValue = 'PRICE: $'+dollars+'.'+cents;
}

function get_attribute_flavors(changed) {
	document.getElementById('flavor_warning').style.display = 'none';
	document.getElementById('flavor_count_warning').style.display = 'none';
	if (document.getElementById('attrib-2')) {
		var select_box = document.getElementById('attrib-2');
		var attrib = 2;
	} else if (document.getElementById('attrib-3')) {
		var select_box = document.getElementById('attrib-3');
		var attrib = 3;
	}
	if (changed == 'none') {
		changed = document.getElementById('attrib-1');
	}
	if (select_box && changed.id == 'attrib-1') {
		if (changed.className == 'attribute_Size') {
			if (selectedFlavor != '') {
				selectedFlavor = select_box.options[select_box.selectedIndex].text;	
			}
			var attribute_id = changed.options[changed.selectedIndex].value;
			attribute_req = createRequestObject();
			url = "get_attribute_flavors.php";
			attribute_req.open('post', url, true);
			attribute_req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
			attribute_req.onreadystatechange = handle_get_attribute_flavors;
			attribute_req.send("attribute_id="+attribute_id+"&products_id="+document.getElementById('products_id').value+"&attrib="+attrib);
		}
	} else {
		update_selection();
	}
}


// Handle the live search results
function handle_get_attribute_flavors() {
	if(attribute_req.readyState == 4) {
	   	if (attribute_req.status == 200) {
			var response = attribute_req.responseText;
			//alert(response);
			document.getElementById('flavor_color_options').innerHTML = response;
			var selects = document.getElementsByTagName('select');
			for (var i=0; i<1; i++) {
				selects[i].onchange = function() {
					get_attribute_flavors(this);
					return false;
				}
			}
			if (document.getElementById('attrib-2')) {
				var select_box = document.getElementById('attrib-2');	
			} else if (document.getElementById('attrib-3')) {
				var select_box = document.getElementById('attrib-3');
			}
			var flavor_select = select_box;
			var found = false;
			for (var i=0; i<flavor_select.length; i++) {
				if (flavor_select.options[i].text == selectedFlavor) {
					flavor_select.options[i].selected = true;
					found = true;
				}		
			}
			if (!found && !firstRun) {
				document.getElementById('flavor_warning').style.display = 'inline';
			}
			if (firstRun) {
				numFlavors = select_box.options.length;
			}
			if (select_box.options.length != numFlavors && !firstRun) {
				numFlavors = select_box.options.length;
				document.getElementById('flavor_count_warning').style.display = 'inline';
			}
			if (firstRun) {
				firstRun = false;	
			}
			update_selection();
   		}
   	}
}

function swap_image(changed) {
	if (!document.getElementById('productMainImage')) return false;
	var img = document.getElementById('productMainImage');
		
	if (changed.value == 116) {
		img.innerHTML = '<img src="images/shakerspout.jpg" alt="Shaker Cup" title=" Shaker Cup " width="204" height="243" />';
	} else if (changed.value == 117) {
		img.innerHTML = '<img src="images/ShakerCup.jpg" alt="Shaker Cup" title=" Shaker Cup " width="204" height="243" />';
	}
	update_selection();
}

function prepareAttributeImage() {
	if (!document.getElementById('attrib-4')) return false;
	var selects = document.getElementById('attrib-4');
	selects.onchange = function() {
		swap_image(this);
		return false;
	}
}

function prepareAttributes() {
	if (selectedFlavor == '') {
		if (document.getElementById('attrib-2')) {
			var select_box = document.getElementById('attrib-2');
			selectedFlavor = select_box.options[0].text
			numFlavors = select_box.options.length;
		} else if (document.getElementById('attrib-3')) {
			var select_box = document.getElementById('attrib-3');
			selectedFlavor = select_box.options[0].text
			numFlavors = select_box.options.length;
		}
	}
	get_attribute_flavors('none');
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById('attribute_selection_info')) return false;
	if (!document.getElementById('productAttributes')) return false;
	var selects = document.getElementsByTagName('select');
	for (var i=0; i<selects.length; i++) {
		selects[i].onchange = function() {
			get_attribute_flavors(this);
			return false;
		}
	}
	document.getElementById('cart_quantity').onkeyup = function() {
		get_attribute_flavors(this);
		return false;
	}
}
