var current_text = '';
function check_models(input, type) {
	var parent = $j('#quick_form');
	$j('#cart-tables table input').removeClass('active');
	if (input.value.length >= 3) {
		if (current_text != input.value) {		
			$j.ajax({
				type: "post",
				url: "/quick_order.php",
				dataType: "json",
				data: "value="+encodeURIComponent(input.value)+"&c="+type,
				success: function(data) {
					parent.children('div.quick_results').remove().end().append(data.results).children('div.quick_results').addClass('modal');
					var pos = $j(input).position();
					parent.children('div.quick_results').css({'left': pos.left + 2, 'top': pos.top + 23});
					var size = getPageSize();
					$j('#click_off').css('height',size[1]).show();
					$j(input).addClass('active');
				}
			});
		}
		else {
			parent.children('div.quick_results').show().addClass('modal');
			var size = getPageSize();
			$j('#click_off').css('height',size[1]).show();
			$j(input).addClass('active');
		}
	}
	else {
		parent.children('div.quick_results').hide();
	}
	current_text = input.value;
}

function set_item(anchor, product_code, name, price) {
	var parent = $j(anchor).parents('div.quick_results');
	parent.hide();
	$j('#click_off').hide();
	var row = $j('#cart-tables table input.active').parents('td:first').parent();
	row.find('input.product_code').val(product_code).end().find('input.name').val(name);
	row.find('td.price').html(format_currency(price));
	row.find('input.qty').val(1);
	update_price(row.find('input.qty')[0]);
}

function update_price(input) {
	var row = $j(input).parents('tr');
	if (!isNaN(parseInt(input.value))) {
		var total = row.find('td.price').html() * parseInt(input.value);
	}
	else {
		var total = 0;
	}
	row.find('td.product_total').html(format_currency(total));
}

function add_new_model() {
	var blank = $j('#cart-tables.quick_order tr:eq(1)').clone(true);
	blank.find('input').val('').end().find('td.price, td.product_total').html('0');
	$j('#cart-tables.quick_order tr:last').before(blank);
}

$j(function () {
	$j('#cart-tables.quick_order td div.input').round_corners({options: 'all', bordercolor: '#dadada'});
	$j('#cart-tables.quick_order input.qty').keyup(function () {
		update_price(this);
	});
	$j('#cart-tables.quick_order input.product_code').keyup(function () {
		check_models(this, 'product_code');
	}).click(function () {
		check_models(this, 'product_code');
	});
	$j('#cart-tables.quick_order input.name').keyup(function () {
		check_models(this, 'name');
	}).click(function () {
		check_models(this, 'name');
	});
	$j('#cart-tables table div:not(.option)').each(function (index) {
		$j(this).css('z-index', index);
	});
	
	$j('#quick_form input').keypress(function (e) {
		if (e.keyCode == '13') {
			$j('#quick_form').submit();
			return false;
		}
	});
});

