<form class="form_wrap form_full" name="deliveryAddForm" id="deliveryAddForm">
	<div class="modal-header htop">
		<h5 class="modal-title" id="adrsAddLabel">배송지 추가</h5>
	</div>
	<div class="modal-body">
		<div class="pop_cont">
			<div class="inner wide">
				<div class="tbl_wrap">
					<div class="tbl tbl_dlvr">
						<table>
							<colgroup>
								<col width="*">
							</colgroup>
							<tbody>
								<tr>
									<td>
										<span class="sr-only">받는분</span>
										<input type="text" class="form_control" name="recipNm" maxlength="10" placeholder="받는분">
									</td>
								</tr>
								<tr>
									<td>
										<span class="sr-only">휴대폰 번호</span>
										<input type="number" class="form_control" name="recipPhnno" placeholder="휴대폰 번호" data-valid-type="numeric">
									</td>
								</tr>
								<tr>
									<td>
										<span class="sr-only">배송 주소</span>
										<div class="input_wrap">
											<input type="text" class="form_control" name="recipAddr" placeholder="배송주소" readonly="readonly">
											<input type="text" class="form_control" name="recipDtlAddr" placeholder="상세주소">
											<input type="hidden" class="form_control" name="recipZipcode" placeholder="우편번호" readonly="readonly">
											<input type="hidden" class="form_control" name="recipBaseAddr" placeholder="기본주소" readonly="readonly">
											<button type="button" class="btn btn_sch" onclick="fnOpenDaumAddr();">
												<span class="sr-only">우편번호 찾기</span>
											</button>
										</div>
										<th:block th:if="${isLogin} == true">
											<div class="default_addrs">
												<div class="form_field">
													<input id="chk-default-addrs" type="checkbox" name="defaultYn" checked>
													<label for="chk-default-addrs"><span>기본 배송지로 등록</span></label>
												</div>
											</div>
										</th:block>
									</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
				<div class="tbl_wrap">
					<div class="tbl_tit">
						<h3>배송 요청사항</h3>
					</div>
					<div class="tbl tbl_request">
						<table>
							<colgroup>
								<col width="*">
							</colgroup>   
							<tbody>
								<tr>
									<th class="sr-only">문 앞</th>
									<td>
										<div class="form_field">
											<input type="radio" name="rdi-request1" id="rdi-11" value="문 앞">
											<label for="rdi-11"><span>문 앞</span></label>
										</div>
									</td>
								</tr>
								<tr>
									<th class="sr-only">직접 받고 부재 시 문 앞</th>
									<td>
										<div class="form_field">
											<input type="radio" name="rdi-request1" id="rdi-21" value="직접 받고 부재 시 문 앞" checked>
											<label for="rdi-21"><span>직접 받고 부재 시 문 앞</span></label>
										</div>
									</td>
								</tr>
								<tr>
									<th class="sr-only">경비실</th>
									<td>
										<div class="form_field">
											<input type="radio" name="rdi-request1" id="rdi-31" value="경비실">
											<label for="rdi-31"><span>경비실</span></label>
										</div>
									</td>
								</tr>
								<tr>
									<th class="sr-only">택배함</th>
									<td>
										<div class="form_field">
											<input type="radio" name="rdi-request1" id="rdi-41" value="택배함">
											<label for="rdi-41"><span>택배함</span></label>
										</div>
									</td>
								</tr>
								<tr>
									<th class="sr-only">기타사항</th>
									<td>
										<div class="form_field">
											<input type="radio" name="rdi-request1" id="rdi-51" value="기타사항">
											<label for="rdi-51"><span>기타사항</span></label>
										</div>
									</td>
								</tr>
								<tr>
									<th class="sr-only">보관 장소만 입력(필수)</th>
									<td>
										<div class="form_text">
											<input type="text" name="delvMemoText" placeholder="보관 장소만 입력 (필수)" maxlength="30" disabled>
											<p class="desc_txt">보관 장소 외 다른 내용 입력시 통보 없이 삭제 될 수 있습니다.</p>
										</div>
									</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="modal-footer">
		<button type="button" class="btn btn_dark" id="btn_delivery_addr_save"><span>배송지 등록</span></button>
	</div>
