$(document).ready(function() {

	//Update structure drop-down on changing customer drop-down
	$(".customer-select").change(function() {
		var selectedCustomer = $(this).attr("value");
		$(".structure-select option").css("display", "none");
		$(".structure-select option.first").attr("selected", "selected");
		$(".structure-select option.master-" + selectedCustomer).css("display", "block");
	});


	//	$(".customer-select-search").change(function() {
	//		alert("test");
	//		var selectedCustomerSearch = $(this).attr("value");
	//		$(".structure-select-search option").attr("disabled","disabled");
	//		$(".structure-select-search option.first").attr("selected","selected");
	//		$(".structure-select-search option.master-" + selectedCustomer).attr("disabled","");
	//	});

	//Update structure id on changing structure drop-down
	$(".structure-select").change(function() {
		var selectedStructure = $(this).attr("value");
		if (selectedStructure == "") {
			//			alert("Nothing");
			$(".structureid").attr("value", "");
		} else {
			//			alert("Something");
			$.post("ajax_structure-name-to-ref.asp", { structureid: selectedStructure },
				function(data) {
					$(".structureid").attr("value", data);
				}, "text");
		}
	});

	//Update structure drop-down on changing structure id
	$(".structureid").blur(function() {
		var structureId = $(this).attr("value");
		$.post("ajax_structure-id-to-name.asp", { structureid: structureId },
			function(data) {
				if (data == "") {
					alert("Your chosen structure ID does not match a structure name in the database.  \nPlease select a structure from the 'Structure' drop-down above.");
				} else {
					$(".structure-select option[value='" + data + "']").attr("selected", "selected");
				}
			}, "text");
	});


	$("#submit").click(function() {
		var customerSelect = $(".customer-select").attr("value");
		if (customerSelect == "") {
			alert("Please select a customer");
			return false;
		}
		var structureSelect = $(".structure-select").attr("value");
		var structureId = $(".structureid").attr("value");
		if (structureSelect == "" || structureId == "") {
			alert("Please select a structure and structure id");
			return false;
		}
	});


	/*
	$(".structure-ref-select").change(function(){
	var selectedStructure = $(this).attr("value");
	$.post("ajax_structure-ref-to-name.asp", { structureid: selectedStructure },
	function(data){
	$(".structureid").attr("value",data);
	},"text");
	});
	*/

	/*
	$("td").hover(
	function(){
	$(this).contents("input").css("border","1px dashed #999");
	},
	function(){
	$(this).contents("input").css("border","1px solid #fff");
	}
	);
	*/



	/*
	$("input").focus(function(){
	$(this).css("background","#d1e5e5");
	});
	$("input").blur(function(){
	$(this).css("background","#fff");
	});
	$("select").focus(function(){
	$(this).css("background","#d1e5e5");
	});
	$("select").blur(function(){
	$(this).css("background","#fff");
	});
	$("textarea").focus(function(){
	$(this).css("background","#d1e5e5");
	});
	$("textarea").blur(function(){
	$(this).css("background","#fff");
	});
	*/
});
