//=============================================================================
CProduct = {};

//-----------------------------------------------------------------------------
CProduct.ToggleCheckbox = function(ID, Type, NormalID, ImprintID, NumColors) {
	if ($('#'+ID).attr('class') == 'CheckboxCheckedSpan') return;	
	
	// Uncheck all other options
	$('div[class^=Checkbox]').each(function(){
		if (this.id.indexOf(Type + 'Option') >= 0) {
			this.className = 'CheckboxUncheckedSpan';
			$('#'+str_replace('Span', '', this.id)).attr('checked', '');
		}
	});

	// Check the selected option
	$('#'+ID).removeClass('CheckboxUncheckedSpan');
	$('#'+ID).addClass('CheckboxCheckedSpan');
	var InputID = str_replace('Span', '', $('#'+ID).attr("id"));
	$('#'+InputID).attr('checked', true);
	
	// Populate other option lists
	if (Type == 'Normal') {
		CProduct.NormalID = $('#'+InputID).attr('value');

		CProduct.UpdateImprintOptions($('#'+InputID).attr('value'));
		CProduct.UpdateProductColorChoices($('#'+InputID).attr('value'), NumColors);
	} else if (Type == 'Imprint') {
		CProduct.ImprintID = $('#'+InputID).attr('rel');
		if (NumColors > 0) CProduct.UpdateColorOptions(NormalID, $('#'+InputID).attr('rel'));
	} else if (Type == 'Color') {
		CProduct.NumColors = NumColors;
		CProduct.UpdateImprintColorChoices(ImprintID, NumColors);
		
		// Update Grid
		CProduct.UpdateGrid();
		
		// Update All Prices
		CProduct.UpdateAllPrices();
	}

}

//-----------------------------------------------------------------------------
CProduct.UpdateImprintOptions = function(ID) {
	var Parms = {};
	Parms["ID"] = ID;
	
	CAJAX.Add('Products', 'Module', 'GetOptionOptions', Parms, function(Code, Content) {
		if(Code == 1) {
			Content = JSON.parse(Content);
		
			var ImprintList = '';
			for (var key in Content) {
				if(Content[key]['Type'] != 'Pantone Color Match') {
					if (!Default) var Default = str_replace(' ', '', Content[key]['Type']);
					if (Content[key]['Type'] == 'Four Color Process') { var Value = 1; } else { var Value = 0; }
					
					ImprintList += "<input type='checkbox' name='ImprintOption" + str_replace(' ', '', Content[key]['Type']) + "' id='ImprintOption" + str_replace(' ', '', Content[key]['Type']) + "' class='Checkbox' value='" + Value + "' rel='" + key + "'>";
					ImprintList += "<div id='ImprintOption" + str_replace(' ', '', Content[key]['Type']) + "Span' class='CheckboxUncheckedSpan' onclick=\"CProduct.ToggleCheckbox(this.id, 'Imprint', " + ID + ", '', " + Value + ");\"></div>";
					ImprintList += "<div class='CheckboxLabel'>" + Content[key]['Type'] + "</div><br>";
				}
			}

			CProduct.ImprintID = 0;
			
			if (ImprintList == '') {
				$('#ImprintOptionList').html(CProduct.NotApplicable);
				for (var i=1; i<=10; i++) {
					if (i == 1) {
						var ColorList = CProduct.NotApplicable;
					} else {
						var ColorList = "";
					}
					$('#ProductColor' + i).html(ColorList);
				}
				$('#ColorOptionList').html(CProduct.NotApplicable);
				for (var i=1; i<=10; i++) {
					if (i == 1) {
						var ColorList = CProduct.NotApplicable;
					} else {
						var ColorList = "";
					}
					$('#ImprintColor' + i).html(ColorList);
				}
				// Update Grid
				CProduct.UpdateGrid();
				
				// Update All Prices
				CProduct.UpdateAllPrices();
			} else {
				$('#ImprintOptionList').html(ImprintList);
				if (Default == 'FourColorProcess') {
					var NumColors = 0;
					$('#ColorOptionList').html(CProduct.NotApplicable);
					for (var i=1; i<=10; i++) {
						if (i == 1) {
							var ColorList = CProduct.NotApplicable;
						} else {
							var ColorList = "";
						}
						$('#ImprintColor' + i).html(ColorList);
					}
					// Update Grid
					CProduct.UpdateGrid();
					
					// Update All Prices
					CProduct.UpdateAllPrices();
				} else {
					var NumColors = 1;
				}
				CProduct.ToggleCheckbox('ImprintOption' + Default + 'Span', 'Imprint', ID, '', NumColors);
			}
		} else {
			//alert(Content + " (UpdateImprintOptions)");
		}
	});
}

