		jQuery(function($){
			var product_types,companies;
			var product_types_i,companies_i;
			product_types_i = 0;
			//$('#grading').attr('disabled', 'disabled');
			//$('#company').attr('disabled', 'disabled');
			$('#view_all_companies_button').attr('style','display:none');
			function set_product_types(){
				$('#product_type_caption').attr('value',product_types[this.id].caption);
				$('#product_type_id').attr('value',product_types[this.id].id);
				$('#product_type').html('');
				product_types_i=0;
				submit_product_type();
			};
			function set_company(){
				$('#company').attr('value',companies[this.id].caption);
				$('#company_list').html('');
				companies_i=0;
				submit_company();
			};			
			function submit_company(){
				$('#infoblock_three').submit();
			}
			function submit_product_type(){
				//$('#grading').attr('disabled', 'disabled');
				//$('#company').attr('disabled', 'disabled');
				get_grading_options();
				$('#grading').attr('disabled', false);
				$('#company').attr('disabled', false);
				hide_all_companies()
				//$('#view_all_companies_button').attr('style','display:block');
			}
			function get_grading_options(){
				p_type = $('#product_type_id').attr('value');
				if(p_type){
					$.post(
						ajax_server,
						{
							'get':'grading',
							'product_type':p_type
						},
						function(results){
							$('#grading').attr('disabled', '');
							$('#grading').empty();
							$('#grading').append(results)
						}
					);
				}				
			}
			function hide_all_product_types(){
				$('#view_all_product_types').html('');
				$('#hide_all_product_types_button').attr('style','display:none');
				$('#view_all_product_types_button').attr('style','display:block');
				return false;
			}
			function hide_all_companies(){
				$('#view_all_companies').html('');
				$('#hide_all_companies_button').attr('style','display:none');
				//$('#view_all_companies_button').attr('style','display:block');
				return false;
			}			
			function view_all_product_types(){
				$.post(
					ajax_server,
					{
						'get':'all_product_types'
					},
					function(results){
						if(results.length){
							var o = eval('('+results+')');
							$('#view_all_product_types').html(o.data);
							$('#hide_all_product_types_button').attr('style','display:block');
							$('#view_all_product_types_button').attr('style','display:none');
							$('#all_product_types_sel').click(function(){
								var selected = $('#all_product_types_sel option[value='+$('#all_product_types_sel').attr('value')+']');
								$('#product_type_caption').attr('value',selected.text());
								$('#product_type_id').attr('value',selected.attr('value'));
							});
							$('#all_product_types_sel').keydown(function(e){
								if(e.keyCode==13){
									var pt_id = $('#product_type_id').attr('value');
									if(pt_id){
										submit_product_type();
									}									
								}
							});	
							$("#all_product_types_sel").bind("dblclick", function(){
								var pt_id = $('#product_type_id').attr('value');
								if(pt_id){
									submit_product_type();
								}
						    });
						}
					}
				);				
				return false;
			}
			function view_all_companies(){
				$.post(
					ajax_server,
					{
						'get':'all_companies',
						'product_type':$('#product_type_id').attr('value'),
						'grading':$('#grading').attr('value')
					},
					function(results){
						if(results.length){
							var o = eval('('+results+')');
							$('#view_all_companies').html(o.data);
							$('#hide_all_companies_button').attr('style','display:block');
							$('#view_all_companies_button').attr('style','display:none');
							$('#all_companies_sel').click(function(){
								var selected = $('#all_companies_sel option[value='+$('#all_companies_sel').attr('value')+']');
								$('#company').attr('value',selected.text());
							});
							$('#all_companies_sel').keydown(function(e){
								if(e.keyCode==13){
									var c_id = $('#company').attr('value');
									if(c_id){
										submit_company();
									}									
								}
							});	
							$("#all_companies_sel").bind("dblclick", function(){
								var c_id = $('#company').attr('value');
								if(c_id){
									submit_company();
								}
						    });
						}
					}
				);				
				return false;
			}
			$('#view_all_product_types_button').click(view_all_product_types);
			$('#hide_all_product_types_button').click(hide_all_product_types);
			$('#view_all_companies_button').click(view_all_companies);
			$('#hide_all_companies_button').click(hide_all_companies);			
			$('#product_type_caption').keydown(function(e){
				// alert(e.keyCode)				
				// 40 - down; 38 - up; 13 - enter
				if(e.keyCode==40){
					var list = $('#product_type_ul li');
					if(list.length){
						list.attr('class','');
						if(list.length-1>product_types_i)
							product_types_i++;
						list[product_types_i].className='current';	
						$('#product_type_id').attr('value',product_types[list[product_types_i].id].id);
					}				
				}
				if(e.keyCode==38){
					var list = $('#product_type_ul li');
					if(list.length){
						list.attr('class','');
						if(0<product_types_i)
							product_types_i--;
						list[product_types_i].className='current';
						$('#product_type_id').attr('value',product_types[list[product_types_i].id].id);
					}	
				}
				if(e.keyCode==13){
					var curr = $('#product_type_ul li.current');
					if(curr.length){
						//$('#product_type_id').attr('value',product_types[curr.attr('id')].id);
						$('#product_type_caption').attr('value',product_types[curr.attr('id')].caption);
						$('#product_type').html('');
						product_types_i=0;
						submit_product_type();
					}
					else{
						var pt_id = $('#product_type_id').attr('value');
						if(pt_id){
							submit_product_type();
						}
					}
					return false;
				}
			});
			$('#company_caption').keydown(function(e){
				// alert(e.keyCode)				
				// 40 - down; 38 - up; 13 - enter
				if(e.keyCode==40){
					var list = $('#company_ul li');
					if(list.length){
						list.attr('class','');
						if(list.length-1>companies_i)
							companies_i++;
						list[companies_i].className='current';	
					}				
				}
				if(e.keyCode==38){
					var list = $('#company_ul li');
					if(list.length){
						list.attr('class','');
						if(0<companies_i)
							companies_i--;
						list[companies_i].className='current';
					}	
				}
				if(e.keyCode==13){
					var curr = $('#company_ul li.current');
					if(curr.length){
						$('#company').attr('value',companies[curr.attr('id')].caption);
						$('#company_list').html('');
						companies_i=0;
						submit_company();
					}
					return false;
				}
			});			
			$('#product_type_caption').keyup(function(e){
				var q = $('#product_type_caption').attr('value');
				if(!q.length){
					$('#product_type').html('');
				}
				if(e.keyCode!=40 && e.keyCode!=38 && e.keyCode!=13 && q.length)
				$.post(
					ajax_server,
					{
						'get':'product_type',
						'q':q
					},
					function(results){
						if(results.length){
							var o = eval('('+results+')');
							product_types=o.object;
							$('#product_type').html(o.data);
							product_types_i=0;
							//$('#product_type_caption').attr('value',o.def.caption);
							$('#product_type_id').attr('value',o.def.id);
							$('#product_type_ul li').click(set_product_types);
						}
					}
				);				
			});
			$('#company').keyup(function(e){
				var q = $('#company').attr('value');
				if(!q.length){
					$('#company_list').html('');
				}
				if(e.keyCode!=40 && e.keyCode!=38 && e.keyCode!=13 && q.length)
				$.post(
					ajax_server,
					{
						'get':'company',
						'q':q,
						'product_type':$('#product_type_id').attr('value'),
						'grading':$('#grading').attr('value')
					},
					function(results){
						if(results.length){
							var o = eval('('+results+')');
							companies=o.object;
							$('#company_list').html(o.data);
							companies_i=0;
							$('#product_type_ul li').click(set_company);
						}
					}
				);				
			});			
		});			