$(document).ready(function(){
	$.validator.addMethod("phoneUS", function(phone_number, element) {
	    phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	}, "Please specify a valid phone number.");
	
	$.validator.addMethod("dateUS", function(value, element) {
		return value.match(/^((((0[13578])|([13578])|(1[02]))[\/](([1-9])|([0-2][0-9])|(3[01])))|(((0[469])|([469])|(11))[\/](([1-9])|([0-2][0-9])|(30)))|((2|02)[\/](([1-9])|([0-2][0-9]))))[\/]\d{4}$|^\d{4}$/);
	}, "Please enter a date in the format mm/dd/yyyy.");

});

function popPage(page, width, height)
{
	window.open(page, 'MidwestMerch', 'width=' + width + ', height=' + height + ', scrollbars=1, resize=1');
}

function shirtSelect(dir, shirt, id, color, hex)
{
	var colorElement = document.getElementById("hdColor");
	
	if (colorElement != null)
	{ 
		var arrColors = parseList(colorElement.value);
	
		if (shirt.src == dir + 'imgShirtMask.png')
		{
			shirt.src = dir + 'imgShirtCheckmark.png';
			
			if (arrColors == null)
			{
				arrColors = [shirt.id.replace("shirt", "")];
			}
			else
			{
				arrColors.push(shirt.id.replace("shirt", ""));
			}
			colorElement.value = arrColors.join(",");
			
			setDefaultColorSelect('add', id, color, hex);
		}
		else
		{
			shirt.src = dir + 'imgShirtMask.png';

			if (arrColors != null)
			{
				arrColors.splice(arrColors.indexOf(shirt.id.replace("shirt", "")), 1);
			}
			colorElement.value = arrColors.join(",");
			
			setDefaultColorSelect('rem', id, color, hex);
		}
	}
}

function setDefaultColorSelect(type, id, color, hex)
{
	var options = $('#ddDefaultColor').attr('options');

	if (type == 'add')
	{
		var newOptions = Array();
		for (var i=0; i<options.length; i++)
		{
			var text = $(options[i]).text();
			var val = $(options[i]).val();
			var rel = $(options[i]).attr('rel');
			
			newOptions.push('<option value="' + val + '" rel="' + rel + '">' + text + '</option>');
		}
		
		newOptions.push('<option value="' + id + '" rel="' + hex+ '">' + color + '</option>');
		newOptions.sort();
	
		var select = $('#ddDefaultColor');
		var selectedValue = $('#ddDefaultColor').val();
		
		$('option', select).remove();	
		for (var i=0; i<newOptions.length; i++)
		{
			$('#ddDefaultColor').append(newOptions[i]);
		}
		
		$('#ddDefaultColor').html($("option", $('#ddDefaultColor')).sort(function(a, b) {
			return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
		}));
	
		$('#ddDefaultColor').val(selectedValue);
	}
	else if (type == 'rem')
	{
		var selectedValue = $('#ddDefaultColor').val();
		$('#ddDefaultColor option[value=' + id + ']').remove();
		
		if (selectedValue != id)
		{
			$('#ddDefaultColor').val(selectedValue);
		}
		else
		{
			$('#ddDefaultColor').val(-1);
		}
	}
}

function parseList(str)
{
	if (str != "")
	{
		var arrStr = str.split(",");
		return arrStr;
	}
	
	return null;
}

function toggleShirtColor(selectElement)
{
	var selectedIndex = selectElement.selectedIndex;
	var hex = selectElement[selectedIndex].getAttribute("rel");
	
	var imageDiv = document.getElementById("shirtColor");
	if (imageDiv != null)
	{
		imageDiv.style.backgroundColor = "#" + hex;
	}

	var imageDiv = document.getElementById("bulkShirtColor");
	if (imageDiv != null)
	{
		imageDiv.style.backgroundColor = "#" + hex;
	}
}

function toggleReferralOther(ddReferral)
{
	divReferralOther = document.getElementById("divReferralOther");
	
	if (divReferralOther != null)
	{
		divReferralOther.className = (ddReferral[ddReferral.selectedIndex].text == "Other") ? "show" : "hide";
	}
}

function loadUrl(url)
{
	document.location.href = url;
}