//-----------------------------------------------------------------------------
CProduct.UpdateColorOptions = function(NormalID, ImprintID) {
	var Parms = {};
	Parms["ID"] = NormalID;
	
	CAJAX.Add('Products', 'Module', 'GetOptionOptions', Parms, function(Code, Content) {
		if(Code == 1) {
			Content = JSON.parse(Content);
		
			var ColorList = '';
			for (var key in Content) {
				if (key != ImprintID) continue;
				
				if(Content[key]['Type'] == 'Four Color Process') break;
				
				for (var n = Content[key]['Min']; n <= Content[key]['Max']; n++) {
					if (!Default) {
						var Default = key;
						var Number = n;
					}
					if (Content[key]['NumberIncluded'] >= n && Content[key]['NumberIncludedType'] == 'Both') { var Note = ' [Free]'; } else { var Note = ''; }
					if (n != 1) { var Suffix = 's'; } else { var Suffix = ''; }
					
					ColorList += "<input type='checkbox' name='ColorOption" + key + "_" + n + "' id='ColorOption" + key + "_" + n + "' class='Checkbox' value='" + key + "'>";
					ColorList += "<div id='ColorOption" + key + "_" + n + "Span' class='CheckboxUncheckedSpan' onclick=\"CProduct.ToggleCheckbox(this.id, 'Color', '', " + ImprintID + ", " + n + ");\"></div>";
					ColorList += "<div class='CheckboxLabel'>" + n + " Color" + Suffix + Note + "</div><br>";
				}
				break;
			}
			
			if (ColorList == '') {
				$('#ColorOptionList').html(CProduct.NotApplicable);
				for (var i=1; i<=10; i++) {
					if (i == 1) {
						var ColorList = CProduct.NotApplicable;
					} else {
						var ColorList = "";
					}
					$('#ImprintColor' + i).html(ColorList);
				}
				// Update Grid
				CProduct.UpdateGrid();
				
				// Update All Prices
				CProduct.UpdateAllPrices();
			} else {
				$('#ColorOptionList').html(ColorList);
				CProduct.ToggleCheckbox('ColorOption' + Default + '_' + Number + 'Span', 'Color', '', ImprintID, Number);
			}
		} else {
			//alert(Content + " (UpdateColorOptions)");
		}
	});
}

