var popp = 0; // display popping box?
var c = 0; // counter
var modsSelected = document.getElementById("modSelected");
var modReset = document.getElementById("modReset");
var modAdd = document.getElementById("modAdd");

//---------------- addIt ----------------
function addIt()
{
	var mods = [];
	var qtys = [];
	
	// store values (weird bug fix. for all non-IE browsers)
	for (j = 0; j < c; j++)
	{
		mods[j] = document.getElementById("mod" + j).value;
		qtys[j] = document.getElementById("qty" + j).value;
	}
	
	// add new field
	document.getElementById("modelsContainer").innerHTML += "<div class=\"modelField\">Model number:<br /><input type=\"text\" name=\"mod" + c + "\" id=\"mod" + c + "\" /></div><div class=\"modelField\">Quantity:<br /><input type=\"text\" name=\"qty" + c + "\" id=\"qty" + c + "\" value=\"1\" class=\"qty\" /></div><br class=\"clearLeft\" />";
	
	// restore values
	for (j = 0; j < c; j++)
	{
		document.getElementById("mod" + j).value = mods[j];
		document.getElementById("qty" + j).value = qtys[j];
	}
}

//---------------- fillIt ----------------
function fillIt(mod, qty)
{
	document.getElementById("mod" + c).value = mod;
	document.getElementById("qty" + c).value = qty;
}

//---------------- reset ----------------
function resetFields()
{
	c = 0;
	document.getElementById("modelsContainer").innerHTML = "<div class=\"modelField\">Model number:<br /><input type=\"text\" name=\"mod0\" id=\"mod0\" /></div><div class=\"modelField\">Quantity:<br /><input type=\"text\" name=\"qty0\" id=\"qty0\" value=\"1\" class=\"qty\" /></div><br class=\"clearLeft\" />";
	modsSelected.innerHTML = '<span class="topNote">Select product on the left.</span>';
	modReset.style.display = "none";
	document.getElementById("poppingBox").style.display = "none";
	document.getElementById("modBox").style.display = "";
	document.getElementById("modBox2Container").style.display = "none";
	document.getElementById("linkAdd2").style.display = "none";
}

//---------------- line ----------------
function quoteLineF()
{
	var that = document.getElementById("quoteLine").value;
	var type = document.getElementById("quoteType").value;
	var style = document.getElementById("quoteStyleContainer");
	if (that)
	{
		if (that == "Other") that = "";
		var code = "<select id=\"quoteStyle\" onchange=\"quoteStyleF();\"><option value=\"\">- Select One -</option>";
		var name = [];
		var other = 0;
		//var count = 0;
		for (j = 0; j < items.length; j++)
		{
			// see if it's already listed
			newItem = 1;
			for (k = 0; k < name.length; k++) if (name[k] == items[j]["style"]) newItem = 0;
			
			// list item
			if (items[j]["type"] == type && items[j]["line"] == that && newItem)
			{
				name.push(items[j]["style"]);
				//count++;
				if (!items[j]["style"])
				{
					other = 1;
					continue;
				}
				code += "<option value=\"" + items[j]["style"] + "\">" + items[j]["style"] + "</option>";
			}
		}
		if (other) code += "<option value=\"Other\">Other</option>";
		code += "</select>";
		if (name.length < 2)
		{
			style.innerHTML = "<select id=\"quoteStyle\"><option value=\"Any\">Any</option></select>";
			findModel();
			modAdd.disabled = false;
		}
		else
		{
			style.innerHTML = code;
			modAdd.disabled = true;
		}
	}
	else
	{
		style.innerHTML = "<select id=\"quoteStyle\" disabled=\"disabled\"><option>- Select One -</option></select>";
		modAdd.disabled = true;
	}
}

//---------------- style ----------------
function quoteStyleF()
{
	if (document.getElementById("quoteStyle").value)
	{
		findModel();
		modAdd.disabled = false;
	}
	else modAdd.disabled = true;
}

//---------------- reset selection ----------------
function resetSel()
{
	document.getElementById("quoteType").selectedIndex = 0;
	document.getElementById("quoteLine").selectedIndex = 0;
	document.getElementById("quoteStyle").selectedIndex = 0;
	document.getElementById("modAdd").selectedIndex = 0;
	document.getElementById("quoteLine").disabled = true;
	document.getElementById("quoteStyle").disabled = true;
	document.getElementById("modAdd").disabled = true;
	document.getElementById("iKnow").checked = false;
}
 
//---------------- find model ----------------
function findModel()
{
	// clean selections
	modN = "";
	pathN = "";
	
	// get selections
	type = document.getElementById("quoteType").value;
	line = document.getElementById("quoteLine").value;
	style = document.getElementById("quoteStyle").value;
	
	// clean selections
	if (line == "Other") line = "";
	if (style == "Other") style = "";
	
	// find model
	if (type != "- Select One -" && line != "- Select One -" && style != "- Select One -")
	{
		for (j = 0; j < items.length; j++)
		{
			if (items[j]["type"] == type && (items[j]["line"] == line || line == "Any" || (!line && !items[j]["line"])) && (items[j]["style"] == style || style == "Any" || (!style && !items[j]["style"])))
			{
				modN = items[j]["model"];
				pathN = "img_c/" + items[j]["path"] + "/cart/";
				qtyN = items[j]["min"];
			}
		}

		// quantity change
		document.getElementById("quoteQty").value = qtyN;
	}
}

