function debug(msg)
{
	if (console.log) { console.log(msg); }
}

function updateAll()
{
	productChangeEvent();
	shapeChangeEvent();
}

function productChangeEvent()
{
//	debug('in productChangeEvent');
	var selection = $("select[name='grill_type']").val();
	if (selection == '1') //	Licensed
	{
		showLicensedOptions();
		hidePersonalMessage();
	}
	else if (selection == '2') //	Personalized
	{
		hideLicensedOptions();
		showPersonalMessage();
	}
	else if (selection == '3') //	Plain
	{
		hideLicensedOptions();
		hidePersonalMessage();
	}
	
	if (selection && selection != "")
	{
		selectItemNumber(selection);
	}
//	debug('Price set to :' + $("#price").attr('value'));
}

function shapeChangeEvent()
{
	var selection = $("input[name='shape']:checked").val();
	if (selection == 'rect')
	{
		showSizingForm();
		sizeChangeEvent();
	}
	else if (selection == 'round')
	{
		hideSizingForm();
		setSKU("round");
		setPrice(134.99);
	}
}

function sizeChangeEvent()
{
	var length = $("select[name='length']").val();
	var l_frac = $("select[name='l_fraction']").val();
	var width = $("select[name='width']").val();
	var w_frac = $("select[name='w_fraction']").val();
	
	recalcPrice(
		(0.0 + length + l_frac), 
		(0.0 + width + w_frac));
	
	setSKU("" + width + w_frac + "x" + length + l_frac);
}

function selectItemNumber(itemnum)
{
//	alert("Selecting itemnum:" + itemnum);
	$("input[name='itemnum']").attr('value', itemnum);
	$("#price").attr('name', itemnum + ":price");
	$("#sku").attr('name', itemnum + ":sku");
}

function recalcPrice(length, width)
{
	var price = 0.0;
	var sq_area = length * width;
	
//	alert("recalc with l: " + length + ", w: " + width + " total: " + sq_area);
	
	if (sq_area <= 500)
	{
		price = 134.99;
	}
	else if (sq_area > 500 && sq_area <= 550)
	{
		price = 144.99;
	}
	else if (sq_area > 550 && sq_area <= 600)
	{
		price = 154.99;
	}
	else if (sq_area > 600 && sq_area <= 650)
	{
		price = 189.99;
	}
	else if (sq_area > 650 && sq_area <= 700)
	{
		price = 204.99;
	}
//	else if (sq_area > 700 && sq_area <= 750)
	else if (sq_area > 700 && sq_area <= 750)
	{
		price = 228.99;
	}
	else if (sq_area > 750 && sq_area <= 800)
	{
		price = 249.99;
	}
	else if (sq_area > 800 && sq_area <= 850)
	{
		price = 269.99;
	}
	else if (sq_area > 850 && sq_area <= 900)
	{
		price = 289.99;
	}
	else if (sq_area > 900 && sq_area <= 950)
	{
	price = 315.99;
	}
	else if (sq_area > 950 && sq_area <= 1000)
	{
	price = 345.99;
	}
	else if (sq_area > 1000 && sq_area <= 1050)
	{
	price = 375.99;
	}
	else if (sq_area > 1050 && sq_area <= 1100)
	{
	price = 409.99;
	}
	
	setPrice(price);
}

function setPrice(price)
{
	if ($("input[name='itemnum']").val() == 2)
	{
		price += 35.00;
	}
//	alert("Setting price to :" + price);
	$("#price").attr('value', price);
//		debug('Price set to :' + $("#price").attr('value'));
}

function setSKU(sku)
{
//	alert("Setting sku to :" + sku);
	$("#sku").attr('value', sku);
}

function showSizingForm()
{
	$('#sizing_form').show();
}

function hideSizingForm() 
{
	$('#sizing_form').hide();
}

function showLicensedOptions() 
{
	$('#licensed_options').show();
}

function hideLicensedOptions() 
{
	$('#licensed_options').hide();
}

function showPersonalMessage() 
{
	$('#personal_message').show();
}

function hidePersonalMessage() 
{
	$('#personal_message').hide();
}

function showHiddenValues()
{
	alert("itemnum: " + $("input[name='itemnum']").val() + "\n"
		+ "price: (" + $("#price").attr('name') + ") " + $("#price").val() + "\n"
		+ "sku: (" + $("#sku").attr('name') + ") " + $("#sku").val());
}