
(function($) {

$.fn.autotab = function(options) {
	var defaults = {
		target: null,			// Where to auto tab to
		previous: null			// Backwards auto tab when all data is backspaced
	};

	$.extend(defaults, options);

	var key = function(e) {
		if(!e)
			e = window.event;

		return e.keyCode;
	};

	defaults.maxlength = 1;
	// Sets targets to element based on the name or ID passed
	if(defaults.target != null)
		defaults.target = $('#querydata input[name=' + defaults.target + ']')[0];

	if(defaults.previous != null)
		defaults.previous = $('#querydata input[name=' + defaults.previous + ']')[0];


	// IE does not recognize the backspace key
	// with keypress in a blank input box
	if($.browser.msie)
	{
		this.keydown(function(e) {
			if(key(e) == 8)
			{
				var val = this.value;

				if(val.length == 0 && defaults.previous) {
					defaults.previous.focus();
					defaults.previous.value = '';
				}
			}
		});
	}

	return this.keypress(function(e) {
		if(key(e) == 8)
		{
			var val = this.value;

			if(val.length == 0 && defaults.previous) {
				defaults.previous.focus();
				$(defaults.previous)[0].value = '';
			}
		}
	}).keyup(function(e) {
		var val = this.value;
		var pattern = new RegExp('[^a-zA-Z\*]+', 'g');
		var val = val.replace(pattern, '');
		this.value = val;
		var keys = [8, 9, 16, 17, 18, 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 144, 145];
		var string = keys.toString();

		if(string.indexOf(key(e)) == -1 && val.length == defaults.maxlength && defaults.target)
			defaults.target.focus();
			
	});
};

})(jQuery);


function checkAll(field,val) {
	$('#querydata input[@name="'+field+'[]"]').attr('checked',val);
}
function checkValues(field) {
	var checkboxes = $('#querydata input[@name="'+field+'[]"]');
	var val = 0;
	for (var i =0; i < checkboxes.length; i++) {
		if (checkboxes[i].checked) { val += Math.pow(2,checkboxes[i].value); }
	}
	if ((val == 0) || (val == (Math.pow(2,checkboxes.length) - 1))) { return ''; }
	return field + '=' + val;
}
$('#querydata').submit(function(){
	var attr = new Array();
	attr.push($('#querydata input[type=text][value]').serialize());
	attr.push($('#querydata input[type=hidden]').serialize());
	attr.push(checkValues('q'));
	attr.push(checkValues('p'));
	attr.push(checkValues('o'));
	if ($('#querydata input[name=pr]')[0].value) { attr.push('po='+$('#querydata select[name=po]')[0].value); }
	if ($('#querydata input[name=d]')[0].value) { attr.push('do='+$('#querydata select[name=do]')[0].value); }
	var theurl = '';
	if (attr.length > 0) {
		for (var i = 0; i < attr.length; i++) {
			if (attr[i]) {
				if (theurl)
					theurl += '&';
				theurl += attr[i];
			}
		}
	}
	if (theurl) { theurl = 'index.php?' + theurl; }
	document.location.href = '/' + theurl;
	return false;
});
$('.venue a').click(function(){
	pageTracker._trackPageview("/clicks/venue/"); 
});
$('.currentauction a').click(function(){
	pageTracker._trackPageview("/clicks/forsale/"); 
});
$('#auctions tr').mouseover(function(){
	$(this).css({background:'#EDE7DD'});
});
$('#auctions tr').mouseout(function(){
	$(this).css({background:'#FFFFFF'});
});
$('#auctions tr').click(function(){
	pageTracker._trackPageview("/clicks/auction/"); 
	//document.location.href = $(this).find('a').attr('href');
	window.open($(this).find('a').attr('href'));
	return false;
});
$(document).ready(function() {
	$('#querydata input[name=l1]').autotab({ target: 'l2' });
	$('#querydata input[name=l2]').autotab({ target: 'l3', previous: 'l1' });
	$('#querydata input[name=l3]').autotab({ target: 'l4', previous: 'l2' });
	$('#querydata input[name=l4]').autotab({ previous: 'l3' });
});
