var _L12N_ = {}
var _lang = $.cookie("lang")

if (_lang=="cn_zh")
{
	_L12N_["REQ"]             = "必须填写";
	_L12N_["REQ_INT"]         = "必须为整数";
	_L12N_["REQ_NUM"]         = "必须为数目";
	_L12N_["REQ_DATE"]        = "必须为正确日期";
	_L12N_["REQ_EMAIL"]       = "必须为电邮地址"
	_L12N_["REQ_MAXLENGTH1"]  = "必须少于";
	_L12N_["REQ_MAXLENGTH2"]  = "字符";
	_L12N_["PLS_RETRY"]       = "请先更正下列错误后再次提交";
}
else if (_lang=="tw_zh")
{
	_L12N_["REQ"]             = "必須填寫";
	_L12N_["REQ_INT"]         = "必須爲整數";
	_L12N_["REQ_NUM"]         = "必須爲數目";
	_L12N_["REQ_DATE"]        = "必須爲正確日期";
	_L12N_["REQ_EMAIL"]       = "必須為電郵地址"
	_L12N_["REQ_MAXLENGTH1"]  = "必須少于";
	_L12N_["REQ_MAXLENGTH2"]  = "字符";
	_L12N_["PLS_RETRY"]       = "請先更正下列錯誤後再次提交";
}
else
{
	_L12N_["REQ"]            = " cannot be empty"
	_L12N_["REQ_INT"]        = " must be an integer"
	_L12N_["REQ_NUM"]        = " must be numeric value"
	_L12N_["REQ_DATE"]       = " must be a valid date"
	_L12N_["REQ_EMAIL"]      = " must be a valid email"
	_L12N_["REQ_MAXLENGTH1"] = " cannot contains more than "
	_L12N_["REQ_MAXLENGTH2"] = " characters"
	_L12N_["PLS_RETRY"]      = "Please correct the following problem and submit again.\n";
}

function registerOnKeyDown()
{
	$(".inputtext, select").bind("keydown", function(e) {
		if (keycode(e) == 13)
		{
			var o = window.event ? e.srcElement : e.target;
			if (o.form.next)
				o.form.next(e)
		}
	})
}

// 使用方法： 在jsp页面调用 .自定义rules 格式为{input name :["onflcus text","validate type", true(not allow empty) or false(allow empty)]，left, top}
// eg:
//registerOnBlurValidation({
//	email:     ["Eg. yourname@yourcompany.com", "email", false],
//	number:     ["Eg. +86-88888888-888", "phone", false, 200, 100],
//})
//left 和 top 为左偏移和上偏移数字 可以缺省
//
function registerOnBlurValidation(rules){
	$("input, textarea").bind("focus", function(e) {
		if (rules[this.name]){
			var length =rules[this.name].length
			onBlurV_display(this, rules[this.name][0], length > 3 ? rules[this.name][3] : 0, length > 4 ? rules[this.name][4] : 0)
		}
	})
	$("input, textarea").bind("blur", function(e) {
		var name = this.name

		if (!rules[name] || rules.length < 4)
			return $("#OnBlurTipsDv_" + name).hide()

		var val = v(this)

		if (rules[name][2] && !val) // check mandatory
			return onBlurV_display2(this, false)
		if (!val)
			return $("#OnBlurTipsDv_" + name).hide()

		var bool = 1
		var type = rules[name][1]
		if (type == "email")
			bool = isEmail(val)
		else if (type == "phone")
			bool = isPhone(val)
		else if (type == "zipcode")
			bool = /^\d{3}-\d{3}$/.test(val)
		else if (type == "password")
			bool = val.length >= 6 && val.length <= 12 && (this.name != "password2" || this.form.password.value == val)

		onBlurV_display2(this, bool)
	})
}
function onBlurV_display(ctl, text, left, top){

	var div = gE("OnBlurTipsDv_" + ctl.name)
	if (div == null){
		var x = $(ctl).offset().left + $(ctl).width() + 8 +left
		var y = $(ctl).offset().top + top - 2
		var div = $("<div id='OnBlurTipsDv_" + ctl.name + "' class='OnBlurTipsDv' style='position:absolute; padding:2px 8px 2px 20px' ></div>")
		div.css("left",x+"px");
		div.css("top",y+"px");
		div.appendTo($("body"))
	}

	$(div).css({
		background: "#E2F5FF url(/apr/static/images/common/info.png) no-repeat",
		border:     "1px solid #00A8FF"
	}).show().text(text)
}
function onBlurV_display2(ctl, bool){
	var div = $("#OnBlurTipsDv_" + ctl.name).show()
	if (bool)
		div.css({
			background: "url(/apr/static/images/common/okay.png) no-repeat",
			border:     "none"
		})
	else
		div.css({
			background: "#FFF2E9 url(/apr/static/images/common/warning.png) no-repeat",
			border:     "1px solid #FF6600"
		})

	if (bool)
		div.html("")
}



function linkupInputs(o1, o2)
{
	o1.onkeyup = function(){
		if(o1.value.length > 3)
		o2.focus()
	}
	o2.onfocus=function(){
		o2.select();
	}
}



Checker.prototype.checkZipCode_KR = function(input, msg)
{
	var val = this.valueOf(input)

	if (val && !/^\d{3}(-| )?\d{3}$/.test(val))
		return this.appendErr(input, msg);

	return true;
}
Checker.prototype.checkPhone_KR = function(input1, input2, input3, msg)
{
	var val1 = this.valueOf(input1)
	var val2 = this.valueOf(input2)
	var val3 = this.valueOf(input3)

	if ((val1 || val2 || val3) && (!/^\d{2,4}$/.test(val1) || !/^\d{3,4}$/.test(val2) || !/^\d{3,4}$/.test(val3)))
		return this.appendErr(input1, msg) || this.appendErr(input2, "") || this.appendErr(input3, "");

	return true;
}

