var gId;
var gType;
$(document).ready(function() {
	$(".emailFindPw").click(function() {
		doGiveNewPw("email");
	});
	$(".mobileFindPw").click(function() {
		doGiveNewPw("ipin");
	});

	$("#nameForm").validn()
	.validnAdd("id", "required", "아이디을 입력하세요")
	.validnAdd("name", "required", "이름을 입력하세요")
	.validnAdd("juminNo1", "required; number; len(6)", "주민등록번호를 정확히 입력하세요.")
	.validnAdd("juminNo2", "required; number; len(7)", "주민등록번호를 정확히 입력하세요.")
	;
	$("#ipinForm").validn()
	.validnAdd("id", "required", "아이디을 입력하세요")
	;

	$("input[name=authType]").click(function() {
		if ( "N" == this.value ) {
			$(".personNumberObj").show();
			$(".ipinObj").hide();
		} else {
			$(".ipinObj").show();
			$(".personNumberObj").hide();
		}
	});
	$("input[name=juminNo1]").keyup(function() {
		if ( this.value.length==6) {
			try { $("input[name=juminNo2]").focus(); } catch(e) {}
		}
	});
});
function goIpinCheck() {
	if ( strNiceId == "" ) { showDialogEffect( getCheckMessage( "S60" ) ); return false;
	} else if ( strPingInfo == "" ) { showDialogEffect( getCheckMessage( "S61" ) ); return false;
	} else if ( strReturnUrl == "" ) { showDialogEffect( getCheckMessage( "S64" ) ); return false;
	}

	document.reqForm.SendInfo.value = makeCertKeyInfoPA( strNiceId, strPingInfo, strOrderNo, strInqRsn, strReturnUrl, strSIKey );
	document.reqForm.ProcessType.value = strPersonalCertKey;
	var popupWindow = window.open( "", "popupCertKey", "top=100, left=200, status=0, width=417, height=490" );
	document.reqForm.target = "popupCertKey";
	document.reqForm.action = strCertKeyServiceUrl;
	document.reqForm.submit();
	popupWindow.focus();
}
function callbackIpin(si) {
	getCheckResultJSON(gId, gType, "I", si);
}
function doGiveNewPw(type) {
	if ( $("input[name=authType]:checked").val()=="N" ) {
		if ( $("#nameForm").validnValidate() ) {
			var id = $("#nameForm input[name=id]").val();
    		var name = $("#nameForm input[name=name]").val();
    		var juminNo = $("#nameForm input[name=juminNo1]").val() + $("#nameForm input[name=juminNo2]").val();
    		var sendInfo = makeSendInfo( name, juminNo, "10", "1" );
			getCheckResultJSON(id, type, "N", sendInfo);
		}
	} else {
		if ( $("#ipinForm").validnValidate() ) {
			gId = $("#ipinForm input[name=id]").val();
			gType = type;
			goIpinCheck();
		}
	}
}
function getCheckResultJSON(id, stype, authType, sendInfo) {
	jQuery.getJSON("/service/findPwResultJSON.do", { id:id, sendType:stype, authType:authType, sendInfo:sendInfo }, function(data) {
		var type = data.sendType;
		var msg = data.msg;
		if ( msg && msg != "" ) {
			showDialogEffect(msg);
			return false;
		}
		if ( type=="email" ) {
			showDialogEffect("회원님의 이메일로 비밀번호를 발송하였습니다.", function() { location.href = "/"; });
		} else {
			location.href = "/service/findPwMobileAuthPage.do";
		}
	});
}