//-----------------------------------------------------------------------------
CProduct.UpdateProductColorChoices = function(ID, NumColors) {
	var Parms = {};
	Parms["ID"] = ID;
	
	CAJAX.Add('Products', 'Module', 'GetOptionColors', Parms, function(Code, Content) {
		if(Code == 1) {

			Content = JSON.parse(Content);
			for (var i=1; i<=10; i++) {
				if (i > NumColors) {
					if (i == 1) {
						var ColorList = CProduct.NotApplicable;
					} else {
						var ColorList = "";
					}
				} else {
					var ColorList = "<table class='ViewProductColorTable' cellpadding='0'><tr>";
					var Cells = 0;
					for (var key in Content) {
						if(Content[key]['Hex']) {
							ColorList += "<td class='ViewProductColorCheckbox'><input type='checkbox' value='" + key + "' class='Checkbox' id='ProductColor" + i + "Option" + key + "' name='ProductColor" + i + "Option" + key + "'><div onclick=\"CProduct.ToggleCheckbox(this.id, 'ProductColor" + i + "', '', '', 1); CProduct.ToggleProductColor('" + i + "ViewProductColorCell" + Cells + "', '#" + Content[key]['Hex'] + "', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\" class='CheckboxUncheckedSpan' id='ProductColor" + i + "Option" + key + "Span'></div></td>";
							ColorList += "<td class='ViewProductColorCell' id='" + i + "ViewProductColorCell" + Cells + "' valign='top'><div id='Imprint" + key + "' ";
							ColorList += "class='ViewProductColorBlock' style='background-color:#" + Content[key]['Hex'] + ";' ";
							ColorList += "onclick=\"CProduct.ToggleCheckbox('ProductColor" + i + "Option" + key + "Span', 'ProductColor" + i + "', '', '', 1); CProduct.ToggleProductColor('" + i + "ViewProductColorCell" + Cells + "', '#" + Content[key]['Hex'] + "', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\"  rel='" + key + "'><img src='" + $('#TemplatePath').attr('value') + "/gfx/ViewProductColorBlock.png' alt=''></div></td>";
						} else if(Content[key]['Filename']) {
							ColorList += "<td class='ViewProductColorCheckbox'><input type='checkbox' value='" + key + "' class='Checkbox' id='ProductColor" + i + "Option" + key + "' name='ProductColor" + i + "Option" + key + "'><div onclick=\"CProduct.ToggleCheckbox(this.id, 'ProductColor" + i + "', '', '', 1); CProduct.ToggleProductColor('" + i + "ViewProductColorCell" + Cells + "', 'url(" + Content[key]['ChosenFilename'] + ")', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\" class='CheckboxUncheckedSpan' id='ProductColor" + i + "Option" + key + "Span'></div></td>";
							ColorList += "<td class='ViewProductColorCell' id='" + i + "ViewProductColorCell" + Cells + "' valign='top'><div id='Imprint" + key + "' ";
							ColorList += "class='ViewProductColorBlock' style='background-image:url(" + Content[key]['Filename'] + ");' ";
							ColorList += "onclick=\"CProduct.ToggleCheckbox('ProductColor" + i + "Option" + key + "Span', 'ProductColor" + i + "', '', '', 1); CProduct.ToggleProductColor('" + i + "ViewProductColorCell" + Cells + "', 'url(" + Content[key]['ChosenFilename'] + ")', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\"  rel='" + key + "'><img src='" + $('#TemplatePath').attr('value') + "/gfx/ViewProductColorBlock.png' alt=''></div></td>";
						} else {
							ColorList += "<td class='ViewProductColorCheckbox'><input type='checkbox' value='" + key + "' class='Checkbox' id='ProductColor" + i + "Option" + key + "' name='ProductColor" + i + "Option" + key + "'><div onclick=\"CProduct.ToggleCheckbox(this.id, 'ProductColor" + i + "', '', '', 1); CProduct.ToggleProductColor('" + i + "ViewProductColorCell" + Cells + "', 'url(" + $('#TemplatePath').attr('value') + "/gfx/ColorMissingSelected.png)', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\" class='CheckboxUncheckedSpan' id='ProductColor" + i + "Option" + key + "Span'></div></td>";
							ColorList += "<td class='ViewProductColorCell' id='" + i + "ViewProductColorCell" + Cells + "' valign='top'><div id='Imprint" + key + "' ";
							ColorList += "class='ViewProductColorBlock' style='background-image:url(" + $('#TemplatePath').attr('value') + "/gfx/ColorMissing.png);' ";
							ColorList += "onclick=\"CProduct.ToggleCheckbox('ProductColor" + i + "Option" + key + "Span', 'ProductColor" + i + "', '', '', 1); CProduct.ToggleProductColor('" + i + "ViewProductColorCell" + Cells + "', 'url(" + $('#TemplatePath').attr('value') + "/gfx/ColorMissingSelected.png)', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\"  rel='" + key + "'><img src='" + $('#TemplatePath').attr('value') + "/gfx/ViewProductColorBlock.png' alt=''></div></td>";
						}
						ColorList += "<td class='ViewProductColorLabel'>" + Content[key]['Name'] + "</td>";
						if (Cells == 0) {
							var SoloCell	= i + "ViewProductColorCell" + Cells;
							var SoloSpan	= "ProductColor" + i + "Option" + key + "Span";
							var SoloType	= "ProductColor" + i;
							if(Content[key]['Hex']) {
								var SoloColor = Content[key]['Hex'];
							} else if(Content[key]['Filename']) {
								var SoloColor = Content[key]['Filename'];
							}
							var SoloName	= Content[key]['Name'];
							var SoloNumber	= i;
							var SoloKey		= key;
						}
						Cells += 1;
						if (!(Cells % 3)) ColorList += "</tr><tr>";
					}
					var ActualCells = Cells;
					while(Cells % 3) {
						ColorList += "<td class='ViewProductColorCell'></td><td></td>";
						Cells += 1;
					}
					if (NumColors == 1) {
						var NumDescription = "a";
					} else {
						var NumDescription = "your " + CProduct.NumDesc[i];
					}
					ColorList += "</tr>";
					ColorList += "</table>";
					ColorList += "<div class='ViewProductColorCellNote'>*Stock item colors shown on website are for reference only and may not match actual item color.  ";
					ColorList += "		Please <em>request a sample</em> for exact colors.</div>";
					ColorList += "<table class='ViewProductColorChosenTable'>";
					ColorList += "<tr>";
					ColorList += "<td class='ViewProductColorChosenCell'><div id='ProductColorChosen" + i + "' class='ViewProductColorChosen'><img src='" + $('#TemplatePath').attr('value') + "/gfx/ViewProductColorChosen.png' alt=''></div></td>";
					ColorList += "	<td class='ViewProductColorChosenNote' id='ProductColorChosenNote" + i + "'>You have NOT selected " + NumDescription + " Product Color.  Please Choose One.</td>";
					ColorList += "</tr></table><br><br>";
				}
				$('#ProductColor' + i).html(ColorList);
				
				if (ActualCells == 1) {
					CProduct.ToggleProductColor(SoloCell, SoloColor, SoloName, SoloNumber, SoloKey);
					CProduct.ToggleCheckbox(SoloSpan, SoloType, '', '', 1);
				}
			}
		} else {
			//alert(Content + " (UpdateProductColorChoices)");
		}
	});
}

