vw.include('/js/user/interface.js');
vw.include('/js/user/yuga.js');
vw.include('/js/user/SpryMenuBar.js');

/* 見積書/注文書ウィンドウ表示 */
vw.openSheet = function(url) {
	window.open(url,'quotesheet','width=740,height=500,toolbar=yes,location=no,status=no,scrollbars=yes,resizable=yes');
};

vw.lb = {
	_overlay:   null,
	_indicator: null,
	
	init: function()
	{
		this._overlay   = $('#vw_overlay').hide();
		this._indicator = $('#vw_indicator').hide();
		$('#searchForm-1').submit(function(){ return vw.lb.show('#pn-1'); });
		$('#searchForm-2').submit(function(){ return vw.lb.show('#pn-2'); });
		$('#searchForm-3').submit(function(){ return vw.lb.show('#pn-3', '#maker'); });
		$('#searchForm-4').submit(function(){ return vw.lb.show('#pn-4'); });
		$('#searchForm-5').submit(function(){ return vw.lb.show('#pn-5', '#maker'); });
		$('#searchForm-15').submit(function(){ return vw.lb.show('#pn-15'); });
	},
	show: function(pn, maker)
	{
		if (maker) {
			// 再生品検索専用
			if (!$(pn).val().length && !$(maker).val().length) {
				alert('型番・メーカ名のどちらかを入力/選択してください。');
				$(pn).focus().select();
				return false;
			}
		} else if (!$(pn).val().length) {
			// その他型番検索
			alert('型番を入力してください。');
			$(pn).focus().select();
			return false;
		}
		
		var h = 75, w = 250;
		var scroll = vw.window.scrollOffset();
		var client = vw.window.clientSize();
		
		var x = vw.util.max([vw.window.scrollSize().y, vw.window.clientSize().y]);
		this._overlay.css({height: x + 'px', display: 'block'});
		this._indicator.css({
			display: 'block',
			left:    Math.floor(scroll.x + (client.x - w) / 2) + 'px',
			top:     Math.floor(scroll.y + (client.y - h) / 2) + 'px',
			width:   w + 'px',
			height:  h + 'px'
		});
		return true;
	}
};
(function($){
	// ページの初期化
	$(function(){
		// SpryMenuBar
		new Spry.Widget.MenuBar('MenuBar1', {
			imgDown:  '/img/user/SpryAssets/SpryMenuBarDownHover.gif',
			imgRight: '/img/user/SpryAssets/SpryMenuBarRightHover.gif'
		});
		
		// search indicator
		vw.lb.init();
		
		// table row mouseover effect
		$('table.system-base tr')
			.bind('mouseover', function(){ $(this).find('td').css({backgroundColor:'#A1DCFF'}); })
			.bind('mouseout',  function(){ $(this).find('td').css({backgroundColor:''}); })
		;
		
		// input field focus effect
		$(':text,:password,textarea,select')
			.bind('focus', function(){ $(this).css({backgroundColor:'#EAFFB3'}); })
			.bind('blur',  function(){ $(this).css({backgroundColor:''}); })
		;
		
		// focus first input filed
		if ($('body').is('#U020')) {
			setTimeout(function(){ $('#quantity').focus().select(); }, 300);
		} else {
			setTimeout(function(){ $(':text:first').focus().select(); }, 300);
		}
		
		// logout confirm
		$('#status-nav-02 a').click(function(){
			return confirm('ログアウトしますか？');
		});
		
		// product order
		$('.productOrder').each(function(){
			$(this).submit(function(){
				var min = $(this).find('.order-min_qty').val() - 0;
				var max = $(this).find('.order-max_qty').val() - 0;
				var qty = $(this).find('.order-qty').val() - 0;
				if (!qty) {
					alert('数量を入力してください。');
					return false;
				} else if (isNaN(qty) || (Math.floor(qty) != qty)) {
					alert('数量は数値で入力してください。');
					return false;
				} else if (min && qty < min) {
					alert(min + '以下の注文はできません。');
					return false;
				} else if (max && max < qty) {
					alert(max + '以上の注文はできません。');
					return false;
				}
			});
		});
		
		// product result
		$('.order-qty').each(function(){
			$(this).change(function(){
				var id = this.id.split('_')[2];
				var target = '#span_order_qty_' + id;
				var lot    = $('#order_lot_' + id).val() - 0;
				$(target).html(vw.util.numberFormat(this.value * lot));
			});
		});
	});
})(jQuery);