function updateDetailPrice()
{
	var selectElement = $('#ddSize');
	var rel = $('#ddSize :selected').attr('rel');
	var arrRel = rel.split('|');
	
	if (arrRel[0] == 1)
	{
		$('#DetailPrice').html('$' + arrRel[1]);
	}
	
	var quantity = $('#tbxQuantity').val();
	$('#DetailPrice').html('$' + arrRel[1] * quantity);
}			

function makeItLookGood()
{
	var width = $('#hdImageWidth').val();
	var height = $('#hdImageHeight').val();
	
	switch($('#ddPlacement').val())
	{
		case '1':
			if (width > height)
			{
				$('#tbxWidth').val('4');
				$('#tbxHeight').val(((height * 4) / width).toFixed(2));
			}
			else if (height > width)
			{
				$('#tbxHeight').val('5');
				$('#tbxWidth').val(((width * 5) / height).toFixed(2));
			}
			else
			{
				$('#tbxWidth').val('4');
				$('#tbxHeight').val('4');
			}
			break;
			
		case '2':
			if (width > height)
			{
				$('#tbxWidth').val('4');
				$('#tbxHeight').val(((height * 4) / width).toFixed(2));
			}
			else if (height > width)
			{
				$('#tbxHeight').val('5');
				$('#tbxWidth').val(((width * 5) / height).toFixed(2));
			}
			else
			{
				$('#tbxWidth').val('4');
				$('#tbxHeight').val('4');
			}
			break;
			
		case '3':
			if (width > height)
			{
				$('#tbxWidth').val('4');
				$('#tbxHeight').val(((height * 4) / width).toFixed(2));
			}
			else if (height > width)
			{
				$('#tbxHeight').val('6');
				$('#tbxWidth').val(((width * 6) / height).toFixed(2));
			}
			else
			{
				$('#tbxWidth').val('4');
				$('#tbxHeight').val('4');
			}
			break;
			
		case '4':
			if (width > height)
			{
				$('#tbxWidth').val('4');
				$('#tbxHeight').val(((height * 4) / width).toFixed(2));
			}
			else if (height > width)
			{
				$('#tbxHeight').val('6');
				$('#tbxWidth').val(((width * 6) / height).toFixed(2));
			}
			else
			{
				$('#tbxWidth').val('4');
				$('#tbxHeight').val('4');
			}
			break;
			
		case '5':
			if (width > height)
			{
				$('#tbxWidth').val('10');
				$('#tbxHeight').val(((height * 10) / width).toFixed(2));
			}
			else if (height > width)
			{
				$('#tbxHeight').val('12');
				$('#tbxWidth').val(((width * 12) / height).toFixed(2));
			}
			else
			{
				$('#tbxWidth').val('10');
				$('#tbxHeight').val('10');
			}
			break;
	}
	
	updateShirtSizePreview();
}

function updateTotalPrice()
{
	var oldTotal = Number($('#subTotal').html());
	var shipping = Number($('#ddShipping').val());
	
	var shippingText = $('#ddShipping :selected').text();
	$('#hdShippingName').val("SHIPPING: " + shippingText);
	$('#hdShippingValue').val(shipping);
	
	if (isNumeric(shipping))
	{
		var newTotal = oldTotal + shipping;
		
		$('#totalPrice').html(newTotal.toFixed(2));
		
		$("div[rel='checkoutSubmitControls']").removeClass("hide").addClass("show");
		$("div[rel='checkoutCall']").removeClass("show").addClass("hide");
	}
	else
	{
		$("div[rel='checkoutSubmitControls']").removeClass("show").addClass("hide");
		$("div[rel='checkoutCall']").removeClass("hide").addClass("show");
	}
}

function updateBulkShipping()
{
	var shipping = Number($('#ddShipping').val());
	
	var shippingText = $('#ddShipping :selected').text();
	$('#hdShippingName').val("SHIPPING: " + shippingText);
	$('#hdShippingValue').val(shipping);	
}