//-----------------------------------------------------------------------------
CProduct.UpdateImprintColorChoices = function(ID, NumColors) {
	var Parms = {};
	Parms["ID"] = ID;
	
	CAJAX.Add('Products', 'Module', 'GetOptionColors', Parms, function(Code, Content) {
		if(Code == 1) {
			Content = JSON.parse(Content);
			
			for (var i=1; i<=10; i++) {
				if (i > NumColors) {
					if (i == 1) {
						var ColorList = CProduct.NotApplicable;
					} else {
						var ColorList = "";
					}
				} else {
					var ColorList = "<table class='ViewImprintColorTable' cellpadding='0'><tr>";
					var Cells = 0;
					for (var key in Content) {
						if(Content[key]['Hex']) {
							ColorList += "<td class='ViewImprintColorCheckbox'><input type='checkbox' value='" + key + "' class='Checkbox' id='ImprintColor" + i + "Option" + key + "' name='ImprintColor" + i + "Option" + key + "'><div onclick=\"CProduct.ToggleCheckbox(this.id, 'ImprintColor" + i + "', '', '', 1); CProduct.ToggleImprintColor('" + i + "ViewImprintColorCell" + Cells + "', '#" + Content[key]['Hex'] +  "', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\" class='CheckboxUncheckedSpan' id='ImprintColor" + i + "Option" + key + "Span'></div></td>";
							ColorList += "<td class='ViewImprintColorCell' id='" + i + "ViewImprintColorCell" + Cells + "' valign='top'><div id='Imprint" + key + "' ";
							ColorList += "class='ViewProductColorBlock' style='background-color:#" + Content[key]['Hex'] + ";' ";
							ColorList += "onclick=\"CProduct.ToggleCheckbox('ImprintColor" + i + "Option" + key + "Span', 'ImprintColor" + i + "', '', '', 1); CProduct.ToggleImprintColor('" + i + "ViewImprintColorCell" + Cells + "', '#" + Content[key]['Hex'] + "', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\" rel='" + key + "'><img src='" + $('#TemplatePath').attr('value') + "/gfx/ViewProductColorBlock.png' alt=''></div></td>";
						} else if(Content[key]['Filename']) {
							ColorList += "<td class='ViewImprintColorCheckbox'><input type='checkbox' value='" + key + "' class='Checkbox' id='ImprintColor" + i + "Option" + key + "' name='ImprintColor" + i + "Option" + key + "'><div onclick=\"CProduct.ToggleCheckbox(this.id, 'ImprintColor" + i + "', '', '', 1); CProduct.ToggleImprintColor('" + i + "ViewImprintColorCell" + Cells + "', 'url(" + Content[key]['ChosenFilename'] +  ")', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\" class='CheckboxUncheckedSpan' id='ImprintColor" + i + "Option" + key + "Span'></div></td>";
							ColorList += "<td class='ViewImprintColorCell' id='" + i + "ViewImprintColorCell" + Cells + "' valign='top'><div id='Imprint" + key + "' ";
							ColorList += "class='ViewProductColorBlock' style='background-image:url(" + Content[key]['Filename'] + ");' ";
							ColorList += "onclick=\"CProduct.ToggleCheckbox('ImprintColor" + i + "Option" + key + "Span', 'ImprintColor" + i + "', '', '', 1); CProduct.ToggleImprintColor('" + i + "ViewImprintColorCell" + Cells + "', 'url(" + Content[key]['ChosenFilename'] + ")', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\" rel='" + key + "'><img src='" + $('#TemplatePath').attr('value') + "/gfx/ViewProductColorBlock.png' alt=''></div></td>";
						} else {
							ColorList += "<td class='ViewImprintColorCheckbox'><input type='checkbox' value='" + key + "' class='Checkbox' id='ImprintColor" + i + "Option" + key + "' name='ImprintColor" + i + "Option" + key + "'><div onclick=\"CProduct.ToggleCheckbox(this.id, 'ImprintColor" + i + "', '', '', 1); CProduct.ToggleImprintColor('" + i + "ViewImprintColorCell" + Cells + "', 'url(" + $('#TemplatePath').attr('value') + "/gfx/ColorMissingSelected.png)', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\" class='CheckboxUncheckedSpan' id='ImprintColor" + i + "Option" + key + "Span'></div></td>";
							ColorList += "<td class='ViewImprintColorCell' id='" + i + "ViewImprintColorCell" + Cells + "' valign='top'><div id='Imprint" + key + "' ";
							ColorList += "class='ViewProductColorBlock' style='background-image:url(" + $('#TemplatePath').attr('value') + "/gfx/ColorMissing.png);' ";
							ColorList += "onclick=\"CProduct.ToggleCheckbox('ImprintColor" + i + "Option" + key + "Span', 'ImprintColor" + i + "', '', '', 1); CProduct.ToggleImprintColor('" + i + "ViewImprintColorCell" + Cells + "', 'url(" + $('#TemplatePath').attr('value') + "/gfx/ColorMissingSelected.png)', '" + Content[key]['Name'] + "', " + i + ", " + key + ");\" rel='" + key + "'><img src='" + $('#TemplatePath').attr('value') + "/gfx/ViewProductColorBlock.png' alt=''></div></td>";
						}
						ColorList += "<td class='ViewImprintColorLabel'>" + Content[key]['Name'] + "</td>";
						if (Cells == 0) {
							var SoloCell	= i + "ViewImprintColorCell" + Cells;
							var SoloSpan	= "ImprintColor" + i + "Option" + key + "Span";
							var SoloType	= "ImprintColor" + i;
							if(Content[key]['Hex']) {
								var SoloColor = Content[key]['Hex'];
							} else if(Content[key]['Filename']) {
								var SoloColor = Content[key]['Filename'];
							}
							var SoloName	= Content[key]['Name'];
							var SoloNumber	= i;
							var SoloKey		= key;
						}
						Cells += 1;
						if (!(Cells % 3)) ColorList += "</tr><tr>";
					}
					var ActualCells = Cells;
					while(Cells % 3) {
						ColorList += "<td class='ViewProductColorCell'></td><td></td>";
						Cells += 1;
					}
					if (NumColors == 1) {
						var NumDescription = "an";
					} else {
						var NumDescription = "your " + CProduct.NumDesc[i];
					}
					ColorList += "</tr>";
					ColorList += "</table>";
					ColorList += "<div class='ViewImprintColorCellNote'>*Stock item colors shown on website are for reference only and may not match actual item color.  ";
					ColorList += "		Please <em>request a sample</em> for exact colors.</div>";
					ColorList += "<table class='ViewImprintColorChosenTable'>";
					ColorList += "<tr>";
					ColorList += "<td class='ViewImprintColorChosenCell'><div id='ImprintColorChosen" + i + "' class='ViewImprintColorChosen'><img src='" + $('#TemplatePath').attr('value') + "/gfx/ViewProductColorChosen.png' alt=''></div></td>";
					ColorList += "	<td class='ViewImprintColorChosenNote' id='ImprintColorChosenNote" + i + "'>You have NOT selected " + NumDescription + " Imprint Color.  Please Choose One.</td>";
					ColorList += "</tr></table><br><br>";
				}
				$('#ImprintColor' + i).html(ColorList);
				$('#ImprintColor' + i).attr("rel", "");
				
				if (ActualCells == 1) {
					CProduct.ToggleImprintColor(SoloCell, SoloColor, SoloName, SoloNumber, SoloKey);
					CProduct.ToggleCheckbox(SoloSpan, SoloType, '', '', 1);
				}
			}
		} else {
			//alert(Content + " (UpdateImprintColorChoices)");
		}
	});
}