// inputs
inputs = document.getElementsByTagName("input");
for (i=0; i<inputs.length; i++)
{
	
//---------------- add model ----------------

	if (inputs[i].className.match("linkAdd"))
	{
		inputs[i].onclick = function()
		{
			// advance counter
			c++;
				
			// add new fields
			addIt();
		}
	}
	
// ---------------- i know ----------------
		
	if (inputs[i].id.match("iKnow"))
	{
		inputs[i].onclick = function()
		{
			resetFields();
			if (this.checked)
			{
				// display block B
				document.getElementById("modelsContainer2").style.display = "inline-block";
				document.getElementById("modelsContainer1").style.display = "none";
			}
			else
			{
				// display block A
				document.getElementById("modelsContainer1").style.display = "inline-block";
				document.getElementById("modelsContainer2").style.display = "none";
			}
		}
	}
	
// ---------------- add (selected) ----------------
		
	if (inputs[i].id.match("modAdd"))
	{
		inputs[i].onclick = function()
		{
			// quantity prompt
			//var qtyN = prompt("Minimum order quantity for this item is " + qtyN + ".\nPlease, enter the desired quantity below:", qtyN);
			qtyN = document.getElementById("quoteQty").value;
			
			// check if qty is a number
			if (qtyN)
			{
				if (qtyN * 1)
				{
					// check if all fields selected
					if (!document.getElementById("quoteLine").disabled && !document.getElementById("quoteStyle").disabled)
					{
						// plural
						var s = qtyN > 1 ? "s" : "";
		
						if (c) // if not the first one
						{
							// add model and quantity box
							addIt();
							
							// fill in new values
							fillIt(modN, qtyN);
							
							// if 4+ hide popping box
							if (c > 3) document.getElementById("poppingBox").style.display = "none";
						}
						else // if the first one
						{
							// clear container
							modsSelected.innerHTML = "";
							
							// display box 2
							document.getElementById("modBox2Container").style.display = "inline-block";
							
							// display reset button
							modReset.style.display = "inline-block";
							
							// display popping box
							if (popp)
							{
								document.getElementById("poppingBox").style.display = "inline-block";
								popp = 0;
							}
							
							// fill in new values
							fillIt(modN, qtyN);
						}
						// display product
						//modsSelected.innerHTML += "<div><img src=\"" + pathN + modN + ".jpg\" border=\"0\" align=\"absmiddle\">" + qtyN + " &times; " + line + " " + style + " " + type  + s + "</div>";
						modsSelected.innerHTML += "<div>" + qtyN + " &times; <img src=\"" + pathN + modN + ".jpg\" border=\"0\" align=\"absmiddle\"></div>";
						//if (debug) modsSelected.innerHTML += "<div class=\"modelNote\">(Model: " + modN + ")</div>";
						
						// hide modbox1
						document.getElementById("modBox").style.display = "none";
						
						// show "add another product" button
						document.getElementById("linkAdd2").style.display = "";
						
						c++;
						resetSel();
					}
					else alert("Make your selection first");
				}
				else alert("Quantity has to be a number");
			}
		}
	}

// ---------------- add Another product (no model) ----------------
		
	if (inputs[i].id.match("linkAdd2"))
	{
		inputs[i].onclick = function()
		{
			document.getElementById("modBox").style.display = "";
			document.getElementById("linkAdd2").style.display = "none";
		}
	}
}

