var ymmUrl = "/ka/store/keystone.aspx?type=YMM";

$(document).ready(function () {
	$.get(ymmUrl, function (result) {
		SetYMMControls(result);
	});
	$('.Enabled').click(function () {
		$(".SearchButtonLoading").attr("style", "display:block;");
	});
});

function ChangeYMMDropDown(id) {
	$.get(ymmUrl + "&DROPDOWNCHANGED=" + id + "&DROPDOWNCHANGED_VALUE=" + $(".YMMSearch #" + id).find("option:selected").val() + "&DROPDOWNCHANGED_TEXT=" + $(".YMMSearch #" + id).find("option:selected").html(), function (result) {
		SetYMMControls(result);
	});
}
function ChangeYMMCheckBox(id) {
	$.get(ymmUrl + "&CHECKBOXCHANGED=" + id + "&CHECKBOXCHANGED_VALUE=" + $(".YMMSearch #" + id + "CheckBox").is(':checked'), function (result) {
		SetYMMControls(result);
	});
}

function SetYMMControls(result) {
	YMM = eval('(' + result + ')');
	if (YMM.Valid) {
		var html = "";
		for (var index = 0; index < YMM.Data.length; index++) {
			var data = YMM.Data[index];
			html += "<div class='YMMKeystoneQuery'>\r\n" +
						"<div class='YMMKeystoneQueryTitle'>" + data.Name + "</div>\r\n" +
						"<select class='YMMKeystoneQueryDropDownList' onchange='ChangeYMMDropDown(\"" + data.Name + "\");' id='" + data.Name + "'";
			if (data.Values.length <= 0) {
				html +=
						"disabled='disabled'";
			}
			html +=
						">\r\n";

			for (var index2 = 0; index2 < data.Values.length; index2++) {
				var currentValue = data.Values[index2];
				html +=
							"<option value='" + currentValue[1] + "'";
				if (data.Selected == currentValue[0]) {
					html +=
							" selected='selected'";
				}
				html +=
							">" + currentValue[0] + "</option>\r\n";
			}
			html +=
						"</select>\r\n" +
					"</div>";

			$(".YMMSearchQuestions").html(html);

			if (index + 1 == YMM.DataCount) {
				$("#" + data.Name).focus();
			}
		}
		
		html =
			"<span class='YMMCheckBox'>\r\n" +
				"<input " + (YMM.ExcludeUniversal ? "checked='checked' " : "") + "id='ExcludeUniversalCheckBox' type='checkbox' onclick='ChangeYMMCheckBox(\"ExcludeUniversal\")'/>\r\n" +
				"<label for='ExcludeUniversalCheckBox'>Exclude Universal Parts</label>\r\n" +
			"</span>";
		html +=
			"<span class='YMMCheckBox'>\r\n" +
				"<input " + (YMM.IncludeExotic ? "checked='checked' " : "") + " id='IncludeExoticCheckBox' type='checkbox' onclick='ChangeYMMCheckBox(\"IncludeExotic\")'/>\r\n" +
				"<label for='IncludeExoticCheckBox'>Include Exotic Makes</label>\r\n" +
			"</span>";
		html +=
			"<span class='YMMCheckBox'>\r\n" +
				"<input " + (YMM.IncludeUncommon ? "checked='checked' " : "") + " id='IncludeUncommonCheckBox' type='checkbox' onclick='ChangeYMMCheckBox(\"IncludeUncommon\")'/>\r\n" +
				"<label for='IncludeUncommonCheckBox'>Include Uncommon Models</label>\r\n" +
			"</span>";
		$(".YMMSearchOptions").html(html);

		var url = document.location.toString();
		var character = "?";
		if(url.indexOf("?") >= 0 && url.indexOf("?") + 1 == url.length)//url ends with "?"
		{
			character = "";
		}
		else if(url.indexOf("?") >= 0)//url contains querystring paramaters
		{
			character = "&";
		}
		if (YMM.DataCount > 3) {
			$(".YMMSearchButton").focus();
			$(".YMMSearchButton").attr("class", "YMMSearchButton Enabled");
			if (YMM.IsTemplatePage) {
				$(".YMMSearchButton").attr("href", url + character + "search=t");
			}
			else {
				$(".YMMSearchButton").attr("href", "/ka/store/productfilter.aspx?search=t");
			}
		}
		else {
			$(".YMMSearchButton").attr("class", "YMMSearchButton Disabled");
		}
		$(".YMMResetLink").attr("href", url + character + "RESETYMM=t");
	}
	else {
		$(".ShellYMMSearch").attr("class", "ShellYMMSearch Disconnected");
		$(".ShellYMMSearch").html("Year / Make / Model search is currently not available due to website maintenance");
	}
}

function ValueFromId(id) {
	prefix = "Store_content_";
	if (document.getElementById(prefix + id) == null) {
		prefix = "ctl00_" + prefix;
	}
	return $("#" + prefix + id).val();
}

function getParameterByName(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
}

