
// 질문 작성 확인
function checkFormCustomerQnaWrite() {

   if(!$('#subject').val()) {

    alert(_i('문의 제목을 입력하세요.'));
    $('#subject').focus();
    return false;

   }

   if(!$('#content').val()) {

    alert(_i('문의 내용을 입력하세요.'));
    $('#content').focus();
    return false;

   }

   if((typeof($('#writeKey').val())!='undefined')&&!$('#writeKey').val()) {

    alert(_i('자동글 방지 번호를 정확히 입력하세요.'));
    return false;

   }

   return true;
   
}

function changeCartDeliveryGroup(cartNo, cartDeliveryGroup, cartInfo) {

    $.ajax({
       url: langHref + '/mall/cart-option',
       data: 'mode=bundle&cartNo=' + cartNo + '&cartDeliveryGroup=' + cartDeliveryGroup + '&cartInfo=' + cartInfo,
       type: 'POST',
       dataType: 'text',
       async: true,
       timeout: 5000,
       error: function() {
        alert(_i('데이터를 불러올 수 없습니다. 다시 시도하세요.'));
       },
	   success: function(data) {

         var obj = eval('(' + data + ')');   

         if(obj.errorMsg) {

          alert(obj.errorMsg);
	      return false;

         } 

         alert(obj.msg);
         document.location.reload();

	   }

    });

}

function changeCartCoupon() {

   var arrUseCouponList = new Array();
   var totalCouponPrice = 0;
   var couponInfo = '';

   $('.orderCartCoupon').each(function() {

      if($(this).val()) {
       var arrCouponInfo = $(this).val().split(';');
	   if(!arrUseCouponList[arrCouponInfo[0]]) {
		 totalCouponPrice += parseFloat(arrCouponInfo[2]);
		 arrUseCouponList[arrCouponInfo[0]] = true;
		 couponInfo += (couponInfo ? ';' : '') + arrCouponInfo[0] + '-' + arrCouponInfo[1];
	   } else $(this)[0].selectedIndex = 0;
	  }

   });

   $('#couponInfo').val(couponInfo);
   $('#couponPrice').val(totalCouponPrice);
   $('.totalCouponPrice').html(number_format(totalCouponPrice, ($('#useDecimal').val() ? 2 : 0)));
   changeTotalSettlePrice();

}

function addOffCoupon() {

   if(!$('#offCouponNo1').val()||!$('#offCouponNo2').val()||!$('#offCouponNo3').val()) {

	alert(_i('쿠폰을 정확히 입력하세요.'));
    return false;

   }

    $.ajax({
       url: langHref + '/mall/coupon-offline-receive',
       data: 'couponNo1=' + $('#offCouponNo1').val() +  '&couponNo2=' + $('#offCouponNo2').val() + '&couponNo3=' + $('#offCouponNo3').val(),
       type: 'POST',
       dataType: 'text',
       async: true,
       timeout: 5000,
       error: function() {
        alert(_i('데이터를 불러올 수 없습니다. 다시 시도하세요.'));
       },
	   success: function(data) {

         var obj = eval('(' + data + ')');   

         if(obj.errorMsg) {

          alert(obj.errorMsg);
	      return false;

         } 

         alert(obj.msg);
         document.location.reload();

	   }

    });

}

function setRssUrl(url) {

   var rssUrl = '';

   if($('#categoryNo').val()) rssUrl += '/categoryNo/' + $('#categoryNo').val();
   if($('#price1').val()) rssUrl += '/price1/' + $('#price1').val();
   if($('#price2').val()) rssUrl += '/price2/' + $('#price2').val();
   if($('#event').val()) rssUrl += '/event/' + $('#event').val();
   if($('#keyword').val()) rssUrl += '/keyword/' + $('#keyword').val();

   $('#rssUrl').val(url + rssUrl);

}

