var foreground = "";
function selectBase(mat)
{
	if (foreground != '') $("#" + foreground).hide();
	$("#" + mat).show();
	foreground = mat;
}

var foregroundLogo = "";
function selectLogo(logo_id)
{
	if (foregroundLogo != '') $("#" + foregroundLogo).hide();
	$("#" + logo_id).show();
	foregroundLogo = logo_id;
}

function uncheckAll(nameStr)
{
	$("input[name='" + nameStr + "']").each(function ()
	{
		$(this).attr('checked','');
	});
}

function setCheckboxLimit(nameStr, limit)
{
	var boxCollection = $("input[name='" + nameStr + "']");
	boxCollection.click(function ()
	{
		//	uncheck box if over limit
		if (boxCollection.filter(":checked").length > limit)
		{
			$(this).attr('checked','');
		}
	});
}

function removeCheckboxLimits(nameStr)
{
	$("input[name='" + nameStr + "']").unbind('click');
}

function attachLogoColorSizeLimit()
{
	$("#customsize select").change(function (e)
	{
		if (overCustomSizeLimit())
		{
			removeCheckboxLimits('logocolor[]');
			uncheckAll('logocolor[]');
			setCheckboxLimit('logocolor[]', 1);
		}
		else
		{
			removeCheckboxLimits('logocolor[]');
			setCheckboxLimit('logocolor[]', 4);
		}
	});
}

function overCustomSizeLimit()
{
	return (($("select[name='feet_w']").val() > 4
				|| ($("select[name='feet_w']").val() == 4 
					&& $("select[name='inches_w']").val() >= 1)) 
			|| ($("select[name='feet_h']").val() > 6
				|| ($("select[name='feet_h']").val() == 6 
					&& $("select[name='inches_h']").val() >= 1))
		? true 
		: false);
}

function attachCustomSizeReveal()
{
	$("select[name='matsize']").change(function (e)
	{
		if ($("select[name='matsize']").val() == 'custom')
		{
			$('#customsize').toggle();
		}
		else
		{
			$('#customsize').hide();
		}
	});
}

function attachFlockedMatReveal()
{
	$("select[name='basemat']").change(function (e)
	{
		var selection = $("select[name='basemat']").val();
		if (selection != "choosebase")
		{
			selectBase(selection);
		}
	});
}

function attachAFEBaseMatReveal()
{
	$("select[name='basemat']").change(function (e)
	{
		selectBase(($("select[name='basemat']").val() == 'Footlover'
			? 'footlover' 
			: 'comfortking'));
	});
}

function attachInlayBaseMatReveal()
{
	$("select[name='basemat']").change(function ()
	{
		selectBase($("select[name='basemat']").val());
		
		if (foregroundLogo != ""
			&& $("select[name='logo_style']").val() == 'A')
		{
			selectLogo(foreground + '_logo');
		}
	});
}

function attachInlayLogoReveal()
{
	$("select[name='logo_style']").change(function ()
	{
		var style = $("select[name='logo_style']").val();
		if (style == 'A')
		{
			if (foreground && foreground != "")
			{
				selectLogo(foreground + '_logo');
			}
			else
			{
				alert('Please select a Base Mat first!');
				$("select[name='logo_style']").val('chooselogo');
			}
		}
		else if (style == 'B')
		{
			selectLogo('logo_styleb');
		}
	});
}

function validateCustomerInfo()
{
	return ($("input[name='employee_name']").val() != "" 
			&& $("input[name='company_name']").val() != "" 
			&& $("input[name='phone']").val() != "" 
			&& $("input[name='email']").val() != "" 
		? true 
		: false);
}

function validateMatSize()
{
	return ($("select[name='matsize']").val() != "choosesize" 
		? true 
		: false);
}

function validateDualMatSize()
{
	return ($("select[name='matsize_wp']").val() != "choosesize" 
			|| $("select[name='matsize_ft']").val() != "choosesize"
		? true 
		: false);
}


function validateInputGroup(nameStr)
{
	return ($("input[name='" + nameStr + "']:checked").length > 0 
		? true 
		: false);
}

function validateSubmission(validateFunc, msg)
{
	$('form').submit(function ()
	{
		if (validateFunc())
		{
		//	alert('Looking good!');
		//	return false;
			return true;	
		}
		else
		{
			alert(msg || "One or more required fields must be filled to continue!");
			return false;
		}
	});
}