//-----------------------------------------------------------------------------
CProduct.UpdateGrid = function() {
	var NormalID = 0;
	var ImprintID = 0;
	var NumColors = 1;
	
	$('input').each(function() {
		if (this.id.indexOf('NormalOption') >= 0) {
			if (this.checked == true) NormalID = this.value;
		} else if (this.id.indexOf('ColorOption') >= 0) {
			if (this.checked == true) {
				var ImprintID = this.value;
				var NumColors = intval(str_replace('ColorOption' + this.value + '_', '', this.id));
			}
		}
	});
	
	if ($('#NormalDefault').attr('value') > 0 && NormalID == 0) var NormalID = $('#NormalDefault').attr('value');
	if ($('#ColorDefault').attr('value') > 0 && ImprintID == 0) var ImprintID = $('#ColorDefault').attr('value');
	
	var Parms = {};
	Parms['NormalID'] = NormalID;
	Parms['ImprintID'] = ImprintID;
	Parms['NumColors'] = NumColors;

	Parms['NormalID']  = CProduct.NormalID;
	Parms['ImprintID'] = CProduct.ImprintID;
	Parms['NumColors'] = CProduct.NumColors;

	CAJAX.Add('Products', 'Module', 'GetTotalPriceGrid', Parms, function(Code, Content) {
		if(Code == 1) {
			Content = JSON.parse(Content);
			
			var PriceGrid = "<div id='CLoading'><div id='LoadingGraphic'>Loading</div></div>";
			PriceGrid += "<table id='ViewProductGridsTable'><tr><th class='ViewProductGridsTableTH'>QTY</th>";
	
			var i = 0;
			for (var key in Content) {
				if(i == 0) {
					$('#MinQuantity').attr('value', key);
				}
	
				PriceGrid += "<th class='ViewProductGridsTableTH'>" + key + "</th>";
				i++;
			}
			PriceGrid += "</tr><tr><td>Price</td>";
			for (var key in Content) {
				PriceGrid += "<td>" + number_format(Content[key], 2) + "</td>";
			}
			PriceGrid += "</tr></table>";
			$("#ViewProductGridsDiv").html(PriceGrid);

			$("#ViewProductGridsTable tr th").bind("mouseover", function() {
				$(this).addClass("ViewProductGridsTableTHOver");
			}).bind("mouseout", function() {
				$(this).removeClass("ViewProductGridsTableTHOver");
			}).bind("click", function() {
				var Value = parseInt($(this).html());

				if(isNaN(Value) || Value == undefined) return;

				$("#Quantity").attr("value", Value);
				$("#Quantity").focus();

				$("#Quantity").trigger("onkeyup");
			});

			if($('#Quantity').attr('value') != '') $("#Quantity").trigger("onkeyup");

		} else {
			//alert(Content + " (UpdateGrid)");
		}
	});
	
}



//-----------------------------------------------------------------------------
CProduct.UpdateProductTotal = function() {
	var Quantity = intval($('#Quantity').attr('value'));
	var NormalID = CProduct.NormalID;
	//var ImprintID = CProduct.ImprintID;
	var NumColors = CProduct.NumColors;
	
	var Parms = {};
	Parms['NormalID'] = NormalID;
	Parms['ImprintID'] = 0;
	Parms['Quantity'] = Quantity;
	Parms['NumColors'] = NumColors;
	CAJAX.Add('Products', 'Module', 'GetTotalPrice', Parms, function(Code, Content) {
		if(Code == 1) {
			$("#ViewProductProductTotal").html('$' + number_format(Content, 2));
			CProduct.ProductTotal = number_format(Content, 2);
		} else {
			//alert(Content + " (UpdateProductTotal)");
		}
		// Update Setup Cost
		CProduct.UpdateSetupCost();
	});
}