function searchQueryMallCountry(xml) {

   if(!countryIsoCode) countryIsoCode = defaultIsoCode;

   $('#countryList')[0].innerHTML=''; 
   $('#countryList')[0].options.add(new Option(_i('▼ 국가를 선택하세요.'), ''));
   $('#toCountryList')[0].innerHTML=''; 
   $('#toCountryList')[0].options.add(new Option(_i('▼ 국가를 선택하세요.'), ''));

   $(xml).find('country').each(function(i) { 
	$('#countryList')[0].options.add(new Option($(this).attr('country'), $(this).attr('isoCode')));
	$('#toCountryList')[0].options.add(new Option($(this).attr('country'), $(this).attr('isoCode')));
	if(countryIsoCode&&($(this).attr('isoCode')==countryIsoCode)) {
	 $('#countryList')[0].selectedIndex = i+1;	
	 $('#toCountryList')[0].selectedIndex = i+1;	
	}
   }); 

   searchQuery('countryState', countryIsoCode, searchQueryMallCountryState);   
   searchQuery('countryState', countryIsoCode, searchQueryMallCountryState2);   

   if($('#countryList').val()=='KR') {

    $('#country_KR').removeClass('none');
    $('#country_default').addClass('none');

    $('#addAddress1').addClass('validate-required');
    $('#addAddress2').addClass('validate-required');

   } 

   if($('#toCountryList').val()=='KR') {

    $('#country_KR2').removeClass('none');
    $('#country_default2').addClass('none');

    $('#addToAddress1').addClass('validate-required');
    $('#addToAddress2').addClass('validate-required');

   } 

}

function searchQueryMallCountryState(xml) {

   $('.flag').attr('src', '/imgs/system/flag/' + $('#countryList').val().toLowerCase() + '.png');

   $('#stateList')[0].innerHTML=''; 
   $('#stateList')[0].options.add(new Option(_i('▼ 지역을 선택하세요.'), ''));

   var checked = false;
   $(xml).find('state').each(function(i) { 
	$('#stateList')[0].options.add(new Option($(this).attr('state'), $(this).attr('stateCode')));
	if(stateCode&&($(this).attr('stateCode')==stateCode)) $('#stateList')[0].selectedIndex = i+1;
	checked = true;
   }); 

   if(checked) {

	$('#state').removeClass('none'); 
	$('#stateList').addClass('validate-selected'); 

   } else {

	$('#state').addClass('none');
	$('#stateList').removeClass('validate-selected'); 
	$('#stateList')[0].selectedIndex = 0;

   }

   if($('#countryList').val()=='KR') {

    $('#country_KR').removeClass('none');
    $('#country_default').addClass('none');

    $('#address1').removeClass('validate-required');
    $('#address3').removeClass('validate-required');
    $('#addAddress1').addClass('validate-required');
    $('#addAddress2').addClass('validate-required');

   } else {

    $('#country_KR').addClass('none');
    $('#country_default').removeClass('none');

    $('#addAddress1').removeClass('validate-required');
    $('#addAddress2').removeClass('validate-required');
    $('#address1').addClass('validate-required');
    $('#address3').addClass('validate-required');

   } 

}

function searchQueryMallCountryState2(xml) {

   $('.flag2').attr('src', '/imgs/system/flag/' + $('#toCountryList').val().toLowerCase() + '.png');

   $('#toStateList')[0].innerHTML=''; 
   $('#toStateList')[0].options.add(new Option(_i('▼ 지역을 선택하세요.'), ''));

   var checked = false;
   $(xml).find('state').each(function(i) { 
	$('#toStateList')[0].options.add(new Option($(this).attr('state'), $(this).attr('stateCode')));
	checked = true;
   }); 

   $('#addToZipcode').val('');
   $('#toZipcode').val('');
   changeBackcountryDelivery();

   if(checked) {

	$('#state2').removeClass('none'); 
	$('#toStateList').addClass('validate-selected'); 

   } else {

	$('#state2').addClass('none');
	$('#toStateList').removeClass('validate-selected'); 
	$('#toStateList')[0].selectedIndex = 0;

   }

   if($('#toCountryList').val()=='KR') {

    $('#country_KR2').removeClass('none');
    $('#country_default2').addClass('none');

    $('#toAddress1').removeClass('validate-required');
    $('#toAddress3').removeClass('validate-required');
    $('#addToAddress1').addClass('validate-required');
    $('#addToAddress2').addClass('validate-required');

   } else {

    $('#country_KR2').addClass('none');
    $('#country_default2').removeClass('none');

    $('#addToAddress1').removeClass('validate-required');
    $('#addToAddress2').removeClass('validate-required');
    $('#toAddress1').addClass('validate-required');
    $('#toAddress3').addClass('validate-required');

   } 

}

// 상품후기 검색시 검색 내용 확인
function checkFormReviewSearch() {

   if(!$('#keyword').val()) { 

	alert(_i('검색어를 입력하세요.'));
	$('#keyword').focus();
	return false; 
	
   }

   return true;

}

// 상품질문 검색시 검색 내용 확인
function checkFormQnaSearch() {

   if(!$('#keyword').val()) { 

	alert(_i('검색어를 입력하세요.'));
	$('#keyword').focus();
	return false; 
	
   }

   return true;

}