</form>

<script th:inline="javascript">
var isLogin				= [[${isLogin}]];				// 로그인여부

//컨텐츠 호출
$(document).ready( function() {
	// 배송요청사항 버튼기능
	$("#deliveryAddForm input[name=rdi-request1]").each(function(){
		// 기타기능 버튼기능
		$(this).on("click", function(){
			var temp = $(this).parent().find('span').text();
			$("#deliveryAddForm input[name=delvMemoText]").val("");
			if (temp == "기타사항") {
				$("#deliveryAddForm input[name=delvMemoText]").attr("disabled", false);
			} else {
				$("#deliveryAddForm input[name=delvMemoText]").attr("disabled", true);
			}
		});
	});
});

// 배송지등록버튼
$("#btn_delivery_addr_save").on("click", function(){
	
	// 유효성체크
	if (!deliveryAddFormCheck()){
		return false;
	}
	
	// 기본배송지여부
	var defaultYn = "N";
	if ($("#deliveryAddForm input[name=defaultYn]").is(":checked")) {
		defaultYn = "Y";
	}
	
	// 기타사항일때 텍스트 등록
	var delvMemo = $("#deliveryAddForm input[name=rdi-request1]:checked").parent().find('span').text();
	if ("기타사항" == delvMemo) {
		delvMemo = $("#deliveryAddForm input[name=delvMemoText]").val();
	}
	
	// 2021.04.29 추가
	if (gagajf.isNull(delvMemo)) {
		mcxDialog.alert("기타사항을 입력해주세요.");
		$("#deliveryAddForm input[name=delvMemoText]").focus();
		return false;
	}
	
	var recipPhnno = fnPhnnoSet($("#deliveryAddForm input[name=recipPhnno]").val());
	
	// 전화번호 유효성 검증 추가
	let regExp = /^(050[0-9]|01[016789]|02|0[3-9][0-9])-?[0-9]{3,4}-?[0-9]{4}$/;
	if (!regExp.test(recipPhnno)) {
		mcxDialog.alert("휴대폰번호를 정확히 입력해주세요.");
		$('#deliveryAddForm input[name=recipPhnno]').focus();
		return false;
	}
	
	// custDelvAddrSq 부모창으로 전달
	var delvObj = {
		"custDelvAddrSq"	: 0
		, "dispYn"			: "Y"
		, "defaultYn"		: defaultYn
		, "recipNm"			: $("#deliveryAddForm input[name=recipNm]").val()
		, "recipPhnno"		: recipPhnno
		, "recipZipcode"	: $("#deliveryAddForm input[name=recipZipcode]").val()
		, "recipBaseAddr"	: $("#deliveryAddForm input[name=recipBaseAddr]").val()
		, "recipDtlAddr"	: $("#deliveryAddForm input[name=recipDtlAddr]").val()
		, "delvMemo"		: delvMemo
	}
	
	// 비회원
	if (isLogin == false) {
		// 부모장으로 값 전닫
		delvAddrSet(delvObj);
		// 팝업닫기
		$(".close-modal").trigger("click");
		return false;
	}
	
	var jsonData = JSON.stringify(delvObj);
	
	$.ajax( {
		type		: "POST",
		url 		: '/common/createCustDeliveryAddr',
		data		: jsonData,
		dataType 	: 'html',
		beforeSend : function(xhr, settings) {
			xhr.setRequestHeader("AJAX"			, "true");
			xhr.setRequestHeader('Accept'		, 'application/json');
			xhr.setRequestHeader('Content-Type'	, 'application/json');
		},
		success 	: function(result) {
			if (result != null) {
				delvObj.custDelvAddrSq = result;

				// 부모장으로 값 전닫
				delvAddrSet(delvObj);
			}
			
			// 팝업닫기
			$(".close-modal").trigger("click");
		}
	});
});

