$(document).ready(function() {
	$("input.input-date").inputDate();
	$("input.input-month").inputMonth();
	$("input.input-time").inputTime();
	
	var nav = $(".horizontal-menu").tabs({
		show: function(event, ui) {
			ntt.setValue("_TABINDEX_", ui.index);
		}
	});
	var EventKey = {
		CTRL: 17,
		ALT: 18,
		LEFT: 37,
		RIGHT: 39,
		NUM_0: 48,
		NUM_1: 49,
		NUM_2: 50,
		NUM_3: 51,
		NUM_4: 52,
		NUM_5: 53,
		NUM_6: 54,
		NUM_7: 55,
		NUM_8: 56,
		NUM_9: 57
	};
	
	var tabbable = false;
	$(document).keydown(function(event) {
		if (event.keyCode == EventKey.CTRL)
			tabbable = true;
	}).keyup(function(event) {
		if (event.keyCode == EventKey.CTRL) {
			tabbable = false;
		}
		if (tabbable) {
			/*
			if (event.keyCode >= EventKey.NUM_1 && event.keyCode <= EventKey.NUM_9) {
				nav.tabs('select', event.keyCode - 49);
			}
			*/
			if (event.keyCode == EventKey.LEFT) {
				var selectedIndex = nav.tabs('option', 'selected');
				if (selectedIndex > 0)
					nav.tabs('select', selectedIndex - 1);
			} else if (event.keyCode == EventKey.RIGHT) {
				var selectedIndex = nav.tabs('option', 'selected');
				nav.tabs('select', selectedIndex + 1);
			}
		}
	});
	


});

MessageUtils = {
	data: {},
	
	createErrorWrapper: function() {
		if ($("#notification .messages.error").length == 0)
			$("<div class=\"messages error\"><ul></ul></div>").appendTo("#notification");
	},
	
	addError: function() {
		this.createErrorWrapper();
		
		var msg = arguments[0].trim();
		for (var i = 1; i < arguments.length; i++) {
			msg = msg.replace("{" + (i - 1) + "}", arguments[i]);
		}
		
		var html = "<li>" + msg + "</li>";
		var messages = $("#notification .messages.error ul li");
		for (var i = 0; i < messages.length; i++) {
			if ($(messages[i]).html() == msg)
				return;
		}
		$(html).appendTo(".messages.error ul");
	},
	
	clearError: function() {
		$("#notification .messages.error").remove();
	},
	
	addMessage: function() {
		
	},
	
	getMessage: function() {
		if (arguments.length == 0)
			return "";

		var key = arguments[0];
		var value = this.data[key];
		if (value == null)
			return "???" + key + "???";
			
		for (var i = 1; i < arguments.length; i++) {
			value = value.replace("{" + (i - 1) + "}", arguments[i]);
		}
		
		return value;
	},
	getMessageWithKey: function() {
		if (arguments.length == 0)
			return "";

		var key = arguments[0];
		var value = this.data[key];
		if (value == null)
			return "???" + key + "???";
			
		for (var i = 1; i < arguments.length; i++) {
			value = value.replace("{" + (i - 1) + "}", this.getMessage(arguments[i]));
		}
		
		return value;
	}
		
};

/*
function createErrorWrapper() {
	if ($("#notification .messages.error").length == 0)
		$("<div class=\"messages error\"><ul></ul></div>").appendTo("#notification");
}

function appendError() {
	createErrorWrapper();
	
	var msg = arguments[0].trim();
	
	for (var i = 1; i < arguments.length; i++) {
		msg = msg.replace("{" + (i - 1) + "}", arguments[i]);
	}
	
	var html = "<li>" + msg + "</li>";
	var messages = $("#notification .messages.error ul li");
	for (var i = 0; i < messages.length; i++) {
		if ($(messages[i]).html() == msg)
			return;
	}
	$(html).appendTo(".messages.error ul");
}

function clearError() {
	$("#notification .messages.error").remove();
}

function appendMessage(msg) {
	
}
*/

function menu_toggle(thiz) {
	var menu = $(thiz).parents("li");
	if (menu.hasClass("open"))
		menu.removeClass("open");
	else
		menu.addClass("open");
	return false;
}

