var s1, s2, s3; /*dropdowns for each style */
var val1 = 'no', val2 = 'no', val3 = 'no'; /*selected value*/
var div2, div3; /*surrounding div for display*/
var workingstyle1; /*holder so I don't always need to iterate*/
var opt; /*select option*/
var cartgroup, qty; /*product information*/
var view = 'grid'; /*Project Page: grid view, list view toggle*/

s1 = $('level_1');
s2 = $('level_2');
s3 = $('level_3');
div2 = $('stylediv_2');
div3 = $('stylediv_3');
cartgroup = $('cartgroup');
productalu = $('productaludisp');
qty = $('quantity');

if (typeof styles != "undefined") {
	cartgroup.style.display = 'none';
	productalu.style.display = 'none';
}

function getValues() {
	val1 = s1.options[s1.selectedIndex].value;
	if (s2 != null && s2.length > 0) {
		val2 = s2.options[s2.selectedIndex].value;
	}
	if (s3 != null && s3.length > 0) {
		val3 = s3.options[s3.selectedIndex].value;
	}
}

function updateStyles(level) {
	getValues();
	if (level == 1) {
		/*there is a next level*/
		if (val2 != 'no') {
			/*hide the pricing if it is showing*/
			cartgroup.style.display = 'none';
			productalu.style.display = 'none';
			/*empty out the next select*/
			for (var i = s2.length; i > 0; i--) {
				s2.options[i] = null;
			}
			if (val3 != 'no') {
				for (i = s3.length; i > 0; i--) {
					s3.options[i] = null;
				}
			}
			/*get the style from the array that I need to get substyles for*/
			for (i = 0; i < styles.stylelookup.style.length; i++) {
				if (styles.stylelookup.style[i].id == val1) {
					workingstyle = styles.stylelookup.style[i];
					break;
				}
			}
			/*add updated options*/
			for (i = 0; i < workingstyle.style.length; i++) {
				opt = document.createElement('option');
				for (j = 0; j < styles.stylelookup.styleinfo.length; j++) {
					if (styles.stylelookup.styleinfo[j].id == workingstyle.style[i].id) {
						opt.value = styles.stylelookup.styleinfo[j].id;
						opt.text = styles.stylelookup.styleinfo[j].name;
						break;
					}
				}
				s2.options[i + 1] = opt;
			}
		}
		else {
			/*I'm at the end, so I need to set my value*/
			for (i = 0; i < styles.stylelookup.style.length; i++) {
				if (styles.stylelookup.style[i].id == val1) {
					setPrices(styles.stylelookup.style[i].product);
					break;
				}
			}
		}

	} else if (level == 2) {
		if (val3 != 'no') {
			/*hide the pricing if it is showing*/
			cartgroup.style.display = 'none';
			productalu.style.display = 'none';
			/*if another level exists, empty the select*/
			for (i = s3.length; i > 0; i--) {
				s3.options[i] = null;
			}
			/*get the style from the array that I need to get substyles for;*/
			for (i = 0; i < styles.stylelookup.style.length; i++) {
				if (styles.stylelookup.style[i].id == val1) {
					workingstyle = styles.stylelookup.style[i];
					for (j = 0; j < workingstyle.style.length; j++) {
						if (workingstyle.style[j].id == val2) {
							workingstyle = workingstyle.style[j];
							break;
						}
					}
					break;
				}
			}

			/*add updated options*/
			for (i = 0; i < workingstyle.style.length; i++) {
				opt = document.createElement('option');
				for (j = 0; j < styles.stylelookup.styleinfo.length; j++) {
					if (styles.stylelookup.styleinfo[j].id == workingstyle.style[i].id) {
						opt.value = styles.stylelookup.styleinfo[j].id;
						opt.text = styles.stylelookup.styleinfo[j].name;
						break;
					}
				}
				s3.options[i + 1] = opt;
			}
		}
		else {
			/*I'm at the end, so I need to set my value*/
			/*get the style from the array that I need to get substyles for*/
			for (i = 0; i < styles.stylelookup.style.length; i++) {
				if (styles.stylelookup.style[i].id == val1) {
					workingstyle = styles.stylelookup.style[i];
					for (j = 0; j < workingstyle.style.length; j++) {
						if (workingstyle.style[j].id == val2) {
							setPrices(workingstyle.style[j].product);
							break;
						}
					}
					break;
				}
			}

		}

	} else if (level == 3) {
		/*get the style from the array that I need to get substyles for*/
		for (i = 0; i < styles.stylelookup.style.length; i++) {
			if (styles.stylelookup.style[i].id == val1) {
				workingstyle = styles.stylelookup.style[i];
				for (j = 0; j < workingstyle.style.length; j++) {
					if (workingstyle.style[j].id == val2) {
						workingstyle = workingstyle.style[j];
						break;
					}
				}
				break;
			}
		}

		/*I'm at the end, so I need to set my value*/
		for (i = 0; i < workingstyle.style.length; i++) {
			if (workingstyle.style[i].id == val3) {
				setPrices(workingstyle.style[i].product);
				break;
			}
		}
	}

}

