﻿// JScript 파일
function zr() {
   this.hl = document.getElementsByTagName("head").item(0);
   this.sc = 0;
   this.sa = new Array();
}
zr.prototype.ast = function (f) {
    var now = new Date()
    this.noCacheIE = "&noCacheIE="+ now.getTime();
    this.fullUrl = f;
    this.scriptId = "YJscriptId" + this.sc++;
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
    this.hl.appendChild(this.scriptObj);
    this.sa.push(this.scriptObj);
}
zr.prototype.rst = function() {
    this.sc--;
    if(this.sc == 0){
        while(this.sa.length){
            this.hl.removeChild(this.sa.pop());
        }
    }
}
//주소를 좌표로 바꾸는 부분 입니다.
function getJibun(_form) {
    if(_form.sido.value == "") {
        alert("광역/시/도를 선택해주세요");
        _form.sido.focus();
        return false;
    } else if(_form.sigungu.value == "") {
        alert("시/군/구를 선택해주세요");
        _form.sigungu.focus();
        return false;
    } else if(_form.dong.value == "") {
        alert("읍/면/동을 선택해주세요");
        _form.dong.focus();        
        return false;
    }
    var bunji = _form.bunji.value;
    var ho = "";
    if(bunji.indexOf("-") >= 0) {
        ho = bunji.substring(bunji.indexOf("-") + 1);
        bunji = bunji.substring(0, bunji.indexOf("-"));
    }
    
    var getUrl = "http://maps.chzero.com/GetJibun_UTF8.jsp?zipcode=" + encodeURIComponent("") + "&sido=" + encodeURIComponent(_form.sido.value);
    getUrl += "&gun=" + encodeURIComponent(_form.sigungu.value);
    getUrl += "&dong=" + encodeURIComponent(_form.dong.value);
    getUrl += "&bunji=" + encodeURIComponent(bunji);
    getUrl += "&ho=" + ho;
    getUrl += "&call=setJibun";
    g_zr.ast(getUrl);
    return false;
}


function getJibunV2() {

    var bunji = document.getElementById("addr2").value;
    var ho = "";
    if(bunji.indexOf("-") >= 0) {
        ho = bunji.substring(bunji.indexOf("-") + 1);
        bunji = bunji.substring(0, bunji.indexOf("-"));
    }
    
    var getUrl = "http://maps.chzero.com/GetJibun_UTF8.jsp?zipcode=" + encodeURIComponent("") + "&sido=" + encodeURIComponent(document.getElementById("sido").value);
    getUrl += "&gun=" + encodeURIComponent(document.getElementById("sigungu").value);
    getUrl += "&dong=" + encodeURIComponent(document.getElementById("dong").value);
    getUrl += "&bunji=" + encodeURIComponent(bunji);
    getUrl += "&ho=" + ho;
    getUrl += "&call=setJibunV2";
    g_zr.ast(getUrl);
    return false;
}

/* 원본 */
function setJibun(jData) {
	var Head = jData.JIBUNDATA[0].Head;
	var Body = jData.JIBUNDATA[1].Body;
	
	//alert(Head.Count + " " +  Head.Result);
	//Count 해당 입력으로 몇개의 지번을 찾았는지
	//Result 정확도 (2:정확 , 1:근처 , 0:찾을수 없다);
	if(Head.Result == 0) {
	    alert("위치를 찾을 수 없습니다");
	    return;
	} else if(Head.Result == 1) {
	    alert("근처 위치를 찾았습니다");
	}
	
	
	//alert(Head.Count);
	
	for(var n = 0 ; n < Head.Count ; n++)
	{
		var strFile = Body[n].Sido + " "; //시도
		strFile += Body[n].Gun + " "; //군
		strFile += Body[n].Gu + " "; //구
		strFile += Body[n].Dong + " "; //대표 동이름
		strFile += Body[n].Adm + " "; //행정동 이름
		strFile += Body[n].Law + " "; //법정동 이름
		strFile += Body[n].Li + " "; //리 명칭
		strFile += " " + Body[n].Bunji + "-"; //번지
		strFile += Body[n].Ho + " "; //호
		strFile += Body[n].X + " "; //좌표 X
		strFile += Body[n].Y; //좌표 Y
		
		alert(strFile);
		if(Head.Count==1&&Head.Result!=0)
		{
		    document.getElementById("hid_x").value = Body[n].X;
            document.getElementById("hid_y").value = Body[n].Y;
            
            //alert(strFile);
		}
		else
		{
		    document.getElementById("hid_x").value = '';
            document.getElementById("hid_y").value = '';
		}
		
	}
}