function setMailHost(obj, layerId) {

   if(obj.value) $('#' + layerId).val(obj.value).attr('readOnly', 'readOnly'); 
   else $('#' + layerId).val('').attr('readOnly', ''); 

}

function checkFormOrderStep1(obj) {

   if(!formValidator.check(obj)) return false;
  
   var checked = false;
   $('.typePay').each(function() { if(this.checked) checked = true; });
   if(!checked) {
     alert(_i('%s (은)는 반드시 선택하셔야 합니다.', _i('결제방법')));
     $('#submit').css('visibility', ''); 
     return false;
   }

   if($('.typePay')[0].checked&&$('.typePay')[0].value=='0') {

    if(!$('#bankInfo').val()) {

     alert(_i('%s (은)는 반드시 선택하셔야 합니다.', _i('입금계좌')));
     $('#bankInfo').focus();
     $('#submit').css('visibility', ''); 
     return false;

    }

    if(!$('#bankPostname').val()) {

     alert(_i('%s (은)는 반드시 입력하셔야 합니다.', _i('입금자명')));
     $('#bankPostname').focus();
     $('#submit').css('visibility', ''); 
     return false;

    }

    if(!$('#bankDate').val()) {

     alert(_i('%s (은)는 반드시 입력하셔야 합니다.', _i('입금예정일')));
     $('#submit').css('visibility', ''); 
     return false;

    }

   }

   if($('#countryList').val()=='KR') {

	$('#address1').val($('#addAddress1').val());
	$('#address2').val($('#addAddress2').val());
	$('#zipcode').val($('#addZipcode').val());
	$('#address3').val('');
	$('#stateCode').val('');

   } 

   if($('#toCountryList').val()=='KR') {

	$('#toAddress1').val($('#addToAddress1').val());
	$('#toAddress2').val($('#addToAddress2').val());
	$('#toZipcode').val($('#addToZipcode').val());
	$('#toAddress3').val('');
	$('#toStateCode').val('');

   } 

   if($('#mailUser').val()&&$('#mailHost').val()) $('#mail').val($('#mailUser').val() + '@' + $('#mailHost').val());   
   else $('#mail').val('');
   if($('#tel_1').val()&&$('#tel_2').val()&&$('#tel_3').val()) $('#tel').val($('#tel_1').val() + '-' + $('#tel_2').val() + '-' + $('#tel_3').val());   
   else $('#tel').val('');
   if($('#hp_1').val()&&$('#hp_2').val()&&$('#hp_3').val()) $('#hp').val($('#hp_1').val() + '-' + $('#hp_2').val() + '-' + $('#hp_3').val());   
   else $('#hp').val('');

   if($('#to_mailUser').val()&&$('#to_mailHost').val()) $('#toMail').val($('#to_mailUser').val() + '@' + $('#to_mailHost').val());   
   else $('#toMail').val('');
   if($('#to_tel_1').val()&&$('#to_tel_2').val()&&$('#to_tel_3').val()) $('#toTel').val($('#to_tel_1').val() + '-' + $('#to_tel_2').val() + '-' + $('#to_tel_3').val());   
   else $('#toTel').val('');
   if($('#to_hp_1').val()&&$('#to_hp_2').val()&&$('#to_hp_3').val()) $('#toHp').val($('#to_hp_1').val() + '-' + $('#to_hp_2').val() + '-' + $('#to_hp_3').val());   
   else $('#toHp').val('');

   return true;

}

function checkFormOrderStep2() {

   if(confirm(_i('현재 작성한 정보로 상품을 주문하시겠습니까?') + '\r\n' + _i('총 결제금액 : %s', $('#totalSettlePrice').val()))) return true;
   else {
    $('#submit').css('visibility', ''); 
    return false;
   }

}

function searchQueryToAddress(xml) {

   $('#toAddressList')[0].innerHTML=''; 
   $('#toAddressList')[0].options.add(new Option(_i('▼ 아래 주소를 선택 해주세요.'), ''));

   var checked = false;
   $(xml).find('address').each(function() { 
	$('#toAddressList')[0].options.add(new Option($(this).attr('sido') + ' ' + $(this).attr('gugun') + ' ' + $(this).attr('dong') + ' ' + $(this).attr('bunji'), $(this).attr('zipcode')));
	checked = true;
   }); 

   if(checked) $('#toAddressList').attr({'size': '10'}).click(function() { $(this).attr({'size': '1'}); });  

}