function setPrices(productid) {
	/*empty quantity*/
	if (!qty.length) {
		for (i = qty.length; i >= 0; i--) {
			qty.options[i] = null;
		}
	}
	$('itemnumber_1').value = productid;
	for (i = 0; i < products.product.length; i++) {
		if (products.product[i].id == productid) {
			$('priceleader').innerHTML = products.product[i].priceleader;
			$('baseprice').innerHTML = products.product[i].priceinfo;
			$('saleprice').innerHTML = products.product[i].saleprice;
			$('extendedprice').innerHTML = products.product[i].extendedinfo;
			$('clubbaseprice').innerHTML = products.product[i].clubpriceinfo;
			$('clubsaleprice').innerHTML = products.product[i].clubsaleprice;
			var ebp = $('elitebaseprice');
			if (ebp != null) {
				ebp.innerHTML = products.product[i].elitepriceinfo;
				if (products.product[i].elitepriceinfo != '')
					ebp.style.paddingRight = "4px";
				else
					ebp.style.paddingRight = "0";
				$('elitesaleprice').innerHTML = products.product[i].elitesaleprice;
			}

			if (products.product[i].priceinfo != '')
				$('baseprice').style.paddingRight = "4px";
			else
				$('baseprice').style.paddingRight = "0";

			if (products.product[i].clubpriceinfo != '')
				$('clubbaseprice').style.paddingRight = "4px";
			else
				$('clubbaseprice').style.paddingRight = "0";

			if ($('productmessages')) {
				$('productmessages').innerHTML = products.product[i].message;
			}
			$('productaludisp').innerHTML = "Item #" + products.product[i].alu;
			if ($('enlargeableimage1') != null) {
				var srctext = $('enlargeableimage1').src;
				srctext = srctext.substr(0, srctext.lastIndexOf('/') + 1) + products.product[i].alu + '-L.jpg-250x';
				$('enlargeableimage1').src = srctext;
				var linktext = $('enlargeableimagelink1').href;
				linktext = linktext.substr(0, linktext.lastIndexOf('/') + 1) + products.product[i].alu + "-L.jpg-600x";
				$('enlargeableimagelink1').href = linktext;
				$('largerimage1').href = linktext;
			}
			/*set quantity dropdowns*/
			if (!qty.length == false) {
				for (j = 0; j < quantities; j++) {
					opt = document.createElement('option');
					opt.value = products.product[i].qty * (j + 1);
					opt.text = products.product[i].qty * (j + 1);
					qty.options[j] = opt;
				}
			}
			$('u_m').innerHTML = products.product[i].u_m;
			if (products.product[i].qty > 1) {
				$('est').style.display = 'inline';
			}
			else {
				$('est').style.display = 'none';
			}
			break;
		}
	}
	cartgroup.style.display = 'block';
	productalu.style.display = 'block';
}

function swapTabs(tabName) {
	// Swap all styles to product_tab_off class
	var tabItem = $('tabs').select('.tabitem');
	tabItem.each(function(e) {
		e.select('div').each(function(f) {
			if (f.hasClassName('product_tab_on')) {
				f.className = 'product_tab_off';
				var tab = f.id + '_div';
				$(tab).hide();
			}
		})
	})
	// Swap class fpr selected tab to product_tab_on
	$(tabName).className = 'product_tab_on';
	var tab = tabName + '_div';
	$(tab).show();
}

function addToWishList() {
	jQuery('#h_processor').val('store');
	jQuery('#h_action').val('gotowishlist');	
	jQuery('#prodform').submit();
}

