$(document).ready(function() {
	showForm();
	
	$("ol.formTabs li a").click(function() {
		var hash = $(this).attr('href').toString().split("#")[1];
		showForm(hash);
	});
	
	$("div#forms form#searchForm select[name='make']").change(function() {
		var thisMake = $(this).val();
		var i, n;
		var modelsStr;
		
		for (i = 0; i < models.length; i++) {
			if (models[i][0] == thisMake) {
				modelsStr = "";
				for (n=1; n<models[i].length; n++) {
					modelsStr += '<option value="' + models[i][n] + '">' + models[i][n] + '</option>'
				}
				$("div#forms form#searchForm select[name='model']").html(modelsStr);
			};
		};
		
	});
	
	if ($("div#forms form#sellersForm input:radio:checked").val() == "Value My Car Request") {
		$("div#forms form#sellersForm label[for='comments']").html("Vehicle Details");
	};
	
	$("div#forms form#sellersForm input:radio").change(function() {
		if ($(this).val() == "Value My Car Request") {
			$("div#forms form#sellersForm label[for='comments']").html("Vehicle Details");
		}
	});
	
	
	$("div#forms form").submit(function() {
		var formName = $(this).attr("id");
		if (formName != "searchForm" ) {
			// if ($("div#forms form#buyersForm input:radio:checked").val() == "Make a Finance Application") {
			// 	window.location = "finance.asp";
			// 	return false;
			// } else {
			// 	window.open('includes/forms.asp', formName, 'height=400,width=400,scrollbars=yes');
			// 	this.target = formName;
			// }
			
			window.open('includes/forms.asp', formName, 'height=400,width=400,scrollbars=yes');
			this.target = formName;
						
		} else {
			if ($("div#forms form#searchForm input:radio:checked").size() <= 0 ) {
				alert("please complete the form");
				return false;
			};
		}
	});
	
	function showForm(hash) {
		if (hash == undefined) {
			hash = String(window.location.hash).split("#")[1];
		}
		if (hash == "sellers") {
			$("div#forms form#sellersForm").removeClass("hide");
			$("div#forms iframe#searchForm").hide();
			$("div#forms form#buyersForm").addClass("hide");
			$("ol.formTabs li#sellers a").addClass("selected");
			$("ol.formTabs li#search a").removeClass("selected");
			$("ol.formTabs li#buyers a").removeClass("selected");
		} else if (hash == "search") {
			$("div#forms form#sellersForm").addClass("hide");
			$("div#forms iframe#searchForm").show();
			$("div#forms form#buyersForm").addClass("hide");
			$("ol.formTabs li#sellers a").removeClass("selected");
			$("ol.formTabs li#search a").addClass("selected");
			$("ol.formTabs li#buyers a").removeClass("selected");
		} else if (hash == "buyer") {
			$("div#forms form#sellersForm").addClass("hide");
			$("div#forms iframe#searchForm").hide();
			$("div#forms form#buyersForm").removeClass("hide");
			$("ol.formTabs li#sellers a").removeClass("selected");
			$("ol.formTabs li#search a").removeClass("selected");
			$("ol.formTabs li#buyers a").addClass("selected");
		} else {
			if ($("body").attr("id") == "home") {
				$("ol.formTabs li#search a").addClass("selected");
			};
		};
	};
});