function updateBulkInfo()
{
	var arrPriceWhite1 = new Array(7.54, 5.88, 5.04, 4.77, 4.21, 3.88, 3.75, 3.64, 3.53);
	var arrPriceWhite2 = new Array(11.68, 8.35, 6.68, 6.13, 5.01, 4.12, 3.87, 3.74, 3.59);
	var arrPriceWhite3 = new Array(15.81, 10.81, 8.31, 7.48, 5.81, 4.36, 3.99, 3.84, 3.65);
	var arrPriceWhite4 = new Array(19.95, 13.28, 9.95, 8.84, 6.61, 4.60, 4.11, 3.94, 3.71);
	var arrPriceWhite5 = new Array(24.08, 15.74, 11.58, 10.19, 7.41, 4.84, 4.23, 4.04, 3.77);
	var arrPriceWhite6 = new Array(28.21, 18.21, 13.21, 11.54, 8.21, 5.08, 4.35, 4.14, 3.83);
	var arrPriceWhite7 = new Array(32.34, 20.68, 14.84, 12.90, 9.01, 5.32, 4.47, 4.24, 3.89);
	var arrPriceWhite8 = new Array(36.48, 23.14, 16.48, 14.25, 9.81, 5.56, 4.59, 4.34, 3.95);
	var arrPriceWhite = new Array(arrPriceWhite1, arrPriceWhite2, arrPriceWhite3, arrPriceWhite4, arrPriceWhite5, arrPriceWhite6, arrPriceWhite7, arrPriceWhite8);
	
	var arrPriceWhiteBack1 = new Array(5.49, 3.83, 2.99, 2.72, 2.16, 2.07, 1.82, 1.69, 1.54);
	var arrPriceWhiteBack2 = new Array(9.63, 6.30, 4.63, 4.08, 2.96, 2.07, 1.82, 1.69, 1.54);
	var arrPriceWhiteBack3 = new Array(13.76, 8.76, 6.26, 5.43, 3.76, 2.31, 1.94, 1.79, 1.60);
	var arrPriceWhiteBack4 = new Array(17.90, 11.23, 7.90, 6.79, 4.56, 2.55, 2.06, 1.89, 1.66);
	var arrPriceWhiteBack5 = new Array(22.03, 13.69, 9.53, 8.14, 5.36, 2.79, 2.18, 1.99, 1.72);
	var arrPriceWhiteBack6 = new Array(26.16, 16.16, 11.16, 9.49, 6.16, 3.03, 2.30, 2.09, 1.78);
	var arrPriceWhiteBack7 = new Array(30.29, 18.63, 12.79, 10.85, 6.96, 3.27, 2.42, 2.19, 1.84);
	var arrPriceWhiteBack8 = new Array(34.43, 21.09, 14.43, 12.20, 7.76, 3.51, 2.54, 2.29, 1.90);
	var arrPriceWhiteBack = new Array(arrPriceWhiteBack1, arrPriceWhiteBack2, arrPriceWhiteBack3, arrPriceWhiteBack4, arrPriceWhiteBack5, arrPriceWhiteBack6, arrPriceWhiteBack7, arrPriceWhiteBack8);
	
	var arrPriceGray1 = new Array(8.00, 6.34, 5.50, 5.23, 4.67, 4.34, 4.21, 4.10, 3.99);
	var arrPriceGray2 = new Array(12.14, 8.81, 7.14, 6.59, 5.47, 4.58, 4.33, 4.20, 4.05);
	var arrPriceGray3 = new Array(16.27, 11.27, 8.77, 7.94, 6.27, 4.82, 4.45, 4.30, 4.11);
	var arrPriceGray4 = new Array(20.41, 13.74, 10.41, 9.30, 7.07, 5.06, 4.57, 4.40, 4.17);
	var arrPriceGray5 = new Array(24.68, 16.20, 12.04, 10.65, 7.87, 5.30, 4.69, 4.50, 4.23);
	var arrPriceGray6 = new Array(28.67, 18.67, 13.67, 12.00, 8.67, 5.54, 4.81, 4.60, 4.29);
	var arrPriceGray7 = new Array(32.80, 21.14, 15.30, 13.36, 9.47, 5.78, 4.93, 4.70, 4.35);
	var arrPriceGray8 = new Array(23.61, 16.94, 13.61, 12.50, 10.27, 6.02, 5.05, 4.80, 4.41);
	var arrPriceGray = new Array(arrPriceGray1, arrPriceGray2, arrPriceGray3, arrPriceGray4, arrPriceGray5, arrPriceGray6, arrPriceGray7, arrPriceGray8);
	
	var arrPriceGrayBack1 = new Array(5.49, 3.83, 2.99, 2.72, 2.16, 1.83, 1.70, 1.59, 1.48);
	var arrPriceGrayBack2 = new Array(9.63, 6.30, 4.63, 4.08, 2.96, 2.07, 1.82, 1.69, 1.54);
	var arrPriceGrayBack3 = new Array(13.76, 8.76, 6.26, 5.43, 3.76, 2.31, 1.94, 1.79, 1.60);
	var arrPriceGrayBack4 = new Array(17.90, 11.23, 7.90, 6.79, 4.56, 2.55, 2.06, 1.89, 1.66);
	var arrPriceGrayBack5 = new Array(22.17, 13.69, 9.53, 8.14, 5.36, 2.79, 2.18, 1.99, 1.72);
	var arrPriceGrayBack6 = new Array(26.16, 16.16, 11.16, 9.49, 6.16, 3.03, 2.30, 2.09, 1.78);
	var arrPriceGrayBack7 = new Array(30.29, 18.63, 12.79, 10.85, 6.96, 3.27, 2.42, 2.19, 1.84);
	var arrPriceGrayBack8 = new Array(21.10, 14.43, 11.10, 9.99, 7.76, 3.51, 2.54, 2.29, 1.90);
	var arrPriceGrayBack = new Array(arrPriceGrayBack1, arrPriceGrayBack2, arrPriceGrayBack3, arrPriceGrayBack4, arrPriceGrayBack5, arrPriceGrayBack6, arrPriceGrayBack7, arrPriceGrayBack8);
	
	var arrPriceColor1 = new Array(8.35, 6.69, 5.85, 5.58, 5.02, 4.67, 4.53, 4.43, 4.32);
	var arrPriceColor2 = new Array(12.49, 9.16, 7.49, 6.94, 5.82, 4.91, 4.65, 4.53, 4.38);
	var arrPriceColor3 = new Array(16.62, 11.62, 9.12, 8.29, 6.62, 5.15, 4.77, 4.63, 4.44);
	var arrPriceColor4 = new Array(20.49, 14.09, 10.76, 9.65, 7.42, 5.39, 4.89, 4.73, 4.50);
	var arrPriceColor5 = new Array(24.89, 16.55, 12.39, 11.00, 8.22, 5.63, 5.01, 4.83, 4.56);
	var arrPriceColor6 = new Array(29.02, 19.02, 14.02, 12.35, 9.02, 5.87, 5.13, 4.93, 4.62);
	var arrPriceColor7 = new Array(33.15, 21.49, 15.65, 13.71, 9.82, 6.11, 5.25, 5.03, 4.68);
	var arrPriceColor8 = new Array(37.29, 23.95, 17.29, 15.06, 10.62, 6.35, 5.37, 5.13, 4.74);
	var arrPriceColor = new Array(arrPriceColor1, arrPriceColor2, arrPriceColor3, arrPriceColor4, arrPriceColor5, arrPriceColor6, arrPriceColor7, arrPriceColor8);

	var arrPriceColorBack1 = new Array(5.63, 3.97, 3.13, 2.86, 2.3, 1.95, 1.80, 1.71, 1.60);
	var arrPriceColorBack2 = new Array(9.77, 6.44, 10.77, 4.22, 3.10, 2.19, 1.93, 1.81, 1.66);
	var arrPriceColorBack3 = new Array(13.90, 8.90, 6.40, 5.57, 3.90, 2.43, 2.05, 1.91, 1.72);
	var arrPriceColorBack4 = new Array(18.04, 11.37, 8.04, 6.93, 4.70, 2.67, 2.17, 2.01, 1.78);
	var arrPriceColorBack5 = new Array(13.17, 13.83, 9.67, 8.28, 5.50, 2.51, 2.29, 2.11, 1.84);
	var arrPriceColorBack6 = new Array(26.30, 16.30, 11.30, 9.63, 6.30, 3.15, 2.41, 2.21, 1.90);
	var arrPriceColorBack7 = new Array(30.43, 18.77, 12.93, 10.99, 7.10, 3.39, 2.53, 2.31, 1.96);
	var arrPriceColorBack8 = new Array(34.57, 21.23, 14.57, 12.34, 7.90, 3.63, 2.65, 2.41, 2.02);
	var arrPriceColorBack = new Array(arrPriceColorBack1, arrPriceColorBack2, arrPriceColorBack3, arrPriceColorBack4, arrPriceColorBack5, arrPriceColorBack6, arrPriceColorBack7, arrPriceColorBack8);

	var color = $('#ddColor :selected').text();
	var colors = $('#hdColors').val();
	if (colors == null)
	{
		colors = $('#ddColorsFront').val();
	}
	
	var colorsBack = $('#ddColorsBack').val();
	
	var totalShirts = 0;
	var totalXXL = 0;
	var totalXXXL = 0;
	
	$(".qtyInput").each( function() {
		
		var shortname = $(this).attr('rel');
		if (shortname == 'XXL') 
		{
			totalXXL = $(this).val();
		}
		if (shortname == 'XXXL') 
		{
			totalXXXL = $(this).val();
		}
		
		if ($(this).val() != null && $(this).val() != '')
		{
			if (isNumeric($(this).val()))
			{
				totalShirts += Number($(this).val());
			}
			else
			{
				$(this).val('');
			}
		}
	});
	
	$('#totalShirts').html(totalShirts);
	
	decTotalPrice = 0;
	if (color != null && colors != null)
	{
		if (color.toUpperCase() == 'WHITE')
		{
			if (totalShirts < 12)
			{
				decTotalPrice = arrPriceWhite[(Number(colors) - 1)][0] * totalShirts;
			} 
			else if (totalShirts < 24)
			{
				decTotalPrice = arrPriceWhite[(Number(colors) - 1)][1] * totalShirts;
			} 
			else if (totalShirts < 36)
			{
				decTotalPrice = arrPriceWhite[(Number(colors) - 1)][2] * totalShirts;
			} 
			else if (totalShirts < 72)
			{
				decTotalPrice = arrPriceWhite[(Number(colors) - 1)][3] * totalShirts;
			} 
			else if (totalShirts < 350)
			{
				decTotalPrice = arrPriceWhite[(Number(colors) - 1)][4] * totalShirts;
			} 
			else if (totalShirts < 500)
			{
				decTotalPrice = arrPriceWhite[(Number(colors) - 1)][5] * totalShirts;
			} 
			else if (totalShirts < 750)
			{
				decTotalPrice = arrPriceWhite[(Number(colors) - 1)][6] * totalShirts;
			} 
			else if (totalShirts < 1000)
			{
				decTotalPrice = arrPriceWhite[(Number(colors) - 1)][7] * totalShirts;
			} 
			else if (totalShirts >= 1000)
			{
				decTotalPrice = arrPriceWhite[(Number(colors) - 1)][8] * totalShirts;
			} 
		}
		else if (color.toUpperCase() == 'ASH GRAY')
		{
			if (totalShirts < 12)
			{
				decTotalPrice = arrPriceGray[(Number(colors) - 1)][0] * totalShirts;
			} 
			else if (totalShirts < 24)
			{
				decTotalPrice = arrPriceGray[(Number(colors) - 1)][1] * totalShirts;
			} 
			else if (totalShirts < 36)
			{
				decTotalPrice = arrPriceGray[(Number(colors) - 1)][2] * totalShirts;
			} 
			else if (totalShirts < 72)
			{
				decTotalPrice = arrPriceGray[(Number(colors) - 1)][3] * totalShirts;
			} 
			else if (totalShirts < 350)
			{
				decTotalPrice = arrPriceGray[(Number(colors) - 1)][4] * totalShirts;
			} 
			else if (totalShirts < 500)
			{
				decTotalPrice = arrPriceGray[(Number(colors) - 1)][5] * totalShirts;
			} 
			else if (totalShirts < 750)
			{
				decTotalPrice = arrPriceGray[(Number(colors) - 1)][6] * totalShirts;
			} 
			else if (totalShirts < 1000)
			{
				decTotalPrice = arrPriceGray[(Number(colors) - 1)][7] * totalShirts;
			} 
			else if (totalShirts >= 1000)
			{
				decTotalPrice = arrPriceGray[(Number(colors) - 1)][8] * totalShirts;
			} 
		}
		else
		{
			if (totalShirts < 12)
			{
				decTotalPrice = arrPriceColor[(Number(colors) - 1)][0] * totalShirts;
			} 
			else if (totalShirts < 24)
			{
				decTotalPrice = arrPriceColor[(Number(colors) - 1)][1] * totalShirts;
			} 
			else if (totalShirts < 36)
			{
				decTotalPrice = arrPriceColor[(Number(colors) - 1)][2] * totalShirts;
			} 
			else if (totalShirts < 72)
			{
				decTotalPrice = arrPriceColor[(Number(colors) - 1)][3] * totalShirts;
			} 
			else if (totalShirts < 350)
			{
				decTotalPrice = arrPriceColor[(Number(colors) - 1)][4] * totalShirts;
			} 
			else if (totalShirts < 500)
			{
				decTotalPrice = arrPriceColor[(Number(colors) - 1)][5] * totalShirts;
			} 
			else if (totalShirts < 750)
			{
				decTotalPrice = arrPriceColor[(Number(colors) - 1)][6] * totalShirts;
			} 
			else if (totalShirts < 1000)
			{
				decTotalPrice = arrPriceColor[(Number(colors) - 1)][7] * totalShirts;
			} 
			else if (totalShirts >= 1000)
			{
				decTotalPrice = arrPriceColor[(Number(colors) - 1)][8] * totalShirts;
			} 
		}
	}
		
	if (color != null && colorsBack != null && colorsBack != 0)
	{
		if (color.toUpperCase() == 'WHITE')
		{
			if (totalShirts < 12)
			{
				decTotalPrice += arrPriceWhiteBack[(Number(colorsBack) - 1)][0] * totalShirts;
			} 
			else if (totalShirts < 24)
			{
				decTotalPrice += arrPriceWhiteBack[(Number(colorsBack) - 1)][1] * totalShirts;
			} 
			else if (totalShirts < 36)
			{
				decTotalPrice += arrPriceWhiteBack[(Number(colorsBack) - 1)][2] * totalShirts;
			} 
			else if (totalShirts < 72)
			{
				decTotalPrice += arrPriceWhiteBack[(Number(colorsBack) - 1)][3] * totalShirts;
			} 
			else if (totalShirts < 350)
			{
				decTotalPrice += arrPriceWhiteBack[(Number(colorsBack) - 1)][4] * totalShirts;
			} 
			else if (totalShirts < 500)
			{
				decTotalPrice += arrPriceWhiteBack[(Number(colorsBack) - 1)][5] * totalShirts;
			} 
			else if (totalShirts < 750)
			{
				decTotalPrice += arrPriceWhiteBack[(Number(colorsBack) - 1)][6] * totalShirts;
			} 
			else if (totalShirts < 1000)
			{
				decTotalPrice += arrPriceWhiteBack[(Number(colorsBack) - 1)][7] * totalShirts;
			} 
			else if (totalShirts >= 1000)
			{
				decTotalPrice += arrPriceWhiteBack[(Number(colorsBack) - 1)][8] * totalShirts;
			} 
		}
		else if (color.toUpperCase() == 'ASH GRAY')
		{
			if (totalShirts < 12)
			{
				decTotalPrice += arrPriceGrayBack[(Number(colorsBack) - 1)][0] * totalShirts;
			} 
			else if (totalShirts < 24)
			{
				decTotalPrice += arrPriceGrayBack[(Number(colorsBack) - 1)][1] * totalShirts;
			} 
			else if (totalShirts < 36)
			{
				decTotalPrice += arrPriceGrayBack[(Number(colorsBack) - 1)][2] * totalShirts;
			} 
			else if (totalShirts < 72)
			{
				decTotalPrice += arrPriceGrayBack[(Number(colorsBack) - 1)][3] * totalShirts;
			} 
			else if (totalShirts < 350)
			{
				decTotalPrice += arrPriceGrayBack[(Number(colorsBack) - 1)][4] * totalShirts;
			} 
			else if (totalShirts < 500)
			{
				decTotalPrice += arrPriceGrayBack[(Number(colorsBack) - 1)][5] * totalShirts;
			} 
			else if (totalShirts < 750)
			{
				decTotalPrice += arrPriceGrayBack[(Number(colorsBack) - 1)][6] * totalShirts;
			} 
			else if (totalShirts < 1000)
			{
				decTotalPrice += arrPriceGrayBack[(Number(colorsBack) - 1)][7] * totalShirts;
			} 
			else if (totalShirts >= 1000)
			{
				decTotalPrice += arrPriceGrayBack[(Number(colorsBack) - 1)][8] * totalShirts;
			} 
		}
		else
		{
			if (totalShirts < 12)
			{
				decTotalPrice += arrPriceColorBack[(Number(colorsBack) - 1)][0] * totalShirts;
			} 
			else if (totalShirts < 24)
			{
				decTotalPrice += arrPriceColorBack[(Number(colorsBack) - 1)][1] * totalShirts;
			} 
			else if (totalShirts < 36)
			{
				decTotalPrice += arrPriceColorBack[(Number(colorsBack) - 1)][2] * totalShirts;
			} 
			else if (totalShirts < 72)
			{
				decTotalPrice += arrPriceColorBack[(Number(colorsBack) - 1)][3] * totalShirts;
			} 
			else if (totalShirts < 350)
			{
				decTotalPrice += arrPriceColorBack[(Number(colorsBack) - 1)][4] * totalShirts;
			} 
			else if (totalShirts < 500)
			{
				decTotalPrice += arrPriceColorBack[(Number(colorsBack) - 1)][5] * totalShirts;
			} 
			else if (totalShirts < 750)
			{
				decTotalPrice += arrPriceColorBack[(Number(colorsBack) - 1)][6] * totalShirts;
			} 
			else if (totalShirts < 1000)
			{
				decTotalPrice += arrPriceColorBack[(Number(colorsBack) - 1)][7] * totalShirts;
			} 
			else if (totalShirts >= 1000)
			{
				decTotalPrice += arrPriceColorBack[(Number(colorsBack) - 1)][8] * totalShirts;
			} 
		}
	}

	if ($('#cbxAmericanApparel:checked').length > 0)
	{
		decTotalPrice = decTotalPrice + (5 * totalShirts);
	}
	
	if (totalXXL > 0)
	{
		decTotalPrice = decTotalPrice + new Number(totalXXL*2);
	}
	
	if (totalXXXL > 0)
	{
		decTotalPrice = decTotalPrice + new Number(totalXXXL*2);
	}
	
	$('#totalPrice').html(addCommas(decTotalPrice.toFixed(2)));

	if (totalShirts >= 6)
	{
		$('#divMinOrder').attr('class', 'hide');
		$('#divSubmitButton').attr('class', 'show');
	}
	else
	{
		$('#divMinOrder').attr('class', 'show');
		$('#divSubmitButton').attr('class', 'hide');
	}
}