function copyOrderInfo(obj) {

   if(obj.checked==true) {

    $('#toRealname').val($('#realname').val());
    $('#toTel').val($('#tel').val());
    $('#to_tel_1').val($('#tel_1').val());
    $('#to_tel_2').val($('#tel_2').val());
    $('#to_tel_3').val($('#tel_3').val());
    $('#toHp').val($('#hp').val());
    $('#to_hp_1').val($('#hp_1').val());
    $('#to_hp_2').val($('#hp_2').val());
    $('#to_hp_3').val($('#hp_3').val());
    $('#toMail').val($('#mail').val());
    $('#to_mailUser').val($('#mailUser').val());
    $('#to_mailHost').val($('#mailHost').val());
    if($('#countryList')[0]&&$('#toCountryList')[0]) $('#toCountryList')[0].selectedIndex = $('#countryList')[0].selectedIndex;
    $('.flag2').attr('src', $('.flag').attr('src'));

	$('#toStateList')[0].className = '';
    $('#toStateList')[0].selectedIndex = 0;
    $('#toState').addClass('none');

	if($('#state').css('display')!='none') {

     if($('#toStateList')[0].nodeName.toLowerCase()==='select') {

      $('#toStateList')[0].innerHTML=''; 
      $('#toStateList')[0].options.add(new Option($('#stateList')[0].options[$('#stateList')[0].selectedIndex].text, $('#stateList')[0].options[$('#stateList')[0].selectedIndex].value));

	  $('#state2').removeClass('none');
      $('#toStateList').addClass('validate-selected'); 

	 } 

	}

	if(($('#state').css('display')=='none')&&($('#state2').css('display')!='none')) $('#state2').addClass('none');

    if($('#countryList').val()=='KR') {

     $('#toAddressList').html($('#addressList').html());
	 $('#toAddressList')[0].selectedIndex = $('#addressList')[0].selectedIndex;
     $('#addToAddress1').val($('#addAddress1').val());
     $('#addToAddress2').val($('#addAddress2').val());
     $('#addToZipcode').val($('#addZipcode').val());

	 $('#country_KR2').removeClass('none');
	 $('#country_default2').addClass('none');

     $('#toAddress1').removeClass('validate-required');
     $('#toAddress3').removeClass('validate-required');
     $('#addToAddress1').addClass('validate-required');
     $('#addToAddress2').addClass('validate-required');

	 changeBackcountryDelivery($('#addZipcode').val());

    } else {

     $('#toAddress1').val($('#address1').val());
     $('#toAddress2').val($('#address2').val());
     $('#toAddress3').val($('#address3').val());
     $('#toZipcode').val($('#zipcode').val());

	 $('#country_KR2').addClass('none');
	 $('#country_default2').removeClass('none');

     $('#addToAddress1').removeClass('validate-required');
     $('#addToAddress2').removeClass('validate-required');
     $('#toAddress1').addClass('validate-required');
     $('#toAddress3').addClass('validate-required');

	 changeBackcountryDelivery($('#zipcode').val());

    }

   } else {

    $('#toRealname').val('');
    $('#toTel').val('');
    $('#to_tel_1').val('');
    $('#to_tel_1')[0].selectedIndex = 0;
    $('#to_tel_2').val('');
    $('#to_tel_3').val('');
    $('#toHp').val('');
    $('#to_hp_1').val('');
    $('#to_hp_1')[0].selectedIndex = 0;
    $('#to_hp_2').val('');
    $('#to_hp_3').val('');
    $('#toMail').val('');
    $('#to_mailHostList')[0].selectedIndex = 0;
    $('#to_mailUser').val('');
    $('#to_mailHost').val('');
    $('#toStateList')[0].selectedIndex = 0;
    $('#toState').addClass('none');
    $('#toAddressList')[0].innerHTML=''; 
    $('#toAddressList')[0].options.add(new Option(_i('▲ 동/면/리를 입력하고 "주소찾기"를 클릭하세요.'), ''));
    $('#toAddress1').val('');
    $('#toAddress2').val('');
    $('#toAddress3').val('');
    $('#toZipcode').val('');
    $('#addToAddress1').val('');
    $('#addToAddress2').val('');
    $('#addToZipcode').val('');

	 changeBackcountryDelivery('');

   }

}

