    var CongAddressData = function() {
		this.resultCode = null;
		this.code = null;
		this.x = null;
		this.y = null;
		this.address = null;
		this.toString = function () {
			return "[result="+this.resultCode+",x="+this.x+",y="+this.y+",code="+this.code+",addr="+this.address+"]";
	    };
	}
    
    var CongAddressService = function() {
    		//this.setProxy("/congsoa/clientproxy.jsp", "http://biz.congnamul.com");
		this.setProxy("/Web/Map/MapProxy.aspx", "http://biz.congnamul.com");
		this.serviceURL = '/congsoa/address/';
		this.regionCodeService = new CongRegionCodeService("ADDR");
    }
    
    CongClass.extend(CongAddressService,CongBasicService);
    CongClass.extend(CongAddressService,CongAjaxClass);
    
	CongAddressService.prototype.binding = null,
	CongAddressService.prototype.onValidateError = null,
	CongAddressService.prototype.regionCodeService = null;
	CongAddressService.prototype.options = ['','SELECT',-1];
	CongAddressService.prototype.onchange = null;
	
    CongAddressService.prototype.bind = function(id) {
        try {
            var binding = new Object();
            binding.city = document.getElementById(id+".CITY");;
            binding.gu = document.getElementById(id+".GU");
            binding.dong = document.getElementById(id+".DONG");
            if(document.getElementById(id+".JIMOK").nodeName=='SELECT') {
               binding.jimok = document.getElementById(id+".JIMOK");
            } else {
               binding.jimok = document.getElementsByName(id+".JIMOK");
            }
            binding.jibun = document.getElementById(id+".JIBUN");
            this.binding = binding;
            if(this.options.length==3) {
	            this.regionCodeService.bind(binding.city,this.options[0],this.options[2],this.options[1],this.options[2]);
	            this.regionCodeService.bind(binding.gu,null,this.options[2],this.options[1],this.options[2]);
	            this.regionCodeService.bind(binding.dong,null,this.options[2],this.options[1],this.options[2]);            
            } else if(this.options.length==7) {
	            this.regionCodeService.bind(binding.city,this.options[0],this.options[2],this.options[1],this.options[2]);
	            this.regionCodeService.bind(binding.gu,null,this.options[4],this.options[3],this.options[4]);
	            this.regionCodeService.bind(binding.dong,null,this.options[6],this.options[5],this.options[6]);            
            } else if(this.options.length==9) {
                    this.regionCodeService.bind(binding.city,'',this.options[0],this.options[1],this.options[2]);
                    this.regionCodeService.bind(binding.gu     ,this.options[0],this.options[3],this.options[4],this.options[5]);
                    this.regionCodeService.bind(binding.dong   ,this.options[3],this.options[6],this.options[7],this.options[8]);
            }            
        } catch(err) {
            alert("binding err :"+err.message);
        }    
    }

    CongAddressService.prototype.getCity = function() {
          var value = null;
          if(this.binding) {
              var sel = this.binding.city;
              value = sel[sel.selectedIndex].text;
          } 
          return value;
    }    
    
    CongAddressService.prototype.getGu = function() {
          var value = null;
          if(this.binding.gu) {
              var sel = this.binding.gu;
              value = sel[sel.selectedIndex].text;
          } 
          return value;
    }    
    
    CongAddressService.prototype.getDong = function() {
          var value = null;
          if(this.binding.dong) {
              var sel = this.binding.dong;
              value = sel[sel.selectedIndex].text;
          } 
          return value;
    }    

    CongAddressService.prototype.getJimok = function() {
          var value = null;
          if(this.binding.jimok) {
              var sel = this.binding.jimok;
              if(this.binding.jimok.nodeName=='SELECT') {
                     value = sel[sel.selectedIndex].text;              
              }       
              else {
        	    	if(sel.length>1) {	    		
        		    	for(var i=0;i<sel.length;i++) {
        		    		if(sel[i].checked) {
        		    			value = sel[i].value;
        		    		}
        		    	}
        	    	} else {
        	    	   	 value = sel.value;
        	    	}
              }    
          } 
          return value;
    }
    
    CongAddressService.prototype.getJibun = function() {
          var value = null;
          if(this.binding.jibun) {
              value = this.binding.jibun.value;
          } 
          return value;
    }    
    
     CongAddressService.prototype.getCoordSystem = function() {
          var value = null;
          if(document.getElementById("coordSystem")){
          	value= document.getElementById("coordSystem").value; 
          }
          return value;
    }   

	CongAddressService.prototype.search = function (city,gu,dong,jimok,jibun,coordSystem) {
	        if(arguments.length==0) {
	            city  = this.getCity();
	            gu    = this.getGu();
	            dong  = this.getDong();
	            jimok = this.getJimok();
	            jibun = this.getJibun();
	            coordSystem = this.getCoordSystem();
	        }

			var url = this.serviceURL+"search.service";
			var query = new CongQueryString(true);
			query.add("CITY",city);
			query.add("GU",gu);
			query.add("DONG",dong);
			query.add("JIMOK",jimok);
			query.add("JIBUN",jibun);
			query.add("outputCoordSystem",coordSystem);
			
			var result  = this.getResultAttributes(url,query.toString(),['rescode','x','y','code','address']);
			var addressData = null;
			if(result && result.length>=5) { // Æ¿·Î¿¡¼­ ¼öÁ¤ : Origin => result.length == 5
    			addressData = new CongAddressData();
    			addressData.resultCode = result[0];
    			addressData.x = parseInt(result[1],10);
    			addressData.y = parseInt(result[2],10);
    			addressData.code = result[3];
    			addressData.address = result[4];
			}
			return addressData;
	}

	/*
	CongAddressService.prototype.requestMatch = function (addr) {
		var url = this.serviceURL+"match.service";
		var query = new CongQueryString(true);
		query.add("ADDR",addr);
		this.request(url,query.toString(),this.onResponseMatch);
	}
	
	CongAddressService.prototype.onResponseMatch = function(httprequest) {
			var addressData = null;
			var dom = httprequest.responseXML;
			if(dom!=null) {
    			addressData = new CongAddressData();
    			addressData.resultCode = dom.documentElement.getAttribute("rescode");
    			addressData.x = parseInt(dom.documentElement.getAttribute("x"),10);
    			addressData.x = parseInt(dom.documentElement.getAttribute("y"),10);
    			addressData.code = dom.documentElement.getAttribute("code");
    			addressData.address = dom.documentElement.getAttribute("address");
			}
			this.onMatch(addressData);
	}
	
	CongAddressService.prototype.onMatch = function(addressData) {
	}

	CongAddressService.prototype.requestMatchList = function (addr) {
		var url = this.serviceURL+"matchlist.service";
		var query = new CongQueryString(true);
		query.add("ADDR",addr);
		this.request(url,query.toString(),this.onResponseMatchList);
		
	}
	
	CongAddressService.prototype.onResponseMatchList = function(httprequest) {
			var list = null;
			if(httprequest!=null) {
				var dom = httprequest.responseXML;
				list = this.parseMatchList(dom);	
			}
			this.onMatchList(list);
			
	}
	CongAddressService.prototype.onMatchList = function(list) {
	}
	*/
	
	CongAddressService.prototype.match = function (addr, coordSystem) {
		var url = this.serviceURL+"match.service";
		var query = new CongQueryString(true);
		query.add("ADDR",addr);
		query.add("outputCoordSystem",coordSystem);
		
		var result  = this.getResultAttributes(url,query.toString(),['rescode','x','y','code','address']);
		var addressData = null;
		if(result && result.length>=5) { // Æ¿·Î¿¡¼­ ¼öÁ¤ : Origin => result.length == 5
    		addressData = new CongAddressData();
    		addressData.resultCode = parseInt(result[0],10);
    		addressData.x = parseInt(result[1],10);
    		addressData.y = parseInt(result[2],10);
    		addressData.code = result[3];
    		addressData.address = result[4];
		}
		return addressData;
	}
	
	CongAddressService.prototype.matchlist = function (addr, coordSystem) {
		var url = this.serviceURL+"matchlist.service";
		var query = new CongQueryString(true);
		query.add("ADDR",addr);
		query.add("outputCoordSystem",coordSystem);
		var dom  = this.getDom(url,query.toString());
		return this.parseMatchList(dom);		
	}
	
	CongAddressService.prototype.parseMatchList = function(dom) {
		var parser = new CongDomParserClass(dom);
		var result = null;
		var handler = {
			rootCode : null,
			process : function(pObj,depth,nodeName,node) {
				var resObj = null;
				if(depth==0) {
					resObj = new Array();
				} else if(depth==1) {
					var addressData = new CongAddressData();
					var rescode = node.getAttribute("rescode");
					var x = node.getAttribute("x");
					var y  = node.getAttribute("y");
					var code  = node.getAttribute("code");
					var address  = node.getAttribute("address");
		    		addressData.resultCode = parseInt(rescode,10);
		    		addressData.x = parseInt(x,10);		    		
		    		addressData.y = parseInt(y,10);
		    		addressData.code = code;
		    		addressData.address = address;
					resObj = addressData;
					pObj[pObj.length]= resObj;					
				} else if(depth==2)  {
				}
				return resObj;
			}
		}
		result = parser.parse(handler);
		return result;	
	}	
	
	CongAddressService.prototype.validateJibun = function(jibun) {	  
	    if(jibun!=null && jibun!='') {
	        if((jibun.search(/^[0-9]+-+[0-9]*$/) == -1 ) && (jibun.search(/^[0-9]*$/) == -1)){
                return false;
             }      
        }                   
        return true;         
	}
	