function AddressSelector( id , url ) { if( !id ) return; this.id = id; this.panel = document.getElementById( id ); if( !this.panel ) return; var ipts = this.panel.getElementsByTagName( 'input' ); for( var i = 0 ; i < ipts.length ; i++ ) { if( ipts[ i ].getAttribute( 'name' ) == 'location' ) { this.dest = ipts[ i ]; } } if( !this.dest ) { this.dest = document.createElement( 'input' ); this.dest.type = 'hidden'; this.dest.name = 'location'; this.dest.value = ''; this.panel.appendChild( this.dest ); } else { if( this.dest.value ) { var value = this.dest.value.split( '/' ); this.value1 = value[ 0 ]; this.value2 = value[ 1 ]; } } this.url = url; this.request = null; try { this.request = new XMLHttpRequest(); if( !this.request ) throw false; } catch( e ) { this.request = new ActiveXObject( 'Msxml2.XMLHTTP' ); if( !this.request ) this.request = new ActiveXObject( 'Microsoft.XMLHTTP' ); } this.target1 = document.getElementById( 'sido' ); this.addEvent( this.target1 , 'change' , this.getGugun ); this.addEvent( this.target1 , 'change' , this.setValue ); this.target2 = document.getElementById( 'gugun' ); this.addEvent( this.target2 , 'change' , this.setValue ); this.getSido(); } AddressSelector.prototype.setValue = function () { if( !this.dest ) return; var adr = ''; this.dest.value = adr; if( this.target1 ) adr = this.target1.value if( this.target1.value && this.target2 ) if( this.target2.value ) adr += '/' + this.target2.value this.dest.value = adr; } AddressSelector.prototype.getSido = function() { this.request.open( "GET" , this.url , true ); this.request.onreadystatechange = this.binding( this.setSido ); this.request.send(null); } AddressSelector.prototype.getGugun = function() { for( var i = ( this.target2.options.length -1 ) ; i >= 1 ; i-- ) { this.target2.removeChild( this.target2.options[ i ] ); } if( !this.target1.value ) return; var params = 'sido=' + encodeURIComponent( this.target1.value ); this.request.open( "GET" , this.url + '?' + params , true ); this.request.onreadystatechange = this.binding( this.setGugun ); this.request.send(null); } AddressSelector.prototype.setSido = function() { if( ( this.request.readyState == 4 ) && ( this.request.status == 200 ) ) { var txt = this.request.responseText; if( !txt ) return; var returns = txt.split( "\n" ); for( var i = 0 , d = null , n = null ; i < returns.length ; i++ ) { d = returns[ i ]; if( !d ) continue; var opt = document.createElement( 'option' ); opt.value = d; if( this.value1 == d ) { opt.selected = true; this.value1 = null; } opt.appendChild( document.createTextNode( d ) ); this.target1.appendChild( opt ); } if( this.value2 ) this.getGugun(); } } AddressSelector.prototype.setGugun = function() { if( ( this.request.readyState == 4 ) && ( this.request.status == 200 ) ) { var txt = this.request.responseText; if( !txt ) return; var returns = txt.split( "\n" ); for( var i = 0 , d = null , n = null ; i < returns.length ; i++ ) { d = returns[ i ]; if( !d ) continue; var opt = document.createElement( 'option' ); opt.value = d; if( this.value2 == d ) { opt.selected = true; this.value2 = null; } opt.appendChild( document.createTextNode( d ) ); this.target2.appendChild( opt ); } } } AddressSelector.prototype.binding = function() { for( var i = 0 , args = [] ; i < arguments.length ; i++ ) args.push( arguments[ i ] ); if( args.length < 1 ) return undefined; var func = args.shift(); var thisObj = this; if( typeof( func ) != 'function' ) return undefined; return function() { return func.apply( thisObj , args ); }; } AddressSelector.prototype.bindEvent = function() { for( var i = 0 , args = [] ; i < arguments.length ; i++ ) args.push( arguments[ i ] ); if( args.length < 1 ) return undefined; var func = args.shift(); var thisObj = this; if( typeof( func ) != 'function' ) return undefined; return function() { var evt = ( ( arguments.length > 0 ) ? arguments[0] : window.event ); return func.apply( thisObj , [ evt ].concat( args ) ); }; } AddressSelector.prototype.addEvent = function( src , evtType , func , args ) { if( src.attachEvent ) { src.attachEvent( 'on' + evtType , this.bindEvent( func , args ) ); } else { src.addEventListener( evtType , this.bindEvent( func , args ) , false ); } }