function changeTotalSettlePrice() {

  var totalSettlePrice = 0;
  var totalPrice = parseFloat($('input.totalPrice').val());
  var totalCouponPrice = ($('#couponPrice').val()&&($('#couponPrice').val()!=undefined)) ? parseFloat($('#couponPrice').val()) : 0;
  var totalDeliveryPrice = ($('#totalDeliveryPrice').val()!=undefined) ? parseFloat($('#totalDeliveryPrice').val()) + parseFloat($('#backcountryDeliveryPrice2').val()) : 0;
  var totalDeliverySinglePrice = parseFloat($('#deliverySinglePrice').val());
  var totalDeliveryOptionPrice = parseFloat($('#deliveryOptionPrice').val());
  var totalDeliveryPrice2 = ($('input[name=globalDeliveryName]:checked').val()) ? parseFloat($('#globalDeliveryPrice2').val()) : 0;
  var totalTax = ($('#totalTax').val()!=undefined) ? parseFloat($('#totalTax').val()) : 0;
  var deliveryAreaPrice = ($('#deliveryAreaPrice').val()!=undefined) ? parseFloat($('#deliveryAreaPrice').val()) : 0;
  var settleCash = ($('#settleCash').val()!='') ? parseFloat($('#settleCash').val()) : 0;

  if(myCash<settleCash) {

   alert(_i('보유 캐쉬가 모자랍니다.'));
   $('#settleCash').val('0');
   changeTotalSettlePrice();
   return false;

  }

  var _totalPrice = (totalPrice + totalTax) - totalCouponPrice;

  if(settleCash&&(settleCash>_totalPrice)) {

   alert(_i('상품합계금액 이상 보유 캐쉬를 사용 할 수 없습니다.'));
   $('#settleCash').val('0');
   changeTotalSettlePrice();
   return false;

  }

  if($('#useDeliveryAreaPrice').attr('checked')) totalDeliveryPrice += deliveryAreaPrice;
  if($('#useDeliveryPrice').attr('checked')) {
   totalDeliveryPrice = 0;
   totalDeliveryPrice2 = 0;
  }

  totalSettlePrice = (totalPrice + (totalDeliveryPrice2 ? totalDeliveryPrice2 : totalDeliveryPrice) + totalTax) - totalCouponPrice - settleCash;
  $('.totalSettlePrice').html(number_format(totalSettlePrice, ($('#useDecimal').val() ? 2 : 0)));

}

function changeBackcountryDelivery(zipcode, delivery) {

    var checked = false;

	if(($('#toCountryList').val()!=undefined)&&(defaultIsoCode!=$('#toCountryList').val())) checked = true;
    if(($('#singleDeliveryGroup').val()==undefined)||($('#singleDeliveryGroup').val()==0)) checked = true;

    if(!zipcode||checked) {

     $('#backcountryDeliveryPrice').val('0');
	 $('#backcountryDeliveryPrice2').val('0');
     $('.totalDeliveryPrice').html(number_format(($('#globalDeliveryPrice2').val()!=undefined&&$('#globalDeliveryPrice2').val()>0) ? parseFloat($('#globalDeliveryPrice2').val()) : (parseFloat($('#totalDeliveryPrice').val())), ($('#useDecimal').val() ? 2 : 0)));
     changeTotalSettlePrice();
	 return false;

    }

    if(typeof(delivery)=='undefined') delivery = $('#deliveryNameGroup').val();	

    $.ajax({
       url: langHref + '/mall/order-backcountry-delivery-option',
       data: 'zipcode=' + zipcode + '&countDeliveryGroup=' + ($('#countDeliveryGroup').val()!=undefined ? $('#countDeliveryGroup').val() : 1) + '&singleDelivery=' + ($('#singleDeliveryGroup').val()!=undefined ? $('#singleDeliveryGroup').val() : '') + '&delivery=' + (delivery!=undefined ? delivery : ''),
       type: 'POST',
       dataType: 'text',
       async: true,
       timeout: 5000,
       error: function() {
        alert(_i('데이터를 불러올 수 없습니다. 다시 시도하세요.'));
       },
	   success: function(data) {

         var obj = eval('(' + data + ')');   

         if(obj.errorMsg) {

          alert(obj.errorMsg);
	      return false;

         } 

		 $('#backcountryDeliveryPrice').val(obj.deliveryPrice);
		 $('#backcountryDeliveryPrice2').val(obj.deliveryPrice2);
         $('.totalDeliveryPrice').html(number_format((parseFloat($('#totalDeliveryPrice').val()) + parseFloat($('#backcountryDeliveryPrice2').val())), ($('#useDecimal').val() ? 2 : 0)));
         changeTotalSettlePrice();

	   }

    });

}