function updateLoginIDInput(form)
{
	var m = v(form.membershipStatus)
	var r = v(form.responseStatus)
	var bool = r == 10012 || m == 10002 || m == 10005 || m == 10006

	if (bool)
		$("#tr_input_loginID td:first").html("<span style='color: red'>* </span>Web Login ID: ")
	else
		$("#tr_input_loginID td:first").html("Web Login ID: ")

	form.chk["loginID"] = ["text", bool]
}

function checkDupEmail(email)
{
	if (!isEmail(email))
		alert("이메일이 올바르지 않습니다.")
	else
		$.getJSON("${contextPath}/user/member_checkEmail.action?email=" + email, function(json) { alert(!json.duplicated ? "사용 가능한 아이디 입니다." : "이미 사용중인 이메일입니다. 다른 이메일을 입력해 주세요.") })
}

function checkDupLoginID(loginID)
{
	$.getJSON("${contextPath}/user/member_checkLoginID.action?loginID=" + loginID, function(json) { alert(!json.duplicated ? "사용 가능한 아이디 입니다." : "이미 사용중인 아이디 입니다. 다시 입력해주세요.") })
}

$(function(){
	if (!$.cookie("TIMEZONE"))
	{
		var now = new Date();
		var offset = (now.getTimezoneOffset() / 60) * -1;
		$.cookie("TIMEZONE", (offset > 0 ? "GMT+" : "GMT") + offset)
	}

	$("a[href^=mailto:]").each(function(){
		this.href = "javascript:mailTo('" + this.href.replace(/@/g,'%40') + "')"
	})
})

$.fn.defVal = function()
{
	$(this).find(":text, :textarea").each(function(){
		var inp = $(this)

		if (inp.attr("default_value"))
		{
			this.value = inp.attr("default_value")
			inp.bind("focus", function() { if ($(this).attr("default_value") == this.value) this.value = "" })
			inp.bind("blur",  function() { if (!this.value) this.value = $(this).attr("default_value") })
		}
	})
}
$.fn.clearDefVal = function()
{
	$(this).find(":text, :textarea").each(function(){
		if (this.value && this.value == $(this).attr("default_value"))
			this.value = ""
	})
}

function dl(id, file, path)
{
	if (path)
		jumpTo("/apr/content/" + path)
	else
		$('#ifDl').attr("src", JS_BASEPATH + "/img.jsp?id=" + id + "&col=" + file + "&inline=false")
}

if (typeof _page != "undefined")
	_page.email = function(title, country) {
		if (s) {
			s.linkTrackVars="events,eVar3";
			s.linkTrackEvents="event14,event15";
			s.events="event14,event15";
			s.eVar3=s.pageName;
			s.tl(true, "o", s.pageName + ":tell_a_friend");
			s.linkTrackVars="None";
			s.linkTrackEvents="None";
		}
		window.setTimeout(function () {
			var strURL = "", strTitle = "";
			try {strURL = encodeURICompent(document.location);} catch (e) {strURL = escape(document.location)}
			strTitle = "Philips Lighting " + country + " - " + title
			window.location = 'mailto:?subject=' + strTitle + '&body=Please see ' + strURL;
		}, 1000);
	};

function mailTo(href) {
	s.linkTrackVars="events, eVar3";
	s.linkTrackEvents="event34";
	s.events = "event34";
	s.eVar3  = s.pageName;
	s.tl(true, "o", s.pageName+":"+"mailto");
	window.location = unescape(href)
}



// SUPPORT OLD VERSION
// generic for popup windows
function popupWin(url,w,h,wname,x,y) {
	w = w || 640
	h = h || 400
	x = x || (window.screen.width - w) / 2
	y = y || (window.screen.height - h) / 2
	__win=window.open(url,wname,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,screenX='+x+',screenY='+y+',top='+y+',left='+x+',resizable=1,width='+w+',height='+h)
	setTimeout('__win.focus()', 500)
}

function popupWinScroll(url,w,h,scrollbar,wname,x,y) {
	w = w || 640
	h = h || 400
	x = x || (window.screen.width - w) / 2
	y = y || (window.screen.height - h) / 2
	scrollbar= scrollbar || 0
	__win=window.open(url,wname,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+scrollbar+',screenX='+x+',screenY='+y+',top='+y+',left='+x+',resizable=1,width='+w+',height='+h)
	setTimeout('__win.focus()', 500)
}

function popupWinPPDiscussion(url,w,h,wname,x,y) {
	w = w || 640
	h = h || 400
	x = x || (window.screen.width - w) / 2
	y = y || (window.screen.height - h) / 2
	return window.open(url,wname,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,screenX='+x+',screenY='+y+',top='+y+',left='+x+',resizable=1,width='+w+',height='+h)
}

// function for popup a center align window,w and h should be number, not string
function popupWinCentered(url,w,h,wname,opt) {
var x,y;
	w = w<640?640:w;
	h = h<400?400:h;
	x =  (window.screen.width - w) / 2;
	y =  (window.screen.height - h) / 2;
	opt=opt.replace(/ *(screen[XY]|top|left|width|height) *= *\w* *,?/ig,'');
	opt=opt.replace(/,$/,'');
	opt='screenX='+x+',screenY='+y+',top='+y+',left='+x+',width='+w+',height='+h+','+opt;
	opt=opt.replace(/,$/,'');
	window.open(url,wname,opt);
}