// a
links = document.getElementsByTagName("a");
for (i=0; i<links.length; i++)
{
	
//---------------- continue to Shipping ----------------

	if (links[i].id.match("continueToBigBoxShipping"))
	{
		links[i].onclick = function()
		{
			var valid = 0;
			var invalid = [];
			
			// see if there's at least one product
			for (i = 0; i <= c; i++) if (document.getElementById("mod" + i)) if (document.getElementById("mod" + i).value) valid = 1;
			if (!valid) invalid.push("- Select at least one product and click ADD button");

			// see if quantities are numbers
			valid = 1;
			for (i = 0; i <= c; i++) if (document.getElementById("qty" + i)) if (!(document.getElementById("qty" + i).value * 1)) valid = 0;
			if (!valid) invalid.push("- Quantity has to be a number");
			
			
			// show errors, or continue
			if (invalid.length) alert("Please fix the following error" + (invalid.length > 1 ? "s" : "") + ":\n" + invalid.join("\n"));
			else
			{
				document.getElementById("bigBoxProduct").style.display = "none";
				document.getElementById("bigBoxShipping").style.display = "block";
			}
		}
	}
	
// ---------------- reset ----------------
		
	if (links[i].className.match("modReset"))
	{
		links[i].onclick = function()
		{
			resetFields();
		}
	}
	
// ---------------- reset2 ----------------
		
	if (links[i].className.match("modReset2"))
	{
		links[i].onclick = function()
		{
			resetFields();
		}
	}
	
// ---------------- manual ----------------
		
	if (links[i].className.match("quoteManual"))
	{
		links[i].onclick = function()
		{
			document.quoteForm.submit();
		}
	}
	
// ---------------- add to cart ----------------
		
	if (links[i].id.match("quoteAtc"))
	{
		links[i].onclick = function()
		{
			document.quoteForm.action = "includes/shipping/atc.php";
			document.quoteForm.submit();
		}
	}
	
// ---------------- quote submit ----------------

	if (links[i].className.match("quoteSubmitButton"))
	{
		links[i].onclick = function()
		{
			var valid = 0;
			var invalid = [];
			
			// see if there's at least one product
			//for (i = 0; i <= c; i++) if (document.getElementById("mod" + i)) if (document.getElementById("mod" + i).value) valid = 1;
			//if (!valid) invalid.push("- Select at least one product");

			// see if quantities are numbers
			//valid = 1;
			//for (i = 0; i <= c; i++) if (document.getElementById("qty" + i)) if (!(document.getElementById("qty" + i).value * 1)) valid = 0;
			//if (!valid) invalid.push("- Quantity has to be a number");
			
			// see if the zip code entered
			if (!document.getElementById("sh_zip").value && document.getElementById("sh_country").value != "--") invalid.push("- Enter your ZIP code");
			
			// show errors, or submit form
			//var s = invalid.length > 1 ? "s" : "";
			if (invalid.length) alert("Please fix the following error" + (invalid.length > 1 ? "s" : "") + ":\n" + invalid.join("\n"));
			else
			{
				resetSel();
				document.quoteForm.submit();
			}
		}
	}
}

// selects
selects = document.getElementsByTagName("select");
for (i=0; i<selects.length; i++)
{
	
//---------------- type ----------------
	if (selects[i].id.match("quoteType"))
	{
		selects[i].onchange = function()
		{
			var line = document.getElementById("quoteLineContainer");
			var style = document.getElementById("quoteStyleContainer");
			if (this.value)
			{
				var code = "<select id=\"quoteLine\" onchange=\"quoteLineF();\"><option value=\"\">- Select One -</option>";
				var name = [];
				var other = 0;
				//var count = 0;
				for (j = 0; j < items.length; j++)
				{
					// see if it's already listed
					newItem = 1;
					for (k = 0; k < name.length; k++) if (name[k] == items[j]["line"]) newItem = 0;

					// list item
					if (items[j]["type"] == this.value && newItem)
					{
						name.push(items[j]["line"]);
						//count++;
						if (!items[j]["line"])
						{
							other = 1;
							continue;
						}
						code += "<option value=\"" + items[j]["line"] + "\">" + items[j]["line"] + "</option>";
					}
				}
				if (other) code += "<option value=\"Other\">Other</option>";
				code += "</select>";
				if (name.length < 2)
				{
					line.innerHTML = "<select id=\"quoteLine\"><option value=\"Any\">Any</option></select>";
					style.innerHTML = "<select id=\"quoteStyle\"><option value=\"Any\">Any</option></select>";
					findModel();
					modAdd.disabled = false;
				}
				else
				{
					line.innerHTML = code;
					style.innerHTML = "<select id=\"quoteStyle\" disabled=\"disabled\"><option>- Select One -</option></select>";
					modAdd.disabled = true;
				}
			}
			else
			{
				line.innerHTML = "<select id=\"quoteLine\" disabled=\"disabled\"><option>- Select One -</option></select>";
				style.innerHTML = "<select id=\"quoteStyle\" disabled=\"disabled\"><option>- Select One -</option></select>";
				modAdd.disabled = true;
			}
		}
	}

//---------------- sh_country_selector ----------------
	if (selects[i].className.match("sh_country_selector"))
	{
		selects[i].onchange = function()
		{
			if (this.value == "--")
			{
				document.getElementById("otherCountrySubmit").style.display = "block";
				document.getElementById("nomalCountrySubmit").style.display = "none";
				document.getElementById("nomalCountryZip").style.display = "none";
				document.getElementById("nomalCountryType").style.display = "none";
				document.getElementById("nomalCountryLift").style.display = "none";
			}
			else
			{
				document.getElementById("otherCountrySubmit").style.display = "none";
				document.getElementById("nomalCountrySubmit").style.display = "block";
				document.getElementById("nomalCountryZip").style.display = "";
				document.getElementById("nomalCountryType").style.display = "";
				document.getElementById("nomalCountryLift").style.display = "";
			}
		}
	}
}

if (document.getElementById("poppingBox"))
{
	document.getElementById("poppingBox").onclick = function()
	{
		document.getElementById("poppingBox").style.display = "none";
	}
}