function changeUseGlobalDelivery() {

   var delivery = $('input[name=globalDeliveryName]:checked').val();

   if(delivery) {
    
    if(!$('#toCountryList').val()) {

	 alert(_i('받을분 국가를 선택하세요.'));
     $('#globalDeliveryPrice').val('0');
     $('#globalDeliveryPrice2').val('0');
	 $('#globalDeliveryName')[0].checked = true;
     $('.totalDeliveryPrice').html(number_format((parseFloat($('#totalDeliveryPrice').val()) + parseFloat($('#backcountryDeliveryPrice2').val())), ($('#useDecimal').val() ? 2 : 0)));		 
	 return false;

    }

    if(($('#state2').css('display')!='none')&&!$('#toStateList').val()) {

	 alert(_i('받을분 지역을 선택하세요.'));
     $('#globalDeliveryPrice').val('0');
     $('#globalDeliveryPrice2').val('0');
	 $('#globalDeliveryName')[0].checked = true;
     $('.totalDeliveryPrice').html(number_format((parseFloat($('#totalDeliveryPrice').val()) + parseFloat($('#backcountryDeliveryPrice2').val())), ($('#useDecimal').val() ? 2 : 0)));		 
	 return false;

    }

    if($('#useDeliveryAreaPrice')[0]) $('#useDeliveryAreaPrice')[0].checked = false;
    if($('#useDeliveryPrice')[0]) $('#useDeliveryPrice')[0].checked = false;

    changeTotalSettlePrice();
	$('.backcountryDelivery').css('display', 'none');
	$('.localDelivery').css('display', 'none');

    $.ajax({
       url: langHref + '/mall/order-global-delivery-option',
       data: 'delivery=' + delivery + '&countDeliveryGroup=' + ($('#countDeliveryGroup').val()!=undefined ? $('#countDeliveryGroup').val() : 1) + '&countryIsoCode=' + $('#toCountryList').val() + '&stateCode=' + ($('#toStateList').val()!=null ? $('#toStateList').val() : '') + '&weight=' + (typeof($('#totalWeight').val())!='undefined'?$('#totalWeight').val():''),
       type: 'POST',
       dataType: 'text',
       async: true,
       timeout: 5000,
       error: function() {
        alert(_i('데이터를 불러올 수 없습니다. 다시 시도하세요.'));
       },
	   success: function(data) {

         var obj = eval('(' + data + ')');   

         if(obj.errorMsg) {

          alert(obj.errorMsg);
		  $('#globalDeliveryPrice').val('0');
          $('#globalDeliveryPrice2').val('0');
          $('#globalDeliveryName')[0].checked = true;
	      $('.backcountryDelivery').css('display', '');
  	      $('.localDelivery').css('display', '');
          $('.totalDeliveryPrice').html(number_format((parseFloat($('#totalDeliveryPrice').val()) + parseFloat($('#backcountryDeliveryPrice2').val())), ($('#useDecimal').val() ? 2 : 0)));		 
          changeTotalSettlePrice();
	      return false;

         } 

         $('#weightDeliveryGroupInfo').val(obj.weightDeliveryGroupInfo);
         $('#weightDeliveryGroupInfo2').val(obj.weightDeliveryGroupInfo2);

         $('#globalDeliveryPrice').val(obj.deliveryPrice);
         $('#globalDeliveryPrice2').val(obj.deliveryPrice2);
         $('.totalDeliveryPrice').html(number_format(parseFloat($('#globalDeliveryPrice2').val()), ($('#useDecimal').val() ? 2 : 0)));
         changeTotalSettlePrice();

	   }

    });
   
   } else {

    $('#globalDeliveryPrice').val('0');
    $('#globalDeliveryPrice2').val('0');
    $('.totalDeliveryPrice').html(number_format((parseFloat($('#totalDeliveryPrice').val()) + parseFloat($('#backcountryDeliveryPrice2').val())), ($('#useDecimal').val() ? 2 : 0)));
	$('.backcountryDelivery').css('display', '');
	$('.localDelivery').css('display', '');
    changeTotalSettlePrice();

   }

}