// PROJECT PAGE FUNCTIONS
function toggleQuantity(input) {
	if (input.id == 'quantity_check_all') {
		$('quickorder').select('input[type="checkbox"]').each(function(element) {
			if (element.id != 'quantity_check_all') {
				element.checked = input.checked;
				var index = element.id.lastIndexOf('_');
				var quantity = "quantity" + element.id.substring(index);
				var itemnumber = "itemnumber" + element.id.substring(index);

				if (element.checked) {
					$(quantity).removeAttribute('disabled');
					$(itemnumber).removeAttribute('disabled');
					$(quantity).previous('span').setStyle({ 'color': '#000000' });
				} else {
					$(quantity).setAttribute('disabled', 'true');
					$(itemnumber).setAttribute('disabled', 'true');
					$(quantity).previous('span').setStyle({ 'color': '#CCCCCC' });
				}
			}
		});
	} else {
		var index = input.id.lastIndexOf('_');
		var quantity = "quantity" + input.id.substring(index);
		var itemnumber = "itemnumber" + input.id.substring(index);

		if (input.checked) {
			$(quantity).removeAttribute('disabled');
			$(itemnumber).removeAttribute('disabled');
			$(quantity).previous('span').setStyle({ 'color': '#000000' });
		} else {
			$(quantity).setAttribute('disabled', 'true');
			$(itemnumber).setAttribute('disabled', 'true');
			$(quantity).previous('span').setStyle({ 'color': '#CCCCCC' });
		}
	}
}

function validateFields() {
	var validForm = false;
	$('quickorder').select('input[type="text"]', 'select').each(function(element) {
		if (!element.readAttribute('disabled')) {
			validForm = true;
			throw $break;
		}
	});
	if (!validForm) {
		HandleAlertMessage('No items selected to add to cart.  Please make sure the item you have chosen has been selected and has a quantity greater than zero.');
		return false;
	}

	$('quickorder').select('input[type="text"]', 'select').each(function(element) {
		if (isNaN(element.value)) {
			validForm = false;
		} else if (element.value <= 0) {
			validForm = false;
		}
	});

	if (!validForm) {
		alert('Only numeric values greater than zero can be entered.');
		return false;
	}

	if (jQuery('#h_action').val() == 'addtowishlistbysku') {
		if (getCookieValue('%5Fuserid') != null) {
			validForm = true;
		} else {
			HandleAlertMessage('You must log in before you can add items to your wishlist.', 'user-message','alert');
			return false;
		}
	}

	if (validForm)
		$('quickorder').submit();
}

function changeView(button) {
	// if grid_view go to list_view
	if ($('project_products_table_grid_view')) {
		if (button.id == 'list_view_img') {
			$('project_products_table_grid_view').setAttribute('id', 'project_products_table');
			$('grid_view_img').setAttribute('src', '/site/framework/images/buttons/grid_view_unclicked.png');
			$('grid_view_img').addClassName('on');
			$('list_view_img').setAttribute('src', '/site/framework/images/buttons/list_view_clicked.png');
			$('list_view_img').removeClassName('on');
			$$('div.product_image').each(function(element) {
				var img = $(element).down('img');
				var source = $(img).src.replace(/105/g, '50');
				$(img).src = source;
				$(img).width = '50';
				$(img).height = '50';
			});
			$$('.pricegroup').each(function(element) {
				$(element).show();
			});
			$$('.pricegroup_grid_view').each(function(element) {
				$(element).hide();
			});
		}
		// else if list_view go to grid_view
	} else if ($('project_products_table')) {
		if (button.id == 'grid_view_img') {
			$('project_products_table').setAttribute('id', 'project_products_table_grid_view');
			$('grid_view_img').setAttribute('src', '/site/framework/images/buttons/grid_view_clicked.png');
			$('grid_view_img').removeClassName('on');
			$('list_view_img').setAttribute('src', '/site/framework/images/buttons/list_view_unclicked.png');
			$('list_view_img').addClassName('on');
			$$('div.product_image').each(function(element) {
				var img = $(element).down('img');
				var source = $(img).src.replace(/50/g, '105');
				$(img).src = source;
				$(img).width = '105';
				$(img).height = '105';
			});
			$$('.pricegroup').each(function(element) {
				$(element).hide();
			});
			$$('.pricegroup_grid_view').each(function(element) {
				$(element).show();
			});
		}
	}
}
Event.observe(window, 'load', function() {
	// initially hide elite/club prices because grid view is default
	if ($('project_page_wrap')) {
		$$('.pricegroup').each(function(element) {
			$(element).hide();
		});
	}
});