// 유효성체크 함수
var deliveryAddFormCheck = function() {
	let phnno = $('#deliveryAddForm input[name=recipPhnno]').val();

	if (gagajf.isNull($("#deliveryAddForm input[name=recipNm]").val())) {
		mcxDialog.alert("받는분을 입력해주세요.");
		$('#deliveryAddForm input[name=custNm]').focus();
		return false;
	}
	
	if (gagajf.isNull(phnno)) {
		mcxDialog.alert("휴대폰번호를 입력해주세요.");
		$('#deliveryAddForm input[name=recipPhnno]').focus();
		return false;
	}

	if (phnno.length < 9) {
		mcxDialog.alert("휴대폰번호를 정확히 입력해주세요.");
		$('#deliveryAddForm input[name=recipPhnno]').focus();
		return false;
	}
	
	if (phnno.length > 12) {
		mcxDialog.alert("휴대폰번호를 정확히 입력해주세요.");
		$('#deliveryAddForm input[name=recipPhnno]').focus();
		return false;
	}
	
	if (gagajf.isNull($("#deliveryAddForm input[name=recipZipcode]").val())) {
		mcxDialog.alert("배송주소를 입력해주세요.");
		$('#deliveryAddForm input[name=recipAddr]').focus();
		return false;
	}
	
	if (gagajf.isNull($("#deliveryAddForm input[name=recipBaseAddr]").val())) {
		mcxDialog.alert("배송주소를 입력해주세요.");
		$('#deliveryAddForm input[name=recipAddr]').focus();
		return false;
	}
	
	if (gagajf.isNull($("#deliveryAddForm input[name=recipDtlAddr]").val())) {
		mcxDialog.alert("상세주소를 입력해주세요.");
		$('#deliveryAddForm input[name=recipDtlAddr]').focus();
		return false;
	}
	
	return true;
}

// 우편번호 DAUM을 이용한 우편번호 팝업 레이어
var fnOpenDaumAddr = function() {
	$("body").css("overflow", "hidden");
	
	let daumZip = new daum.Postcode({
		oncomplete: function(data) {
			$('#deliveryAddForm input[name=recipAddr]').val(data.zonecode + '    ' + cfnGetDaumRoadAddr(data));
			$('#deliveryAddForm input[name=recipZipcode]').val(data.zonecode);
			$('#deliveryAddForm input[name=recipBaseAddr]').val(cfnGetDaumRoadAddr(data));
			$('#deliveryAddForm input[name=recipDtlAddr]').focus();
			
			cfnCloseDaumAddr();
			
			$("#adrsAddPop").modal("show");
		},
		width: '100%'
	});
	cfnOpenDaumAddr(daumZip);
}

// 하이픈처리
var fnPhnnoSet = function(str) {
	var rtnStr = str.replace(/[^0-9]/g, "").replace(/(^02|^050[0-9]|^1[0-9]{3}|^0[0-9]{2})([0-9]+)?([0-9]{4})$/,"$1-$2-$3").replace("--", "-");
	return rtnStr;
}

// length 체크
$("#deliveryAddForm input[name=recipPhnno]").on("keyup", function(){
	var str = $(this).val();
	if (str.length > 12) {
		str = str.slice(0,13);
	}
	$(this).val(str);
});

//2021.04.09 하이픈처리
//$(document).on("keyup", "#deliveryAddForm input[name=recipPhnno]", function() { 
//	$(this).val( $(this).val().replace(/[^0-9]/g, "").replace(/(^02|^0505|^1[0-9]{3}|^0[0-9]{2})([0-9]+)?([0-9]{4})$/,"$1-$2-$3").replace("--", "-") ); 
//});
</script>