;(function($) {
	$.fn.autocompleteProduct = function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: ACTION_PATH + "/ajax/list/ibatis?sqlId=common.getProductAll",
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.product_no,
						result : row.product_no
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.product_no;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);
					/*
					if (data == null) {
						if (this.value != "") {
							// clear
						}
						stockoutOrder_change(this);
					} else {
						// value
						stockoutOrder_change(this);
					}
					*/
				});
				
			}
		});
	};
	
	$.fn.isDuplicated = function(fnGetValue) {
		if (fnGetValue == null) {
			fnGetValue = function() {
				return $(this).val();
			};
		}
		var existedMap = {};
		var duplicated = false;
		this.each(function() {
			// no more check if duplicated
			if (duplicated != true) {
				// call value function
				var value = fnGetValue.call(this);
				// does not check empty field
				if (value != null && value != "") {
					// check duplication
					if (existedMap[value] == null) {
						existedMap[value] = true;
					} else {
						duplicated = true;
					}
				}
			}
		});
		return duplicated;
	};
	
	$.fn.autocompleteSerial = function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: ACTION_PATH + "/ajax/list/ibatis?sqlId=common.getSerialOfProduct",
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.serial_no,
						result : row.serial_no
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.serial_no;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);							
				});				
			}
		});
	};
	$.fn.autocompleteProductByWhCs = function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: ACTION_PATH + "/ajax/list/ibatis?sqlId=common.getProductNoByWhCs",
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.product_no,
						result : row.product_no
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.product_no;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);							
				});				
			}
		});
	};
	
	$.fn.autocompleteStockInNoByProduct = function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: ACTION_PATH + "/ajax/list/ibatis?sqlId=common.getStockInNoByProduct",
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.stock_in_no,
						result : row.stock_in_no
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.stock_in_no;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);							
				});				
			}
		});
	};
	
	$.fn.autocompleteFromLocationByStockIno = function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: ACTION_PATH + "/ajax/list/ibatis?sqlId=common.getFromLocationByStockInNo",
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.loc_code,
						result : row.loc_code
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.loc_code;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);							
				});				
			}
		});
	};
	$.fn.autocompleteToLocationByProduct = function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: ACTION_PATH + "/ajax/list/ibatis?sqlId=common.getToLocationByProduct",
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.loc_code,
						result : row.loc_code
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.loc_code;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);							
				});				
			}
		});
	};
	$.fn.autocompletePalletCode= function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: ACTION_PATH + "/ajax/list/ibatis?sqlId=common.getPalletBarcode",
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.pallet_barcode,
						result : row.pallet_barcode
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.pallet_barcode;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);							
				});				
			}
		});
	};
	$.fn.autocompleteFromLocationByPalletCode = function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: ACTION_PATH + "/ajax/list/ibatis?sqlId=common.getFromLocationByPalletCode",
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.loc_code,
						result : row.loc_code
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.loc_code;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);							
				});				
			}
		});
	};
	$.fn.autocompleteToLocationByPalletCode = function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: ACTION_PATH + "/ajax/list/ibatis?sqlId=common.getToLocationByPalletCode",
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.loc_code,
						result : row.loc_code
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.loc_code;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);							
				});				
			}
		});
	};	
	$.fn.autocompleteProductNoQuickRearr = function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: ACTION_PATH + "/ajax/list/ibatis?sqlId=common.getProductNoByWhCs",
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.product_no,
						result:row.product_no					
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.product_no ;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);							
				});				
			}
		});
	};
	
	$.fn.autocompleteItem = function(opt, fnSuccess, fnNotMatched) {
		var options = $.extend({
			sqlId: opt.autoUrl,
			extraParams : {},
			minChars : 0,
			dataType : "json",
			matchContains : "word",
			max: 15,
			cacheLength: 10,
			autoFill : false,
			mustMatch : true,
			mouseDownOnSelect: true,
			parse : function(data) {
				return $.map(data, function(row) {
					return {
						data : row,
						value : row.item,
						result : row.item
					}
				});
			},
			formatItem : function(row, i, max) {
				return row.item;
			}
		}, opt);
		// today
		// valid pattern
		return this.each(function() {
			// working with just text box
			if ($(this).is("input[type='text']")) {
				var thiz = $(this);
				thiz.autocomplete(options.sqlId, options).result(function(event, data, formatted) {
					if (data == null)
						fnNotMatched.call(this);
					else
						fnSuccess.call(this, data);							
				});				
			}
		});
	};
	
	$.fn.disableTemporality = function(timeout) {
		if (timeout == null)
			timeout = 2000;
		// valid pattern
		return this.each(function() {
			var thiz = $(this);
			thiz.attr("disabled", "disabled");
			// enable in seconds
			setTimeout(function() {
				thiz.removeAttr("disabled");
			}, timeout);
		});
	};
	
})(jQuery);

function fixParentTarget(thiz) {
	$(thiz).parents("form").attr("target", "_parent");
	return true;
}

function submitParent() {
	if ($(parent) != null)
		$(parent.document).find("form").submit();
}