//-----------------------------------------------------------------------------
CProduct.UpdateSetupCost = function() {
	//var Quantity = intval($('#Quantity').attr('value'));
	var NormalID = CProduct.NormalID;
	var ImprintID = CProduct.ImprintID;
	var NumColors = CProduct.NumColors;
	
	var Parms = {};
	Parms['NormalID'] = NormalID;
	Parms['ImprintID'] = ImprintID;
	Parms['Quantity'] = 0;
	Parms['NumColors'] = NumColors;
	CAJAX.Add('Products', 'Module', 'GetTotalPrice', Parms, function(Code, Content) {
		if(Code == 1) {
			$("#ViewProductSetupCost").html('$' + number_format(Content, 2));
			CProduct.SetupCost = number_format(Content, 2);
		} else {
			//alert(Content + " (UpdateSetupCost)");
		}
		// Update SubTotal
		CProduct.UpdateSubTotal();
	});
}

//-----------------------------------------------------------------------------
CProduct.UpdateSubTotal = function() {
	var Quantity = intval($('#Quantity').attr('value'));
	var NormalID = CProduct.NormalID;
	var ImprintID = CProduct.ImprintID;
	var NumColors = CProduct.NumColors;
	
	var Parms = {};
	Parms['NormalID'] = NormalID;
	Parms['ImprintID'] = ImprintID;
	Parms['Quantity'] = Quantity;
	Parms['NumColors'] = NumColors;
	CAJAX.Add('Products', 'Module', 'GetTotalPrice', Parms, function(Code, Content) {
		if(Code == 1) {
			// Display SubTotal
			$("#ViewProductSubTotal").html('$' + number_format(Content, 2));
			
			// Update Tax
			CProduct.UpdateTax(number_format(Content, 2));
		} else {
			//alert(Content + " (UpdateSubTotal)");
		}
	});
	/*
	var SubTotal = CProduct.ProductTotal + CProduct.SetupCost;
	$("#ViewProductSubTotal").html('$' + number_format(SubTotal, 2));
	*/
}

//-----------------------------------------------------------------------------
CProduct.UpdateTax = function(SubTotal) {
	var Parms = {};
	Parms['ShippingZip']	= $('#ZipCode').attr('value');
	Parms['SubTotal']		= SubTotal;
	
	CAJAX.Add('Products', 'Module', 'GetTax', Parms, function(Code, Content) {
		if(Code == 1) {
			// Display Tax
			$("#ViewProductTax").html('$' + number_format(Content, 2));
			
			// Update Total
			CProduct.UpdateTotal(parseFloat(number_format(Content, 2)) + parseFloat(str_replace(',', '', str_replace('$', '', $("#ViewProductSubTotal").html()))) + parseFloat(str_replace(',', '', str_replace('$', '', $("#ViewProductShipping").html()))));
		} else {
			//alert(Content + " (UpdateTax)");
		}
	});
}

//-----------------------------------------------------------------------------
CProduct.UpdateTotal = function(Total) {
	var NewTotal = Total.toFixed(2);
	$("#ViewProductTotal").html("$" + NewTotal);
}

//-----------------------------------------------------------------------------
CProduct.CheckQuantity = function(ID) {
	var Minimum = intval($('#MinQuantity').attr('value'));
	Minimum = (typeof Minimum == "undefined") ? 1 : Minimum;
	
	if (($('#'+ID).attr('value') - 0) != $('#'+ID).attr('value') || $('#'+ID).attr('value').length <= 0) {
		$("#ViewProductQuantityCheckImage").html("<div id='InvalidQuantity'></div>");
		$("#ViewProductQuantityCheckNote").html("Please enter a valid number.");
		//CProduct.UpdateProductTotal(0);
		CProduct.UpdateAllPrices();
	} else if (($('#'+ID).attr('value') - 0) < Minimum) {
		$("#ViewProductQuantityCheckImage").html("<div id='InvalidQuantity'></div>");
		$("#ViewProductQuantityCheckNote").html("Minimum quantity allowed is " + Minimum + ".");
		//CProduct.UpdateProductTotal(0);
		CProduct.UpdateAllPrices();
	} else {
		$("#ViewProductQuantityCheckImage").html("<div id='ValidQuantity'></div>");
		$("#ViewProductQuantityCheckNote").html("");
		//CProduct.UpdateProductTotal();
		CProduct.UpdateAllPrices();
	}
}

//-----------------------------------------------------------------------------
CProduct.ToggleImage = function(size200, size400) {
	$('#ViewProductImageLarge').attr('src', size200);
	$('#ViewProductImageLargeLink').attr('href', size400);
}

//-----------------------------------------------------------------------------
CProduct.ToggleProductColor = function(ID, Color, Name, Number, ColorID) {
	$('td[id^=' + Number + 'ViewProductColorCell]').each(function() {
		$(this).addClass('ViewProductColorCell');
		$(this).removeClass('ViewProductColorCellSelected');
	});
	$('#' + ID).addClass('ViewProductColorCellSelected');
	$('#' + ID).removeClass('ViewProductColorCell');
	
	$('#ProductColorChosen' + Number).css('background', Color);
	$('#ProductColorChosen' + Number).attr("rel", ColorID);

	$('#ProductColorChosenNote' + Number).html('You have selected ' + Name + '.  Click on any color to change.');
	$('#ProductColorChosenNote' + Number).css('color', '#000000');
}