/* 2008-06-22 setJibun 펑션으로 수정 */
function setJibunV2(jData) {
	var Head = jData.JIBUNDATA[0].Head;
	var Body = jData.JIBUNDATA[1].Body;
	
	//alert(Head.Count + " " +  Head.Result);
	//Count 해당 입력으로 몇개의 지번을 찾았는지
	//Result 정확도 (2:정확 , 1:근처 , 0:찾을수 없다);
	if(Head.Result == 0) {
	    alert("위치를 찾을 수 없습니다");
	    return;
	} else if(Head.Result == 1) {
	    alert("근처 위치를 찾았습니다");
	}
	
	
	//alert(Head.Count);
	var arr_addr = new Array();
	for(var n = 0 ; n < Head.Count ; n++)
	{
	    var fulladdr = "";
	    
		var strFile = Body[n].Sido + " "; //시도
		
		if(Body[n].Sido!="")
		{
		    fulladdr+= Body[n].Sido;
		}
				
		strFile += Body[n].Gun + " "; //군
		if(Body[n].Gun!="")
		{
		    fulladdr+= " "+Body[n].Gun;
		}
		
		
		strFile += Body[n].Gu + " "; //구
		if(Body[n].Gu!="")
		{
		    fulladdr+= " "+Body[n].Gu;
		}
		
		strFile += Body[n].Dong + " "; //대표 동이름
		strFile += Body[n].Adm + " "; //행정동 이름
		if(Body[n].Adm!="")
		{
		    fulladdr+= " "+Body[n].Adm;
		}
		
		
		
		strFile += Body[n].Law + " "; //법정동 이름
		strFile += Body[n].Li + " "; //리 명칭
		if(Body[n].Li!="")
		{
		    fulladdr+= " "+Body[n].Li;
		}
		
		
		strFile += " " + Body[n].Bunji + "-"; //번지
		if(Body[n].Bunji!="")
		{
		    fulladdr+= " "+Body[n].Bunji;
		}
		
		
		strFile += Body[n].Ho + " "; //호
		if(Body[n].Ho!="")
		{
		    fulladdr+= " "+Body[n].Ho;
		}
		
		
		strFile += Body[n].X + " "; //좌표 X
		strFile += Body[n].Y; //좌표 Y
		if(Body[n].X!=""&&Body[n].Y!="")
		{
		    fulladdr+= "↑"+Body[n].X+","+Body[n].Y;
		}
		else
		{
		    fulladdr+= "↑";
		}
		
		
		
//		arr_addr[n] = fulladdr;
//		
//		alert(strFile);
//		if(Head.Count==1&&Head.Result!=0)
//		{
//        if(n==0)
//        {
		    document.getElementById("hid_x").value = Body[n].X;
            document.getElementById("hid_y").value = Body[n].Y;
            
            
//        }

        //alert("fulladdr : "+fulladdr+" strFile : "+strFile+" x,y 좌표 : "+document.getElementById("hid_x").value+","+document.getElementById("hid_y").value);
            
            //alert(strFile);
//		}
//		else
//		{
//		    document.getElementById("hid_x").value = '';
//            document.getElementById("hid_y").value = '';
//		}
		
	}
	var tmpVal = "";
	for(var n = 0 ; n < arr_addr.length ; n++)
	{
	    tmpVal += arr_addr[n]+"\r\n";
	}
	//alert(tmpVal);
}




//좌표를 주소로
function getAddr(x,y) {
     
    var getUrl = "http://maps.chzero.com/GetAddr_UTF8.jsp?x=" + x + "&y=" + y;
    getUrl += "&call=setAddr";
    g_zr.ast(getUrl);
    
    return ;
}
function setAddr(jData) {
	alert(jData.find)
	alert(jData.sido)
	alert(jData.sigungu)
	alert(jData.gu)
	alert(jData.adm_emd)
	alert(jData.law_emd)
	alert(jData.law_li)
}

//좌표를 주소로
function getAddrV2(x,y,key) {
     
    var getUrl = "http://maps.chzero.com/GetAddr_UTF8.jsp?x=" + x + "&y=" + y;
    
    getUrl += "&call=setAddrV2";
    
    
    g_zr.ast(getUrl);
    
    return ;
}

var txtbox = ['',''];

var addr_temp = "";
function setAddrV2(jData) {
//	alert(jData.find);
//	alert(jData.sido);
//	alert(jData.sigungu);
//	alert(jData.gu);
//	alert(jData.adm_emd);
//	alert(jData.law_emd);
//	alert(jData.law_li);
	
	addr_temp = "";
	if(jData.find==1)
	{
	    if(jData.sido!="")
	    {
	        addr_temp += jData.sido;
	    }
	    if(jData.sigungu!="")
	    {
	        addr_temp += " "+jData.sigungu;
	    }
	    if(jData.gu!="")
	    {
	        addr_temp += " "+jData.gu;
	    }
	    if(jData.law_emd!="")
	    {
	        addr_temp += " "+jData.law_emd;
	    }
	    if(jData.law_li!="")
	    {
	        addr_temp += " "+jData.law_li;
	    }
	    //alert(addr_temp);
	}
	//document.getElementById('spAddr').innerHTML = addr_temp;
	document.getElementById(txtbox[0]).value = addr_temp;
	//document.getElementById(txtbox[1]).innerHTML = addr_temp;
	document.getElementById(txtbox[1]).value = addr_temp;
	
	
}

