$(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");
	});
	*/




	$('.cabinet').click(function () {
		$(this).css('background-image','url(img/ico_plus_green.gif)');
	});




});










$.fn.capitalize = function() {
    $(this).keypress(function(e) {
        if (e.target.createTextRange) {
                var r = document.selection.createRange().duplicate();
                r.moveEnd('character', e.target.value.length);
                if (r.text == '') mstart = e.target.value.length;
                else mstart = e.target.value.lastIndexOf(r.text);
                r.moveStart('character', -e.target.value.length);
                mend = r.text.length;
        } else {
                mstart = e.target.selectionStart;
                mend = e.target.selectionEnd;
        }
        if(e.which>96 && e.which<123) {
                e.preventDefault();
                e.stopPropagation();
                z = $(e.target).val();
                front = z.substring(0, mstart);
                back = z.substring(mend);
                $(e.target).val(front+String.fromCharCode(e.which-32)+back);
 
                if(e.target.createTextRange) { 
                    var range = e.target.createTextRange(); 
                    range.move("character", mend+1); 
                    range.select(); 
                } else if(e.target.selectionStart) { 
                    e.target.focus(); 
                    e.target.setSelectionRange(mend+1, mend+1); 
                }  
        }
    });
}
