$(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(6.19, 4.53, 3.69, 3.42, 2.86, 2.53, 2.40, 2.29, 2.18);
	var arrPriceWhite2 = new Array(10.33, 7.00, 5.33, 4.78, 3.66, 2.77, 2.52, 2.39, 2.24);
	var arrPriceWhite3 = new Array(14.46, 9.46, 6.96, 6.13, 4.46, 3.01, 2.64, 2.49, 2.30);
	var arrPriceWhite4 = new Array(18.60, 11.93, 8.60, 7.49, 5.26, 3.25, 2.76, 2.59, 2.36);
	var arrPriceWhite5 = new Array(22.73, 14.39, 10.23, 8.84, 6.06, 3.49, 2.88, 2.69, 2.42);
	var arrPriceWhite6 = new Array(26.86, 16.86, 11.86, 10.19, 6.86, 3.73, 3.00, 2.79, 2.48);
	var arrPriceWhite7 = new Array(30.99, 19.33, 13.49, 11.55, 7.66, 3.97, 3.12, 2.89, 2.54);
	var arrPriceWhite8 = new Array(35.13, 21.79, 15.13, 12.90, 8.46, 4.21, 3.24, 2.99, 2.60);
	var arrPriceWhite = new Array(arrPriceWhite1, arrPriceWhite2, arrPriceWhite3, arrPriceWhite4, arrPriceWhite5, arrPriceWhite6, arrPriceWhite7, arrPriceWhite8);
	
	var arrPriceWhiteBack1 = new Array(4.64, 2.98, 2.14, 1.87, 1.31, 1.22, 0.97, 0.84, 0.69);
	var arrPriceWhiteBack2 = new Array(8.78, 5.45, 3.78, 3.23, 2.11, 1.22, 0.97, 0.84, 0.69);
	var arrPriceWhiteBack3 = new Array(12.91, 7.91, 5.41, 4.58, 2.91, 1.46, 1.09, 0.94, 0.75);
	var arrPriceWhiteBack4 = new Array(17.05, 10.38, 7.05, 5.94, 3.71, 1.70, 1.21, 1.04, 0.81);
	var arrPriceWhiteBack5 = new Array(21.18, 12.84, 8.68, 7.29, 4.51, 1.94, 1.33, 1.14, 0.87);
	var arrPriceWhiteBack6 = new Array(25.31, 15.31, 10.31, 8.64, 5.31, 2.18, 1.45, 1.24, 0.93);
	var arrPriceWhiteBack7 = new Array(29.44, 17.78, 11.94, 10.00, 6.11, 2.42, 1.57, 1.34, 0.99);
	var arrPriceWhiteBack8 = new Array(33.58, 20.24, 13.58, 11.35, 6.91, 2.66, 1.69, 1.44, 1.05);
	var arrPriceWhiteBack = new Array(arrPriceWhiteBack1, arrPriceWhiteBack2, arrPriceWhiteBack3, arrPriceWhiteBack4, arrPriceWhiteBack5, arrPriceWhiteBack6, arrPriceWhiteBack7, arrPriceWhiteBack8);
	
	var arrPriceGray1 = new Array(6.65, 4.99, 4.15, 3.88, 3.32, 2.99, 2.86, 2.75, 2.64);
	var arrPriceGray2 = new Array(10.79, 7.46, 5.79, 5.24, 4.12, 3.23, 2.98, 2.85, 2.70);
	var arrPriceGray3 = new Array(14.92, 9.92, 7.42, 6.59, 4.92, 3.47, 3.10, 2.95, 2.76);
	var arrPriceGray4 = new Array(19.06, 12.39, 9.06, 7.95, 5.72, 3.71, 3.22, 3.05, 2.82);
	var arrPriceGray5 = new Array(23.33, 14.85, 10.69, 9.30, 6.52, 3.95, 3.34, 3.15, 2.88);
	var arrPriceGray6 = new Array(27.32, 17.32, 12.32, 10.65, 7.32, 4.19, 3.46, 3.25, 2.94);
	var arrPriceGray7 = new Array(31.45, 19.79, 13.95, 12.01, 8.12, 4.43, 3.58, 3.35, 3.00);
	var arrPriceGray8 = new Array(22.26, 15.59, 12.26, 11.15, 8.92, 4.67, 3.70, 3.45, 3.06);
	var arrPriceGray = new Array(arrPriceGray1, arrPriceGray2, arrPriceGray3, arrPriceGray4, arrPriceGray5, arrPriceGray6, arrPriceGray7, arrPriceGray8);
	
	var arrPriceGrayBack1 = new Array(4.64, 2.98, 2.14, 1.87, 1.31, 0.98, 0.85, 0.74, 0.63);
	var arrPriceGrayBack2 = new Array(8.78, 5.45, 3.78, 3.23, 2.11, 1.22, 0.97, 0.84, 0.69);
	var arrPriceGrayBack3 = new Array(12.91, 7.91, 5.41, 4.58, 2.91, 1.46, 1.09, 0.94, 0.75);
	var arrPriceGrayBack4 = new Array(17.05, 10.38, 7.05, 5.94, 3.71, 1.70, 1.21, 1.04, 0.81);
	var arrPriceGrayBack5 = new Array(21.32, 12.84, 8.68, 7.29, 4.51, 1.94, 1.33, 1.14, 0.87);
	var arrPriceGrayBack6 = new Array(25.31, 15.31, 10.31, 8.64, 5.31, 2.18, 1.45, 1.24, 0.93);
	var arrPriceGrayBack7 = new Array(29.44, 17.78, 11.94, 10.00, 6.11, 2.42, 1.57, 1.34, 0.99);
	var arrPriceGrayBack8 = new Array(20.25, 13.58, 10.25, 9.14, 6.91, 2.66, 1.69, 1.44, 1.05);
	var arrPriceGrayBack = new Array(arrPriceGrayBack1, arrPriceGrayBack2, arrPriceGrayBack3, arrPriceGrayBack4, arrPriceGrayBack5, arrPriceGrayBack6, arrPriceGrayBack7, arrPriceGrayBack8);	
	
	var arrPriceColor1 = new Array(7.00, 5.34, 4.50, 4.23, 3.67, 3.32, 3.18, 3.08, 2.97);
	var arrPriceColor2 = new Array(11.14, 7.81, 6.14, 5.59, 4.47, 3.56, 3.30, 3.18, 3.03);
	var arrPriceColor3 = new Array(15.27, 10.27, 7.77, 6.94, 5.27, 3.80, 3.42, 3.28, 3.09);
	var arrPriceColor4 = new Array(19.41, 12.74, 9.41, 8.30, 6.07, 4.04, 3.54, 3.38, 3.15);
	var arrPriceColor5 = new Array(23.54, 15.20, 11.04, 9.65, 6.87, 4.28, 3.66, 3.48, 3.21);
	var arrPriceColor6 = new Array(27.67, 17.67, 12.67, 11.00, 7.67, 4.52, 3.78, 3.58, 3.27);
	var arrPriceColor7 = new Array(31.80, 20.14, 14.30, 12.36, 8.47, 4.76, 3.90, 3.68, 3.33);
	var arrPriceColor8 = new Array(35.94, 22.60, 15.94, 13.71, 9.27, 5.00, 4.02, 3.78, 3.39);
	var arrPriceColor = new Array(arrPriceColor1, arrPriceColor2, arrPriceColor3, arrPriceColor4, arrPriceColor5, arrPriceColor6, arrPriceColor7, arrPriceColor8);

	var arrPriceColorBack1 = new Array(4.78, 3.12, 2.28, 2.01, 1.45, 1.10, 0.95, 0.86, 0.75);
	var arrPriceColorBack2 = new Array(8.92, 5.59, 9.92, 3.37, 2.25, 1.34, 1.08, 0.96, 0.81);
	var arrPriceColorBack3 = new Array(13.05, 8.05, 5.55, 4.72, 3.05, 1.58, 1.20, 1.06, 0.87);
	var arrPriceColorBack4 = new Array(17.19, 10.52, 7.19, 6.08, 3.85, 1.82, 1.32, 1.16, 0.93);
	var arrPriceColorBack5 = new Array(12.32, 12.98, 8.82, 7.43, 4.65, 2.06, 1.44, 1.26, 0.99);
	var arrPriceColorBack6 = new Array(25.45, 15.45, 10.45, 8.78, 5.45, 2.30, 1.56, 1.36, 1.05);
	var arrPriceColorBack7 = new Array(29.58, 17.92, 12.08, 10.14, 6.25, 2.54, 1.68, 1.46, 1.11);
	var arrPriceColorBack8 = new Array(33.72, 20.38, 13.72, 11.49, 7.05, 2.78, 1.80, 1.56, 1.17);
	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;
	
	$(".qtyInput").each( function() {
		
		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);
	}
	
	$('#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));
}