function isNumeric(input)
{	
   	return (input - 0) == input && input.toString().length > 0;
}

function deleteShirt(url)
{
	if (window.confirm("Are you sure you want to delete this shirt?  The cannot be undone."))
	{
		document.location.href = url;
	}
}

function selectAllText(el)
{
	var element = document.getElementById(el);
	element.focus();
	element.select();
}

function showInstructions(div)
{
	$('#' + div).attr("class", "instructions show");
}

function hideInstructions(div)
{
	$('#' + div).attr("class", "instructions hide");
}

function toggleMyspaceType()
{
	var type = $('#ddMyspaceType').val();
	
	if (type == 1)
	{
		$('#myspace1').removeClass("hide").addClass("show");
		$('#myspace2').removeClass("show").addClass("hide");
	}
	else if (type == 2)
	{
		$('#myspace1').removeClass("show").addClass("hide");
		$('#myspace2').removeClass("hide").addClass("show");
	}	
	
	$('#MyspaceInstructions1').removeClass("show").addClass("hide");
	$('#MyspaceInstructions2').removeClass("show").addClass("hide");
}

function updateShirtUploadPreview()
{
	var placement = $('#ddPlacement').val();
	if (placement > 0 && placement <=5)
	{
		$('#PreviewImage').attr('class', 'center preview321_' + placement);
	}
}