//-----------------------------------------------------------------------------
CProduct.ToggleImprintColor = function(ID, Color, Name, Number, ColorID) {
	$('td[id^=' + Number + 'ViewImprintColorCell]').each(function() {
		$(this).addClass('ViewImprintColorCell');
		$(this).removeClass('ViewImprintColorCellSelected');
	});
	$('#' + ID).addClass('ViewImprintColorCellSelected');
	$('#' + ID).removeClass('ViewImprintColorCell');
	
	$('#ImprintColorChosen' + Number).css('background', Color);
	$('#ImprintColorChosen' + Number).attr("rel", ColorID);

	$('#ImprintColorChosenNote' + Number).html('You have selected ' + Name + '.  Click on any color to change.');
	$('#ImprintColorChosenNote' + Number).css('color', '#000000');
}

//-----------------------------------------------------------------------------
CProduct.UpdateShippingCost = function() {
	/*
	var ShipClass = new CShippingMethods.Class();

	ShipClass.SetOrigin($('#ManufacturerZip').attr('value'), "US");
	ShipClass.SetDestination($('#ZipCode').attr('value'), "US");
	
	var ShippingMethod = $('#ShippingMethod').attr('value');
	var ShippingMethodSplit = ShippingMethod.split("-");
	ShipClass.SetShippingMethod(ShippingMethodSplit[0], ShippingMethodSplit[1]);
	
	ShipClass.AddPackage($('#BoxLength').attr('value'), $('#BoxWidth').attr('value'), $('#BoxHeight').attr('value'), $('#BoxWeight').attr('value'));
	*/
	var ShippingMethod = $('#ShippingMethod').attr('value');
	var ShippingMethodSplit = ShippingMethod.split("-");
	
	var Parms = {};
	Parms["ProductsID"]				= $('#ProductID').attr('value');
	Parms["NormalOptionID"]			= CProduct.NormalID;
	Parms["Quantity"]				= $('#Quantity').attr('value');
	Parms["ShippingMethodsID"]		= ShippingMethodSplit[2];
	Parms["DestinationZipcode"]		= $('#ZipCode').attr('value');
	Parms["DestinationCountry"]		= "US";
	
	if ($('#ZipCode').attr('value') == '' || $('#ShippingMethod').attr('value') == '' || $('#Quantity').attr('value') == '') {
		$('#ViewProductShipping').html('$0.00');
	} else {
		CAJAX.Add('Products', 'Module', 'GetShipping', Parms, function(Code, Content) {
			if(Code == 1) {
				// Display Shipping
				$("#ViewProductShipping").html('$' + number_format(Content, 2));
				
				// Update Total
				//CProduct.UpdateTotal(parseFloat(number_format(Content, 2)) + parseFloat(str_replace(',', '', str_replace('$', '', $("#ViewProductSubTotal").html()))) + parseFloat(str_replace(',', '', str_replace('$', '', $("#ViewProductTax").html()))));
				
				// Update Tax
				CProduct.UpdateTax(parseFloat(str_replace(',', '', str_replace('$', '', $("#ViewProductSubTotal").html()))));
			} else {
				//alert(Content + " (UpdateShippingCost)");
			}
		});
		/*
		ShipClass.GetCost(function(Cost) {
			if (isNaN(Cost)) {
				$('#ViewProductShipping').html('$0.00');
			} else {
				Num = Cost * 1;
				$('#ViewProductShipping').html('$' + Num.toFixed(2));
				
				
				
				//CProduct.UpdateTotal(parseFloat(Num.toFixed(2)) + parseFloat(str_replace('$', '', $("#ViewProductTax").html())) + parseFloat(str_replace('$', '', $("#ViewProductSubTotal").html())));
			}
		});
		*/
	}
}

