
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;	
	}
   }); 

   if(countryIsoCode&&stateCode) searchQuery('countryState', countryIsoCode, searchQueryMallCountryState);   

   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;
   }); 

   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('결제방법') + _i('(은)는 반드시 선택하셔야 합니다.'));
     $('#submit').css('visibility', ''); 
     return false;
   }

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

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

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

    }

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

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

    }

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

     alert(_i('입금예정일') + _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());   
   if($('#tel_1').val()&&$('#tel_2').val()&&$('#tel_3').val()) $('#tel').val($('#tel_1').val() + '-' + $('#tel_2').val() + '-' + $('#tel_3').val());   
   if($('#hp_1').val()&&$('#hp_2').val()&&$('#hp_3').val()) $('#hp').val($('#hp_1').val() + '-' + $('#hp_2').val() + '-' + $('#hp_3').val());   

   if($('#to_mailUser').val()&&$('#to_mailHost').val()) $('#toMail').val($('#to_mailUser').val() + '@' + $('#to_mailHost').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());   
   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());   

   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'));

	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-required'); 

	 }

	}

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

     $('#toAddressList').html($('#addressList').html());
     $('#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');

    } 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');

    }

   } 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('');

   }

}

function changeTotalSettlePrice() {

  var totalSettlePrice = 0;
  var totalPrice = parseFloat($('#_totalPrice').val());
  var totalDeliveryPrice = parseFloat($('#_totalDeliveryPrice').val());
  var totalDeliveryPrice2 = ($('#globalDeliveryPrice2').val()!=undefined) ? 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')
   return false;

  }

  if(settleCash>totalPrice) {

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

  }

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

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

}

function changeUseGlobalDelivery(delivery) {

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

	 alert(_i('받을분 국가를 선택하세요.'));
     $('#globalDeliveryPrice').val('0');
     $('#globalDeliveryPrice2').val('0');
	 $('#globalDeliveryName')[0].checked = true;
	 return false;

    }

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

	 alert(_i('받을분 지역을 선택하세요.'));
     $('#globalDeliveryPrice').val('0');
     $('#globalDeliveryPrice2').val('0');
	 $('#globalDeliveryName')[0].checked = true;
	 return false;

    }

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

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

    $.ajax({
       url: '/mall/order-delivery-option',
       data: 'delivery=' + delivery +  '&countryIsoCode=' + $('#toCountryList').val() + '&stateCode=' + $('#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');
          $('#globalDeliveryName')[0].checked = true;
	      $('.localDelivery').css('display', '');
	      return false;

         } 

         $('#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($('#globalDeliveryPrice2').val())), ($('#useDecimal').val() ? 2 : 0)));
	$('.localDelivery').css('display', '');
    changeTotalSettlePrice();

   }

}
