var $j = jQuery.noConflict();

jQuery(function() {
	jQuery("#keyword").keypress(function(){
		Creation.postalCodeLookupKR.search();
	}).change(function(){
		Creation.postalCodeLookupKR.search();
	});
});

var Creation = {
	postalCodeLookupKR: {
         search: function(address) {

			$j("#ZipcodeListBox").empty();
			var address_string = '';
			var address_html = '';
			var ZipcodeListBox = document.getElementById('ZipcodeListBox');

			 //[CDATA[
			 var keyword = $j("#keyword").val();
			var urlJSON="/store/data/search-zipcode.html";
			 $j.getJSON(urlJSON, {keyword:encodeURIComponent(keyword)},  function(data){
						$j("#ZipcodeListBox").empty();
    	 		 		$j.each(data, function(index, entry){
    	 		 				$j.each(entry, function(index2, inner_entry){
									address_string = '<a href="#"  onclick="Creation.postalCodeLookupKR.setAddress(' + inner_entry.zip + ',\'' + inner_entry.siDo +'\'' + ',\'' + inner_entry.siGunGu +'\'' + ',\''  +inner_entry.eupMyeonDong +'\',\'' + inner_entry.massDelivery+'\'); return false;">';
    	 		 					if (inner_entry.siDo != null)
    	 		 						address_string += inner_entry.siDo + ' ';
    	 		 					if (inner_entry.siGunGu != null)
    	 		 						address_string += inner_entry.siGunGu + ' ';
    	 		 					if (inner_entry.eupMyeonDong != null)
    	 		 						address_string += inner_entry.eupMyeonDong + ' ';
									if (inner_entry.ri != null)
    	 		 						address_string += inner_entry.ri + ' ';
    	 		 					if (inner_entry.massDelivery != null)
    	 		 						address_string += inner_entry.massDelivery + ' ';
    	 		 					if (inner_entry.bunji != null)
    	 		 						address_string += inner_entry.bunji + ' ';
    	 		 					if (inner_entry.zip != null)
    	 		 						address_string += '(' + inner_entry.zip + ')';
									address_string += '</a><br/>';
									address_html += address_string;
    	 		 				});
								ZipcodeListBox.innerHTML  = address_html;
    	 		 		});
    	 	 });
			//]]
         },
		setAddress: function(zipcode,siDo,siGunGu, eupMyeonDong, massDelivery) {

			 var address = siDo;
			 if (siGunGu != 'undefined'){
				address = address + ' ' + siGunGu;
			 }
			 if (eupMyeonDong != 'undefined'){
				address = address + ' ' + eupMyeonDong;
			 }
			  if (massDelivery != 'undefined'){
				address = address + ' ' + massDelivery;
			 }
			$j('.zip').val(zipcode);
			$j('.address1').val(address);
			$j('#zipcodePopup').hide();
			$j('#keyword').val('');
			$j('#ZipcodeListBox').empty();
         }
	}
}

