function _next(name) {
	if(name == 'pnum01'){
		$('#pnum02').focus();
	}else if (name == 'pnum02'){
		$('#pnum03').focus();
	}else if (name == 'pnum03'){
		$('#pnum04').focus();
	}else if (name == 'pnum04'){
		$('#pnum05').focus();
	}else if (name == 'pnum05'){
		$('#pnum05').focus();
	}
}
function _prev(name) {
	if(name == 'pnum01') {
		$('#pnum01').focus();
	}else if (name == 'pnum02'){
		$('#pnum01').focus();
	}else if (name == 'pnum03'){
		$('#pnum02').focus();
	}else if (name == 'pnum04'){
		$('#pnum03').focus();
	}else if (name == 'pnum05'){
		$('#pnum04').focus();
	}
}
function _down(e) {
	var shift, ctrl, alt;

	if (e != null) {
		// Mozilla(Firefox, NN) and Opera
		keycode = e.which;
		alt = typeof e.modifiers == 'undefined' ? e.altKey : e.modifiers & Event.ALT_MASK;
		ctrl = typeof e.modifiers == 'undefined' ? e.ctrlKey : e.modifiers & Event.CONTROL_MASK;
		shift = typeof e.modifiers == 'undefined' ? e.shiftKey : e.modifiers & Event.SHIFT_MASK;
		// イベントの上位伝播を防止
		e.preventDefault();
		e.stopPropagation();
	} else {
		// Internet Explorer
		keycode = event.keyCode;
		alt = event.altKey;
		ctrl = event.ctrlKey;
		shift = event.shiftKey;
		// イベントの上位伝播を防止
		event.returnValue = false;
		event.cancelBubble = true;
	}
	// キーコードの文字を取得
	keychar = String.fromCharCode(keycode).toUpperCase();
	if (ctrl) {
		// Ctrl同時押しの場合
	} else 	if (alt) {
		// Alt同時押しの場合
	} else 	if (shift) {
		// Shift同時押しの場合
		if (keycode == 9) {
			_prev(this.name);
		}
	} else {
		// 通常のキーダウン時の場合
		if (keychar == "0" || keychar == "0" || keychar == "1" || keychar == "2" || keychar == "3" || keychar == "4" || keychar == "5" || keychar == "6" || keychar == "7" || keychar == "8" || keychar == "9") {
			this.value = keychar;
			_next(this.name);
			get_product_count();
		}
		if (keycode == 96 || keycode == 97 || keycode == 98 || keycode == 99 || keycode == 100 || keycode == 101 || keycode == 102 || keycode == 103 || keycode == 104 || keycode == 105) {
			this.value = '0' + keycode - 96;
			_next(this.name);
			get_product_count();
		}
		else if (keycode == 9) {
			// Tab
			_next(this.name);
		}
		else if (keycode == 37) {
			// ←
			_prev(this.name);
		}
		else if (keycode == 39) {
			// →
			_next(this.name);
		}
		else if (keycode == 46) {
			// Del
			this.value = '';
			this.focus();
			get_product_count();
		}
		else if (keycode == 8) {
			// BS
			this.value = '';
			_prev(this.name);
			get_product_count();
		}
	}
	return false;
}
function get_product_count(form, counter) {
	var param = $(form).serialize();
	$.ajax({
		type: 'POST',
		url: '/product/search/ajax/count.php',
		data: param,
		complete: function(rtn, status){
			if(counter != undefined){
				if(rtn.responseText != ""){
					$(counter).html(rtn.responseText);
				}else{
					$(counter).html('');
				}
			}
		}
	});
}




$(function() {
	$("#labelsizeform").observe(0.5, function(){
		get_product_count("#labelsizeform", "#labelsizeCountArea");
	});
	$("#harusizeform").observe(0.5, function(){
		get_product_count("#harusizeform", "#harusizeCountArea");
	});

	$("#searchform").observe(0.5, function(){
		get_product_count("#searchform","#countArea");
	});
	$("input.pnum").keydown(_down);
});

$.fn.observe = function( time, callback ){
	return this.each(function(){
		var form = this, change = false;
		var save = [];
		var i=0;
//		$(form.elements).each(function(){
		$(":input").each(function(){
			var type = $(this).attr("type");
			if(type == "checkbox" || type == "radio"){
				save[i] = $(this).attr('checked');
			}else{
				save[i] = $(this).val();
			}
			i++;
		});
		
		var sid = setInterval(function(){
			var j = 0;
//			$(form.elements).each(function(){
			$(":input").each(function(){
				var type = $(this).attr("type");
//alert(this.name);
				if(type == "checkbox" || type == "radio"){
					if(save[j] != $(this).attr('checked')){
						save[j] = $(this).attr('checked');
						change = true;
					}
				}else if(save[j] != $(this).val()){
					save[j] = $(this).val();
					change = true;
				}
				j++;
			});
			if(change){
				callback.call(form);
			}
			change = false;
		}, time * 1000);
	});
};

