$(function() {
	// set disabled
	// $("body#knitIndex #purpose").attr("disabled", "disabled");

	// country list
	/*
	$.each(country, function() {
		if (this[2] == 1) {
			$("#country").append("<option value=\"" + this[0] + "\" selected=\"selected\">" + this[1] + "</option>");
		}
		else {
			$("#country").append("<option value=\"" + this[0] + "\">" + this[1] + "</option>");
		}
	});
	*/

	// purpose list
	$.each(purpose, function() {
		if (this[2] == 1) {
			$("#purpose").append("<option value=\"" + this[0] + "\" selected=\"selected\">" + this[1] + "</option>");
		}
		else {
			$("#purpose").append("<option value=\"" + this[0] + "\">" + this[1] + "</option>");
		}
	});

	// knitting list
	$.each(knitting, function() {
		if (selectedProduct == 0) {
			if (this[2] == 1) {
				$("#knitting").append("<dd><input type=\"checkbox\" name=\"knitting[]\" value=\"" + this[0] + "\" id=\"chk" + this[0] + "\" checked=\"checked\" /> <label for=\"chk" + this[0] + "\">" + this[1] + "</label></dd>");
			}
			else {
				$("#knitting").append("<dd><input type=\"checkbox\" name=\"knitting[]\" value=\"" + this[0] + "\" id=\"chk" + this[0] +  "\" /> <label for=\"chk" + this[0] + "\">" + this[1] + "</label></dd>");
			}
		}
		else if (selectedProduct == 1 && this[3] == 1) {
			if (this[2] == 1) {
				$("#knitting").append("<dd><input type=\"checkbox\" name=\"knitting[]\" value=\"" + this[0] + "\" id=\"chk" + this[0] + "\" checked=\"checked\" /> <label for=\"chk" + this[0] + "\">" + this[1] + "</label></dd>");
			}
			else {
				$("#knitting").append("<dd><input type=\"checkbox\" name=\"knitting[]\" value=\"" + this[0] + "\" id=\"chk" + this[0] +  "\" /> <label for=\"chk" + this[0] + "\">" + this[1] + "</label></dd>");
			}
		}
		else if (selectedProduct == 2 && this[4] == 1) {
			if (this[2] == 1) {
				$("#knitting").append("<dd><input type=\"checkbox\" name=\"knitting[]\" value=\"" + this[0] + "\" id=\"chk" + this[0] + "\" checked=\"checked\" /> <label for=\"chk" + this[0] + "\">" + this[1] + "</label></dd>");
			}
			else {
				$("#knitting").append("<dd><input type=\"checkbox\" name=\"knitting[]\" value=\"" + this[0] + "\" id=\"chk" + this[0] +  "\" /> <label for=\"chk" + this[0] + "\">" + this[1] + "</label></dd>");
			}
		}
	});

	// gauge list
	$.each(gauge, function() {
		if (this[2] == 1) {
			$("#gauge").append("<dd><input type=\"radio\" name=\"gauge\" value=\"" + this[0] + "\" id=\"rad" + this[0] + "\" checked=\"checked\" /> <label for=\"rad" + this[0] + "\">" + this[1] + "</label></dd>");
		}
		else {
			$("#gauge").append("<dd><input type=\"radio\" name=\"gauge\" value=\"" + this[0] + "\" id=\"rad" + this[0] + "\" /> <label for=\"rad" + this[0] + "\">" + this[1] + "</label></dd>");
		}
	});

	/*
	$("body#knitIndex #country").change(function() {
		var value = $("#country option:selected").attr("value");
		if (value != "") {
			$("#purpose").removeAttr("disabled");
		}
		else {
			$("#purpose").attr("disabled", "disabled");
		}
	});
	*/

	/*
	$("#country").change(function() {
		var value = $("#country option:selected").attr("value");
		if (value == "none") {
			$("div#searchPanel").css("display", "none");
			$("#purpose").val("none");
			$("#purpose").attr("disabled", "disabled");
		}
		else {
			$("#purpose").removeAttr("disabled");
		}
	});
	*/

	$("#purpose").change(function() {
		var value = $("#purpose option:selected").attr("value");

		if ($("div#searchError1").css("display") == "block") {
			$("div#searchError1").css("display", "none");
		}

		if ($("div#searchError1").text() != "") {
			$("div#searchError1").text("");
		}

		if ($("div#searchError2").text() != "") {
			$("div#searchError2").text("");
		}

		if ($("#knitting").text() != "") {
			$("#knitting dd").remove();
		}

		if (value == 1) {
			$.each(knitting, function() {
				if (this[3] == 1) {
					if (this[2] == 1) {
						$("#knitting").append("<dd><input type=\"checkbox\" name=\"knitting[]\" value=\"" + this[0] + "\" id=\"chk" + this[0] + "\" checked=\"checked\" /> <label for=\"chk" + this[0] + "\">" + this[1] + "</label></dd>");
					}
					else {
						$("#knitting").append("<dd><input type=\"checkbox\" name=\"knitting[]\" value=\"" + this[0] + "\" id=\"chk" + this[0] +  "\" /> <label for=\"chk" + this[0] + "\">" + this[1] + "</label></dd>");
					}
				}
			});

			$("div#searchPanel").css("display", "block");
			// $("p#searchPurpose").text($("#purpose option:selected").text());
		}
		else if (value == 2) {
			$.each(knitting, function() {
				if (this[4] == 1) {
					if (this[2] == 1) {
						$("#knitting").append("<dd><input type=\"checkbox\" name=\"knitting[]\" value=\"" + this[0] + "\" id=\"chk" + this[0] + "\" checked=\"checked\" /> <label for=\"chk" + this[0] + "\">" + this[1] + "</label></dd>");
					}
					else {
						$("#knitting").append("<dd><input type=\"checkbox\" name=\"knitting[]\" value=\"" + this[0] + "\" id=\"chk" + this[0] +  "\" /> <label for=\"chk" + this[0] + "\">" + this[1] + "</label></dd>");
					}
				}
			});

			$("div#searchPanel").css("display", "block");
			// $("p#searchPurpose").text($("#purpose option:selected").text());
		}
		/*
		else if (value != "none") {
			$("div#searchPanel").css("display", "none");
			$("div#ttlSearch form").submit();
		}
		*/
		else {
			$("div#searchPanel").css("display", "none");
		}

		if ($("#knitting input:checked")) {
			$("#knitting input").removeAttr('checked');
		}

		if ($("#gauge input:checked")) {
			$("#gauge input").removeAttr('checked');
		}
	});

	// Error check
	$("p.searchButton input").click(function() {
		var flag1 = false;
		var flag2 = false;

		if ($("div#searchError1").text() != "") {
			$("div#searchError1").text("");
		}

		if ($("div#searchError2").text() != "") {
			$("div#searchError2").text("");
		}

		/*
		// country
		if ($("#country option:selected").attr("value") == "none") {
			$("div#searchError1").append(error[0]);
			flag1 = true;
		}
		*/

		// purpose
		if ($("#purpose option:selected").attr("value") == "none") {
			$("div#searchError1").append(error[1]);
			flag1 = true;
		}

		var value = $("#purpose option:selected").attr("value");
		if ((value == 1) || (value == 2)) {
			// knitting
			if ($("#searchPanel input[type='checkbox']:checked").size() == 0) {
				$("div#searchError2").append(error[2]);
				flag2 = true;
			}

			// gauge
			if ($("#searchPanel input[type='radio']:checked").size() == 0) {
				$("div#searchError2").append(error[3]);
				flag2 = true;
			}
		}

		// flag check
		if (flag1) {
			$("div#searchError1").slideDown("fast");
			return false;
		}
		else if (flag2) {
			$("div#searchError2").slideDown("fast");
			return false;
		}
	});
});