//-----------------------------------------------------------------------------
CProduct.AddToCart = function(ArtworkOption, UID) {
	var NormalOptionID		= CProduct.NormalID;
	var ImprintOptionID		= CProduct.ImprintID;
	var NumColors			= CProduct.NumColors;

	var ProductColorsAvailable = false;
	var ImprintColorsAvailable = false;

	var NormalOptionColors	= {};
	var ImprintOptionColors	= {};

	$(".ViewProductColorBlock").each(function() { ProductColorsAvailable = true; });
	$(".ViewImprintColorBlock").each(function() { ImprintColorsAvailable = true; });

	$(".ViewProductColorCellSelected").each(function(index, Element) {
		$(this).children("div").each(function() {
			var ColorID = $(this).attr("rel");
			NormalOptionColors[ColorID] = ColorID;
		});
	});

	$(".ViewImprintColorCellSelected").each(function(index, Element) {
		$(this).children("div").each(function() {
			var ColorID = $(this).attr("rel");
			ImprintOptionColors[ColorID] = ColorID;
		});
	});
    
	var Quantity = parseInt($("#Quantity").attr("value"));

	if(isNaN(Quantity)) Quantity = 0;

	var Minimum = parseInt(document.getElementById('MinQuantity').value);

	if(Quantity <= 0 || Quantity < Minimum || (Quantity - 0) != Quantity || Quantity.length <= 0) {
		alert("Please enter a valid Quantity");

		$.scrollTo("#ViewProductStepQuantity", 800, {
			"onAfter" : function() {
				$("#Quantity").focus();
			}
		});
	}else
	if(NormalOptionID <= 0) {
		alert("Please select a valid Product Option");
		$.scrollTo("#BuyNow", 800);
	}else
//	if(NumColors <= 0 && ImprintOptionType != "Four Color Process") {
//		alert("Please select a valid Number of Colors");
//		$.scrollTo("#BuyNow", 800);
//	}else
	if(count(NormalOptionColors) <= 0 && ProductColorsAvailable) {
		alert("Please select your Product Color(s)");
		$.scrollTo("#ViewProductStepProductColors", 800);
	}else
	if(count(ImprintOptionColors) < NumColors && ImprintColorsAvailable) {
		alert("Please select your Imprint Color(s)");
		$.scrollTo("#ViewProductStepImprintColors", 800);		
	}else{
		if(!ProductColorsAvailable)								NormalOptionColors  = {};
		if(NumColors <= 0 || ImprintColorsAvailable == false)	ImprintOptionColors = {};

		var Parms = {
			"ProductsID"				: $('#ProductID').attr("value"),
			"ProductsShippingMethodsID" : $('#DefaultShippingMethodID').attr("value"),

			"NormalOptionID"			: NormalOptionID,
			"ImprintOptionID"			: ImprintOptionID,
			"NumColors"					: NumColors,

			"NormalOptionColors"		: JSON.stringify(NormalOptionColors),
			"ImprintOptionColors"		: JSON.stringify(ImprintOptionColors),

			"Quantity"					: Quantity,

			"Artwork"					: ArtworkOption
		};

		MCarts.AddToCart(Parms, function(Code, Content) {
			if(Code == 1) {
                if(ArtworkOption == "Upload") {
					window.location.href = "/.Checkout?Page=Upload&GoBack=viewcart.php&ID=" + Content + "&UID=" + UID;
				}else{
					window.location.href = "/viewcart.php";
				}
			}else{
				alert(Content);
			}
		});
	}
}

//=============================================================================
CProduct.QuickQuote = function(StoresID) {
	var Name		= explode(" ", $("#Name").attr("value"), 2);

	var NumColors	= CProduct.NumColors;

	var FirstName	= Name[0];
	var LastName	= Name[1];

	var Zip			= $("#ZipCode").attr("value");
	var Email		= $("#Email").attr("value");

	var NormalOptionColors	= [];
	var ImprintOptionColors	= [];

	var Quantity = parseInt($("#Quantity").attr("value"));

	if(isNaN(Quantity)) Quantity = 0;

	var Minimum = parseInt(document.getElementById('MinQuantity').value);

	if(Quantity <= 0 || Quantity < Minimum || (Quantity - 0) != Quantity || Quantity.length <= 0) {
		alert("Please enter a valid Quantity");

		$.scrollTo("#ViewProductStepQuantity", 800, {
			"onAfter" : function() {
				$("#Quantity").focus();
			}
		});
		return;
	}

	$(".ViewProductColorCellSelected").each(function(index, Element) {
		$(this).children("div").each(function() {
			NormalOptionColors.push($(this).attr("rel"));
		});
	});

	$(".ViewImprintColorCellSelected").each(function(index, Element) {
		$(this).children("div").each(function() {
			ImprintOptionColors.push($(this).attr("rel"));
		});
	});

	if(Zip.length <= 4) {
		alert("Please enter a valid zipcode.");
		$("#ZipCode").focus();
		return;
	}

	if(Email.length <= 0) {
		alert("Please enter a valid email.");
		$("#Email").focus();
		return;
	}

	var ShippingMethodsID = split("-", $('#ShippingMethod').attr("value"));
		ShippingMethodsID = ShippingMethodsID[2];

	var Parms = { 
		"StoresID"			: StoresID,

		"BillingFirstName"	: FirstName,
		"BillingLastName"	: LastName,
		"BillingZip"		: Zip,
		"BillingPhone"		: $("#Phone1").attr("value") + "-" + $("#Phone2").attr("value") + "-" + $("#Phone3").attr("value"),
		"BillingEmail"		: Email,

		"ShippingFirstName"	: FirstName,
		"ShippingLastName"	: LastName,
		"ShippingZip"		: Zip,

		"Products"			: [{
			"ID"					: $('#ProductID').attr("value"),
			"ShippingMethodsID"		: ShippingMethodsID,

			"Options" : [
				//Normal
				{
					"ID"		: CProduct.NormalID,
					"Quantity"	: Quantity,
					"NumColors"	: NumColors,
					"Colors"	: NormalOptionColors
				},

				//Imprint
				{
					"ID"		: CProduct.ImprintID,
					"Quantity"	: Quantity,
					"NumColors"	: NumColors,
					"Colors"	: ImprintOptionColors
				}
			]
		}]
	}

	CAJAX.Add("Quote", "Module", "NewQuote", Parms, function(Code, Content) {
		if(Code == 1) {
			alert("A copy of the Quick Quote has been emailed to the address you entered. Thank You.");
		}else{
			alert(Content);
		}
	});
}

//=============================================================================
$(CProduct.OnInit);
//$(CProduct.UpdateGrid);

//=============================================================================