function updateShirtSizePreview()
{
	var width = $('#tbxWidth').val();
	var height = $('#tbxHeight').val();
	
	var oneInch = 14.32;
	var designWidth = Math.round(width * oneInch);
	var designHeight = Math.round(height * oneInch);

	var image = $('#ShirtPreviewImage').attr('src');
	
	var placement = $('#ddPlacement').val();
	var imageHtml = '<div class="center preview321_' + placement + '" id="PreviewImage">';
	imageHtml += '<img src="' + image + '" width="' + designWidth + '" height="' + designHeight + '" id="ShirtPreviewImage" />';
	imageHtml += '</div>';
	
	$('#ImagePreview').html(imageHtml);
}

function updateInternationalShipping(qty)
{
	var country = $('#ddCountry').val();
	var countryText = $('#ddCountry option:selected').text();
	
	var shippingPrice = 0;
	var shippingPriceQty1 = 0;
	var shippingMulitplier = 8;
	var shippingMulitplierPrice = 0;
	var isDomestic = true;
	
	switch(countryText)
	{
		case 'United States':
			isDomestic = true;
			break;
		case 'Canada':
		case 'Mexico':
			isDomestic = false;
			shippingPriceQty1 = 12;
			shippingMulitplierPrice = 30;
			break;
		
		default:
			isDomestic = false;
			shippingPriceQty1 = 15;
			shippingMulitplierPrice = 45;
			break;
	}
	
	switch (qty)
	{
		case 1:
			shippingPrice = shippingPriceQty1;
			break;
			
		default:
			var multiplier = Math.ceil(qty / shippingMulitplier);
			shippingPrice = shippingMulitplierPrice * multiplier;
			break;
	}
	
	var oldTotal = Number($('#subTotal').html());
	var newTotal = oldTotal + shippingPrice;
	$('#totalPrice').html(newTotal.toFixed(2));

	if (isDomestic)
	{
		$('#InternationalShipping').removeClass('show').addClass('hide');
		$('#DomesticShipping').removeClass('hide').addClass('show');
		
		$('#hdShippingName').val('SHIPPING: USPS First Class - FREE');
		$('#hdShippingValue').val(0);
	}
	else
	{
		$('#ddIntlShipping').html('');
		$('<option value="' + shippingPrice + '">USPS Priority Mail - $' + shippingPrice + '</option>').appendTo('#ddIntlShipping');
		$('#InternationalShipping').removeClass('hide').addClass('show');
		$('#DomesticShipping').removeClass('show').addClass('hide');
		
		$('#hdShippingName').val('SHIPPING: USPS Priority Mail - $' + shippingPrice);
		$('#hdShippingValue').val(shippingPrice);
	}
}