function copyOrderDeliveryInfo(layerId) {

    var realname = $(layerId).find('.realname').text();
    var tel = $(layerId).find('.tel').text();
    var tel_1 = $(layerId).find('.tel_1').text();
    var tel_2 = $(layerId).find('.tel_2').text();
    var tel_3 = $(layerId).find('.tel_3').text();
    var hp = $(layerId).find('.hp').text();
    var hp_1 = $(layerId).find('.hp_1').text();
    var hp_2 = $(layerId).find('.hp_2').text();
    var hp_3 = $(layerId).find('.hp_3').text();
    var mail = $(layerId).find('.mail').text();
    var mail_1 = $(layerId).find('.mail_1').text();
    var mail_2 = $(layerId).find('.mail_2').text();
    var _countryIsoCode = $(layerId).find('.countryIsoCode').text();
    var _stateCode = $(layerId).find('.stateCode').text();
    var state = $(layerId).find('.state').text();
    var address1 = $(layerId).find('.address1').text();
    var address2 = $(layerId).find('.address2').text();
    var address3 = $(layerId).find('.address3').text();
    var zipcode = $(layerId).find('.zipcode').text();

    opener.$('#toRealname').val(realname);
    opener.$('#toTel').val(tel);
    opener.$('#to_tel_1').val(tel_1);
    opener.$('#to_tel_2').val(tel_2);
    opener.$('#to_tel_3').val(tel_3);
    opener.$('#toHp').val(hp);
    opener.$('#to_hp_1').val(hp_1);
    opener.$('#to_hp_2').val(hp_2);
    opener.$('#to_hp_3').val(hp_3);
    opener.$('#toMail').val(mail);
    opener.$('#to_mailUser').val(mail_1);
    opener.$('#to_mailHost').val(mail_2);

	opener.$('#toCountryList>option').each(function() { if($(this).val()==_countryIsoCode) $(this).attr('selected', 'selected'); });
	opener.$('.flag2').attr('src', $($(layerId).find('.deliveryFlag')).attr('src'));

    if(_stateCode&&state) {
	   
     opener.$('#toStateList')[0].innerHTML=''; 
     try {
      opener.$('#toStateList')[0].options.add(new Option(state, _stateCode));
     } catch(e) { 
	  var objOption = opener.document.createElement('option');
      objOption.text = state;
      objOption.value = _stateCode;
      opener.$('#toStateList')[0].options.add(objOption);
	 }

	 opener.$('#state2').removeClass('none');
     opener.$('#toStateList').addClass('validate-selected'); 

	} else {

	 opener.$('#state2').addClass('none');
     //opener.$('#toStateList').removeClass('validate-selected'); 
	 opener.$('#toStateList')[0].className = '';
     opener.$('#toStateList')[0].innerHTML=''; 
     $('#toState').addClass('none');

    }	
	
    if(_countryIsoCode=='KR') {

     opener.$('#toAddressList')[0].innerHTML=''; 
     try {
	  opener.$('#toAddressList')[0].options.add(new Option(address1, address1));
     } catch(e) { 
	  var objOption = opener.document.createElement('option');
      objOption.text = address1;
      objOption.value = address1;
      opener.$('#toAddressList')[0].options.add(objOption);
	 }

     opener.$('#addToAddress1').val(address1);
     opener.$('#addToAddress2').val(address2);
     opener.$('#addToZipcode').val(zipcode);

	 opener.$('#country_KR2').removeClass('none');
	 opener.$('#country_default2').addClass('none');

     opener.$('#toAddress1').removeClass('validate-required');
     opener.$('#toAddress3').removeClass('validate-required');
     opener.$('#addToAddress1').addClass('validate-required');
     opener.$('#addToAddress2').addClass('validate-required');

    } else {

     opener.$('#toAddress1').val(address1);
     opener.$('#toAddress2').val(address2);
     opener.$('#toAddress3').val(address3);
     opener.$('#toZipcode').val(zipcode);

	 opener.$('#country_KR2').addClass('none');
	 opener.$('#country_default2').removeClass('none');

     opener.$('#addToAddress1').removeClass('validate-required');
     opener.$('#addToAddress2').removeClass('validate-required');
     opener.$('#toAddress1').addClass('validate-required');
     opener.$('#toAddress3').addClass('validate-required');

    }

    opener.changeBackcountryDelivery(zipcode);

	top.close();

}

function searchQueryMallDeliveryCountry(xml) {

   if(!deliveryCountryIsoCode) deliveryCountryIsoCode = deliveryDefaultIsoCode;

   $('#deliveryCountryList')[0].innerHTML=''; 
   $('#deliveryCountryList')[0].options.add(new Option(_i('▼ 국가를 선택하세요.'), ''));

   $(xml).find('country').each(function(i) { 
	$('#deliveryCountryList')[0].options.add(new Option($(this).attr('country'), $(this).attr('isoCode')));
	if(deliveryCountryIsoCode&&($(this).attr('isoCode')==deliveryCountryIsoCode)) $('#deliveryCountryList')[0].selectedIndex = i+1;	
   }); 

   searchQuery('countryState', deliveryCountryIsoCode, searchQueryMallDeliveryCountryState);   

   if($('#deliveryCountryList').val()=='KR') {

    $('#deliveryCountry_KR').removeClass('none');
    $('#deliveryCountry_default').addClass('none');

    $('#addDeliveryAddress1').addClass('validate-required');
    $('#addDeliveryAddress2').addClass('validate-required');

   } 

}

