// 내부 검색인가 외부 검색인가 확인
function isAccessServer( ePosition )
{

	var retReadDomain;

	if ( !isUndefined(ePosition) )
	{
		alert("ePosition이 존재하지 않습니다.");
		return false;
	}

	ePosition = ePosition.replace(" ","");	 // 공백 제거
	
	if ( ePosition.indexOf("#") > -1 )
	{
		var epinfo = ePosition.split("#");
		var epdomain = epinfo[1]; // 사용자 입력 도메인 또는 URL
	}
	else if ( ePosition.indexOf("{@}") > -1 )
	{
		var epinfo = ePosition.split("{@}");
		var epdomain = epinfo[1]; // 사용자 입력 도메인 또는 URL
	}
	else
	{
		var epinfo = new Array(2);
		epinfo[0] = ePosition;
		epinfo[1] = "";
		var epdomain = ""; // 사용자 입력 도메인 또는 URL
	}

	var retInfo = new Array(2);

	if ( isUndefined( epdomain ) )
	{

		// 리얼 도메인인 경우 처리
		if ( domainClassDistinction( epdomain ) )
		{
			retReadDomain = "http://world.epmap."+epdomain+"/";
		}
		// 키워드 도메인인 경우 처리
		else
		{
			if ( !isepKeywordDomain( epdomain ) && epdomain != "82" )
			{
				alert("올바르지 못한 키워드 도메인입니다.");
				return false;
			}
			else
			{
				// 외부 도메인 정보 로딩
				xmlDomainData=new remoteDomainIndexLoad( encodeURIComponent(epdomain) );

				if ( !isUndefined( xmlDomainData.durl[0] ) )
				{
					if ( isUndefined( xmlDomainData.reason ) )
					{
						alert(xmlDomainData.reason);
					}
					else
					{
						alert("서비스 되지 않는 도메인입니다.");
					}
					return false;
				}
				else
				{
					retReadDomain = xmlDomainData.durl[0];
				}
			}
		}

	}
	else
	{
		retReadDomain = "http://egosio.epmap.egosio.com/";
	}

	return retReadDomain+"d/";

}
// 도메인이 리얼 도메인인가 아니면 키워드 도메인인가 판단. : 리얼 도메인인경우 true 반환
function domainClassDistinction( domainName )
{
	var strArray = domainName.split(".");
	var reg;
	if ( strArray.length < 2 ) return false;
	for( i=0; i < strArray.length; i++ )
	{
		if ( !isUndefined(strArray[i]) ) return false;
		if ( i == 0 )
		{
			reg = /^[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,}[a-zA-Z0-9]{1}$/;
		}
		else
		{
			reg = /^[a-zA-Z0-9]{2,}$/;
		}
		if ( !reg.test(strArray[i]) ) return false;
	}
	return true;
}

// ePosition 용 한글 키워드 도메인이 맞는가 확인
function isepKeywordDomain( domainName )
{
	// 한글을 포함하고 있는가( 반드시 한글자 이상 포함하여야 한다. )
	//if ( isInclusionKorean( domainName ) < 1 ) return false;
	// 도메인 규칙에 위배되지 않는가.
	if ( !isCorrectlyKeywordDomain ) return false;
	return true;
}
// 한글포함 여부 체크 : 한글 포함 갯수를 반환한다.
function isInclusionKorean(tmp) {
	var cnt=0;
	for(i=0;i<tmp.length;i++) {
		temp = tmp.charAt(i);
		if(temp.charCodeAt() >= 44032 && temp.charCodeAt() <= 55203){
			++cnt;
		}
	}
	return cnt;
}
// 도메인 규칙에 위배되는가?
function isCorrectlyKeywordDomain( domainName )
{
    if(domainName.length == 0) { return false; }
	// 특문이 들어가 있으면 실패
    for ( i=0;i<domainName.length;i++ )
    {
        if ((domainName.charAt(i) >= ' ' && domainName.charAt(i) <= '+') ||
            (domainName.charAt(i) == '/') ||
            (domainName.charAt(i) >= ':' && domainName.charAt(i) <= '@') ||
            (domainName.charAt(i) >= '[' && domainName.charAt(i) <= '`') ||
            (domainName.charAt(i) >= '{' && domainName.charAt(i) <= '~'))
        {
            return false;
        }
        if ((i == 0 && domainName.charAt(i) == '-') ||
            (i == (domainName.length-1) && domainName.charAt(i) == '-') ){
            return false;
        }
    }
	// 두글자 이하이면 실패
	if ( domainName.length < 2 ) { return false; }
	return true;
}

/*--------------------------------------------------------------
                     XML Control
---------------------------------------------------------------*/
var xmlDoc; 
// xmlhttprequest 연결을 만듬
function makeXmlHttpRequest(qurl)
{
	var xtr;
	try{
		xtr=new ActiveXObject("Msxml2.XMLHTTP.3.0");
	}
	catch(e){
		try{
			xtr=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc){
			xtr=null;
		}
	}

	if(! xtr&&typeof XMLHttpRequest!="undefined")
		xtr=new XMLHttpRequest();

	xtr.open("GET",qurl,false);
	//location.href=qurl;
	//return false;
	xtr.send(null);
	
	xmlDoc =  xtr.responseXML.documentElement;
	

}

// 외부 도메인의 리얼 도메인 찾기
function remoteDomainIndexLoad(qd)
{ 
	try
	{

		makeXmlHttpRequest("/xml/remote_domain.php?qd="+qd);
		//window.open("/xml/remote_domain.php?qd="+qd)
		// 인덱스 값 정리하기
		cnt=xmlDoc.getElementsByTagName("index").length;
		this.dname=new Array(cnt);
		this.dflag=new Array(cnt);
		this.durl=new Array(cnt);

		if(navigator.appName == "Netscape")
		{
			for(i=0,n=1;i<cnt;i++,n+=2)
			{
				this.dname[i]=xmlDoc.childNodes.item(i).getAttribute("domainname");
				this.dflag[i]=xmlDoc.childNodes.item(i).getAttribute("flag");
				this.durl[i]=xmlDoc.childNodes.item(i).getAttribute("ref_url");
			}
		}
		else
		{
			for(i=0;i<cnt;i++)
			{
				this.dname[i]=xmlDoc.childNodes.item(i).getAttribute("domainname");
				this.dflag[i]=xmlDoc.childNodes.item(i).getAttribute("flag");
				this.durl[i]=xmlDoc.childNodes.item(i).getAttribute("ref_url");
			}
		}

	}
	catch(e)
	{
		this.dname=new Array(1);
		this.dflag=new Array(1);
		this.durl=new Array(1);
		this.cnt=0;
		this.dname[0]="";
		this.dflag[0]="";
		this.durl[0]="";
		this.reason="잠시후에 다시 시도해주세요.";
	}
}