function updateBulkCountry()
{
	var country = $('#ddCountry').val();
	var countryText = $('#ddCountry option:selected').text();
	
	switch(countryText)
	{
		case 'United States':
			isDomestic = true;
			break;

		default:
			isDomestic = false;
			break;
	}
	
	if (isDomestic)
	{
		$('#ZipCode').removeClass('hide').addClass('show');
		
		$("#bulkForm").validate({
			rules: {
		    	tbxZip: { required: true }
	    	},
		    errorPlacement: function(error, element) {
				$('#zipError').html(error);
			}		
		});	
	}
	else
	{	
		$('#ZipCode').removeClass('show').addClass('hide');
		$("#tbxZip").rules('remove');
		$("#tbxZip").val('');
	}
}

function changeShirtColor()
{
	var hex = $('#ddDefaultColor option:selected').attr('rel');
	$('#ImagePreview').css('background-color', '#' + hex);	
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function rgbToHex(rgb) {
	if (rgb.match(/^#[0-9A-Fa-f]{6}$/)) {
   		return rgb;
   	}
   	
   	var rgbvals = /rgb\((.+),(.+),(.+)\)/i.exec(rgb);
   	if (!rgbvals) {
   		return rgb;
   	}
   	
   	var rval = parseInt(rgbvals[1]);
  	var gval = parseInt(rgbvals[2]);
  	var bval = parseInt(rgbvals[3]);
  	var pad = function(value) {
  		return (value.length < 2 ? '0' : '') + value;
  	};
  
	return '#' + pad(rval.toString(16)) + pad(gval.toString(16)) + pad(bval.toString(16));
}