/* gis 팝업창 */
// 페이지주소,좌표X,좌표Y,스케일:기본1줍니다,넓이,높이,포스트idx,블로그코드
function WinOpenGis(gourl,mapx,mapy,scale,_width,_height,postidx,blogcode)
{
    
    
    var realDomain = "http://gis.imcook.imbc.com/";
    // map1_1.aspx','" + dr3["longitude"].ToString() + "','" + dr3["latitude"].ToString() + "','1',1024,768,'"+dr["postidx"].ToString()+"','"+this.blogcode+"');";
    // http:///gis/map1_1.aspx?mapx=" + dr3["longitude"].ToString() + "&mapy=" + dr3["latitude"].ToString() + "&scale=1
    var sw = screen.availWidth;
    var sh = screen.availHeight;
    
    var px = (sw-_width)/2;
    var py = (sh-_height)/2;
    if(gourl.indexOf("?") > 0)
    {
        gourl = realDomain+gourl+"&mapx="+mapx+"&mapy="+mapy+"&scale="+scale+"&postidx="+postidx+"&logtidx="+blogcode;
    }
    else
    {
        gourl = realDomain+gourl+"?mapx="+mapx+"&mapy="+mapy+"&scale="+scale+"&postidx="+postidx+"&logtidx="+blogcode;
    }
    
    //alert(gourl);
    //WinOpenPop = window.open(gourl,'PopUpOpenGis','width='+_width+',height='+_height+',top='+py+',left='+px+',status=no,scrollbars=no');
    WinOpenPop = window.open(gourl,'PopUpOpenGis','');
}

function Request2(valuename)  
{
    //document.domain = 'imcook.imbc.com';
    var rtnval = "";
    var nowAddress = unescape(document.location.href);
    var parameters = (nowAddress.slice(nowAddress.indexOf("?")+1,nowAddress.length)).split("&");
    
    for(var i = 0 ; i < parameters.length ; i++){
        var varName = parameters[i].split("=")[0];
        if(varName.toUpperCase() == valuename.toUpperCase())
        {
            rtnval = parameters[i].split("=")[1];
            break;
        }
    }
    return rtnval;
}
/* 좌표 찾기및 찍기 */
function WinOpenGisSearch(gourl,_width,_height)
{
    //document.domain = 'imcook.imbc.com';
    var realDomain = "http://gis.imcook.imbc.com/";
    // map1_1.aspx','" + dr3["longitude"].ToString() + "','" + dr3["latitude"].ToString() + "','1',1024,768,'"+dr["postidx"].ToString()+"','"+this.blogcode+"');";
    // http:///gis/map1_1.aspx?mapx=" + dr3["longitude"].ToString() + "&mapy=" + dr3["latitude"].ToString() + "&scale=1
    var sw = screen.availWidth;
    var sh = screen.availHeight;
    
    var px = (sw-_width)/2;
    var py = (sh-_height)/2;
    
    if(gourl.indexOf("?") > 0)
    {
        gourl += "&Logcode="+Request2("Logcode");
    }
    else
    {
        gourl += "?Logcode="+Request2("Logcode");
    }
    var SearchName = "맛";
    gourl = realDomain+gourl+"&SearchName="+encodeURIComponent(SearchName);
    
    WinOpenPop = window.open(gourl,'PopUpOpenGisSearch','width='+_width+',height='+_height+',top='+py+',left='+px+',status=no,scrollbars=no');
    //WinOpenPop = window.open(gourl,'PopUpOpenGisSearch','');
}

function WinOpenGisSearchV2(gourl,_width,_height,SearchName)
{
    //document.domain = 'imcook.imbc.com';
    var realDomain = "http://gis.imcook.imbc.com/";
    // map1_1.aspx','" + dr3["longitude"].ToString() + "','" + dr3["latitude"].ToString() + "','1',1024,768,'"+dr["postidx"].ToString()+"','"+this.blogcode+"');";
    // http:///gis/map1_1.aspx?mapx=" + dr3["longitude"].ToString() + "&mapy=" + dr3["latitude"].ToString() + "&scale=1
    var sw = screen.availWidth;
    var sh = screen.availHeight;
    
    var px = (sw-_width)/2;
    var py = (sh-_height)/2;
    
    if(gourl.indexOf("?") > 0)
    {
        gourl += "&Logcode="+Request2("Logcode");
    }
    else
    {
        gourl += "?Logcode="+Request2("Logcode");
    }
    //var SearchName = "맛";
    gourl = realDomain+gourl+"&SearchName="+SearchName;//encodeURIComponent(SearchName);
    
    WinOpenPop = window.open(gourl,'PopUpOpenGisSearch','width='+sw+',height='+sh+',top='+py+',left='+px+',status=no,scrollbars=yes, resizable=yes');
    //WinOpenPop = window.open(gourl,'PopUpOpenGisSearch','');
}