function searchQueryMallDeliveryCountryState(xml) {

   $('.deliveryFlag').attr('src', '/imgs/system/flag/' + $('#deliveryCountryList').val().toLowerCase() + '.png');

   $('#deliveryStateList')[0].innerHTML=''; 
   $('#deliveryStateList')[0].options.add(new Option(_i('▼ 지역을 선택하세요.'), ''));

   var checked = false;
   $(xml).find('state').each(function(i) { 
	$('#deliveryStateList')[0].options.add(new Option($(this).attr('state'), $(this).attr('stateCode')));
	if(deliveryStateCode&&($(this).attr('stateCode')==deliveryStateCode)) $('#deliveryStateList')[0].selectedIndex = i+1;
	checked = true;
   }); 

   if(checked) {

	$('#deliveryState').removeClass('none'); 
	$('#deliveryStateList').addClass('validate-selected'); 

   } else {

	$('#deliveryState').addClass('none');
	$('#deliveryStateList').removeClass('validate-selected'); 
	$('#deliveryStateList')[0].selectedIndex = 0;

   }

   if($('#deliveryCountryList').val()=='KR') {

    $('#deliveryCountry_KR').removeClass('none');
    $('#deliveryCountry_default').addClass('none');

    $('#deliveryAddress1').removeClass('validate-required');
    $('#deliveryAddress3').removeClass('validate-required');
    $('#addDeliveryAddress1').addClass('validate-required');
    $('#addDeliveryAddress2').addClass('validate-required');

   } else {

    $('#deliveryCountry_KR').addClass('none');
    $('#deliveryCountry_default').removeClass('none');

    $('#addDeliveryAddress1').removeClass('validate-required');
    $('#addDeliveryAddress2').removeClass('validate-required');
    $('#deliveryAddress1').addClass('validate-required');
    $('#deliveryAddress3').addClass('validate-required');

   } 

}

function searchQueryDeliveryAddress(xml) {

   $('#deliveryAddressList')[0].innerHTML=''; 
   $('#deliveryAddressList')[0].options.add(new Option(_i('▼ 아래 주소를 선택 해주세요.'), ''));

   var checked = false;
   $(xml).find('address').each(function() { 
	$('#deliveryAddressList')[0].options.add(new Option($(this).attr('sido') + ' ' + $(this).attr('gugun') + ' ' + $(this).attr('dong') + ' ' + $(this).attr('bunji'), $(this).attr('zipcode')));
	checked = true;
   }); 

}

function checkFormDeliveryWrite(obj) {

   if(!formValidator.check(obj)) return false;
  
   if($('#deliveryCountryList').val()=='KR') {

	$('#deliveryAddress1').val($('#addDeliveryAddress1').val());
	$('#deliveryAddress2').val($('#addDeliveryAddress2').val());
	$('#deliveryZipcode').val($('#addDeliveryZipcode').val());
	$('#deliveryAddress3').val('');
	$('#deliveryStateCode').val('');

   } else {
   
    if($('#deliveryState').css('display')!='none') $('#deliveryStateName').val($('#deliveryStateList option:selected').text());
		
   }

   if($('#deliveryMailUser').val()&&$('#deliveryMailHost').val()) $('#deliveryMail').val($('#deliveryMailUser').val() + '@' + $('#deliveryMailHost').val());   
   else $('#deliveryMail').val('');
   if($('#deliveryTel_1').val()&&$('#deliveryTel_2').val()&&$('#deliveryTel_3').val()) $('#deliveryTel').val($('#deliveryTel_1').val() + '-' + $('#deliveryTel_2').val() + '-' + $('#deliveryTel_3').val());   
   else $('#deliveryTel').val('');
   if($('#deliveryHp_1').val()&&$('#deliveryHp_2').val()&&$('#deliveryHp_3').val()) $('#deliveryHp').val($('#deliveryHp_1').val() + '-' + $('#deliveryHp_2').val() + '-' + $('#deliveryHp_3').val());   
   else $('#deliveryHp').val('');

   return true;

}
